handle `break` & `continue` in `collapse_vars` (#2875)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 2 Feb 2018 23:58:43 +0000 (07:58 +0800)
committerGitHub <noreply@github.com>
Fri, 2 Feb 2018 23:58:43 +0000 (07:58 +0800)
fixes #2873

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

index 6f9d64f..993092a 100644 (file)
@@ -968,6 +968,7 @@ merge(Compressor.prototype, {
                     || node instanceof AST_Call && lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression)
                     || node instanceof AST_Debugger
                     || node instanceof AST_IterationStatement && !(node instanceof AST_For)
+                    || node instanceof AST_LoopControl
                     || node instanceof AST_Try
                     || node instanceof AST_With
                     || parent instanceof AST_For && node !== parent.init
index a2571c2..ad09edc 100644 (file)
@@ -4383,3 +4383,53 @@ cond_branch_switch: {
     }
     expect_stdout: "1"
 }
+
+issue_2873_1: {
+    options = {
+        collapse_vars: true,
+    }
+    input: {
+        var b = 1, c = 0;
+        do {
+            c++;
+            if (!--b) break;
+            c = 1 + c;
+        } while (0);
+        console.log(b, c);
+    }
+    expect: {
+        var b = 1, c = 0;
+        do {
+            c++;
+            if (!--b) break;
+            c = 1 + c;
+        } while (0);
+        console.log(b, c);
+    }
+    expect_stdout: "0 1"
+}
+
+issue_2873_2: {
+    options = {
+        collapse_vars: true,
+    }
+    input: {
+        var b = 1, c = 0;
+        do {
+            c++;
+            if (!--b) continue;
+            c = 1 + c;
+        } while (0);
+        console.log(b, c);
+    }
+    expect: {
+        var b = 1, c = 0;
+        do {
+            c++;
+            if (!--b) continue;
+            c = 1 + c;
+        } while (0);
+        console.log(b, c);
+    }
+    expect_stdout: "0 1"
+}