fix boolean context detection (#3466)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 11 Oct 2019 19:42:57 +0000 (03:42 +0800)
committerGitHub <noreply@github.com>
Fri, 11 Oct 2019 19:42:57 +0000 (03:42 +0800)
fixes #3465

lib/ast.js
test/compress/booleans.js [new file with mode: 0644]
test/compress/evaluate.js

index 5e1fbbb..b170491 100644 (file)
@@ -985,6 +985,7 @@ TreeWalker.prototype = {
                     fn = this.parent(++i);
                     if (!fn) return false;
                 } while (!(fn instanceof AST_Lambda));
+                if (fn.name) return false;
                 self = this.parent(++i);
                 if (!self || self.TYPE != "Call" || self.expression !== fn) return false;
             } else {
diff --git a/test/compress/booleans.js b/test/compress/booleans.js
new file mode 100644 (file)
index 0000000..9b8c166
--- /dev/null
@@ -0,0 +1,88 @@
+iife_boolean_context: {
+    options = {
+        booleans: true,
+        evaluate: true,
+    }
+    input: {
+        console.log(function() {
+            return Object(1) || false;
+        }() ? "PASS" : "FAIL");
+        console.log(function() {
+            return [].length || true;
+        }() ? "PASS" : "FAIL");
+    }
+    expect: {
+        console.log(function() {
+            return Object(1);
+        }() ? "PASS" : "FAIL");
+        console.log(function() {
+            return [].length, 1;
+        }() ? "PASS" : "FAIL");
+    }
+    expect_stdout: [
+        "PASS",
+        "PASS",
+    ]
+    expect_warnings: [
+        "WARN: Dropping side-effect-free || [test/compress/booleans.js:2,19]",
+        "WARN: Boolean || always true [test/compress/booleans.js:5,19]",
+    ]
+}
+
+issue_3465_1: {
+    options = {
+        booleans: true,
+    }
+    input: {
+        console.log(function(a) {
+            return typeof a;
+        }() ? "PASS" : "FAIL");
+    }
+    expect: {
+        console.log(function(a) {
+            return 1;
+        }() ? "PASS" : "FAIL");
+    }
+    expect_stdout: "PASS"
+}
+
+issue_3465_2: {
+    options = {
+        booleans: true,
+    }
+    input: {
+        console.log(function f(a) {
+            if (!a) console.log(f(42));
+            return typeof a;
+        }() ? "PASS" : "FAIL");
+    }
+    expect: {
+        console.log(function f(a) {
+            if (!a) console.log(f(42));
+            return typeof a;
+        }() ? "PASS" : "FAIL");
+    }
+    expect_stdout: [
+        "number",
+        "PASS",
+    ]
+}
+
+issue_3465_3: {
+    options = {
+        booleans: true,
+        passes: 2,
+        unused: true,
+    }
+    input: {
+        console.log(function f(a) {
+            return typeof a;
+        }() ? "PASS" : "FAIL");
+    }
+    expect: {
+        console.log(function(a) {
+            return 1;
+        }() ? "PASS" : "FAIL");
+    }
+    expect_stdout: "PASS"
+}
index 22c87b7..0ac117f 100644 (file)
@@ -1757,34 +1757,3 @@ issue_3387_2: {
     }
     expect_stdout: "NaN"
 }
-
-iife_boolean_context: {
-    options = {
-        booleans: true,
-        evaluate: true,
-    }
-    input: {
-        console.log(function() {
-            return Object(1) || false;
-        }() ? "PASS" : "FAIL");
-        console.log(function() {
-            return [].length || true;
-        }() ? "PASS" : "FAIL");
-    }
-    expect: {
-        console.log(function() {
-            return Object(1);
-        }() ? "PASS" : "FAIL");
-        console.log(function() {
-            return [].length, 1;
-        }() ? "PASS" : "FAIL");
-    }
-    expect_stdout: [
-        "PASS",
-        "PASS",
-    ]
-    expect_warnings: [
-        "WARN: Dropping side-effect-free || [test/compress/evaluate.js:2,19]",
-        "WARN: Boolean || always true [test/compress/evaluate.js:5,19]",
-    ]
-}