Skip to content

Commit

Permalink
Fix shadow variables reassignment for block scoping in loops. (#6814)
Browse files Browse the repository at this point in the history
  • Loading branch information
yavorsky authored and Andarist committed Nov 13, 2017
1 parent 84fe8e4 commit 056a995
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/babel-plugin-transform-block-scoping/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ const continuationVisitor = {
}
state.reassignments[name] = true;
}
} else if (path.isReturnStatement()) {
state.returnStatements.push(path);
}
},
};
Expand Down Expand Up @@ -585,6 +587,7 @@ class BlockScoping {
addContinuations(fn) {
const state = {
reassignments: {},
returnStatements: [],
outsideReferences: this.outsideLetReferences,
};

Expand All @@ -599,6 +602,12 @@ class BlockScoping {

this.scope.rename(param.name, newParam.name, fn);

state.returnStatements.forEach(returnStatement => {
returnStatement.insertBefore(
t.expressionStatement(t.assignmentExpression("=", param, newParam)),
);
});

// assign outer reference as it's been modified internally and needs to be retained
fn.body.body.push(
t.expressionStatement(t.assignmentExpression("=", param, newParam)),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let data = [true, false, false, true, false];

for (let index = 0; index < data.length; index++) {
let item = data[index];
if (!item) {
data.splice(index, 1);
index--;
continue;
}
let fn = function () {item;};
}

assert(data.every(item => item));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
for (let index = 0; index < 10; index++) {
if (index % 2) {
index+=3;
continue;
}
let fn = function () {index;};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var _loop = function (_index) {
if (_index % 2) {
_index += 3;
index = _index;
return "continue";
}

var fn = function () {
_index;
};

index = _index;
};

for (var index = 0; index < 10; index++) {
var _ret = _loop(index);

if (_ret === "continue") continue;
}

0 comments on commit 056a995

Please sign in to comment.