enhance evaluate & reduce_vars (#5060)

This commit is contained in:
Alex Lam S.L
2021-07-07 17:29:23 +01:00
committed by GitHub
parent 2340feff87
commit 6577d641ac
4 changed files with 79 additions and 48 deletions

View File

@@ -734,7 +734,12 @@ merge(Compressor.prototype, {
var save = fixed;
if (save) fixed = function() {
var value = save();
return is_undefined(value) ? make_sequence(node, [ value, node.value ]) : node;
var ev;
if (is_undefined(value, compressor)
|| (ev = fuzzy_eval(compressor, value, true)) === undefined) {
return make_sequence(node, [ value, node.value ]);
}
return ev instanceof AST_Node ? node : value;
};
node.name.walk(scanner);
fixed = save;
@@ -7293,7 +7298,7 @@ merge(Compressor.prototype, {
node.in_bool = true;
var value = node.value;
if (value) {
var ev = value.is_truthy() || value.evaluate(compressor, true);
var ev = fuzzy_eval(compressor, value);
if (!ev) {
value = value.drop_side_effect_free(compressor);
node.value = value ? make_sequence(node.value, [
@@ -8006,7 +8011,7 @@ merge(Compressor.prototype, {
OPT(AST_Do, function(self, compressor) {
if (!compressor.option("loops")) return self;
var cond = self.condition.is_truthy() || self.condition.evaluate(compressor, true);
var cond = fuzzy_eval(compressor, self.condition);
if (!(cond instanceof AST_Node)) {
if (cond && !has_loop_control(self, compressor.parent(), AST_Continue)) return make_node(AST_For, self, {
body: make_node(AST_BlockStatement, self.body, {
@@ -8174,36 +8179,25 @@ merge(Compressor.prototype, {
if (self.step) self.step = self.step.drop_side_effect_free(compressor);
}
if (self.condition) {
var cond = self.condition.evaluate(compressor);
if (cond instanceof AST_Node) {
cond = self.condition.is_truthy() || self.condition.evaluate(compressor, true);
} else if (cond) {
self.condition = null;
}
var cond = fuzzy_eval(compressor, self.condition);
if (!cond) {
if (compressor.option("dead_code")) {
var body = [];
if (is_statement(self.init)) {
body.push(self.init);
} else if (self.init) {
body.push(make_node(AST_SimpleStatement, self.init, {
body: self.init
}));
body.push(make_node(AST_SimpleStatement, self.init, { body: self.init }));
}
body.push(make_node(AST_SimpleStatement, self.condition, {
body: self.condition
}));
body.push(make_node(AST_SimpleStatement, self.condition, { body: self.condition }));
extract_declarations_from_unreachable_code(compressor, self.body, body);
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
}
} else if (self.condition && !(cond instanceof AST_Node)) {
} else if (!(cond instanceof AST_Node)) {
self.body = make_node(AST_BlockStatement, self.body, {
body: [
make_node(AST_SimpleStatement, self.condition, {
body: self.condition
}),
self.body
]
make_node(AST_SimpleStatement, self.condition, { body: self.condition }),
self.body,
],
});
self.condition = null;
}
@@ -8894,7 +8888,7 @@ merge(Compressor.prototype, {
if (!compressor.option("optional_chains")) return;
if (!self.optional) return;
var expr = self.expression;
var ev = expr.evaluate(compressor, true);
var ev = fuzzy_eval(compressor, expr, true);
if (ev == null) return make_node(AST_UnaryPrefix, self, {
operator: "void",
expression: expr,
@@ -10492,7 +10486,7 @@ merge(Compressor.prototype, {
}
// (x || false) && y ---> x ? y : false
if (self.left.operator == "||") {
var lr = self.left.right.evaluate(compressor, true);
var lr = fuzzy_eval(compressor, self.left.right);
if (!lr) return make_node(AST_Conditional, self, {
condition: self.left.left,
consequent: self.right,
@@ -10545,7 +10539,7 @@ merge(Compressor.prototype, {
}
// x && true || y ---> x ? true : y
if (!nullish && self.left.operator == "&&") {
var lr = self.left.right.is_truthy() || self.left.right.evaluate(compressor, true);
var lr = fuzzy_eval(compressor, self.left.right);
if (lr && !(lr instanceof AST_Node)) return make_node(AST_Conditional, self, {
condition: self.left.left,
consequent: self.left.right,
@@ -11070,21 +11064,16 @@ merge(Compressor.prototype, {
}
var local = self.fixed !== def.fixed;
if (fixed && (local || def.should_replace !== false)) {
var init;
var ev, init;
if (fixed instanceof AST_This) {
if (!is_funarg(def) && same_scope(def)) {
init = fixed;
}
} else {
var ev = fixed.evaluate(compressor, true);
if (ev !== fixed
&& typeof ev != "function"
&& (typeof ev != "object"
|| ev instanceof RegExp
&& compressor.option("unsafe_regexp")
&& !def.cross_loop && same_scope(def))) {
init = make_node_from_constant(ev, fixed);
}
if (!is_funarg(def) && same_scope(def)) init = fixed;
} else if ((ev = fixed.evaluate(compressor, true)) !== fixed
&& typeof ev != "function"
&& (ev === null
|| typeof ev != "object"
|| compressor.option("unsafe_regexp")
&& ev instanceof RegExp && !def.cross_loop && same_scope(def))) {
init = make_node_from_constant(ev, fixed);
}
if (init) {
if (!local && def.should_replace === undefined) {