Skip to content

Commit d82b7de

Browse files
authored
fix corner case in collapse_vars (#5901)
fixes #5899
1 parent cd0a8ec commit d82b7de

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/compress.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3285,14 +3285,24 @@ Compressor.prototype.compress = function(node) {
32853285
if (expr instanceof AST_Assign && expr.right.single_use) return;
32863286
var lhs_ids = Object.create(null);
32873287
var marker = new TreeWalker(function(node) {
3288-
if (node instanceof AST_SymbolRef) lhs_ids[node.definition().id] = true;
3288+
if (!(node instanceof AST_SymbolRef)) return;
3289+
for (var level = 0, parent, child = node; parent = marker.parent(level++); child = parent) {
3290+
if (is_direct_assignment(child, parent)) {
3291+
if (parent instanceof AST_DestructuredKeyVal) parent = marker.parent(level++);
3292+
continue;
3293+
}
3294+
lhs_ids[node.definition().id] = true;
3295+
return;
3296+
}
3297+
lhs_ids[node.definition().id] = "a";
32893298
});
32903299
while (expr instanceof AST_Assign && expr.operator == "=") {
32913300
expr.left.walk(marker);
32923301
expr = expr.right;
32933302
}
32943303
if (expr instanceof AST_ObjectIdentity) return rhs_exact_match;
32953304
if (expr instanceof AST_SymbolRef) {
3305+
if (lhs_ids[expr.definition().id] === "a") return;
32963306
var value = expr.evaluate(compressor);
32973307
if (value === expr) return rhs_exact_match;
32983308
return rhs_fuzzy_match(value, rhs_exact_match);

test/compress/destructured.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4241,3 +4241,51 @@ issue_5866_12: {
42414241
expect_stdout: "PASS"
42424242
node_version: ">=6"
42434243
}
4244+
4245+
issue_5899_1: {
4246+
options = {
4247+
collapse_vars: true,
4248+
}
4249+
input: {
4250+
var log = console.log, a, b;
4251+
a = "foo";
4252+
log(a && a);
4253+
b = { p: a } = a;
4254+
log(a);
4255+
}
4256+
expect: {
4257+
var log = console.log, a, b;
4258+
log((a = "foo") && a);
4259+
b = { p: a } = a;
4260+
log(a);
4261+
}
4262+
expect_stdout: [
4263+
"foo",
4264+
"undefined",
4265+
]
4266+
node_version: ">=6"
4267+
}
4268+
4269+
issue_5899_2: {
4270+
options = {
4271+
collapse_vars: true,
4272+
}
4273+
input: {
4274+
var log = console.log, a, b;
4275+
a = "foo";
4276+
log(a && a);
4277+
b = [ a ] = a;
4278+
log(a);
4279+
}
4280+
expect: {
4281+
var log = console.log, a, b;
4282+
log((a = "foo") && a);
4283+
b = [ a ] = a;
4284+
log(a);
4285+
}
4286+
expect_stdout: [
4287+
"foo",
4288+
"f",
4289+
]
4290+
node_version: ">=6"
4291+
}

0 commit comments

Comments
 (0)