aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-11-13 04:17:09 +0800
committerGitHub <noreply@github.com>2019-11-13 04:17:09 +0800
commitd6fd18d0b000e1e4fbe01259139043f42d8fdebf (patch)
tree7dca39ef5d97e53a4e5c1d062565df3d9313373f /lib
parent0d17c5b0fa3a4305177f9561ba1f05835a9ffea3 (diff)
downloadtracifyjs-d6fd18d0b000e1e4fbe01259139043f42d8fdebf.tar.gz
tracifyjs-d6fd18d0b000e1e4fbe01259139043f42d8fdebf.zip
enhance `evaluate` & `inline` (#3580)
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js20
1 files changed, 8 insertions, 12 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 08728bb4..2bf120b3 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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);
}
}