fix corner case in `reduce_vars` (#4203)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 12 Oct 2020 11:02:44 +0000 (12:02 +0100)
committerGitHub <noreply@github.com>
Mon, 12 Oct 2020 11:02:44 +0000 (19:02 +0800)
fixes #4198

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

index fc65e42..f930dd8 100644 (file)
@@ -6640,7 +6640,13 @@ merge(Compressor.prototype, {
                 if (def.scope === scope) return true;
                 return !scope.variables.has(node.name) && !scope.globals.has(node.name);
             }
-            return def.scope === scope || !scope.find_variable(node);
+            if (def.scope === scope) return true;
+            var s = def.scope;
+            do {
+                s = s.parent_scope;
+                if (s.variables.has(node.name)) return false;
+            } while (s !== scope);
+            return true;
         }) ? make_node(AST_Var, self, {
             definitions: self.definitions.map(function(defn) {
                 var name = make_node(AST_SymbolVar, defn.name, defn.name);
index f2b9224..72033f4 100644 (file)
@@ -805,3 +805,38 @@ issue_4197: {
     }
     expect_stdout: "1"
 }
+
+issue_4198: {
+    options = {
+        reduce_vars: true,
+    }
+    input: {
+        console.log(function() {
+            try {
+                throw "PASS";
+            } catch (e) {
+                {
+                    const e = "FAIL";
+                }
+                return function() {
+                    return e;
+                }();
+            }
+        }());
+    }
+    expect: {
+        console.log(function() {
+            try {
+                throw "PASS";
+            } catch (e) {
+                {
+                    const e = "FAIL";
+                }
+                return function() {
+                    return e;
+                }();
+            }
+        }());
+    }
+    expect_stdout: "PASS"
+}