Skip to content

Commit

Permalink
enhance booleans (#5898)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jul 31, 2024
1 parent c7152b5 commit cd0a8ec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
47 changes: 47 additions & 0 deletions test/compress/booleans.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit cd0a8ec

Please sign in to comment.