aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-04-05 03:42:23 +0100
committerGitHub <noreply@github.com>2020-04-05 10:42:23 +0800
commit1b07f640573494d1f8625f8926868caeaeaaaa9e (patch)
treeb2f77fdafe1a05439bad270a9b561eb437db5916 /lib
parent80d9c44b22fd8d479d0cf86b1037568ea3f94cd7 (diff)
downloadtracifyjs-1b07f640573494d1f8625f8926868caeaeaaaa9e.tar.gz
tracifyjs-1b07f640573494d1f8625f8926868caeaeaaaa9e.zip
enhance `inline` (#3760)
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js27
1 files changed, 24 insertions, 3 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 72690fef..f03e0eef 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -5813,12 +5813,24 @@ merge(Compressor.prototype, {
var is_func = fn instanceof AST_Lambda;
var stat = is_func && fn.first_statement();
var can_inline = compressor.option("inline") && !self.is_expr_pure(compressor);
- if (exp === fn && can_inline && stat instanceof AST_Return) {
+ if (can_inline && stat instanceof AST_Return) {
var value = stat.value;
- if (!value || value.is_constant_expression()) {
+ if (exp === fn && (!value || value.is_constant_expression())) {
var args = self.args.concat(value || make_node(AST_Undefined, self));
return make_sequence(self, args).optimize(compressor);
}
+ var funarg, pos;
+ if (value instanceof AST_SymbolRef
+ && (funarg = resolve_funarg(value.definition().orig))
+ && (pos = fn.argnames.indexOf(funarg)) >= 0
+ && (pos >= self.args.length - 1 || all(self.args.slice(pos), function(funarg) {
+ return !funarg.has_side_effects(compressor);
+ }))) {
+ var args = self.args.slice();
+ args.push(args.splice(pos, 1)[0] || make_node(AST_Undefined, self));
+ var node = make_sequence(self, args).optimize(compressor);
+ return maintain_this_binding(compressor, compressor.parent(), compressor.self(), node);
+ }
}
if (is_func) {
var def, value, scope, in_loop, level = -1;
@@ -5837,7 +5849,8 @@ merge(Compressor.prototype, {
&& can_inject_symbols()) {
fn._squeezed = true;
if (exp !== fn) fn.parent_scope = exp.scope;
- return make_sequence(self, flatten_fn()).optimize(compressor);
+ var node = make_sequence(self, flatten_fn()).optimize(compressor);
+ return maintain_this_binding(compressor, compressor.parent(), compressor.self(), node);
}
if (compressor.option("side_effects")
&& all(fn.body, is_empty)
@@ -5864,6 +5877,14 @@ merge(Compressor.prototype, {
}
return try_evaluate(compressor, self);
+ function resolve_funarg(orig) {
+ var funarg;
+ for (var i = 0; orig[i] instanceof AST_SymbolFunarg; i++) {
+ funarg = orig[i];
+ }
+ return funarg;
+ }
+
function return_value(stat) {
if (!stat) return make_node(AST_Undefined, self);
if (stat instanceof AST_Return) {