enhance collapse_vars (#5312)

This commit is contained in:
Alex Lam S.L
2022-01-25 20:18:58 +00:00
committed by GitHub
parent 6de708af37
commit 5c863b74d7
4 changed files with 88 additions and 23 deletions

View File

@@ -2058,6 +2058,11 @@ Compressor.prototype.compress = function(node) {
assign.right = rvalue;
return assign;
}
// Stop signals related to AST_SymbolRef
if (should_stop_ref(node, parent)) {
abort = true;
return node;
}
// These node types have child nodes that execute sequentially,
// but are otherwise not safe to scan into or beyond them.
if (is_last_node(node, parent) || may_throw(node)) {
@@ -2184,7 +2189,8 @@ Compressor.prototype.compress = function(node) {
var stop_if_hit = null;
var lhs = get_lhs(candidate);
var side_effects = lhs && lhs.has_side_effects(compressor);
var scan_lhs = lhs && !side_effects && !is_lhs_read_only(lhs, compressor);
var scan_lhs = lhs && (!side_effects || lhs instanceof AST_SymbolRef)
&& !is_lhs_read_only(lhs, compressor);
var scan_rhs = foldable(candidate);
if (!scan_lhs && !scan_rhs) continue;
var compound = candidate instanceof AST_Assign && candidate.operator.slice(0, -1);
@@ -2318,25 +2324,27 @@ Compressor.prototype.compress = function(node) {
if (node instanceof AST_DestructuredKeyVal) return node.key instanceof AST_Node;
if (node instanceof AST_DWLoop) return true;
if (node instanceof AST_LoopControl) return true;
if (node instanceof AST_SymbolRef) {
if (node.is_declared(compressor)) {
if (node.fixed_value()) return false;
if (can_drop_symbol(node)) {
return !(parent instanceof AST_PropAccess && parent.expression === node)
&& is_arguments(node.definition());
}
} else if (is_direct_assignment(node, parent)) {
return false;
}
if (!replace_all) return true;
scan_rhs = false;
return false;
}
if (node instanceof AST_Try) return true;
if (node instanceof AST_With) return true;
return false;
}
function should_stop_ref(node, parent) {
if (!(node instanceof AST_SymbolRef)) return false;
if (node.is_declared(compressor)) {
if (node.fixed_value()) return false;
if (can_drop_symbol(node)) {
return !(parent instanceof AST_PropAccess && parent.expression === node)
&& is_arguments(node.definition());
}
} else if (is_direct_assignment(node, parent)) {
return false;
}
if (!replace_all) return true;
scan_rhs = false;
return false;
}
function in_conditional(node, parent) {
if (parent instanceof AST_Assign) return parent.left !== node && lazy_op[parent.operator.slice(0, -1)];
if (parent instanceof AST_Binary) return parent.left !== node && lazy_op[parent.operator];