Skip to content

fix(eslint-parser): lint @switch, and give ESLint comments and tokens - #1093

Merged
jonkwheeler merged 13 commits into
tsrx-org:mainfrom
jonkwheeler:fix/eslint-parser-switch-comments-tokens
Nov 12, 2026
Merged

jonkwheeler merged 13 commits into
tsrx-org:mainfrom
jonkwheeler:fix/eslint-parser-switch-comments-tokens

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

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 .tsrx files hard or impossible to lint:

  • @switch no longer crashes ESLint. Before: any file with a @case or @default threw Cannot read properties of null (reading 'hasCase'), even with rules: {}, and the CLI exited 2. After: it lints normally. ESLint's code path analysis treats every SwitchCase as part of a switch statement, so the parser now gives ESLint the @switch cases as JSXSwitchCase nodes, visited like SwitchCase. @case bodies never fall through, and @if branches get no code paths either.
  • Directive comments work. Before: the program had an empty comments list, so // eslint-disable-next-line, /* eslint-disable */, /* global */ and every other directive was ignored. After: ESLint gets every comment with range and loc. The value comes from the source text, because the parser strips the indentation from multi-line block comment values.
  • Rules that read tokens work. Before: the token list was empty, so no-unused-vars crashed on export function f(unused) {} while it built its suggestion, and template-curly-spacing, indent and other token rules crashed too. After: no-unused-vars reports 'unused' is defined but never used., and the other rules run.
  • @for is a loop to ESLint. Before: with tokens in place, semi --fix rewrote @for (const item of items) into @for (const item; of items), which does not parse. init-declarations reported @for variables, and core no-undef reported a declared @for index (the rest of [Bug]: ESLint valid-for-of-key rejects a declared @for index used as the key #559). After: the loop is a real ForOfStatement, ForInStatement or ForStatement, and the parser returns its own scope manager. semi and init-declarations treat the head as a loop head. no-undef and no-unused-vars resolve the index, the key and the @empty branch in the right scopes.

How @for reaches ESLint

The loop sits in a new loop field of JSXForExpression. index, key and the @empty branch stay on JSXForExpression itself. ESLint's code path analysis asserts that a for…of or for…in statement has no children beyond left, right and body, 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 version eslint already installs). The parser runs it with the options ESLint uses, plus @for handling:

  • the loop's index is declared in the loop's scope, next to the loop variable
  • the key is resolved in that scope
  • the @empty branch is resolved outside it

pnpm-lock.yaml changes only in the @tsrx/eslint-parser importer.

On the plugin side, valid-for-of-key drops its own exception for the index, and control-flow-jsx reads the loop body from loop.

Release: a minor bump

The changeset bumps @tsrx/eslint-parser minor, as this repo's earlier AST field moves were (#869, #874, #877, #879). It says what tools read now:

  • A JSXForExpression's left, right, await, init, test, update and body move into loop. Tools read loop.left, loop.body and so on. index, key and empty stay.
  • @case and @default arms are JSXSwitchCase nodes for ESLint, not SwitchCase, so a SwitchCase selector no longer matches them.

@tsrx/eslint-parser is in a fixed group with @tsrx/eslint-plugin, @tsrx/language-server, @tsrx/prettier-plugin and @tsrx/typescript-plugin, so this one minor takes all five from 0.6.3 to 0.7.0. changeset status --since origin/main confirms it. The @tsrx/eslint-plugin and @tsrx/core entries stay patch: the plugin's change is a fix, and core's tsrx_tokens is an addition. Core goes to 0.5.6.

Tokens

A keywordTokens parse now also stores every lexer token on the program as tsrx_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:

  • sorted, with the copies the lexer reports again after a lookahead dropped
  • template chunks merged into Template tokens
  • JSX attribute strings typed as JSXText
  • one JSXText token per JSX text node
  • a raw </style> or </script> closing tag split into its four tokens
  • a Punctuator for each directive @, and a JSXText for each raw <style> or <script> body

Every character outside whitespace and comments is in exactly one token. TemplateElement spans its delimiters, as in espree. The JSXExpressionContainer placeholder for a comment in JSX text is dropped, so every node starts and ends on a token.

Known issues

  • Over the 113 repo sources and doc examples, all 292 core rules (deprecated ones included) run without crashing except for two AST-shape problems. Both also crash on main, and neither changed in this round:
    • max-params reads params on TypeScript function types, which the TSRX parser names parameters (9 files).
    • indent expects an = in every JSX attribute that has a value, and the TSRX {name} shorthand attribute has none (7 files).
  • Loop rules now apply to @for: no-await-in-loop reports an await in a @for body, which TSRX does run one item at a time, and guard-for-in reports a @for…in without a guard.
  • no-unused-vars reports an unused @for index, but its "remove variable" suggestion deletes only the name and leaves index ;. Suggestions are never applied by --fix.
  • no-unreachable reports code after a @case arm that throws, as it already does for @if and @try on main.
  • A keywordTokens program now carries tsrx_tokens, and structuredClone of 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 removes tsrx_tokens before handing the AST to ESLint.

Verification

  • packages/eslint-parser/tests/index.test.ts. Each new test failed for the reported reason before its fix:
    • @switch with rules: {}
    • disable directives
    • no-unused-vars on an unused parameter
    • an exact token list with coverage and loc checks
    • template-curly-spacing and indent
    • text after a directive block
    • semi under verifyAndFix leaving @for heads untouched (const, let, using, index with key, and a plain for) while still adding a missing ; elsewhere
    • no-undef, no-unused-vars and init-declarations on @for loops: names used only in key or @empty count as used, unused loop variables and indexes are reported, and a loop variable used in @empty is undefined there
  • eslint-parser and eslint-plugin: 59/59, with the parser rebuilt.
  • The reviewer's checkers over 113 sources:
    • Token checker: everything parses, and the only flags are its "suspicious JSXText" heuristic on real text such as the , after an inline <code>.
    • All core rules: the same two pre-existing crashes as the previous head, and nothing new.
    • Lint-and-fix check: no fix output that fails to parse. The semi breaks are gone, and the remaining structure changes are the intended quote-props, curly and prefer-template fixes.
  • pnpm typecheck, pnpm changeset:check and Prettier are clean.
  • On 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, and pnpm typecheck, pnpm changeset:check and pnpm format:check clean. For 9.39.5, eslint resolves to a scratch install, with no lockfile change.
  • Codex on 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.
  • With fix(eslint-plugin): valid-for-of-key accepts a declared @for index and checks optional-chain keys #1094's head a2365088d merged in (b99667a0c): eslint-parser and eslint-plugin 61/61.
  • npx vitest run on 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-undef on 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 .tsrx files, though limited to tooling rather than runtime app code.

Overview
Fixes ESLint crashes and blind spots on .tsrx by having @tsrx/eslint-parser emit comments, espree-style tokens, and a custom eslint-scope scope manager, and by reshaping a few TSRX nodes for ESLint’s walkers.

@switch arms are normalized to JSXSwitchCase (not SwitchCase) so code-path analysis no longer throws. @for loop heads live under a new loop field as real ForStatement / ForInStatement / ForOfStatement nodes; index, key, and empty stay on JSXForExpression, with TSRXReferencer declaring index in the loop scope and visiting @empty outside 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 with keywordTokens still records tsrx_tokens on the program (stripped before ESLint).

@tsrx/eslint-plugin updates control-flow-jsx and valid-for-of-key for loop.body and the new scopes. New parser tests cover @switch, comments/tokens, fixes, and @for scoping. @tsrx/eslint-parser minor bump reflects the JSXForExpression / switch-case AST moves.

Reviewed by Cursor Bugbot for commit f803020. Bugbot is set up for automated code reviews on this repo. Configure here.

jonkwheeler and others added 5 commits October 26, 2026 14:34
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]>
jonkwheeler and others added 4 commits October 26, 2026 16:34
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]>
`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]>

@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 7059f37. Configure here.

Comment thread packages/eslint-parser/src/index.ts Resolved
jonkwheeler and others added 3 commits November 9, 2026 17:50
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
jonkwheeler merged commit d5015d2 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#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
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