fix corner case in `loops` (#4275)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 13 Nov 2020 18:08:05 +0000 (18:08 +0000)
committerGitHub <noreply@github.com>
Fri, 13 Nov 2020 18:08:05 +0000 (02:08 +0800)
fixes #4274

lib/compress.js
test/compress/const.js
test/compress/let.js

index f44552a..97252f7 100644 (file)
@@ -1166,7 +1166,9 @@ merge(Compressor.prototype, {
 
     function as_statement_array(thing) {
         if (thing === null) return [];
-        if (thing instanceof AST_BlockStatement) return thing.body;
+        if (thing instanceof AST_BlockStatement) return all(thing.body, function(stat) {
+            return !(stat instanceof AST_Const || stat instanceof AST_Let);
+        }) ? thing.body : [ thing ];
         if (thing instanceof AST_EmptyStatement) return [];
         if (thing instanceof AST_Statement) return [ thing ];
         throw new Error("Can't convert thing to statement array");
index 9ffb0d3..333171b 100644 (file)
@@ -1175,3 +1175,53 @@ issue_4261: {
     }
     expect_stdout: "42"
 }
+
+issue_4274_1: {
+    options = {
+        loops: true,
+    }
+    input: {
+        for (;;) {
+            if (console.log("PASS")) {
+                const a = 0;
+            } else {
+                break;
+                var a;
+            }
+        }
+    }
+    expect: {
+        for (; console.log("PASS");) {
+            {
+                const a = 0;
+            }
+            var a;
+        }
+    }
+    expect_stdout: true
+}
+
+issue_4274_2: {
+    options = {
+        loops: true,
+    }
+    input: {
+        for (;;) {
+            if (!console.log("PASS")) {
+                break;
+                var a;
+            } else {
+                const a = 0;
+            }
+        }
+    }
+    expect: {
+        for (; console.log("PASS");) {
+            {
+                const a = 0;
+            }
+            var a;
+        }
+    }
+    expect_stdout: true
+}
index 9a6fc94..728e40b 100644 (file)
@@ -950,3 +950,59 @@ issue_4248: {
     expect_stdout: "PASS"
     node_version: ">=4"
 }
+
+issue_4274_1: {
+    options = {
+        loops: true,
+    }
+    input: {
+        "use strict";
+        for (;;) {
+            if (console.log("PASS")) {
+                let a;
+            } else {
+                break;
+                var a;
+            }
+        }
+    }
+    expect: {
+        "use strict";
+        for (; console.log("PASS");) {
+            {
+                let a;
+            }
+            var a;
+        }
+    }
+    expect_stdout: "PASS"
+    node_version: ">=4"
+}
+
+issue_4274_2: {
+    options = {
+        loops: true,
+    }
+    input: {
+        "use strict";
+        for (;;) {
+            if (!console.log("PASS")) {
+                break;
+                var a;
+            } else {
+                let a;
+            }
+        }
+    }
+    expect: {
+        "use strict";
+        for (; console.log("PASS");) {
+            {
+                let a;
+            }
+            var a;
+        }
+    }
+    expect_stdout: "PASS"
+    node_version: ">=4"
+}