From c7152b5e6618089c359d8f6c944572997271cfc9 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L." Date: Wed, 31 Jul 2024 17:56:39 +0300 Subject: [PATCH] enhance `comparisons` & `inline` (#5897) --- lib/compress.js | 20 +++++++++--- test/compress/comparisons.js | 60 ++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 728523652c..a84014c344 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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; @@ -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; @@ -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; diff --git a/test/compress/comparisons.js b/test/compress/comparisons.js index 2b43cc4fba..9faf34437c 100644 --- a/test/compress/comparisons.js +++ b/test/compress/comparisons.js @@ -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; + } +}