fix corner case in `evaluate` (#3729)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 19 Feb 2020 00:41:10 +0000 (00:41 +0000)
committerGitHub <noreply@github.com>
Wed, 19 Feb 2020 00:41:10 +0000 (00:41 +0000)
lib/compress.js
test/compress/conditionals.js
test/compress/issue-637.js
test/compress/issue-640.js
test/compress/regexp.js

index 23c7304..e455ea8 100644 (file)
@@ -3416,6 +3416,8 @@ merge(Compressor.prototype, {
                         line: this.start.line,
                         col: this.start.col
                     });
+                } finally {
+                    if (val instanceof RegExp) val.lastIndex = 0;
                 }
             }
             return this;
@@ -6866,7 +6868,7 @@ merge(Compressor.prototype, {
             if (node.truthy) return true;
             if (node.falsy) return false;
             if (node.is_truthy()) return true;
-            return node.evaluate(compressor);
+            return node.evaluate(compressor, true);
         }
 
         function is_indexFn(node) {
index 8044489..c4ae909 100644 (file)
@@ -1240,11 +1240,11 @@ issue_2535_1: {
     expect: {
         y();
         x() && y();
-        (x(), 1) && y();
+        x(), y();
         x() && y();
         x() && y();
         x() && y();
-        (x(), 0) && y();
+        x();
     }
 }
 
index 64978bb..e39c69f 100644 (file)
@@ -16,7 +16,6 @@ wrongly_optimized: {
         function func() {
             foo();
         }
-        // TODO: optimize to `func(), bar()`
-        (func(), 1) && bar();
+        func(), 1, bar();
     }
 }
index 394d6ba..b0a165f 100644 (file)
@@ -84,6 +84,7 @@ wrongly_optimized: {
     options = {
         booleans: true,
         conditionals: true,
+        dead_code: true,
         evaluate: true,
         expression: true,
     }
@@ -99,8 +100,8 @@ wrongly_optimized: {
         function func() {
             foo();
         }
-        // TODO: optimize to `func(), bar()`
-        if (func(), 1) bar();
+        func(), 1;
+        bar();
     }
 }
 
index e8b2da7..3194d38 100644 (file)
@@ -423,3 +423,53 @@ var_test_global: {
         "PASS",
     ]
 }
+
+lazy_boolean: {
+    options = {
+        evaluate: true,
+        passes: 2,
+        side_effects: true,
+        unsafe: true,
+    }
+    input: {
+        /b/.exec({}) && console.log("PASS");
+        /b/.test({}) && console.log("PASS");
+        /b/g.exec({}) && console.log("PASS");
+        /b/g.test({}) && console.log("PASS");
+    }
+    expect: {
+        console.log("PASS");
+        console.log("PASS");
+        console.log("PASS");
+        console.log("PASS");
+    }
+    expect_stdout: [
+        "PASS",
+        "PASS",
+        "PASS",
+        "PASS",
+    ]
+}
+
+reset_state_between_evaluate: {
+    options = {
+        evaluate: true,
+        passes: 2,
+        unsafe: true,
+    }
+    input: {
+        console.log(function() {
+            for (var a in /[abc4]/g.exec("a"))
+                return "PASS";
+            return "FAIL";
+        }());
+    }
+    expect: {
+        console.log(function() {
+            for (var a in /[abc4]/g.exec("a"))
+                return "PASS";
+            return "FAIL";
+        }());
+    }
+    expect_stdout: "PASS"
+}