fix recursive function `inline` (#2738)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 7 Jan 2018 07:31:24 +0000 (15:31 +0800)
committerGitHub <noreply@github.com>
Sun, 7 Jan 2018 07:31:24 +0000 (15:31 +0800)
fixes #2737

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

index 28d6a1c..011bb1b 100644 (file)
@@ -4075,9 +4075,10 @@ merge(Compressor.prototype, {
             if (compressor.option("inline")
                 && !fn.uses_arguments
                 && !fn.uses_eval
+                && !(fn.name && fn instanceof AST_Function)
                 && (value = can_flatten_body(stat))
-                && (exp === fn ? !fn.name
-                    : compressor.option("unused")
+                && (exp === fn
+                    || compressor.option("unused")
                         && (def = exp.definition()).references.length == 1
                         && !recursive_ref(compressor, def)
                         && fn.is_constant_expression(exp.scope))
index f466208..222aa0c 100644 (file)
@@ -1905,3 +1905,49 @@ duplicate_arg_var: {
     }
     expect_stdout: "PASS"
 }
+
+issue_2737_1: {
+    options = {
+        inline: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        (function(a) {
+            while (a());
+        })(function f() {
+            console.log(typeof f);
+        });
+    }
+    expect: {
+        (function(a) {
+            while (a());
+        })(function f() {
+            console.log(typeof f);
+        });
+    }
+    expect_stdout: "function"
+}
+
+issue_2737_2: {
+    options = {
+        inline: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        (function(bar) {
+            for (;bar(); ) break;
+        })(function qux() {
+            return console.log("PASS"), qux;
+        });
+    }
+    expect: {
+        (function(bar) {
+            for (;bar(); ) break;
+        })(function qux() {
+            return console.log("PASS"), qux;
+        });
+    }
+    expect_stdout: "PASS"
+}