fix corner cases in `inline` (#3834)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 1 May 2020 01:06:40 +0000 (02:06 +0100)
committerGitHub <noreply@github.com>
Fri, 1 May 2020 01:06:40 +0000 (09:06 +0800)
fixes #3833
fixes #3835

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

index 15db233..79f5278 100644 (file)
@@ -3991,10 +3991,17 @@ merge(Compressor.prototype, {
                 if (!call || call.TYPE != "Call") break;
                 var fn = call.expression;
                 if (fn instanceof AST_SymbolRef) {
+                    if (self.name && self.name.definition() === fn.definition()) break;
                     fn = fn.fixed_value();
                 }
                 if (!(fn instanceof AST_Lambda)) break;
                 if (fn.uses_arguments) break;
+                if (fn === call.expression) {
+                    if (fn.parent_scope !== self) break;
+                    if (!all(fn.enclosed, function(def) {
+                        return def.scope !== self;
+                    })) break;
+                }
                 if (fn.contains_this()) break;
                 var j = fn.argnames.length;
                 if (j > 0 && compressor.option("inline") < 2) break;
index 262bb6a..f5ad08b 100644 (file)
@@ -4590,3 +4590,50 @@ substitude_use_strict: {
         "PASS",
     ]
 }
+
+issue_3833: {
+    options = {
+        inline: true,
+        keep_fargs: "strict",
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        function f(a) {
+            return function() {
+                while (a);
+                console.log("PASS");
+            }();
+        }
+        f();
+    }
+    expect: {
+        (function() {
+            while (a);
+            console.log("PASS");
+        })();
+        var a;
+    }
+    expect_stdout: "PASS"
+}
+
+issue_3835: {
+    options = {
+        inline: true,
+        reduce_vars: true,
+    }
+    input: {
+        (function f() {
+            return function() {
+                return f();
+            }();
+        })();
+    }
+    expect: {
+        (function f() {
+            return f();
+        })();
+    }
+    expect_stdout: true
+}