microoptimize module formatters, change method names to a new API, and add support for exporting multiple variable declarators - fixes #299

This commit is contained in:
Sebastian McKenzie
2014-12-15 22:34:49 +11:00
parent e01e010577
commit a03d491ac6
14 changed files with 40 additions and 32 deletions

View File

@@ -3,15 +3,14 @@ module.exports = DefaultFormatter;
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
var _ = require("lodash");
function DefaultFormatter(file) {
this.exports = [];
this.file = file;
var localExports = [];
_.each(file.ast.program.body, function (node) {
var declar = node.declaration;
traverse(file.ast, function (node) {
var declar = node && node.declaration;
if (t.isExportDeclaration(node) && declar && t.isStatement(declar)) {
localExports = localExports.concat(t.getIds(declar));
}
@@ -29,7 +28,7 @@ DefaultFormatter.prototype.remapAssignments = function () {
if (t.isAssignmentExpression(node)) {
var left = node.left;
if (t.isIdentifier(left) && _.contains(localExports, left.name)) {
if (t.isIdentifier(left) && localExports.indexOf(left.name) >= 0) {
return t.assignmentExpression(
"=",
left,
@@ -119,7 +118,7 @@ DefaultFormatter.prototype._exportSpecifier = function (getRef, specifier, node,
}
};
DefaultFormatter.prototype.export = function (node, nodes) {
DefaultFormatter.prototype.exportDeclaration = function (node, nodes) {
var declar = node.declaration;
if (node.default) {
@@ -130,14 +129,18 @@ DefaultFormatter.prototype.export = function (node, nodes) {
var assign;
if (t.isVariableDeclaration(declar)) {
var decl = declar.declarations[0];
for (var i in declar.declarations) {
var decl = declar.declarations[i];
decl.init = util.template("exports-assign", {
VALUE: decl.init,
KEY: decl.id
});
decl.init = util.template("exports-assign", {
VALUE: decl.init,
KEY: decl.id
});
nodes.push(declar);
var newDeclar = t.variableDeclaration(declar.kind, [decl]);
if (i == 0) t.inherits(newDeclar, declar);
nodes.push(newDeclar);
}
} else {
assign = util.template("exports-assign", {
VALUE: declar.id,

View File

@@ -23,9 +23,9 @@ AMDFormatter.prototype.transform = function (ast) {
// build an array of module names
var names = [t.literal("exports")];
_.each(this.ids, function (id, name) {
for (var name in this.ids) {
names.push(t.literal(name));
});
}
names = t.arrayExpression(names);
// build up define container
@@ -68,7 +68,7 @@ AMDFormatter.prototype._push = function (node) {
}
};
AMDFormatter.prototype.import = function (node) {
AMDFormatter.prototype.importDeclaration = function (node) {
this._push(node);
};

View File

@@ -1,15 +1,15 @@
module.exports = CommonJSFormatter;
var DefaultFormatter = require("./_default");
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
var _ = require("lodash");
function CommonJSFormatter(file) {
DefaultFormatter.apply(this, arguments);
var hasNonDefaultExports = false;
_.each(file.ast.program.body, function (node) {
traverse(file.ast, function (node) {
if (t.isExportDeclaration(node) && !node.default) hasNonDefaultExports = true;
});
this.hasNonDefaultExports = hasNonDefaultExports;
@@ -48,7 +48,7 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes)
}
};
CommonJSFormatter.prototype.import = function (node, nodes) {
CommonJSFormatter.prototype.importDeclaration = function (node, nodes) {
// import "foo";
nodes.push(util.template("require", {
//inherits: node,
@@ -57,7 +57,7 @@ CommonJSFormatter.prototype.import = function (node, nodes) {
}, true));
};
CommonJSFormatter.prototype.export = function (node, nodes) {
CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
if (node.default) {
var declar = node.declaration;
@@ -74,7 +74,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) {
// hoist to the top if this default is a function
nodes.push(this._hoistExport(declar, assign));
} else {
DefaultFormatter.prototype.export.apply(this, arguments);
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
}
};

View File

@@ -6,19 +6,13 @@ function IgnoreFormatter() {
}
IgnoreFormatter.prototype.import = function () {
};
IgnoreFormatter.prototype.importSpecifier = function () {
};
IgnoreFormatter.prototype.export = function (node, nodes) {
IgnoreFormatter.prototype.exportDeclaration = function (node, nodes) {
var declar = t.toStatement(node.declaration, true);
if (declar) nodes.push(t.inherits(declar, node));
};
IgnoreFormatter.prototype.importDeclaration =
IgnoreFormatter.prototype.importSpecifier =
IgnoreFormatter.prototype.exportSpecifier = function () {
};

View File

@@ -18,9 +18,9 @@ UMDFormatter.prototype.transform = function (ast) {
// build an array of module names
var names = [];
_.each(this.ids, function (id, name) {
for (var name in this.ids) {
names.push(t.literal(name));
});
}
// factory