diff options
-rw-r--r-- | lib/compress.js | 31 | ||||
-rw-r--r-- | test/compress/functions.js | 18 |
2 files changed, 35 insertions, 14 deletions
diff --git a/lib/compress.js b/lib/compress.js index 481af929..3a3aac4f 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4184,16 +4184,18 @@ merge(Compressor.prototype, { function append_var(decls, expressions, name, value) { var def = name.definition(); - scope.var_names()[name.name] = true; scope.variables.set(name.name, def); scope.enclosed.push(def); - decls.unshift(make_node(AST_VarDef, name, { - name: name, - value: null - })); + if (!scope.var_names()[name.name]) { + scope.var_names()[name.name] = true; + decls.push(make_node(AST_VarDef, name, { + name: name, + value: null + })); + } var sym = make_node(AST_SymbolRef, name, name); def.references.push(sym); - if (value) expressions.unshift(make_node(AST_Assign, self, { + if (value) expressions.push(make_node(AST_Assign, self, { operator: "=", left: sym, right: value @@ -4203,13 +4205,13 @@ merge(Compressor.prototype, { function flatten_args(decls, expressions) { var len = fn.argnames.length; for (var i = self.args.length; --i >= len;) { - expressions.unshift(self.args[i]); + expressions.push(self.args[i]); } for (i = len; --i >= 0;) { var name = fn.argnames[i]; var value = self.args[i]; if (name.__unused || scope.var_names()[name.name]) { - if (value) expressions.unshift(value); + if (value) expressions.push(value); } else { var symbol = make_node(AST_SymbolVar, name, name); name.definition().orig.push(symbol); @@ -4217,14 +4219,16 @@ merge(Compressor.prototype, { append_var(decls, expressions, symbol, value); } } + decls.reverse(); + expressions.reverse(); } function flatten_vars(decls, expressions) { - if (in_loop) in_loop.length = 0; - for (var i = fn.body.length; --i >= 0;) { + var pos = expressions.length; + for (var i = 0, lines = fn.body.length; i < lines; i++) { var stat = fn.body[i]; if (!(stat instanceof AST_Var)) continue; - for (var j = stat.definitions.length; --j >= 0;) { + for (var j = 0, defs = stat.definitions.length; j < defs; j++) { var var_def = stat.definitions[j]; var name = var_def.name; append_var(decls, expressions, name, var_def.value); @@ -4232,7 +4236,7 @@ merge(Compressor.prototype, { var def = name.definition(); var sym = make_node(AST_SymbolRef, name, name); def.references.push(sym); - in_loop.unshift(make_node(AST_Assign, var_def, { + expressions.splice(pos++, 0, make_node(AST_Assign, var_def, { operator: "=", left: sym, right: make_node(AST_Undefined, name) @@ -4240,14 +4244,13 @@ merge(Compressor.prototype, { } } } - if (in_loop) [].unshift.apply(expressions, in_loop); } function flatten_fn() { var decls = []; var expressions = []; - flatten_vars(decls, expressions); flatten_args(decls, expressions); + flatten_vars(decls, expressions); expressions.push(value); if (decls.length) { i = scope.body.indexOf(compressor.parent(level - 1)) + 1; diff --git a/test/compress/functions.js b/test/compress/functions.js index 5b0c49b8..f4662082 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -1887,3 +1887,21 @@ use_before_init_in_loop: { } expect_stdout: "PASS" } + +duplicate_arg_var: { + options = { + inline: true, + toplevel: true, + } + input: { + console.log(function(b) { + return b; + var b; + }("PASS")); + } + expect: { + console.log((b = "PASS", b)); + var b; + } + expect_stdout: "PASS" +} |