Repository navigation
fix(prettier): lay out template children like Prettier's TSX, put template values in parentheses like elements, keep significant spaces, and break attribute values like Prettier (#369, #402, #403, #376, #443, #447, #450, #457) - #459
Merged
Merged
Conversation
- 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]>
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]>
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]>
…ettier (#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]>
…alue 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]>
…nk 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]>
…#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]>
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]>
…02' into fix/prettier-template-children-402
…-children-402 # Conflicts: # packages/prettier-plugin/src/index.js
…dren (#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]>
…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]>
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]>
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]>
…-children-402 # Conflicts: # packages/vite-plugin-hono/tests/runtime.test.tsrx
AstPath.match's types don't take `undefined` for a level to skip. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ies, 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]>
…02' into fix/prettier-template-children-402
…s braces Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ments
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…02' into fix/prettier-template-children-402
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Stacked JSX wraps duplicate parens
- Comment-breaking elements now take only the existing comment-paren wrap, and the multi-line element wrap no longer runs when a parent already opened parentheses, so a second pair and a repeated trailing comment are not printed.
Or push these changes by commenting:
@cursor push 85e5d6b750
Preview (85e5d6b750)
diff --git a/packages/prettier-plugin/src/index.js b/packages/prettier-plugin/src/index.js
--- a/packages/prettier-plugin/src/index.js
+++ b/packages/prettier-plugin/src/index.js
@@ -3719,23 +3719,6 @@
nodeContent = [...printDecorators(decorated, path, options, print), nodeContent];
}
- // Like Prettier's `printJsxElement`, an element's comments print inside
- // the parentheses around a multi-line element, so a comment on its own
- // line opens them
- let suppressTrailingComments = args?.suppressTrailingComments;
- if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
- const trailingParts = suppressTrailingComments ? [] : printTrailingComments(node, options);
- nodeContent = maybeWrapJSXElementInParens(
- path,
- parts.length > 0 || trailingParts.length > 0
- ? [...parts, nodeContent, ...trailingParts]
- : nodeContent,
- options,
- );
- parts.length = 0;
- suppressTrailingComments = true;
- }
-
// A cast's parens belong to the cast, so they print even where a parent
// lays out the node's other parens (`suppressOwnParens`)
if (typeCastParens) {
@@ -3747,6 +3730,10 @@
args,
);
} else if (elementPrintsCommentsInParens(path, options, args)) {
+ // Comments that break the line print inside this one pair. Wrapping
+ // again in `maybeWrapJSXElementInParens` would stack a second pair
+ // around a group that already breaks, and reprint a trailing comment
+ // already inside the first pair.
return printElementInParens(
path,
options,
@@ -3758,6 +3745,17 @@
args?.suppressTrailingComments,
),
);
+ } else if (
+ (node.type === 'JSXElement' || node.type === 'JSXFragment') &&
+ !args?.suppressOwnParens
+ ) {
+ // A multi-line element in an expression position. A parent that already
+ // opened parentheses (`suppressOwnParens`, as `return` and `throw` do
+ // for an own-line comment) must not get another pair here.
+ nodeContent = maybeWrapJSXElementInParens(path, nodeContent, options);
+ if (needsParens(path, options)) {
+ nodeContent = ['(', nodeContent, ')'];
+ }
} else if (!args?.suppressOwnParens && needsParens(path, options)) {
nodeContent = ['(', nodeContent, ')'];
}
@@ -3767,7 +3765,7 @@
parts,
nodeContent,
options,
- suppressTrailingComments,
+ args?.suppressTrailingComments,
);
}You can send follow-ups to the cloud agent here.
Reviewed by Cursor Bugbot for commit f58b3df. Configure here.
…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]>
…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]>
…e wrap isTemplateExpression doesn't narrow the node from StyleSheet. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 task done
This was referenced Sep 25, 2026
jonkwheeler
added a commit
that referenced
this pull request
Nov 12, 2026
…ks (#1105) * fix(website): restore the spaces between words and inline code or links (#481) The page sources separated words from `<code>`, `<a>` and string children with a line break only (`has\n<code>…</code>\nand`), or with nothing at all (`a<code>`, `,<code>`). Whitespace that contains a line break is layout in JSX, so the text read "TSRX no longer has&{ ... }and&[ ... ]." in every target. Most of these came from the old formatter turning a space between children into a line break (#402, fixed in #459). A codemod over the parsed TSRX AST put a space at each such boundary, except after an opening bracket or a slash (`(<code>`, `if</code>/<code>else`) and before closing punctuation or a plural suffix (`</code>.`, `</code>s`); one `@switch` `@case` pair got a `{' '}` by hand. Prettier then laid the paragraphs out again. 1,105 boundaries in 8 pages changed. Each page compiles, with the Ripple compiler the site uses, to the same output except for whitespace. Under JSX's whitespace rules (React, Preact, Solid, Vue), the glued word/inline-element boundaries go from 1,087 to 0. 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 #369
Closes #402
Closes #403
Closes #376
Closes #443
Closes #447
Closes #450
Closes #457
Closes #472
Summary
Templates now lay out their children the way Prettier 3.9.6 lays out the same JSX in a TSX file, and the formatter no longer changes what a template renders. Every JSX subtree in the repo's
.tsrxfiles that has no TSRX-only syntax (749 of them, including all 454 on the website pages) prints exactly as Prettier prints it as TSX, except one fixture kept withprettier-ignore.<code>…</code>+ line break +. The compiler…</code>and.on separate lines</code>. Thefilled on the line, like Prettier<div>/<b>1</b> <b>2</b>/</div><b>1</b>, blank line,<b>2</b>: renders121 2hello <b>x</b> worldin a broken elementhelloxworldhello <b>x</b> world<p class="x" id="y">Hello</p><p class="x" id="y">,Hello,</p>(Prettier's forced break for two attributes)return <div>…multi-line…</div>return (…);const x = @if (a) { <div>Hello</div> },return @{ … }=/returnconst x = (…);,return (…);(templates in value positions get parentheses like elements, as decided)items.map((item) => <li … />)too longitems.map((item) => (…))<div>a··b</div>(·is U+00A0)<div>a b</div><script>const broken = ;</script>in a componentclass={a && b && c ? "x" : "y"}past the widthclass={a && b &&with continuation lines at the attribute's columnclass={, the value indented,}<Foo prop=<Bar /> /><Foo prop />(value deleted)Changes
printJSXChildrenports Prettier'sprintJsxChildren: text is split into words, andseparatorNoWhitespace,separatorWithWhitespace, andjsxWhitespacepick each break, so text and the children around it fill their lines, a child that touches text (</code>.) stays against it, and a line breaks at a space as{" "}.printJSXElementBodyports the rest ofprintJsxElementInternal: the separator cleanup, trimming,multilineChildrenwith the{" "}cases,fill(with text) or one child per line (without),forcedBreak(more than one attribute, a child element, more than one{…}child, an opening tag or child that breaks), and theconditionalGroupthat keeps an element on one line when it fits. Elements and fragments share it. The parser drops the whitespace with a line break after a closing tag, so the layout reads it back from the source, both to pick Prettier's separators and to keep blank lines between children.printJSXElementportsprintJsxOpeningElement: a lone string attribute never breaks the tag, a blank line between attributes stays,singleAttributePerLineneeds two attributes, andbracketSameLinefollowsshouldPrintBracketSameLine.maybeWrapJsxElementInParensadds parentheses around a multi-line element afterreturn,=,=>,&&, and in other expression positions. It also forces them for an arrow body that is a call argument in a{…}child. Template statement positions (isStatementSlot) don't wrap, like statements. The element's comments print inside the parentheses, as in Prettier'sprintJsxElement. fix(prettier): print an element's line-breaking comments inside its parentheses like Prettier, so a returned element keeps them (#456) #487, merged inmainmeanwhile, fixed [Bug]: prettier-plugin drops the parentheses of a returned element with a line comment before it, so it returns undefined #456 with a mechanism of its own for line-breaking comments (elementPrintsCommentsInParens,printElementInParens). Both applied after the merge, so they're now one function,printTemplateInParens, which keeps fix(prettier): print an element's line-breaking comments inside its parentheses like Prettier, so a returned element keeps them (#456) #487's rules: a JSDoc cast's comments stay ahead of the cast, a parent that lays out the parentheses gets the value bare, andneedsParensparentheses always print with the comments inside. All of fix(prettier): print an element's line-breaking comments inside its parentheses like Prettier, so a returned element keeps them (#456) #487's tests pass. The arrow printer's own rule for template bodies is gone, andcouldExpandArgaccepts an arrow with an element body, soitems.map((item) => (hugs.@if,@for,@switch,@try, and a@{ … }code block used as a value get the same parentheses as a multi-line element, in the same positions: after=(declarators, assignments, class fields, object values),return/throw, an expression-bodied=>,export default, and&&. They don't get them under the same no-wrap parents: call arguments, array items, attributes, children, conditional branches, and 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 ((p) => (@{ … })) withexpression: true, and that one is wrapped.isTemplateExpressioncovers these values wherever the printer follows Prettier's JSX rules, so an arrow body stays on the=>line, a last-argument arrow hugs, and a returned value's comments print inside its parentheses. A test formats every form in every position, checks that the output parses to the same AST, and a render test compiles three of them with@tsrx/react. No file in the repo has such a value, sopnpm formatchanges nothing for this.{…}children ([Bug]: prettier-plugin prints a comment at the start of a {…} child against the brace, with the expression at column 0 #457). A child container prints throughprintJsxExpressionContainer: a value with a comment breaks inside its braces, a conditional or binary child hugs, and so does a@{ }code block or template control flow.{" "}is read as a plain space, as in Prettier. The render test below checks that formatting changes no rendered text.<script>bodies ([Bug]: prettier-plugin adds blank lines around a script body that doesn't parse on every pass #403). The fallback prints the body like Prettier's HTML printer prints script text it can't format (dedentString(htmlTrimPreserveIndentation(value))joined withhardline).printJsxExpressionContainer'sshouldInlinerule, and a value written as an element without braces is printed instead of dropped.<script>and<style>bodies print as blocks, and a lone@{ … }child hugs its tags.features.tsrxwrote/* (unused) */as template text inside<code>, which TSRX reads as a comment, so the code span was empty. It's a string now.pnpm formatreformats 24 files: the website pages and components, the compiler's scoped-styles fixtures, the vite-plugin runtime tests and fixtures, and the Solid playground README. The rendered text of every element in them is unchanged (checked by cleaning each element's children with Babel's JSX whitespace rule before and after).vite-plugin-react/tests/runtime.test.tsrxkeeps its multi-line<pre>fixture with// prettier-ignore, since that test is about the line break.Output differences from Prettier that remain
cond ? (…) : (…)): [Bug]: prettier-plugin doesn't print conditionals with JSX branches in Prettier's JSX mode #446.//comment between text children moves to the closing tag (parser attachment): [Bug]: tsrx-core: Formatter moves a comment between JSX text children to the closing tag #388.{"string"}attribute values lose their braces: [Bug]: prettier-plugin removes the braces around a string literal in a JSX attribute #408. The opening-tag rules treat such a value as a string, like the string it prints as, so the output stays idempotent.{.../* c */ b}attaches after the attribute, andprettier-ignorethere prints twice: [Bug]: core: A comment inside a spread attribute attaches after the attribute, and prettier-ignore there prints twice #489 (parser).Verification
pnpm vitest run --project prettier-plugin: 1834 pass. 119 of them fail withmain's printer (new tests plus the ones updated to Prettier's output). Review follow-ups pin a commented<style>or@forafterreturn/throw/yieldkeeping its parentheses, CRLF script bodies with everyendOfLine, and Prettier's hug rule fornew,import(), andawaitof a fragment.main, with Prettier's output for the same JSX as TSX, computed in the test. Two render tests compile the input and the output with@tsrx/react, run TypeScript'sreact-jsxtransform, and check that the rendered markup is unchanged, one of them on website sections..tsrxfiles as TSX with Prettier (repo options, same nesting depth): 748 of 749 match (all 454 on the website), and the one left is theprettier-ignorefixture.pnpm format:check, Prettier checks of the changed files,node --check, andpnpm changeset:checkpass.tests/format/jsxsuite, plus the JSX@tsrx/reactcompiles from the repo's.tsrxfiles) withmain's printer (at b4ea5ca plus fix(prettier): print an element's line-breaking comments inside its parentheses like Prettier, so a returned element keeps them (#456) #487), this branch's, and Prettier'stypescriptparser. 7 don't parse as TSRX. 175 matched Prettier onmainand 572 here (397 fixed, none regressed). Outputs that aren't idempotent went from 18 to 3: the [Bug]: core: A comment inside a spread attribute attaches after the attribute, and prettier-ignore there prints twice #489 spread comment twice and a conditional with an element test ([Bug]: prettier-plugin doesn't print conditionals with JSX branches in Prettier's JSX mode #446), all unstable onmaintoo. The rendered text or AST changed in 66 outputs onmainand 7 here, all of which also change onmain(comment placement, [Bug]: prettier-plugin removes the braces around a string literal in a JSX attribute #408), and so do the 12 outputs where Babel counts fewer comments (//lines inside JSX text, which TSRX reads as comments).Related findings
Fixed here: #443, #447, #450, #457, #472 (#456 was closed by #487, whose mechanism this PR folds into the template wrap).
Filed separately:
<code>), likely left by the old formatter's [Bug]: prettier-plugin turns a significant space between template children into a blank line #402 bug. A content fix.🤖 Generated with Claude Code
Note
Medium Risk
Large, behavior-changing formatter logic affects layout and parentheses across the repo, with prior bugs around rendered whitespace making regressions user-visible though not runtime-critical.
Overview
Aligns
@tsrx/prettier-plugintemplate and JSX printing with Prettier 3.9’s TSX behavior so formatted.tsrxmatches what Prettier would do on equivalent JSX, without changing rendered text for significant whitespace.The printer replaces ad-hoc child merging with ports of Prettier’s
printJsxChildren/printJsxElementInternal: text fills lines, glued boundaries stay glued, spaces break as{" "}, and opening tags follow Prettier’s attribute/break rules. Elements and fragments shareprintJSXElementBody.Multi-line templates in expression positions (elements, fragments,
@if/@for/@switch/@try, value@{ … }, but not function-body code blocks) now getprintTemplateInParenswrapping like JSX afterreturn,=,=>,&&, etc., with comments inside the parens (folding in the earlier #487 behavior).Additional fixes:
printJSXExpressionContainer/shouldHugJSXExpressionfor attribute and child{…}values;printUnformattedRawTextfor unparseable<script>bodies (stable, re-indented);trimJSXWhitespace/ NBSP handling; braced-less attribute values likeprop=<Bar />are printed instead of dropped.Tests and repo fixtures are updated to the new output; the changeset documents the patch release.
Reviewed by Cursor Bugbot for commit 8306c3e. Bugbot is set up for automated code reviews on this repo. Configure here.