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 all 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
3 changes: 2 additions & 1 deletion packages/babel-plugin-transform-for-of/package.json
Expand Up @@ -17,7 +17,8 @@
"babel-plugin"
],
"dependencies": {
"@babel/helper-plugin-utils": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^",
"@babel/helper-skip-transparent-expression-wrappers": "workspace:^"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
Expand Down
14 changes: 13 additions & 1 deletion packages/babel-plugin-transform-for-of/src/index.ts
Expand Up @@ -3,6 +3,7 @@ import { template, types as t } from "@babel/core";
import type { NodePath } from "@babel/traverse";

import transformWithoutHelper from "./no-helper-implementation.ts";
import { skipTransparentExprWrapperNodes } from "@babel/helper-skip-transparent-expression-wrappers";

export interface Options {
allowArrayLike?: boolean;
Expand Down Expand Up @@ -89,13 +90,24 @@ export default declare((api, options: Options) => {
visitor: {
ForOfStatement(path) {
const { scope } = path;
const { left, right, await: isAwait } = path.node;
const { left, await: isAwait } = path.node;
if (isAwait) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const right = skipTransparentExprWrapperNodes(
path.node.right,
) as t.Expression;
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]);
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -2724,6 +2724,7 @@ __metadata:
"@babel/core": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^"
"@babel/helper-skip-transparent-expression-wrappers": "workspace:^"
peerDependencies:
"@babel/core": ^7.0.0-0
languageName: unknown
Expand Down