Skip to content

Commit

Permalink
fix corner case in collapse_vars (#4919)
Browse files Browse the repository at this point in the history
fixes #4918
  • Loading branch information
alexlamsl committed May 7, 2021
1 parent ee9ceb7 commit bbca9de
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
44 changes: 21 additions & 23 deletions lib/compress.js
Expand Up @@ -2546,12 +2546,7 @@ merge(Compressor.prototype, {
if (parent instanceof AST_PropAccess) {
var exp = parent.expression;
if (exp === node) return find_stop_unused(parent, level + 1);
var sym = root_expr(exp);
if (!(sym instanceof AST_SymbolRef)) return find_stop_unused(parent, level + 1);
var lvalue = lvalues.get(sym.name);
return !lvalue || all(lvalue, function(lhs) {
return !(lhs instanceof AST_PropAccess);
}) ? find_stop_unused(parent, level + 1) : node;
return check_expr(exp);
}
if (parent instanceof AST_Sequence) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_SimpleStatement) return find_stop_unused(parent, level + 1);
Expand All @@ -2562,25 +2557,28 @@ merge(Compressor.prototype, {
if (parent instanceof AST_Yield) return node;
return null;

function check_expr(expr) {
var replace = can_replace;
can_replace = false;
var after = stop_after;
var if_hit = stop_if_hit;
var stack = scanner.stack;
scanner.stack = [ parent ];
expr.transform(scanner);
scanner.stack = stack;
stop_if_hit = if_hit;
stop_after = after;
can_replace = replace;
if (abort) {
abort = false;
return node;
}
return find_stop_unused(parent, level + 1);
}

function check_assignment(lhs) {
if (may_throw(parent)) return node;
if (lhs !== node && lhs instanceof AST_Destructured) {
var replace = can_replace;
can_replace = false;
var after = stop_after;
var if_hit = stop_if_hit;
var stack = scanner.stack;
scanner.stack = [ parent ];
lhs.transform(scanner);
scanner.stack = stack;
stop_if_hit = if_hit;
stop_after = after;
can_replace = replace;
if (abort) {
abort = false;
return node;
}
}
if (lhs !== node && lhs instanceof AST_Destructured) return check_expr(lhs);
return find_stop_unused(parent, level + 1);
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/compress/collapse_vars.js
Expand Up @@ -9171,3 +9171,26 @@ issue_4914: {
}
expect_stdout: "function"
}

issue_4918: {
options = {
collapse_vars: true,
}
input: {
var a = "FAIL";
({
get 42() {
console.log(a);
}
}[a = "PASS", 42] += "PASS");
}
expect: {
var a = "FAIL";
({
get 42() {
console.log(a);
}
}[a = "PASS", 42] += "PASS");
}
expect_stdout: "PASS"
}

0 comments on commit bbca9de

Please sign in to comment.