Repository navigation
fix(prettier): decide expression parentheses like Prettier (#242, #244, #245, #247) - #258
Merged
Merged
Conversation
#245, #247) Parentheses were decided per printer: some nodes kept any parentheses the source had, some parents re-added them for a few child types, and the rest dropped them. Required parentheses were lost wherever no printer happened to keep them: `(a || b)()` printed as `a || b()`, `(await f())()` as `await f()()`, `(yield 2) + 1` as `yield 2 + 1`, and `(a?.b)()` as `a?.b()`. Redundant ones were kept around identifiers, calls and members. A single `needsParens(path)` now follows Prettier's needs-parens rules and wraps the node where it is printed, after its leading comments. `hasTypeCastParens` keeps the parentheses a JSDoc `@type`/`@satisfies` cast needs, laid out like Prettier's. Class heritage, `export default`, and `return`/`throw` with an own-line comment still own their parentheses. Nested ternary branches and conditional arrow bodies keep the parentheses they were written with, because the plugin's layouts for them differ from Prettier's. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
The website typecheck runs the plugin source with noFallthroughCasesInSwitch, so the shared cases in nodeNeedsParens are merged instead of falling through. The runtime fixtures pick up Prettier's parentheses around a logical JSX spread argument. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.
Bugbot Autofix is ON. A cloud agent has been kicked off to fix the reported issues. You can view the agent here.
Reviewed by Cursor Bugbot for commit 0a8d69b. Configure here.
…nstantiations
TypeScript reads the `{` of `class A extends {}.Base {}` as the class body,
so an object literal at the start of a superclass keeps its parentheses
(Prettier drops them). An instantiation expression followed by `!`, a
second type argument list, or a call or tag with type arguments keeps them
too: `a<T>!` and `a<T><U>` do not parse.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merged
1 task done
This was referenced Sep 24, 2026
Merged
This was referenced Sep 30, 2026
This branch was successfully deployed
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.

Closes #242
Closes #244
Closes #245
Closes #247
Summary
The formatter now decides expression parentheses in one place, following Prettier's
needs-parensrules. The four issues share one cause. Each printer decided parentheses for itself: some nodes kept any parentheses the source had, some parents re-added them for a few child types, and everything else dropped them. That lost required parentheses and changed what code does:(primary || fallback)()(#245)primary || fallback()(await load())()(#247)await load()()(yield 2) + 1(#247)yield 2 + 1(a?.b)(),new (a?.b)(),(a?.b)!.c(#244)a?.b(),new a?.b(),a?.b!.ca || (() => 1),- -aa || () => 1,--aa || (() => 1),-(-a)It also kept redundant parentheses (#242):
const x = (a);,foo((a));, and(foo)()now format like Prettier, asa,foo(a), andfoo().Changes
needsParens(path)replacesbinaryExpressionNeedsParens,castOperandNeedsParens,assignmentExpressionNeedsParens, and the parenthesis handling in the identifier, call, member, conditional, unary, non-null, spread, expression-statement, and arrow-body printers.printTsrxNodewraps the node after its leading comments.hasTypeCastParenskeeps/** @type {T} */ (value)casts, like Prettier'sbabelparser. The cast comment can hang on the node itself or on an ancestor that starts at the paren (/** @type {T} */ (node).start). The parentheses break and indent like Prettier's, which keeps the existing JSDoc layout tests passing.superClassNeedsParensadds Prettier's readability parentheses fornew, object literal, and tagged-template superclasses (the second half of [Bug]: Formatter keeps redundant parentheses and omits Prettier's superclass parentheses #242).forhead:for (i = 0, j = 0; ; i++, j++). This also fixes doubled parentheses:() => ((a, b))and((a, b)).c.class A extends ({}).Base {}, because TypeScript reads the{as the class body. An instantiation expression before!or a second type-argument list stays wrapped too, as in(a<T>)!and(a<T>)<U>().prettier-ignored operand keeps its parentheses. Its span excludes them, so(a || b)()used to lose them.export default, andreturn/throwwith an own-line comment still own their parentheses (suppressOwnParens).Output changes for existing code
Besides dropping redundant parentheses, the formatter now adds the parentheses Prettier adds for readability. Examples:
(x + y) as string,(a * b) / c,(a & b) | c,(a ?? b) ? c : d,[...(a ?? [])],{...(a && b)}, andf((a = 1)). The changeset calls this out. Across the repo's 68.tsrxfiles, only the fivevite-plugin-*/tests/runtime.test.tsrxfixtures format differently:{...props.enabled && { disabled: true }}becomes{...(props.enabled && { disabled: true })}. This PR reformats them sopnpm format:checkpasses.Two places intentionally keep authored parentheses instead of matching Prettier. The plugin's layouts for these differ from Prettier's, so matching one without the other would look worse:
a ? b : (c ? d : e)stays, and Prettier drops the alternate's parentheses.() => (a ? b : c)stays as written, and Prettier adds the parentheses whenever the body fits.Where Prettier's own output parses differently or not at all, the plugin keeps the parentheses:
(a || b)<T>(Prettier printsa || b<T>),class A extends ({}).Base {}(Prettier printsextends {}.Base, which TypeScript rejects), and(a<T>)!(Prettier printsa<T>!).Verification
pnpm test: 118 files, 5382 tests pass.pnpm test --project prettier-plugin: 694 pass, including 101 new cases.(foo)()→foo(),x + y as string→(x + y) as string, and(… ?? null) ? a : b. Each now matches Prettier'stypescriptoutput.pnpm typecheck,pnpm format:checkandpnpm changeset:checkpass. The website typecheck compiles the plugin withnoFallthroughCasesInSwitch, sonodeNeedsParensmerges its shared cases instead of falling through.typescriptparser gives the same tree as the input, and TypeScript's own parser reports no new syntax errors. Prettier's parser tolerates some of those errors, so this second check is separate. The remaining differences from Prettier's output are the choices listed above. The new testpreserves execution when formatting parenthesized operandsruns a 170-case subset of this check.42before and after, and its superclass case compiles. [Bug]: Formatter drops await and yield parentheses and changes execution #247'sawaitandyieldcases return the same values. [Bug]: Formatter drops parentheses around optional chains that are called, used with new, or tagged #244's six cases keep their parentheses and compile. [Bug]: Formatter keeps redundant parentheses and omits Prettier's superclass parentheses #242's ten rows match Prettier byte for byte.Related findings
Found while testing, filed separately because they have different causes:
ifbody (;) is dropped, so the next statement becomes the body. This one changes behavior.=.sql<Row>`…`→sql`…`).🤖 Generated with Claude Code
Note
Medium Risk
Formatter output changes widely across
.tsrxfiles and can alter parse meaning if rules are wrong; logic is complex but heavily tested against Prettier and TypeScript parsing.Overview
Centralizes expression parentheses in
@tsrx/prettier-pluginso formatting matches Prettier’sneeds-parensrules instead of each node printer guessing frommetadata.parenthesized.A new
needsParens/nodeNeedsParenslayer (with updated precedence including??,shouldFlatten, JSDochasTypeCastParens, optional-chain boundaries, TypeScript-only cases likeextends ({}).Base) wraps nodes once inprintTsrxNodeand removes ad-hoc wrapping from calls, members, casts, arrows, spreads, and similar printers. Redundant parens are dropped; required and readability parens are kept or added (e.g.(x + y) as string,{...(a && b)}).Tests: large new suite in
index.test.js(Prettier parity, execution preservation); five vite runtime fixtures updated for conditional JSX spreads. Patch changeset documents user-visible output changes.Reviewed by Cursor Bugbot for commit 29d00c5. Bugbot is set up for automated code reviews on this repo. Configure here.