fix function name eliminiation (#1576)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 8 Mar 2017 04:39:57 +0000 (12:39 +0800)
committerGitHub <noreply@github.com>
Wed, 8 Mar 2017 04:39:57 +0000 (12:39 +0800)
Function expression can be assigned to a variable and be given a name. Ensure function name is the reduced variable before clearing it out.

fixes #1573
fixes #1575

lib/compress.js
test/compress/reduce_vars.js

index 85b457e..f423fdd 100644 (file)
@@ -2611,7 +2611,8 @@ merge(Compressor.prototype, {
                 if (compressor.option("unused")
                     && def.references.length == 1
                     && compressor.find_parent(AST_Scope) === def.scope) {
-                    if (!compressor.option("keep_fnames")) {
+                    if (!compressor.option("keep_fnames")
+                        && exp.name && exp.name.definition() === def) {
                         exp.name = null;
                     }
                     self.expression = exp;
index 53e2815..10dc9d9 100644 (file)
@@ -1122,3 +1122,25 @@ defun_label: {
         }();
     }
 }
+
+double_reference: {
+    options = {
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        function f() {
+            var g = function g() {
+                g();
+            };
+            g();
+        }
+    }
+    expect: {
+        function f() {
+            (function g() {
+                g();
+            })();
+        }
+    }
+}