fix `reduce_vars` on boolean binary expressions (#1819)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 17 Apr 2017 09:24:29 +0000 (17:24 +0800)
committerGitHub <noreply@github.com>
Mon, 17 Apr 2017 09:24:29 +0000 (17:24 +0800)
Side effects of `&&` and `||` have not mattered until #1814, which takes assignment expressions into account.

lib/compress.js
test/compress/reduce_vars.js

index 7324fe0..f49dd60 100644 (file)
@@ -340,6 +340,14 @@ merge(Compressor.prototype, {
                         });
                     }
                 }
+                if (node instanceof AST_Binary
+                    && (node.operator == "&&" || node.operator == "||")) {
+                    node.left.walk(tw);
+                    push();
+                    node.right.walk(tw);
+                    pop();
+                    return true;
+                }
                 if (node instanceof AST_If) {
                     node.condition.walk(tw);
                     push();
index 82b0021..94d37cb 100644 (file)
@@ -2217,3 +2217,26 @@ try_abort: {
     }
     expect_stdout: "1 undefined"
 }
+
+boolean_binary_assign: {
+    options = {
+        evaluate: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        !function() {
+            var a;
+            void 0 && (a = 1);
+            console.log(a);
+        }();
+    }
+    expect: {
+        !function() {
+            var a;
+            void 0;
+            console.log(a);
+        }();
+    }
+    expect_stdout: "undefined"
+}