handle negated constants correctly in `collapse_vars` (#2975)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 5 Mar 2018 16:45:58 +0000 (00:45 +0800)
committerGitHub <noreply@github.com>
Mon, 5 Mar 2018 16:45:58 +0000 (00:45 +0800)
fixes #2974

lib/compress.js
test/compress/collapse_vars.js

index a63a5c6..2f109a1 100644 (file)
@@ -778,8 +778,7 @@ merge(Compressor.prototype, {
                 lhs = lhs.fixed_value();
             }
             if (!lhs) return true;
-            if (lhs instanceof AST_RegExp) return false;
-            if (lhs instanceof AST_Constant) return true;
+            if (lhs.is_constant()) return true;
             return is_lhs_read_only(lhs);
         }
         return false;
index 97efc21..8b0f033 100644 (file)
@@ -5207,3 +5207,39 @@ collapse_rhs_undefined: {
     }
     expect_stdout: "true true true"
 }
+
+issue_2974: {
+    options = {
+        booleans: true,
+        collapse_vars: true,
+        evaluate: true,
+        loops: true,
+        passes: 2,
+        pure_getters: "strict",
+        reduce_vars: true,
+        sequences: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        var c = 0;
+        (function f(b) {
+            var a = 2;
+            do {
+                b && b[b];
+                b && (b.null = -4);
+                c++;
+            } while (b.null && --a > 0);
+        })(true);
+        console.log(c);
+    }
+    expect: {
+        var c = 0;
+        (function(b) {
+            var a = 2;
+            for (; b.null = -4, c++, b.null && --a > 0;);
+        })(!0),
+        console.log(c);
+    }
+    expect_stdout: "1"
+}