Repository navigation
feat(core, prettier plugins): read // like JavaScript and render comments between children like {/* … */} - #858
Merged
Merged
Conversation
…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
marked this pull request as ready for review
September 27, 2026 15:20
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: 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.
- The parser now treats a JSDoc comment body whose line starts with optional stars before
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 wasYou can send follow-ups to the cloud agent here.
Reviewed by Cursor Bugbot for commit 95681a5. Configure here.
This was referenced Sep 27, 2026
…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]>
This was referenced Sep 27, 2026
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]>
This was referenced Sep 27, 2026
This was referenced Sep 27, 2026
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
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.


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/oxcneed 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:Before, nothing rendered between
<b>and<i>. Now there's a space, as TSX renders it for{/* c */} <i />. Andrendered
a b; now it rendersab, as TSX does around{// c}. Of the 107 elements with comment children in the repository's.tsrxfiles 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.https://x.dev,a//b,a/// b,/* a */// ba // cthen a line break{x}// c,<b />// c,// cat the start of a line<p> a //comment </p><p>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//.It also fixes #668. Inside a
{…}container,aa // cwas recorded as a comment and also kept in the text, so@tsrx/prettier-pluginprinted it twice and lost the next word. Now// cis a comment in and out of containers, left out of the text, and both formatters printaa // candccon their own lines, stably.Closes #668. Closes #850.
Why
The goal of
tsrx-prettier-pluginis 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):formats to
as Prettier formats
<p><a />{/* c */}<b /></p>. Two rules come from//being bare:<p>// c+ line break +a</p>prints// candaon separate lines. Without the rule,ajoins the comment.a+ line break +// cprints on two lines. Without the rule, it printsa// 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-errorbefore 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: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-ignorebefore a child keeps that child as written in both formatters.tsrx-prettier-plugindidn'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 runninganalyzeTsrx:<p>JSXText "a b"(the comment cut out and attached to it)JSXText "a ",{},JSXText " b"The
{}is aJSXExpressionContainerwith aJSXEmptyExpression, spanning the comments between the two pieces (there are no braces in the source), and the comments are itsinnerComments, 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:#isTemplateLineCommentStartreads rule 2. The tokenizer's own check injsx_readToken, which read any//as a comment, uses it too.#parseTemplateRawTextadds the text to the children itself (#addTemplateText), with no extra walk.parse/index.js(comment attachment): a comment in a{}dangles on itsJSXEmptyExpression, even on the line of the child before.isCommentInTextand its helpers are removed: no text node contains a comment any more (checked over the 1,815 text nodes of the repository's.tsrxfiles).transform/jsx/helpers.js: the editor's print writes a tooling comment of aJSXEmptyExpressionin block form inside the braces.comment-utils.jsaddsis_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,printCommentChildand theTSRXJSXCommentnode are gone, andjsx.jsis 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.prepareCommentsdrops the parser's brace-less{}children and takes the comments between an element's children off the nodes the parser attached them to (detachJSXChildComments), andprintJSXElementBodybuilds the children from the source, with each comment as a child printed as written, and the same rules.pushJSXTextWithComments,getJSXChildCommentItemsand the comment-side map are gone. Its output for comments between children now matchestsrx-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
{/* c */}on every target), both formatters, the ESLint parser and rules, the TypeScript plugin, the language server andtsrx-mcp.{}, 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.{" "}, 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.typescriptparser 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// ctakes one line where{// c}takes three..tsrxfiles 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
{}child as nothing and never join text across it.@tsrx/oxchas its own parser and needs rule 2 and the same children.//any more.//rule.<p> {count}, a line break,s /* shown in the header too */, atprintWidth: 40) formats to{count}s{" "}and the comment on the next line, stably, in both formatters: the comment moves to the next line as Prettier moves{/* c */}. The 28 leftover cases under "Verification" have a different cause.🤖 Generated with Claude Code