From 633dd64701242c7734c287b3df95dd0504b1fbfe Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Fri, 19 Jul 2024 14:11:45 +0300 Subject: [PATCH] enhance `comparisons` --- lib/compress.js | 7 +- test/compress/comparisons.js | 246 ++++++++++++++++------------------- 2 files changed, 114 insertions(+), 139 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index b85bd8bb021..3195084a86d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -12079,7 +12079,7 @@ Compressor.prototype.compress = function(node) { case "&&": case "||": // void 0 !== x && null !== x ---> null != x - // void 0 === x || null === x ---> null == x + // void 0 === x.a || null === x.a ---> null == x.a var left = self.left; if (!(left instanceof AST_Binary)) break; if (left.operator != (self.operator == "&&" ? "!==" : "===")) break; @@ -12087,9 +12087,8 @@ Compressor.prototype.compress = function(node) { 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 expr = left.right; - if (expr instanceof AST_Assign && expr.operator == "=") expr = expr.left; - if (expr.has_side_effects(compressor)) break; + var expr = extract_lhs(left.right, compressor); + if (!repeatable(compressor, expr)) break; if (!expr.equals(self.right.right)) break; left.operator = left.operator.slice(0, -1); left.left = make_node(AST_Null, self); diff --git a/test/compress/comparisons.js b/test/compress/comparisons.js index bc860ce9a0a..2b43cc4fba4 100644 --- a/test/compress/comparisons.js +++ b/test/compress/comparisons.js @@ -167,48 +167,24 @@ issue_2857_1: { comparisons: true, } input: { - function f1(a) { - a === undefined || a === null; - a === undefined || a !== null; - a !== undefined || a === null; - a !== undefined || a !== null; - a === undefined && a === null; - a === undefined && a !== null; - a !== undefined && a === null; - a !== undefined && a !== null; - } - function f2(a) { - a === null || a === undefined; - a === null || a !== undefined; - a !== null || a === undefined; - a !== null || a !== undefined; - a === null && a === undefined; - a === null && a !== undefined; - a !== null && a === undefined; - a !== null && a !== undefined; - } + a === undefined || a === null; + a === undefined || a !== null; + a !== undefined || a === null; + a !== undefined || a !== null; + a === undefined && a === null; + a === undefined && a !== null; + a !== undefined && a === null; + a !== undefined && a !== null; } expect: { - function f1(a) { - null == a; - void 0 === a || null !== a; - void 0 !== a || null === a; - void 0 !== a || null !== a; - void 0 === a && null === a; - void 0 === a && null !== a; - void 0 !== a && null === a; - null != a; - } - function f2(a) { - null == a; - null === a || void 0 !== a; - null !== a || void 0 === a; - null !== a || void 0 !== a; - null === a && void 0 === a; - null === a && void 0 !== a; - null !== a && void 0 === a; - null != a; - } + null == a; + void 0 === a || null !== a; + void 0 !== a || null === a; + void 0 !== a || null !== a; + void 0 === a && null === a; + void 0 === a && null !== a; + void 0 !== a && null === a; + null != a; } } @@ -217,28 +193,24 @@ issue_2857_2: { comparisons: true, } input: { - function f(a, p) { - a === undefined || a === null || p; - a === undefined || a !== null || p; - a !== undefined || a === null || p; - a !== undefined || a !== null || p; - a === undefined && a === null || p; - a === undefined && a !== null || p; - a !== undefined && a === null || p; - a !== undefined && a !== null || p; - } + a === null || a === undefined; + a === null || a !== undefined; + a !== null || a === undefined; + a !== null || a !== undefined; + a === null && a === undefined; + a === null && a !== undefined; + a !== null && a === undefined; + a !== null && a !== undefined; } expect: { - function f(a, p) { - null == a || p; - void 0 === a || null !== a || p; - void 0 !== a || null === a || p; - void 0 !== a || null !== a || p; - void 0 === a && null === a || p; - void 0 === a && null !== a || p; - void 0 !== a && null === a || p; - null != a || p; - } + null == a; + null === a || void 0 !== a; + null !== a || void 0 === a; + null !== a || void 0 !== a; + null === a && void 0 === a; + null === a && void 0 !== a; + null !== a && void 0 === a; + null != a; } } @@ -247,59 +219,50 @@ issue_2857_3: { comparisons: true, } input: { - function f(a, p) { - a === undefined || a === null && p; - a === undefined || a !== null && p; - a !== undefined || a === null && p; - a !== undefined || a !== null && p; - a === undefined && a === null && p; - a === undefined && a !== null && p; - a !== undefined && a === null && p; - a !== undefined && a !== null && p; - } + a === undefined || a === null || p; + a === undefined || a !== null || p; + a !== undefined || a === null || p; + a !== undefined || a !== null || p; + a === undefined && a === null || p; + a === undefined && a !== null || p; + a !== undefined && a === null || p; + a !== undefined && a !== null || p; } expect: { - function f(a, p) { - void 0 === a || null === a && p; - void 0 === a || null !== a && p; - void 0 !== a || null === a && p; - void 0 !== a || null !== a && p; - void 0 === a && null === a && p; - void 0 === a && null !== a && p; - void 0 !== a && null === a && p; - null != a && p; - } + null == a || p; + void 0 === a || null !== a || p; + void 0 !== a || null === a || p; + void 0 !== a || null !== a || p; + void 0 === a && null === a || p; + void 0 === a && null !== a || p; + void 0 !== a && null === a || p; + null != a || p; } } issue_2857_4: { options = { comparisons: true, - conditionals: true, } input: { - function f(a, p) { - p || a === undefined || a === null; - p || a === undefined || a !== null; - p || a !== undefined || a === null; - p || a !== undefined || a !== null; - p || a === undefined && a === null; - p || a === undefined && a !== null; - p || a !== undefined && a === null; - p || a !== undefined && a !== null; - } + a === undefined || a === null && p; + a === undefined || a !== null && p; + a !== undefined || a === null && p; + a !== undefined || a !== null && p; + a === undefined && a === null && p; + a === undefined && a !== null && p; + a !== undefined && a === null && p; + a !== undefined && a !== null && p; } expect: { - function f(a, p) { - p || null == a; - p || void 0 === a || null !== a; - p || void 0 !== a || null === a; - p || void 0 !== a || null !== a; - p || void 0 === a && null === a; - p || void 0 === a && null !== a; - p || void 0 !== a && null === a; - p || null != a; - } + void 0 === a || null === a && p; + void 0 === a || null !== a && p; + void 0 !== a || null === a && p; + void 0 !== a || null !== a && p; + void 0 === a && null === a && p; + void 0 === a && null !== a && p; + void 0 !== a && null === a && p; + null != a && p; } } @@ -309,32 +272,55 @@ issue_2857_5: { conditionals: true, } input: { - function f(a, p) { - p && a === undefined || a === null; - p && a === undefined || a !== null; - p && a !== undefined || a === null; - p && a !== undefined || a !== null; - p && a === undefined && a === null; - p && a === undefined && a !== null; - p && a !== undefined && a === null; - p && a !== undefined && a !== null; - } + p || a === undefined || a === null; + p || a === undefined || a !== null; + p || a !== undefined || a === null; + p || a !== undefined || a !== null; + p || a === undefined && a === null; + p || a === undefined && a !== null; + p || a !== undefined && a === null; + p || a !== undefined && a !== null; } expect: { - function f(a, p) { - p && void 0 === a || null === a; - p && void 0 === a || null !== a; - p && void 0 !== a || null === a; - p && void 0 !== a || null !== a; - p && void 0 === a && null === a; - p && void 0 === a && null !== a; - p && void 0 !== a && null === a; - p && null != a; - } + p || null == a; + p || void 0 === a || null !== a; + p || void 0 !== a || null === a; + p || void 0 !== a || null !== a; + p || void 0 === a && null === a; + p || void 0 === a && null !== a; + p || void 0 !== a && null === a; + p || null != a; } } issue_2857_6: { + options = { + comparisons: true, + conditionals: true, + } + input: { + p && a === undefined || a === null; + p && a === undefined || a !== null; + p && a !== undefined || a === null; + p && a !== undefined || a !== null; + p && a === undefined && a === null; + p && a === undefined && a !== null; + p && a !== undefined && a === null; + p && a !== undefined && a !== null; + } + expect: { + p && void 0 === a || null === a; + p && void 0 === a || null !== a; + p && void 0 !== a || null === a; + p && void 0 !== a || null !== a; + p && void 0 === a && null === a; + p && void 0 === a && null !== a; + p && void 0 !== a && null === a; + p && null != a; + } +} + +issue_2857_7: { options = { comparisons: true, pure_getters: "strict", @@ -342,27 +328,17 @@ issue_2857_6: { } input: { function f(a) { - if (({}).b === undefined || {}.b === null) + if ({}.b === undefined || {}.b === null) return a.b !== undefined && a.b !== null; } - console.log(f({ - a: [ null ], - get b() { - return this.a.shift(); - } - })); + console.log(f({ b: [] })); } expect: { function f(a) { if (null == {}.b) - return void 0 !== a.b && null !== a.b; + return null != a.b; } - console.log(f({ - a: [ null ], - get b() { - return this.a.shift(); - } - })); + console.log(f({ b: [] })); } expect_stdout: "true" }