Skip to content

Commit

Permalink
fix corner case in collapse_vars (#4896)
Browse files Browse the repository at this point in the history
fixes #4895
  • Loading branch information
alexlamsl committed May 2, 2021
1 parent fb03561 commit 4114431
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
15 changes: 10 additions & 5 deletions lib/compress.js
Expand Up @@ -1886,8 +1886,7 @@ merge(Compressor.prototype, {
if (is_lhs(node, multi_replacer.parent())) return node;
var ref = rvalue.clone();
value_def.references.push(ref);
if (abort && candidate instanceof AST_Assign
&& def.references.length - def.replaced - (assignments[def.name] || 0) > 1) {
if (abort && candidate instanceof AST_Assign && remaining_refs(def) > 1) {
return make_node(AST_Assign, candidate, {
operator: "=",
left: node,
Expand Down Expand Up @@ -2281,7 +2280,9 @@ merge(Compressor.prototype, {
if (!(lhs instanceof AST_Destructured)) candidates.push(hit_stack.slice());
extract_candidates(lhs);
extract_candidates(expr.right);
if (lhs instanceof AST_SymbolRef) assignments[lhs.name] = (assignments[lhs.name] || 0) + 1;
if (lhs instanceof AST_SymbolRef && expr.operator == "=") {
assignments[lhs.name] = (assignments[lhs.name] || 0) + 1;
}
} else if (expr instanceof AST_Await) {
extract_candidates(expr.expression, unused);
} else if (expr instanceof AST_Binary) {
Expand Down Expand Up @@ -2584,6 +2585,10 @@ merge(Compressor.prototype, {
return value_def = def;
}

function remaining_refs(def) {
return def.references.length - def.replaced - (assignments[def.name] || 0);
}

function get_lhs(expr) {
if (expr instanceof AST_Assign) {
var lhs = expr.left;
Expand All @@ -2592,7 +2597,7 @@ merge(Compressor.prototype, {
var def = lhs.definition();
if (scope.uses_arguments && is_funarg(def)) return lhs;
if (compressor.exposed(def)) return lhs;
remaining = def.references.length - def.replaced - (assignments[def.name] || 0);
remaining = remaining_refs(def);
if (def.fixed && lhs.fixed) remaining = Math.min(remaining, def.references.filter(function(ref) {
return ref.fixed === lhs.fixed;
}).length - 1);
Expand All @@ -2608,7 +2613,7 @@ merge(Compressor.prototype, {
if (!member(lhs, def.orig)) return;
if (scope.uses_arguments && is_funarg(def)) return;
var declared = def.orig.length - def.eliminated - (declare_only[def.name] || 0);
remaining = def.references.length - def.replaced - (assignments[def.name] || 0);
remaining = remaining_refs(def);
if (def.fixed) remaining = Math.min(remaining, def.references.filter(function(ref) {
if (!ref.fixed) return true;
if (!ref.fixed.assigns) return true;
Expand Down
24 changes: 23 additions & 1 deletion test/compress/collapse_vars.js
Expand Up @@ -8560,7 +8560,7 @@ issue_4047_1: {
expect: {
var b = 1;
var a;
console.log((a = --b + ((a = 0) !== typeof A), +void ((a >>= 0) && console.log("PASS"))));
console.log((a = --b + (0 !== typeof A), +void ((a >>= 0) && console.log("PASS"))));
}
expect_stdout: [
"PASS",
Expand Down Expand Up @@ -9085,3 +9085,25 @@ issue_4891: {
"0",
]
}

issue_4895: {
options = {
collapse_vars: true,
toplevel: true,
}
input: {
var a, b;
(function f() {
a = 42;
})();
console.log((b = a) || b, b += 0);
}
expect: {
var a, b;
(function f() {
a = 42;
})();
console.log((b = a) || b, b += 0);
}
expect_stdout: "42 42"
}

0 comments on commit 4114431

Please sign in to comment.