Skip to content

fix(core): collect decorator, modifier, and rest parameter mistakes that TypeScript reports from its checker (#610, #611, #616, #617) - #647

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

leonidaz merged 5 commits into
mainfrom
fix/core-collect-checker-errors-3

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #610
Closes #611
Closes #616
Closes #617

These are follow-ups to #558 and #618: mistakes that TypeScript's parser accepts and reports only from its checker. As there, 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. A message joins CHECKER_LEVEL_ERRORS in packages/tsrx/src/plugin.js only where the raise site goes on to build the node it builds for valid code; where it can't, a narrow override handles it. main is merged in, with #614 (which touches the same decorator code), #633, #605, #635, and #641.

The bugs and the fixes

#617: a repeated modifier

#616: ? after a binding list element

parseBindingListItem (#618's override) now handles every ? that acorn-typescript would take:

#611: a modifier on a rest parameter

acorn-typescript's parseAssignableListItem reads the modifiers and then parseMaybeDefault, which can't read .... When collecting, #parseRestParameterProperty looks ahead with tsTryParse: if the modifiers are followed by ..., it reads the rest element, records TS1317 A parameter property cannot be declared using a rest parameter. across the modifiers, and returns the rest element as the parameter, without them. typescript-estree rejects a rest parameter property, so there's no ESTree node to mirror, and dropping the modifiers is what the issue suggests. A comma after that rest element is reported as for any rest parameter. Otherwise the lookahead is undone and acorn-typescript reads the item as before. A strict parse still throws Unexpected token at the .... It works after #614's decorators too (constructor(@dec public ...rest)).

#610: decorators that TypeScript reports as TS1206

Which cases are collected, and why. TypeScript's parser takes decorators before any declaration and reports them from its checker. Whether it reports them depends on experimentalDecorators in only two places, and the parser can't know that option:

Decorators TypeScript 5.9.3 Here
before a declaration other than a class: @dec function f() {}, @dec const x = 1;, @dec interface I {}, @dec import …, export @dec function f() {}, export default @dec function f() {}, @dec export function f() {}, @dec export default 1; TS1206 from the checker, with or without experimentalDecorators threw in every mode (@dec export … since #614); now recorded when collecting
on a constructor: class A { @dec constructor() {} } TS1206, either way threw in every mode; now recorded when collecting (CHECKER_LEVEL_ERRORS)
before a statement that isn't a declaration: @dec x;, @dec if (a) {}, export @dec x;, export default @dec 1; TS1146 Declaration expected. from the parser still throws in every mode
in an expression: const y = @dec 1;, const y = @dec function () {}; TS1109 from the parser still throws in every mode
on a method or constructor parameter, rest or not TS1206 only without experimentalDecorators accepted, no error in any mode (unchanged; #614 made the rest parameter match)
on a class expression: const A = @dec class {}; TS1206 only with experimentalDecorators accepted, no error in any mode (unchanged)

So only the rows that TypeScript reports whatever the option says, and that threw here, are collected. The two option-dependent rows stay as they are: accepted, with TypeScript's checker deciding in the editor and tsrx-tsc.

How. acorn-typescript raises Leading decorators must be attached to a class declaration. after reading the decorators, and leaves them on the decorator stack, so the next class parsed would take them. parseDecorators now marks the decorators of a statement (the only caller that allows export after them) and those after export default (#614's parseExportDefaultDeclaration). When raise gets that error for them while collecting, and a declaration keyword follows, it records the error at the first @, where TypeScript reports TS1206, and takes the decorators off the stack. The statement is then parsed without them. #parseDecoratedStatement still throws the original error, at its original position, if the statement turns out not to be a declaration (@dec type;). An at-sign construct after export (@dec export @if (a) { … }) isn't a declaration, so that still throws. #614's check for decorators before export is unchanged; it's now #checkDecoratorsBeforeExport, called from the combined parseDecorators.

ESTree has no place for decorators on a function or a variable, so the tree leaves them out. That's fine for the editor. The formatter refuses it (below).

Overrides

Override Issue Upstream What it does
raise, for a repeated modifier #617 sveltejs/acorn-typescript#129 (filed) Moves the error from the token after the modifier to the modifier.
parseBindingListItem, for an array pattern element #616 sveltejs/acorn-typescript#130 (filed) Throws Unexpected token at the ?.
parseBindingListItem, for a rest parameter #616 none: TSRX's own report Reports TS1047 instead of TS2463's message. #110 drops acorn-typescript's check altogether, so this stays after #110, like #618's TS2463 check in parseFunctionBody.
parseAssignableListItem (#parseRestParameterProperty) #611 sveltejs/acorn-typescript#89 Collect only: reads a rest element after the modifiers.
parseDecorators, parseStatement, parseExportDefaultDeclaration (#dropLeadingDecorators, #parseDecoratedStatement) #610 sveltejs/acorn-typescript#89 Collect only: records the error and drops the decorators before a declaration.

sveltejs/acorn-typescript#122 and #123 are the other position bugs in the same tsParseModifiers loop, and neither covers the repeated-modifier errors, so I filed #129. I checked both new issues against a build of #110's head (65376678): it doesn't fix #129, and for #130 it accepts every array-element ? with no error, because it drops the only check. #130 also covers a type annotation on an array element (const [a: number] = b;), which acorn-typescript accepts in the same method. This PR doesn't reject that (see Findings).

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

The formatters

tsrx-prettier-plugin parses with collect: true. For each new case I compared it with Prettier's typescript parser:

  • Refused through REJECTED_MISTAKES. The tree leaves code out, and Prettier's typescript parser rejects the input too:

    • a second accessibility modifier (it printed public x = 1);
    • a modifier on a rest parameter (it printed constructor(...rest: number[]));
    • decorators before a declaration other than a class (it printed function f() {}).

    Each error is at the same position as Prettier's ((1:18), (1:23), (1:1), (1:8), (1:16)).

  • Formatted as Prettier formats them:

    • readonly readonly x becomes readonly x.
    • function f(...a?: number[]) {} becomes function f(...a: number[]) {}. Prettier's printer doesn't print a rest element's ? either.
  • Formatted, where Prettier's parsers reject it: class A { @dec constructor() {} }. The tree keeps the decorator, so nothing is lost, and it prints unchanged.

@tsrx/prettier-plugin parses strictly, so it throws for all of them, as before. The messages are the new ones for the repeated modifiers and the optional rest parameter.

Language server

The language server compiles with loose: true. I ran every new case through compile_to_volar_mappings for all five targets, and none fails. I checked the React output with a TypeScript 5.9.3 program. Only TSRX reports each of them, because the printer leaves the construct out:

  • the second modifier;
  • the ? of a rest parameter;
  • the modifiers on a rest parameter;
  • the decorators before a declaration.

The exception is the constructor decorator, which both TSRX and TypeScript report (TS1206), following the convention #558 describes.

Verification

Parse comparison with main (4701beb1). 18,064 distinct sources:

  • the 70 tracked .tsrx files;
  • the 2,040 inputs and 2,040 outputs of the Prettier snapshot cases under packages/tsrx-prettier-plugin/tests/prettier/ (readSnapshotEntries/readFormatCase);
  • 13,914 string and template literals from the test files.

Each was parsed strict, with { collect: true, errors: [], comments: [], preserveParens: true }, and with { loose: true }. That's 54,192 parses in four workers, each killed after 15 s, with no hangs. I compared every AST, comment list, and error list, or the thrown message and position.

52 sources changed: 1 Prettier input, 16 existing test strings, and 35 of this PR's new test strings. No .tsrx file changed.

Tests.

Local runs. These pass:

  • vitest tsrx-utils, tsrx-analyze, prettier-plugin, and tsrx-prettier-plugin (--maxWorkers=4), after merging main; prettier-known-failures.json stays empty.
  • pnpm typecheck. tsrx-tsc isn't linked after --ignore-scripts, so I built @tsrx/typescript-plugin and ran vite-plugin-hono's typecheck:tests through packages/typescript-plugin/dist/tsc.js.
  • pnpm format:check and pnpm changeset:check.

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

Findings

Filed upstream:

Filed here, each with a separate cause and not fixed in this PR:

Same cause as the issues here, and not covered:

🤖 Generated with Claude Code


Note

Medium Risk
Changes core parser error handling and AST recovery paths used by the language server and formatter; behavior shifts for invalid TypeScript that previously threw or parsed incorrectly.

Overview
Aligns TSRX parsing with TypeScript checker diagnostics in collect/loose mode (editor, Prettier plugin) while strict compile still throws: repeated accessibility modifiers, leading decorators on non-class declarations (including export forms), modifiers on rest parameters, optional rest parameters, and constructor decorators are recorded and parsing continues instead of hard-failing or mis-attaching decorators to a later class.

Parser fixes in plugin.js: repeated-modifier errors point at the duplicate modifier (not the following token); ...rest? reports TS1047 at ?; const [a?] is a syntax error everywhere; collect-mode paths drop invalid leading decorators and parse the declaration without them via #dropLeadingDecorators / #parseRestParameterProperty.

Prettier plugin treats the new collected errors like existing “would lose AST” mistakes—formatting is refused when output would silently strip the second modifier, parameter-property+rest, or misplaced leading decorators; some checker-only issues still format like Prettier’s TypeScript parser (e.g. duplicate readonly, optional rest ? stripped).

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

leonidaz and others added 5 commits September 25, 2026 16:23
…hat TypeScript reports from its checker (#610, #611, #616, #617)

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:

- A repeated accessibility modifier (#617): "Accessibility modifier already
  seen." joins CHECKER_LEVEL_ERRORS. It and "Duplicate modifier" are now
  raised at the repeated modifier instead of the token after it, in every
  mode (UPSTREAM(sveltejs/acorn-typescript#129)).
- Decorators before a declaration other than a class (#610): when a
  declaration follows, `raise` records acorn-typescript's error at the first
  decorator and takes the decorators off the decorator stack, so no later
  class takes them. Before anything else (`@dec x;`), or in an expression,
  it still throws. Decorators on a constructor join CHECKER_LEVEL_ERRORS.
- A modifier on a rest parameter (#611): parseAssignableListItem reads the
  rest element after the modifiers, records TS1317 at them, and keeps the
  rest element without them.

An optional rest parameter is reported as TS1047 "A rest parameter cannot
be optional.", at the `?`, instead of TS2463's message (#616). A `?` after
an element of an array pattern is a syntax error in every mode, as in
TypeScript (UPSTREAM(sveltejs/acorn-typescript#130)).

tsrx-prettier-plugin refuses the mistakes that leave code out of the tree
and that Prettier's typescript parser rejects: a second accessibility
modifier, a modifier on a rest parameter, and decorators before a
declaration other than a class.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ker-errors-3

#614 overrides parseDecorators (decorators before `export`), parseAssignableListItem
(decorators on a rest parameter), and parseExportDefaultDeclaration (decorators
after `export default`). Combined with this branch:

- parseDecorators keeps #614's check before `export` (now
  #checkDecoratorsBeforeExport) and sets the leading-decorator scope for
  collecting, so `@dec export function f() {}` is recorded at the decorators
  when collecting, like the other decorators before a declaration.
- parseExportDefaultDeclaration reads the decorators after `export default` as
  the declaration's leading decorators: `export default @dec function f() {}`
  is recorded when collecting, and `export default @dec 1;` still throws.
- parseAssignableListItem reads a rest element after parameter property
  modifiers after any decorators too (`constructor(@dec public ...rest)`).
  The comma check after a rest element is shared.
- An at-sign construct after `export` is no declaration, so the error there
  is still thrown when collecting.

#614's tests expect these to throw when collecting too; they now expect the
error to be recorded, as TypeScript reports it from its checker (TS1206).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-level list at the new sites

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 8eeec66 into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-collect-checker-errors-3 branch September 25, 2026 20:50
This was referenced Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment