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) {
var variableName = util.getSpecifierName(specifier);
if (node.source) {
var object = this._push(node);
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));
}
var self = this;
return this._exportSpecifier(function () {
return self._push(node);
}, specifier, node, nodes);
};

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