Skip to content

Commit

Permalink
Fix nullish coalescing negation inference (#1045)
Browse files Browse the repository at this point in the history
This PR does 2 things:

- It removes special casing for nullish coalescing (??) in negation
  - This fixes #1003
- We also made sure optional chaining (?.) is ok
  - This cements #1007 as fixed
  • Loading branch information
Robin Ricard committed Aug 22, 2021
1 parent 9101e63 commit 622654c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 0 additions & 3 deletions lib/compress/inference.js
Expand Up @@ -818,9 +818,6 @@ export function is_lhs(node, parent) {
self.left = self.left.negate(compressor, first_in_statement);
self.right = self.right.negate(compressor);
return best(this, self, first_in_statement);
case "??":
self.right = self.right.negate(compressor);
return best(this, self, first_in_statement);
}
return basic_negation(this);
});
Expand Down
16 changes: 16 additions & 0 deletions test/compress/issue-1003.js
@@ -0,0 +1,16 @@
nullish_coalescing_boolean_expr: {
options = {
booleans: true,
ecma: true,
}
input: {
(function(a, b) {
if(!(a ?? b)) throw new Error("Some error");
})()
}
expect: {
(function(a, b) {
if(!(a ?? b)) throw new Error("Some error");
})()
}
}
18 changes: 18 additions & 0 deletions test/compress/issue-1007.js
@@ -0,0 +1,18 @@
optional_chaining_boolean_expr: {
options = {
booleans: true,
ecma: true,
}
input: {
(function(option) {
if (!(option.container?.tagName === "DIV"))
throw new Error("Invalid `container` and/or `viewer` option.");
})()
}
expect: {
(function(option) {
if ("DIV" !== option.container?.tagName)
throw new Error("Invalid `container` and/or `viewer` option.");
})()
}
}

0 comments on commit 622654c

Please sign in to comment.