Repository navigation
fix(prettier): keep comments in JSX braces and dynamic tag names like Prettier (#572, #573, #574) - #595
Merged
Conversation
…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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tier-jsx-brace-comments-572
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 #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.<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 parsea={,// only,}, like Prettier<div>{/* a */ /* b */}</div>{/* a *//* b */}{/* a *///* b */}, like Prettier<div>{a/// g/}</div>{a}, then// gbetween the children{,a,// g,}, like Prettier<div b={a/// g/} c="1" />b={a}, then// gbeforec(deleted before #575)b={,a,// g,}, like Prettier<div>text {a/// g/} more</div>text {a}/// g/more: the space beforemoreis losttext{" "},{,a,// g,}{" "},more, like Prettier<{Comp /* c */}>text</{Comp}></{Comp /* c */}>: the comment prints again<{Comp}>text</{Comp /* d */}></{Comp /* c */}>or</{Comp}>: the closing tag's comment is replaced or deleted<{Comp/// c/}>text</{Comp}>// cmoves into the children<{,Comp,// c,}><{Comp // c/}>text</{Comp}><{Comp}> // cand</{Comp}> // c: printed twice, and the next format moves the first into the children<{,Comp // c,}>Changes
JSXEmptyExpression, whichprintTsrxNodeprinted side by side, with nothing to end a line comment before the}. This PR ports Prettier'sprintJsxEmptyExpression. The comments print on consecutive lines (join(hardline)). When one of them is a line comment, they're indented on lines of their own, with a line break before the}. This covers children, attribute values, and TSRX template children, including those in control flow.printJSXElementprinted a dynamic tag's name once, from the opening tag's expression, and used that doc for the closing tag too. The closing tag repeated the opening tag's comments and never printed its own. The newprintJSXDynamicTagNameprints each tag from its own expression. The parser requires the two expressions to match as source, without the comments around them, so the tags still print alike. A comment inside the expression, as ina /* x */ .b, is in both tags' source and prints in both. With comments, the braces print like an attribute value's (printJSXExpressionContainer), so a line comment breaks them open and doesn't take the}or>. Without comments, the name prints as before.{…}([Bug]: prettier-plugin: A comment on its own line after the expression of a {…} moves out of the braces, or is deleted before an attribute #574).keepsCommentsAfterChildren(core) keeps the comments after the last child of everyJSX…node, so a comment after a container's expression went to the next child, the closing tag, or the next attribute's name. That moved it out of the braces. This PR leaves outJSXExpressionContainer, as [Bug]: prettier-plugin: A comment on its own line after a spread's argument is deleted or moves out of the braces #517 did for spreads, so the comment trails the expression the way Prettier attaches it. The container then prints it inside broken braces. This also covers a dynamic tag's name, now that [Bug]: prettier-plugin: A comment in a dynamic tag's braces prints again in the closing tag #573 prints each tag's comments once. The compilers print no comments in JSX, so their output doesn't change.Output differences from Prettier that remain
</{,Comp // d,}>), and the element stays on its line. Prettier prints a line comment in a closing tag the same way (</div>with// dgives</,// d,div,>).{name,// c,}) still move a comment on its own line out of the braces. The parser ends their container right after the name ([Bug]: tsrx-core: A shorthand attribute's braces end right after the name, so a comment on its own line before the } moves out of them #592, routed to the parser work). Once that's fixed, the comment trails the name like here.Verification
pnpm vitest run --project prettier-plugin: 2401 pass (withmainat 1af65cc merged), 44 of them new. 36 fail withmain'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 onmain. 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.{" "}, 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. Withmain, 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 withmain.tests/format/jsxinputs and compiled repo JSX): 1 output differs frommain. It's Prettier'sjsx/comments/eslint-disable.jsfixture, which now matches Prettier's snapshot (/* eslint-enable */stays in the braces). Against Prettier: 590 match here, 589 onmain, and nothing regresses. The 7 problems are the same onmain([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).//-word corpora (2,400, 3,312, and 540): same output asmain, 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).pnpm formatchanges no other file in the repo.pnpm format:check,node --check, andpnpm changeset:checkpass.Related findings
JSXExpressionContainerends one character after the name, not at its}. A comment on its own line before the}falls outside the container and moves out of the braces.🤖 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
JSXExpressionContainerlike 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
JSXEmptyExpressionso 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 viaprintJSXDynamicTagName, preserving per-tag comments and reusingprintJSXExpressionContainerwhen comments need broken braces.Reviewed by Cursor Bugbot for commit 724a674. Bugbot is set up for automated code reviews on this repo. Configure here.