enhance `side_effects` (#3383)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 24 Apr 2019 20:14:21 +0000 (04:14 +0800)
committerGitHub <noreply@github.com>
Wed, 24 Apr 2019 20:14:21 +0000 (04:14 +0800)
lib/compress.js
test/compress/conditionals.js
test/compress/issue-1639.js
test/compress/sequences.js

index 0a19594..13fc953 100644 (file)
@@ -4172,10 +4172,18 @@ merge(Compressor.prototype, {
             var right = this.right.drop_side_effect_free(compressor, first_in_statement);
             if (!right) return this.left.drop_side_effect_free(compressor, first_in_statement);
             if (lazy_op[this.operator]) {
-                if (right === this.right) return this;
-                var node = this.clone();
-                node.right = right.drop_side_effect_free(compressor);
-                return node;
+                var node;
+                if (right === this.right) {
+                    node = this;
+                } else {
+                    node = this.clone();
+                    node.right = right.drop_side_effect_free(compressor);
+                }
+                return (first_in_statement ? best_of_statement : best_of_expression)(node, make_node(AST_Binary, this, {
+                    operator: node.operator == "&&" ? "||" : "&&",
+                    left: node.left.negate(compressor, first_in_statement),
+                    right: node.right
+                }));
             } else {
                 var left = this.left.drop_side_effect_free(compressor, first_in_statement);
                 if (!left) return right;
index 78c0ca2..a208165 100644 (file)
@@ -1416,3 +1416,22 @@ issue_3271: {
     }
     expect_stdout: "1 1"
 }
+
+iife_condition: {
+    options = {
+        conditionals: true,
+        side_effects: true,
+    }
+    input: {
+        if (function() {
+            return console;
+        }())
+            console.log("PASS");
+    }
+    expect: {
+        !function() {
+            return console;
+        }() || console.log("PASS");
+    }
+    expect_stdout: "PASS"
+}
index 80d45a7..1579e06 100644 (file)
@@ -1,4 +1,3 @@
-
 issue_1639_1: {
     options = {
         booleans: true,
@@ -12,7 +11,6 @@ issue_1639_1: {
     }
     input: {
         var a = 100, b = 10;
-
         var L1 = 5;
         while (--L1 > 0) {
             if ((--b), false) {
@@ -21,7 +19,6 @@ issue_1639_1: {
                 }
             }
         }
-
         console.log(a, b);
     }
     expect: {
@@ -29,7 +26,7 @@ issue_1639_1: {
             if (--b, 0) var ignore = 0;
         console.log(a, b);
     }
-    expect_stdout: true
+    expect_stdout: "100 6"
 }
 
 issue_1639_2: {
@@ -44,25 +41,23 @@ issue_1639_2: {
     }
     input: {
         var a = 100, b = 10;
-
         function f19() {
             if (++a, false)
                 if (a)
                     if (++a);
         }
         f19();
-
         console.log(a, b);
     }
     expect: {
         var a = 100, b = 10;
         function f19() {
-            ++a, 0;
+            ++a, 1;
         }
         f19(),
         console.log(a, b);
     }
-    expect_stdout: true
+    expect_stdout: "101 10"
 }
 
 issue_1639_3: {
@@ -84,5 +79,5 @@ issue_1639_3: {
         a++,
         console.log(a, b);
     }
-    expect_stdout: true
+    expect_stdout: "101 10"
 }
index 0670ab5..29cede4 100644 (file)
@@ -702,7 +702,7 @@ side_effects_cascade_3: {
     }
     expect: {
         function f(a, b) {
-            !(b += a) && ((b = a) || (b -= a, b ^= a)),
+            (b += a) || (b = a) || (b -= a, b ^= a),
             a--;
         }
     }