enhance evaluate & inline (#3580)

This commit is contained in:
Alex Lam S.L
2019-11-13 04:17:09 +08:00
committed by GitHub
parent 0d17c5b0fa
commit d6fd18d0b0
7 changed files with 45 additions and 37 deletions

View File

@@ -3059,15 +3059,15 @@ merge(Compressor.prototype, {
});
def(AST_Call, function(compressor, cached, depth) {
var exp = this.expression;
if (exp instanceof AST_SymbolRef) {
var fn = exp.fixed_value();
if (!(fn instanceof AST_Lambda)) return this;
var fn = exp instanceof AST_SymbolRef ? exp.fixed_value() : exp;
if (fn instanceof AST_Lambda) {
if (fn.evaluating) return this;
if (fn.name && fn.name.definition().recursive_refs > 0) return this;
if (fn.body.length != 1 || !fn.is_constant_expression()) return this;
var stat = fn.body[0];
if (!(stat instanceof AST_Return)) return this;
var args = eval_args(this.args);
if (!args) return this;
if (!stat.value) return undefined;
fn.argnames.forEach(function(sym, i) {
var value = args[i];
sym.definition().references.forEach(function(node) {
@@ -3077,8 +3077,9 @@ merge(Compressor.prototype, {
cached.push(node);
});
});
if (!stat.value) return undefined;
fn.evaluating = true;
var val = stat.value._eval(compressor, cached, depth);
delete fn.evaluating;
if (val === stat.value) return this;
return val;
} else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {
@@ -5280,15 +5281,10 @@ merge(Compressor.prototype, {
}
var stat = is_func && fn.body[0];
var can_inline = compressor.option("inline") && !self.is_expr_pure(compressor);
if (can_inline && stat instanceof AST_Return) {
if (exp === fn && can_inline && stat instanceof AST_Return) {
var value = stat.value;
if (!value || value.is_constant_expression()) {
if (value) {
value = value.clone(true);
} else {
value = make_node(AST_Undefined, self);
}
var args = self.args.concat(value);
var args = self.args.concat(value || make_node(AST_Undefined, self));
return make_sequence(self, args).optimize(compressor);
}
}