fix corner case in conditionals (#5676)

This commit is contained in:
Alex Lam S.L
2022-09-23 04:09:55 +01:00
committed by GitHub
parent 37d3e4feaa
commit 9ac3879b06
3 changed files with 74 additions and 10 deletions

View File

@@ -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) {

View File

@@ -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,
@@ -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,

View File

@@ -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"
@@ -33,7 +33,7 @@ collapse_vars_2: {
var a = "FAIL";
try {
a = "PASS";
[ ...42, "PASS"].slice();
[ ...42, "PASS" ].slice();
} catch (e) {
console.log(a);
}
@@ -42,7 +42,7 @@ collapse_vars_2: {
var a = "FAIL";
try {
a = "PASS";
[ ...42, "PASS"].slice();
[ ...42, "PASS" ].slice();
} catch (e) {
console.log(a);
}
@@ -58,7 +58,7 @@ collapse_vars_3: {
input: {
var a = "FAIL";
try {
[ ...(a = "PASS", 42), "PASS"].slice();
[ ...(a = "PASS", 42), "PASS" ].slice();
} catch (e) {
console.log(a);
}
@@ -66,7 +66,7 @@ collapse_vars_3: {
expect: {
var a = "FAIL";
try {
[ ...(a = "PASS", 42), "PASS"].slice();
[ ...(a = "PASS", 42), "PASS" ].slice();
} catch (e) {
console.log(a);
}