Visitar URL original
fix: assign closure body after handling return (#18193) · babel/babel@b1f818a · GitHub
Skip to content

Commit b1f818a

Browse files
JLHwungnicolo-ribaudo
authored andcommitted
fix: assign closure body after handling return (#18193)
backport of 2d762de
1 parent 28fb9f5 commit b1f818a

7 files changed

Lines changed: 54 additions & 6 deletions

File tree

‎packages/babel-plugin-transform-block-scoping/src/loop.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,7 @@ export function wrapLoopBody(
390390
}
391391

392392
const id = loopPath.scope.generateUid("loop");
393-
const fn = t.functionExpression(
394-
null,
395-
closureParams,
396-
t.toBlock(loopNode.body),
397-
);
393+
const fn = t.functionExpression(null, closureParams, t.blockStatement([]));
398394
let call: t.Expression = t.callExpression(t.identifier(id), callArgs);
399395

400396
const fnParent = loopPath.findParent(p => p.isFunction());
@@ -410,7 +406,6 @@ export function wrapLoopBody(
410406
updater.length > 0
411407
? t.expressionStatement(t.sequenceExpression(updater))
412408
: null;
413-
if (updaterNode) fn.body.body.push(updaterNode);
414409

415410
// NOTE: Calling .insertBefore on the loop path might cause the
416411
// loop to be moved in the AST. For example, in
@@ -559,7 +554,13 @@ export function wrapLoopBody(
559554
}
560555
}
561556

557+
// Assign loop closure body after the original loop body was manipulated by
558+
// the completion record handling above. Doing so also avoids duplicate AST
559+
// nodes during the transform
560+
const loopBlockBody = t.toBlock(loopNode.body);
562561
loopNode.body = t.blockStatement(bodyStmts);
562+
fn.body = loopBlockBody;
563+
if (updaterNode) loopBlockBody.body.push(updaterNode);
563564

564565
return varPath;
565566
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function run() {
2+
for (let i = 0; i < 2; i++) return () => i;
3+
}
4+
5+
const reader = run();
6+
expect(reader()).toBe(0);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function run() {
2+
for (let i = 0; i < 2; i++) return () => i;
3+
}
4+
5+
const reader = run();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function run() {
2+
var _loop = function (i) {
3+
return {
4+
v: function () {
5+
return i;
6+
}
7+
};
8+
},
9+
_ret;
10+
for (var i = 0; i < 2; i++) {
11+
_ret = _loop(i);
12+
if (_ret) return _ret.v;
13+
}
14+
}
15+
var reader = run();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function run() {
2+
for (let i = 0; i < 2; i++) return () => i;
3+
}
4+
5+
const reader = run();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["transform-block-scoping"]
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function run() {
2+
var _loop = function (i) {
3+
return {
4+
v: () => i
5+
};
6+
},
7+
_ret;
8+
for (var i = 0; i < 2; i++) {
9+
_ret = _loop(i);
10+
if (_ret) return _ret.v;
11+
}
12+
}
13+
var reader = run();

0 commit comments

Comments
 (0)