fix corner case in `functions` (#4789)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 16 Mar 2021 12:58:51 +0000 (12:58 +0000)
committerGitHub <noreply@github.com>
Tue, 16 Mar 2021 12:58:51 +0000 (20:58 +0800)
fixes #4788

lib/compress.js
test/compress/functions.js
test/compress/hoist_vars.js
test/ufuzz/index.js

index 2dc6fab..ac66ce6 100644 (file)
@@ -6279,11 +6279,15 @@ merge(Compressor.prototype, {
                     function can_rename(fn, name) {
                         if (!fn.name) return !fn.variables.get(name);
                         old_def = fn.name.definition();
-                        if (old_def.assignments > 0) return false;
-                        if (old_def.name == name) return true;
+                        if (old_def.orig.length > 1) {
+                            old_def = null;
+                        } else {
+                            if (old_def.assignments > 0) return false;
+                            if (old_def.name == name) return true;
+                        }
                         if (name == "await" && is_async(fn)) return false;
                         if (name == "yield" && is_generator(fn)) return false;
-                        return all(old_def.references, function(ref) {
+                        return !old_def || all(old_def.references, function(ref) {
                             return ref.scope.find_variable(name) === sym;
                         });
                     }
index 68ff709..e43efce 100644 (file)
@@ -5912,3 +5912,40 @@ issue_4753_2: {
     }
     expect_stdout: "PASS"
 }
+
+issue_4788: {
+    options = {
+        evaluate: true,
+        functions: true,
+        keep_fnames: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        function f() {
+            var a = function g() {
+                if (0) {
+                    var g = 42;
+                    f();
+                }
+                g || console.log("PASS");
+            };
+            a(a);
+        }
+        f();
+    }
+    expect: {
+        (function f() {
+            function a() {
+                if (0) {
+                    var g = 42;
+                    f();
+                }
+                g || console.log("PASS");
+            }
+            a();
+        })();
+    }
+    expect_stdout: "PASS"
+}
index 305f98c..4118f76 100644 (file)
@@ -153,7 +153,7 @@ issue_4487: {
     }
     expect: {
         function a() {
-            var a = console.log(typeof a);
+            var f = console.log(typeof f);
         }
         a();
     }
index bc9e62a..f7ad29a 100644 (file)
@@ -1947,7 +1947,11 @@ function createTypeofExpr(recurmax, stmtDepth, canThrow) {
 }
 
 function createValue() {
-    return VALUES[rng(VALUES.length)];
+    var v;
+    do {
+        v = VALUES[rng(VALUES.length)];
+    } while (v == "new.target" && rng(200));
+    return v;
 }
 
 function createBinaryOp(noComma, canThrow) {