fix corner case in `collapse_vars` (#4206)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 12 Oct 2020 17:30:21 +0000 (18:30 +0100)
committerGitHub <noreply@github.com>
Mon, 12 Oct 2020 17:30:21 +0000 (01:30 +0800)
fixes #4205

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

index f6de865..bc01ebc 100644 (file)
@@ -1494,14 +1494,22 @@ merge(Compressor.prototype, {
             }
 
             function handle_custom_scan_order(node, tt) {
+                if (!(node instanceof AST_BlockScope)) return;
+                // 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) {
+                    return !lvalues.has(def.name);
+                })) {
+                    abort = true;
+                    return node;
+                }
                 // Scan object only in a for-in statement
                 if (node instanceof AST_ForIn) {
                     node.object = node.object.transform(tt);
                     abort = true;
                     return node;
                 }
-                // Skip (non-executed) functions
-                if (node instanceof AST_Scope) return node;
                 // Scan first case expression only in a switch statement
                 if (node instanceof AST_Switch) {
                     node.expression = node.expression.transform(tt);
index 28dda01..d1164cc 100644 (file)
@@ -872,3 +872,40 @@ issue_4202: {
     }
     expect_stdout: "42"
 }
+
+issue_4205: {
+    options = {
+        collapse_vars: true,
+    }
+    input: {
+        var a = function(b) {
+            var c = function() {
+                switch (0) {
+                  case a:
+                    return 0;
+                  case b:
+                  case console.log("PASS"):
+                }
+            }();
+            {
+                const b = c;
+            }
+        }();
+    }
+    expect: {
+        var a = function(b) {
+            var c = function() {
+                switch (0) {
+                  case a:
+                    return 0;
+                  case b:
+                  case console.log("PASS"):
+                }
+            }();
+            {
+                const b = c;
+            }
+        }();
+    }
+    expect_stdout: true
+}