Fix reused nodes - part 2 (#7149)

This commit is contained in:
Nicolò Ribaudo
2018-01-11 19:31:48 +01:00
parent 912bcc186d
commit 252ea5a966
20 changed files with 261 additions and 170 deletions

View File

@@ -50,9 +50,17 @@ export default function(api, options) {
let isPostUpdateExpression = path.isUpdateExpression() && !node.prefix;
if (isPostUpdateExpression) {
if (node.operator === "++") {
node = t.binaryExpression("+", node.argument, t.numericLiteral(1));
node = t.binaryExpression(
"+",
t.cloneNode(node.argument),
t.numericLiteral(1),
);
} else if (node.operator === "--") {
node = t.binaryExpression("-", node.argument, t.numericLiteral(1));
node = t.binaryExpression(
"-",
t.cloneNode(node.argument),
t.numericLiteral(1),
);
} else {
isPostUpdateExpression = false;
}
@@ -74,10 +82,12 @@ export default function(api, options) {
visitor: {
CallExpression(path, state) {
if (path.node.callee.type === TYPE_IMPORT) {
const contextIdent = state.contextIdent;
path.replaceWith(
t.callExpression(
t.memberExpression(contextIdent, t.identifier("import")),
t.memberExpression(
t.identifier(state.contextIdent),
t.identifier("import"),
),
path.node.arguments,
),
);
@@ -90,17 +100,20 @@ export default function(api, options) {
!path.scope.hasBinding("__moduleName")
) {
path.replaceWith(
t.memberExpression(state.contextIdent, t.identifier("id")),
t.memberExpression(
t.identifier(state.contextIdent),
t.identifier("id"),
),
);
}
},
Program: {
enter(path, state) {
state.contextIdent = path.scope.generateUidIdentifier("context");
state.contextIdent = path.scope.generateUid("context");
},
exit(path, state) {
const exportIdent = path.scope.generateUidIdentifier("export");
const exportIdent = path.scope.generateUid("export");
const contextIdent = state.contextIdent;
const exportNames = Object.create(null);
@@ -134,7 +147,10 @@ export default function(api, options) {
function buildExportCall(name, val) {
return t.expressionStatement(
t.callExpression(exportIdent, [t.stringLiteral(name), val]),
t.callExpression(t.identifier(exportIdent), [
t.stringLiteral(name),
val,
]),
);
}
@@ -175,7 +191,7 @@ export default function(api, options) {
if (id) {
nodes.push(declar.node);
nodes.push(buildExportCall("default", id));
nodes.push(buildExportCall("default", t.cloneNode(id)));
addExportName(id.name, "default");
} else {
nodes.push(
@@ -206,7 +222,9 @@ export default function(api, options) {
if (canHoist) {
addExportName(name, name);
beforeBody.push(node);
beforeBody.push(buildExportCall(name, node.id));
beforeBody.push(
buildExportCall(name, t.cloneNode(node.id)),
);
removedPaths.push(path);
} else {
bindingIdentifiers = { [name]: node.id };
@@ -250,13 +268,17 @@ export default function(api, options) {
modules.forEach(function(specifiers) {
const setterBody = [];
const target = path.scope.generateUidIdentifier(specifiers.key);
const target = path.scope.generateUid(specifiers.key);
for (let specifier of specifiers.imports) {
if (t.isImportNamespaceSpecifier(specifier)) {
setterBody.push(
t.expressionStatement(
t.assignmentExpression("=", specifier.local, target),
t.assignmentExpression(
"=",
specifier.local,
t.identifier(target),
),
),
);
} else if (t.isImportDefaultSpecifier(specifier)) {
@@ -272,7 +294,10 @@ export default function(api, options) {
t.assignmentExpression(
"=",
specifier.local,
t.memberExpression(target, specifier.imported),
t.memberExpression(
t.identifier(target),
specifier.imported,
),
),
),
);
@@ -280,13 +305,14 @@ export default function(api, options) {
}
if (specifiers.exports.length) {
const exportObjRef = path.scope.generateUidIdentifier(
"exportObj",
);
const exportObj = path.scope.generateUid("exportObj");
setterBody.push(
t.variableDeclaration("var", [
t.variableDeclarator(exportObjRef, t.objectExpression([])),
t.variableDeclarator(
t.identifier(exportObj),
t.objectExpression([]),
),
]),
);
@@ -295,8 +321,8 @@ export default function(api, options) {
setterBody.push(
buildExportAll({
KEY: path.scope.generateUidIdentifier("key"),
EXPORT_OBJ: exportObjRef,
TARGET: target,
EXPORT_OBJ: t.identifier(exportObj),
TARGET: t.identifier(target),
}),
);
} else if (t.isExportSpecifier(node)) {
@@ -304,8 +330,11 @@ export default function(api, options) {
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(exportObjRef, node.exported),
t.memberExpression(target, node.local),
t.memberExpression(
t.identifier(exportObj),
node.exported,
),
t.memberExpression(t.identifier(target), node.local),
),
),
);
@@ -316,7 +345,9 @@ export default function(api, options) {
setterBody.push(
t.expressionStatement(
t.callExpression(exportIdent, [exportObjRef]),
t.callExpression(t.identifier(exportIdent), [
t.identifier(exportObj),
]),
),
);
}
@@ -325,7 +356,7 @@ export default function(api, options) {
setters.push(
t.functionExpression(
null,
[target],
[t.identifier(target)],
t.blockStatement(setterBody),
),
);
@@ -368,8 +399,8 @@ export default function(api, options) {
SETTERS: t.arrayExpression(setters),
SOURCES: t.arrayExpression(sources),
BODY: path.node.body,
EXPORT_IDENTIFIER: exportIdent,
CONTEXT_IDENTIFIER: contextIdent,
EXPORT_IDENTIFIER: t.identifier(exportIdent),
CONTEXT_IDENTIFIER: t.identifier(contextIdent),
}),
];
},