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 7 commits
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
7 changes: 7 additions & 0 deletions packages/babel-plugin-transform-for-of/src/index.ts
Expand Up @@ -96,6 +96,13 @@ export default declare((api, options: Options) => {
const i = scope.generateUidIdentifier("i");
let array: t.Identifier | t.ThisExpression =
scope.maybeGenerateMemoised(right, true);
if (
!array &&
t.isIdentifier(right) &&
path.get("body").scope.hasOwnBinding(right.name)
) {
array = scope.generateUidIdentifier("arr");
}

const inits = [t.variableDeclarator(i, t.numericLiteral(0))];
if (array) {
Expand Down
@@ -0,0 +1,3 @@
for (let o of arr) {
arr = null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this test already passing in the main branch? I will be surprised if the arr binding is constant given then reassignment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this is passed in the main branch, I made some bad attempts so adding this test to avoid regressions.

@@ -0,0 +1,4 @@
for (let _i = 0, _arr = arr; _i < _arr.length; _i++) {
let o = _arr[_i];
arr = null;
}
@@ -0,0 +1,10 @@
function x(e){
const r = [];
for (const s of e) {
e = null;
r.push(s);
}
return r;
}

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

expect(x([1, 2, 3])).toEqual([1, 2, 3]);
@@ -0,0 +1,7 @@
{
"assumptions": {
"iterableIsArray": true
},
"targets": "node 4.0",
"presets": ["env"]
}
@@ -0,0 +1,10 @@
function x(e) {
var r = [];
for (var _i2 = 0, _e2 = e; _i2 < _e2.length; _i2++) {
var s = _e2[_i2];
e = null;
r.push(s);
}
return r;
}
expect(x([1, 2, 3])).toEqual([1, 2, 3]);
@@ -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]);
@@ -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]);
@@ -0,0 +1,7 @@
{
"assumptions": {
"iterableIsArray": true
},
"targets": "node 4.0",
"presets": ["env"]
}
@@ -0,0 +1,10 @@
function x(e) {
var r = [];
for (var _i2 = 0, _arr2 = e; _i2 < _arr2.length; _i2++) {
var s = _arr2[_i2];
var _e = s;
r.push(_e);
}
return r;
}
expect(x([1, 2, 3])).toEqual([1, 2, 3]);