fix corner case in `functions` (#4234)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 22 Oct 2020 02:13:11 +0000 (03:13 +0100)
committerGitHub <noreply@github.com>
Thu, 22 Oct 2020 02:13:11 +0000 (10:13 +0800)
fixes #4233

lib/compress.js
test/compress/functions.js

index b82fcca..ec9fd2a 100644 (file)
@@ -4774,7 +4774,9 @@ merge(Compressor.prototype, {
                     node.definitions.forEach(function(defn) {
                         var def = defn.name.definition();
                         var_defs_by_id.add(def.id, defn);
-                        if ((!drop_vars || (node instanceof AST_Const ? def.redefined() : def.const_redefs))
+                        var redef = def.redefined();
+                        if (redef && node instanceof AST_Var) var_defs_by_id.add(redef.id, defn);
+                        if ((!drop_vars || (node instanceof AST_Const ? redef : def.const_redefs))
                             && !(def.id in in_use_ids)) {
                             in_use_ids[def.id] = true;
                             in_use.push(def);
index 4e65b12..4ed0dc1 100644 (file)
@@ -5076,3 +5076,42 @@ issue_4186: {
     }
     expect_stdout: "function"
 }
+
+issue_4233: {
+    options = {
+        functions: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        (function() {
+            try {
+                var a = function() {};
+                try {
+                    throw 42;
+                } catch (a) {
+                    (function() {
+                        console.log(typeof a);
+                    })();
+                    var a;
+                }
+            } catch (e) {}
+        })();
+    }
+    expect: {
+        (function() {
+            try {
+                var a = function() {};
+                try {
+                    throw 42;
+                } catch (a) {
+                    (function() {
+                        console.log(typeof a);
+                    })();
+                    var a;
+                }
+            } catch (e) {}
+        })();
+    }
+    expect_stdout: "number"
+}