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) {
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
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;
// 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);
}
&& 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) {