Skip to content

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
leonidaz merged 33 commits into
mainfrom
fix/prettier-template-children-402
Sep 25, 2026

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

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 .tsrx files 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 with prettier-ignore.

Input Before Now
<code>…</code> + line break + . The compiler… </code> and . on separate lines </code>. The filled on the line, like Prettier
<div> / <b>1</b> <b>2</b> / </div> <b>1</b>, blank line, <b>2</b>: renders 12 unchanged, renders 1 2
hello <b>x</b> world in a broken element one child per line: renders helloxworld hello <b>x</b> world
<p class="x" id="y">Hello</p> one line <p class="x" id="y">, Hello, </p> (Prettier's forced break for two attributes)
return <div>…multi-line…</div> no parentheses return ( … );
const x = @if (a) { <div>Hello</div> }, return @{ … } the template against = / return const x = ( … );, return ( … ); (templates in value positions get parentheses like elements, as decided)
items.map((item) => <li … />) too long the arrow on the next line items.map((item) => ( … ))
<div>a··b</div> (· is U+00A0) <div>a b</div> unchanged
<script>const broken = ;</script> in a component more blank lines on every pass stable, lines re-indented under the tag
class={a && b && c ? "x" : "y"} past the width class={a && b && with continuation lines at the attribute's column class={, the value indented, }
<Foo prop=<Bar /> /> <Foo prop /> (value deleted) unchanged

Changes

Output differences from Prettier that remain

Verification

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:

🤖 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-plugin template and JSX printing with Prettier 3.9’s TSX behavior so formatted .tsrx matches 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 share printJSXElementBody.

Multi-line templates in expression positions (elements, fragments, @if/@for/@switch/@try, value @{ … }, but not function-body code blocks) now get printTemplateInParens wrapping like JSX after return, =, =>, &&, etc., with comments inside the parens (folding in the earlier #487 behavior).

Additional fixes: printJSXExpressionContainer / shouldHugJSXExpression for attribute and child {…} values; printUnformattedRawText for unparseable <script> bodies (stable, re-indented); trimJSXWhitespace / NBSP handling; braced-less attribute values like prop=<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.

leonidaz and others added 14 commits September 24, 2026 22:30
- 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]>
…-children-402

# Conflicts:
#	packages/prettier-plugin/src/index.js
leonidaz and others added 5 commits September 24, 2026 23:54
…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
@leonidaz leonidaz changed the title fix(prettier): keep significant spaces in templates, keep unparsable script bodies stable, and break attribute values like Prettier (#402, #403, #376, #443, #447, #450) fix(prettier): lay out template children like Prettier's TSX, keep significant spaces, and break attribute values like Prettier (#369, #402, #403, #376, #443, #447, #450, #456, #457) Sep 25, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread packages/prettier-plugin/src/index.js Resolved
Comment thread packages/prettier-plugin/src/index.js Resolved
Comment thread packages/prettier-plugin/src/index.js Resolved
leonidaz and others added 8 commits September 25, 2026 00:14
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]>
leonidaz and others added 3 commits September 25, 2026 00:42
…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]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

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.

Create PR

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.

Comment thread packages/prettier-plugin/src/index.js Resolved
leonidaz and others added 2 commits September 25, 2026 00:46
…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]>
@leonidaz leonidaz changed the title fix(prettier): lay out template children like Prettier's TSX, keep significant spaces, and break attribute values like Prettier (#369, #402, #403, #376, #443, #447, #450, #456, #457) 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) Sep 25, 2026
…e wrap

isTemplateExpression doesn't narrow the node from StyleSheet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 9fdebc1 into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/prettier-template-children-402 branch September 25, 2026 05:04
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment