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,41 @@ const rewriteReferencesVisitor = {
}
},
},
"ForOfStatement|ForInStatement"(path) {
const { scope, node } = path;
const { left } = node;
const { exported, scope: programScope } = this;

// if it's a variable declaration, such as for (let {foo} of []) {}
// then no transformation is needed
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.generateUidIdentifier();
vedantroy marked this conversation as resolved.
Show resolved Hide resolved
bodyPath.unshiftContainer(
"body",
t.expressionStatement(t.assignmentExpression("=", left, newLoopId)),
);
path
.get("left")
.replaceWith(
t.variableDeclaration("let", [t.variableDeclarator(newLoopId)]),
);
}
},
};
@@ -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 _temp of []) {
exports.bar = exports.foo = foo = _temp;
}

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

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

let _foo;
}

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

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

let _foo2;
}

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

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

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

let _foo3;
}

{
let foo;

for (foo of []) {}
}