aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-06-30 17:52:54 +0100
committerGitHub <noreply@github.com>2021-07-01 00:52:54 +0800
commit611abff49f8c5aa158b0bd43c4af3ef79a875905 (patch)
treef2050f0f1661d95c0fe67f06df0543b0cd82030e
parent4ba8b66c5aa86e1c2a48be1fc0c4743f9510aabf (diff)
downloadtracifyjs-611abff49f8c5aa158b0bd43c4af3ef79a875905.tar.gz
tracifyjs-611abff49f8c5aa158b0bd43c4af3ef79a875905.zip
fix corner case in `booleans` (#5042)
fixes #5041
-rw-r--r--lib/compress.js8
-rw-r--r--test/compress/booleans.js21
2 files changed, 25 insertions, 4 deletions
diff --git a/lib/compress.js b/lib/compress.js
index a418925a..a93e34fd 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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;
@@ -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;
diff --git a/test/compress/booleans.js b/test/compress/booleans.js
index 0926c0e4..280a355d 100644
--- a/test/compress/booleans.js
+++ b/test/compress/booleans.js
@@ -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"
+}