[Pkg-javascript-commits] [uglifyjs] 258/491: maintain call argument order in `collapse_vars` (#2426)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:42 UTC 2018


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.

commit fe5a68f9d5a93557d44c48cd0a8ee533e2bd1a47
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Sun Nov 5 00:00:18 2017 +0800

    maintain call argument order in `collapse_vars` (#2426)
    
    fixes #2425
---
 lib/compress.js                | 22 +++++++++++---
 test/compress/collapse_vars.js | 66 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 4 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index f09f2b9..1b4a1f7 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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) {
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index b5b97d2..e2c5f1b 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -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"
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list