diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-11-05 00:00:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-05 00:00:18 +0800 |
commit | fe5a68f9d5a93557d44c48cd0a8ee533e2bd1a47 (patch) | |
tree | 8e228eb21e078f380f4e9d47c7b2c17e74cd9d4e /lib | |
parent | 71e61153b1b3d5f2446b83a3507231509e11e90e (diff) | |
download | tracifyjs-fe5a68f9d5a93557d44c48cd0a8ee533e2bd1a47.tar.gz tracifyjs-fe5a68f9d5a93557d44c48cd0a8ee533e2bd1a47.zip |
maintain call argument order in `collapse_vars` (#2426)
fixes #2425
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/lib/compress.js b/lib/compress.js index f09f2b9b..1b4a1f77 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) { |