diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-04-30 21:33:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-01 04:33:46 +0800 |
commit | f80d5b8c9ec0de7b3e7f99981da9f2d39ece66c4 (patch) | |
tree | 0ce35a27e44708540b3b320e525cd8a7f8dfecae /lib | |
parent | d90000697320fa6ffeea80b77dca9d6cd32d2127 (diff) | |
download | tracifyjs-f80d5b8c9ec0de7b3e7f99981da9f2d39ece66c4.tar.gz tracifyjs-f80d5b8c9ec0de7b3e7f99981da9f2d39ece66c4.zip |
enhance `inline` (#3832)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 36 |
1 files changed, 34 insertions, 2 deletions
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; |