Skip to content

fix(core): report '}' expected for a missing closing brace (#583) - #590

Merged
leonidaz merged 5 commits into
mainfrom
fix/core-brace-expected
Sep 25, 2026
Merged

leonidaz merged 5 commits into
mainfrom
fix/core-brace-expected

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #583.

Phase 1 item 1.4 of #511 (decision 15): a missing } is reported as '}' expected, like TypeScript, not as acorn's generic Unexpected token, for template and JavaScript blocks alike. This touches error reporting like #558 (item 1.3), so it was meant to merge after it. #558 is merged now, and this branch includes it (main is merged in, with no rebase).

The rule

The parser reports '}' expected. (TypeScript's TS1005 text) where TypeScript's parser reports it, at the same position, through this.raise. So it is an ordinary syntax error with acorn's (line:column) suffix, pos, and loc:

  • At the end of the input, in every { … } list: function and statement blocks (if/else, loops, try/catch/finally, labeled and plain blocks), switch bodies and case blocks, class bodies and static blocks, object literals and patterns, arrow and function-expression bodies, namespaces and declare module/global, enums, interfaces, type literals and mapped types, import and export lists and with { … }, template literal ${ … }, JSX containers and spreads, and TSRX's @{ … }, @if/@else, @for/@empty, @switch/@case/@default, and @try/@pending/@catch bodies. The position is the end of the input, after trailing whitespace and comments, where acorn's Unexpected token already pointed, so only the message changes.
  • At the token found in place of the }, where TypeScript reports it there too: after the expression of a template literal's ${ … } (value or type), and after the expression of a JSX container ({value}, {...spread}, an attribute's {…}, and TSRX's {name} attribute).
  • Directive bodies left open at the end of the input (@if, @else, @for, @empty, @try, @pending, @catch) used to be taken as closed. With nothing around them (const v = @if (ok) { and then the end of the file) the file parsed in every mode: it compiled, and both formatters printed a } that was never written. Inside an @{ … } the enclosing body reported Unexpected token, inside an element the element was reported unclosed, and an open @try body reported a missing @catch. They all report '}' expected. at the end of the input now.

Everything else keeps its message and position. Where TypeScript reports something else first, the error is unchanged: a missing ) or ], a missing expression ({ a +, { if (x), x = { a:, <div>{, `${), a ; or , expected at the next token (x = [{ a: 1 ];, const o = { a: 1 then the next declaration), a mapped type followed by another member, an import attribute without its comma, and a decorator at the end of a class body (TypeScript: Declaration expected.). The tables in #583 give TypeScript's first parse diagnostic and position for each shape (TypeScript 5.9.3, ScriptKind.TSX), and whether it adds related information pointing at the opening {.

Parse modes. A missing } is a syntax error, not a type-check-level one, so it isn't in #558's CHECKER_LEVEL_ERRORS and throws in strict, collect, and loose mode, like the unterminated @switch arm in #497. Loose mode's recoveries are unchanged: an unclosed tag, including an unclosed <style> (#71), is still recorded and parsed past (or thrown first, when not collecting), and the missing } after it now reads '}' expected. instead of Unexpected token. The only inputs that parsed before and throw now are directive bodies left open at the end of the input, as described above.

Language server. No change needed. It shows the thrown error as a tsrx-compile-error diagnostic from pos: at the end of the document the range is empty, like TypeScript's zero-length diagnostic, and after a container's expression it covers the token found (raisedAt), like TypeScript's. Two new tests cover both. TypeScript's related information (the opening {) isn't reproduced: a CompileError has one position.

Where the change lives

All in TSRXPlugin (packages/tsrx/src/plugin.js), plus three method types in types/parse.d.ts. The new methods are at the end of the class, away from #551's, #555's, and #558's overrides.

  • expect(type): '}' expected. when a } is expected at the end of the input or after a template span's expression (the ${ context is under the current token); when a comma is expected at the end of the input right after an element of a { … } list; and when an expected { is followed by the end of the input (a switch body before its first case, where acorn calls unexpected() itself). Every { that acorn, acorn-typescript, or TSRX expects opens a list.
  • The element parsers of the { … } lists check for the end of the input before reading an element, and record where the element ended for expect's comma check: parseProperty, parseImportSpecifier, parseExportSpecifier, tsParseEnumMember, tsParseTypeMember (one shared #parseBraceListElement wrapper), parseClassElement (not right after a decorator; parseDecorator records where one ended), and parseStatement without a context, which is the next statement of a block, case, or module body. After if (…), else, a loop head, or a label (context set) it keeps Unexpected token, where TypeScript reports a missing expression.
  • TSRX's own template bodies: #parseCodeBlock (@{ … }, checked before the return-statement check now), #parseTemplateControlFlowBlock (the directive bodies, which didn't check at all), and the @switch body when no @case follows. The JSX container and spread expect(tt.braceR) calls go through #expectContainerClosingBrace, which reports at any token.

No upstream issue: this is a message choice, not an acorn or acorn-typescript defect.

Verification

  • Tests. packages/tsrx/tests/utils/closing-brace.test.js parses 84 JavaScript and 34 template shapes at the end of the input, 8 directive bodies that used to parse, and 12 template-span and container shapes with a token after the expression, in strict, collect, and loose mode, in a worker (parse_in_worker). Each checks the message and position. It also checks that an unclosed tag is still thrown first when not collecting and recovered in collect and loose mode, that 38 other syntax errors keep their messages, and that the shapes left out keep theirs (below). Two existing tests of the unterminated @switch arm (fix(core): stop an @switch arm body at the end of the input (#492) #497) now expect '}' expected.. The language server tests check the diagnostic's message, code, and range.
  • AST and error comparison, main (935dfe42) against this branch, strict and parseModule(text, file, { collect: true, errors: [], comments: [], preserveParens: true }), in 4 worker threads with a hard timeout. The inputs are 16,803: the 69 tracked .tsrx files, the input and the output of all 2,040 Prettier snapshot cases in packages/tsrx-prettier-plugin/tests/prettier/ (readSnapshotEntries/readFormatCase), and 12,654 distinct string literals from test files. Without the 253 strings from this PR's own tests:
    • no hangs; no AST changed; no collected errors changed for any input that parses;
    • no .tsrx file and no Prettier input or output changed at all;
    • 85 strict and 90 collect errors changed from Unexpected token to '}' expected., all at the same position;
    • 4 inputs that parsed now throw '}' expected. in both modes: @if (…) { and @for (…) { lines used as grammar and completion fixtures, which end in an open directive body.
    • With this PR's own strings, the only other changes are the shapes the new tests exercise (open @try bodies and directive bodies inside an element, whose missing-@catch and unclosed-tag errors become '}' expected. at the end of the input).
  • vitest run --maxWorkers=4 for tsrx-utils, tsrx-analyze, tsrx-vite, prettier-plugin, tsrx-prettier-plugin, language-server, typescript-plugin, eslint-parser, and the tsrx-react/-preact/-solid/-vue/-hono compilers (which run the shared compile tests): 57 files, 8,655 passed, 7 expected failures, 70 skipped.
  • pnpm typecheck: all 36 steps pass. tsrx-tsc isn't linked after pnpm install --ignore-scripts, so I built @tsrx/typescript-plugin and ran the @tsrx/vite-plugin-hono typecheck:tests step through packages/typescript-plugin/dist/tsc.js.
  • pnpm format:check and pnpm changeset:check pass. Changeset: patch for @tsrx/core.
  • No snapshot elsewhere recorded Unexpected token for a missing }. The toThrow(/Unexpected token/) tests in tsrx-react, tsrx-vue, and prettier-plugin are about other errors and still pass.

Findings

Left out, with their own issues because their causes are elsewhere (section F of #583's tables):

Same cause as this issue, left out because acorn reads them differently before the list can see the end of the input (both keep Unexpected token at the end of the input, a test pins them):

  • x = { get at the end of the input: acorn takes get as a getter's keyword and fails reading its name. TypeScript reads a shorthand property and reports '}' expected.
  • import a from './a.json' with { type: 'json', at the end of the input: acorn-typescript reads the attribute list in one loop, which reads a key after the comma. The same list without the trailing comma reports '}' expected.

Also: @finally, which #583's brief listed, isn't TSRX syntax (@try takes @pending and @catch), so it has no body to report.

🤖 Generated with Claude Code


Note

Medium Risk
Parser error messaging and EOF acceptance change affects all parse modes and previously-valid open directive snippets; scope is diagnostics/parse failure only, not runtime semantics for successfully parsed files.

Overview
The TSRX parser now surfaces missing } as TypeScript’s '}' expected. (TS1005) instead of acorn’s Unexpected token, at the same positions: EOF inside brace-delimited JS/TS/template constructs (@{…}, @if/@for/@switch/@try, blocks, objects, imports, ${…}, JSX {…}, etc.) and on the token after an expression when a container’s closing } is absent.

Behavior fix: standalone directive bodies left open at EOF (e.g. const v = @if (ok) {) no longer parse silently; they error with '}' expected. instead of compiling/formatting with an implicit }.

Implementation is centralized in TSRXPlugin via #raiseClosingBraceExpected, #expectContainerClosingBrace, brace-list tracking in overridden expect / parseProperty / import-export / enum / type members / parseClassElement, plus EOF checks on template code blocks and control-flow bodies. Other syntax errors and unclosed-tag precedence in non-collect modes are unchanged. Tests and a @tsrx/core patch changeset document the rule; language-server tests assert tsrx-compile-error message and range (empty at EOF, token span after containers).

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

leonidaz and others added 4 commits September 25, 2026 13:35
A missing `}` is reported as TypeScript reports it, `'}' expected.`, instead
of acorn's `Unexpected token`, for JavaScript and template blocks alike: at
the end of the input in every `{ … }` list, and after the expression of a
template literal's `${ … }` or an expression container. Unclosed
`@if`/`@for`/`@try` bodies at the end of the input, which parsed before,
report it too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Parse every block shape from #583 in every parse mode and check the message
and position, including directive bodies that used to parse, the unclosed-tag
errors and recovery before a missing `}`, and other syntax errors that keep
their messages. The language server shows the error with an empty range at the
end of the document, and on the token found after a container's expression.

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 ON. A cloud agent has been kicked off to fix the reported issue. You can view the agent here.

Reviewed by Cursor Bugbot for commit 457a169. Configure here.

Comment thread packages/tsrx/src/plugin.js Resolved
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: A missing } is reported as Unexpected token instead of '}' expected, and an unclosed directive body at the end of the input parses

1 participant