fix corner case in collapse_vars (#4061)
This commit is contained in:
parent
aa83ecdb3b
commit
fee677786e
|
|
@ -1952,6 +1952,7 @@ merge(Compressor.prototype, {
|
||||||
function get_lvalues(expr) {
|
function get_lvalues(expr) {
|
||||||
var lvalues = new Dictionary();
|
var lvalues = new Dictionary();
|
||||||
if (expr instanceof AST_VarDef) lvalues.add(expr.name.name, lhs);
|
if (expr instanceof AST_VarDef) lvalues.add(expr.name.name, lhs);
|
||||||
|
var find_arguments = scope.uses_arguments && !compressor.has_directive("use strict");
|
||||||
var scan_toplevel = scope instanceof AST_Toplevel;
|
var scan_toplevel = scope instanceof AST_Toplevel;
|
||||||
var tw = new TreeWalker(function(node) {
|
var tw = new TreeWalker(function(node) {
|
||||||
var value;
|
var value;
|
||||||
|
|
@ -1960,21 +1961,27 @@ merge(Compressor.prototype, {
|
||||||
} else if (node instanceof AST_This) {
|
} else if (node instanceof AST_This) {
|
||||||
value = node;
|
value = node;
|
||||||
}
|
}
|
||||||
if (value) {
|
if (value) lvalues.add(node.name, is_modified(compressor, tw, node, value, 0));
|
||||||
lvalues.add(node.name, is_modified(compressor, tw, node, value, 0));
|
if (find_arguments && node instanceof AST_Sub) {
|
||||||
} else if (scan_toplevel) {
|
scope.argnames.forEach(function(argname) {
|
||||||
if (node.TYPE == "Call") {
|
if (!compressor.option("reduce_vars") || argname.definition().assignments) {
|
||||||
if (modify_toplevel) return;
|
lvalues.add(argname.name, true);
|
||||||
var exp = node.expression;
|
|
||||||
if (exp instanceof AST_PropAccess) return;
|
|
||||||
if (exp instanceof AST_Function && !exp.contains_this()) return;
|
|
||||||
modify_toplevel = true;
|
|
||||||
} else if (node instanceof AST_PropAccess && may_be_global(node.expression)) {
|
|
||||||
if (node === lhs && !(expr instanceof AST_Unary)) {
|
|
||||||
modify_toplevel = true;
|
|
||||||
} else {
|
|
||||||
read_toplevel = true;
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
find_arguments = false;
|
||||||
|
}
|
||||||
|
if (!scan_toplevel) return;
|
||||||
|
if (node.TYPE == "Call") {
|
||||||
|
if (modify_toplevel) return;
|
||||||
|
var exp = node.expression;
|
||||||
|
if (exp instanceof AST_PropAccess) return;
|
||||||
|
if (exp instanceof AST_Function && !exp.contains_this()) return;
|
||||||
|
modify_toplevel = true;
|
||||||
|
} else if (node instanceof AST_PropAccess && may_be_global(node.expression)) {
|
||||||
|
if (node === lhs && !(expr instanceof AST_Unary)) {
|
||||||
|
modify_toplevel = true;
|
||||||
|
} else {
|
||||||
|
read_toplevel = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1599,7 +1599,7 @@ collapse_vars_constants: {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
collapse_vars_arguments: {
|
collapse_vars_arguments_1: {
|
||||||
options = {
|
options = {
|
||||||
booleans: true,
|
booleans: true,
|
||||||
collapse_vars: true,
|
collapse_vars: true,
|
||||||
|
|
@ -1636,6 +1636,78 @@ collapse_vars_arguments: {
|
||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
collapse_vars_arguments_2: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function log(a, b) {
|
||||||
|
console.log(b);
|
||||||
|
}
|
||||||
|
function f(c) {
|
||||||
|
var d = arguments[0];
|
||||||
|
c = "FAIL";
|
||||||
|
log(c, d);
|
||||||
|
}
|
||||||
|
f();
|
||||||
|
f("PASS");
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function log(a, b) {
|
||||||
|
console.log(b);
|
||||||
|
}
|
||||||
|
function f(c) {
|
||||||
|
var d = arguments[0];
|
||||||
|
log(c = "FAIL", d);
|
||||||
|
}
|
||||||
|
f();
|
||||||
|
f("PASS");
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"undefined",
|
||||||
|
"PASS",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
collapse_vars_arguments_3: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function log(a, b) {
|
||||||
|
console.log(b);
|
||||||
|
}
|
||||||
|
function f(c) {
|
||||||
|
var args = arguments;
|
||||||
|
console.log(c);
|
||||||
|
var d = args[0];
|
||||||
|
c = "FAIL";
|
||||||
|
log(c, d);
|
||||||
|
}
|
||||||
|
f();
|
||||||
|
f("PASS");
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function log(a, b) {
|
||||||
|
console.log(b);
|
||||||
|
}
|
||||||
|
function f(c) {
|
||||||
|
var args = arguments;
|
||||||
|
console.log(c);
|
||||||
|
var d = args[0];
|
||||||
|
log(c = "FAIL", d);
|
||||||
|
}
|
||||||
|
f();
|
||||||
|
f("PASS");
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"undefined",
|
||||||
|
"undefined",
|
||||||
|
"PASS",
|
||||||
|
"PASS",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
collapse_vars_short_circuit: {
|
collapse_vars_short_circuit: {
|
||||||
options = {
|
options = {
|
||||||
booleans: true,
|
booleans: true,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user