enhance inline (#5243)

This commit is contained in:
Alex Lam S.L
2021-12-27 16:53:12 +00:00
committed by GitHub
parent 835d130ccf
commit d51caaf358
3 changed files with 374 additions and 6 deletions

View File

@@ -12778,14 +12778,19 @@ Compressor.prototype.compress = function(node) {
(function(def) {
def(AST_Node, noop);
def(AST_Await, function(compressor, scope) {
return this.expression.try_inline(compressor, scope);
});
def(AST_Call, function(compressor, scope) {
if (compressor.option("inline") < 4) return;
var call = this;
if (call.is_expr_pure(compressor)) return;
var fn = call.expression;
if (!(fn instanceof AST_Function)) return;
if (!(fn instanceof AST_LambdaExpression)) return;
if (fn.name) return;
if (fn.uses_arguments) return;
if (is_arrow(fn) && fn.value) return;
if (is_generator(fn)) return;
if (fn.contains_this()) return;
if (!scope) scope = find_scope(compressor);
var defined = new Dictionary();
@@ -12795,6 +12800,7 @@ Compressor.prototype.compress = function(node) {
});
scope = scope.parent_scope;
}
if (is_async(fn) && !(compressor.option("awaits") && is_async(scope))) return;
var names = scope.var_names();
if (!fn.variables.all(function(def, name) {
if (!defined.has(name) && !names.has(name)) return true;
@@ -12864,9 +12870,16 @@ Compressor.prototype.compress = function(node) {
syms.each(function(orig, id) {
[].unshift.apply(defs[id].orig, orig);
});
return make_node(AST_BlockStatement, call, {
var inlined = make_node(AST_BlockStatement, call, {
body: body.concat(fn.body, make_node(AST_Return, call, { value: null })),
});
if (is_async(fn)) scan_local_returns(inlined, function(node) {
var value = node.value;
if (!value) return;
if (is_undefined(value)) return;
node.value = make_node(AST_Await, call, { expression: value });
});
return inlined;
function process(sym, argname) {
var def = argname.definition();
@@ -12901,11 +12914,31 @@ Compressor.prototype.compress = function(node) {
}
node.value = make_node(AST_UnaryPrefix, self, {
operator: op,
expression: value || make_node(AST_Undefined, node),
}).optimize(compressor);
expression: value || make_node(AST_Undefined, node).transform(compressor),
});
});
return inlined;
})
});
def(AST_Yield, function(compressor, scope) {
if (!compressor.option("yields")) return;
if (!this.nested) return;
var call = this.expression;
if (call.TYPE != "Call") return;
var fn = call.expression;
switch (fn.CTOR) {
case AST_AsyncGeneratorFunction:
fn = make_node(AST_AsyncFunction, fn, fn);
break;
case AST_GeneratorFunction:
fn = make_node(AST_Function, fn, fn);
break;
default:
return;
}
call = call.clone();
call.expression = fn;
return call.try_inline(compressor, scope);
});
})(function(node, func) {
node.DEFMETHOD("try_inline", func);
});