Repository navigation
fix(prettier): keep one pair of parentheses per stacked JSDoc cast (#291) - #308
Merged
Merged
Conversation
) `/** @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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…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]>
This was referenced Sep 24, 2026
Merged
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]>
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 #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
babeloutput:const e = /** @type {Entry} */ (/** @type {unknown} */ (node));const e = /** @type {Entry} */+/** @type {unknown} */ (node);console.log(/** @type {Entry} */ (/** @type {unknown} */ (node)));console.log(/** @type {Entry} */ /** @type {unknown} */ (node));return /** @type {Entry} */ (/** @type {unknown} */ (node));return (+ both comments on separate lines +(node)+);x = /** @type {A} */ (/** @type {B} */ (node).y).z;x = /** @type {A} */ /** @type {B} */ (node).y.z;x = /** @type {A} */ (/* note */ node.y);x = /** @type {A} */+/* note */ node.y;foo /** @type {A} */ ((node));foo(/** @type {A} */ (node));(the argument becomes cast)foo(/** @type {A} */ node);Changes
@tsrx/core).parseParenAndDistinguishExpressionalready marksmetadata.parenthesized. It now also recordsmetadata.paren_start, the offset of the outermost grouping(. Nested parens finish outermost last, so the last write wins. The parens of a call,if,whileand other statements go through other parser paths, so they are not included.getTypeCastParensreplaceshasTypeCastParens. It scans the node's own parens fromparen_startto 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.printTsrxNodemoved unchanged intoprintLeadingComments, so comments inside a pair follow the same rules.printTypeCastParensbuilds 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'sParenthesizedExpressionprinter. Theprettier-ignorepath uses the same pairs.returnandthrow.getOwnLineCommentAheadonly checks the comments printed ahead of the node. The inner cast comment no longer counts as a second comment on its own line, soreturndoesn'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 nowprintLeadingComments.printTsrxNodedecides 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.startsWithASIHazardcallsgetTypeCastParens.if/while/switchtests and array elements passsuppressLeadingCommentsand 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 onmain(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:foo /** @type {A} */ ((node))andif /** @type {A} */ ((node)) {}, the first(belongs to the call or theif. Walking back through it makes the comment a cast.mainalready does that with one pair, and a stacked walk would do it for more shapes./** @type {A} */ (/** @type {B} */ (node).y).z, the member expression's own paren sits beforeB, andnode's paren sits after it. Bounding the walk by the parent's start handles this, but the call andifcase 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
packages/*/srcJS files and 69.tsrxfiles withmain(1b7ec4b4) and with this branch: outputs differ in 13 JS files, all at stacked casts or casts before member chains. Re-parsed with the TSRX parser, each of those 13 has the same tree as its source. The.tsrxoutput is identical on both.(:mainkeeps 748 of the 810 in its sources, and this branch keeps 800 of 812. All 12 still missing are inif/else iftests, which is [Bug]: Formatter moves comments out of if, while, and switch tests, dropping JSDoc casts and breaking @if #299.mainand 13 on this branch. The 13 are a subset ofmain's 19. The six that became idempotent areprettier-plugin/src/index.jsand the fivevite-plugin-*/src/index.jsfiles.unwrapcase of the fix(prettier): keep empty bodies and lay out assigned values like Prettier (#254, #255, #256) #283 test "keeps a type cast inside parentheses that return, throw, or a superclass add" expected the old stacked output. Its one-line input no longer makesreturnadd parens, so it moved to the new stacked-cast test. Theunwrapcase now has an own-line comment, which keeps it testing a stacked cast inside the parensreturnadds./* a */ /* b */ run();and/** @type {A} */ /** @type {B} */ (node).Verification
pnpm testafter mergingmain: 118 files, 5614 tests pass (60 skipped), except one timeout intsrx-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 withmain's printer.tsrx-utilshas 2 new tests forparen_start.pnpm typecheck,pnpm format:checkandpnpm changeset:checkpass. (tsrx-tscfor the Hono test typecheck was run by path in this worktree, because its bin was not linked.)babeloutput: casts in call arguments, template literals, arrow bodies,export default,newcallees, binary operands, objects and arrays, a line comment inside the cast's parens, a JSX attribute, andprettier-ignore.Related findings
Filed separately because they have different causes:
if,else if,while,do…while,switch,@ifand@switchmove their test's leading comments above the keyword. A cast in the test is dropped, and@if (/* note */ x)prints@/* note */followed byif (x)on the next line, which isn't idempotent.tsrx-mcp's first Hono test can hit the 5-second timeout during a full test run, because it pays for the coldimport()of the compiler.🤖 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 recordmetadata.paren_start(outermost grouping(only—not call/ifsyntax), so the printer can scan nested parens without mis-treatingfoo /** @type {A} */ ((node))as casting the argument.Prettier plugin:
hasTypeCastParensbecomesgetTypeCastParens, which maps cast comments to paren pairs and splits leading comments intoaheadvs inside each pair. NewprintLeadingComments/printTypeCastParenswrap printed nodes from the inside out;return/throwlayout andsemi: falseleading-semicolon logic use the split comments so stacked casts are not wrapped in extra parens.prettier-ignoreand 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.