fix `reduce_vars` on `do...while` (#2596)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 15 Dec 2017 08:33:19 +0000 (16:33 +0800)
committerGitHub <noreply@github.com>
Fri, 15 Dec 2017 08:33:19 +0000 (16:33 +0800)
lib/compress.js
test/compress/reduce_vars.js

index aaadcd1..0162fa4 100644 (file)
@@ -479,7 +479,17 @@ merge(Compressor.prototype, {
                     }
                     return true;
                 }
-                if (node instanceof AST_DWLoop) {
+                if (node instanceof AST_Do) {
+                    var saved_loop = in_loop;
+                    in_loop = node;
+                    push();
+                    node.body.walk(tw);
+                    node.condition.walk(tw);
+                    pop();
+                    in_loop = saved_loop;
+                    return true;
+                }
+                if (node instanceof AST_While) {
                     var saved_loop = in_loop;
                     in_loop = node;
                     push();
index 9ed9c97..394bb58 100644 (file)
@@ -4867,3 +4867,35 @@ defun_single_use_loop: {
         "true",
     ]
 }
+
+do_while: {
+    options = {
+        evaluate: true,
+        reduce_vars: true,
+    }
+    input: {
+        function f(a) {
+            do {
+                (function() {
+                    a && (c = "PASS");
+                })();
+            } while (a = 0);
+        }
+        var c = "FAIL";
+        f(1);
+        console.log(c);
+    }
+    expect: {
+        function f(a) {
+            do {
+                (function() {
+                    a && (c = "PASS");
+                })();
+            } while (a = 0);
+        }
+        var c = "FAIL";
+        f(1);
+        console.log(c);
+    }
+    expect_stdout: "PASS"
+}