explode duplicate identifiers in export/import specifiers and property shorthand - fixes #1458

This commit is contained in:
Sebastian McKenzie
2015-05-11 22:08:38 +01:00
parent 8277a532f4
commit 2b42773e01
11 changed files with 66 additions and 51 deletions

View File

@@ -3,7 +3,7 @@ import * as t from "../../types";
export function ImportSpecifier(node, print) {
print(node.imported);
if (node.local && node.local !== node.imported) {
if (node.local && node.local.name !== node.imported.name) {
this.push(" as ");
print(node.local);
}
@@ -19,7 +19,7 @@ export function ExportDefaultSpecifier(node, print) {
export function ExportSpecifier(node, print) {
print(node.local);
if (node.exported && node.local !== node.exported) {
if (node.exported && node.local.name !== node.exported.name) {
this.push(" as ");
print(node.exported);
}

View File

@@ -1,4 +1,5 @@
import each from "lodash/collection/each";
import * as t from "../../types";
export function Identifier(node) {
this.push(node.name);
@@ -39,7 +40,14 @@ export function Property(node, print) {
this.push("]");
} else {
print(node.key);
if (node.shorthand) return;
// shorthand!
if (node.shorthand &&
(t.isIdentifier(node.key) &&
t.isIdentifier(node.value) &&
node.key.name === node.value.name)) {
return;
}
}
this.push(":");

View File

@@ -1,5 +1,3 @@
import * as t from "../../../types";
export function Property(node) {
if (node.method) {
node.method = false;
@@ -7,6 +5,5 @@ export function Property(node) {
if (node.shorthand) {
node.shorthand = false;
node.key = t.removeComments(t.clone(node.key));
}
}

View File

@@ -1,5 +1,6 @@
export default {
//- builtin-setup
_explode: require("./internal/explode"),
_validation: require("./internal/validation"),
_hoistDirectives: require("./internal/hoist-directives"),
"minification.removeDebugger": require("./minification/remove-debugger"),

View File

@@ -0,0 +1,18 @@
import clone from "lodash/lang/clone";
import * as t from "../../../types";
export var metadata = {
group: "builtin-setup"
};
function buildClone(bindingKey, refKey) {
return function (node) {
if (node[bindingKey] === node[refKey]) {
node[refKey] = t.removeComments(clone(node[refKey]));
}
};
}
export var Property = buildClone("value", "key");
export var ExportSpecifier = buildClone("local", "exported");
export var ImportSpecifier = buildClone("local", "imported");

View File

@@ -4,6 +4,7 @@
// a generator function as a default then regenerator will destroy the export
// declaration and leave a variable declaration in it's place... yeah, handy.
import clone from "lodash/lang/clone";
import * as t from "../../../types";
export var metadata = {
@@ -75,7 +76,7 @@ export function ExportNamedDeclaration(node, parent, scope) {
var bindings = this.get("declaration").getBindingIdentifiers();
for (var key in bindings) {
var id = bindings[key];
specifiers.push(t.exportSpecifier(id, id));
specifiers.push(t.exportSpecifier(clone(id), clone(id)));
}
return [declar, t.exportNamedDeclaration(null, specifiers)];
}

View File

@@ -1,8 +1,11 @@
import { bare } from "../../helpers/name-method";
export var metadata = {
group: "builtin-setup"
};
export {
bare as FunctionExpression,
bare as ArrowFunctionExpression
} from "../../helpers/name-method";
export var FunctionExpression = {
exit: bare
};
export { FunctionExpression as ArrowFunctionExpression };

View File

@@ -90,50 +90,13 @@ var blockVariableVisitor = explode({
var renameVisitor = explode({
ReferencedIdentifier(node, parent, scope, state) {
if (node.name !== state.oldName) return;
if (this.parentPath.isProperty() && this.key === "key" && parent.shorthand) {
var value = t.identifier(state.newName);;
if (parent.value === state.binding) {
state.info.identifier = state.binding = value;
}
parent.shorthand = false;
parent.value = value;
parent.key = t.identifier(state.oldName);
} else {
if (node.name === state.oldName) {
node.name = state.newName;
}
},
Declaration(node, parent, scope, state) {
var ids = {};
var matchesLocal = (node, key) => {
return node.local === node[key] && (node.local.name === state.oldName || node.local.name === state.newName);
};
if (this.isExportDeclaration() && this.has("specifiers")) {
var specifiers = this.get("specifiers");
for (var specifier of (specifiers: Array)) {
if (specifier.isExportSpecifier() && matchesLocal(specifier.node, "exported")) {
specifier.get("exported").replaceWith(t.identifier(state.oldName));
}
}
} else if (this.isImportDeclaration() && this.has("specifiers")) {
var specifiers = this.get("specifiers");
for (var specifier of (specifiers: Array)) {
if (specifier.isImportSpecifier() && matchesLocal(specifier.node, "imported")) {
state.binding = state.info.identifier = t.identifier(state.newName);
specifier.get("local").replaceWith(state.binding);
} else {
extend(ids, specifier.getBindingIdentifiers());
}
}
} else {
ids = this.getBindingIdentifiers();
}
var ids = this.getBindingIdentifiers();;
for (var name in ids) {
if (name === state.oldName) ids[name].name = state.newName;

View File

@@ -29,7 +29,7 @@ export function isReferenced(node: Object, parent: Object): boolean {
// no: { NODE: "" }
case "Property":
if (parent.key === node) {
return parent.computed || parent.shorthand;
return parent.computed;
}
// no: var NODE = init;