handle function/variable name collisions correctly (#3451)

fixes #3439
This commit is contained in:
Alex Lam S.L
2019-10-06 08:51:38 +08:00
committed by GitHub
parent d57b606e73
commit 35338a100f
3 changed files with 71 additions and 7 deletions

View File

@@ -1293,6 +1293,7 @@ merge(Compressor.prototype, {
return lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression); return lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression);
} }
if (node instanceof AST_Debugger) return true; if (node instanceof AST_Debugger) return true;
if (node instanceof AST_Defun) return funarg && lhs.name === node.name.name;
if (node instanceof AST_IterationStatement) return !(node instanceof AST_For); if (node instanceof AST_IterationStatement) return !(node instanceof AST_For);
if (node instanceof AST_LoopControl) return true; if (node instanceof AST_LoopControl) return true;
if (node instanceof AST_Try) return true; if (node instanceof AST_Try) return true;
@@ -5226,25 +5227,26 @@ merge(Compressor.prototype, {
return return_value(stat); return return_value(stat);
} }
function var_exists(catches, name) { function var_exists(defined, name) {
return catches[name] || identifier_atom[name] || scope.var_names()[name]; return defined[name] || identifier_atom[name] || scope.var_names()[name];
} }
function can_inject_args(catches, safe_to_inject) { function can_inject_args(catches, used, safe_to_inject) {
for (var i = 0; i < fn.argnames.length; i++) { for (var i = 0; i < fn.argnames.length; i++) {
var arg = fn.argnames[i]; var arg = fn.argnames[i];
if (arg.__unused) continue; if (arg.__unused) continue;
if (!safe_to_inject || var_exists(catches, arg.name)) return false; if (!safe_to_inject || var_exists(catches, arg.name)) return false;
used[arg.name] = true;
if (in_loop) in_loop.push(arg.definition()); if (in_loop) in_loop.push(arg.definition());
} }
return true; return true;
} }
function can_inject_vars(catches, safe_to_inject) { function can_inject_vars(catches, used, safe_to_inject) {
for (var i = 0; i < fn.body.length; i++) { for (var i = 0; i < fn.body.length; i++) {
var stat = fn.body[i]; var stat = fn.body[i];
if (stat instanceof AST_Defun) { if (stat instanceof AST_Defun) {
if (!safe_to_inject || var_exists(catches, stat.name.name)) return false; if (!safe_to_inject || var_exists(used, stat.name.name)) return false;
continue; continue;
} }
if (!(stat instanceof AST_Var)) continue; if (!(stat instanceof AST_Var)) continue;
@@ -5273,8 +5275,9 @@ merge(Compressor.prototype, {
var safe_to_inject = (!(scope instanceof AST_Toplevel) || compressor.toplevel.vars) var safe_to_inject = (!(scope instanceof AST_Toplevel) || compressor.toplevel.vars)
&& (exp !== fn || fn.parent_scope === compressor.find_parent(AST_Scope)); && (exp !== fn || fn.parent_scope === compressor.find_parent(AST_Scope));
var inline = compressor.option("inline"); var inline = compressor.option("inline");
if (!can_inject_vars(catches, inline >= 3 && safe_to_inject)) return false; var used = Object.create(catches);
if (!can_inject_args(catches, inline >= 2 && safe_to_inject)) return false; if (!can_inject_args(catches, used, inline >= 2 && safe_to_inject)) return false;
if (!can_inject_vars(catches, used, inline >= 3 && safe_to_inject)) return false;
return !in_loop || in_loop.length == 0 || !is_reachable(fn, in_loop); return !in_loop || in_loop.length == 0 || !is_reachable(fn, in_loop);
} }

View File

@@ -6197,3 +6197,43 @@ Infinity_assignment: {
} }
expect_stdout: true expect_stdout: true
} }
issue_3439_1: {
options = {
collapse_vars: true,
unused: true,
}
input: {
console.log(typeof function(a) {
function a() {}
return a;
}(42));
}
expect: {
console.log(typeof function(a) {
function a() {}
return a;
}(42));
}
expect_stdout: "function"
}
issue_3439_2: {
options = {
collapse_vars: true,
unused: true,
}
input: {
console.log(typeof function() {
var a = 42;
function a() {}
return a;
}());
}
expect: {
console.log(typeof function() {
return 42;
}());
}
expect_stdout: "number"
}

View File

@@ -3149,6 +3149,27 @@ issue_3402: {
] ]
} }
issue_3439: {
options = {
inline: true,
}
input: {
console.log(typeof function() {
return function(a) {
function a() {}
return a;
}(42);
}());
}
expect: {
console.log(typeof function(a) {
function a() {}
return a;
}(42));
}
expect_stdout: "function"
}
issue_3444: { issue_3444: {
options = { options = {
inline: true, inline: true,