fix: preserve shadowed for-loop declarators (#11242)

* fix: do not remove for-loop declarators

* fix: do not remove uninitialized var declarators in for init
This commit is contained in:
Huáng Jùnliàng 2020-03-12 14:07:59 -04:00 committed by GitHub
parent dadd22e161
commit 565ab9a3fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 56 additions and 4 deletions

View File

@ -76,11 +76,19 @@ export default function convertFunctionParams(path, loose) {
switch (node.type) {
case "VariableDeclarator":
if (node.init === null) {
redeclarator.remove();
} else {
state.iife = true;
const declaration = redeclarator.parentPath;
// The following uninitialized var declarators should not be removed
// for (var x in {})
// for (var x;;)
if (
!declaration.parentPath.isFor() ||
declaration.parentPath.get("body") === declaration
) {
redeclarator.remove();
break;
}
}
break;
// fall through
case "FunctionDeclaration":
state.iife = true;
break;

View File

@ -0,0 +1,5 @@
function foo(a = 2) {
for (let a = 1; a > 0; a--);
expect(a).toBe(2);
}
foo();

View File

@ -0,0 +1,5 @@
function foo(a = 2) {
for (let a, i = 0; i < 1; i++) a = 1;
expect(a).toBe(2);
}
foo();

View File

@ -0,0 +1,5 @@
function foo(a = 2) {
for (var i of [0]) var a = 1;
expect(a).toBe(1);
}
foo();

View File

@ -0,0 +1,5 @@
function foo(a = 2) {
for (var [a] of [[1]]);
expect(a).toBe(1);
}
foo();

View File

@ -0,0 +1,5 @@
function foo(a = 2) {
for (var i of [0]) var a;
expect(a).toBe(2);
}
foo();

View File

@ -0,0 +1,5 @@
function foo(a = 2) {
for (var a, i = 0; i < 1; i++);
expect(a).toBe(undefined);
}
foo();

View File

@ -0,0 +1,3 @@
(i = "__proto__") => {
for (var i in {});
};

View File

@ -0,0 +1,5 @@
{
"plugins": [
"transform-parameters"
]
}

View File

@ -0,0 +1,6 @@
(function () {
let i = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "__proto__";
return function () {
for (var i in {});
}();
});