fix corner cases in loops & unused (#4995)

fixes #4994
This commit is contained in:
Alex Lam S.L
2021-06-10 00:58:33 +01:00
committed by GitHub
parent 8dbf0b042e
commit e70b84895c
3 changed files with 67 additions and 20 deletions

View File

@@ -6042,27 +6042,16 @@ merge(Compressor.prototype, {
function compose(child, level, find) {
var parent = compressor.parent(level);
if (!parent) return find;
if (parent instanceof AST_DestructuredKeyVal) {
var destructured = compressor.parent(level + 1);
if (parent.key === child) {
var fn = compressor.parent(level + 2);
if (fn instanceof AST_Lambda) {
return compose(fn, level + 3, fn.argnames.indexOf(destructured) >= 0 ? function(name) {
var def = find(name);
if (def) return def;
def = fn.variables.get(name);
if (def) {
var sym = def.orig[0];
if (sym instanceof AST_SymbolFunarg || sym instanceof AST_SymbolLambda) return def;
}
} : function(name) {
return find(name) || fn.variables.get(name);
});
}
var in_arg = parent instanceof AST_Lambda && member(child, parent.argnames);
return compose(parent, level + 1, in_arg ? function(name) {
var def = find(name);
if (def) return def;
def = parent.variables.get(name);
if (def) {
var sym = def.orig[0];
if (sym instanceof AST_SymbolFunarg || sym instanceof AST_SymbolLambda) return def;
}
return compose(destructured, level + 2, find);
}
return compose(parent, level + 1, parent.variables ? function(name) {
} : parent.variables ? function(name) {
return find(name) || parent.variables.get(name);
} : find);
}

View File

@@ -1726,3 +1726,28 @@ issue_4916: {
expect_stdout: "undefined"
node_version: ">=6"
}
issue_4994: {
options = {
loops: true,
unused: true,
}
input: {
var a = "FAIL";
(function(b = function() {
for (a in { PASS: 42 });
}()) {
var a;
})();
console.log(a);
}
expect: {
var a = "FAIL";
(function(b = function() {
for (a in { PASS: 42 });
}()) {})();
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=6"
}

View File

@@ -2592,3 +2592,36 @@ issue_4608_2: {
expect_stdout: "f"
node_version: ">=6"
}
issue_4994: {
options = {
loops: true,
unused: true,
}
input: {
var a = "FAIL";
(function([
{
[function() {
for (a in { PASS: null });
}()]: b,
},
]) {
var a;
})([ 42 ]);
console.log(a);
}
expect: {
var a = "FAIL";
(function([
{
[function() {
for (a in { PASS: null });
}()]: b,
},
]) {})([ 42 ]);
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=6"
}