Don't attempt to negate non-boolean AST_Binary
authorMihai Bazon <mihai.bazon@gmail.com>
Wed, 22 Jul 2015 13:53:25 +0000 (16:53 +0300)
committerMihai Bazon <mihai.bazon@gmail.com>
Wed, 22 Jul 2015 13:55:55 +0000 (16:55 +0300)
Fix #751

lib/compress.js
test/compress/issue-751.js [new file with mode: 0644]

index 54873f0..4c4cf97 100644 (file)
@@ -2183,7 +2183,7 @@ merge(Compressor.prototype, {
             }
             break;
         }
-        if (compressor.option("comparisons")) {
+        if (compressor.option("comparisons") && self.is_boolean()) {
             if (!(compressor.parent() instanceof AST_Binary)
                 || compressor.parent() instanceof AST_Assign) {
                 var negated = make_node(AST_UnaryPrefix, self, {
diff --git a/test/compress/issue-751.js b/test/compress/issue-751.js
new file mode 100644 (file)
index 0000000..829b7ca
--- /dev/null
@@ -0,0 +1,29 @@
+negate_booleans_1: {
+    options = {
+        comparisons: true
+    };
+    input: {
+        var a = !a || !b || !c || !d || !e || !f;
+    }
+    expect: {
+        var a = !(a && b && c && d && e && f);
+    }
+}
+
+negate_booleans_2: {
+    options = {
+        comparisons: true
+    };
+    input: {
+        var match = !x &&       // should not touch this one
+            (!z || c) &&
+            (!k || d) &&
+            the_stuff();
+    }
+    expect: {
+        var match = !x &&
+            (!z || c) &&
+            (!k || d) &&
+            the_stuff();
+    }
+}