enhance reduce_vars (#5038)

This commit is contained in:
Alex Lam S.L
2021-06-28 20:51:44 +01:00
committed by GitHub
parent b23b333d9d
commit 798121c9f3
2 changed files with 40 additions and 44 deletions

View File

@@ -477,14 +477,6 @@ merge(Compressor.prototype, {
&& !compressor.exposed(def) && !compressor.exposed(def)
&& !(def.init instanceof AST_LambdaExpression && def.init !== def.scope) && !(def.init instanceof AST_LambdaExpression && def.init !== def.scope)
&& def.init; && def.init;
if (def.fixed instanceof AST_LambdaDefinition && !all(def.references, function(ref) {
var scope = ref.scope.resolve();
do {
if (def.scope === scope) return true;
} while (scope instanceof AST_LambdaExpression && (scope = scope.parent_scope.resolve()));
})) {
tw.defun_ids[def.id] = false;
}
def.reassigned = 0; def.reassigned = 0;
def.recursive_refs = 0; def.recursive_refs = 0;
def.references = []; def.references = [];
@@ -526,23 +518,25 @@ merge(Compressor.prototype, {
var marker = tw.defun_ids[def.id]; var marker = tw.defun_ids[def.id];
if (!marker) return; if (!marker) return;
var visited = tw.defun_visited[def.id]; var visited = tw.defun_visited[def.id];
if (marker === tw.safe_ids) { if (marker === tw.safe_ids) return !visited && def.fixed;
if (!visited) return def.fixed; if (visited) {
} else if (visited) {
def.init.enclosed.forEach(function(d) { def.init.enclosed.forEach(function(d) {
if (def.init.variables.get(d.name) === d) return; if (def.init.variables.get(d.name) === d) return;
if (!safe_to_read(tw, d)) d.fixed = false; if (!safe_to_read(tw, d)) d.fixed = false;
}); });
} else { return;
tw.defun_ids[def.id] = false;
} }
} else { } else if (!tw.in_loop) {
if (!tw.in_loop) { var scope = def.scope;
tw.defun_ids[def.id] = tw.safe_ids; var s = tw.find_parent(AST_Scope);
return def.fixed; do {
} if (s === scope) {
tw.defun_ids[def.id] = false; tw.defun_ids[def.id] = tw.safe_ids;
return def.fixed;
}
} while (s instanceof AST_LambdaExpression && (s = s.parent_scope.resolve()));
} }
tw.defun_ids[def.id] = false;
} }
function walk_defuns(tw, scope) { function walk_defuns(tw, scope) {
@@ -967,11 +961,11 @@ merge(Compressor.prototype, {
}); });
}); });
def(AST_Call, function(tw, descend) { def(AST_Call, function(tw, descend) {
tw.find_parent(AST_Scope).may_call_this(); var node = this;
var exp = this.expression; var exp = node.expression;
if (exp instanceof AST_LambdaExpression) { if (exp instanceof AST_LambdaExpression) {
var iife = is_iife_single(this); var iife = is_iife_single(node);
this.args.forEach(function(arg) { node.args.forEach(function(arg) {
arg.walk(tw); arg.walk(tw);
if (arg instanceof AST_Spread) iife = false; if (arg instanceof AST_Spread) iife = false;
}); });
@@ -980,28 +974,28 @@ merge(Compressor.prototype, {
if (iife) delete exp.reduce_vars; if (iife) delete exp.reduce_vars;
return true; return true;
} }
if (exp instanceof AST_SymbolRef) { var def = exp instanceof AST_SymbolRef && exp.definition();
var def = exp.definition(); if (node.TYPE == "Call" && tw.in_boolean_context()) {
if (this.TYPE == "Call" && tw.in_boolean_context()) def.bool_fn++; if (def) {
if (def.fixed instanceof AST_LambdaDefinition) { def.bool_fn++;
var defun = mark_defun(tw, def); } else if (exp instanceof AST_Assign && exp.operator == "=" && exp.left instanceof AST_SymbolRef) {
if (defun) { exp.left.definition().bool_fn++;
descend();
defun.walk(tw);
return true;
}
} }
} else if (this.TYPE == "Call"
&& exp instanceof AST_Assign
&& exp.operator == "="
&& exp.left instanceof AST_SymbolRef
&& tw.in_boolean_context()) {
exp.left.definition().bool_fn++;
} }
if (!this.optional) return; if (def && def.fixed instanceof AST_LambdaDefinition) {
var defun = mark_defun(tw, def);
if (defun) {
descend();
defun.walk(tw);
return true;
}
} else {
tw.find_parent(AST_Scope).may_call_this();
}
if (!node.optional) return;
exp.walk(tw); exp.walk(tw);
push(tw); push(tw);
this.args.forEach(function(arg) { node.args.forEach(function(arg) {
arg.walk(tw); arg.walk(tw);
}); });
pop(tw); pop(tw);
@@ -10911,7 +10905,10 @@ merge(Compressor.prototype, {
single_use = false; single_use = false;
} }
if (single_use) fixed.parent_scope = self.scope; if (single_use) fixed.parent_scope = self.scope;
} else if (!fixed || !fixed.is_constant_expression() || fixed.drop_side_effect_free(compressor)) { } else if (!fixed
|| def.recursive_refs > 0
|| !fixed.is_constant_expression()
|| fixed.drop_side_effect_free(compressor)) {
single_use = false; single_use = false;
} }
} }

View File

@@ -2735,8 +2735,7 @@ issue_4135: {
0; 0;
a++; a++;
if (!a) if (!a)
c = (a++, c = 0, void (c && c.p)); var c = void a++;
var c;
console.log(a, -1, c); console.log(a, -1, c);
} }
expect_stdout: "1 -1 undefined" expect_stdout: "1 -1 undefined"