Skip to content

feat(core, prettier plugins): read // like JavaScript and render comments between children like {/* … */} - #858

Merged
leonidaz merged 5 commits into
mainfrom
proto/comment-containers
Sep 27, 2026
Merged

leonidaz merged 5 commits into
mainfrom
proto/comment-containers

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Important

For review. Don't merge until the user has reviewed it. It changes two language rules for comments between JSX children, and both formatters follow them. Ripple, Octane and @tsrx/oxc need their upgrade issues updated before a release (see "Not done here").

The two rules

1. A comment between children renders like {/* … */} in TSX. The text on each side of it follows JSX's whitespace rules on its own. Before, a comment was invisible: the text on both sides was joined first (#615). Where the two rules differ is where TSX's familiar {/* */} behavior shows:

<div>
  <b>t</b>
  /* c */ <i />
</div>

Before, nothing rendered between <b> and <i>. Now there's a space, as TSX renders it for {/* c */} <i />. And

<p>
  a
  // c
  b
</p>

rendered a b; now it renders ab, as TSX does around {// c}. Of the 107 elements with comment children in the repository's .tsrx files and the tests' inputs, 18 render differently. None is in a real file, only tests and the runtime fixtures that pinned #615's rule.

2. // is read like JavaScript's. It starts a line comment where whitespace comes right before it, at the start of a line, or right after a tag, an expression container (}), or a block. The comment runs to the end of the line, a closing tag on it included. A // that touches other text stays text.

Source Now Before
https://x.dev, a//b, a/// b, /* a */// b text text
a // c then a line break comment text
{x}// c, <b />// c, // c at the start of a line comment comment
<p> a //comment </p> comment to the end of the line, then an unclosed-tag error for <p> text

In the repository and 1,934 test inputs, only 15 test lines had text with // after whitespace; none in a real file. For text, write &#47;/.

It also fixes #668. Inside a {…} container, aa // c was recorded as a comment and also kept in the text, so @tsrx/prettier-plugin printed it twice and lost the next word. Now // c is a comment in and out of containers, left out of the text, and both formatters print aa // c and cc on their own lines, stably.

Closes #668. Closes #850.

Why

The goal of tsrx-prettier-plugin is to let Prettier do the work. Under #615's rule it couldn't: Prettier only knows TSX, where {/* c */} splits the text, so the plugin kept its own whitespace model for comments (describeCommentRuns, printCommentChild), and every comment bug (#639, #849, #850, #596's Bugbot review) came from that model. With rule 1, a comment between children is exactly {/* c */} without the braces: the plugin gives Prettier that {…} child, and Prettier's layout keeps what renders by construction. Rule 2 makes the only other difference small: a bare // needs two layout rules (below), and text never has a word that starts with //, so #849's workaround goes.

What users see

Formatting (both formatters). A comment between children is laid out where Prettier lays out {/* c */}, printed as written (no braces):

const b = <p><a />/* c */<b /></p>;

formats to

const b = (
  <p>
    <a />
    /* c */
    <b />
  </p>
);

as Prettier formats <p><a />{/* c */}<b /></p>. Two rules come from // being bare:

  • A line comment ends its line: <p>// c + line break + a</p> prints // c and a on separate lines. Without the rule, a joins the comment.
  • A line comment after text keeps whitespace before it, since touching the text it would be text: a + line break + // c prints on two lines. Without the rule, it prints a// c.

Text right after a block comment that starts with // (/* x */// two) stays on the comment's line, where it's text.

TypeScript directives between children now work in the editor. Before, a // @ts-expect-error before a child was dropped from the TypeScript the editor checks. Now the editor's TSX has it right before the child, as TSX writes it:

<div>
  // @ts-expect-error
  <Missing a={1} />
</div>

becomes {/* @ts-expect-error */} on the line before <Missing a={1} />. Block /* @ts-ignore */ works too. Build output is unchanged: a comment is {} there. File-level pragmas (@jsxImportSource, @ts-nocheck, /// <reference>) are unaffected.

// prettier-ignore before a child keeps that child as written in both formatters. tsrx-prettier-plugin didn't support it before.

Changes

@tsrx/core (minor; it ships in 0.5.0, decision 47 in #852):

  • The parse tree gives text with comments the children TSX has for {/* … */}, so every target's JSX compiler renders rule 1, and every tool that reads the tree sees it without running analyzeTsrx:

    <p>a /* c */ b</p>
    
    children of <p>
    before JSXText "a b" (the comment cut out and attached to it)
    now JSXText "a ", {}, JSXText " b"

    The {} is a JSXExpressionContainer with a JSXEmptyExpression, spanning the comments between the two pieces (there are no braces in the source), and the comments are its innerComments, as in TSX. Each piece is exactly its source. A piece that is whitespace with a line break is left out, as such text is anywhere, so a comment with nothing rendering on one side gets no {}. The exception is a tooling comment (// @ts-expect-error): it gets a {} of its own, and the text around it keeps its line breaks, so the editor's TypeScript reads it on the line before the child.

  • plugin.js: #isTemplateLineCommentStart reads rule 2. The tokenizer's own check in jsx_readToken, which read any // as a comment, uses it too. #parseTemplateRawText adds the text to the children itself (#addTemplateText), with no extra walk.

  • parse/index.js (comment attachment): a comment in a {} dangles on its JSXEmptyExpression, even on the line of the child before. isCommentInText and its helpers are removed: no text node contains a comment any more (checked over the 1,815 text nodes of the repository's .tsrx files).

  • transform/jsx/helpers.js: the editor's print writes a tooling comment of a JSXEmptyExpression in block form inside the braces. comment-utils.js adds is_jsx_child_tooling_comment, which also accepts /* @ts-ignore */ and /* @ts-expect-error */.

  • The analyzer is unchanged.

tsrx-prettier-plugin (private): 208 fewer lines (2,385 → 2,177). The adapter gives each comment as the {…} child {/* c */} is, rebuilt from the source with the text (the parser's brace-less {} is skipped, since the parser leaves out whitespace that Prettier reads). describeCommentRuns, printCommentChild and the TSRXJSXComment node are gone, and jsx.js is Prettier's copy plus the two line-comment rules and the one for text after a block comment.

@tsrx/prettier-plugin (patch): 199 fewer lines. prepareComments drops the parser's brace-less {} children and takes the comments between an element's children off the nodes the parser attached them to (detachJSXChildComments), and printJSXElementBody builds the children from the source, with each comment as a child printed as written, and the same rules. pushJSXTextWithComments, getJSXChildCommentItems and the comment-side map are gone. Its output for comments between children now matches tsrx-prettier-plugin's. All 26 changed expectations are fixpoints of the new plugin.

Docs: the specification (4.3) and the features page state both rules. tsrx-prettier-plugin's README is updated.

Verification

  • Tests:
    • 11,042 passed (77 skipped) across core's parser and analyzer, the five compilers (React, Preact, Solid, Vue, Hono) and their runtimes (the fix(core): start the text after a closing tag at the tag, and read a non-breaking space as text (#442, #444, #540) #615 fixtures now render like TSX's {/* c */} on every target), both formatters, the ESLint parser and rules, the TypeScript plugin, the language server and tsrx-mcp.
    • New tests: the children shape in the parser (pieces, {}, positions, innerComments, the tooling-comment layout, the same in every parse mode, and the analyzer leaving it as is), the // rule in the parser, {} in compiled output, directives in the editor's TSX on every target, a tooling comment between children rendering on every target, prettier-ignore, and the // rule in both formatters.
  • Render check under rule 1 (run before the split moved from the analyzer to the parser; the formatters read the same source either way and their tests are unchanged), judged from core's own tree and comment list, independent of either formatter. It covers every sequence of up to three of text, an element, an expression, {" "}, a block comment, a line comment and {/* c */ " "}, plus four-part sets with two comments, with a {" "}, and with a {/* c */ " "}, at widths 80 and 12, about 440,000 formats per formatter. 0 render changes and 0 errors in both formatters.
    • 261 of those formats change on a second format, the same in both formatters. With a comment of the same width in TSX, Prettier's typescript parser is unstable on 233 of them too. The other 28 are all {x}, a line break, a one-letter word, and a comment, with another comment before them, at width 12. Prettier glues the word to {x} ({x}a) past the width in TSX too; its second format keeps that, and ours breaks {x} open, because a bare // c takes one line where {// c} takes three.
    • Before the rework, the old formatter changed rendering in 3,251 of the 3-part cases under rule 1.
  • The repository's .tsrx files format without error and don't change on a second format. pnpm format:check, pnpm changeset:check, and the typechecks of @tsrx/core, both formatters, the language server, the TypeScript plugin and the ESLint parser pass.

Not done here

🤖 Generated with Claude Code

leonidaz and others added 3 commits September 27, 2026 01:20
…ildren as {/* … */}

Prototype, not for merging. A comment between JSX children becomes the `{…}`
child that `{/* … */}` is in TSX (`JSXExpressionContainer` with a
`JSXEmptyExpression`), so Prettier lays it out; the printer prints the comment
without the braces. The plugin's own whitespace model for comments
(`describeCommentRuns`, `printCommentChild`) is gone, and `jsx.js` is back to
Prettier's code plus two TSRX rules: a line comment ends its line and starts
one after text or a comment, and `//` text never starts a line.

This assumes a bare comment renders like `{/* … */}` (the text on each side
follows JSX's whitespace rules on its own), which core doesn't do yet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… comments between children like {/* … */}

Core:
- `//` in template text starts a comment when whitespace comes right before it,
  at the start of a line, or right after a tag, `}`, or a block; it runs to the
  end of the line. Touching other text (`https://…`, `a//b`), it's text. The
  tokenizer (`jsx_readToken`) follows the same rule.
- The parser records the text between the comments in a text run
  (`metadata.text_pieces`), and keeps a run where any piece renders.
  `analyzeTsrx` splits it into those pieces with `{}` between them, so every
  target renders a comment like `{/* … */}` in TSX.

tsrx-prettier-plugin: rule 3 (`//` text never starts a line) is gone, since text
can't have a word that starts with `//`; what's left is text right after a
block comment. Rule 2 keeps a space before a line comment after text.

Tests follow the new rules: parser, shared compile and source mappings, the
runtime whitespace fixtures, and this plugin's `//` tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…and the old formatter on the {/* … */} rule

Core:
- A text run keeps a comment that starts with `@` even where its text renders
  nothing, and `analyzeTsrx` puts a tooling comment (`// @ts-expect-error`,
  `/* @ts-ignore */`) into the `{}` it leaves for a comment, with the text's
  line breaks kept, so the editor's TypeScript reads
  `{/* @ts-expect-error */}` on the line before the child, as in TSX.
  `is_jsx_child_tooling_comment` also accepts block `@ts-` directives there.

@tsrx/prettier-plugin: a comment between children is a child of its own, taken
off the node the parser attached it to, and printed as written where Prettier
lays out `{/* … */}`, with the same line-comment rules as tsrx-prettier-plugin.
The text around it prints as written. `pushJSXTextWithComments`,
`getJSXChildCommentItems` and the comment-side map are gone. A
`// prettier-ignore` keeps the child after it, or the child before it on its
line, as written.

tsrx-prettier-plugin: a `prettier-ignore` comment between children keeps the
next child as written; a line break after a line comment's merges with the
next, as after `{// …}`.

Tests, the specification, the features page, and this plugin's README follow
the rules.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz leonidaz changed the title proto(tsrx-prettier-plugin): let Prettier lay out comments between children as {/* … */} (don't merge) feat(core, prettier plugins): read // like JavaScript and render comments between children like {/* … */} Sep 27, 2026
@leonidaz
leonidaz marked this pull request as ready for review September 27, 2026 15:20

@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: JSDoc directives dropped between children
    • The parser now treats a JSDoc comment body whose line starts with optional stars before @ as a keep-alive directive, so /** @ts-expect-error */ is kept and placed in {} for the editor.

Create PR

Or push these changes by commenting:

@cursor push 73189456b8
Preview (73189456b8)
diff --git a/.changeset/core-jsdoc-child-directives.md b/.changeset/core-jsdoc-child-directives.md
new file mode 100644
--- /dev/null
+++ b/.changeset/core-jsdoc-child-directives.md
@@ -1,0 +1,5 @@
+---
+'@tsrx/core': patch
+---
+
+Keep a JSDoc `@ts-expect-error` or `@ts-ignore` between template children for the editor. `/** @ts-expect-error */` and a directive on a later line of the comment were dropped, so the editor's TypeScript never saw them, while `// @ts-expect-error` and `/* @ts-expect-error */` were kept.

diff --git a/packages/tsrx/src/plugin.js b/packages/tsrx/src/plugin.js
--- a/packages/tsrx/src/plugin.js
+++ b/packages/tsrx/src/plugin.js
@@ -760,6 +760,19 @@
 const regex_script_end_tag_start = /<\/script/giu;
 
 /**
+ * A comment body (the text after `//` or `/*`) that may be a tooling
+ * directive. The editor keeps it where the text around it renders nothing.
+ * The directive is `@` at the start of a line (`// @ts-expect-error`,
+ * `/* @ts-expect-error *\/`). A JSDoc comment's body starts with `*`
+ * (`/** @ts-expect-error *\/` is `* @ts-expect-error`), and the `@` may sit
+ * on a later line (`/**\n * @ts-expect-error\n *\/`).
+ * @param {string} body
+ */
+function comment_body_has_directive(body) {
+	return /(?:^|[\n\r])\s*\**\s*@/.test(body);
+}
+
+/**
  * HTML's whitespace: tab, line feed, form feed, carriage return, and space.
  * @param {number} code
  */
@@ -1684,9 +1697,9 @@
 				/** @type {Array<[number, number]>} */
 				const pieces = [];
 				let piece_start = start;
-				// A comment that may be a tooling directive (`// @ts-expect-error`),
-				// which the editor's TypeScript keeps even where the text renders
-				// nothing.
+				// A comment that may be a tooling directive (`// @ts-expect-error`,
+				// including the JSDoc spelling `/** @ts-expect-error */`), which the
+				// editor's TypeScript keeps even where the text renders nothing.
 				let has_directive = false;
 				while (index < this.input.length) {
 					if (this.#isTemplateLineCommentStart(index, start)) {
@@ -1704,7 +1717,9 @@
 						if (comment_start >= token_end) {
 							this.#emitTemplateLineComment(comment_start, index, null);
 						}
-						if (/^\s*@/.test(this.input.slice(comment_start + 2, index))) has_directive = true;
+						if (comment_body_has_directive(this.input.slice(comment_start + 2, index))) {
+							has_directive = true;
+						}
 						piece_start = index;
 						continue;
 					}
@@ -1727,7 +1742,9 @@
 								null,
 							);
 						}
-						if (/^\s*@/.test(this.input.slice(comment_start + 2, value_end))) has_directive = true;
+						if (comment_body_has_directive(this.input.slice(comment_start + 2, value_end))) {
+							has_directive = true;
+						}
 						piece_start = index;
 						continue;
 					}

diff --git a/packages/tsrx/tests/shared/source-mappings.js b/packages/tsrx/tests/shared/source-mappings.js
--- a/packages/tsrx/tests/shared/source-mappings.js
+++ b/packages/tsrx/tests/shared/source-mappings.js
@@ -361,6 +361,32 @@
 			expect(result.code).toContain('plain {} more');
 			expect(result.code).not.toContain('not a directive');
 		});
+
+		// `/** @ts-expect-error */` is the same directive. Its body starts with
+		// `*`, and the `@` may be on a later line.
+		it('keeps a JSDoc directive on the line before the child', () => {
+			const source = `export function App() @{
+	<div>
+		/** @ts-expect-error */
+		<JsDoc a={1} />
+		/**
+		 * @ts-expect-error
+		 */
+		<Multi a={1} />
+		/**@ts-ignore*/
+		<Tight a={1} />
+		/** note */
+		<Plain />
+	</div>
+}`;
+			const result = compile_to_volar_mappings(source, 'App.tsrx', { loose: true });
+			expect(result.errors).toEqual([]);
+			expect(result.code).toMatch(/\{\/\*\* {1,2}@ts-expect-error \*\/\}\s*<JsDoc a=\{1\} \/>/);
+			expect(result.code).toMatch(/@ts-expect-error \*\/\}\s*<Multi a=\{1\} \/>/);
+			expect(result.code).toMatch(/\{\/\*\* @ts-ignore \*\/\}\s*<Tight a=\{1\} \/>/);
+			expect(result.code).not.toContain('note');
+			expect(result.code).toContain('<Plain />');
+		});
 	});
 
 	describe(`[${name}] whitespace or a comment after an element's \`<\``, () => {

diff --git a/packages/tsrx/types/index.d.ts b/packages/tsrx/types/index.d.ts
--- a/packages/tsrx/types/index.d.ts
+++ b/packages/tsrx/types/index.d.ts
@@ -127,7 +127,10 @@
 	 * TSX, so each piece follows JSX's whitespace rules on its own;
 	 * `analyzeTsrx` splits the text into these pieces.
 	 */
-	/** A template `JSXText` with a comment in it that starts with `@`. */
+	/**
+	 * A template `JSXText` with a comment in it that is a tooling directive
+	 * (`@` at the start of a line, including after a JSDoc `*`).
+	 */
 	text_directive?: boolean;
 	/**
 	 * The empty `JSXEmptyExpression` that `analyzeTsrx` puts where a comment was

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 95681a5. Configure here.

Comment thread packages/tsrx/src/plugin.js Resolved
…or the editor

`/** @ts-expect-error */` between children was dropped: its comment value
starts with `*`, which neither the parser's check for a directive (`@`) nor
`is_jsx_child_tooling_comment` allowed. Both allow leading `*` now, and the
editor's print writes a block directive as written.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The parser now gives text with comments the children TSX has for
`{/* … */}`: the pieces of text, and an empty `{}` between two pieces
that render, holding the comments as the JSXEmptyExpression's
innerComments. Tools that read the parse tree (formatters, ESLint,
Ripple, Octane) no longer need analyzeTsrx to see it.

Removes what this made dead: the analyzer's split, the text_pieces,
text_directive and tooling_comment metadata, the joined text value, and
the comment attachment's in-text handling (isCommentInText and helpers),
since no text node contains a comment any more. Both formatters skip
the parser's brace-less `{}` and read comments from the source as before.

Adds parser tests that lock in the children shape, positions, comment
attachment and the tooling-comment layout, and runtime cases for a
tooling comment between children on all targets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit bf793b1 into main Sep 27, 2026
8 checks passed
@leonidaz
leonidaz deleted the proto/comment-containers branch September 27, 2026 17:12
thejackshelton added a commit to tsrx-org/yuku that referenced this pull request Sep 27, 2026
@tsrx/core main (tsrx-org/tsrx#858, #869) gives an element's and a
fragment's children exactly the shape TSX's parsers give them. In a
.tsrx source (and through parseModule for every filename):

- every comment between children is its own JSXExpressionContainer with
  a JSXEmptyExpression, both spanning exactly the comment, and the
  comment joins the file's comment list;
- all text is kept, value and raw exactly its source, so text is no
  longer stripped of comments;
- `//` starts a comment only after whitespace or at the start of a text
  run, so `a//b`, `https://x` and `/* a */// b` stay text;
- a comment that runs over the closing tag leaves the element unclosed,
  reported as core's `tsrx-unclosed-tag`.

The dialect finds the comments in the source while it owns the children,
so the seam's jsx_text_skip hook and extension_flags are gone (seam
09012bd9), along with the comment stripping in text.zig. The `tsrx`
option now travels on the dialect's own options. Owned children also
keep the comments and advisory diagnostics of nested elements and
containers, which a rewind dropped before.

The script body scan is folded into one pass over bodyEnd.
thejackshelton added a commit to tsrx-org/yuku that referenced this pull request Sep 27, 2026
@tsrx/core main (tsrx-org/tsrx#858, #869) gives an element's and a
fragment's children exactly the shape TSX's parsers give them. In a
.tsrx source (and through parseModule for every filename):

- every comment between children is its own JSXExpressionContainer with
  a JSXEmptyExpression, both spanning exactly the comment, and the
  comment joins the file's comment list;
- all text is kept, value and raw exactly its source, so text is no
  longer stripped of comments;
- `//` starts a comment only after whitespace or at the start of a text
  run, so `a//b`, `https://x` and `/* a */// b` stay text;
- a comment that runs over the closing tag leaves the element unclosed,
  reported as core's `tsrx-unclosed-tag`.

The dialect finds the comments in the source while it owns the children,
so the seam's jsx_text_skip hook and extension_flags are gone (seam
09012bd9), along with the comment stripping in text.zig. The `tsrx`
option now travels on the dialect's own options. Owned children also
keep the comments and advisory diagnostics of nested elements and
containers, which a rewind dropped before.

The script body scan is folded into one pass over bodyEnd.
jonkwheeler added a commit that referenced this pull request Nov 12, 2026
…ctive (#1281)

* fix(core): read a // right after a block comment as text after a directive (#1278)

After a directive's block, and after a child that follows one, the
tokenizer reads the next token as code, so `skipSpace` skipped
`/* c */// c` as two comments, and `#readSkippedTemplateText` made every
`//` in the skipped span a comment. A `//` that touches a block comment is
text in element children (#858), as it was without the directive.

When the skipped span holds such a `//`, read the run again as text from
where the previous child ends, as without the directive. The parser drops
the line comments the tokenizer recorded there, and the second copy of the
comments the template records again, after the parse, so that speculative
parses, whose comments acorn-typescript buffers, end up with the same list.

* fix(core): read text after a directive's block before the token after it (#1278)

When `/* c */// text` comes right after a directive's `}`, read the text
from the `}` on, without reading the token after the block as code: that
token may not be one (`'s fine`, `1st prize`, `"open`), and the tokenizer
threw on it before `#readSkippedTemplateText` could read the span again.

Before a `}` (an unclosed element in a container), the span after a child
can't be read again, since the `}` popped a context when it was read. Add
its text lines as text there when they hold only text, and drop the
tokenizer's comment for them, which was kept before.

* fix(core): skip JavaScript whitespace before text after a directive's block (#1278)

The check for template text after a directive's `}` skipped only HTML
whitespace, so a U+2028 or a non-breaking space before `/* c */// c`
still sent the token after it to the tokenizer as code. Skip whitespace
and line terminators as the tokenizer does.

* fix(core): look past line comments and into comments for text after a directive

Two cases where a `//` that touches a block comment was still read as a comment, both found by Bugbot on this branch.

`#readsTemplateTextAfterBlock` skipped only whitespace and block comments after a directive's `}`, then stopped at the first `//`. A real line comment there, as in `}` then `// note` on its own line, ended the lookahead, so the tokenizer read the following `/* c */// c` line as code. When the line after it wasn't code (`1st prize`, `'s fine`), the parse threw. The lookahead now skips a line comment the template reads as one, to the end of its line, and goes on.

`#isPlainTemplateText`, which decides whether the line skipped before a container's `}` is added as text, counted a `<` or a brace inside a comment as a tag or a container. So `/* c */// c /* { */` there kept its `//` as a line comment. It now skips comments as `#parseTemplateRawText` does.

The directive-equivalence test gains both shapes, and a new test pins the second one's children and comments. Both fail without the fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant