maintain call argument order in `collapse_vars` (#2426)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 4 Nov 2017 16:00:18 +0000 (00:00 +0800)
committerGitHub <noreply@github.com>
Sat, 4 Nov 2017 16:00:18 +0000 (00:00 +0800)
fixes #2425

lib/compress.js
test/compress/collapse_vars.js

index f09f2b9..1b4a1f7 100644 (file)
@@ -808,6 +808,7 @@ merge(Compressor.prototype, {
         function collapse(statements, compressor) {
             var scope = compressor.find_parent(AST_Scope);
             if (scope.uses_eval || scope.uses_with) return statements;
+            var args;
             var candidates = [];
             var stat_index = statements.length;
             while (--stat_index >= 0) {
@@ -828,7 +829,7 @@ merge(Compressor.prototype, {
                     var one_off = lhs instanceof AST_Symbol && lhs.definition().references.length == 1;
                     var side_effects = value_has_side_effects(candidate);
                     var hit = candidate.name instanceof AST_SymbolFunarg;
-                    var abort = false, replaced = false;
+                    var abort = false, replaced = false, can_replace = !args || !hit;
                     var tt = new TreeTransformer(function(node, descend) {
                         if (abort) return node;
                         // Skip nodes before `candidate` as quickly as possible
@@ -853,7 +854,8 @@ merge(Compressor.prototype, {
                             return node;
                         }
                         // Replace variable with assignment when found
-                        if (!(node instanceof AST_SymbolDeclaration)
+                        if (can_replace
+                            && !(node instanceof AST_SymbolDeclaration)
                             && !is_lhs(node, parent)
                             && lhs.equivalent_to(node)) {
                             CHANGED = replaced = abort = true;
@@ -904,6 +906,12 @@ merge(Compressor.prototype, {
                         // Skip (non-executed) functions and (leading) default case in switch statements
                         if (node instanceof AST_Default || node instanceof AST_Scope) return node;
                     });
+                    if (!can_replace) {
+                        for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; j < args.length; j++) {
+                            args[j].transform(tt);
+                        }
+                        can_replace = true;
+                    }
                     for (var i = stat_index; !abort && i < statements.length; i++) {
                         statements[i].transform(tt);
                     }
@@ -921,12 +929,18 @@ merge(Compressor.prototype, {
                     && iife.expression === fn) {
                     var fn_strict = compressor.has_directive("use strict");
                     if (fn_strict && fn.body.indexOf(fn_strict) < 0) fn_strict = false;
+                    var len = fn.argnames.length;
+                    args = iife.args.slice(len);
                     var names = Object.create(null);
-                    for (var i = fn.argnames.length; --i >= 0;) {
+                    for (var i = len; --i >= 0;) {
                         var sym = fn.argnames[i];
+                        var arg = iife.args[i];
+                        args.unshift(make_node(AST_VarDef, sym, {
+                            name: sym,
+                            value: arg
+                        }));
                         if (sym.name in names) continue;
                         names[sym.name] = true;
-                        var arg = iife.args[i];
                         if (!arg) arg = make_node(AST_Undefined, sym).transform(compressor);
                         else {
                             var tw = new TreeWalker(function(node) {
index b5b97d2..e2c5f1b 100644 (file)
@@ -2949,3 +2949,69 @@ conditional_2: {
     }
     expect_stdout: "5 5"
 }
+
+issue_2425_1: {
+    options = {
+        collapse_vars: true,
+        unused: true,
+    }
+    input: {
+        var a = 8;
+        (function(b) {
+            b.toString();
+        })(--a, a |= 10);
+        console.log(a);
+    }
+    expect: {
+        var a = 8;
+        (function(b) {
+            b.toString();
+        })(--a, a |= 10);
+        console.log(a);
+    }
+    expect_stdout: "15"
+}
+
+issue_2425_2: {
+    options = {
+        collapse_vars: true,
+        unused: true,
+    }
+    input: {
+        var a = 8;
+        (function(b, c) {
+            b.toString();
+        })(--a, a |= 10);
+        console.log(a);
+    }
+    expect: {
+        var a = 8;
+        (function(b, c) {
+            b.toString();
+        })(--a, a |= 10);
+        console.log(a);
+    }
+    expect_stdout: "15"
+}
+
+issue_2425_3: {
+    options = {
+        collapse_vars: true,
+        unused: true,
+    }
+    input: {
+        var a = 8;
+        (function(b, b) {
+            b.toString();
+        })(--a, a |= 10);
+        console.log(a);
+    }
+    expect: {
+        var a = 8;
+        (function(b, b) {
+            (a |= 10).toString();
+        })(--a);
+        console.log(a);
+    }
+    expect_stdout: "15"
+}