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: for of with iterableIsArray and shadowing variable #16011

Merged
merged 8 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/babel-plugin-transform-for-of/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function buildLoopBody(
const body = newBody ?? bodyPath.node;
if (
t.isBlockStatement(body) &&
Object.keys(path.getBindingIdentifiers()).some(id =>
bodyPath.scope.hasOwnBinding(id),
)
Object.keys(path.getBindingIdentifiers())
.concat(Object.keys(path.get("right").getBindingIdentifiers()))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we fix this in getBindingIdentifiers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure about what's happening here. right does not introduce new bindings, because it cannot contain variable declarations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the fix should be to force memoization in

let array: t.Identifier | t.ThisExpression =
if t.isIdentifier(right) and body.scope.hasOwnBinding(right.name).

.some(id => bodyPath.scope.hasOwnBinding(id))
) {
block = t.blockStatement([declar, body]);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function x(e){
const r = [];
for (const s of e) {
const e = s;
r.push(e);
}
return r;
}

expect(x([1, 2, 3])).toEqual([1, 2, 3]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function x(e){
const r = [];
for (const s of e) {
const e = s;
r.push(e);
}
return r;
}

expect(x([1, 2, 3])).toEqual([1, 2, 3]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"assumptions": {
"iterableIsArray": true
},
"targets": "node 4.0",
"presets": ["env"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function x(e) {
var r = [];
for (var _i2 = 0; _i2 < e.length; _i2++) {
var s = e[_i2];
{
var _e = s;
r.push(_e);
}
}
return r;
}
expect(x([1, 2, 3])).toEqual([1, 2, 3]);