aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-11-13 18:08:05 +0000
committerGitHub <noreply@github.com>2020-11-14 02:08:05 +0800
commit6fd5b5b371dd805e679042916c7c0fae79798d75 (patch)
treec12e4c47ed2a26bcb8589b93657307fb25db20e3
parentfba27bfb71cd973f76e8d1007edec935f25ace77 (diff)
downloadtracifyjs-6fd5b5b371dd805e679042916c7c0fae79798d75.tar.gz
tracifyjs-6fd5b5b371dd805e679042916c7c0fae79798d75.zip
fix corner case in `loops` (#4275)
fixes #4274
-rw-r--r--lib/compress.js4
-rw-r--r--test/compress/const.js50
-rw-r--r--test/compress/let.js56
3 files changed, 109 insertions, 1 deletions
diff --git a/lib/compress.js b/lib/compress.js
index f44552aa..97252f7f 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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");
diff --git a/test/compress/const.js b/test/compress/const.js
index 9ffb0d34..333171b4 100644
--- a/test/compress/const.js
+++ b/test/compress/const.js
@@ -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
+}
diff --git a/test/compress/let.js b/test/compress/let.js
index 9a6fc944..728e40b4 100644
--- a/test/compress/let.js
+++ b/test/compress/let.js
@@ -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"
+}