Skip to content

fix(prettier): keep comments in JSX braces and dynamic tag names like Prettier (#572, #573, #574) - #595

Merged
leonidaz merged 5 commits into
mainfrom
fix/prettier-jsx-brace-comments-572
Sep 25, 2026
Merged

leonidaz merged 5 commits into
mainfrom
fix/prettier-jsx-brace-comments-572

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #572
Closes #573
Closes #574

Summary

Comments inside the braces of a JSX child, an attribute value, or a dynamic tag's name stay in those braces. A line comment alone in braces no longer takes the } into the comment, which made the output fail to parse. A comment in a dynamic tag's braces now prints once instead of again in the closing tag.

Input Before Now
<div>{ / // only / }</div> <div>{// only}</div>, which doesn't parse {, // only, } on lines of their own, like Prettier
<div a={ / // only / } /> <div a={// only} />, which doesn't parse a={, // only, }, like Prettier
<div>{/* a */ /* b */}</div> {/* a *//* b */} {/* a */ / /* b */}, like Prettier
<div>{a / // g / }</div> {a}, then // g between the children {, a, // g, }, like Prettier
<div b={a / // g / } c="1" /> b={a}, then // g before c (deleted before #575) b={, a, // g, }, like Prettier
<div>text {a / // g / } more</div> text {a} / // g / more: the space before more is lost text{" "}, {, a, // g, }{" "}, more, like Prettier
<{Comp /* c */}>text</{Comp}> </{Comp /* c */}>: the comment prints again unchanged
<{Comp}>text</{Comp /* d */}> </{Comp /* c */}> or </{Comp}>: the closing tag's comment is replaced or deleted unchanged
<{Comp / // c / }>text</{Comp}> // c moves into the children <{, Comp, // c, }>
<{Comp // c / }>text</{Comp}> <{Comp}> // c and </{Comp}> // c: printed twice, and the next format moves the first into the children <{, Comp // c, }>

Changes

Output differences from Prettier that remain

Verification

  • pnpm vitest run --project prettier-plugin: 2401 pass (with main at 1af65cc merged), 44 of them new. 36 fail with main's printer and parser. The 8 pins pass on both: a block comment alone in braces as a child, as a value, and in a template, an own-line block comment alone, a same-line line comment after a child's or value's expression, a comment after the }, and a comment inside a dynamic tag's member expression.
  • pnpm vitest run packages/tsrx/tests/utils/parser.test.js: 535 pass, 1 new, which fails on main. It checks that the comment after a child's, an attribute value's, or a dynamic tag's expression trails it, in both of a dynamic tag's tags.
  • A generated corpus of 406 cases covers 25 brace contents (alone, before, after, and own-line comments, block and line, after calls, conditionals, arrows, arrays, strings, {" "}, and templates). They're placed as a child, a child between text, a child before a sibling, a value with and without other attributes, a fragment child, a template child (also in @if), and a returned element. The corpus adds 9 dynamic tag names in opening, closing, self-closing, and nested tags. With main, 113 of the 406 fail: 52 outputs don't parse ([Bug]: prettier-plugin: A line comment alone in the braces of a JSX child or attribute swallows the closing brace #572), 43 lose or duplicate a comment or need a second pass, and 18 change the rendered text. Here none fail: every output parses to the same AST, keeps every comment, and is idempotent. The 225 cases that are valid TSX all match Prettier 3.9.6 (typescript) here, against 81 with main.
  • Group L's 730 JSX statements (Prettier's tests/format/jsx inputs and compiled repo JSX): 1 output differs from main. It's Prettier's jsx/comments/eslint-disable.js fixture, which now matches Prettier's snapshot (/* eslint-enable */ stays in the braces). Against Prettier: 590 match here, 589 on main, and nothing regresses. The 7 problems are the same on main ([Bug]: core: A comment inside a spread attribute attaches after the attribute, and prettier-ignore there prints twice #489 and two known embedded-CSS and key-quoting cases).
  • Group U's comment-in-text corpora (5,488 and 1,296 templates) and Group AA's //-word corpora (2,400, 3,312, and 540): same output as main, with the same problems ([Bug]: core: A block comment on the line after a closing tag makes the space after it render #540 and inputs that don't parse).
  • The React, Preact, Solid, Vue, and Hono compilers and the Volar mappings drop JSX comments before and after this PR, so the attachment change doesn't reach their output.
  • pnpm format changes no other file in the repo. pnpm format:check, node --check, and pnpm changeset:check pass.

Related findings

🤖 Generated with Claude Code


Note

Medium Risk
Touches core comment attachment and JSX printing paths used broadly by the formatter; behavior changes are well-covered by tests but could affect idempotent formatting of edge-case JSX comment layouts.

Overview
Aligns TSRX JSX comment placement and printing with Prettier so brace comments stay valid and aren’t lost or duplicated.

The parser no longer treats JSXExpressionContainer like a node that “keeps” post-child comments on the container itself, so a comment on its own line before } becomes a trailing comment on the expression (children, attribute values, dynamic tag names) instead of leaking to the next sibling, attribute, or closing tag.

The prettier plugin updates JSXEmptyExpression so comment-only {…} blocks break lines correctly—especially line comments—so } isn’t swallowed into // … (unparseable output). Dynamic tags (<{Comp}>) now print opening and closing names from each tag’s own expression via printJSXDynamicTagName, preserving per-tag comments and reusing printJSXExpressionContainer when comments need broken braces.

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

leonidaz and others added 5 commits September 25, 2026 14:54
…574)

Like Prettier, the comments after the expression of a JSX expression
container (a child, an attribute value, or a dynamic tag's name), before
its `}`, trail the expression, as they already do for a spread's
argument. The next child, the closing tag, or the next attribute's name
took them, so they moved out of the braces.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…572, #573, #574)

- Port Prettier's `printJsxEmptyExpression`: the comments alone in `{…}`
  print on consecutive lines, and with a line comment on lines of their
  own followed by a line break, so the `}` no longer joins the comment's
  line, which didn't parse (#572).
- Print each tag of a dynamic tag from its own expression, so the comments
  in the opening tag's braces no longer print again in the closing tag,
  and the closing tag's own comments are no longer dropped. With comments,
  the braces print like those of an attribute value (#573).
- Test that the comments after the expression of a `{…}`, which now trail
  it, print inside its braces like Prettier (#574).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit e82305f into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/prettier-jsx-brace-comments-572 branch September 25, 2026 19:42
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