Skip to content

fix(language-server): list <script> body declarations as document symbols - #1277

Merged
jonkwheeler merged 2 commits into
tsrx-org:mainfrom
jonkwheeler:fix/script-body-document-symbols
Nov 12, 2026
Merged

jonkwheeler merged 2 commits into
tsrx-org:mainfrom
jonkwheeler:fix/script-body-document-symbols

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Declarations inside <script> bodies show up again in the Outline, breadcrumbs and Go to Symbol in File. Before, they appeared on no path. Since script bodies became blocks of the generated TypeScript (#135), they were type-checked in place but never listed, because TypeScript's navigation tree skips bare blocks. Now each body appears as a <script> symbol with the body's top-level declarations as children:

<script type="text/typescript">
	const analyticsEnabled: boolean = 1 < 2;
</script>

lists <script> › analyticsEnabled. The children are variables, functions, classes, interfaces, type aliases and enums, with the locals and members other declarations already show (a function's locals, a class's methods). Data blocks such as <script type="application/json"> get no symbol, matching the compiler, which gives them no region. Scoping is unchanged.

How

  • Language server side, the option the issue recommends. documentSymbolPlugin adds one <script> symbol (SymbolKind.Module) for each region in the compiler's scriptMappings. TSRXVirtualCode now keeps those regions as scriptRegions.
  • Parsing. Each body is parsed as TypeScript with the TSRX parser, where it stands in the source: the rest of the file is blanked, so every offset is a source offset and maps through the body's existing mapping. No TypeScript module is needed, so this works on the plugin and native backends too.
  • Range. Each symbol comes from its <script> element in the source AST, found by where its body starts. The range runs from the opening tag's name to the closing tag's name, the parts of the element that map to the generated TSX. The opening tag's name is the selection. A </script > or a <script in an attribute value or the body doesn't change either.
  • Placement. The <script> symbols are top-level and in source order, after the component that renders them, like the CSS rules of <style> blocks. The element's generated JSX can sit outside its component's generated code (React hoists a static <script> to module scope). Nested under the component, it widened the component's fallback range backwards and broke the component's mapped range: the component symbol disappeared in a test.
  • Enum declarations are now symbols (SymbolKind.Enum), in script bodies and elsewhere in a .tsrx file, since the issue lists enum among the declarations to show.
  • packages/content-mapper/COMPATIBILITY.md row 46 now describes the restored behaviour.

Verification

  • documentSymbolPlugin.test.js: new test covering two code <script> bodies and a JSON data block. It checks the symbol tree, kinds, nested children, and the source ranges of the <script> symbol and its children.
  • documentSymbolPlugin.test.js: a second test covers </script > closers and <script inside an attribute value and the body. Each range stays within its own element, with the opening tag's name as the selection.
  • nativeBackendLsp.test.js: the assertion that pinned the old behaviour now expects <script> › analyticsEnabled, with the range starting at the tag. It runs on the native, plugin and classic backends.
  • Both tests fail on main (4 failures: the unit test and the LSP test on the 3 backends).
  • Probe over stdio with the React, Preact, Solid, Vue and Ripple compilers: the same tree for a static and a dynamic <script> element on each.
  • Full npx vitest run: 176 files, 10010 passed, 82 skipped. pnpm typecheck, prettier --check and pnpm changeset:check are clean.

Related

Fixes #137

🤖 Generated with Claude Code

…ymbols (tsrx-org#137)

Since script bodies became blocks appended to the generated TSX,
TypeScript's navigation tree skips them, so their declarations were
missing from the Outline, breadcrumbs and Go to Symbol in File on every
backend.

The document symbol plugin now adds a `<script>` symbol for each body of
code the compiler reports in its script mappings (data blocks have
none), with the body's top-level declarations as children. The body is
parsed where it stands in the source, the rest of the file blanked, so
every offset stays a source offset and maps through the body's mapping.
The `<script>` symbols are top-level, like the CSS rules of `<style>`
blocks: React hoists a static `<script>` element out of its component,
so under the component it would break the range the component maps back
to. The TSRX virtual code keeps the compiler's script regions for this.
Enum declarations become symbols too.

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 is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ceb4d0b. Configure here.

The `<script>` symbol's element was found by searching the text for
`<script` before the body and `</script>` after it. A valid `</script >`
made the range run on to the next script, or dropped the symbol when no
script followed, and a `<script` in an attribute value took over the
range and the selection. Each symbol now comes from its element in the
source AST, looked up by where its body starts: the range runs from the
opening tag's name to the closing tag's name, and the opening tag's name
selects it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jonkwheeler
jonkwheeler marked this pull request as ready for review October 29, 2026 14:59
@jonkwheeler
jonkwheeler merged commit 180067b into tsrx-org:main Nov 12, 2026
8 checks passed
jonkwheeler added a commit that referenced this pull request Nov 18, 2026
…ainers as in children lists (#1367)

* fix(core, solid): print edge whitespace in JSX inside expression containers as in children lists

The spaces and tabs at the edges of an element's children print as
`{' '}` (#1277), but JSX inside a `{…}` container kept its text as
written on React, Preact, Hono and Solid, so
`<><div> 123 </div>{<div> 123 </div>}</>` printed two forms (#1351).

- React, Preact, Hono: a static hoisted out of a container (#1279)
  leaves the walk that applies the rule. lower_static_script_bodies,
  renamed lower_static_jsx, already redoes the walk's `<script>`
  lowering for a static; it now also wraps each children list in the
  static. This covers a container inside a bare static element too.
- Solid: create_element_children returns wrapEdgeWhitespace(...), so
  JSX in a container, an attribute value, a callback, a `const`, a
  module-level declaration or nested in a control-flow body gets the
  rule. Solid's compiler collapses a whitespace run in JSX text but
  keeps a `{'  '}` literal, so `{<b>  1  </b>}` now renders "  1  "
  like a children list instead of " 1 ".
- Vue already printed both forms the same; its output is unchanged.

In those places an element's text with edge whitespace now gets three
children instead of one string, as in a children list, so React's
existing <textarea>/<title> problem with that shape reaches them too.

wrapEdgeWhitespace returns its input when it wraps nothing, and gives
the trimmed text the location of what is left of it: with the whole
text's location, a `@` after a space (`<div> @</div>`) mapped one
character early and got no directive completion, in a children list
and now in a container.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
jonkwheeler added a commit that referenced this pull request Nov 19, 2026
…s one literal (#1381)

* fix(core, solid): print an element's only text with edge whitespace as one literal

The spaces and tabs at the edges of an element's text print as `{' '}`
(#1277), so an element whose only child was ` a ` got three children:
`<textarea>{' '}a{' '}</textarea>`. React's `<textarea>` throws for more
than one child, `<title>` renders empty, a `<pre>` loses a leading
`&#10;` in SSR, and a component whose `children` is typed `string` gets
something else, which TypeScript reports as TS2322 in the editor (#1368).

An element's only text with a space or tab at an edge now prints as one
literal of its value, `<textarea>{' a '}</textarea>`. wrapEdgeWhitespace
takes a second argument that says the children are an element's, which
the element paths pass (core's create_element_children and
lower_static_jsx, Solid's create_element_children). A fragment's only
text keeps the split, so the fragment stays, and so does text beside
other children.

only_text_value gives the parser's value, which is how TypeScript,
esbuild and oxc read such a text for React, Preact and Hono. Solid's and
Vue's compilers read some of these texts differently (a whitespace run,
`&lang;`, a decoded line feed) and now render the literal as written.
The split stays for a text where the literal could differ from that
reading: one that spans lines or holds whitespace other than spaces and
tabs (vue-jsx-vapor keeps a text's lines, Solid's compiler turns a
non-breaking space into a space and drops a CR), a character reference
the parser leaves as written or decodes where they don't (`&#+65;`,
`&copy` without a semicolon, `&#X41;`), and a reference to a control
character or a lone surrogate.

The container gets the text's location. segments.js maps the text to
the container that prints the literal where the text's start maps (it
can also map to the `return` a template body adds), so a type error on
the literal shows at the text, and maps an `@` that starts the text into
the literal for directive completions, as it does for JSX text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46


* fix(core): keep the split for an only text whose literal would differ on Solid, Vue or in the printed source

An independent review of the one-literal rule for an element's only text
(#1368) found two more texts where the literal changes what was rendered:

- A named reference the parser doesn't decode, such as `&check;` (the
  review counted 1,476 HTML names of up to eight letters and digits that
  the parser's XHTML table lacks): Solid's and Vue's HTML templates read
  it in the split text (" ✓ "), while the literal keeps it as written
  (" &check; "). only_text_value now keeps the split when the parser left
  a `&name;` in the text undecoded. names_all_decoded finds that in one
  pass over the text and its value, following each reference to where it
  went, so `&amp;amp;` (value `&amp;`) still prints one literal.
- `&#8232;`, `&#8233;` and `&#xFEFF;` decode to U+2028, U+2029 and U+FEFF,
  which the literal prints as is. JavaScript reads the first two as line
  breaks where the source map doesn't, so positions after them are off.
  They join the control characters that keep the split.

The changeset also names the SWC change the review found: SWC trimmed a
line feed written as `&#10;` at the edge of the split text, and the
literal keeps it, as TypeScript, esbuild and oxc do.

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

Development

Successfully merging this pull request may close these issues.

Declarations inside <script> bodies are not document symbols (Outline, breadcrumbs) since bodies became blocks

1 participant