From cd0a8ec757156737a71be697b0cdd4a95a2dd6ab Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L." Date: Wed, 31 Jul 2024 20:07:51 +0300 Subject: [PATCH] enhance `booleans` (#5898) --- lib/compress.js | 2 +- test/compress/booleans.js | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index a84014c3447..ce8e23283e4 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -12114,7 +12114,7 @@ Compressor.prototype.compress = function(node) { var parent = compressor.parent(); if (compressor.option("booleans")) { var lhs = extract_lhs(self.left, compressor); - if (lazy_op[self.operator] && !lhs.has_side_effects(compressor)) { + if (lazy_op[self.operator] && repeatable(compressor, lhs)) { // a || a ---> a // (a = x) && a --> a = x if (lhs.equals(self.right)) { diff --git a/test/compress/booleans.js b/test/compress/booleans.js index ae69fd0f150..f005b635a25 100644 --- a/test/compress/booleans.js +++ b/test/compress/booleans.js @@ -99,6 +99,53 @@ de_morgan_1d: { expect_stdout: "false false" } +de_morgan_1e: { + options = { + booleans: true, + } + input: { + function f(a) { + return a.p || a.p; + } + console.log(f({ p: null }), f({ p: 42 })); + } + expect: { + function f(a) { + return a.p; + } + console.log(f({ p: null }), f({ p: 42 })); + } + expect_stdout: "null 42" +} + +de_morgan_1f: { + options = { + booleans: true, + inline: true, + pure_getters: "strict", + reduce_vars: true, + toplevel: true, + } + input: { + function f(a, b) { + return a.p + b.q; + } + console.log(f({ p: null }, { q: false }) || f({ p: null }, { q: false })); + console.log(f({ p: "foo" }, { q: 42 }) && f({ p: "foo" }, { q: 42 })); + } + expect: { + function f(a, b) { + return a.p + b.q; + } + console.log(f({ p: null }, { q: !1 })); + console.log(f({ p: "foo" }, { q: 42 })); + } + expect_stdout: [ + "0", + "foo42", + ] +} + de_morgan_2a: { options = { booleans: true,