Skip to content

fix(prettier): keep one pair of parentheses per stacked JSDoc cast (#291) - #308

Merged
leonidaz merged 3 commits into
mainfrom
fix/prettier-stacked-cast-291
Sep 24, 2026
Merged

leonidaz merged 3 commits into
mainfrom
fix/prettier-stacked-cast-291

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #291

Summary

A stacked JSDoc cast lost its outer parentheses, so the outer comment stopped being a cast. The same walk also dropped a cast whose parentheses start with another comment. Every row now matches Prettier's babel output:

Input Before Now
const e = /** @type {Entry} */ (/** @type {unknown} */ (node)); const e = /** @type {Entry} */ + /** @type {unknown} */ (node); unchanged
console.log(/** @type {Entry} */ (/** @type {unknown} */ (node))); console.log(/** @type {Entry} */ /** @type {unknown} */ (node)); unchanged
return /** @type {Entry} */ (/** @type {unknown} */ (node)); return ( + both comments on separate lines + (node) + ); unchanged
x = /** @type {A} */ (/** @type {B} */ (node).y).z; x = /** @type {A} */ /** @type {B} */ (node).y.z; unchanged
x = /** @type {A} */ (/* note */ node.y); x = /** @type {A} */ + /* note */ node.y; unchanged
foo /** @type {A} */ ((node)); foo(/** @type {A} */ (node)); (the argument becomes cast) foo(/** @type {A} */ node);

Changes

  • Parser: where a node's parentheses open (@tsrx/core). parseParenAndDistinguishExpression already marks metadata.parenthesized. It now also records metadata.paren_start, the offset of the outermost grouping (. Nested parens finish outermost last, so the last write wins. The parens of a call, if, while and other statements go through other parser paths, so they are not included.
  • Printer: one pair per cast. getTypeCastParens replaces hasTypeCastParens. It scans the node's own parens from paren_start to the node, skipping comments. It keeps each pair that a cast comment directly precedes, and drops the other pairs like any redundant parens. The cast comment comes from the node, or for the outermost pair from an ancestor that starts at that paren, as before. It returns the node's leading comments split into those printed ahead of the outermost pair and those printed inside each pair.
  • Comments inside the parens. The leading-comment loop in printTsrxNode moved unchanged into printLeadingComments, so comments inside a pair follow the same rules. printTypeCastParens builds the pairs from the inside out: /** A */ (/** B */ (node)). A pair hugs an object or array only when no comment sits inside it, as in Prettier's ParenthesizedExpression printer. The prettier-ignore path uses the same pairs.
  • return and throw. getOwnLineCommentAhead only checks the comments printed ahead of the node. The inner cast comment no longer counts as a second comment on its own line, so return doesn't wrap a stacked cast.
  • semi: false (merged with fix(prettier): guard statements without semicolons, keep directives and tagged-template type arguments (#263, #266, #268) #290). fix(prettier): guard statements without semicolons, keep directives and tagged-template type arguments (#263, #266, #268) #290 put a statement's leading ; right before a JSDoc cast comment that touches its (. That rule was in the loop that is now printLeadingComments. printTsrxNode decides it for the comments printed ahead of the node, and the helper prints the ; before the last one, so ;/** @type {A} */ (/** @type {B} */ (node)).start() matches Prettier. startsWithASIHazard calls getTypeCastParens.
  • Parents that print the comments themselves. if/while/switch tests and array elements pass suppressLeadingComments and print every leading comment ahead of the node. In that case only the outermost pair prints, since the inner pairs have lost their comments, and the output stays as it was on main (see [Bug]: Formatter moves comments out of if, while, and switch tests, dropping JSDoc casts and breaking @if #299, [Bug]: Formatter moves a comment before a later array element onto the previous element, dropping JSDoc casts #300).

Parser or printer

The issue suggested walking back from the node in the printer. I tried that first, but the source text alone can't tell a grouping ( from one that belongs to the parent:

  • In foo /** @type {A} */ ((node)) and if /** @type {A} */ ((node)) {}, the first ( belongs to the call or the if. Walking back through it makes the comment a cast. main already does that with one pair, and a stacked walk would do it for more shapes.
  • In /** @type {A} */ (/** @type {B} */ (node).y).z, the member expression's own paren sits before B, and node's paren sits after it. Bounding the walk by the parent's start handles this, but the call and if case would still need a list of every parent with its own (.

The parser already knows which parens are grouping parens, so recording the outermost one is a two-line change, and the printer no longer guesses. Comment attachment is unchanged.

Output changes for existing code

Verification

  • pnpm test after merging main: 118 files, 5614 tests pass (60 skipped), except one timeout in tsrx-mcp's Hono test, which passes when rerun and passed in both full runs before the merge ([Bug]: tsrx-mcp Hono runtime test times out during a full test run #310). pnpm test --project prettier-plugin: 780 pass, including 5 new tests and the updated one. All six fail with main's printer. tsrx-utils has 2 new tests for paren_start.
  • pnpm typecheck, pnpm format:check and pnpm changeset:check pass. (tsrx-tsc for the Hono test typecheck was run by path in this worktree, because its bin was not linked.)
  • Checked by hand against Prettier's babel output: casts in call arguments, template literals, arrow bodies, export default, new callees, binary operands, objects and arrays, a line comment inside the cast's parens, a JSX attribute, and prettier-ignore.

Related findings

Filed separately because they have different causes:

🤖 Generated with Claude Code


Note

Medium Risk
Touches core print paths for expressions, comments, and ASI/return layout; behavior changes for many real stacked-cast patterns but is covered by new formatter and parser tests.

Overview
Fixes JSDoc type-cast formatting so each stacked cast keeps its own parentheses, matching Prettier and preserving checker semantics (e.g. /** @type {Entry} */ (/** @type {unknown} */ (node)) no longer collapses to two adjacent comments with only the inner cast).

Parser (@tsrx/core): parenthesized expressions now record metadata.paren_start (outermost grouping ( only—not call/if syntax), so the printer can scan nested parens without mis-treating foo /** @type {A} */ ((node)) as casting the argument.

Prettier plugin: hasTypeCastParens becomes getTypeCastParens, which maps cast comments to paren pairs and splits leading comments into ahead vs inside each pair. New printLeadingComments / printTypeCastParens wrap printed nodes from the inside out; return/throw layout and semi: false leading-semicolon logic use the split comments so stacked casts are not wrapped in extra parens. prettier-ignore and own-line comment detection follow the same rules.

Reviewed by Cursor Bugbot for commit c927fbe. Bugbot is set up for automated code reviews on this repo. Configure here.

)

`/** @type {A} */ (/** @type {B} */ (node))` lost its outer parentheses. The
parser attaches both comments to `node`, and `hasTypeCastParens` walked back
from the node over `(` and whitespace only, so it stopped at the inner comment
and the formatter printed one pair after both comments. The outer comment was
then a plain comment. The same walk stopped at any comment inside a cast's
parentheses, as in `/** @type {A} */ (/* note */ node.y)`, and at an inner
cast before a member chain, as in `/** @type {A} */ (/** @type {B} */ (n).y)`.

The parser now records where a parenthesized expression's outermost grouping
paren opens (`metadata.paren_start`), so the formatter reads the node's own
parens from the source instead of guessing past call or statement parens.
`getTypeCastParens` keeps the pairs that a cast comment directly precedes,
drops the others like any redundant parens, and splits the node's leading
comments into the ones printed ahead of the outermost pair and the ones
printed inside each pair. `getOwnLineCommentAhead` only looks at the comments
printed ahead, so `return` no longer wraps a stacked cast.

A comment between a callee and its argument list (`foo /** @type {A} */ ((n))`)
no longer becomes a cast of the argument, since the call's paren is not the
argument's own.

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 8:53pm UTC
tsrx-website-mcp Ready Ready Preview Sep 24, 2026 8:53pm UTC

Request Review

leonidaz and others added 2 commits September 24, 2026 16:57
…cast-291

#290 added a rule to printTsrxNode's leading-comment loop: with `semi: false`,
a statement's leading `;` goes right before a JSDoc cast comment that touches
its `(`. That loop now lives in `printLeadingComments`, so printTsrxNode makes
the decision for the comments it prints ahead of the node, and the helper puts
the `;` before the last one. `startsWithASIHazard` calls `getTypeCastParens`
in place of the renamed `hasTypeCastParens`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With `semi: false`, a statement that starts with a stacked cast prints `;`
before the outer cast comment, as Prettier does, and keeps both pairs of
parentheses.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 68d5218 into main Sep 24, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/prettier-stacked-cast-291 branch September 24, 2026 21:11
@github-actions github-actions Bot mentioned this pull request Sep 24, 2026
leonidaz added a commit that referenced this pull request Sep 24, 2026
#308 changed hasOwnLineLeadingComment to take the comments printed ahead
of the node, so the number-array printer passed the options object as
the comments and every number array with two or more elements threw.
The separator before an element is now pushed while visiting that
element, whose path gives getTypeCastParens the comments that print
ahead of a JSDoc cast.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz added a commit that referenced this pull request Sep 24, 2026
* fix(prettier): respect trailingComma none in arrays with blank lines (#275)

In the blank-line ArrayExpression layout, the last element got its own
comma whenever trailing commas were off, and the separate trailing comma
was appended when they were on, so a comma printed either way. The last
element now never carries a comma, and the shared trailingCommaDoc adds
it (and keeps a trailing hole's comma) like every other layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(prettier): lay out arrays like Prettier's printArray

The array printer kept any array written across lines on several lines,
kept blank lines between elements even when the array fit, forced
one-property objects inline, and printed numbers one per line. It is
now a port of Prettier's printArray: an array breaks only when it
doesn't fit or is a matrix of objects or arrays with several entries,
a blank line survives only in a broken array, and number-only arrays
pack with fill. Objects in arrays print like any other object.

Supporting changes from Prettier:
- call arguments don't expand a deps array after a lone arrow or a
  number-only array after other arguments, and hugged arguments that
  break add a breakParent (breaks don't propagate out of conditionalGroup)
- array declarator inits use the fluid layout

A block comment on its own line before an element in a number array
keeps its line break, which Prettier drops and then reparses as a
trailing comment on the previous element.

Also reformats the tracked .tsrx files that had short multiline arrays.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(prettier): cover comments before array elements (#295, #300)

The array printer rewrite dropped the code that moved an element's
leading comments after the previous comma, so the comment now stays
with its element and JSDoc casts survive a second pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(prettier): pass the comments ahead of a number-array element (#308)

#308 changed hasOwnLineLeadingComment to take the comments printed ahead
of the node, so the number-array printer passed the options object as
the comments and every number array with two or more elements threw.
The separator before an element is now pushed while visiting that
element, whose path gives getTypeCastParens the comments that print
ahead of a JSDoc cast.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
leonidaz added a commit that referenced this pull request Sep 24, 2026
#308 took the same leading-comment loop out of printTsrxNode as
printLeadingComments(node, comments, …), so this branch's copy is dropped
and printLabeledStatement calls main's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Formatter drops the outer parentheses of a stacked JSDoc cast

1 participant