fix corner case in booleans (#5042)

fixes #5041
This commit is contained in:
Alex Lam S.L
2021-06-30 17:52:54 +01:00
committed by GitHub
parent 4ba8b66c5a
commit 611abff49f
2 changed files with 25 additions and 4 deletions

View File

@@ -8239,7 +8239,9 @@ merge(Compressor.prototype, {
} }
function mark_duplicate_condition(compressor, node) { function mark_duplicate_condition(compressor, node) {
for (var level = 0, child = compressor.self(), parent; ; child = parent) { var level = 0, child, parent = compressor.self();
if (!is_statement(parent)) while (true) {
child = parent;
parent = compressor.parent(level++); parent = compressor.parent(level++);
if (parent instanceof AST_Binary) { if (parent instanceof AST_Binary) {
var op = parent.operator; var op = parent.operator;
@@ -8275,9 +8277,7 @@ merge(Compressor.prototype, {
if (parent instanceof AST_BlockStatement) { if (parent instanceof AST_BlockStatement) {
if (parent.body[0] === child) continue; if (parent.body[0] === child) continue;
} else if (parent instanceof AST_If) { } else if (parent instanceof AST_If) {
var cond = parent.condition; if (node.equivalent_to(parent.condition)) switch (child) {
if (cond === child) continue;
if (node.equivalent_to(cond)) switch (child) {
case parent.body: case parent.body:
node.truthy = true; node.truthy = true;
break; break;

View File

@@ -634,3 +634,24 @@ issue_5028_3: {
} }
expect_stdout: "-1" expect_stdout: "-1"
} }
issue_5041: {
options = {
booleans: true,
conditionals: true,
}
input: {
var a = 42;
if (a)
if ([ a = null ])
if (a)
console.log("FAIL");
else
console.log("PASS");
}
expect: {
var a = 42;
a && [ a = null ] && (a ? console.log("FAIL") : console.log("PASS"));
}
expect_stdout: "PASS"
}