Repository navigation
fix(core): attach comments in static blocks, namespaces, code blocks, interfaces, and enums like their siblings (#286, #293) - #302
Merged
Conversation
…cks like a function body (#286) The parser's comment attachment only treated BlockStatement, Program, and ClassBody bodies as statement lists. In a StaticBlock, TSModuleBlock, or JSXCodeBlock, a block comment on the next statement's line became a trailing comment of the previous statement, so the formatter separated a JSDoc cast from its parentheses and dropped the cast. A comment after the last statement of a static block or namespace, or in an empty one, moved out of the block. StaticBlock and TSModuleBlock bodies now attach comments as BlockStatement bodies do, including empty-block inner comments. A code block's siblings are its setup statements then its render node, so a block comment on the render node's line leads it. Comments after the last node still go to the code block's innerComments, where printJSXCodeBlock prints them. The formatter prints StaticBlock through the BlockStatement case, so a static block also prints its inner comments and keeps blank lines between statements. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…atement-lists-286 # Conflicts: # packages/prettier-plugin/src/index.js
…286) #290 guards the statement with a leading semicolon before the cast. The next pass must read the cast as the start of that statement in static blocks, namespaces, and code blocks too. Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz
added a commit
that referenced
this pull request
Sep 24, 2026
Static blocks and namespace bodies are not statement lists for comment attachment yet (#286), so after the last statement a comment left the block once its empty statement stopped taking it. Their empty statements keep their comments again, and statement lists keep an empty statement that has one, which formats them as main does until #302 lands. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ke members (#293) The parser's comment attachment treated TSTypeLiteral members as a sibling list, but not TSInterfaceBody bodies or TSEnumDeclaration members. A block comment on the next member's line became a trailing comment of the member before it, so `a: 1; /** @deprecated */ b: 2;` deprecated `a`. A comment after the last member moved after the closing `}`. Both are sibling lists now. An enum's name is kept out of its member list: with no members, it would count as the last one and take the body's comments. A comment in an empty interface, enum, or type literal moved out of the body too. The parser now keeps it as an inner comment, and printEmptyMemberList prints it between the braces. Like Prettier, an enum or type literal keeps a lone block comment on the braces' line when it fits. Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz
added a commit
that referenced
this pull request
Sep 24, 2026
#303) (#309) * fix(core): attach comments around empty statements like Prettier (#288, #303) An empty statement in a statement list prints as nothing, so a comment attached to it printed with a stray leading space and moved on the next pass. In a switch case the comment was deleted. Like Prettier's canAttachComment, the parser now skips these empty statements: the statement before takes a comment on its line, the statement after takes an own-line comment, and a list of only empty statements leaves its comments to its block or program. A `;` body such as `if (x) ;` keeps its comments. The formatter prints a program's inner comments, so a file that has only comments keeps them (#303), and statement lists drop every empty statement. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(core): keep empty-statement comments in static blocks and namespaces Static blocks and namespace bodies are not statement lists for comment attachment yet (#286), so after the last statement a comment left the block once its empty statement stopped taking it. Their empty statements keep their comments again, and statement lists keep an empty statement that has one, which formats them as main does until #302 lands. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
…atement-lists-286 # Conflicts: # packages/tsrx/src/parse/index.js
…mespaces (#286) #309 left the empty statements of static blocks and namespace bodies owning their comments, because a comment after their last statement used to leave the block. These bodies are statement lists now, so their empty statements take no comments, like a function body's. getPrintedStatementIndexes kept an empty statement that had a comment; the parser leaves none on a list entry, so it drops every empty statement. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…atement-lists-286
Merged
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 #286
Closes #293
Summary
The parser attached comments in class static blocks, namespaces,
@{ … }code blocks, interfaces, and enums differently from a function body. A block comment before the next statement or member on the same line became a trailing comment of the one before it. The formatter then separated a JSDoc cast from its parentheses and dropped the cast, or moved a JSDoc tag onto the wrong member. Comments at the end of these bodies, or in empty ones, moved outside the braces.class C { static { a; /** @type {Foo} */ (x).y(); } }a; /** @type {Foo} */+x.y();(cast dropped)a;+/** @type {Foo} */ (x).y();namespace N { a; /** @type {Foo} */ (x).y(); }@{ const a = 1; /** @type {Foo} */ (x).y(); <div /> }@{ const a = 1; /* c */ <div /> }const a = 1; /* c */+<div />const a = 1;+/* c */ <div />interface I { a: 1; /** @deprecated */ b: 2; }a: 1 /** @deprecated */;+b: 2;(deprecatesa)a: 1;+/** @deprecated */ b: 2;enum E { A, /** @deprecated */ B }A /** @deprecated */,+B,A,+/** @deprecated */ B,// tailbefore}// tailafter the closing}declare global, interface, enum, or type literal with// only{}+// onlyafter itstatic {+a;+ blank line +b;+}Root cause
get_comment_handlers(packages/tsrx/src/parse/index.js) finds a node's siblings throughnode_array. OnlyBlockStatement,Program, andClassBodybodies counted as statement lists, and onlyTSTypeLiteralmembers counted as a member list. Children ofStaticBlock,TSModuleBlock,JSXCodeBlock,TSInterfaceBody, andTSEnumDeclarationhad no siblings. So three rules never ran for them:/** @type {SomeType} */ (a) = 5;)The empty-body rule, which keeps comments as
innerComments, only checkedBlockStatement.Changes
Static blocks and namespaces ([Bug]: Comments in static blocks, namespaces, and code blocks attach to the previous statement and drop JSDoc casts #286).
StaticBlockandTSModuleBlockbodies are sibling lists likeBlockStatementbodies. That covers the same-line rule, the comments after the last statement, the blank-line grouping, and the empty-blockinnerComments.Code blocks ([Bug]: Comments in static blocks, namespaces, and code blocks attach to the previous statement and drop JSDoc casts #286). A code block's siblings are its setup statements, then its render node, so a block comment on the render node's line becomes the render node's leading comment.
printJSXCodeBlockalready reads it from there to measure the blank line before the output. The last node is not treated as last in the list, so comments after it still go to the code block'sinnerComments, andprintJSXCodeBlockis unchanged. Control-flow bodies (@if,@for, …) areBlockStatements and already worked.Interfaces and enums ([Bug]: Comments in interfaces, enums, and type literals move to the previous member or out of the body #293).
TSInterfaceBodybodies andTSEnumDeclarationmembers are sibling lists. An enum'sidis kept out of its member list: with no members, it would count as the last member and take the body's comments.Empty member lists ([Bug]: Comments in interfaces, enums, and type literals move to the previous member or out of the body #293). The parser keeps the comments of an empty interface, enum, or type literal as
innerComments. The newprintEmptyMemberListprints them between the braces for all three. Like Prettier, an enum or type literal keeps a lone block comment on the braces' line when it fits (enum F {/* note */}). An interface, or any line comment, puts each comment on its own line.Static block printer.
StaticBlocknow prints through theBlockStatementcase with astatic {opener, together with fix(prettier): guard statements without semicolons, keep directives and tagged-template type arguments (#263, #266, #268) #290's empty-statement filtering. The old case printed neitherinnerCommentsnor blank lines, so it would have dropped the comments the parser now keeps in an empty static block.Empty statements (fix(core): attach comments around empty statements like Prettier (#288, #303) #309). fix(core): attach comments around empty statements like Prettier (#288, #303) #309 skips a
;in a statement list for comments, like Prettier'scanAttachComment, but left static blocks and namespace bodies out, since a comment after their last statement used to leave the block. They are statement lists now, so they skip empty statements too:static { a; ; // c }gives// ctoa, andstatic { ; // c }keeps it as the block's inner comment.getPrintedStatementIndexeskept an empty statement that had a comment. The parser no longer leaves one on a list entry, so it now drops every empty statement.semi: falseWith #290 merged,
semi: falseguards the cast with a leading;. All four statement containers now printa+;/** @type {Foo} */ (x).y(), and a second pass leaves that unchanged. Before this PR, the next pass attached the comment toain a static block, namespace, or code block, and dropped the cast. There is a formatter test for this, and the parser tests cover the;-first form in each container.Output changes for existing code
Formatting the repository's 460 JS/TS/TSRX files with the plugin from
main(1b7ec4b4) and from this branch gives identical output.Verification
pnpm test: 5665 tests pass (60 skipped) after merging fix(core): attach comments around empty statements like Prettier (#288, #303) #309. After merging fix(prettier): lay out arrays like Prettier (#275, #295, #300) #292, which only changed the formatter,pnpm test --project prettier-pluginpasses (821 tests).packages/tsrx/tests/utils/parser.test.js, 14 of which fail onmain. The rest are the function-body and type-literal baselines. There are also 9 formatter tests inpackages/prettier-plugin/src/index.test.js, all of which fail onmain.pnpm typecheck,pnpm format:check, andpnpm changeset:checkpass.declare module,declare global, dotted and nested namespaces,const/declareenums, interfaces withextends, empty bodies with block, line, and several comments, and type literals in parameters and annotations.Related findings
class C {+// c+}printsclass C {}+// c.class A { a = 1; b = 2; }), and a blank line between members prints as a double space, which changes on the next pass.;.node.id.namedirectly.The repository check also found 25 files that
mainformats in a way that changes on a second pass. The same 25 files did this on this branch before the later merges. 8 were stacked casts (#291, since fixed by #308), and the rest are filed as #294–#298. #292 has since fixed #295.🤖 Generated with Claude Code
Note
Medium Risk
Changes parser comment attachment and formatter output across many constructs; behavior is well-tested but affects formatting semantics (JSDoc/deprecation placement) on reformat.
Overview
Aligns comment attachment and printing in class static blocks, namespaces,
@{ … }code blocks, interfaces, enums, and type literals with how function bodies already behave—fixing dropped JSDoc casts, mis-placed@deprecatedtags, and comments that used to jump outside closing}.Parser (
@tsrx/core): TreatsStaticBlock,TSModuleBlock,JSXCodeBlock(setup + render siblings),TSInterfaceBody, andTSEnumDeclarationmembers as real sibling lists so same-line block comments lead the next statement/member, tail comments stay on the last item, and blank-line groups are preserved. Empty bodies now collect comments asinnerComments; empty statements in static/namespace lists no longer own comments (neighbors or the container get them).Formatter (
@tsrx/prettier-plugin): RoutesStaticBlockthrough the shared block printer (static {, blank lines between statements, empty-blockinnerComments). AddsprintEmptyMemberListfor empty interfaces, enums, and type literals. Stops printing empty statements solely because they had comments.Reviewed by Cursor Bugbot for commit 52140f6. Bugbot is set up for automated code reviews on this repo. Configure here.