Skip to content

Commit

Permalink
fix corner case in collapse_vars (#4431)
Browse files Browse the repository at this point in the history
fixes #4430
  • Loading branch information
alexlamsl authored Dec 20, 2020
1 parent 7aefe97 commit 47b63ed
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,11 @@ merge(Compressor.prototype, {
if (node instanceof AST_LoopControl) return true;
if (node instanceof AST_SymbolRef) {
if (node.is_declared(compressor)) {
if (node.fixed_value() || can_drop_symbol(node)) return false;
if (node.fixed_value()) return false;
if (can_drop_symbol(node)) {
return !(parent instanceof AST_PropAccess && parent.expression === node)
&& is_arguments(node.definition());
}
} else if (parent instanceof AST_Assign && parent.operator == "=" && parent.left === node) {
return false;
}
Expand Down Expand Up @@ -1812,7 +1816,9 @@ merge(Compressor.prototype, {
return compressor.option("ie8") && node.name && lvalues.has(node.name.name);
}
if (node instanceof AST_PropAccess) {
return side_effects || !value_def && node.expression.may_throw_on_access(compressor);
var exp = node.expression;
return side_effects || !value_def && exp.may_throw_on_access(compressor)
|| exp instanceof AST_SymbolRef && is_arguments(exp.definition());
}
if (node instanceof AST_Spread) return true;
if (node instanceof AST_SymbolRef) {
Expand Down
60 changes: 60 additions & 0 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -8602,3 +8602,63 @@ issue_4248: {
}
expect_stdout: "1"
}

issue_4430_1: {
options = {
collapse_vars: true,
pure_getters: "strict",
}
input: {
function f(a) {
switch (a = 1, arguments[0]) {
case 1:
return "PASS";
case 2:
return "FAIL";
}
}
console.log(f(2));
}
expect: {
function f(a) {
switch (a = 1, arguments[0]) {
case 1:
return "PASS";
case 2:
return "FAIL";
}
}
console.log(f(2));
}
expect_stdout: "PASS"
}

issue_4430_2: {
options = {
collapse_vars: true,
pure_getters: "strict",
}
input: {
function f(a) {
switch (a = 0, arguments[0]) {
case 0:
return "PASS";
case 1:
return "FAIL";
}
}
console.log(f(1));
}
expect: {
function f(a) {
switch (arguments[a = 0]) {
case 0:
return "PASS";
case 1:
return "FAIL";
}
}
console.log(f(1));
}
expect_stdout: "PASS"
}

0 comments on commit 47b63ed

Please sign in to comment.