Skip to content

Commit

Permalink
fix corner case in conditionals (#5676)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Sep 23, 2022
1 parent 37d3e4f commit 9ac3879
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lib/compress.js
Expand Up @@ -9722,15 +9722,16 @@ Compressor.prototype.compress = function(node) {
if (stat.equals(alt_stat)) {
body_stats.splice(body_index--, 1);
alt_stats.splice(alt_index--, 1);
stats.unshift(stat);
stats.unshift(merge_expression(stat, alt_stat));
} else {
if (!(stat instanceof AST_SimpleStatement)) break;
if (!(alt_stat instanceof AST_SimpleStatement)) break;
var expr = stat.body.tail_node();
if (!expr.equals(alt_stat.body.tail_node())) break;
var expr1 = stat.body.tail_node();
var expr2 = alt_stat.body.tail_node();
if (!expr1.equals(expr2)) break;
body_index = pop_expr(body_stats, stat.body, body_index);
alt_index = pop_expr(alt_stats, alt_stat.body, alt_index);
stats.unshift(make_node(AST_SimpleStatement, expr, { body: expr }));
stats.unshift(make_node(AST_SimpleStatement, expr1, { body: merge_expression(expr1, expr2) }));
}
}
if (stats.length > 0) {
Expand Down
63 changes: 63 additions & 0 deletions test/compress/conditionals.js
Expand Up @@ -278,6 +278,36 @@ merge_tail_2: {
]
}

merge_tail_3: {
options = {
conditionals: true,
reduce_vars: true,
unused: true,
}
input: {
(function(a, b) {
if (b = a.shift())
console.log(b);
else {
if (b = a.shift())
while (console.log("foo"));
console.log(b);
}
})([ false, "bar" ]);
}
expect: {
(function(a, b) {
if (!(b = a.shift()) && (b = a.shift()))
while (console.log("foo"));
console.log(b);
})([ false, "bar" ]);
}
expect_stdout: [
"foo",
"bar",
]
}

merge_tail_sequence_1: {
options = {
conditionals: true,
Expand Down Expand Up @@ -368,6 +398,39 @@ merge_tail_sequence_2: {
]
}

merge_tail_sequence_3: {
options = {
conditionals: true,
reduce_vars: true,
unused: true,
}
input: {
(function(a, b) {
if (b = a.shift())
console.log("foo"),
console.log(b);
else {
if (b = a.shift())
while (console.log("bar"));
console.log(b);
}
})([ false, "baz" ]);
}
expect: {
(function(a, b) {
if (b = a.shift())
console.log("foo");
else if (b = a.shift())
while (console.log("bar"));
console.log(b);
})([ false, "baz" ]);
}
expect_stdout: [
"bar",
"baz",
]
}

cond_1: {
options = {
conditionals: true,
Expand Down
12 changes: 6 additions & 6 deletions test/compress/spreads.js
Expand Up @@ -13,12 +13,12 @@ collapse_vars_1: {
}
input: {
var a;
[ ...a = "PASS", "PASS"].slice();
[ ...a = "PASS", "PASS" ].slice();
console.log(a);
}
expect: {
var a;
[ ...a = "PASS", "PASS"].slice();
[ ...a = "PASS", "PASS" ].slice();
console.log(a);
}
expect_stdout: "PASS"
Expand All @@ -33,7 +33,7 @@ collapse_vars_2: {
var a = "FAIL";
try {
a = "PASS";
[ ...42, "PASS"].slice();
[ ...42, "PASS" ].slice();
} catch (e) {
console.log(a);
}
Expand All @@ -42,7 +42,7 @@ collapse_vars_2: {
var a = "FAIL";
try {
a = "PASS";
[ ...42, "PASS"].slice();
[ ...42, "PASS" ].slice();
} catch (e) {
console.log(a);
}
Expand All @@ -58,15 +58,15 @@ collapse_vars_3: {
input: {
var a = "FAIL";
try {
[ ...(a = "PASS", 42), "PASS"].slice();
[ ...(a = "PASS", 42), "PASS" ].slice();
} catch (e) {
console.log(a);
}
}
expect: {
var a = "FAIL";
try {
[ ...(a = "PASS", 42), "PASS"].slice();
[ ...(a = "PASS", 42), "PASS" ].slice();
} catch (e) {
console.log(a);
}
Expand Down

0 comments on commit 9ac3879

Please sign in to comment.