fix corner case in collapse_vars (#3597)

fixes #3596
This commit is contained in:
Alex Lam S.L
2019-11-19 02:30:52 +08:00
committed by GitHub
parent 02cc4a0d03
commit c289ba1139
3 changed files with 57 additions and 25 deletions

View File

@@ -1331,7 +1331,7 @@ merge(Compressor.prototype, {
return side_effects || lhs instanceof AST_PropAccess || may_modify(lhs);
}
if (node instanceof AST_Function) {
return compressor.option("ie8") && node.name && node.name.name in lvalues;
return compressor.option("ie8") && node.name && lvalues.has(node.name.name);
}
if (node instanceof AST_PropAccess) {
return side_effects || node.expression.may_throw_on_access(compressor);
@@ -1345,10 +1345,10 @@ merge(Compressor.prototype, {
if (node instanceof AST_This) return symbol_in_lvalues(node, parent);
if (node instanceof AST_VarDef) {
if (!node.value) return false;
return node.name.name in lvalues || side_effects && may_modify(node.name);
return lvalues.has(node.name.name) || side_effects && may_modify(node.name);
}
var sym = is_lhs(node.left, node);
if (sym && sym.name in lvalues) return true;
if (sym && lvalues.has(sym.name)) return true;
if (sym instanceof AST_PropAccess) return true;
}
@@ -1514,7 +1514,16 @@ merge(Compressor.prototype, {
if (parent instanceof AST_Exit) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_If) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_IterationStatement) return node;
if (parent instanceof AST_PropAccess) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_PropAccess) {
var exp = parent.expression;
if (exp === node) return find_stop_unused(parent, level + 1);
var sym = root_expr(exp);
if (!(sym instanceof AST_SymbolRef)) return find_stop_unused(parent, level + 1);
var lvalue = lvalues.get(sym.name);
return !lvalue || all(lvalue, function(lhs) {
return !(lhs instanceof AST_PropAccess);
}) ? find_stop_unused(parent, level + 1) : node;
}
if (parent instanceof AST_Sequence) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_SimpleStatement) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Switch) return find_stop_unused(parent, level + 1);
@@ -1612,10 +1621,8 @@ merge(Compressor.prototype, {
}
function get_lvalues(expr) {
var lvalues = Object.create(null);
if (candidate instanceof AST_VarDef) {
lvalues[candidate.name.name] = lhs;
}
var lvalues = new Dictionary();
if (candidate instanceof AST_VarDef) lvalues.add(candidate.name.name, lhs);
var scan_iife = scope instanceof AST_Toplevel;
var tw = new TreeWalker(function(node) {
if (scan_iife && node.TYPE == "Call") {
@@ -1632,9 +1639,7 @@ merge(Compressor.prototype, {
} else if (node instanceof AST_This) {
value = node;
}
if (value && !lvalues[node.name]) {
lvalues[node.name] = is_modified(compressor, tw, node, value, 0);
}
if (value) lvalues.add(node.name, is_modified(compressor, tw, node, value, 0));
});
expr.walk(tw);
return lvalues;
@@ -1679,7 +1684,7 @@ merge(Compressor.prototype, {
return sym instanceof AST_SymbolRef
&& sym.definition().scope === scope
&& !(in_loop
&& (sym.name in lvalues && lvalues[sym.name] !== lhs
&& (lvalues.has(sym.name) && lvalues.get(sym.name)[0] !== lhs
|| candidate instanceof AST_Unary
|| candidate instanceof AST_Assign && candidate.operator != "="));
}
@@ -1707,9 +1712,11 @@ merge(Compressor.prototype, {
}
function symbol_in_lvalues(sym, parent) {
var lvalue = lvalues[sym.name];
if (!lvalue) return;
if (lvalue !== lhs) return true;
var lvalue = lvalues.get(sym.name);
if (!lvalue || all(lvalue, function(lhs) {
return !lhs;
})) return;
if (lvalue[0] !== lhs) return true;
scan_rhs = false;
}