fix: keep parens around nested ternary in ?: - #2519
Open
Dextheking1 wants to merge 1 commit into
Open
Dextheking1 wants to merge 1 commit into
Dextheking1 wants to merge 1 commit into
Conversation
Formatting `(1 ? 2 : 3) ?: 4` stripped the parentheses, producing `1 ? 2 : 3 ?: 4`, which is a syntax error in PHP 8 (unparenthesized nested ternary). needs-parens now keeps parens for a full ternary in the test position of a shorthand ternary (`?:`). Shorthand chains still format unparenthesized since they associate to the left. Closes prettier#2115
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Formatting
(1 ? 2 : 3) ?: 4stripped the parentheses, producing1 ? 2 : 3 ?: 4, which is a syntax error in PHP 8 (unparenthesized nested ternary).src/needs-parens.mjsreturnedfalsefor anyretifnode in the test position of a shorthand ternary (?:). It now returnstruewhen the node is itself a full ternary (!!node.trueExpr), keeping the parens. Shorthand chains ($a ?: $b ?: $c) still format without parens since they associate to the left.Added regression cases to
tests/retif/retif.php; snapshot diff is purely additive. Full suite: 101 suites / 595 tests pass, plusAST_COMPARE=1on the retif suite confirms the formatted output re-parses to an equivalent AST.Closes #2115