Skip to content

fix(core): collect type-check-level errors in collect mode instead of throwing (#415) - #558

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

leonidaz merged 4 commits into
mainfrom
fix/core-collect-checker-errors-415

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #415

This is item 1.3 of Phase 1 in #511 (decision 14). Item 1.4, reporting '}' expected for a missing }, touches error reporting as well and follows in a separate PR.

The rule

Some mistakes are accepted by TypeScript's parser and reported only by its checker. In collect and loose mode (the language server, the formatter, and other editor tooling) the parser now records these in errors and keeps parsing. It already did this for a redeclared variable. A strict parse, which is what every compile does, still throws each of them, with the same message and position as before. Any error not on the list still throws in every mode.

raise and raiseRecoverable both go through a single allow-list, CHECKER_LEVEL_ERRORS in packages/tsrx/src/plugin.js. The comment above it states the rule. Before this change raiseRecoverable matched only has already been declared and Argument name clash, and raise wasn't overridden at all. acorn-typescript routes both through acorn's base raiseRecoverable, which skipped TSRX's override, and that's why type 'T' has already been declared. was thrown.

The allow-list

Each message went on the list only after I read the code that raises it and checked that parsing continues correctly when raise returns: the node gets finished, no token is skipped, and nothing loops. For every row, TypeScript 5.9.3 gives no syntax diagnostic and reports the code shown from its checker.

Message (exact, or pattern) Example TypeScript Raised by
Identifier '…' has already been declared (optional ., optional #) let a; let a;, duplicate imports and private names TS2451, TS2300 acorn declareName, class body; acorn-typescript declareName
type '…' has already been declared. type T = 1; type T = 2; TS2300 acorn-typescript declareName
Argument name clash function f(a, a) {} TS2300 acorn checkLValSimple (already collected)
Export '…' is not defined export { missing }; TS2304 acorn parseTopLevel
Optional chaining cannot appear in left-hand side a?.b = c; TS2779 acorn toAssignable, checkLValSimple
The only valid meta property for import is 'import.meta' import.source('x'); TS18061 acorn parseImportMeta
'new.target' can only be used in functions and class static block const x = new.target; TS17013 acorn parseNew
'super' keyword outside a method super(); TS2337 acorn parseExprAtom (raise)
super() call outside constructor of a subclass super() at top level or in a base class TS2337, TS2335 acorn parseExprAtom (raise, same site)
Comma is not permitted after the rest element function f(...a,) {} TS1013, TS1014 acorn; needs special handling, see below
Abstract methods can only appear within an abstract class. class A { abstract m(): void; } TS1244 acorn-typescript parseClassElement
Initializers are not allowed in ambient contexts. declare class A { x = 1; }, declare let x = 1; TS1039 acorn-typescript parseClassField, parseVarStatement
'…' modifier must precede '…' modifier. / '…' modifier cannot be used with '…' modifier. readonly public x, static abstract x TS1029, TS1243 acorn-typescript tsParseModifiers
Duplicate modifier: '…'. readonly readonly x TS1030 acorn-typescript tsParseModifiers
Private elements cannot have an accessibility modifier ('…'). private #x = 1; TS18010 acorn-typescript parseClassField
Private elements cannot have the 'abstract' modifier. abstract accessor #s; TS18019 acorn-typescript parseClassField
A binding pattern parameter cannot be optional in an implementation signature. function f({ a }?: T) {} TS2463 acorn-typescript parseBindingListItem
Duplicated key in attributes with { type: 'json', type: 'x' } none (TypeScript doesn't report it) acorn-typescript parseWithEntries

Private elements cannot have the 'abstract' modifier. and super() call outside constructor of a subclass aren't rows in #415's table. The first is what decorator-auto-accessors-*.ts fails on once the accessibility error is collected. The second comes from the same parseExprAtom site as super();, and it also rejects super() in a class without extends.

Sites handled without raise returning

  • A comma after a rest element. acorn raises this at the comma and then expects the list to close, so returning would just fail with Unexpected token on the comma.
    • A trailing comma (...a,), ,], ,}) is recorded and skipped in #collectCheckerLevelError, the same way acorn-typescript already skips one in ambient contexts. A trailing comma after a rest parameter in an ambient context (declare function f(...a: T[],)) is valid TypeScript, so it's still skipped silently, as before.
    • For a parameter after the rest element, function fn5(x: string, ...y: any[], z: string); in functionOverloadErrorsSyntax.ts, acorn leaves the list. When collecting, parseBindingList is acorn's loop with one change: the rest branch records the error and keeps reading elements. Without collecting it calls acorn's own method.
  • const without an initializer (export const v: string;, TS1155). acorn calls unexpected() in parseVar. When collecting, parseVarStatement and parseVar (for a for head) let const declarators omit the initializer, except in an ambient context, where they already could. parseVarId runs acorn's own check at that point: for a name it records TypeScript's message, 'const' declarations must be initialized., and for a pattern (const { a };) it throws acorn's Unexpected token at the same position as before.
  • #x in obj outside a class (TS18016). acorn calls unexpected() unless checkPrivateFields is off. When collecting, the constructor turns it off, and parsePrivateIdent records Private field '#x' must be declared in an enclosing class for a private name outside any class, which is the error acorn raises for obj.#x there. One consequence: a class that uses #y without declaring it isn't reported by the parser when collecting any more. TypeScript reports it as TS2339, and the editor shows that diagnostic.
  • await in a namespace (TS1308). acorn-typescript gives a namespace body acorn's class static block scope flag, which makes it a var scope. As a result acorn reads await there as an identifier and throws Cannot use await in class static initialization block, and returning from that raise would leave 42 as an unexpected token. When collecting, canAwait looks through namespace scopes. It only does that when acorn's own canAwait says no. finishNode then records TypeScript's message for an await expression, a for await loop (TS1103), or an await using declaration (TS2852), at the await keyword. The flag values are acorn internals that neither package exports, so they're constants in plugin.js, and the tests fail if they change.
  • Modifier positions. acorn-typescript raises the order and compatibility errors at the modifier's column, read as an offset (Modifier order and compatibility errors are reported at the modifier's column instead of its offset sveltejs/acorn-typescript#122, filed for this PR). A collected error is recorded at the modifier (this.lastTokStart). The strict message is unchanged, including its wrong location.

Two more safeguards:

  • An error recorded while acorn-typescript tries a parse and then abandons it is rolled back with that attempt (parseEffects.willAppend).
  • An error raised twice at the same position with the same message is recorded once. acorn checks an optional-chaining target in both toAssignable and checkLValSimple.

Upstream

Language server: duplicate diagnostics

The language server compiles with loose: true (compile_to_volar_mappings). It shows collected errors as TSRX diagnostics next to TypeScript's diagnostics on the virtual code. Before this change, each of these mistakes was a fatal compile error, and the TypeScript diagnostic filter drops every TypeScript diagnostic while a fatal error exists, so the editor showed one TSRX error. Now the file compiles, so both show up. I checked each row with @tsrx/react's compile_to_volar_mappings and a TypeScript 5.9.3 program over the generated code:

  • Both reported: a redeclared type (TS2300, twice), abstract in a concrete class (TS1244), an ambient initializer (TS1039), static abstract (TS1243), private #x (TS18010), abstract #x (TS18019), a parameter after a rest parameter (TS1014), export { missing } (TS2304), a?.b = c (TS2779), import.source (TS18061), new.target (TS17013), super() (TS2337), await in a namespace (TS1308), #x in obj (TS18016), and const without an initializer (TS1155).
  • Only TSRX reports it: readonly public x and readonly readonly x (the printer writes public readonly x and readonly x), a trailing comma after a rest parameter (the printer drops it), an optional binding pattern (the printer drops the ?, [Bug]: tsrx-core: Optional binding pattern parameters lose their ? in compiled and editor output #556), and a duplicate import attribute (TypeScript doesn't check it).

This is the convention let a = 1; let a = 2; already follows: TSRX's Identifier 'a' has already been declared next to TypeScript's TS2451, twice. Nothing in the language server removes TSRX errors that TypeScript also reports. Suppressing TSRX's copy would be a separate change to the language server, and it would lose the errors in the last group, so I left the convention as it is.

Verification

A patch changeset for @tsrx/core is included.

Findings

Merged with #551 and #555

main is merged in, with #551 (Phase 1 item 1.1) and #555 (item 1.2). The only conflict was prettier-known-failures.json, which I regenerated. plugin.js and types/parse.d.ts merged cleanly. The modifier errors work with #551's tsParseModifier and tsParseModifiers overrides: static abstract, abstract static, readonly public, and readonly readonly are recorded at the modifier. static on one line and abstract x on the next is recorded at abstract. class C { static static a() {} } throws Unexpected token in every mode, as on main: TypeScript's parser rejects it as well, so it isn't a checker-level error.

🤖 Generated with Claude Code


Note

Medium Risk
Changes core parser error handling for collect/loose tooling paths across many syntax edge cases; strict compile behavior is preserved but editor/formatter parsing semantics shift materially.

Overview
In collect and loose mode (language server, Prettier, and other editor tooling), the parser now records a broad set of TypeScript checker-level mistakes in errors and keeps building an AST, instead of aborting on acorn/acorn-typescript parse errors that TypeScript only reports from its checker. Strict compiles are unchanged and still throw the same messages as before.

The behavior is driven by a CHECKER_LEVEL_ERRORS allow-list in plugin.js, wired through overridden raise / raiseRecoverable, plus targeted recovery for cases where a bare raise return would break parsing (rest parameters after a rest element, const without an initializer, await in a namespace, #private outside a class, deduped error recording, and ambient trailing commas).

Tests add a large matrix in parser.test.js (worker parses with AST + positions), extend parse_in_worker_with_ast, update parse.d.ts, and drop many Prettier known failures now that invalid-but-checker-level snippets format successfully.

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

… throwing (#415)

In `collect` and `loose` mode, `raise` and `raiseRecoverable` record the
errors that TypeScript's parser accepts and reports only from its checker
(`CHECKER_LEVEL_ERRORS` in plugin.js) and keep parsing, as the parser already
did for a redeclared variable. A strict parse still throws every one of them,
and every other error still throws.

Sites where the raising code can't continue get narrow overrides instead:

- a comma after a rest element: a trailing comma is skipped (as
  acorn-typescript does in ambient contexts), and `parseBindingList` keeps
  reading elements after a rest element
- `const` without an initializer (`parseVarStatement`, `parseVar`,
  `parseVarId`), recorded with TypeScript's TS1155 message
- `await` in a namespace, which acorn-typescript marks as a class static
  block: `canAwait` looks through namespaces, and `finishNode` records
  TypeScript's message for an await expression, `for await`, or `await using`
- `#x in obj` outside a class: private-field checks are off when collecting,
  and `parsePrivateIdent` records a private name outside any class

Errors recorded while acorn-typescript tries a parse it then abandons are
rolled back with it, and an error acorn raises twice at one position is
recorded once. The modifier order and compatibility errors are recorded at the
modifier, not at acorn-typescript's column (sveltejs/acorn-typescript#122).

All 59 Prettier format tests that failed on these now pass.

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.

Stale Bugbot comment from a previous run.

Comment thread packages/tsrx/src/plugin.js Resolved
…omma and the closing paren

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Regenerated prettier-known-failures.json: the 7 cases #511 skips remain.

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 4cf9d1a. Configure here.

Comment thread packages/tsrx/src/plugin.js Resolved
@leonidaz
leonidaz merged commit c9469b3 into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-collect-checker-errors-415 branch September 25, 2026 17:34
@github-actions github-actions Bot mentioned this pull request Sep 25, 2026
This was referenced Sep 25, 2026
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 throws on mistakes that TypeScript reports only as type-check diagnostics, even with collect: true

1 participant