Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix corner case in collapse_vars #5901

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}