Skip to content

Commit

Permalink
fix corner case in booleans (#5042)
Browse files Browse the repository at this point in the history
fixes #5041
  • Loading branch information
alexlamsl committed Jun 30, 2021
1 parent 4ba8b66 commit 611abff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -8239,7 +8239,9 @@ merge(Compressor.prototype, {
}

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++);
if (parent instanceof AST_Binary) {
var op = parent.operator;
Expand Down Expand Up @@ -8275,9 +8277,7 @@ merge(Compressor.prototype, {
if (parent instanceof AST_BlockStatement) {
if (parent.body[0] === child) continue;
} else if (parent instanceof AST_If) {
var cond = parent.condition;
if (cond === child) continue;
if (node.equivalent_to(cond)) switch (child) {
if (node.equivalent_to(parent.condition)) switch (child) {
case parent.body:
node.truthy = true;
break;
Expand Down
21 changes: 21 additions & 0 deletions test/compress/booleans.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,24 @@ issue_5028_3: {
}
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"
}

0 comments on commit 611abff

Please sign in to comment.