enhance collapse_vars (#3602)
This commit is contained in:
@@ -1151,7 +1151,7 @@ merge(Compressor.prototype, {
|
|||||||
return node;
|
return node;
|
||||||
} else {
|
} else {
|
||||||
replaced++;
|
replaced++;
|
||||||
if (value_def && candidate instanceof AST_VarDef) return node;
|
if (value_def) return node;
|
||||||
}
|
}
|
||||||
CHANGED = abort = true;
|
CHANGED = abort = true;
|
||||||
AST_Node.info("Collapsing {name} [{file}:{line},{col}]", {
|
AST_Node.info("Collapsing {name} [{file}:{line},{col}]", {
|
||||||
@@ -1207,7 +1207,7 @@ merge(Compressor.prototype, {
|
|||||||
if (is_lhs(node, multi_replacer.parent())) return node;
|
if (is_lhs(node, multi_replacer.parent())) return node;
|
||||||
def.replaced++;
|
def.replaced++;
|
||||||
value_def.replaced--;
|
value_def.replaced--;
|
||||||
return candidate.value.clone();
|
return get_rvalue(candidate).clone();
|
||||||
}
|
}
|
||||||
// Skip (non-executed) functions and (leading) default case in switch statements
|
// Skip (non-executed) functions and (leading) default case in switch statements
|
||||||
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
|
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
|
||||||
@@ -1255,10 +1255,17 @@ merge(Compressor.prototype, {
|
|||||||
statements[i].transform(scanner);
|
statements[i].transform(scanner);
|
||||||
}
|
}
|
||||||
if (value_def) {
|
if (value_def) {
|
||||||
var def = candidate.name.definition();
|
var def = lhs.definition();
|
||||||
if (abort && def.references.length - def.replaced > replaced) replaced = false;
|
if (abort) {
|
||||||
else {
|
var referenced = def.references.length - def.replaced;
|
||||||
abort = false;
|
if (candidate instanceof AST_Assign) referenced--;
|
||||||
|
if (referenced > replaced) {
|
||||||
|
replaced = false;
|
||||||
|
} else {
|
||||||
|
abort = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!abort) {
|
||||||
hit_index = 0;
|
hit_index = 0;
|
||||||
hit = funarg;
|
hit = funarg;
|
||||||
for (var i = stat_index; !abort && i < statements.length; i++) {
|
for (var i = stat_index; !abort && i < statements.length; i++) {
|
||||||
@@ -1334,7 +1341,7 @@ merge(Compressor.prototype, {
|
|||||||
return compressor.option("ie8") && node.name && lvalues.has(node.name.name);
|
return compressor.option("ie8") && node.name && lvalues.has(node.name.name);
|
||||||
}
|
}
|
||||||
if (node instanceof AST_PropAccess) {
|
if (node instanceof AST_PropAccess) {
|
||||||
return side_effects || node.expression.may_throw_on_access(compressor);
|
return side_effects || !value_def && node.expression.may_throw_on_access(compressor);
|
||||||
}
|
}
|
||||||
if (node instanceof AST_SymbolRef) {
|
if (node instanceof AST_SymbolRef) {
|
||||||
if (symbol_in_lvalues(node, parent)) {
|
if (symbol_in_lvalues(node, parent)) {
|
||||||
|
|||||||
@@ -3852,10 +3852,9 @@ issue_2436_9: {
|
|||||||
expect: {
|
expect: {
|
||||||
var o = console;
|
var o = console;
|
||||||
console.log({
|
console.log({
|
||||||
x: (c = o).a,
|
x: o.a,
|
||||||
y: c.b,
|
y: o.b,
|
||||||
});
|
});
|
||||||
var c;
|
|
||||||
}
|
}
|
||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
}
|
}
|
||||||
@@ -6567,3 +6566,29 @@ issue_3596: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "42"
|
expect_stdout: "42"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local_value_replacement: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f(a, b) {
|
||||||
|
(a = b) && g(a);
|
||||||
|
}
|
||||||
|
function g(c) {
|
||||||
|
console.log(c);
|
||||||
|
}
|
||||||
|
f("FAIL", "PASS");
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f(a, b) {
|
||||||
|
(a = b) && g(a);
|
||||||
|
}
|
||||||
|
function g(c) {
|
||||||
|
console.log(c);
|
||||||
|
}
|
||||||
|
f("FAIL", "PASS");
|
||||||
|
}
|
||||||
|
expect_stdout: "PASS"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user