Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix export bindings not updated by 'for ... in' and 'for ... of' #11074

Merged
merged 13 commits into from Feb 9, 2020
Expand Up @@ -306,4 +306,40 @@ const rewriteReferencesVisitor = {
}
},
},
"ForOfStatement|ForInStatement"(path) {
const { scope, node } = path;
const { left } = node;
const { exported, scope: programScope } = this;

if (!t.isVariableDeclaration(left)) {
let didTransform = false;
const bodyPath = path.get("body");
const loopBodyScope = bodyPath.scope;
for (const name of Object.keys(t.getOuterBindingIdentifiers(left))) {
if (
exported.get(name) &&
programScope.getBinding(name) === scope.getBinding(name)
) {
didTransform = true;
if (loopBodyScope.hasOwnBinding(name)) {
loopBodyScope.rename(name);
}
}
}
if (!didTransform) {
return;
}
const newLoopId = scope.generateUidIdentifierBasedOnNode(left);
bodyPath.unshiftContainer(
"body",
t.expressionStatement(t.assignmentExpression("=", left, newLoopId)),
);
path
.get("left")
.replaceWith(
t.variableDeclaration("let", [t.variableDeclarator(newLoopId)]),
);
scope.registerDeclaration(path.get("left"));
}
},
};
@@ -0,0 +1,22 @@
export let foo;
export {foo as bar}

for (foo of []) {}
for (foo in []) {}
for (foo of []) {
let foo;
}
for ({foo} of []) {}
for ({foo} of []) {
let foo;
}
for ({test: {foo}} of []) {}
for ([foo, [...foo]] of []) {}
for ([foo, [...foo]] of []) {
let foo;
}

{
let foo;
for(foo of []) {}
}
@@ -0,0 +1,65 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.bar = exports.foo = void 0;
let foo;
exports.bar = exports.foo = foo;

for (let _foo of []) {
exports.bar = exports.foo = foo = _foo;
}

for (let _foo2 in []) {
exports.bar = exports.foo = foo = _foo2;
}

for (let _foo4 of []) {
exports.bar = exports.foo = foo = _foo4;

let _foo3;
}

for (let _foo5 of []) {
({
foo
} = _foo5);
exports.bar = exports.foo = foo;
}

for (let _foo7 of []) {
({
foo
} = _foo7);
exports.bar = exports.foo = foo;

let _foo6;
}

for (let _test of []) {
({
test: {
foo
}
} = _test);
exports.bar = exports.foo = foo;
}

for (let _ref of []) {
[foo, [...foo]] = _ref;
exports.bar = exports.foo = foo;
}

for (let _ref2 of []) {
[foo, [...foo]] = _ref2;
exports.bar = exports.foo = foo;

let _foo8;
}

{
let foo;

for (foo of []) {}
}