Skip to content

Commit

Permalink
handle duplicate argument names in collapse_vars (#2215)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Jul 7, 2017
1 parent 4f70d2e commit 71ee91e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,11 @@ merge(Compressor.prototype, {
&& !fn.uses_eval
&& (iife = compressor.parent()) instanceof AST_Call
&& iife.expression === fn) {
fn.argnames.forEach(function(sym, i) {
var names = Object.create(null);
for (var i = fn.argnames.length; --i >= 0;) {
var sym = fn.argnames[i];
if (sym.name in names) continue;
names[sym.name] = true;
var arg = iife.args[i];
if (!arg) arg = make_node(AST_Undefined, sym);
else {
Expand All @@ -840,11 +844,11 @@ merge(Compressor.prototype, {
});
arg.walk(tw);
}
if (arg) candidates.push(make_node(AST_VarDef, sym, {
if (arg) candidates.unshift(make_node(AST_VarDef, sym, {
name: sym,
value: arg
}));
});
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -2320,3 +2320,25 @@ issue_2203_2: {
}
expect_stdout: "PASS"
}

duplicate_argname: {
options = {
collapse_vars: true,
unused: true,
}
input: {
function f() { return "PASS"; }
console.log(function(a, a) {
f++;
return a;
}("FAIL", f()));
}
expect: {
function f() { return "PASS"; }
console.log(function(a, a) {
f++;
return a;
}("FAIL", f()));
}
expect_stdout: "PASS"
}

0 comments on commit 71ee91e

Please sign in to comment.