fix corner case in `collapse_vars` (#4217)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 13 Oct 2020 23:18:26 +0000 (00:18 +0100)
committerGitHub <noreply@github.com>
Tue, 13 Oct 2020 23:18:26 +0000 (07:18 +0800)
fixes #4216

lib/compress.js
lib/output.js
test/compress/const.js

index baa501f..b2ed17c 100644 (file)
@@ -1501,9 +1501,9 @@ merge(Compressor.prototype, {
                 // Skip (non-executed) functions
                 if (node instanceof AST_Scope) return node;
                 // Stop upon collision with block-scoped variables
-                if (node.variables && !node.variables.all(function(def) {
+                if (!(node.variables && node.variables.all(function(def) {
                     return !lvalues.has(def.name);
-                })) {
+                }))) {
                     abort = true;
                     return node;
                 }
index 05baa23..7077d6d 100644 (file)
@@ -1385,8 +1385,6 @@ function OutputStream(options) {
             make_block(stat, output);
         } else if (!stat || stat instanceof AST_EmptyStatement) {
             output.force_semicolon();
-        } else if (stat instanceof AST_Const) {
-            make_block(stat, output);
         } else {
             stat.print(output);
         }
index 4411cbf..27c55dd 100644 (file)
@@ -1009,3 +1009,26 @@ issue_4212_2: {
     }
     expect_stdout: true
 }
+
+issue_4216: {
+    options = {
+        collapse_vars: true,
+        conditionals: true,
+        dead_code: true,
+        evaluate: true,
+    }
+    input: {
+        if (a = 0) {
+            const a = 0;
+        }
+        console.log(typeof a);
+    }
+    expect: {
+        a = 0;
+        {
+            const a = void 0;
+        }
+        console.log(typeof a);
+    }
+    expect_stdout: true
+}