Skip to content

Commit 9ac3879

Browse files
authored
fix corner case in conditionals (#5676)
1 parent 37d3e4f commit 9ac3879

File tree

3 files changed

+74
-10
lines changed

3 files changed

+74
-10
lines changed

lib/compress.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9722,15 +9722,16 @@ Compressor.prototype.compress = function(node) {
97229722
if (stat.equals(alt_stat)) {
97239723
body_stats.splice(body_index--, 1);
97249724
alt_stats.splice(alt_index--, 1);
9725-
stats.unshift(stat);
9725+
stats.unshift(merge_expression(stat, alt_stat));
97269726
} else {
97279727
if (!(stat instanceof AST_SimpleStatement)) break;
97289728
if (!(alt_stat instanceof AST_SimpleStatement)) break;
9729-
var expr = stat.body.tail_node();
9730-
if (!expr.equals(alt_stat.body.tail_node())) break;
9729+
var expr1 = stat.body.tail_node();
9730+
var expr2 = alt_stat.body.tail_node();
9731+
if (!expr1.equals(expr2)) break;
97319732
body_index = pop_expr(body_stats, stat.body, body_index);
97329733
alt_index = pop_expr(alt_stats, alt_stat.body, alt_index);
9733-
stats.unshift(make_node(AST_SimpleStatement, expr, { body: expr }));
9734+
stats.unshift(make_node(AST_SimpleStatement, expr1, { body: merge_expression(expr1, expr2) }));
97349735
}
97359736
}
97369737
if (stats.length > 0) {

test/compress/conditionals.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,36 @@ merge_tail_2: {
278278
]
279279
}
280280

281+
merge_tail_3: {
282+
options = {
283+
conditionals: true,
284+
reduce_vars: true,
285+
unused: true,
286+
}
287+
input: {
288+
(function(a, b) {
289+
if (b = a.shift())
290+
console.log(b);
291+
else {
292+
if (b = a.shift())
293+
while (console.log("foo"));
294+
console.log(b);
295+
}
296+
})([ false, "bar" ]);
297+
}
298+
expect: {
299+
(function(a, b) {
300+
if (!(b = a.shift()) && (b = a.shift()))
301+
while (console.log("foo"));
302+
console.log(b);
303+
})([ false, "bar" ]);
304+
}
305+
expect_stdout: [
306+
"foo",
307+
"bar",
308+
]
309+
}
310+
281311
merge_tail_sequence_1: {
282312
options = {
283313
conditionals: true,
@@ -368,6 +398,39 @@ merge_tail_sequence_2: {
368398
]
369399
}
370400

401+
merge_tail_sequence_3: {
402+
options = {
403+
conditionals: true,
404+
reduce_vars: true,
405+
unused: true,
406+
}
407+
input: {
408+
(function(a, b) {
409+
if (b = a.shift())
410+
console.log("foo"),
411+
console.log(b);
412+
else {
413+
if (b = a.shift())
414+
while (console.log("bar"));
415+
console.log(b);
416+
}
417+
})([ false, "baz" ]);
418+
}
419+
expect: {
420+
(function(a, b) {
421+
if (b = a.shift())
422+
console.log("foo");
423+
else if (b = a.shift())
424+
while (console.log("bar"));
425+
console.log(b);
426+
})([ false, "baz" ]);
427+
}
428+
expect_stdout: [
429+
"bar",
430+
"baz",
431+
]
432+
}
433+
371434
cond_1: {
372435
options = {
373436
conditionals: true,

test/compress/spreads.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ collapse_vars_1: {
1313
}
1414
input: {
1515
var a;
16-
[ ...a = "PASS", "PASS"].slice();
16+
[ ...a = "PASS", "PASS" ].slice();
1717
console.log(a);
1818
}
1919
expect: {
2020
var a;
21-
[ ...a = "PASS", "PASS"].slice();
21+
[ ...a = "PASS", "PASS" ].slice();
2222
console.log(a);
2323
}
2424
expect_stdout: "PASS"
@@ -33,7 +33,7 @@ collapse_vars_2: {
3333
var a = "FAIL";
3434
try {
3535
a = "PASS";
36-
[ ...42, "PASS"].slice();
36+
[ ...42, "PASS" ].slice();
3737
} catch (e) {
3838
console.log(a);
3939
}
@@ -42,7 +42,7 @@ collapse_vars_2: {
4242
var a = "FAIL";
4343
try {
4444
a = "PASS";
45-
[ ...42, "PASS"].slice();
45+
[ ...42, "PASS" ].slice();
4646
} catch (e) {
4747
console.log(a);
4848
}
@@ -58,15 +58,15 @@ collapse_vars_3: {
5858
input: {
5959
var a = "FAIL";
6060
try {
61-
[ ...(a = "PASS", 42), "PASS"].slice();
61+
[ ...(a = "PASS", 42), "PASS" ].slice();
6262
} catch (e) {
6363
console.log(a);
6464
}
6565
}
6666
expect: {
6767
var a = "FAIL";
6868
try {
69-
[ ...(a = "PASS", 42), "PASS"].slice();
69+
[ ...(a = "PASS", 42), "PASS" ].slice();
7070
} catch (e) {
7171
console.log(a);
7272
}

0 commit comments

Comments
 (0)