Skip to content

fix(prettier): decide expression parentheses like Prettier (#242, #244, #245, #247) - #258

Merged
leonidaz merged 4 commits into
mainfrom
fix/prettier-logical-callee-245
Sep 24, 2026
Merged

leonidaz merged 4 commits into
mainfrom
fix/prettier-logical-callee-245

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #242
Closes #244
Closes #245
Closes #247

Summary

The formatter now decides expression parentheses in one place, following Prettier's needs-parens rules. 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:

Input Before Now
(primary || fallback)() (#245) primary || fallback() unchanged
(await load())() (#247) await load()() unchanged
(yield 2) + 1 (#247) yield 2 + 1 unchanged
(a?.b)(), new (a?.b)(), (a?.b)!.c (#244) a?.b(), new a?.b(), a?.b!.c unchanged
a || (() => 1), - -a a || () => 1, --a a || (() => 1), -(-a)

It also kept redundant parentheses (#242): const x = (a);, foo((a));, and (foo)() now format like Prettier, as a, foo(a), and foo().

Changes

  • needsParens(path) replaces binaryExpressionNeedsParens, castOperandNeedsParens, assignmentExpressionNeedsParens, and the parenthesis handling in the identifier, call, member, conditional, unary, non-null, spread, expression-statement, and arrow-body printers. printTsrxNode wraps the node after its leading comments.
  • hasTypeCastParens keeps /** @type {T} */ (value) casts, like Prettier's babel parser. 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.
  • superClassNeedsParens adds Prettier's readability parentheses for new, object literal, and tagged-template superclasses (the second half of [Bug]: Formatter keeps redundant parentheses and omits Prettier's superclass parentheses #242).
  • Sequences drop their parentheses in a for head: for (i = 0, j = 0; ; i++, j++). This also fixes doubled parentheses: () => ((a, b)) and ((a, b)).c.
  • Parentheses TypeScript needs but Prettier drops are kept. An object literal at the start of a superclass stays wrapped, as in 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>().
  • A prettier-ignored operand keeps its parentheses. Its span excludes them, so (a || b)() used to lose them.
  • Class heritage, export default, and return/throw with 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)}, and f((a = 1)). The changeset calls this out. Across the repo's 68 .tsrx files, only the five vite-plugin-*/tests/runtime.test.tsrx fixtures format differently: {...props.enabled && { disabled: true }} becomes {...(props.enabled && { disabled: true })}. This PR reformats them so pnpm format:check passes.

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 nested ternary in a branch: a ? b : (c ? d : e) stays, and Prettier drops the alternate's parentheses.
  • A conditional arrow body: () => (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 prints a || b<T>), class A extends ({}).Base {} (Prettier prints extends {}.Base, which TypeScript rejects), and (a<T>)! (Prettier prints a<T>!).

Verification

Related findings

Found while testing, filed separately because they have different causes:

🤖 Generated with Claude Code


Note

Medium Risk
Formatter output changes widely across .tsrx files 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-plugin so formatting matches Prettier’s needs-parens rules instead of each node printer guessing from metadata.parenthesized.

A new needsParens / nodeNeedsParens layer (with updated precedence including ??, shouldFlatten, JSDoc hasTypeCastParens, optional-chain boundaries, TypeScript-only cases like extends ({}).Base) wraps nodes once in printTsrxNode and 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.

#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]>
@vercel

vercel Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tsrx-website Ready Ready Preview Sep 24, 2026 2:35pm UTC
tsrx-website-mcp Ready Ready Preview Sep 24, 2026 2:35pm UTC

Request Review

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]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

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.

Comment thread packages/prettier-plugin/src/index.js Resolved
Comment thread packages/prettier-plugin/src/index.js Resolved
…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]>
@leonidaz
leonidaz merged commit e1c068d into main Sep 24, 2026
11 checks passed
@leonidaz
leonidaz deleted the fix/prettier-logical-callee-245 branch September 24, 2026 14:41
@github-actions github-actions Bot mentioned this pull request Sep 24, 2026

This branch was successfully deployed

2 active deployments
Preview – tsrx-website — 29d00c5b Deployed Sep 24, 2026 by vercel[bot]
Preview – tsrx-website-mcp — 29d00c5b Deployed Sep 24, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant