fix corner case in `functions` & `reduce_vars` (#4452)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 25 Dec 2020 00:38:24 +0000 (00:38 +0000)
committerGitHub <noreply@github.com>
Fri, 25 Dec 2020 00:38:24 +0000 (08:38 +0800)
fixes #4451

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

index a661577..33aa5b9 100644 (file)
@@ -933,10 +933,10 @@ merge(Compressor.prototype, {
                         def.fixed = false;
                     }
                 }, true);
-            } else if (init instanceof AST_SymbolRef && !init.is_immutable()) {
+            } else if (init instanceof AST_SymbolRef) {
                 var def = init.definition();
                 def.assignments++;
-                def.fixed = false;
+                if (!init.is_immutable()) def.fixed = false;
             }
             this.body.walk(tw);
             pop(tw);
index 837d0a0..066bc3c 100644 (file)
@@ -5134,8 +5134,8 @@ issue_4259: {
         console.log(typeof a);
     }
     expect: {
-        function a() {
-            for (a in a);
+        var a = function b() {
+            for (b in b);
         }
         a();
         console.log(typeof a);
@@ -5225,3 +5225,27 @@ trailing_comma: {
     expect_exact: 'new function(a,b){console.log(b,a)}(42,"PASS");'
     expect_stdout: "PASS 42"
 }
+
+issue_4451: {
+    options = {
+        functions: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var a = function f() {
+            for (f in "foo")
+                return f;
+        };
+        while (console.log(typeof a()));
+    }
+    expect: {
+        var a = function f() {
+            for (f in "foo")
+                return f;
+        };
+        while (console.log(typeof a()));
+    }
+    expect_stdout: "function"
+}