Repository navigation
fix(grammars): end a <script> body at </script >, and parse using declarations - #1104
Merged
jonkwheeler merged 6 commits intoNov 12, 2026
Merged
Conversation
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]>
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. |
… 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
marked this pull request as ready for review
October 26, 2026 20:45
This was referenced Oct 27, 2026
Merged
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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>.<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.>. 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.usingandawait usingdeclarations (#455).using x = open();,await using conn = connect();,for (using item of items)and@for (using item of items)asERRORnodes. The TextMate grammar already highlighted them in statements andfor…of, but not in an@forhead.using_declarationnode, named as in tree-sitter-javascript, in statements,@{ … }scopes, fragment bodies,for…ofheads and@forheads. In an@forhead it takes onlyof, as TypeScript does: the compiler rejects@for (using item in items)with TS1493.usingis highlighted as@keyword.storage. Ausingdeclaration binds identifiers only, as TypeScript requires, sousing[0]stays an index expression.usingas a variable name parses to the same tree as on main. It is aliased toidentifiereverywhere a name can go where a declaration could also start: expressions, assignment targets, patterns, parameters (using => 1,(using) => using), labels (using: …; break using), andfor…of/for…inheads.@forhead and thefor (…; index …)head takeusing/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)keepusingas a variable.Sync with the editor packages. Generated
src/files come fromtree-sitter generate(tree-sitter-cli 0.26.6) and the plist frompnpm regenerate-textmate. Neovim's query copy comes frompnpm copy-tree-sitter-queries.Zed's queries now follow the pinned grammar. Zed builds the grammar at the
revinpackages/zed-plugin/extension.tomland 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 nousingtoken, 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.jsnow updates Zed's copy only with--zed.sync-zed-grammar-rev.jsmoves the pin to HEAD and then copies the queries with--zed. Version Packages runs it when the@tsrx/zed-pluginchangeset bumps the extension, so the queries and the grammar move in the same commit. Zed'shighlights.scmstays 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.jschecks quoted tokens too: the anonymous (named: false) types in the pin'snode-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.copy-tree-sitter-queries, which now leaves Zed's copy alone. Version Packages already runssync-zed-grammar-rev, and it checks out the full history.Known limits (both are invalid code, or already broken on main):
</scriptat the end of a line followed by something other than>(</scriptthenconst 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 +>.using /* a⏎b */ x = open()as ausing_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 3ERROR/MISSINGnodes. 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 withreading '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:checkpasses, now including tokens..tsrxfiles 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).generate,regenerate-textmate,copy-tree-sitter-queries) leaves no diff.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 typecheckis clean,pnpm changeset:checkpasses, and Prettier passes on the changed files.Related
Fixes #792
Fixes #455
🤖 Generated with Claude Code