simplify scan

This commit is contained in:
alexlamsl 2017-05-04 12:41:34 +08:00
parent eb570c4661
commit 1d24488655

View File

@ -673,6 +673,7 @@ merge(Compressor.prototype, {
var lhs = get_lhs(candidate);
if (!lhs || is_lhs_read_only(lhs)) continue;
var lvalues = get_lvalues(candidate);
if (lhs instanceof AST_SymbolRef) lvalues[lhs.name] = false;
var side_effects = value_has_side_effects(candidate);
var hit = false, abort = false, replaced = false;
var tt = new TreeTransformer(function(node, descend) {
@ -697,10 +698,10 @@ merge(Compressor.prototype, {
return node;
}
// Replace variable with assignment when found
if (lhs.equivalent_to(node)) {
abort = true;
if (is_lhs(node, parent)) return node;
CHANGED = replaced = true;
if (!(node instanceof AST_SymbolDeclaration)
&& !is_lhs(node, parent)
&& lhs.equivalent_to(node)) {
CHANGED = replaced = abort = true;
compressor.info("Collapsing {name} [{file}:{line},{col}]", {
name: node.print_to_string(),
file: node.start.file,
@ -723,24 +724,6 @@ merge(Compressor.prototype, {
}
return candidate;
}
if (lhs instanceof AST_SymbolRef) {
// `a = x; a = a + y;` => `a = x + y;`
if (node instanceof AST_Assign
&& node.operator == "="
&& lhs.equivalent_to(node.left)) {
node.right = node.right.transform(tt);
abort = true;
return node;
}
// `var a = x, a = a + y;` => `var a = x + y;`
if (node instanceof AST_VarDef
&& node.value
&& node.name.definition() === lhs.definition()) {
node.value = node.value.transform(tt);
abort = true;
return node;
}
}
// These node types have child nodes that execute sequentially,
// but are otherwise not safe to scan into or beyond them.
var sym;
@ -748,9 +731,9 @@ merge(Compressor.prototype, {
|| node instanceof AST_Exit
|| node instanceof AST_PropAccess
|| node instanceof AST_SymbolRef
&& (lvalues && lvalues[node.name]
&& (lvalues[node.name]
|| side_effects && !references_in_scope(node.definition()))
|| lvalues && (sym = lhs_or_def(node)) && get_symbol(sym).name in lvalues
|| (sym = lhs_or_def(node)) && get_symbol(sym).name in lvalues
|| parent instanceof AST_Binary
&& (parent.operator == "&&" || parent.operator == "||")
|| parent instanceof AST_Case
@ -807,8 +790,9 @@ merge(Compressor.prototype, {
}
function get_lvalues(expr) {
if (expr instanceof AST_Unary) return;
var lvalues, scope;
var lvalues = Object.create(null);
if (expr instanceof AST_Unary) return lvalues;
var scope;
var tw = new TreeWalker(function(node, descend) {
if (node instanceof AST_Scope) {
var save_scope = scope;
@ -819,7 +803,6 @@ merge(Compressor.prototype, {
if (node instanceof AST_SymbolRef || node instanceof AST_PropAccess) {
var sym = get_symbol(node);
if (sym instanceof AST_SymbolRef) {
if (!lvalues) lvalues = Object.create(null);
lvalues[sym.name] = lvalues[sym.name] || is_lhs(node, tw.parent());
}
}