fix explosion of modules and colliding identifiers

This commit is contained in:
Sebastian McKenzie 2015-05-13 03:16:03 +01:00
parent 1aa0bbfac9
commit c4ebfeb0fa
2 changed files with 21 additions and 6 deletions

View File

@ -13,6 +13,18 @@ function buildClone(bindingKey, refKey) {
};
}
function buildListClone(listKey, bindingKey, refKey) {
var clone = buildClone(bindingKey, refKey);
return function (node) {
if (!node[listKey]) return;
for (var subNode of (node[listKey]: Array)) {
clone(subNode);
}
};
}
export var Property = buildClone("value", "key");
export var ExportSpecifier = buildClone("local", "exported");
export var ImportSpecifier = buildClone("local", "imported");
export var ExportDeclaration = buildListClone("specifiers", "local", "exported");
export var ImportDeclaration = buildListClone("specifiers", "local", "imported");

View File

@ -49,6 +49,10 @@ export function ExportDefaultDeclaration(node, parent, scope) {
}
}
function buildExportSpecifier(id) {
return t.exportSpecifier(clone(id), clone(id));
}
export function ExportNamedDeclaration(node, parent, scope) {
ImportDeclaration.apply(this, arguments);
@ -61,12 +65,12 @@ export function ExportNamedDeclaration(node, parent, scope) {
if (t.isClassDeclaration(declar)) {
// export class Foo {}
node.specifiers = [t.exportSpecifier(declar.id, declar.id)];
node.specifiers = [buildExportSpecifier(declar.id)];
node.declaration = null;
return [getDeclar(), node];
} else if (t.isFunctionDeclaration(declar)) {
// export function Foo() {}
node.specifiers = [t.exportSpecifier(declar.id, declar.id)];
node.specifiers = [buildExportSpecifier(declar.id)];
node.declaration = null;
node._blockHoist = 2;
return [getDeclar(), node];
@ -75,8 +79,7 @@ export function ExportNamedDeclaration(node, parent, scope) {
var specifiers = [];
var bindings = this.get("declaration").getBindingIdentifiers();
for (var key in bindings) {
var id = bindings[key];
specifiers.push(t.exportSpecifier(clone(id), clone(id)));
specifiers.push(buildExportSpecifier(bindings[key]));
}
return [declar, t.exportNamedDeclaration(null, specifiers)];
}