Skip to content

fix(prettier): print an element's line-breaking comments inside its parentheses like Prettier, so a returned element keeps them (#456) - #487

Merged
leonidaz merged 4 commits into
mainfrom
fix/prettier-return-comment-456
Sep 25, 2026
Merged

leonidaz merged 4 commits into
mainfrom
fix/prettier-return-comment-456

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

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. A return, throw, yield, or await keeps the element instead of ending at the comment and returning undefined.

Input Before Now
return ( + // note + <Note /> + ); return // note + <Note />;: returns undefined, and the next pass prints return; unchanged, like Prettier
the same with throw throw // note + <Note />;, which doesn't parse unchanged
the same with yield or await yield // note + <Note />; unchanged
return ( + ( + // note + <Note /> + ).props + ); return // note + (<Note />).props;: returns undefined return ( + // note + <Note /> + ).props;
return (/** + * note + */ <Note />); return /** … */ <Note />;: returns undefined return ( + /** … */ <Note /> + );
return ( + // note + /** @type {X} */ (<Note />) + ); return // note + /** @type {X} */ (<Note />);: returns undefined unchanged, like Prettier with babel
const f = () => + // note + <Note />; const f = () => // note + <Note />; const f = () => ( + // note + <Note /> + );
const x = + // note + <Note />; (also a: values and class fields) const x = // note + <Note />; const x = ( + // note + <Note /> + );
export default ( + // note + <Note /> + ); export default // note + <Note />; unchanged
x = a && ( + // note + <Note /> + ); x = a && // note + <Note />;, and the next pass moves the comment unchanged
const x = ( + <Note /> + // note + ); const x = <Note />; + // note unchanged

Changes

Output differences from Prettier that remain

Verification

  • pnpm vitest run --project prettier-plugin (on main at 3b3e128): 1655 pass, including 42 new cases in elements 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 the format helper checks that a second pass changes nothing. 36 of them fail with main'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 @if bodies). They cover return, throw, yield, and await; elements, fragments, and an element with an @if child; 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 .tsrx component body.

  • pnpm prettier --check on the changed files, pnpm format:check, node --check, and pnpm changeset:check pass; pnpm format changes 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, and export default (197 files; each whole file and each blank-line-separated chunk, 1136 snippets, 1033 of which all three parsed) with main's plugin (62ed470), this branch's, and Prettier (babel for .js, typescript for .ts/.tsx):

    Snippets Matched Prettier on main Here Changed Fixed Regressed
    1033 586 590 8 4 0

    The 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 on main: in comments/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 in jsx/fragment.js, <></>; statements that print as TSRX template statements.

Related findings

🤖 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, or await looked like they ended at the comment (e.g. return // note + <Note />).

The prettier plugin now mirrors Prettier’s printJsxElement behavior: elementPrintsCommentsInParens detects elements/fragments whose leading or trailing comments force a line break, and printElementInParens wraps comment + element in the right parentheses (including ifBreak wrapping when the parent wouldn’t add its own). printTsrxNode takes that path before generic needsParens. hasLeadingOwnLineComment gains an isTypeCast flag 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.

leonidaz and others added 4 commits September 24, 2026 23:55
…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]>
@leonidaz
leonidaz merged commit d77c039 into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/prettier-return-comment-456 branch September 25, 2026 04:40
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: prettier-plugin drops the parentheses of a returned element with a line comment before it, so it returns undefined

1 participant