Disallow reversal where lhs has higher or equal precedence
authorRichard van Velzen <rvanvelzen1@gmail.com>
Tue, 29 Oct 2013 20:37:36 +0000 (21:37 +0100)
committerRichard van Velzen <rvanvelzen1@gmail.com>
Tue, 29 Oct 2013 20:37:36 +0000 (21:37 +0100)
Fixes #267

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

index 13ae7c7..108a4e9 100644 (file)
@@ -1907,7 +1907,11 @@ merge(Compressor.prototype, {
                 // if right is a constant, whatever side effects the
                 // left side might have could not influence the
                 // result.  hence, force switch.
-                reverse(null, true);
+
+                if (!(self.left instanceof AST_Binary
+                    && PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
+                    reverse(null, true);
+                }
             }
             if (/^[!=]==?$/.test(self.operator)) {
                 if (self.left instanceof AST_SymbolRef && self.right instanceof AST_Conditional) {
diff --git a/test/compress/issue-267.js b/test/compress/issue-267.js
new file mode 100644 (file)
index 0000000..7233d9f
--- /dev/null
@@ -0,0 +1,11 @@
+issue_267: {
+    options = { comparisons: true };
+    input: {
+        x = a % b / b * c * 2;
+        x = a % b * 2
+    }
+    expect: {
+        x = a % b / b * c * 2;
+        x = a % b * 2;
+    }
+}