Skip to content

fix(grammars): end a <style> body at </style >, and parse TypeScript generics in tree-sitter - #1180

Merged
jonkwheeler merged 9 commits into
tsrx-org:mainfrom
jonkwheeler:fix/grammars-ts-generics-style-close
Nov 12, 2026
Merged

jonkwheeler merged 9 commits into
tsrx-org:mainfrom
jonkwheeler:fix/grammars-ts-generics-style-close

Conversation

@jonkwheeler

Copy link
Copy Markdown
Contributor

Stacked on #1104; review the commits after 92402cc; merge after #1104.

The </style > half matches the compiler once #1138 merges. Until then, the grammars accept a closing tag that the compiler still reports as Unclosed tag '<style>'.

Summary

Two grammar fixes for the TextMate grammar (VS Code, JetBrains IDEs, Sublime Text) and the tree-sitter grammar (Zed, Neovim).

</style > ends a <style> block (#1156). #1138 makes the compiler end a style body at </style, optional whitespace, then >. #1104 did this for <script>, and this PR does the same for <style>.

  • Before: in <style>.a { color: red; }</style >, the closing tag was read as CSS. TextMate highlighted the rest of the file as CSS, <p class="a">{text}</p> included. tree-sitter parsed the file as ERROR from the start.
  • After: both grammars end the body where the compiler does. That includes a closing tag with a line break before its >. As with <script> in fix(grammars): end a <script> body at </script >, and parse using declarations #1104, TextMate can't see the next line, so it ends the element at </style and reads the > on the next line as JSX text.
  • tree-sitter: the closing tag is still the single </style> token, now matched as </style, whitespace, >. Node types and queries don't change, so Zed's pinned queries are unaffected.

TypeScript generics and as T[] in tree-sitter (#1155).

Source Before After
<T,>(x: T) => x, <T extends U>(x: T) => x, async <T, U = T>(x: T): U => x ERROR over the statement and the lines after it arrow_function with type_parameters
useState<T[]>([]) useState < T[<MISSING>] > ([]) call_expression with type_arguments (array_type)
useState<T>(x), obj.m<A, B>(x), sql<Row>`…` , ref<A | null>(null) < and > comparisons call_expression with type_arguments
!f<T>(x), await f<T>(x) comparisons the operator applies to the whole call
value as T[], value satisfies T[], value as T[K] (value as T)[<MISSING>] as_expression / satisfies_expression with array_type / lookup_type
  • In a call with type arguments, the callee is highlighted as a function (@function.call / @function.method.call), as in any other call.
  • How the call is parsed: the type-argument form has its own callee rule, a primary expression. It is declared as a conflict with expression, so the parser keeps both f<T>(x) and f < T > (x) until one fails. Dynamic precedence picks the call when both parse, as TypeScript does. a < b > c and a < b && c > d stay comparisons. new Foo<T>() keeps its own type_arguments.
  • extends is now a keyword after <T, where an arrow's type parameters can start. It is also accepted as a JSX attribute name, so <Foo extends={x} />, <Foo extends /> and <Foo extends bar /> still parse as tags, with the same trees as before.
  • The postfix array and lookup types now take precedence over as/satisfies (their precedence is now PREC.REL + 1; it was 4). Their order relative to the other type operators doesn't change.

Known limits:

  • x as A | B and x as A & B are still read as (x as A) | B, with no error. TypeScript reads the whole union as the type. The issue didn't cover this.
  • await f(x) without type arguments is still (await f)(x), because await has call precedence. This is a separate, pre-existing gap. await f<T>(x) is now correct.
  • <const T,>(x) => x, and const type parameters in general, are still ERROR: type_parameter has no const modifier.
  • tree-sitter reads the bare <T>(x: T) => x as an arrow function. The compiler reads it as a tag, and after refactor(core): read generic arrows by TypeScript's TSX rule (#183) #1124 it reports TSRX1001 there, so the error now comes only from the language server.
  • TextMate keeps a CSS string whole. In content: "</style >", the body therefore continues past the closing tag, where the compiler (after fix(core): end a style body at </style >, and name </> in a mismatch #1138) and tree-sitter end it. This was already the case for </style> and </script>.

Generated src/ files come from tree-sitter generate (tree-sitter-cli 0.26.6) and the plist from pnpm regenerate-textmate. No query changed: copy-tree-sitter-queries leaves the Neovim copy as is, and Zed's copy stays at its pin.

Verification

  • tree-sitter test --rebuild: 104/104. New corpus cases: "Style Element Closing Tag With Whitespace", "Call Expressions With Type Arguments", "As And Satisfies With Array Types", "Generic Arrow Functions", and "Tags That Start Like Generic Arrows" (a regression guard). With fix(grammars): end a <script> body at </script >, and parse using declarations #1104's grammar, the first four fail and the guard passes.
  • packages/vscode-plugin/tests/grammar.test.js: 2 new <style> closing-tag tests (same-line </style >, and </style then > on the next line). Both fail on fix(grammars): end a <script> body at </script >, and parse using declarations #1104's grammar and pass here, 44/44.
  • All 78 repository .tsrx files, comparing fix(grammars): end a <script> body at </script >, and parse using declarations #1104 (92402cc7) with this branch:
    • TextMate tokens (Shiki, Oniguruma): identical.
    • tree-sitter trees (--lib-path, with ranges): 72 files are identical. The 6 that differ are the 6 that had MISSING nodes, and now have none. In them, the only changes are at useState<X[]>(…) and ) as HTMLInputElement[] (the MISSING sites) and at useState<'section' | 'article'>(…), createSignal<…>(…) and ref<HTMLInputElement | null>(…), which were comparisons and are now calls with type_arguments.
    • The const statements around the old MISSING nodes change from lexical_declaration to variable_declaration. That is the node every other const in those files already gets; the old one came from error recovery.
  • 64 extra probes compared with fix(grammars): end a <script> body at </script >, and parse using declarations #1104's parser: generic calls in ternaries, arguments, JSX expressions and template bodies; operator guards (a * b < c, !a < b, i++ < n, a < b + c, x < y >> z); and tags such as <b>(i) => i</b> and <Foo>(text)</Foo>. Every difference is one of the fixes above.
  • Parse throughput over the 78 files is unchanged within noise (best of 6: 15.9k vs 16.6k bytes/ms, load average around 60).
  • Full npx vitest run: 10,001 passed, 82 skipped, 18 failed, at load average 60–100. Of the 18, 16 are timeouts and 2 are Shiki's reading 'startIndex' (its per-line time limit, the same failure fix(grammars): end a <script> body at </script >, and parse using declarations #1104 saw on main's grammar). The failing files pass when re-run: --project vscode-plugin --project website (169/169), and the other 7 files with --testTimeout=120000 (51/51).
  • pnpm typecheck is clean, Prettier passes on the changed files, and pnpm changeset:check passes (Zed grammar rev check included).
  • CI's generated-files step (generate, regenerate-textmate, copy-tree-sitter-queries) leaves no diff.

Changesets: one for the TextMate editors and one for the tree-sitter editors, as #1104 split them.

Related

Fixes #1155
Fixes #1156

🤖 Generated with Claude Code

jonkwheeler and others added 8 commits October 26, 2026 14:50
tsrx-org#792)

The compiler ends a `<script>` body at `</script`, optional HTML whitespace,
then `>` (tsrx-org#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.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tsrx-org#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.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… 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.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eclaration

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.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ck 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.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]>
…srx-org#1156)

tsrx-org#1138 makes the compiler end a `<style>` body at `</style`, optional HTML
whitespace, then `>`, as a `<script>` body already ends. Both grammars still
ended it only at the exact `</style>`, so in `<style>…</style >` the closing
tag was read as CSS: tree-sitter parsed the file as an error from the start,
and the TextMate grammar highlighted everything after it as CSS.

- tree-sitter: `scan_raw_text` allows whitespace before the `>` for both raw
  text elements, and the closing tag is one token, `</style`, whitespace and
  `>`, aliased to `</style>`. The node types and queries are unchanged.
- TextMate: the style element ends at `(</)(style)\s*(>)` and its body at
  `(?=</style\s*(?:>|$))`, as tsrx-org#1104 did for `<script>`. A closing tag with a
  line break before its `>` ends the element at `</style` on its line.

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…[]` (tsrx-org#1155)

The tree-sitter grammar misread common TypeScript expressions:

- `<T,>(x: T) => x` and `<T extends U>(x: T) => x` were an `ERROR` over the
  statement and the lines after it: `arrow_function` had no type parameters.
- `useState<T>([])` was `useState < T > ([])`, and `useState<T[]>([])` added
  a `MISSING` identifier for `T[` as an index: `call_expression` had no type
  arguments.
- `value as T[]` was `(value as T)[<MISSING>]`: `as` (PREC.REL) outranked the
  postfix array type (4), so the expression was reduced before the `[`.

Now:

- `arrow_function` takes optional `type_parameters` before its parameter
  list, as in tree-sitter-typescript. `extends` is a keyword after `<T` there,
  so it is also accepted as a JSX attribute name, keeping
  `<Foo extends={x} />` and `<Foo extends />` tags.
- `call_expression` has a second form with `type_arguments`. Its callee is a
  hidden `_type_arguments_callee` (a primary expression), declared as a
  conflict with `expression`, so the parser keeps both `f<T>(x)` and
  `f < T > (x)` until one fails; dynamic precedence takes the call when both
  parse, as TypeScript does. Since the callee is a primary expression, a prefix
  operator applies to the whole call (`!f<T>(x)`, `await f<T>(x)`), and
  `new Foo<T>()` keeps its own type arguments.
- The postfix type precedence is above PREC.REL, so `as T[]`,
  `satisfies T[]` and `as T[K]` keep the brackets in the type.

The 6 repository .tsrx files that had `MISSING` nodes now parse without
errors. Their trees change only at those statements and at the generic calls
`useState<'section' | 'article'>(…)` and `ref<HTMLInputElement | null>(…)`;
the other 72 files get identical trees.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cursor

cursor Bot commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

This PR is too large for Bugbot to review. It changes 104,866 lines and 4,076,299 characters. Split the change into smaller pull requests to get a review.

main now has tsrx-org#1104's squash commit; merged against the parent commit this branch has so git doesn't redo that work.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
@jonkwheeler
jonkwheeler merged commit 985dbfd into tsrx-org:main Nov 12, 2026
7 of 8 checks passed
jonkwheeler added a commit to jonkwheeler/tsrx that referenced this pull request Nov 12, 2026
main now has tsrx-org#1180's squash commit; merged against the parent commit this branch has so git doesn't redo that work.

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

1 participant