Skip to content

fix(core): report a dynamic tag expression that isn't an identifier, a member access, or a string (#737) - #782

Merged
leonidaz merged 4 commits into
mainfrom
fix/core-dynamic-tag-rule
Sep 26, 2026
Merged

leonidaz merged 4 commits into
mainfrom
fix/core-dynamic-tag-rule

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #737

The rule

A dynamic tag expression (<{expr}>) must be one of three forms, as decided in #737:

  • an identifier: tag, Comp;
  • a member access, including chains: props.as, Content.value, this.tag, registry[name], items[0]. Each computed key is an identifier, a string or number literal, or a member access;
  • a string literal: 'section'.

Everything else is reported as tsrx-dynamic-tag-expression: conditionals, ||, ?? and &&; parentheses and type-only wrappers ((tag), tag as any, tag satisfies T, tag!); null, undefined, numbers, booleans, objects, arrays and regular expressions; optional member access; computed keys outside the list (registry[getName()], items[-1]); arrow functions, functions, classes, elements and fragments; calls, new, spreads, concatenation, template literals, assignments, sequences, !tag and await tag.

The message suggests computing the tag first:

A dynamic tag expression must be an identifier, a member access such as `props.as` or `registry[name]`, or a string literal. Compute anything else before the element: `const Tag = c ? Child : Fallback;`, then `<{Tag} />`.

The bug

The old check in plugin.js threw "Dynamic element names must be…" in every mode, so one call or concatenation in a tag lost the whole file's AST in collect mode, the mode the editor and both formatters use. It also let through the forms #737 lists: arrow functions, elements inside a conditional or ||, null as a branch, && and a?.b.

The fix

  • find_invalid_dynamic_tag_part in plugin.js returns the first part of the expression that isn't one of the three forms. It follows only the chain of a member access (the object and each computed key), so it never walks the whole expression.
  • jsx_parseOpeningElementAt checks the name once per element. The closing tag repeats the expression and isn't checked, so an element is reported once.
  • The error goes through #report_recoverable_error_range: thrown in a strict parse, recorded in collect and loose mode, where parsing goes on. It points at the disallowed part: the whole tag for c ? A : B, getName() in registry[getName()], (a) in (a).b. The range of a parenthesized part includes its parentheses when preserveParens is off (end_after_parentheses, from the metadata.paren_start the parser already records).
  • The check doesn't change the tree: every AST is the same as before wherever both versions parse.
  • <{...a}> and <{}> still throw in every mode, with the new message. They aren't an AssignmentExpression, so they don't fit the grammar at all, and a spread child or an empty expression in the name position would break the printers and transforms that read the name.
  • New exports: DIAGNOSTIC_CODES.DYNAMIC_TAG_EXPRESSION and TSRX_DYNAMIC_TAG_EXPRESSION_ERROR.
  • @tsrx/prettier-plugin parses strictly, like a normal compile, so it refuses a file with a disallowed tag, as it refuses every other error a compile reports. Its code is unchanged; its tests check that it rejects the tag with tsrx-dynamic-tag-expression, and it has no changeset entry. tsrx-prettier-plugin collects errors and formats such files: this error isn't one of its REJECTED_MISTAKES, since it drops no code.
  • The specification (4.3 and section 5), llms.txt, the features page, and the MCP docs (regenerated) list the three forms and show the const Tag = … pattern.

No upstream overrides.

Decisions to check

  • this starts a chain. <{this.tag} /> and <{this.#Tag} /> are allowed; a bare <{this} /> isn't. The rule lists chains without naming their start. this behaves like an identifier here, and a source-mapping test for private names already uses <{this.#Tag} />. super.x and import.meta.x are reported.
  • A template literal without substitutions (<{`div`} />) was accepted before and is reported now, since the rule reports template literals. A parser test and an old-formatter test used one; they now use allowed forms.

Verification

Every input parsed before and after the change, strict and with { collect: true, errors: [], comments: [], preserveParens: true } and { loose: true }. That's 19,750 inputs: 70 tracked .tsrx files, 2,040 Prettier snapshot inputs and 2,040 outputs, and 15,600 distinct string literals from test files. 24 changed, and every change is intended. No AST changed where both versions parse.

  • 8 inputs that parsed before now throw in a strict parse and record the error when collecting: <{`div`} />, <{`article`} />, and six tests with an element in a conditional tag (<{c ? <b>/* c */</b> : "div"} />, <{c ? <b>&#123;x&#125; …</b> : 'i'} />).
  • 15 inputs that threw in every mode now record the error when collecting: the old rejection tests (<{null} />, <{tagName()} />, <{'hello' + 'by'} />, <{`d${kind}`} />, …).
  • 1 input only changes its message: <{...a} />.

Existing tests whose inputs are now errors:

New tests:

  • Parser: every allowed form, strict and collect; 49 reported forms, each in strict, collect, collect with preserveParens and loose mode, checking the message and range; a file with three reported tags that still parses to the end in collect and loose mode, one error per element; a tag nested in a tag; and a spread or empty container, which still throw.
  • Every target (React, Preact, Solid, Vue, Hono): a normal compile throws, collect records the error and still produces code that parses as TSX, and the editor's virtual code keeps and maps the expression.
  • Both formatters: a file whose tags are only reported is formatted.

Local runs (--maxWorkers=4): tsrx-utils (1,314), the five target projects and tsrx-analyze (3,398), prettier-plugin and tsrx-prettier-plugin (4,955), tsrx-mcp (83) and website (47) all pass. The Prettier known-failures list didn't change. pnpm typecheck passes; tsrx-tsc wasn't linked after --ignore-scripts, so its one step (vite-plugin-hono tests) ran through packages/typescript-plugin/dist/tsc.js. pnpm format left no diff.

Downstream

Checked against Ripple and Octane at origin/main, with @tsrx/core overridden to this branch in scratch worktrees, and compared with a run on main's core:

Both issues start with a box saying to wait for the @tsrx/core release that includes this PR.

🤖 Generated with Claude Code


Note

Medium Risk
This is a breaking language change for some previously accepted dynamic tags (e.g. conditionals and template literals in tags), and it touches core parsing used by the editor and formatters; behavior is well-tested but downstream packages may need updates.

Overview
Tightens dynamic tag rules (<{expr}>) so only an identifier, member access (with allowed computed keys), or a string literal is valid; everything else (conditionals, calls, templates, casts, etc.) is tsrx-dynamic-tag-expression, with guidance to hoist via const Tag = ….

The parser replaces the old deep walk with find_invalid_dynamic_tag_part and reports on the opening tag only through recoverable errors: strict compile throws, collect / loose record once per element and keep the AST—fixing cases where invalid tags used to abort the whole file in editor/format paths.

@tsrx/prettier-plugin adds parseTsrx: retry with collect when the sole failure is this diagnostic so Prettier can still format. Spec, site docs, MCP generated docs, and a changeset document the rule; tests cover parser ranges, all compile targets, and formatters.

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

…a member access, or a string (#737)

A dynamic tag expression (`<{expr}>`) must be an identifier, a member access
(chains from an identifier or `this`, computed keys that are an identifier, a
string or number literal, or a member access), or a string literal. Any other
expression parses and is reported as `tsrx-dynamic-tag-expression` at the part
that isn't allowed, once per element: thrown in a strict parse, recorded in
`collect` and `loose` mode. This replaces the old check, which threw in every
mode and let conditionals, `&&`, arrow functions and elements through.

@tsrx/prettier-plugin formats a file whose only error is a reported tag. The
specification, llms.txt, the features page and the MCP docs list the three
forms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz and others added 2 commits September 26, 2026 14:52
…e any compile error

The old plugin parses strictly and refuses every other error a normal
compile reports, so it no longer parses a second time in collect mode
for a disallowed dynamic tag. Its code is back to main's, its tests
check that it rejects the tag with core's error, and its changeset entry
is dropped. tsrx-prettier-plugin still formats such files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit de88f59 into main Sep 26, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-dynamic-tag-rule branch September 26, 2026 20:00
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