remove paranthesis for `-(x*y)` (#1732)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 30 Mar 2017 08:09:00 +0000 (16:09 +0800)
committerGitHub <noreply@github.com>
Thu, 30 Mar 2017 08:09:00 +0000 (16:09 +0800)
lib/compress.js
test/compress/numbers.js

index be76015..ac7ea35 100644 (file)
@@ -3034,6 +3034,13 @@ merge(Compressor.prototype, {
                 })).optimize(compressor);
             }
         }
+        if (e instanceof AST_Binary
+            && (self.operator == "+" || self.operator == "-")
+            && (e.operator == "*" || e.operator == "/" || e.operator == "%")) {
+            self.expression = e.left;
+            e.left = self;
+            return e.optimize(compressor);
+        }
         // avoids infinite recursion of numerals
         if (self.operator != "-"
             || !(self.expression instanceof AST_Number
index ea439ec..946a7f2 100644 (file)
@@ -168,3 +168,37 @@ issue_1710: {
     }
     expect_stdout: true
 }
+
+unary_binary_parenthesis: {
+    input: {
+        var v = [ 0, 1, NaN, Infinity, null, undefined, true, false, "", "foo", /foo/ ];
+        v.forEach(function(x) {
+            v.forEach(function(y) {
+                console.log(
+                    +(x*y),
+                    +(x/y),
+                    +(x%y),
+                    -(x*y),
+                    -(x/y),
+                    -(x%y)
+                );
+            });
+        });
+    }
+    expect: {
+        var v = [ 0, 1, 0/0, 1/0, null, void 0, true, false, "", "foo", /foo/ ];
+        v.forEach(function(x) {
+            v.forEach(function(y) {
+                console.log(
+                    +x*y,
+                    +x/y,
+                    +x%y,
+                    -x*y,
+                    -x/y,
+                    -x%y
+                );
+            });
+        });
+    }
+    expect_stdout: true
+}