Skip to content

Commit

Permalink
fix: transform-block-scoping captures the variables of the method i…
Browse files Browse the repository at this point in the history
…n the loop (#15962)

fix
  • Loading branch information
liuxingbaoyu committed Sep 18, 2023
1 parent 38e839d commit b7f04ac
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/babel-plugin-transform-block-scoping/src/loop.ts
Expand Up @@ -75,7 +75,9 @@ function relativeLoopLocation(path: NodePath, loopPath: NodePath<t.Loop>) {
let inClosure = false;

for (let currPath = path; currPath; currPath = currPath.parentPath) {
if (currPath.isFunction() || currPath.isClass()) inClosure = true;
if (currPath.isFunction() || currPath.isClass() || currPath.isMethod()) {
inClosure = true;
}
if (currPath === bodyPath) {
return { inBody: true, inClosure };
} else if (currPath === loopPath) {
Expand Down
@@ -0,0 +1,15 @@
var objects = [];
var i2 = 0;
for (var i = 0; i < 10; i++) {
let captured = i2;
i2++;

objects.push({
foo() {
return captured;
}
});
}

expect(objects[0].foo()).toBe(0);
expect(objects[1].foo()).toBe(1);
@@ -0,0 +1,15 @@
var objects = [];
var i2 = 0;
for (var i = 0; i < 10; i++) {
let captured = i2;
i2++;

objects.push({
foo() {
return captured;
}
});
}

expect(objects[0].foo()).toBe(0);
expect(objects[1].foo()).toBe(1);
@@ -0,0 +1,16 @@
var objects = [];
var i2 = 0;
var _loop = function () {
var captured = i2;
i2++;
objects.push({
foo() {
return captured;
}
});
};
for (var i = 0; i < 10; i++) {
_loop();
}
expect(objects[0].foo()).toBe(0);
expect(objects[1].foo()).toBe(1);

0 comments on commit b7f04ac

Please sign in to comment.