fix corner case in `reduce_vars` (#5121)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 25 Aug 2021 02:39:35 +0000 (03:39 +0100)
committerGitHub <noreply@github.com>
Wed, 25 Aug 2021 02:39:35 +0000 (10:39 +0800)
fixes #5120

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

index fa1e121..91aee40 100644 (file)
@@ -11231,21 +11231,20 @@ merge(Compressor.prototype, {
                         if (!(node instanceof AST_SymbolRef)) return;
                         var def = node.definition();
                         if (def === defun_def) {
-                            node.thedef = lambda_def;
-                            lambda_def.references.push(node);
+                            node.thedef = def = lambda_def;
                         } else {
                             def.single_use = false;
                             var fn = node.fixed_value();
-                            if (!is_lambda(fn)) return;
-                            if (!fn.name) return;
-                            if (fn.name.definition() !== def) return;
-                            if (def.scope !== fn.name.scope) return;
-                            if (fixed.variables.get(fn.name.name) !== def) return;
-                            fn.name = fn.name.clone();
-                            var value_def = value.variables.get(fn.name.name) || value[def_fn_name](fn.name);
-                            node.thedef = value_def;
-                            value_def.references.push(node);
+                            if (is_lambda(fn)
+                                && fn.name
+                                && fn.name.definition() === def
+                                && def.scope === fn.name.scope
+                                && fixed.variables.get(fn.name.name) === def) {
+                                fn.name = fn.name.clone();
+                                node.thedef = def = value.variables.get(fn.name.name) || value[def_fn_name](fn.name);
+                            }
                         }
+                        def.references.push(node);
                     }));
                 } else {
                     if (fixed instanceof AST_Scope) {
index e945e13..084e005 100644 (file)
@@ -99,8 +99,8 @@ issue_4664: {
     expect: {
         (function f() {
             new function(a) {
-                console.log(typeof f, 1073741824, typeof this);
-            }(A = 0);
+                console.log(typeof f, a, typeof this);
+            }((A = 0, 2 ** 30));
         })();
     }
     expect_stdout: "function 1073741824 object"
index 05d175c..6f59666 100644 (file)
@@ -6600,3 +6600,32 @@ shorter_without_void: {
         "baz",
     ]
 }
+
+issue_5120: {
+    options = {
+        functions: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var a = function f() {
+            function g() {
+                f || g();
+            }
+            g();
+            return f.valueOf();
+        };
+        console.log(a() === a ? "PASS" : "FAIL");
+    }
+    expect: {
+        function a() {
+            (function g() {
+                a || g();
+            })();
+            return a.valueOf();
+        }
+        console.log(a() === a ? "PASS" : "FAIL");
+    }
+    expect_stdout: "PASS"
+}