Repository navigation
fix(eslint-parser): lint @switch, and give ESLint comments and tokens - #1093
Merged
jonkwheeler merged 13 commits intoNov 12, 2026
Merged
jonkwheeler merged 13 commits into
jonkwheeler merged 13 commits into
Conversation
ESLint's code path analysis treats every `SwitchCase` node as a case of a `switch` statement. A `@case` or `@default` inside `@switch` has no enclosing switch context, so linting any file with `@switch` threw "Cannot read properties of null (reading 'hasCase')", even with no rules enabled. The parser now hands ESLint those cases as `JSXSwitchCase` nodes, visited like `SwitchCase`. `@case` bodies never fall through, so they need no switch code paths, the same way `@if` branches get none. Fixes tsrx-org#913 Co-Authored-By: Claude Opus 5.5 <[email protected]>
A parse with `keywordTokens` now also stores every token the lexer reports on the program as `tsrx_tokens`. The ESLint parser reads them to give ESLint a token list. This is the same change as the parser hunk of tsrx-org#1085, which records the tokens for esrap's `tokens` option, so the two merge cleanly in either order. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The parser collected comments but returned the program with empty `comments` and `tokens`. ESLint therefore ignored every `eslint-disable` and other directive comment in a `.tsrx` file, and rules that read tokens broke: `no-unused-vars` crashed on an unused function parameter while it built its suggestion. The program now carries the comments, with `range` and `loc`, and a token list converted from the TSRX lexer's tokens in espree's shape: template chunks merged, JSX attribute strings as `JSXText`, and the tokens the lexer reports twice after a lookahead removed. The lexer reports no token for the `@` of a template directive or the body of a raw `<style>` or `<script>` element, so those become `Punctuator` and `JSXText` tokens; every character outside whitespace and comments is in a token. Fixes tsrx-org#454 Fixes tsrx-org#228 Co-Authored-By: Claude Opus 5.5 <[email protected]>
valid-for-of-key walked only some expression types, so it never reached an undeclared variable inside an optional chain (`key missing?.id`), a non-null assertion (`missing!`), an `as` or `satisfies` expression, a spread (`[...missing]`), a sequence (`(missing, item.id)`), `new`, or `await`, and reported nothing. It now walks those expressions too. Fixes tsrx-org#860 Co-Authored-By: Claude Opus 5.5 <[email protected]>
valid-for-of-key resolves key variables through ESLint's scopes, and the ESLint parser declares no `@for` index there, so `@for (...; index i; key i)` reported "Variable 'i' is not defined". The rule now accepts the loop's own index binding. Fixes tsrx-org#559 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Rules that look up a node's first or last token crashed on nodes with no
token at their ends:
- `TemplateElement` kept acorn's range, which leaves out the delimiters,
so `template-curly-spacing` threw on `` `x${y}z` ``. It now spans its
delimiters, as in espree.
- The lexer reports no token for whitespace-only JSX text, so `indent`
threw on a JSX child list with line breaks. Every `JSXText` node is now
one `JSXText` token. This also fixes text right after a template
directive's block, which the lexer read as code (`Identifier " hello"`).
- A comment in JSX text kept a `JSXExpressionContainer` placeholder spanning
just the comment, so `indent` threw on it. ESLint already gets the comment
itself, so the placeholder is dropped from the children.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…f-key key
The rule walked a fixed list of expression types, so it still missed an
undeclared variable in a tagged template (`` key tag`${missing}` ``), an
update (`missing++`), an assignment (`(t = missing)`), `import(missing)`,
or a function (`((x) => missing)(item)`).
It now reads ESLint's scope analysis instead: every reference inside the
key that no scope resolves is reported. The loop's own `index` binding,
which ESLint's scopes do not know, is still accepted, and so are type names
(`key item as Id`), which scope analysis reports as unresolved when the
type is declared in the file.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…arser-switch-comments-tokens
`semi --fix` rewrote `@for (const item of items)` into `@for (const item; of items)`, which does not parse. `semi`, like `init-declarations` and other loop-aware rules, only treats a declaration as a loop head when its parent is a for statement, and the parent was `JSXForExpression`. ESLint's scope analysis also declared no `@for` index, so core `no-undef` reported it. The loop of a `@for` is now a real `ForOfStatement`, `ForInStatement` or `ForStatement` in the new `loop` field of `JSXForExpression`. ESLint's code path analysis allows a for statement no children of its own beyond its standard fields, so `index`, `key` and the `@empty` branch stay on `JSXForExpression`, and visitor keys still reach every part. The parser now returns its own scope manager, built with `eslint-scope` (a new dependency, the version `eslint` already installs). It declares the loop's `index` and resolves its `key` in the loop's scope, next to the loop variable, and the `@empty` branch outside it. `valid-for-of-key` no longer needs its own exception for the index, and `control-flow-jsx` reads the loop body from `loop`. Related: tsrx-org#559 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 7059f37. Configure here.
jonkwheeler
marked this pull request as ready for review
October 27, 2026 12:00
This was referenced Oct 27, 2026
Merged
Closed
This was referenced Nov 6, 2026
A `@for`'s loop fields move from the `JSXForExpression` into a real for statement in `loop`, and `@switch` arms become `JSXSwitchCase` nodes. Earlier AST field moves in this repo released as minor. The changeset now says which fields moved and what tools read instead. The `@tsrx/eslint-plugin` and `@tsrx/core` entries stay at patch: the plugin's change is a fix, and core's `tsrx_tokens` is an addition. The fixed group still takes the plugin, with the parser, language server, Prettier plugin and TypeScript plugin, to 0.7.0. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…or-of-key key `is_type_name` skipped every identifier under a TypeScript node other than the value-holding expressions, so the default in `constructor(public x = missing)` counted as a type and `missing` was not reported. A `TSTypeAssertion` cannot reach the rule: TSRX reads `<T>value` as JSX. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
…arser-switch-comments-tokens Brings in tsrx-org#1094's head a236508, which carries the fixes for Cursor Bugbot's findings from the 2026-11-10 sweep. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
jonkwheeler
added a commit
to jonkwheeler/tsrx
that referenced
this pull request
Nov 11, 2026
…nt-parser-rule-edge-cases Brings in tsrx-org#1093's head b99667a, which carries the fixes for Cursor Bugbot's findings from the 2026-11-10 sweep. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
main now has tsrx-org#1094'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
to jonkwheeler/tsrx
that referenced
this pull request
Nov 12, 2026
main now has tsrx-org#1093'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
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.

Stacked on #1094; merge after #1094. #1094's branch is merged into this one with merge commits, so review the commits after
a2365088d.Summary
This PR fixes ESLint parser bugs that made
.tsrxfiles hard or impossible to lint:@switchno longer crashes ESLint. Before: any file with a@caseor@defaultthrewCannot read properties of null (reading 'hasCase'), even withrules: {}, and the CLI exited 2. After: it lints normally. ESLint's code path analysis treats everySwitchCaseas part of aswitchstatement, so the parser now gives ESLint the@switchcases asJSXSwitchCasenodes, visited likeSwitchCase.@casebodies never fall through, and@ifbranches get no code paths either.commentslist, so// eslint-disable-next-line,/* eslint-disable */,/* global */and every other directive was ignored. After: ESLint gets every comment withrangeandloc. The value comes from the source text, because the parser strips the indentation from multi-line block comment values.no-unused-varscrashed onexport function f(unused) {}while it built its suggestion, andtemplate-curly-spacing,indentand other token rules crashed too. After:no-unused-varsreports'unused' is defined but never used., and the other rules run.@foris a loop to ESLint. Before: with tokens in place,semi --fixrewrote@for (const item of items)into@for (const item; of items), which does not parse.init-declarationsreported@forvariables, and coreno-undefreported a declared@forindex (the rest of [Bug]: ESLint valid-for-of-key rejects a declared @for index used as the key #559). After: the loop is a realForOfStatement,ForInStatementorForStatement, and the parser returns its own scope manager.semiandinit-declarationstreat the head as a loop head.no-undefandno-unused-varsresolve theindex, thekeyand the@emptybranch in the right scopes.How
@forreaches ESLintThe loop sits in a new
loopfield ofJSXForExpression.index,keyand the@emptybranch stay onJSXForExpressionitself. ESLint's code path analysis asserts that afor…oforfor…instatement has no children beyondleft,rightandbody, so they cannot go on the loop node. The visitor keys (index,key,loop,empty) still reach every part.The scope manager is
eslint-scope, now a dependency of@tsrx/eslint-parser(^9.1.2, the versioneslintalready installs). The parser runs it with the options ESLint uses, plus@forhandling:indexis declared in the loop's scope, next to the loop variablekeyis resolved in that scope@emptybranch is resolved outside itpnpm-lock.yamlchanges only in the@tsrx/eslint-parserimporter.On the plugin side,
valid-for-of-keydrops its own exception for the index, andcontrol-flow-jsxreads the loop body fromloop.Release: a minor bump
The changeset bumps
@tsrx/eslint-parserminor, as this repo's earlier AST field moves were (#869, #874, #877, #879). It says what tools read now:JSXForExpression'sleft,right,await,init,test,updateandbodymove intoloop. Tools readloop.left,loop.bodyand so on.index,keyandemptystay.@caseand@defaultarms areJSXSwitchCasenodes for ESLint, notSwitchCase, so aSwitchCaseselector no longer matches them.@tsrx/eslint-parseris in a fixed group with@tsrx/eslint-plugin,@tsrx/language-server,@tsrx/prettier-pluginand@tsrx/typescript-plugin, so this one minor takes all five from 0.6.3 to 0.7.0.changeset status --since origin/mainconfirms it. The@tsrx/eslint-pluginand@tsrx/coreentries stay patch: the plugin's change is a fix, and core'stsrx_tokensis an addition. Core goes to 0.5.6.Tokens
A
keywordTokensparse now also stores every lexer token on the program astsrx_tokens. This is #1085's parser hunk byte for byte, so the two PRs merge cleanly in either order. The ESLint parser converts those tokens to espree's shape:TemplatetokensJSXTextJSXTexttoken per JSX text node</style>or</script>closing tag split into its four tokensPunctuatorfor each directive@, and aJSXTextfor each raw<style>or<script>bodyEvery character outside whitespace and comments is in exactly one token.
TemplateElementspans its delimiters, as in espree. TheJSXExpressionContainerplaceholder for a comment in JSX text is dropped, so every node starts and ends on a token.Known issues
max-paramsreadsparamson TypeScript function types, which the TSRX parser namesparameters(9 files).indentexpects an=in every JSX attribute that has a value, and the TSRX{name}shorthand attribute has none (7 files).@for:no-await-in-loopreports anawaitin a@forbody, which TSRX does run one item at a time, andguard-for-inreports a@for…inwithout a guard.no-unused-varsreports an unused@forindex, but its "remove variable" suggestion deletes only the name and leavesindex ;. Suggestions are never applied by--fix.no-unreachablereports code after a@casearm that throws, as it already does for@ifand@tryon main.keywordTokensprogram now carriestsrx_tokens, andstructuredCloneof that program throws, because acorn token types hold functions. This comes from fix(core): require esrap 2.4 and map editor output through its tokens #1085's shared hunk. The ESLint parser removestsrx_tokensbefore handing the AST to ESLint.Verification
packages/eslint-parser/tests/index.test.ts. Each new test failed for the reported reason before its fix:@switchwithrules: {}no-unused-varson an unused parameterloccheckstemplate-curly-spacingandindentsemiunderverifyAndFixleaving@forheads untouched (const,let,using,indexwithkey, and a plainfor) while still adding a missing;elsewhereno-undef,no-unused-varsandinit-declarationson@forloops: names used only inkeyor@emptycount as used, unused loop variables and indexes are reported, and a loop variable used in@emptyis undefined thereJSXText" heuristic on real text such as the,after an inline<code>.semibreaks are gone, and the remaining structure changes are the intendedquote-props,curlyandprefer-templatefixes.pnpm typecheck,pnpm changeset:checkand Prettier are clean.2a4ae6891, which changes only the changeset: eslint-parser and eslint-plugin 59/59 on ESLint 10.3 and on 9.39.5, with the parser rebuilt, andpnpm typecheck,pnpm changeset:checkandpnpm format:checkclean. For 9.39.5,eslintresolves to a scratch install, with no lockfile change.2a4ae6891's changeset: every claim matched the parser's output against main's, and the levels are right. Its note that the first commit message understated the fixed group's effect was fixed before the push.a2365088dmerged in (b99667a0c): eslint-parser and eslint-plugin 61/61.npx vitest runon the first version: all failures were load timeouts in files this PR does not touch, and they pass with a longer timeout. It has not been re-run since; later changes touch only the eslint-parser and eslint-plugin packages and the lockfile importer.Related
Fixes #913
Fixes #454
Fixes #228
Related: #559 (fixed by #1094; this PR clears core
no-undefon the index)Related: #1085 (same parser hunk)
🤖 Generated with Claude Code
Note
Medium Risk
Minor AST shape changes for eslint-parser consumers and broad lint/scope behavior changes across all
.tsrxfiles, though limited to tooling rather than runtime app code.Overview
Fixes ESLint crashes and blind spots on
.tsrxby having@tsrx/eslint-parseremit comments, espree-style tokens, and a customeslint-scopescope manager, and by reshaping a few TSRX nodes for ESLint’s walkers.@switcharms are normalized toJSXSwitchCase(notSwitchCase) so code-path analysis no longer throws.@forloop heads live under a newloopfield as realForStatement/ForInStatement/ForOfStatementnodes;index,key, andemptystay onJSXForExpression, withTSRXReferencerdeclaringindexin the loop scope and visiting@emptyoutside it. Directive comments, token-based rules (no-unused-vars,template-curly-spacing,indent), and loop rules (semi --fix,init-declarations,no-undef) behave on TSRX as on JS. Parsing withkeywordTokensstill recordstsrx_tokenson the program (stripped before ESLint).@tsrx/eslint-pluginupdatescontrol-flow-jsxandvalid-for-of-keyforloop.bodyand the new scopes. New parser tests cover@switch, comments/tokens, fixes, and@forscoping.@tsrx/eslint-parserminor bump reflects theJSXForExpression/ switch-case AST moves.Reviewed by Cursor Bugbot for commit f803020. Bugbot is set up for automated code reviews on this repo. Configure here.