Skip to content

Commit

Permalink
fix corner case in collapse_vars (#5901)
Browse files Browse the repository at this point in the history
fixes #5899
  • Loading branch information
alexlamsl authored Aug 1, 2024
1 parent cd0a8ec commit d82b7de
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3285,14 +3285,24 @@ Compressor.prototype.compress = function(node) {
if (expr instanceof AST_Assign && expr.right.single_use) return;
var lhs_ids = Object.create(null);
var marker = new TreeWalker(function(node) {
if (node instanceof AST_SymbolRef) lhs_ids[node.definition().id] = true;
if (!(node instanceof AST_SymbolRef)) return;
for (var level = 0, parent, child = node; parent = marker.parent(level++); child = parent) {
if (is_direct_assignment(child, parent)) {
if (parent instanceof AST_DestructuredKeyVal) parent = marker.parent(level++);
continue;
}
lhs_ids[node.definition().id] = true;
return;
}
lhs_ids[node.definition().id] = "a";
});
while (expr instanceof AST_Assign && expr.operator == "=") {
expr.left.walk(marker);
expr = expr.right;
}
if (expr instanceof AST_ObjectIdentity) return rhs_exact_match;
if (expr instanceof AST_SymbolRef) {
if (lhs_ids[expr.definition().id] === "a") return;
var value = expr.evaluate(compressor);
if (value === expr) return rhs_exact_match;
return rhs_fuzzy_match(value, rhs_exact_match);
Expand Down
48 changes: 48 additions & 0 deletions test/compress/destructured.js
Original file line number Diff line number Diff line change
Expand Up @@ -4241,3 +4241,51 @@ issue_5866_12: {
expect_stdout: "PASS"
node_version: ">=6"
}

issue_5899_1: {
options = {
collapse_vars: true,
}
input: {
var log = console.log, a, b;
a = "foo";
log(a && a);
b = { p: a } = a;
log(a);
}
expect: {
var log = console.log, a, b;
log((a = "foo") && a);
b = { p: a } = a;
log(a);
}
expect_stdout: [
"foo",
"undefined",
]
node_version: ">=6"
}

issue_5899_2: {
options = {
collapse_vars: true,
}
input: {
var log = console.log, a, b;
a = "foo";
log(a && a);
b = [ a ] = a;
log(a);
}
expect: {
var log = console.log, a, b;
log((a = "foo") && a);
b = [ a ] = a;
log(a);
}
expect_stdout: [
"foo",
"f",
]
node_version: ">=6"
}

0 comments on commit d82b7de

Please sign in to comment.