Skip to content

fix(grammars): end a <script> body at </script >, and parse using declarations - #1104

Merged
jonkwheeler merged 6 commits into
tsrx-org:mainfrom
jonkwheeler:fix/grammars-script-close-using
Nov 12, 2026
Merged

jonkwheeler merged 6 commits into
tsrx-org:mainfrom
jonkwheeler:fix/grammars-script-close-using

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

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

</script > ends a <script> block (#792). The compiler ends a script body at </script, optional whitespace, then > (#790). Both grammars ended it only at the exact </script>.

  • Before: in <script>go();</script >, the closing tag was read as script. In the TextMate grammar the rest of the file, the following <p>{text}</p> included, was highlighted as TypeScript. tree-sitter parsed the body with errors.
  • After: both grammars end the body where the compiler does, including a closing tag with a line break before its >. A TextMate rule matches one line at a time, so for </script + line break + > it ends the element at </script. The > on the next line is then JSX text, and the code after it is highlighted normally.
  • <style> is unchanged: the compiler accepts only </style> there (</style > is a compile error), so both grammars still require it.

using and await using declarations (#455).

  • Before: tree-sitter parsed using x = open();, await using conn = connect();, for (using item of items) and @for (using item of items) as ERROR nodes. The TextMate grammar already highlighted them in statements and for…of, but not in an @for head.
  • After: tree-sitter has a using_declaration node, named as in tree-sitter-javascript, in statements, @{ … } scopes, fragment bodies, for…of heads and @for heads. In an @for head it takes only of, as TypeScript does: the compiler rejects @for (using item in items) with TS1493. using is highlighted as @keyword.storage. A using declaration binds identifiers only, as TypeScript requires, so using[0] stays an index expression.
  • using as a variable name parses to the same tree as on main. It is aliased to identifier everywhere a name can go where a declaration could also start: expressions, assignment targets, patterns, parameters (using => 1, (using) => using), labels (using: …; break using), and for…of / for…in heads.
  • TextMate: the @for head and the for (…; index …) head take using / await using. In a directive head it is a keyword only right after ( and before <name> of, so @for (using of items), @for (using item in items) and @if (using instanceof Foo) keep using as a variable.

Sync with the editor packages. Generated src/ files come from tree-sitter generate (tree-sitter-cli 0.26.6) and the plist from pnpm regenerate-textmate. Neovim's query copy comes from pnpm copy-tree-sitter-queries.

Zed's queries now follow the pinned grammar. Zed builds the grammar at the rev in packages/zed-plugin/extension.toml and loads the extension's queries against it. A query that names a token the grammar lacks fails to load, which turns highlighting off. The new "using" capture would have done that: the grammar at the current pin (aa133921) has no using token, so Zed built from main (pnpm zed:stage-dev) would lose highlighting until the pin moved. #1057's "keyof" had the same window.

  • copy-tree-sitter-queries.js now updates Zed's copy only with --zed.
  • sync-zed-grammar-rev.js moves the pin to HEAD and then copies the queries with --zed. Version Packages runs it when the @tsrx/zed-plugin changeset bumps the extension, so the queries and the grammar move in the same commit. Zed's highlights.scm stays at main's version in this PR, and "using" reaches it with that commit. Neovim builds the grammar from the default branch and still takes every query change at once.
  • check-zed-grammar-rev.js checks quoted tokens too: the anonymous (named: false) types in the pin's node-types.json. A small query reader (scripts/tree-sitter-query-nodes.js, with tests) skips comments and predicate arguments. The check fails on this PR's earlier Zed copy (highlights.scm references missing token "using") and passes now.
  • I chose this over only reporting the window because CI's generated-files step still passes unchanged. It runs copy-tree-sitter-queries, which now leaves Zed's copy alone. Version Packages already runs sync-zed-grammar-rev, and it checks out the full history.

Known limits (both are invalid code, or already broken on main):

  • TextMate matches one line at a time. A </script at the end of a line followed by something other than > (</script then const stillRaw = 1;) ends the highlighted script there, while the compiler keeps scanning and reports TSRX1004. A rule can't see the next line, and ending only at a same-line > would instead highlight the rest of the file as script after a valid </script + line break + >.
  • tree-sitter reads using /* a⏎b */ x = open() as a using_declaration, while the compiler reads two statements. The automatic-semicolon scanner doesn't count a line break inside a block comment. That is a general gap: a /* c⏎ */ b; is a parse error on main. A plain line break (using⏎x = open()) already gives two statements.

Changesets: one for the TextMate editors (VS Code, IntelliJ, Sublime Text) and one for the tree-sitter editors (Zed, Neovim), as #1057/#1060 split them.

Verification

  • tree-sitter test: 99/99. The new corpus cases are "Script Element Closing Tag With Whitespace", "Using Declarations", "Using As An Identifier", "Using As A Parameter, Pattern Or Label Name", and "Using Declarations In A Template Scope And For-of". Parsed with main's parser, their inputs give 3, 7, 0, 0 and 3 ERROR/MISSING nodes. The two identifier cases are regression guards, so they give 0 on main too. On this branch all five give 0, and both guards match main's trees exactly.
  • packages/vscode-plugin/tests/grammar.test.js: 4 new tests, all passing here (42/42). Three fail with main's grammar. The fourth (@for (using item in items), @if (using instanceof Foo)) guards the follow-up commit: it fails with this PR's first version and passes on main. At load averages above 150, some runs fail in Shiki with reading 'startIndex', on main's grammar as well. That comes from Shiki's per-line tokenize time limit, not from the grammar.
  • npx vitest run --project scripts (25/25, including the query reader) and --project website (47/47) pass. pnpm changeset:check passes, now including tokens.
  • All 78 .tsrx files in the repository get the same tree-sitter parse trees (main vs. this branch, compared with --lib-path) and the same TextMate tokens (Shiki, Oniguruma).
  • CI's generated-files step (generate, regenerate-textmate, copy-tree-sitter-queries) leaves no diff.
  • Before the follow-up commits: full npx vitest run (all three of my branches merged locally, on a machine with load average 50-75 from parallel runs): 10,002 passed, 82 skipped, 10 failed. All 10 are "Test timed out in 5000ms" in projects this PR doesn't touch (content-mapper, typescript-plugin, tsrx-vue). Those three projects pass in full with --testTimeout=60000 (1,284 passed, 15 skipped). pnpm typecheck is clean, pnpm changeset:check passes, and Prettier passes on the changed files.

Related

Fixes #792
Fixes #455

🤖 Generated with Claude Code

jonkwheeler and others added 2 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]>
@cursor

cursor Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

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

jonkwheeler and others added 4 commits October 26, 2026 16:24
… 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]>
@jonkwheeler
jonkwheeler marked this pull request as ready for review October 26, 2026 20:45
@jonkwheeler
jonkwheeler merged commit a283f9d into tsrx-org:main Nov 12, 2026
8 checks passed
jonkwheeler added a commit to jonkwheeler/tsrx that referenced this pull request Nov 12, 2026
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 added a commit that referenced this pull request Nov 12, 2026
…generics in tree-sitter (#1180)

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

#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 #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.

* fix(tree-sitter): parse generic arrows, call type arguments and `as T[]` (#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]>
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