Skip to content

Commit

Permalink
Fix export bindings not updated by 'for...in' and 'for...of' (babel#1…
Browse files Browse the repository at this point in the history
…1074)

* Correctly transpile export bindings for some for-of loops

* Correctly transform non-destructured for of loops to update exported variables

* Add tests

* Don't replace entire for of loop

* Correctly transform destructured for-of/for-in exports

* Update exported variables in array pattern and more fixes

* Refresh test output

* Update tests and rebase on master

* Refactor ForOf|ForIn visitor

* Don't transform re-declared exported vars

* Generate better name for loop id

Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>

* Idiomatically generate UidIdentifier

* Update scope after replacing loop declaration

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
2 people authored and rajasekarm committed Feb 17, 2020
1 parent 7a76265 commit c89392a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
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 []) {}
}

0 comments on commit c89392a

Please sign in to comment.