dry up amd and common module formatter export specifiers

This commit is contained in:
Sebastian McKenzie 2014-10-22 07:21:56 +11:00
parent 73f65ae634
commit dba935c63d
2 changed files with 13 additions and 28 deletions

View File

@ -75,28 +75,8 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
}; };
AMDFormatter.prototype.exportSpecifier = function (specifier, node, nodes) { AMDFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
var variableName = util.getSpecifierName(specifier); var self = this;
return this._exportSpecifier(function () {
if (node.source) { return self._push(node);
var object = this._push(node); }, specifier, node, nodes);
if (specifier.type === "ExportBatchSpecifier") {
// export * from "foo";
nodes.push(util.template("exports-wildcard", {
OBJECT: object
}, true));
} else {
// export { foo } from "test";
nodes.push(util.template("exports-assign-key", {
VARIABLE_NAME: variableName.name,
OBJECT: object,
KEY: specifier.id
}, true));
}
} else {
// export { foo };
nodes.push(util.template("exports-assign", {
VALUE: specifier.id,
KEY: variableName
}, true));
}
}; };

View File

@ -64,21 +64,20 @@ CommonJSFormatter.prototype.export = function (node, nodes) {
} }
}; };
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) { CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node, nodes) {
var variableName = util.getSpecifierName(specifier); var variableName = util.getSpecifierName(specifier);
if (node.source) { if (node.source) {
var object = b.callExpression(b.identifier("require"), [node.source]);
if (specifier.type === "ExportBatchSpecifier") { if (specifier.type === "ExportBatchSpecifier") {
// export * from "foo"; // export * from "foo";
nodes.push(util.template("exports-wildcard", { nodes.push(util.template("exports-wildcard", {
OBJECT: object OBJECT: getRef()
}, true)); }, true));
} else { } else {
// export { foo } from "test"; // export { foo } from "test";
nodes.push(util.template("exports-assign-key", { nodes.push(util.template("exports-assign-key", {
VARIABLE_NAME: variableName.name, VARIABLE_NAME: variableName.name,
OBJECT: object, OBJECT: getRef(),
KEY: specifier.id KEY: specifier.id
}, true)); }, true));
} }
@ -90,3 +89,9 @@ CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes)
}, true)); }, true));
} }
}; };
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
return this._exportSpecifier(function () {
return b.callExpression(b.identifier("require"), [node.source]);
}, specifier, node, nodes);
};