Skip to content

Commit 565ab9a

Browse files
authored
fix: preserve shadowed for-loop declarators (#11242)
* fix: do not remove for-loop declarators * fix: do not remove uninitialized var declarators in for init
1 parent dadd22e commit 565ab9a

10 files changed

Lines changed: 56 additions & 4 deletions

File tree

packages/babel-plugin-transform-parameters/src/params.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,19 @@ export default function convertFunctionParams(path, loose) {
7676
switch (node.type) {
7777
case "VariableDeclarator":
7878
if (node.init === null) {
79-
redeclarator.remove();
80-
} else {
81-
state.iife = true;
79+
const declaration = redeclarator.parentPath;
80+
// The following uninitialized var declarators should not be removed
81+
// for (var x in {})
82+
// for (var x;;)
83+
if (
84+
!declaration.parentPath.isFor() ||
85+
declaration.parentPath.get("body") === declaration
86+
) {
87+
redeclarator.remove();
88+
break;
89+
}
8290
}
83-
break;
91+
// fall through
8492
case "FunctionDeclaration":
8593
state.iife = true;
8694
break;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function foo(a = 2) {
2+
for (let a = 1; a > 0; a--);
3+
expect(a).toBe(2);
4+
}
5+
foo();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function foo(a = 2) {
2+
for (let a, i = 0; i < 1; i++) a = 1;
3+
expect(a).toBe(2);
4+
}
5+
foo();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function foo(a = 2) {
2+
for (var i of [0]) var a = 1;
3+
expect(a).toBe(1);
4+
}
5+
foo();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function foo(a = 2) {
2+
for (var [a] of [[1]]);
3+
expect(a).toBe(1);
4+
}
5+
foo();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function foo(a = 2) {
2+
for (var i of [0]) var a;
3+
expect(a).toBe(2);
4+
}
5+
foo();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function foo(a = 2) {
2+
for (var a, i = 0; i < 1; i++);
3+
expect(a).toBe(undefined);
4+
}
5+
foo();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(i = "__proto__") => {
2+
for (var i in {});
3+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugins": [
3+
"transform-parameters"
4+
]
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(function () {
2+
let i = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "__proto__";
3+
return function () {
4+
for (var i in {});
5+
}();
6+
});

0 commit comments

Comments
 (0)