From e23bf480522e64db43f8764fef64312750c58e77 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 10 May 2020 20:08:05 +0100 Subject: enhance `evaluate` & `reduce_vars` (#3873) --- lib/compress.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/compress.js b/lib/compress.js index 6a444e92..fcbbffb3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -492,8 +492,7 @@ merge(Compressor.prototype, { function ref_once(tw, compressor, def) { return compressor.option("unused") && !def.scope.pinned() - && def.references.length - def.recursive_refs == 1 - && tw.loop_ids[def.id] === tw.in_loop; + && def.references.length - def.recursive_refs == 1; } function is_immutable(value) { @@ -797,8 +796,9 @@ merge(Compressor.prototype, { if (recursive) { d.recursive_refs++; } else if (value && ref_once(tw, compressor, d)) { + d.in_loop = tw.loop_ids[d.id] !== tw.in_loop; d.single_use = value instanceof AST_Lambda && !value.pinned() - || d.scope === this.scope && value.is_constant_expression(); + || !d.in_loop && d.scope === this.scope && value.is_constant_expression(); } else { d.single_use = false; } @@ -3501,7 +3501,21 @@ merge(Compressor.prototype, { if (fn.name && fn.name.definition().recursive_refs > 0) return this; if (this.is_expr_pure(compressor)) return this; var stat = fn.first_statement(); - if (!(stat instanceof AST_Return)) return this; + if (!(stat instanceof AST_Return)) { + if (ignore_side_effects) { + var found = false; + fn.walk(new TreeWalker(function(node) { + if (found) return true; + if (node instanceof AST_Return) { + if (node.value && node.value.evaluate(compressor, true) !== undefined) found = true; + return true; + } + if (node instanceof AST_Scope && node !== fn) return true; + })); + if (!found) return void 0; + } + return this; + } var args = eval_args(this.args); if (!args) return this; if (!stat.value) return undefined; @@ -7427,7 +7441,7 @@ merge(Compressor.prototype, { var single_use = def.single_use && !(parent instanceof AST_Call && parent.is_expr_pure(compressor)); if (single_use) { if (fixed instanceof AST_Lambda) { - if (def.scope !== self.scope + if ((def.scope !== self.scope || def.in_loop) && (!compressor.option("reduce_funcs") || def.escaped.depth == 1 || fixed.inlined)) { single_use = false; } else if (recursive_ref(compressor, def)) { @@ -7771,7 +7785,7 @@ merge(Compressor.prototype, { expressions.push(self); return make_sequence(self, expressions); } - var condition = self.condition.is_truthy() || self.condition.evaluate(compressor); + var condition = self.condition.is_truthy() || self.condition.evaluate(compressor, true); if (!condition) { AST_Node.warn("Condition always false [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.condition, self.alternative ]).optimize(compressor); -- cgit v1.2.3