Skip to content

fix(core): read a <script> body as raw text and print it from content in every target (#708) - #790

Merged
leonidaz merged 2 commits into
mainfrom
fix/core-script-raw-text
Sep 26, 2026
Merged

leonidaz merged 2 commits into
mainfrom
fix/core-script-raw-text

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #708.

A <script> element is now a raw-text element, like <style>, in templates and in plain JSX, as #708 decided on 2026-09-26. Its body is content, taken as written, and each core target prints it in the form that renders it exactly, on the client and in server HTML. Whether a script runs stays each target's decision.

The bug

The parser kept the body on content and also mirrored it as one JSXText child, which the targets printed as JSX text. A JSX compiler joins the lines of JSX text and decodes its character references, so this script:

export function App() @{
	<div>
		<script>
			// c
			run();
			if (a < b) x = "&amp;";
		</script>
	</div>
}

rendered // c run(); if (a < b) x = "&"; in React, all one comment, and // c run(); if (a &lt; b) x = "&amp;"; in Solid's and Vue's HTML templates. <script>{code}</script> printed back as an expression container, and static hoisting then threw ReferenceError: code is not defined. The body also ended only at an exact </script>: </script > was reported as unclosed, and </SCRIPT> stayed in the body, where a browser ends the script.

The fix

  • Parser (#parseScriptElement, #findScriptBodyEnd in packages/tsrx/src/plugin.js). The element has no children; content is its only body, "" for <script></script>. The body ends at </script, optional HTML whitespace, then >. Any other </script, in any letter case, is the new tsrx-script-end-tag-in-body error, collected in collect and loose mode:

    '</SCRIPT' can end a script in HTML, so a '<script>' body can't contain it. Write '<\/SCRIPT' instead.
    

    That includes </scripts> and </script-x>, which a browser doesn't treat as an end tag. The rule stays the one [Bug]: tsrx-core: A <script> body compiles to JSX text, so its line breaks collapse and its character references decode #708 states: every other </script.

  • Targets. A new JsxPlatform.jsx.scriptBody option picks the form, and createScriptBody builds it, for core's to_jsx_element and Solid's own:

    Target Output
    React <script>{"…"}</script>
    Preact, Hono (both modes) <script dangerouslySetInnerHTML={{ __html: "…" }} />
    Solid <script innerHTML={"…"} />
    Vue <script v-html={"…"} />

    Vue uses v-html, not the innerHTML in [Bug]: tsrx-core: A <script> body compiles to JSX text, so its line breaks collapse and its character references decode #708's table. [Bug]: tsrx-core: A <script> body compiles to JSX text, so its line breaks collapse and its character references decode #708's check used innerHTML={code}, a variable. With a string literal, vue-jsx-vapor writes the value into its HTML template as an attribute, <script innerHTML="…">, so the client's script is empty. v-html sets the same innerHTML when the element is created (_setHtml(el, "…")), and the server renders it as innerHTML. A dynamic body in Vue is still innerHTML={code}, which vue-jsx-vapor sets at runtime.

    A body of only whitespace outputs an empty script, as both formatters print it. The type-only editor output still leaves the body out; the embedded document from scriptMappings covers it.

  • JSX text printer. escape_jsx_text no longer writes { and } as &#123; and &#125;: only script bodies brought braces to it. fix(core): write a > in text as &gt;, read it as text in containers, and print JSX text from raw (#694, #693) #711's pending changeset said script braces compile to references, so I removed that sentence from .changeset/core-text-escaping.md.

  • @tsrx/prettier-plugin. embed() formats the body from content on the element and records the doc for printJSXElement, which prints it, or the body's lines as written. With embeddedLanguageFormatting: 'off' the body was printed as JSX text before, with its lines joined; it keeps its lines now. tsrx-prettier-plugin already printed from content and needed no change.

  • @tsrx/typescript-plugin. The fallback script extraction for a file that doesn't compile ends a body at </script, whitespace, >, as the parser does.

  • ESLint parser and plugin, language server, segments.js. Nothing read the child: scriptMappings and the language server use content. ESLint no longer visits the body as a JSXText node.

  • Docs. The specification has a new 4.6 Script Elements (Server Extensions is now 4.7), a 5.2 early error, a host-defined item in 6, and the AST appendix's content. llms.txt has a Scripts section with each target's dynamic-body property and whether a client render and server HTML run a script. The Hono README has a Scripts section. The React, Preact, Solid, and Vue compiler packages have no README, so their notes are only in llms.txt. The MCP docs' text section describes the rule and includes the grammar.

  • Changeset. minor for @tsrx/core, @tsrx/react, @tsrx/preact, @tsrx/hono, @tsrx/solid, and @tsrx/vue, since it changes what <script>{…}</script> means and what existing scripts render. patch for @tsrx/prettier-plugin, @tsrx/typescript-plugin, and @tsrx/mcp.

Overrides

None.

Verification

  • Parse comparison. Every tracked .tsrx file, the input and output of all 2,040 Prettier snapshot cases (readSnapshotEntries/readFormatCase), and 15,663 string literals from test files, 19,820 inputs from main (9c2f24f8) and this branch, parsed strict, with { collect: true, errors: [], comments: [], preserveParens: true }, and with { loose: true }, in 4 workers with a 15 s kill. No hangs. 55 inputs change, 46 of them on main:
    • 54 change only by the removed JSXText child, in all three modes: after dropping script children from main's AST, both ASTs and their errors are the same. All 46 on main are among them.
    • 1, this PR's </script\n\t> test, now parses; main reports Unclosed tag '<script>'.
  • Compiled output. The 97 inputs that contain <script (86 on main), compiled with each target on main and this branch. On main's inputs, 41 change for every target, each script body now a string; 15 are the same (self-closing scripts, empty bodies, bodies of only whitespace); 30 fail to compile on both for other reasons. Every output of both revisions parses with TypeScript's TSX parser and builds with esbuild.
  • In Chromium, through each target's Vite plugin with playwright, a body with a // c line, <, >, &amp;, and a block, a JSON body, and <script>{window.__ran = …, window.__amp = typeof code}</script > in a plain function. In every target the script's textContent is the body as written, on a client render and from server HTML. React, Solid, and Vue don't run it on a client render; Preact and hono/jsx/dom do; server HTML runs it in React, Preact, Hono, Solid, and Vue. typeof code reads "undefined": the component's code isn't inserted.
  • Tests. vitest run --maxWorkers=4 passes for tsrx-utils, tsrx-analyze, tsrx-react, tsrx-preact, tsrx-solid, tsrx-vue, tsrx-hono, the five runtime projects, vite-plugin-*, prettier-plugin, tsrx-prettier-plugin, tsrx-mcp, typescript-plugin, language-server, eslint-parser, and eslint-plugin. The Prettier known-failures list is unchanged. New tests:
    • parser, through parse_in_worker_with_ast: no children; comments, references, and braces as text, in a template, a plain function, and at the module level; </script >, </script\n\t>, and </script\t\f\r > close the body; </SCRIPT>, </Script >, </script/>, </script foo>, and </scripts> are reported, strict and collected; <\/script is text;
    • compile, for each target (runSharedScriptBodyTests): the form and exact string for a code body, plain JSX, a JSON body, an import map, {code}, a closing tag with a line break, and <\/script; a body of only whitespace; a self-closing script with the target's dynamic-body property;
    • runtime, shared fixture script-body-components.tsrx: each script's textContent in React, Preact, Solid, Vue, and hono/jsx/dom (jsdom);
    • server output (runScriptBodyServerTests), through a Vite SSR server: Hono's hono/jsx and Solid's generate: 'ssr'.
  • pnpm typecheck and pnpm format:check pass.

Downstream

Findings

🤖 Generated with Claude Code


Note

Medium Risk
Semantically changes how every inline <script> compiles and renders across all targets; incorrect migration could affect script execution and XSS-sensitive output, though the change aligns behavior with HTML and is heavily tested.

Overview
<script> bodies are now raw text (like <style>): the parser stores verbatim source on content and stops mirroring it as a JSXText child, so JSX text rules no longer join lines, decode entities, or treat {…} as expressions. Closing tags follow HTML rules (</script >, case-insensitive ends) with a new tsrx-script-end-tag-in-body diagnostic for stray </script sequences.

Each JSX target emits the body so it renders exactly via new JsxPlatform.jsx.scriptBody and createScriptBody: React uses a string child; Preact/Hono use dangerouslySetInnerHTML; Solid uses innerHTML; Vue uses v-html for static bodies. Whitespace-only bodies compile to empty scripts.

Tooling and docs move with the model: Prettier formats from content, the TypeScript plugin’s script fallback regex matches the parser, escape_jsx_text no longer escapes braces (scripts aren’t JSX text), plus spec/MCP/Hono README updates and shared compile/runtime/server tests.

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

…prints from `content` (#708)

A `<script>` body is raw text, like a `<style>` body, in templates and in
plain JSX: `content`, taken as written, with no comments, no character
references, and no `{…}` expressions. The parser no longer mirrors it as a
`JSXText` child, which compiled to JSX text: its lines were joined, so a
`// c` line commented out the rest of the script, `&amp;` rendered `&`, and
`<script>{code}</script>` became an expression container.

Each target prints the body in the form that renders it exactly, on the
client and in server HTML: React a string child, Preact and Hono
`dangerouslySetInnerHTML`, Solid `innerHTML`, and Vue `v-html`
(vue-jsx-vapor writes a string `innerHTML` into its template as an
attribute). Whether a script runs stays each target's decision.

A body ends where HTML ends it, at `</script`, optional whitespace, and
`>`. Any other `</script` in the body, in any letter case, is the new
`tsrx-script-end-tag-in-body` error, with a hint to write `<\/script`.

The JSX text printer no longer escapes braces, which only reached it from
script bodies. Both formatters, the TypeScript plugin's fallback script
extraction, the specification, llms.txt, the Hono README, and the MCP docs
follow the new rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit c70964d into main Sep 26, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-script-raw-text branch September 26, 2026 20:23
leonidaz added a commit that referenced this pull request Sep 30, 2026
Conflicts, resolved to this branch's design:
- packages/typescript-plugin/src/language.js: keep the removal of the
  `<script>` fallback extractor (071421a); main's end-tag tweak to it (#790)
  has no counterpart here.
- packages/typescript-plugin/tests/tsc.test.js: keep this branch's
  TypeScript 7 tests and add main's new tests after them.
- website-tsrx/src/pages/getting-started.tsrx: keep this branch's text about
  TypeScript 7, formatted by main's formatter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jonkwheeler added a commit that referenced this pull request Nov 12, 2026
…larations (#1104)

* fix(grammars): end a <script> body at </script >, as the compiler does (#792)

The compiler ends a `<script>` body at `</script`, optional HTML whitespace,
then `>` (#790). Both grammars ended it only at the exact `</script>`, so
`</script >` was read as script, and in the TextMate grammar everything after
it, the following elements included, was highlighted as TypeScript.

- tree-sitter: `scan_raw_text` skips HTML whitespace after `</script` and ends
  the body before the `<` when a `>` follows. A `<style>` body still ends only
  at `</style>`, which is what the compiler accepts there.
- TextMate: the script element ends at `(</)(script)\s*(>)` and its body at
  `(?=</script\s*(?:>|$))`. A TextMate rule matches one line at a time, so a
  closing tag with a line break before its `>` ends the element at `</script`
  on its line; the `>` on the next line is then JSX text and the code after it
  is highlighted normally.

The tokens of the repository's 78 .tsrx files are unchanged in both grammars.

* fix(grammars): parse and highlight using and await using declarations (#455)

The parser, compilers and formatter accept `using` and `await using`
declarations (TypeScript 5.2), but the tree-sitter grammar parsed them as
errors, and the TextMate grammar highlighted `using` in an `@for` head as a
plain identifier.

- tree-sitter: a `using_declaration` node, named as in tree-sitter-javascript,
  in statements, `@{ … }` scopes, fragment bodies, `for…of` heads and `@for`
  heads. It binds identifiers only, as TypeScript requires, so `using [i]`
  stays an index expression. `using` is a keyword where a declaration can
  start, so it is aliased to `identifier` in expressions and assignment
  targets: `using(x)`, `using.dispose()`, `using = x` and
  `for (using of items)` parse to the same trees as before. `using` is
  highlighted as `@keyword.storage`.
- TextMate: the directive header and the `for (…; index …)` head now take
  `using` and `await using`, with the lookahead TypeScript-TmLanguage uses, so
  `@for (using of items)` keeps `using` as the loop variable.

The tree-sitter parse trees and TextMate tokens of the repository's 78 .tsrx
files are unchanged.

* fix(tree-sitter): keep using a variable name in parameters, patterns, labels and for…in heads

`using` is a keyword wherever a `using` declaration can start, so it has to be
aliased to `identifier` everywhere else a variable name can go at those
positions. The previous commit covered expressions and assignment targets but
not the rest, so these parsed with errors where main had none:

- `using => 1;` and `(using) => using` (an arrow parameter)
- `[using] = arr;` (a pattern)
- `using: for (;;) { break using; }` (a label)
- `for (using in obj) {}` (a `for…in` head)

`_using_identifier` now sits next to `identifier` in `pattern`,
`required_parameter`, the arrow's single parameter, `labeled_statement` and the
`for…in` head, and these parse to the same trees as on main.

In an `@for` head a `using` declaration now takes only `of`, as in TypeScript
(the compiler reports TS1493 for `@for (using item in items)`); `using` as the
loop variable still takes `of` or `in`.

The parse trees of the repository's 78 .tsrx files are unchanged.

* fix(textmate): highlight using in a directive head only as a for…of declaration

The directive header pattern marked `using` as a declaration keyword before any
name, so `@for (using item in items)`, which the compiler rejects (TS1493), and
`@if (using instanceof Foo)` showed it as `storage.type`. It now needs to follow
the `(` and come before `<name> of`, the one place a `using` declaration can be
in a directive head.

* fix(zed): keep the Zed queries matched to the pinned grammar, and check their tokens

Zed builds the grammar at the `rev` pinned in `packages/zed-plugin/extension.toml`
and loads the extension's own queries against it. A query that names a node or a
token the grammar lacks fails to load, and Zed then highlights nothing. The
previous commit copied a `"using"` capture into Zed's `highlights.scm`, but the
grammar at the pin (aa13392) has no `using` token, so Zed built from main
(`pnpm zed:stage-dev`) would lose highlighting until Version Packages moved the
pin. `check-zed-grammar-rev.js` checked only named nodes, so it did not notice.

- `copy-tree-sitter-queries.js` updates Zed's copy only with `--zed`, and
  `sync-zed-grammar-rev.js`, which Version Packages runs when the extension
  version changes, moves the pin to HEAD and then copies the queries with
  `--zed`. Zed's queries and the grammar it builds now move together. Neovim
  builds the grammar from the default branch and still takes every change.
- `check-zed-grammar-rev.js` also checks quoted tokens against the anonymous
  node types at the pin, with a small query reader
  (`tree-sitter-query-nodes.js`) that skips comments and predicate arguments.
  It fails on the previous commit's Zed copy (`missing token "using"`) and
  passes with this one.
- Zed's `highlights.scm` is back to main's. `"using"` reaches it with the next
  Version Packages.

* chore(scripts): stop suggesting a manual Zed rev sync on query drift

Moving the rev on a pull request branch pins Zed to a commit that a
squash merge drops. Version Packages already moves the rev and copies
the queries together, so the message now points there only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jonkwheeler added a commit that referenced this pull request Nov 12, 2026
…eep raw script bodies (#1121)

* fix(core): leave static JSX in place when its tag reads an uninitialized binding

Static JSX is hoisted to a module-level const placed before the
top-level statement it came from, so its tag is read when the module
loads. A tag that reads the class the JSX is in (`<Screen.Tag>` in a
method of `class Screen`), a const or class declared later, or `this`
then threw on import: "Cannot access 'Screen' before initialization".

Hoisting now skips JSX whose tag reads `this` or a name that a
top-level statement binds at or after the one the JSX is in. Function
declarations and imports are initialized before the module body runs,
so tags that read them are still hoisted.

Fixes #204

* fix(core): keep raw script bodies in hoisted static JSX

Since #790 a raw `<script>` keeps its body in `content`, and the walk
lowers it when it reaches the element. An element in an expression
container or attribute value of static JSX is reached only in the
render output the walk returns, which a hoisted static has already
left. So `<div>{<script type="application/json">{"a":1}</script>}</div>`
and a script passed as a prop rendered an empty script on every target.

Hoisting now lowers those scripts with the platform's element
transform, as the walk would.

Fixes #845

* fix(core): hoist static JSX only when each tag resolves to a stable module binding

The #204 fix checked tags only against top-level declarations, by
position. It still hoisted `<super.Tag>` (a module that fails to
parse), `<Screen.Tag>` in `const C = class Screen { … }` (`Screen is
not defined`), tags that read a static block's or block's bindings, and
member tags such as `<Menu.Item>` whose property is assigned after the
module loads, along with tags that read a `let` or `var` assigned
later.

Each tag now resolves through the scopes it is written in, and static
JSX is hoisted only when every tag that reads a binding resolves to an
import, a function declaration, or a `const` or class declared in an
earlier statement. A member tag that starts with a function or class
declared in the module is read at render time, as is a tag that reads
`this`, `super`, a local binding, a `let`, `var`, enum, namespace or
import alias, or a name the module does not declare. Intrinsic tags
like `<div>` read no binding, so a later `const div` no longer stops
them from being hoisted.

Related: #204

* fix(core): keep static JSX in place in a component inside another function

A component's statics and helper components go to module level through
the top-level statement that holds it, and `expand_component_helpers`
finds them only through nodes that are not functions. A component
declared inside another function (`function outer() { function App() {
return <div>hi</div> } }`) still hoisted its JSX, so it returned
`App__static1`, which was never declared, and threw a ReferenceError
when it rendered.

Such a component now gets no helper state, so it lowers its statics
and helpers in place, as code outside a component does.

* fix(core): hoist a member tag only when it starts with an import

A member tag on a `const` declared earlier was still hoisted, so the
compound-component pattern `const Menu = …; <Menu.Item />` with
`Menu.Item = …` (or `Object.assign(Menu, { Item })`) after it read
`Menu.Item` when the module loaded and rendered an undefined type.
A member tag now stays at render time unless it starts with an import
(named, default or namespace). Plain tags keep the earlier rules.

Related: #204

* fix(core): keep the helper state of a component inside another function

Giving such a component no helper state, to keep its statics and
helper components in place, also moved its spread temporary to the
fallback name `_tsrx_spread_props_N`, which can shadow or redeclare a
user variable of that name. The component now gets a local helper
state again: it names things as it did on main and keeps its statics
and helper components in place. The spread temporary, in core and in
Solid, also skips any name the module source already uses.

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

Development

Successfully merging this pull request may close these issues.

[Bug]: tsrx-core: A <script> body compiles to JSX text, so its line breaks collapse and its character references decode

1 participant