Skip to content

Commit

Permalink
fix corner case in collapse_vars (#4892)
Browse files Browse the repository at this point in the history
fixes #4891
  • Loading branch information
alexlamsl committed May 1, 2021
1 parent 53b57ee commit 6ab26ee
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/compress.js
Expand Up @@ -1884,9 +1884,17 @@ merge(Compressor.prototype, {
&& node.name == def.name) {
if (!--replaced) abort = true;
if (is_lhs(node, multi_replacer.parent())) return node;
def.replaced++;
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) {
return make_node(AST_Assign, candidate, {
operator: "=",
left: node,
right: ref,
});
}
def.replaced++;
return ref;
}
// Skip (non-executed) functions and (leading) default case in switch statements
Expand Down
28 changes: 28 additions & 0 deletions test/compress/collapse_vars.js
Expand Up @@ -9057,3 +9057,31 @@ issue_4874: {
}
expect_stdout: "PASS"
}

issue_4891: {
options = {
collapse_vars: true,
reduce_vars: true,
toplevel: true,
}
input: {
var a = 0, b;
a++;
console.log(b = a, b);
b--;
a.a += 0;
console.log(b);
}
expect: {
var a = 0, b;
a++;
console.log(a, b = a);
b--;
a.a += 0;
console.log(b);
}
expect_stdout: [
"1 1",
"0",
]
}

0 comments on commit 6ab26ee

Please sign in to comment.