utilise arrow functions omfg i love ES6 holy shit

This commit is contained in:
Sebastian McKenzie
2015-02-25 22:52:22 +11:00
parent c13f8a3f50
commit ba11069b1c
7 changed files with 52 additions and 70 deletions

View File

@@ -40,16 +40,14 @@ exports.JSXExpressionContainer = function (node, print) {
};
exports.JSXElement = function (node, print) {
var self = this;
var open = node.openingElement;
print(open);
if (open.selfClosing) return;
this.indent();
each(node.children, function (child) {
each(node.children, (child) => {
if (t.isLiteral(child)) {
self.push(child.value);
this.push(child.value);
} else {
print(child);
}

View File

@@ -58,8 +58,6 @@ exports.ExportDeclaration = function (node, print) {
};
exports.ImportDeclaration = function (node, print) {
var self = this;
this.push("import ");
if (node.isType) {
@@ -70,16 +68,16 @@ exports.ImportDeclaration = function (node, print) {
if (specfiers && specfiers.length) {
var foundImportSpecifier = false;
each(node.specifiers, function (spec, i) {
each(node.specifiers, (spec, i) => {
if (+i > 0) {
self.push(", ");
this.push(", ");
}
var isDefault = t.isSpecifierDefault(spec);
if (!isDefault && spec.type !== "ImportBatchSpecifier" && !foundImportSpecifier) {
foundImportSpecifier = true;
self.push("{ ");
this.push("{ ");
}
print(spec);

View File

@@ -15,16 +15,15 @@ exports.TemplateLiteral = function (node, print) {
this.push("`");
var quasis = node.quasis;
var self = this;
var len = quasis.length;
each(quasis, function (quasi, i) {
each(quasis, (quasi, i) => {
print(quasi);
if (i + 1 < len) {
self.push("${ ");
this.push("${ ");
print(node.expressions[i]);
self.push(" }");
this.push(" }");
}
});

View File

@@ -58,23 +58,22 @@ exports.Property = function (node, print) {
exports.ArrayExpression =
exports.ArrayPattern = function (node, print) {
var elems = node.elements;
var self = this;
var len = elems.length;
this.push("[");
each(elems, function (elem, i) {
each(elems, (elem, i) => {
if (!elem) {
// If the array expression ends with a hole, that hole
// will be ignored by the interpreter, but if it ends with
// two (or more) holes, we need to write out two (or more)
// commas so that the resulting code is interpreted with
// both (all) of the holes.
self.push(",");
this.push(",");
} else {
if (i > 0) self.push(" ");
if (i > 0) this.push(" ");
print(elem);
if (i < len - 1) self.push(",");
if (i < len - 1) this.push(",");
}
});

View File

@@ -105,20 +105,18 @@ CodeGenerator.prototype.generate = function () {
};
CodeGenerator.prototype.buildPrint = function (parent) {
var self = this;
var print = function (node, opts) {
return self.print(node, parent, opts);
var print = (node, opts) => {
return this.print(node, parent, opts);
};
print.sequence = function (nodes, opts) {
print.sequence = (nodes, opts) => {
opts = opts || {};
opts.statement = true;
return self.printJoin(print, nodes, opts);
return this.printJoin(print, nodes, opts);
};
print.join = function (nodes, opts) {
return self.printJoin(print, nodes, opts);
print.join = (nodes, opts) => {
return this.printJoin(print, nodes, opts);
};
print.list = function (items, opts) {
@@ -127,12 +125,12 @@ CodeGenerator.prototype.buildPrint = function (parent) {
print.join(items, opts);
};
print.block = function (node) {
return self.printBlock(print, node);
print.block = (node) => {
return this.printBlock(print, node);
};
print.indentOnComments = function (node) {
return self.printAndIndentOnComments(print, node);
print.indentOnComments = (node) => {
return this.printAndIndentOnComments(print, node);
};
return print;
@@ -150,11 +148,9 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
this.format.concise = true;
}
var self = this;
opts = opts || {};
var newline = function (leading) {
var newline = (leading) => {
if (!opts.statement && !n.isUserWhitespacable(node, parent)) {
return;
}
@@ -164,9 +160,9 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
if (node.start != null && !node._ignoreUserWhitespace) {
// user node
if (leading) {
lines = self.whitespace.getNewlinesBefore(node);
lines = this.whitespace.getNewlinesBefore(node);
} else {
lines = self.whitespace.getNewlinesAfter(node);
lines = this.whitespace.getNewlinesAfter(node);
}
} else {
// generated node
@@ -178,10 +174,10 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
if (needs(node, parent)) lines++;
// generated nodes can't add starting file whitespace
if (!self.buffer.buf) lines = 0;
if (!this.buffer.buf) lines = 0;
}
self.newline(lines);
this.newline(lines);
};
if (this[node.type]) {
@@ -224,28 +220,27 @@ CodeGenerator.prototype.printJoin = function (print, nodes, opts) {
opts = opts || {};
var self = this;
var len = nodes.length;
var len = nodes.length;
if (opts.indent) self.indent();
if (opts.indent) this.indent();
each(nodes, function (node, i) {
each(nodes, (node, i) => {
print(node, {
statement: opts.statement,
addNewlines: opts.addNewlines,
after: function () {
after: () => {
if (opts.iterator) {
opts.iterator(node, i);
}
if (opts.separator && i < len - 1) {
self.push(opts.separator);
this.push(opts.separator);
}
}
});
});
if (opts.indent) self.dedent();
if (opts.indent) this.dedent();
};
CodeGenerator.prototype.printAndIndentOnComments = function (print, node) {
@@ -289,14 +284,13 @@ CodeGenerator.prototype.getComments = function (key, node, parent) {
var comments = [];
var nodes = [node];
var self = this;
if (t.isExpressionStatement(node)) {
nodes.push(node.argument);
}
each(nodes, function (node) {
comments = comments.concat(self._getComments(key, node));
each(nodes, (node) => {
comments = comments.concat(this._getComments(key, node));
});
return comments;
@@ -312,13 +306,11 @@ CodeGenerator.prototype._printComments = function (comments) {
if (!this.format.comments) return;
if (!comments || !comments.length) return;
var self = this;
each(comments, function (comment) {
each(comments, (comment) => {
var skip = false;
// find the original comment in the ast and set it as displayed
each(self.ast.comments, function (origComment) {
each(this.ast.comments, function (origComment) {
if (origComment.start === comment.start) {
// comment has already been output
if (origComment._displayed) skip = true;
@@ -331,38 +323,38 @@ CodeGenerator.prototype._printComments = function (comments) {
if (skip) return;
// whitespace before
self.newline(self.whitespace.getNewlinesBefore(comment));
this.newline(this.whitespace.getNewlinesBefore(comment));
var column = self.position.column;
var val = self.generateComment(comment);
var column = this.position.column;
var val = this.generateComment(comment);
if (column && !self.isLast(["\n", " ", "[", "{"])) {
self._push(" ");
if (column && !this.isLast(["\n", " ", "[", "{"])) {
this._push(" ");
column++;
}
//
if (comment.type === "Block" && self.format.indent.adjustMultilineComment) {
if (comment.type === "Block" && this.format.indent.adjustMultilineComment) {
var offset = comment.loc.start.column;
if (offset) {
var newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
val = val.replace(newlineRegex, "\n");
}
var indent = Math.max(self.indentSize(), column);
var indent = Math.max(this.indentSize(), column);
val = val.replace(/\n/g, "\n" + repeating(" ", indent));
}
if (column === 0) {
val = self.getIndent() + val;
val = this.getIndent() + val;
}
//
self._push(val);
this._push(val);
// whitespace after
self.newline(self.whitespace.getNewlinesAfter(comment));
this.newline(this.whitespace.getNewlinesAfter(comment));
});
};

View File

@@ -370,8 +370,6 @@ File.prototype.addCode = function (code) {
};
File.prototype.parse = function (code) {
var self = this;
code = this.addCode(code);
var opts = this.opts;
@@ -379,9 +377,9 @@ File.prototype.parse = function (code) {
opts.allowImportExportEverywhere = this.isLoose("es6.modules");
opts.strictMode = this.transformers.useStrict.canRun();
return parse(opts, code, function (tree) {
self.transform(tree);
return self.generate();
return parse(opts, code, (tree) => {
this.transform(tree);
return this.generate();
});
};

View File

@@ -40,18 +40,16 @@ function Transformer(key, transformer, opts) {
}
Transformer.prototype.normalize = function (transformer) {
var self = this;
if (isFunction(transformer)) {
transformer = { ast: transformer };
}
traverse.explode(transformer);
each(transformer, function (fns, type) {
each(transformer, (fns, type) => {
// hidden property
if (type[0] === "_") {
self[type] = fns;
this[type] = fns;
return;
}