Skip to content

Commit

Permalink
fix parens output for nullish coalescing operators. Closes #834
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Oct 18, 2020
1 parent c881c78 commit 8a42649
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/output.js
Expand Up @@ -971,6 +971,10 @@ function OutputStream(options) {
return true;
}

if (po === "??" && (so === "||" || so === "&&")) {
return true;
}

const pp = PRECEDENCE[po];
const sp = PRECEDENCE[so];
if (pp > sp
Expand Down
18 changes: 18 additions & 0 deletions test/compress/nullish.js
Expand Up @@ -127,3 +127,21 @@ nullish_coalescing_mandatory_parens: {

expect_exact: "(x??y)||z;x||(y??z);"
}

nullish_coalescing_parens: {
input: {
console.log((false || null) ?? "PASS");
console.log(null ?? (true && "PASS"));
console.log((null ?? 0) || "PASS");
console.log(null || (null ?? "PASS"));
}

node_version: ">=14"

expect_stdout: [
"PASS",
"PASS",
"PASS",
"PASS",
]
}

0 comments on commit 8a42649

Please sign in to comment.