Skip to content

Commit 28a7ce5

Browse files
committed
fix: keep parens when a full ternary is the condition of ?:
`(1 ? 2 : 3) ?: 4` was flattened to `1 ? 2 : 3 ?: 4`, a fatal compile error in PHP 8. Pure-Elvis chains still flatten safely. Closes #2115
1 parent 269374b commit 28a7ce5

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

‎src/needs-parens.mjs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,12 @@ function needsParens(path, options) {
192192
case "bin":
193193
case "retif":
194194
if (key === "test" && !parent.trueExpr) {
195-
return false;
195+
// A shorthand ternary flattens safely (`($a ?: $b) ?: $c` ->
196+
// `$a ?: $b ?: $c`), but a full ternary used as the condition
197+
// of `?:` must keep its parentheses: `a ? b : c ?: d` is a
198+
// fatal error in PHP 8.
199+
// https://github.com/prettier/plugin-php/issues/2115
200+
return node.trueExpr !== null;
196201
}
197202

198203
return true;

0 commit comments

Comments
 (0)