Skip to content

Commit

Permalink
enhance comparisons & inline (#5897)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Jul 31, 2024
1 parent 4bf9a4f commit c7152b5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -11065,6 +11065,7 @@ Compressor.prototype.compress = function(node) {
var node = make_sequence(self, exprs).optimize(compressor);
fn.inlined = save_inlined;
node = maintain_this_binding(parent, current, node);
self.inlined_node = node;
if (replacing || best_of_expression(node, self) === node) {
refs.forEach(function(ref) {
ref.scope = exp === fn ? fn.parent_scope : exp.scope;
Expand All @@ -11081,6 +11082,12 @@ Compressor.prototype.compress = function(node) {
exprs.unshift(self.expression);
return make_sequence(self, exprs).drop_side_effect_free(compressor, first_in_statement);
};
self.has_side_effects = function(compressor) {
var self = this;
var exprs = self.args.slice();
exprs.unshift(self.expression);
return make_sequence(self, exprs).has_side_effects(compressor);
};
}
}
var arg_used, insert, in_loop, scope;
Expand Down Expand Up @@ -12085,15 +12092,18 @@ Compressor.prototype.compress = function(node) {
// void 0 !== x && null !== x ---> null != x
// void 0 === x.a || null === x.a ---> null == x.a
var left = self.left;
if (left.inlined_node) left = left.inlined_node;
if (!(left instanceof AST_Binary)) break;
if (left.operator != (self.operator == "&&" ? "!==" : "===")) break;
if (!(self.right instanceof AST_Binary)) break;
if (left.operator != self.right.operator) break;
if (is_undefined(left.left, compressor) && self.right.left instanceof AST_Null
|| left.left instanceof AST_Null && is_undefined(self.right.left, compressor)) {
var right = self.right;
if (right.inlined_node) right = right.inlined_node;
if (!(right instanceof AST_Binary)) break;
if (left.operator != right.operator) break;
if (is_undefined(left.left, compressor) && right.left instanceof AST_Null
|| left.left instanceof AST_Null && is_undefined(right.left, compressor)) {
var expr = extract_lhs(left.right, compressor);
if (!repeatable(compressor, expr)) break;
if (!expr.equals(self.right.right)) break;
if (!expr.equals(right.right)) break;
left.operator = left.operator.slice(0, -1);
left.left = make_node(AST_Null, self);
return left;
Expand Down
60 changes: 60 additions & 0 deletions test/compress/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,63 @@ nullish_chain: {
A || B || null == a || C;
}
}

nullish_inline: {
options = {
comparisons: true,
inline: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function isNull(a) {
return null === a;
}
function isUndefined(b) {
return void 0 === b;
}
null === c || void 0 === c;
isNull(c) || void 0 === c;
null === c || isUndefined(c);
isNull(c) || isUndefined(c);
}
expect: {
null == c;
null == c;
null == c;
null == c;
}
}

nullish_inline_renamed: {
rename = true
mangle = {
toplevel: true,
}
options = {
comparisons: true,
inline: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function isNull(a) {
return null === a;
}
function isUndefined(b) {
return void 0 === b;
}
null === c || void 0 === c;
isNull(c) || void 0 === c;
null === c || isUndefined(c);
isNull(c) || isUndefined(c);
}
expect: {
null == c;
null == c;
null == c;
null == c;
}
}

0 comments on commit c7152b5

Please sign in to comment.