Repository navigation
fix(prettier): print an element's line-breaking comments inside its parentheses like Prettier, so a returned element keeps them (#456) - #487
Merged
Conversation
…arentheses like Prettier (#456) Like Prettier's printJsxElement, an element or fragment whose comment breaks the line prints the comment inside its own parentheses: the ones from needsParens, or else the ones maybeWrapJsxElementInParens adds when the element breaks. A return, throw, yield, or await argument that starts with a line comment, or a block comment that ends its line or spans lines, keeps its parentheses instead of ending at the comment and returning undefined. The same element after =, =>, :, export default, or an operator prints in parentheses like Prettier, and a trailing comment inside them stays there. A JSDoc-cast element's comments still print ahead of the cast, so hasLeadingOwnLineComment counts them for getOwnLineCommentAhead, as for the ParenthesizedExpression Prettier's babel parser keeps. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…omment-456 Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz
added a commit
that referenced
this pull request
Sep 25, 2026
…template wrap #487 (merged in main) printed an element's line-breaking comments inside its parentheses with its own elementPrintsCommentsInParens and printElementInParens. The template wrap from #369 already prints every element's and template value's comments inside the parentheses Prettier's maybeWrapJsxElementInParens adds, so both applied. They are one mechanism now, printTemplateInParens, which keeps #487's rules: a JSDoc cast's comments stay ahead of the cast, a parent that lays out the parentheses (suppressOwnParens) gets the value bare, needsParens parentheses always print with the comments inside, and template statement positions (isStatementSlot) don't wrap. All of #487's tests pass. Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz
added a commit
that referenced
this pull request
Sep 25, 2026
…ed element A return or throw whose element has a leading and a trailing comment prints one pair of parentheses with each comment once, and the output parses back to the same AST. With #487's wrap stacked on the template wrap, the element was wrapped twice. Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz
added a commit
that referenced
this pull request
Sep 25, 2026
…plate values in parentheses like elements, keep significant spaces, and break attribute values like Prettier (#369, #402, #403, #376, #443, #447, #450, #457) (#459) * fix(prettier): keep significant spaces between template children (#402) - A space between two children, or between a child and a tag, renders, so it no longer becomes a line break. Children separated by one stay together in one unit, like glued children, and break at the space as `{" "}` when they don't fit, like Prettier's jsxWhitespace. - A leading space prints as `{" "}` on a line of its own and a trailing one as `{" "}` after the last child once the body breaks; a lone space stays `<div> </div>` / `<> </>`. - A single text child keeps its edge spaces against the tags and switches to the `{" "}` form when it moves onto its own lines. - `{" "}` children are treated as a plain space, as in Prettier, so a second pass reads back what the first printed. - The element and fragment printers share printJSXChildLines, and the one-line layout for mixed text and expressions is built from the same entries. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(prettier): keep non-breaking spaces in template text (#443) The JSX text helpers trimmed, split, and collapsed text with `\s` and `String#trim`, which match U+00A0, so `a··b` became `a b` and a non-breaking space between or before children was dropped. Like Prettier's jsxWhitespace they now count only space, tab, and line breaks as JSX whitespace. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(prettier): keep an unparsable <script> body stable (#403) The fallback for a `<script>` body that doesn't parse printed the raw text with its surrounding newlines and indentation inside the element's own hardlines, so every pass added blank lines. It now prints the body the way Prettier's HTML printer prints text it can't format (getTextValueParts): drop the blank line before it and the whitespace after it, remove the indentation its lines share, and put each line at the element's indentation. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(prettier): break JSX attribute values inside their braces like Prettier (#376) printJSXAttribute printed an expression value as `name={expr}` with no group, so a value that didn't fit stayed against `={` and `}` and its continuation lines started at the attribute's column. It now follows Prettier's printJsxExpressionContainer: a value that can break after its first token (array, object, function, call, template, or `await` of one) hugs the braces, and any other value breaks onto its own lines inside them, with a lineSuffixBoundary before `}` so a trailing line comment stays inside. Co-Authored-By: Claude Opus 5.5 <[email protected]> * style: format the repo with the new template children and attribute value layout Space-separated children now fill their lines with `{' '}` at the breaks, and a JSX element attribute value breaks inside its braces. The rendered text of every element in these files is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(prettier): keep element attribute values and fill text across blank lines (#447, #450) - printJSXAttribute returned only the name for a value that is neither a string nor an expression container, so `prop=<Bar />` became `prop`. It now prints the element or fragment, like Prettier (#447). - printRawText kept a line break that no indentation followed, so text after a blank line or an unindented line printed at column 0 or 1 through a literalline. It now splits the text at every JSX whitespace run and fills the words, like Prettier's printJsxChildren (#450). The vite-plugin-react runtime fixture that tests multi-line text gets a `// prettier-ignore` so it keeps its unindented line. - Children that are only spaces (`{" "}{" "}`) print as one space, like a lone space, instead of a `{" "}` line that the next pass joins (#402). Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(prettier): pin the text fill with text that Prettier also breaks (#450) Short text that fits on one line keeps the current layout for an element whose text was written on several lines, which Prettier prints against the tags. Use text long enough that both break, so the test pins Prettier's output. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(prettier): type the element child node lists The `{" "}` branch pushes the child itself, so the evolving arrays lost their inferred type under the typecheck. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(prettier): lay out template children like Prettier's printJsxChildren (#369, #457) Port the rest of Prettier's JSX layout: - printJSXChildren is Prettier's printJsxChildren: text splits into words, and separatorNoWhitespace, separatorWithWhitespace, and jsxWhitespace pick each break, so text and the children next to it fill the lines and a child that touches text stays against it. - printJSXElementBody is the rest of printJsxElementInternal: the separator cleanup, the trimming, multilineChildren with the `{" "}` cases, the fill or one-child-per-line content, forcedBreak, and the conditionalGroup that keeps an element on one line when it fits. Elements and fragments share it. - printJSXElement ports printJsxOpeningElement: a lone string attribute doesn't break the tag, a blank line between attributes stays, singleAttributePerLine needs two attributes, and bracketSameLine follows shouldPrintBracketSameLine. - A `{…}` child prints through printJsxExpressionContainer, so a child that starts with a comment breaks inside its braces (#457); a code block or template control flow hugs them. - maybeWrapJSXElementInParens adds Prettier's parentheses around a multi-line element after `return`, `=`, `=>`, and in other expression positions; template bodies are no-wrap parents like statements. The arrow printer's own rule for template bodies goes, and couldExpandArg takes an arrow with an element body, so `items.map((item) => (` hugs. - The parser drops the whitespace with a line break after a closing tag, so the layout reads it back from the source to pick the same separators and keep blank lines between children. `<script>`, `<style>`, and a lone `@{ … }` child keep their own layouts. The old child-layout helpers (glued units, the single-text path, the fragment's line-per-text layout) are gone. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(website): show the /* (unused) */ code span on the features page (#472) TSRX reads `/* … */` in template text as a comment, so the code span was empty and the sentence read "left behind as a comment." Write the text as a string, like the page's other code spans. Co-Authored-By: Claude Opus 5.5 <[email protected]> * style: format the repo with Prettier's template child layout Templates across the website, the compiler and plugin test fixtures, and the Solid playground README now print like the same JSX in TSX: text and inline elements fill their lines, elements with more than one attribute or a child element break, and multi-line elements after `return` or `=>` get parentheses. Every JSX subtree without TSRX-only syntax in the repo's `.tsrx` files matches Prettier's TSX output, and the rendered text of every element is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(prettier): print an element's comments inside its parentheses (#456) Like Prettier's printJsxElement, the parentheses around a multi-line element wrap its comments too, so a comment on its own line opens them. A returned or thrown element with a line comment before it no longer prints as `return // note`, which returned undefined. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(prettier): pass a predicate for every path.match level AstPath.match's types don't take `undefined` for a level to skip. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(prettier): pin commented templates after return, CRLF script bodies, and attribute hugging - A <style> block or template control flow after `return`, `throw`, or `yield` with a comment before it keeps its parentheses (printed by printReturnOrThrowArgument), so the statement still returns it. - An unparsable <script> body with CRLF line endings prints cleanly with every endOfLine setting; the fallback now also normalizes `\r\n` itself. - Like Prettier's shouldInline, `new`, `import()`, and `await` of a fragment break inside an attribute's braces. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(prettier): pin await of a fragment breaking inside an attribute's braces Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(prettier): put multi-line template values in parentheses like elements A user decision: `@if`, `@for`, `@switch`, `@try`, and a `@{ … }` value get the parentheses Prettier's maybeWrapJsxElementInParens gives a multi-line element, in the same positions (after `=`, `return`, `throw`, an expression-bodied `=>`, a class field, an object value, `export default`, `&&`) and not under the same no-wrap parents (call arguments, array items, attributes, children, conditional branches, template bodies). A code block that is a function's body (`function A() @{ … }`, `(p) => @{ … }`) stays bare; the parser marks an arrow whose expression body is a code block with `expression: true`. isTemplateExpression now covers these template values wherever the printer follows Prettier's JSX rules (the arrow body staying on the `=>` line, a last-argument arrow hugging, `&&` operands, and a returned value's comments printing inside its parentheses). Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(changeset): reflow the template children paragraph Co-Authored-By: Claude Opus 5.5 <[email protected]> * refactor(prettier): fold #487's element comment parentheses into the template wrap #487 (merged in main) printed an element's line-breaking comments inside its parentheses with its own elementPrintsCommentsInParens and printElementInParens. The template wrap from #369 already prints every element's and template value's comments inside the parentheses Prettier's maybeWrapJsxElementInParens adds, so both applied. They are one mechanism now, printTemplateInParens, which keeps #487's rules: a JSDoc cast's comments stay ahead of the cast, a parent that lays out the parentheses (suppressOwnParens) gets the value bare, needsParens parentheses always print with the comments inside, and template statement positions (isStatementSlot) don't wrap. All of #487's tests pass. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(prettier): pin one pair of parentheses around a commented returned element A return or throw whose element has a leading and a trailing comment prints one pair of parentheses with each comment once, and the output parses back to the same AST. With #487's wrap stacked on the template wrap, the element was wrapped twice. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(prettier): cast the node for printTrailingComments in the template wrap isTemplateExpression doesn't narrow the node from StyleSheet. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- 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 #456
Summary
An element or fragment with a comment that breaks the line now prints the comment inside its own parentheses, like Prettier 3.9.6's
printJsxElement. Areturn,throw,yield, orawaitkeeps the element instead of ending at the comment and returningundefined.return (+// note+<Note />+);return // note+<Note />;: returnsundefined, and the next pass printsreturn;throwthrow // note+<Note />;, which doesn't parseyieldorawaityield // note+<Note />;return (+(+// note+<Note />+).props+);return // note+(<Note />).props;: returnsundefinedreturn (+// note+<Note />+).props;return (/**+* note+*/ <Note />);return /**…*/ <Note />;: returnsundefinedreturn (+/**…*/ <Note />+);return (+// note+/** @type {X} */ (<Note />)+);return // note+/** @type {X} */ (<Note />);: returnsundefinedbabelconst f = () =>+// note+<Note />;const f = () => // note+<Note />;const f = () => (+// note+<Note />+);const x =+// note+<Note />;(alsoa:values and class fields)const x = // note+<Note />;const x = (+// note+<Note />+);export default (+// note+<Note />+);export default // note+<Note />;x = a && (+// note+<Note />+);x = a && // note+<Note />;, and the next pass moves the commentconst x = (+<Note />+// note+);const x = <Note />;+// noteChanges
getOwnLineCommentAhead, whichprintReturnOrThrowArgumentandprintYieldExpressionuse, tohasLeadingOwnLineComment, the port of Prettier's helper from fix(prettier): lay out assignments, arrow functions, and call arguments like Prettier (#340, #332, #316, #337, #335, #336, #325, #326, #298) #383. Like Prettier's, it answers onlyprettier-ignorefor an element. Prettier can skip the element's comments there becauseprintJsxElementprints them itself, inside the parenthesesmaybeWrapJsxElementInParensadds, and a comment's line break breaks those parentheses onto their own lines. The plugin printed the comments ahead of the element with no parentheses. Before fix(prettier): keep comments where they are written and print them like Prettier (#274, #279, #282, #294, #296, #299, #301, #306, #307, #319, #320, #353, #355, #358, #364, #365, #390) #391, the check (A'shasOwnLineLeadingComment) had no exception for elements, soreturnkept the parentheses, and an assigned value or arrow body broke after the=or=>. Since fix(prettier): keep comments where they are written and print them like Prettier (#274, #279, #282, #294, #296, #299, #301, #306, #307, #319, #320, #353, #355, #358, #364, #365, #390) #391 they print the comment after the operator and the element at column 0.printTsrxNodeports that part ofprintJsxElementfor an element whose comments break the line (elementPrintsCommentsInParens): a leading comment that ends its line or spans lines, and a trailing line comment, one on its own line, or one that spans lines.printElementInParensputs the comments with the element and wraps them the way Prettier does: in theneedsParensparentheses when there are some ((// note+<Note />).props), bare under Prettier's no-wrap parents (ELEMENT_NO_WRAP_PARENTS: arrays, call andnewarguments, conditionals, JSX children, attributes, and containers) and in a TSRX statement slot (isStatementSlotfrom fix(prettier): drop redundant type parentheses, parenthesize element operands, and break around continued strings like Prettier (#396, #425, #407) #451), and otherwise ingroup([ifBreak("("), indent([softline, …]), softline, ifBreak(")")]), which the comment breaks. Other elements are unchanged: parentheses around a multi-line element without such a comment are [Bug]: prettier-plugin lays out JSX children and multi-line elements differently from Prettier #369, and inline comments stay outsideneedsParensparentheses ([Bug]: prettier-plugin prints an element's comments outside its parentheses #449).printReturnOrThrowArgument(return,throw),printYieldExpression([Bug]: Formatter changes yielded values when the argument starts with a line comment #274),await, the assignment layout andprintValueAfterLeadingComment(declarators, assignments, property values, class fields),export default, arrow bodies and chains, and logical operands needed no change: they print the element, which now brings its own parentheses, sohasLeadingOwnLineCommentkeeps Prettier's exception for elements. The one exception is an element in a JSDoc cast. It still prints its comments ahead of the cast's parentheses, where Prettier'sbabelparser keeps aParenthesizedExpressionthat isn't an element, sohasLeadingOwnLineCommenttakes anisTypeCastflag, whichgetOwnLineCommentAheadpasses. Without it,return (// note /** @type {X} */ (<Note />))still returnedundefined. The cast in an assignment already matchedbabelthroughprintValueAfterLeadingComment.pnpm formatchanges nothing in the repo.Output differences from Prettier that remain
return,throw, oryieldargument that isn't an element and starts with a block comment spanning lines on its line (return (/* a+b */ foo);) still loses its parentheses and returnsundefined: [Bug]: prettier-plugin drops the parentheses of a return, throw, or yield argument that starts with a block comment spanning lines, so it returns undefined #473 (filed here; Prettier'sreturnArgumentHasLeadingCommenthas a clause for it). Same output asmain.foo(() => (+// c+<div />+));breaks the arguments;mainbreaks them too, with the body at column 0: [Bug]: prettier-plugin doesn't hug a last-argument arrow that returns an element, and breaks the arguments instead #480 (filed here, pairs with [Bug]: prettier-plugin lays out JSX children and multi-line elements differently from Prettier #369).const f = () => (+<Note />+// note+);), since the parser gives it to the declaration: [Bug]: prettier-plugin: A comment after a parenthesized arrow body moves out of the arrow, after the statement #475 (filed here). Same output asmain.babeladds them: [Bug]: prettier-plugin prints an element's comment inside a JSDoc cast without the element's own parentheses #476 (filed here). Same output asmain.=of a default value stays there: [Bug]: prettier-plugin keeps a comment after the = of a default value there, where Prettier moves it before the parameter #474 (filed here;handleAssignmentPatternComments). The element value now prints in parentheses.?(cond ? (+// note+<Note />+) : nullprintscond+? // note+ … ): the JSX-mode conditional of [Bug]: prettier-plugin doesn't print conditionals with JSX branches in Prettier's JSX mode #446. Same output asmain.needsParensparentheses ([Bug]: prettier-plugin prints an element's comments outside its parentheses #449).Verification
pnpm vitest run --project prettier-plugin(onmainat 3b3e128): 1655 pass, including 42 new cases inelements print comments that break the line inside their parentheses. Each one also parses the output and checks that the syntax tree matches the input's, and theformathelper checks that a second pass changes nothing. 36 of them fail withmain's printer; the other 6 pin layouts that already matched (an inline block comment, call arguments, arrays, trailing comments after the statement, and comments on elements in@{ }and@ifbodies). They coverreturn,throw,yield, andawait; elements, fragments, and an element with an@ifchild; line comments, own-line block comments, and block comments spanning lines; a comment on the(line, before nested parentheses, and inside them; arrow bodies and chains, declarators, property values, class fields,export default, logical operands, member objects, callees, and a JSDoc cast; and a.tsrxcomponent body.pnpm prettier --checkon the changed files,pnpm format:check,node --check, andpnpm changeset:checkpass;pnpm formatchanges nothing. CI runs the other projects and the typecheck.A probe formatted Prettier 3.9.6's own fixtures for JSX, TSX, comments,
return,throw,yield, arrows, assignment comments, closure type casts, andexport default(197 files; each whole file and each blank-line-separated chunk, 1136 snippets, 1033 of which all three parsed) withmain's plugin (62ed470), this branch's, and Prettier (babelfor.js,typescriptfor.ts/.tsx):mainThe other 4 changed outputs are whole files that now match in these places but differ elsewhere, and
KEYPAD_NUMBERS.map((num) => (+// Buttons 0-9…, which matches except for [Bug]: prettier-plugin doesn't hug a last-argument arrow that returns an element, and breaks the arguments instead #480. No output loses a comment. The outputs that aren't idempotent or change the syntax tree are the same as onmain: incomments/return-statement.js, cases where Prettier's own output isn't idempotent either and the [Bug]: prettier-plugin drops the parentheses of a return, throw, or yield argument that starts with a block comment spanning lines, so it returns undefined #473 cases, and injsx/fragment.js,<></>;statements that print as TSRX template statements.Related findings
return,throw, oryieldargument that isn't an element and starts with a block comment spanning lines loses its parentheses (returnsundefined).=of a default value stays after it (handleAssignmentPatternCommentsisn't ported).couldExpandArglacks Prettier's element case).🤖 Generated with Claude Code
Note
Low Risk
Scoped to @tsrx/prettier-plugin output layout with broad regression tests; no runtime or auth/data changes, only closer Prettier parity for comment + element formatting.
Overview
Fixes #456, where line-breaking comments before or after TSRX elements could be printed outside the element so
return,throw,yield, orawaitlooked like they ended at the comment (e.g.return // note+<Note />).The prettier plugin now mirrors Prettier’s
printJsxElementbehavior:elementPrintsCommentsInParensdetects elements/fragments whose leading or trailing comments force a line break, andprintElementInParenswraps comment + element in the right parentheses (includingifBreakwrapping when the parent wouldn’t add its own).printTsrxNodetakes that path before genericneedsParens.hasLeadingOwnLineCommentgains anisTypeCastflag so JSDoc-cast elements still print comments ahead of the cast parens.Adds a large test suite (format idempotence + unchanged AST shape) for
return/throw/yield/await, assignments, arrows,export default, trailing comments, and cases that should not add extra parens.Reviewed by Cursor Bugbot for commit d73abf0. Bugbot is set up for automated code reviews on this repo. Configure here.