Skip to content

fix(core): start an element at its <, and continue an element statement across a line break (#467, #496) - #500

Merged
domgan-oai merged 3 commits into
mainfrom
fix/parser-element-start-continuation-467
Sep 25, 2026
Merged

domgan-oai merged 3 commits into
mainfrom
fix/parser-element-start-continuation-467

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #467
Closes #496

Summary

Two parser fixes in @tsrx/core, plus the formatter change the first one needs.

Input Before Now
const a = < div>x</div>; element starts at 11 (the space) starts at 10 (the <)
const a = < >x</>; fragment starts at 12 starts at 10
const a = < + // c + > on the next lines starts at 18, { line: 3, column: -1 } starts at 10, { line: 1, column: 10 }
<div /> then > 5;, ? a : b;, && b;, === x; on the next line Unexpected token (2:0) one ExpressionStatement (BinaryExpression, ConditionalExpression, LogicalExpression)
<div /> then + 1; on the next line two statements, <div /> and +1 one BinaryExpression

TypeScript 5.9.3's TSX parser gives the same statement structure for every #496 source, with no syntax diagnostics.

Changes

Boundaries kept

  • Templates don't change. A @{ … } render node, a directive body's render node, an @case body, and element children still don't go through parseStatement: @{\n <div />\n > 5\n}, the same in @if and @case bodies, still fail with Unexpected token; @{\n const a = 1\n <div />\n + 1\n} still reports "statements cannot follow the rendered output" with the same loose AST; and <div>\n <span />\n > 5\n</div> still has > 5 as text. A plain block inside a setup statement (@{ if (x) { <div />\n > 5 } <span /> }) continues, like any function body.
  • A tag start is the next element. <div /> then <span /> on the next line is still two elements (TypeScript reports "JSX expressions must have one parent element").
  • Subscripts, prefix operators, and assignment don't change. (, [, a template literal, !, and ++ on the next line start the next statement, as in TypeScript, and = is still rejected.

Behavior change to note

As in TSX (and in JavaScript after any expression), a / on the line after an element statement now divides. <div /> followed by /re/.test(x); on the next line used to be two statements; it's now (<div />) / re / .test(x), which fails with Unexpected token (2:4). TypeScript reports an error there too. The changeset says a ; after the element is needed.

Verification

Related issues filed

🤖 Generated with Claude Code


Note

Medium Risk
Changes core JSX parse boundaries and node spans used for AST and source maps; behavior is intentionally aligned with TSX but can shift how some multi-line snippets parse (e.g. / on the next line now divides).

Overview
Aligns @tsrx/core parsing and @tsrx/prettier-plugin output with TypeScript/Prettier for two JSX edge cases.

Element start positions (#467): Elements and fragments with whitespace or comments after < now begin at the < (via lastTokStart / startNodeAt), not in the gap before the tag name—fixing negative columns and wrong editor mappings. Comments in that gap attach like Prettier: leading on the tag name, or inner on JSXOpeningFragment. The formatter prints those comments inside the opening tag/fragment instead of before the node.

Expression continuation (#496): When a JSX element starts a statement, binary/logical/relational operators, **, ?, and , on the next line now continue the same expression (e.g. <div />\n+ 1;). Same-line-only rules remain for as/satisfies; a < starting the next line is still a new tag. Template/@{…} render rules and in-template text behavior are unchanged.

Adds changeset, parser/mapping tests, and formatter snapshot-style tests for these shapes.

Reviewed by Cursor Bugbot for commit d0d2352. Bugbot is set up for automated code reviews on this repo. Configure here.

…ment across a line break (#467, #496)

- An element or fragment with whitespace or a comment after its `<`
  (`< div>`, `<  >`, `<` then a comment on the next line) now starts at the
  `<`, and so does its opening tag, from the `<` token's own position. It used
  to start one character before the tag name, inside the gap, with a negative
  column when the gap crossed a line. Like Prettier, a comment between a
  fragment's `<` and `>` now dangles on the opening fragment, and the formatter
  prints it, and a comment before a tag name, there.
- An operator on the line after an element that starts a statement continues
  the expression, as in TSX: binary, logical, and relational operators, `**`,
  `?`, and `,`. `as` and `satisfies` still continue only on the element's
  line, a `<` that starts the next line is still the next element, and
  template render nodes and children don't change.

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: Line comments break opening tags
    • A line comment leading a tag name now breaks onto its own indented line after <, so the opening tag stays valid instead of printing <// note.

Create PR

Or push these changes by commenting:

@cursor push b67ab6e377
Preview (b67ab6e377)
diff --git a/.changeset/prettier-opening-tag-line-comments.md b/.changeset/prettier-opening-tag-line-comments.md
new file mode 100644
--- /dev/null
+++ b/.changeset/prettier-opening-tag-line-comments.md
@@ -1,0 +1,5 @@
+---
+'@tsrx/prettier-plugin': patch
+---
+
+A line comment between `<` and a tag name stays on its own line inside the opening tag. It used to print against the `<` (`<// note`), which comments out the rest of that line.

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
@@ -12494,14 +12494,20 @@
 		typeArgsDoc = path.call(print, 'openingElement', 'typeArguments');
 	}
 	// A comment before the tag name, as in `</* note */ div>`, prints before
-	// it, and one after it, as in `<div // note`, prints after it
+	// it, and one after it, as in `<div // note`, prints after it.
+	// A line comment must not sit against `<` (`<// note` comments out the rest
+	// of that line). Break after `<`, as `printJSXOpeningFragment` does.
 	const nameNode = /** @type {AST.Node & AST.NodeWithMaybeComments} */ (openingElement.name);
-	const openingTagName = finishTsrxNode(
+	const nameLeadingComments = nameNode.leadingComments ?? [];
+	let openingTagName = finishTsrxNode(
 		nameNode,
-		printLeadingComments(nameNode, nameNode.leadingComments ?? [], options),
+		printLeadingComments(nameNode, nameLeadingComments, options),
 		tagName,
 		options,
 	);
+	if (nameLeadingComments[0]?.type === 'Line') {
+		openingTagName = indent([hardline, openingTagName]);
+	}
 	const nameHasComments =
 		hasComment(nameNode) ||
 		Boolean(

diff --git a/packages/prettier-plugin/src/index.test.js b/packages/prettier-plugin/src/index.test.js
--- a/packages/prettier-plugin/src/index.test.js
+++ b/packages/prettier-plugin/src/index.test.js
@@ -14784,6 +14784,34 @@
 			expect(await format(source)).toBeWithNewline(expected);
 		});
 
+		// A line comment glued to `<` (`<// note`) comments out the rest of that
+		// line. Keep it on its own line inside the tag, as a fragment does.
+		it.each([
+			[
+				'const a = <\n  // note\n  div></div>;',
+				'const a = (\n  <\n    // note\n    div\n  ></div>\n);',
+			],
+			['const a = <\n  // note\n  div />;', 'const a = (\n  <\n    // note\n    div\n  />\n);'],
+			[
+				'const a = <\n  // note\n  div a="1"></div>;',
+				'const a = (\n  <\n    // note\n    div\n    a="1"\n  ></div>\n);',
+			],
+			[
+				'function App() {\n  return (\n    <\n      // note\n      div\n    >\n      test\n    </div>\n  );\n}',
+				'function App() {\n  return (\n    <\n      // note\n      div\n    >\n      test\n    </div>\n  );\n}',
+			],
+			[
+				'export function App() @{\n  <\n    // note\n    div\n  >\n    x\n  </div>\n}',
+				'export function App() @{\n  <\n    // note\n    div\n  >\n    x\n  </div>\n}',
+			],
+			[
+				'const a = <\n  // note\n  div<T> />;',
+				'const a = (\n  <\n    // note\n    div<T>\n  />\n);',
+			],
+		])('keeps a line comment after the `<` of %j inside the tag', async (source, expected) => {
+			expect(await format(source)).toBeWithNewline(expected);
+		});
+
 		it('joins an operator on the next line to the element it continues, like Prettier', async () => {
 			const source = '<div />\n+ 1;\n\nfunction f() {\n  <div />\n  > 5;\n}';
 			expect(await format(source)).toBeWithNewline(

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

Reviewed by Cursor Bugbot for commit d0d2352. Configure here.

Comment thread packages/prettier-plugin/src/index.js Resolved
…er the `<`

A line comment between an opening tag's `<` and its name printed right
after the `<`, and `<//` reads as a closing tag in TSX. That comment, and a
block comment on a line of its own (which joined the `<`'s line on the next
format), now start on the line after the `<`, indented with the name and
the attributes, as Prettier prints the comments of closing tags and
fragments.

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

Labels

None yet

Projects

None yet

2 participants