enhance `collapse_vars` (#3680)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 9 Jan 2020 20:28:43 +0000 (04:28 +0800)
committerGitHub <noreply@github.com>
Thu, 9 Jan 2020 20:28:43 +0000 (04:28 +0800)
closes #3679

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

index c596de3..e645bd4 100644 (file)
@@ -1356,7 +1356,10 @@ merge(Compressor.prototype, {
                     return node.operator != "=" && lhs.equivalent_to(node.left);
                 }
                 if (node instanceof AST_Call) {
-                    return lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression);
+                    if (!(lhs instanceof AST_PropAccess)) return false;
+                    if (!lhs.equivalent_to(node.expression)) return false;
+                    var rhs = get_rvalue(candidate);
+                    return !(rhs instanceof AST_Function && !rhs.contains_this());
                 }
                 if (node instanceof AST_Debugger) return true;
                 if (node instanceof AST_Defun) return funarg && lhs.name === node.name.name;
index 3b9def3..c8bced5 100644 (file)
@@ -3700,3 +3700,88 @@ pr_3595_4: {
     }
     expect_stdout: "PASS"
 }
+
+issue_3679_1: {
+    options = {
+        collapse_vars: true,
+        inline: true,
+        pure_getters: "strict",
+        reduce_vars: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        (function() {
+            var f = function() {};
+            f.g = function() {
+                console.log("PASS");
+            };
+            f.g();
+        })();
+    }
+    expect: {
+        console.log("PASS");
+    }
+    expect_stdout: "PASS"
+}
+
+issue_3679_2: {
+    options = {
+        collapse_vars: true,
+        inline: true,
+        passes: 2,
+        pure_getters: "strict",
+        reduce_vars: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        (function() {
+            "use strict";
+            var f = function() {};
+            f.g = function() {
+                console.log("PASS");
+            };
+            f.g();
+        })();
+    }
+    expect: {
+        (function() {
+            "use strict";
+            console.log("PASS");
+        })();
+    }
+    expect_stdout: "PASS"
+}
+
+issue_3679_3: {
+    options = {
+        collapse_vars: true,
+        inline: true,
+        functions: true,
+        pure_getters: "strict",
+        reduce_vars: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        (function() {
+            var f = function() {};
+            f.p = "PASS";
+            f.g = function() {
+                console.log(f.p);
+            };
+            f.g();
+        })();
+    }
+    expect: {
+        (function() {
+            function f() {};
+            f.p = "PASS";
+            (f.g = function() {
+                console.log(f.p);
+            })();
+        })();
+    }
+    expect_stdout: "PASS"
+}