fix corner case in collapse_vars (#4892)

fixes #4891
This commit is contained in:
Alex Lam S.L
2021-05-01 20:57:17 +01:00
committed by GitHub
parent 53b57ee57e
commit 6ab26eef6c
2 changed files with 37 additions and 1 deletions

View File

@@ -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

View File

@@ -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",
]
}