Skip to content

fix(core): stop an @switch arm body at the end of the input (#492) - #497

Merged
leonidaz merged 1 commit into
mainfrom
fix/parser-switch-eof-492
Sep 25, 2026
Merged

leonidaz merged 1 commit into
mainfrom
fix/parser-switch-eof-492

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #492

Cause

The loop over an @case/@default body, while (this.type !== tt.braceR), stopped only at }. At the end of the input, #parseJSXSwitchCaseConsequent skipped its statement branch (which excluded tt.eof) and fell through to #parseJSXSwitchCaseRawText(). That read an empty text node from this.start, which was already the end of the input, so the token stayed tt.eof and the loop never ended.

  • In every parse mode, an unterminated arm looped forever.
  • With an unclosed element in the arm (@case 1: { <section>), the default mode threw the unclosed-tag error first. collect and loose only record that error and parse on to the end of the input, so they looped forever. loose is what the editor parses with on every keystroke.

Fix

  • The arm loop stops at tt.eof, like #parseCodeBlockBody, the loop for @{ … } and @if/@for/@try bodies. The this.expect(tt.braceR) after it then reports the missing } as Unexpected token at the end of the input. An unterminated @if or @for body gives the same error, in every mode.
  • The raw-text fallback at the end of #parseJSXSwitchCaseConsequent could only be reached at the end of the input, so it's gone. The helper now parses a statement there, and parseStatement throws at the end of the input rather than returning empty text. I checked that: with the loop guard taken out again, all three modes still throw Unexpected token (3:14) instead of hanging.
Input Mode Before After
…@switch (mode) {\n @case 1: { default, collect, loose hangs Unexpected token (3:14)
…@switch (mode) {\n @default: { default, collect, loose hangs Unexpected token (3:15)
…@case 1: {\n <section> default Unclosed tag '<section>'. … (4:15) unchanged
…@case 1: {\n <section> collect, loose hangs Unexpected token (4:15)
…@if (mode) {\n <section> (for comparison) collect, loose Unexpected token (3:15) unchanged

Unexpected token at the end of the input is also how the parser reports a missing } after @if (ok) {, @for (…) {, @switch (x) {, @{, and a plain if (ok) {. So the arm error matches those rather than getting its own message.

Other loops checked

I went through the template parser's loops for the same missing end-of-input stop:

  • #parseCodeBlockBody (@{ }, @if, @for, @try bodies) already stops at tt.eof.
  • The @switch loop over arms keeps while (this.type !== tt.braceR). At the end of the input, #eatJSXSwitchCaseClauseKeyword() finds no label and this.unexpected() throws. Adding tt.eof there would skip that error and close the switch without its }.
  • parseTemplateBody returns at tt.eof.
  • The opening element's attribute loop ends because jsx_parseAttribute throws at the end of the input.
  • jsx_readToken's while (true) returns eof or raises Unterminated JSX contents at the end of the input.
  • The index scanners (#templateRawTextEnd, #switchCaseLabelStart, #parseJSXSwitchCaseRawText, and the rest) and the CSS parser's loops in parse/style.js are bounded by the input length.

I also parsed truncated inputs to test this empirically. The inputs were every .tsrx file in the repository, cut at every line end and after every {, <, >, :, and (, plus every string or template-literal token from the test files that contains @ or <, cut at every character. That's 261,071 prefixes, each parsed in all three modes: 783,213 parses, each in a worker that was killed after 3 s.

  • This branch: no hangs.
  • origin/main: A 1-in-10 sample of the prefixes, 78,324 parses killed after 2 s, hung 710 times across 259 prefixes: 197 in the default mode, 254 in collect, and 259 in loose. Every one of those prefixes ends after an @case or @default label, and each one throws Unexpected token on this branch.

So I found no other end-of-input hang, and no separate issue is filed.

Tests

  • packages/tsrx/tests/utils/parser.test.js, input that ends inside an @switch arm:
    • Seven sources ending inside an arm (@case, @default, after a statement, after an element, in the second arm, in a value-position @switch, and in an @switch inside a plain TSX element) report Unexpected token at the end of the input in all three modes.
    • An unclosed element in an arm gives the unclosed-tag error in the default mode and Unexpected token (4:15) in collect and loose.
    • Every prefix of a component with @switch arms (setup statements, @for, @if, a fragment, a value-position @switch) returns in all three modes, and the whole component parses.
  • A new helper, tests/shared/parse-in-worker.js, runs these parses in a worker thread. A vitest timeout can't interrupt a synchronous loop, so the helper terminates the worker when a parse runs past 10 s (the worker's heap is capped too) and fails with the input that hung. Against origin/main's parser, each of the three tests failed after about 10 s with, for example, Parsing "function App({ mode }) @{\n @switch (mode) {\n @case 1: {" with undefined did not return within 10000 ms., and the run went on.

Checks

  • AST comparison with origin/main (e9274468): I parsed 12,159 distinct sources in all three modes, each parse in a worker killed after 5 s: the 69 .tsrx files, and the 20,739 string and template-literal tokens of the 147 test files, deduplicated. That's 36,477 parses. I compared each program's AST, its comments, and its collected errors, or the thrown message and position. 36,443 match. The other 34 hung on origin/main and now throw Unexpected token at the end of the input. They're the arm-ending strings that fix(core): parse JSX spread children and report them as unsupported (#417) #495's comparison found, in tsrx-mcp/tests/authoring.test.js, tsrx/tests/analyze/styles.test.js, and tsrx/tests/utils/parser.test.js, plus this PR's new test inputs.
  • Language server: I built the Volar language service the way packages/language-server/tests/setup.js does, with the TSRX language plugin, the compile-error diagnostic plugin, and the React fixture, which compiles in loose mode. I then asked for diagnostics on a file ending in @case 1: {. On origin/main, getDiagnostics didn't return within 20 s, so the process was killed. On this branch it returned in 54 ms with one fatal diagnostic, Unexpected token (3:14), at the end of the file (line 2, character 14, zero-based).
  • Formatter and ESLint parser: On origin/main, both hung on the same file and were killed after 15 s. On this branch, the formatter throws Unexpected token (3:14) and the ESLint parser throws Failed to parse TSRX file: Unexpected token (3:14).
  • Local runs: These all pass:
    • vitest tsrx-utils, tsrx-analyze, prettier-plugin, eslint-parser, eslint-plugin, language-server, typescript-plugin, and tsrx-mcp: 58 files, 3,270 tests
    • tsrx-react, tsrx-preact, tsrx-solid, tsrx-vue, and tsrx-hono: 2,982 tests
    • pnpm typecheck, pnpm format:check, and pnpm changeset:check

A patch changeset for @tsrx/core is included.

🤖 Generated with Claude Code


Note

Medium Risk
Targets core parser control flow for @switch arms; behavior change is limited to previously hanging truncated input, with broad regression tests and AST parity checks on complete sources.

Overview
Fixes an infinite parse loop when source ends inside an @switch @case or @default arm (typical while typing in the editor). The arm body loop now stops at end-of-input like @if/@for/@{ bodies, so the following expect(tt.braceR) surfaces Unexpected token at EOF for the missing } in default, collect, and loose modes instead of hanging the language server, formatter, ESLint parser, and build tooling.

#parseJSXSwitchCaseConsequent no longer falls through to raw-text parsing at EOF (which re-read empty text and never advanced). Non-text tokens in an arm are always handled as setup statements via parseStatement, which errors at EOF. Default mode behavior for an unclosed element in an arm is unchanged; collect/loose now error at the missing } after recovering past the tag.

Adds parse_in_worker (worker thread + timeout/heap cap) and parser tests that assert EOF diagnostics across modes, unclosed-tag vs missing-} ordering, and that every prefix of a complex @switch template returns. Includes a @tsrx/core patch changeset.

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

The loop over an `@case`/`@default` body stopped only at `}`. At the end of
the input, `#parseJSXSwitchCaseConsequent` fell through to
`#parseJSXSwitchCaseRawText`, which read an empty text node without moving,
so the parser looped forever: in every parse mode for an unterminated arm,
and in `collect` and `loose` mode (the editor's) when the arm also held an
unclosed element.

The arm loop now stops at `tt.eof`, like a code block body, so the
`expect(tt.braceR)` after it reports `Unexpected token` at the end of the
input, as an unterminated `@if` or `@for` body does. The raw-text fallback
at the end of `#parseJSXSwitchCaseConsequent` was reachable only at the end
of the input and is removed, so the helper reads a statement there, which
throws instead of spinning.

The tests parse in a worker thread that is terminated after a timeout, since
a vitest timeout cannot interrupt a synchronous loop.

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

Development

Successfully merging this pull request may close these issues.

[Bug]: tsrx-core: Parser hangs when the input ends inside an @switch case body

1 participant