Repository navigation
fix(core): stop an @switch arm body at the end of the input (#492) - #497
Merged
Merged
Conversation
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]>
This was referenced Sep 25, 2026
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.
Closes #492
Cause
The loop over an
@case/@defaultbody,while (this.type !== tt.braceR), stopped only at}. At the end of the input,#parseJSXSwitchCaseConsequentskipped its statement branch (which excludedtt.eof) and fell through to#parseJSXSwitchCaseRawText(). That read an empty text node fromthis.start, which was already the end of the input, so the token stayedtt.eofand the loop never ended.@case 1: { <section>), the default mode threw the unclosed-tag error first.collectandlooseonly record that error and parse on to the end of the input, so they looped forever.looseis what the editor parses with on every keystroke.Fix
tt.eof, like#parseCodeBlockBody, the loop for@{ … }and@if/@for/@trybodies. Thethis.expect(tt.braceR)after it then reports the missing}asUnexpected tokenat the end of the input. An unterminated@ifor@forbody gives the same error, in every mode.#parseJSXSwitchCaseConsequentcould only be reached at the end of the input, so it's gone. The helper now parses a statement there, andparseStatementthrows 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 throwUnexpected token (3:14)instead of hanging.…@switch (mode) {\n @case 1: {Unexpected token (3:14)…@switch (mode) {\n @default: {Unexpected token (3:15)…@case 1: {\n <section>Unclosed tag '<section>'. … (4:15)…@case 1: {\n <section>Unexpected token (4:15)…@if (mode) {\n <section>(for comparison)Unexpected token (3:15)Unexpected tokenat the end of the input is also how the parser reports a missing}after@if (ok) {,@for (…) {,@switch (x) {,@{, and a plainif (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,@trybodies) already stops attt.eof.@switchloop over arms keepswhile (this.type !== tt.braceR). At the end of the input,#eatJSXSwitchCaseClauseKeyword()finds no label andthis.unexpected()throws. Addingtt.eofthere would skip that error and close the switch without its}.parseTemplateBodyreturns attt.eof.jsx_parseAttributethrows at the end of the input.jsx_readToken'swhile (true)returnseofor raisesUnterminated JSX contentsat the end of the input.#templateRawTextEnd,#switchCaseLabelStart,#parseJSXSwitchCaseRawText, and the rest) and the CSS parser's loops inparse/style.jsare bounded by the input length.I also parsed truncated inputs to test this empirically. The inputs were every
.tsrxfile 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.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 incollect, and 259 inloose. Every one of those prefixes ends after an@caseor@defaultlabel, and each one throwsUnexpected tokenon 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:@case,@default, after a statement, after an element, in the second arm, in a value-position@switch, and in an@switchinside a plain TSX element) reportUnexpected tokenat the end of the input in all three modes.Unexpected token (4:15)incollectandloose.@switcharms (setup statements,@for,@if, a fragment, a value-position@switch) returns in all three modes, and the whole component parses.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. Againstorigin/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
origin/main(e9274468): I parsed 12,159 distinct sources in all three modes, each parse in a worker killed after 5 s: the 69.tsrxfiles, 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 onorigin/mainand now throwUnexpected tokenat 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, intsrx-mcp/tests/authoring.test.js,tsrx/tests/analyze/styles.test.js, andtsrx/tests/utils/parser.test.js, plus this PR's new test inputs.packages/language-server/tests/setup.jsdoes, with the TSRX language plugin, the compile-error diagnostic plugin, and the React fixture, which compiles inloosemode. I then asked for diagnostics on a file ending in@case 1: {. Onorigin/main,getDiagnosticsdidn'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).origin/main, both hung on the same file and were killed after 15 s. On this branch, the formatter throwsUnexpected token (3:14)and the ESLint parser throwsFailed to parse TSRX file: Unexpected token (3:14).tsrx-utils,tsrx-analyze,prettier-plugin,eslint-parser,eslint-plugin,language-server,typescript-plugin, andtsrx-mcp: 58 files, 3,270 teststsrx-react,tsrx-preact,tsrx-solid,tsrx-vue, andtsrx-hono: 2,982 testspnpm typecheck,pnpm format:check, andpnpm changeset:checkA
patchchangeset for@tsrx/coreis included.🤖 Generated with Claude Code
Note
Medium Risk
Targets core parser control flow for
@switcharms; 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@caseor@defaultarm (typical while typing in the editor). The arm body loop now stops at end-of-input like@if/@for/@{bodies, so the followingexpect(tt.braceR)surfacesUnexpected tokenat EOF for the missing}in default,collect, andloosemodes instead of hanging the language server, formatter, ESLint parser, and build tooling.#parseJSXSwitchCaseConsequentno 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 viaparseStatement, which errors at EOF. Default mode behavior for an unclosed element in an arm is unchanged;collect/loosenow 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@switchtemplate returns. Includes a@tsrx/corepatch changeset.Reviewed by Cursor Bugbot for commit 6ed8ef2. Bugbot is set up for automated code reviews on this repo. Configure here.