Skip to content

fix(core): collect more checker-level mistakes, report a disallowed modifier's message, and accept an optional binding pattern in an overload (#587, #588, #550, #557) - #618

Merged
leonidaz merged 2 commits into
mainfrom
fix/core-collect-checker-errors-2
Sep 25, 2026

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #587
Closes #588
Closes #550
Closes #557

These are more of the #415 family: mistakes that TypeScript's parser accepts and reports only from its checker. As in #558, collect and loose mode (the language server, the formatter, and other editor tooling) record them in errors and keep parsing, and a strict parse, which every compile uses, still throws them. Each message joins CHECKER_LEVEL_ERRORS in packages/tsrx/src/plugin.js only after I checked that the raise site goes on to build the node it builds for valid code. Where it can't, a narrow override handles it instead, as #558 did for const without an initializer.

The bugs and the fixes

#587: import or export inside a block

acorn raises 'import' and 'export' may only appear at the top level in parseStatement and then parses the declaration as it would at the top level, so the message goes on the list and nothing else changes. Every form TypeScript accepts there parses to the same node as at the top level: export const, export default, export { … }, export *, export =, export as namespace, import … from, import x = require(…), import type. TypeScript reports them as TS1184, TS1231, TS1232, TS1233, TS1258, and TS1316.

This also fixes the typing case from #583: when a function is missing its }, the next top-level export lands inside it, and a collecting parse now goes on to report '}' expected. at the end, like TypeScript.

#588: const or var with nothing after it, and a bare let

  • const and var. acorn reads a declarator straight after the keyword and throws Unexpected token at whatever follows. When collecting, parseVarStatement now checks whether TypeScript's parser would end an empty declaration list there: the next token can't start a declarator (a name, {, [, or #x), and it is ;, }, or the end of the input, or a line break comes before it (isVariableDeclaratorListTerminator). If so, it finishes the declaration as acorn-typescript's parseVarStatement would, with declarations: [], and records TS1123's Variable declaration list cannot be empty. with no width, right after the keyword, where TypeScript reports it. Anything else takes the old path, so const 1;, const if (a) {}, and var followed by #x still throw in every mode, as TypeScript's parser rejects them too. A const followed by a declarator on the next line is still one declaration.
  • let. A let that no binding follows is an identifier, in acorn and in TypeScript. acorn reports it through raiseRecoverable, which returns once the message, The keyword 'let' is reserved, is on the list. TypeScript reports TS1214 from its checker for every use of let as a name, so the entry covers let;, let.x, and x = let too.

#550: a disallowed modifier's message

acorn-typescript's tsParseModifiers raises a disallowed modifier's error with the error template itself, this.raise(this.start, errorTemplate), so the message was the function's source, at the token after the modifier. raise now calls a template it's given with the modifier (the token just read) and moves the error to the modifier. It's the only raise site in acorn-typescript 1.0.13 that passes a function: InvalidModifierOnTypeMember, InvalidModifierOnTypeParameter, and InvalidModifierOnTypeParameterPositions are passed uncalled only as tsParseModifiers' errorTemplate.

TypeScript reports all three from its checker (TS1070, TS1273, TS1274), so they join the list: raise returns, the loop reads the next modifier, and the node keeps the modifier (accessibility: 'private' on the property signature, in: true on the type parameter). A strict parse throws the message at the modifier:

interface I { private x: number }   'private' modifier cannot appear on a type member. (1:14)
interface I<public T> {}            'public' modifier cannot appear on a type parameter. (1:12)
function f<in T>() {}               'in' modifier can only appear on a type parameter of a class, interface or type alias. (1:11)

The text is acorn-typescript's template. It's TypeScript's text, except that TypeScript's TS1273 and TS1274 have no final period. static on the line before an interface member, which #551 made a modifier, gets the same error, at the static.

#557: an optional binding pattern parameter in an overload

acorn-typescript raises TS2463 while it reads the parameter, before it knows whether a body follows. TypeScript's checker reports TS2463 only for a function with a body.

  • parseBindingListItem now accepts the ? on a parameter that is an object or array pattern, as test: run the parser over the TypeScript compiler corpus sveltejs/acorn-typescript#110 does, and with the same node (optional: true, then typeAnnotation). An element of an array pattern (const [{ a }?] = b), which TypeScript's parser rejects, and an optional rest parameter keep acorn-typescript's error. parseBindingList records the list's closing token so the item can tell a parameter from an array element.
  • parseFunctionBody raises TS2463, with the same message and position as before, for each optional pattern parameter when a body follows: a { … } block, a @{ … } code block, or anything else acorn-typescript would read as a body. #isBodilessSignature is acorn-typescript's own test for a TSDeclareFunction or TSDeclareMethod, without eating the ;. It still skips ambient contexts, as acorn-typescript did, and arrow functions, whose parameters acorn-typescript never checked.

Overload signatures, constructor overloads, and abstract methods now parse in every mode. A function with a body is reported as before: collected in collect mode (#558), thrown in a strict parse.

Overrides

Override Upstream What it does
raise, for a template message sveltejs/acorn-typescript#123 (open, filed for #550) Calls the template with the modifier and raises it at the modifier.
parseBindingListItem sveltejs/acorn-typescript#110 (open), commit "fix: accept an optional binding pattern parameter" Accepts ? on an object or array pattern parameter, with #110's AST.

Once a release includes #110, parseBindingListItem and the closing-token bookkeeping in parseBindingList go. The TS2463 check in parseFunctionBody stays: #110 drops the error everywhere, and this keeps reporting it for a function with a body. #110 also stops reporting [{ a }?] and ...a?; that's in #616.

Nothing is filed with acorn: its errors here are ECMAScript early errors or syntax errors, which acorn is right to raise.

The formatter

tsrx-prettier-plugin parses with collect: true, so without a change it would have formatted these. For an import or export inside a block and a bare let, it prints what Prettier's typescript parser prints. For the other two it didn't:

  • An empty declaration list failed inside the plugin's locEnd (Cannot destructure property 'type').
  • A disallowed modifier was printed without the modifier (interface I { private x: number } became interface I { x: number; }), since Prettier's printer doesn't print it.

Prettier's typescript parser rejects both (typescript-estree throws A variable declaration list must have at least one variable declarator. and the modifier errors), so the plugin now refuses them too, through the same path as broken markup and with #511's Prettier-style position: REJECTED_MISTAKES in src/parse.js. @tsrx/prettier-plugin parses strictly, so it throws as before, with the new messages for #550, and it formats the #557 overloads, keeping the ?.

Language server

The language server compiles with loose: true. I ran every new case through compile_to_volar_mappings for all five targets: none fails, and the React output was checked with a TypeScript 5.9.3 program:

ESLint's core rules run on the new ASTs, including declarations: [], without a rule failing. Only the two rules that already fail on any .tsrx file, no-div-regex and no-dupe-args, fail, because @tsrx/eslint-parser returns no tokens.

Verification

A patch changeset for @tsrx/core is included. tsrx-prettier-plugin is private.

Findings

New issues:

Same root cause as the issues here, and not covered:

  • for (var; ;) and for (const of x) still throw when collecting. TypeScript reports TS1123 from its checker for both. This PR handles only statements, the typing case in [Bug]: tsrx-core: Collect mode throws for const or var with no declarator and a bare let, which TypeScript reports from its checker #588; a for head needs parseForStatement and TypeScript's of lookahead.
  • let as a binding name (var let, class let {}, import let from 'a', function f(let) {}) still throws when collecting. The message is now acorn's next error for the same name, Binding let in strict mode, instead of The keyword 'let' is reserved, at the same position. TypeScript reports TS1212, TS1213, and TS1214 from its checker.
  • An arrow function's optional pattern parameter (({ a }?: T) => a) isn't reported in any mode, as before, since acorn-typescript reads arrow parameters as expressions. TypeScript's checker reports TS2463 for it.
  • TS2463 is now raised before the body instead of while its parameter is read. A strict parse therefore throws an error from later in the parameter list first: class A { constructor(private { a }?: T) {} } throws A parameter property may not be declared using a binding pattern., and function f({ a }?: T, b: ) {} throws Unexpected token. When collecting, TS2463 is recorded after those errors. No source in the comparison has both.

🤖 Generated with Claude Code


Note

Medium Risk
Touches core parser error recovery and Prettier integration; behavior changes for invalid TS in collect mode and when formatting, but strict compile paths remain throwing for these mistakes.

Overview
Extends collect/loose parsing so more TypeScript checker-only mistakes are recorded while parsing continues (strict compile still throws), matching editor and language-server behavior.

Parser (@tsrx/core): Adds checker-level handling for import/export inside blocks, bare let, empty const/var lists (TS1123 at the keyword), and illegal modifiers on type members/parameters or misplaced in/out. raise now evaluates acorn-typescript’s template errors so disallowed modifiers report real messages at the modifier, not function source. Optional binding-pattern parameters ({ a }?: T) parse in bodiless signatures (overloads, abstract methods); implementations with a body still get TS2463 via parseFunctionBody.

Prettier plugin: After a collecting parse, REJECTED_MISTAKES turns empty declaration lists and disallowed-modifier diagnostics into SyntaxErrors (like broken markup), because Prettier’s TypeScript parser/printer would reject or drop those constructs. Other diagnostic-only issues (e.g. import in a block, bare let) still format.

Tests cover parser AST/errors, closing-brace recovery, upstream workarounds, and formatter accept/refuse cases. Patch changeset for @tsrx/core.

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

…odifier's message, and accept an optional binding pattern in an overload (#587, #588, #550, #557)

In collect and loose mode the parser records these mistakes, which
TypeScript's parser accepts and reports only from its checker, and keeps
parsing; a strict parse still throws them:

- An import or export inside a block (#587): acorn's "'import' and
  'export' may only appear at the top level" joins CHECKER_LEVEL_ERRORS,
  and acorn goes on to parse the declaration as at the top level.
- A `const` or `var` with nothing after it, and a bare `let` (#588).
  parseVarStatement ends the declaration with no declarators where
  TypeScript's parser ends an empty list (before `;`, `}`, the end of the
  input, or a line break) and records TS1123 right after the keyword.
  "The keyword 'let' is reserved" joins CHECKER_LEVEL_ERRORS.
- A disallowed modifier (#550): acorn-typescript's tsParseModifiers raises
  the error template itself, at the token after the modifier. `raise` now
  calls the template with the modifier and moves the error to it
  (UPSTREAM(sveltejs/acorn-typescript#123)), and the three messages
  (TS1070, TS1273, TS1274) join CHECKER_LEVEL_ERRORS.

An optional binding pattern parameter (#557) is accepted while reading the
parameter, as sveltejs/acorn-typescript#110 does
(UPSTREAM(sveltejs/acorn-typescript#110)), and parseFunctionBody raises
TS2463 only when a body follows, so overload signatures and abstract
methods parse. An array pattern element and a rest parameter keep
acorn-typescript's error.

tsrx-prettier-plugin refuses an empty declaration list and a disallowed
modifier, as Prettier's typescript parser does, instead of printing
without the modifier or failing inside the printer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 2c964db into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-collect-checker-errors-2 branch September 25, 2026 19:48
leonidaz added a commit that referenced this pull request Sep 25, 2026
Resolves the conflicts with #618 in plugin.js, keeping both sides: the
OPTIONAL_BINDING_PATTERN_PARAMETER constant next to
UNEXPECTED_LEADING_DECORATOR, and #618's parseBindingListItem after this
branch's parseExportDefaultDeclaration, parseDecorators and
parseAssignableListItem, which both sides added at the same place.

parseAssignableListItem now uses #618's #bindingListClose to leave a
decorated `...` in an array pattern an error, as on main, instead of
accepting it like the other decorated array elements (#609). Tests cover
errors inside a decorated rest parameter, which keep their message and
position, and a decorated optional pattern parameter in an overload.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment