Skip to content

fix(core): attach comments in static blocks, namespaces, code blocks, interfaces, and enums like their siblings (#286, #293) - #302

Merged
leonidaz merged 7 commits into
mainfrom
fix/comment-attach-statement-lists-286
Sep 24, 2026
Merged

leonidaz merged 7 commits into
mainfrom
fix/comment-attach-statement-lists-286

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

Input Before Now
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(); } same as above same as above
@{ const a = 1; /** @type {Foo} */ (x).y(); <div /> } same as above same as above
@{ 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; (deprecates a) a: 1; + /** @deprecated */ b: 2;
enum E { A, /** @deprecated */ B } A /** @deprecated */, + B, A, + /** @deprecated */ B,
static block, namespace, interface, or enum + // tail before } // tail after the closing } unchanged
empty static block, namespace, declare global, interface, enum, or type literal with // only {} + // only after it unchanged
static { + a; + blank line + b; + } blank line removed unchanged

Root cause

get_comment_handlers (packages/tsrx/src/parse/index.js) finds a node's siblings through node_array. Only BlockStatement, Program, and ClassBody bodies counted as statement lists, and only TSTypeLiteral members counted as a member list. Children of StaticBlock, TSModuleBlock, JSXCodeBlock, TSInterfaceBody, and TSEnumDeclaration had no siblings. So three rules never ran for them:

  • a block comment on the next node's line leads that node (/** @type {SomeType} */ (a) = 5;)
  • comments after the last node stay with it
  • blank-line comment groups stay together

The empty-body rule, which keeps comments as innerComments, only checked BlockStatement.

Changes

semi: false

With #290 merged, semi: false guards the cast with a leading ;. All four statement containers now print a + ;/** @type {Foo} */ (x).y(), and a second pass leaves that unchanged. Before this PR, the next pass attached the comment to a in 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-plugin passes (821 tests).
  • New tests: 17 parser tests in packages/tsrx/tests/utils/parser.test.js, 14 of which fail on main. The rest are the function-body and type-literal baselines. There are also 9 formatter tests in packages/prettier-plugin/src/index.test.js, all of which fail on main.
  • pnpm typecheck, pnpm format:check, and pnpm changeset:check pass.
  • Checked by hand against Prettier:
    • 12 comment layouts give the same output in a function body, a static block, a namespace, and a code block (same line, next line, blank-line groups, last statement, a JSDoc cast after a newline).
    • For interfaces, type literals, and enums: declare module, declare global, dotted and nested namespaces, const/declare enums, interfaces with extends, empty bodies with block, line, and several comments, and type literals in parameters and annotations.

Related findings

The repository check also found 25 files that main formats 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 @deprecated tags, and comments that used to jump outside closing }.

Parser (@tsrx/core): Treats StaticBlock, TSModuleBlock, JSXCodeBlock (setup + render siblings), TSInterfaceBody, and TSEnumDeclaration members 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 as innerComments; empty statements in static/namespace lists no longer own comments (neighbors or the container get them).

Formatter (@tsrx/prettier-plugin): Routes StaticBlock through the shared block printer (static {, blank lines between statements, empty-block innerComments). Adds printEmptyMemberList for 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.

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

Request Review

leonidaz and others added 2 commits September 24, 2026 16:54
…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 leonidaz changed the title fix(core): attach comments in static blocks, namespaces, and code blocks like a function body (#286) fix(core): attach comments in static blocks, namespaces, code blocks, interfaces, and enums like their siblings (#286, #293) Sep 24, 2026
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]>
leonidaz and others added 3 commits September 24, 2026 17:34
…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]>
@leonidaz
leonidaz merged commit 36e131a into main Sep 24, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/comment-attach-statement-lists-286 branch September 24, 2026 21:43
@github-actions github-actions Bot mentioned this pull request Sep 24, 2026
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