improve `collapse_vars` on side-effect-free replacements (#2583)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 12 Dec 2017 20:52:54 +0000 (04:52 +0800)
committerGitHub <noreply@github.com>
Tue, 12 Dec 2017 20:52:54 +0000 (04:52 +0800)
lib/compress.js
test/compress/collapse_vars.js

index 00d03fd..6aee949 100644 (file)
@@ -893,10 +893,11 @@ merge(Compressor.prototype, {
                     || node instanceof AST_Call && lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression)
                     || node instanceof AST_Debugger
                     || node instanceof AST_IterationStatement && !(node instanceof AST_For)
-                    || node instanceof AST_SymbolRef && !node.is_declared(compressor)
                     || node instanceof AST_Try
                     || node instanceof AST_With
-                    || parent instanceof AST_For && node !== parent.init) {
+                    || parent instanceof AST_For && node !== parent.init
+                    || (side_effects || !replace_all)
+                        && (node instanceof AST_SymbolRef && !node.is_declared(compressor))) {
                     abort = true;
                     return node;
                 }
index 330667d..5840580 100644 (file)
@@ -3805,3 +3805,51 @@ may_throw: {
         }
     }
 }
+
+side_effect_free_replacement: {
+    options = {
+        collapse_vars: true,
+        inline: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        var b;
+        (function(a) {
+            x(a);
+        })(b);
+    }
+    expect: {
+        var b;
+        x(b);
+    }
+}
+
+recursive_function_replacement: {
+    rename = true
+    options = {
+        collapse_vars: true,
+        inline: true,
+        passes: 2,
+        reduce_funcs: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    mangle = {}
+    input: {
+        function f(a) {
+            return x(g(a));
+        }
+        function g(a) {
+            return y(f(a));
+        }
+        console.log(f(c));
+    }
+    expect: {
+        function f(n) {
+            return x(y(f(n)));
+        }
+        console.log(f(c));
+    }
+}