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

@ -237,7 +237,7 @@ ModuleFormatter.prototype.transform = function (ast) {
// feel free to modify this however
};
ModuleFormatter.prototype.import = function (node, nodes) {
ModuleFormatter.prototype.importDeclaration = function (node, nodes) {
// node is an ImportDeclaration
};
@ -246,7 +246,7 @@ ModuleFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
// node is an ImportDeclaration
};
ModuleFormatter.prototype.export = function (node, nodes) {
ModuleFormatter.prototype.exportDeclaration = function (node, nodes) {
// node is an ExportDeclaration
};

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

View File

@ -1,4 +1,5 @@
export var foo = 1;
export var foo = 1, bar = 2;
export var foo2 = function () {};
export var foo3;
export let foo4 = 2;

View File

@ -3,6 +3,8 @@ define(["exports"], function (exports) {
exports.foo7 = foo7;
var foo = exports.foo = 1;
var foo = exports.foo = 1;
var bar = exports.bar = 2;
var foo2 = exports.foo2 = function () {};
var foo3 = exports.foo3 = undefined;
var foo4 = exports.foo4 = 2;

View File

@ -1,4 +1,5 @@
export var foo = 1;
export var foo = 1, bar = 2;
export var foo2 = function () {};
export var foo3;
export let foo4 = 2;

View File

@ -2,6 +2,8 @@
exports.foo7 = foo7;
var foo = exports.foo = 1;
var foo = exports.foo = 1;
var bar = exports.bar = 2;
var foo2 = exports.foo2 = function () {};
var foo3 = exports.foo3 = undefined;
var foo4 = exports.foo4 = 2;

View File

@ -1,4 +1,5 @@
export var foo = 1;
export var foo = 1, bar = 2;
export var foo2 = function () {};
export var foo3;
export let foo4 = 2;

View File

@ -1,6 +1,7 @@
"use strict";
var foo = 1;
var foo = 1, bar = 2;
var foo2 = function () {};
var foo3 = undefined;
var foo4 = 2;

View File

@ -1,4 +1,5 @@
export var foo = 1;
export var foo = 1, bar = 2;
export var foo2 = function () {};
export var foo3;
export let foo4 = 2;

View File

@ -9,6 +9,8 @@
exports.foo7 = foo7;
var foo = exports.foo = 1;
var foo = exports.foo = 1;
var bar = exports.bar = 2;
var foo2 = exports.foo2 = function () {};
var foo3 = exports.foo3 = undefined;
var foo4 = exports.foo4 = 2;