From f80d5b8c9ec0de7b3e7f99981da9f2d39ece66c4 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 30 Apr 2020 21:33:46 +0100 Subject: enhance `inline` (#3832) --- lib/compress.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/compress.js b/lib/compress.js index c679a8b2..15db2332 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -225,7 +225,7 @@ merge(Compressor.prototype, { // output and performance. descend(node, this); var opt = node.optimize(this); - if (is_scope) { + if (is_scope && opt === node) { opt.drop_unused(this); descend(opt, this); } @@ -3981,6 +3981,38 @@ merge(Compressor.prototype, { return self; }); + OPT(AST_Function, function(self, compressor) { + self.body = tighten_body(self.body, compressor); + if (compressor.option("inline")) for (var i = 0; i < self.body.length; i++) { + var stat = self.body[i]; + if (stat instanceof AST_Directive) continue; + if (stat instanceof AST_Return) { + var call = stat.value; + if (!call || call.TYPE != "Call") break; + var fn = call.expression; + if (fn instanceof AST_SymbolRef) { + fn = fn.fixed_value(); + } + if (!(fn instanceof AST_Lambda)) break; + if (fn.uses_arguments) break; + if (fn.contains_this()) break; + var j = fn.argnames.length; + if (j > 0 && compressor.option("inline") < 2) break; + if (j > self.argnames.length) break; + if (j < self.argnames.length && !compressor.drop_fargs(fn, call)) break; + while (--j >= 0) { + var arg = call.args[j]; + if (!(arg instanceof AST_SymbolRef)) break; + if (arg.definition() !== self.argnames[j].definition()) break; + } + if (j >= 0) break; + return call.expression; + } + break; + } + return self; + }); + AST_Scope.DEFMETHOD("drop_unused", function(compressor) { if (!compressor.option("unused")) return; if (compressor.has_directive("use asm")) return; @@ -6133,7 +6165,7 @@ merge(Compressor.prototype, { function can_substitute_directly() { if (var_assigned) return; - if (compressor.option("inline") <= 1 && fn.argnames.length) return; + if (compressor.option("inline") < 2 && fn.argnames.length) return; if (!fn.variables.all(function(def) { return def.references.length < 2 && def.orig[0] instanceof AST_SymbolFunarg; })) return; -- cgit v1.2.3