Skip to content

fix(core): read an async arrow function's optional rest parameter, report a generic arrow function's errors where they are, collect a signature parameter's default, and reject async(a)(b) => and type assertions in arrow parameters (#702, #703, #704, #705, #706) - #728

Merged
leonidaz merged 3 commits into
mainfrom
fix/core-arrow-signature-follow-ups
Sep 26, 2026

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #702
Closes #703
Closes #704
Closes #705
Closes #706

These follow #701: arrow functions and signatures that TSRX read differently from TypeScript's TSX parser. Each cause is in @sveltejs/acorn-typescript or acorn, and each fix is a narrow override in TSRXPlugin marked UPSTREAM(…), as in #701. Two kinds of input that compiled now throw, because TypeScript rejects them (decision 23 in #596; see Behavior change).

The bugs and the fixes

#702: an async arrow function's optional rest parameter

async (...a?: number[]) => a failed with Unexpected token at the ? in every mode. acorn reads an async arrow function's parameters as the arguments of async (…), and acorn-typescript's parseExprList reads a type annotation after a spread there, but not a ? (sveltejs/acorn-typescript#140).

The rest parameter's range stays the spread's, which ends before its type annotation, as for any async rest parameter today (#725).

#703: a syntax error in a generic arrow function

acorn-typescript reads an expression that starts with < as an element first, then as a generic arrow function, and when both failed it threw the element's error. It reads async <T,>(…) => … in a tsTryParseAndCatch that took any error to mean "not an arrow function" and went on to read async < T as a comparison (sveltejs/acorn-typescript#141). So an error in the arrow function's parameters or body was reported as Unexpected token at its type parameters, or after them.

  • #parseGenericArrowFunction wraps both places, #parseMaybeAssign (an expression that starts with <) and parseSubscript (async <). It notes where the type parameters start, tsParseTypeParameters notes where the arrow function's parameters start, and parseArrowExpression records the SyntaxError that arrow function throws. It's thrown instead of acorn-typescript's.
  • parseArrowExpression is only called once the => is read, so this covers the errors from there on: in the body, and the ones the parser raises for the parameters after the => (TS1047, TS2463, [Bug]: tsrx-core: A type assertion in an arrow function's parameters parses with no error, and the output keeps it #706's type assertion).
  • An async generic arrow function's optional rest parameter was the exception: acorn-typescript reads its parameters with parseBindingList inside its tsTryParseAndCatch, where parseBindingListItem threw TS1047, before the =>. For that list, parseBindingListItem now leaves it to parseFunctionBody, which raises it after the => for every arrow function.
const f = <T,>(x: T) => { x = ; };

This is reported as Unexpected token (1:30) at the ;, where TypeScript reports TS1109, instead of Unexpected token (1:10) at the <. The message is acorn's, as for the same mistake anywhere else.

#704: async(a)(b) => 1

acorn's parseSubscripts works out maybeAsyncArrow once, for the async identifier, and passes it to every subscript in its loop, so an => after a later call made the whole chain an async arrow function, and async(a)(b) => 1 compiled to async (b) => 1 (acornjs/acorn#1460). parseSubscript now keeps maybeAsyncArrow only while the base is still the async identifier, which is the first subscript. The chain is a call, and the => after it fails with Unexpected token, where TypeScript reports TS1005 ';' expected..

#705: a parameter's default in a signature (TS2371)

acorn-typescript's tsParseBindingListForSignature rejected an AssignmentPattern with its own message, Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got AssignmentPattern., in every mode. TypeScript's parser reads it, and its checker reports TS2371 A parameter initializer is only allowed in a function or constructor implementation.. #701's override of that method now raises TS2371 at the parameter instead, in every mode. CHECKER_LEVEL_ERRORS lists it, so collecting records it and keeps the default in the tree, as typescript-estree does, and a strict parse throws it. The strict parse still reads the list as acorn-typescript does; only the message changes.

type F = (public x = 1) => void records TS2369 and TS2371 when collecting, both at the modifier, as TypeScript reports them.

#706: a type assertion in an arrow function's parameters

(x as number) => x, (x!) => x, and async ([a satisfies number]) => a parsed in every mode, and the output kept the assertion. TypeScript's parser doesn't read these as arrow functions (TS1005). acorn-typescript's toAssignable accepts an assertion in a binding, where @babel/parser reports it (sveltejs/acorn-typescript#142).

checkLValPattern, which already unwraps type assertions for assignment targets, now raises @babel/parser's Unexpected type cast in parameter position. at an assertion when it checks a binding (a binding type other than BIND_NONE), in every mode. It isn't in toAssignable, as the issue suggested, for two reasons:

  • acorn-typescript's toAssignable gets isBinding: true for an assignment target too (parseMaybeAssignOrigin), and acorn's nested conversions drop preserveTypeScriptWrapper, so it can't tell ([a as T]) => a from [a as T] = arr, which TypeScript accepts.
  • A pattern that a default already made a pattern, ([b as T] = []) => b, isn't converted again, but checkParams checks every parameter with a binding type.

Only an arrow function's parameters, which acorn reads as expressions, can hold an assertion in a binding. An assignment target keeps working: (x as number) = 1, [a as number, b!] = [1, 2], [{ a: o } as { a: unknown }] = [], ({ a: b! } = o).

Behavior change: inputs that parsed and now throw

TypeScript's parser rejects each of these (decision 23). A strict compile, and now collect and loose mode too, throw:

Input Error
async(a)(b) => 1, async(a)[0](b) => 1, async(a)`t`(b) => 1, async!(a) => 1, async<T>(a)(b) => 1 Unexpected token at the => (TypeScript: TS1005 ';' expected. there)
(x as number) => x, (x!) => x, (x!: number) => x, async (x!) => x, async ([a satisfies number]) => a, ({ a: b as string }) => b, ([b as string] = []) => b, <T,>(x as T) => x, (props as Props) => @{ … }, function f(x = (a as T) => a) {} Unexpected type cast in parameter position. at the assertion (TypeScript: TS1005 ';' expected. or ',' expected.)

Inputs that threw before and throw a different error now:

@tsrx/prettier-plugin parses strictly, so it now refuses the behavior change inputs too.

Overrides

Override Issue Upstream What it does
parseSubscript #704 acornjs/acorn#1460 (filed with #701) maybeAsyncArrow only while the base is async.
parseSpread, with parseExprList and #parseAsyncArguments (from parseSubscript) #702 sveltejs/acorn-typescript#140 Takes a ? after a spread among the arguments of async (…), and fails at it when no => follows.
#parseGenericArrowFunction, from #parseMaybeAssign and parseSubscript, with tsParseTypeParameters, parseArrowExpression, and parseBindingListItem (TS1047) #703 sveltejs/acorn-typescript#141 Throws a generic arrow function's own error once it's read past its =>.
tsParseBindingListForSignature (#701's override, now in every mode) #705 sveltejs/acorn-typescript#89 (whether acorn-typescript reports checker diagnostics), and #136 for the collect-mode reading TS2371 for a parameter's default, and keeps it when collecting.
checkLValPattern #706 sveltejs/acorn-typescript#142 Rejects a type assertion in a binding.

I checked #110's head (65376678): it doesn't touch parseExprList, parseSubscript, parseMaybeAssign, tsTryParseGenericAsyncArrowFunction, tsParseBindingListForSignature, or toAssignable, so it fixes none of these.

The formatters

tsrx-prettier-plugin parses with collect: true:

@tsrx/prettier-plugin parses strictly, so it throws for all of these.

Language server

The language server compiles with loose: true. I ran the new collect-mode parses through compile_to_volar_mappings for all five targets (type F = (a = 1) => void, the interface above, type G = (public x = 1) => void, async arrow functions' optional rest parameters, also in a template's setup, and async <T,>({ a }?: T) => a). None fails. The virtual code keeps the default (export type F = (a = 1) => void;), so TypeScript reports its own TS2371 there too, following #558's convention. It leaves out the rest parameter's ?, as for other functions (#556).

Verification

Parse comparison with main (f73b6760). 19,427 sources, 18,453 distinct:

  • the 70 tracked .tsrx files (59 distinct);
  • the 1,548 distinct inputs and 1,841 distinct outputs of the 2,040 Prettier snapshot cases under packages/tsrx-prettier-plugin/tests/prettier/ (readSnapshotEntries/readFormatCase);
  • 15,277 distinct string and template literals from the test files, this PR's included.

Each was parsed strict, with { collect: true, errors: [], comments: [], preserveParens: true }, and with { loose: true }, before and after. That's 116,562 parses in four worker threads, each killed if a parse took over 15 s. None hung. I compared every AST, comment list, and error list, or the thrown message and position.

41 sources changed, and all of them are this PR's new test strings. No .tsrx file, Prettier input or output, or existing test string changed.

I also timed parsing the .tsrx files and Prettier sources, five times in each mode, in alternating processes before and after. The difference was within run-to-run noise.

Tests.

Local runs. These pass:

  • vitest tsrx-utils, tsrx-analyze, prettier-plugin, tsrx-prettier-plugin, tsrx-react, tsrx-preact, tsrx-solid, tsrx-vue, and tsrx-hono (--maxWorkers=4). prettier-known-failures.json is unchanged.
  • 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 (no changes outside this PR's files), 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
Core parser semantics change with new strict errors and shifted diagnostic locations; impact is broad (compile, loose/collect, formatter) but scoped to edge-case syntax and heavily regression-tested.

Overview
Aligns @tsrx/core parsing with TypeScript for async/generic arrows and type signatures via targeted TSRXPlugin overrides (marked UPSTREAM for acorn / acorn-typescript).

Parsing and diagnostics: Optional rest on async (...a?: T[]) => now parses; collect/loose record TS1047 at ? instead of failing at the token. Generic arrow syntax errors (and parameter checker errors after =>) surface at the real site, not at <. Defaults in signatures (type F = (a = 1) => void, etc.) stay in the AST while collect/loose record TS2371 and strict compile throws TypeScript’s message.

Intentional breaks (match TypeScript): async(a)(b) => … no longer rewrites to an async arrow; type assertions in arrow parameters ((x as number) => x, x!, satisfies in params) throw Unexpected type cast in parameter position. Assignment targets like (x as number) = 1 still work.

Tests: Parser, upstream-workaround, and prettier-plugin cases cover the new behavior and formatting of signature defaults / async optional rest.

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

…port a generic arrow function's errors where they are, collect a signature parameter's default, and reject `async(a)(b) =>` and type assertions in arrow parameters (#702, #703, #704, #705, #706)

- An async arrow function's optional rest parameter parses, and TS1047 is
  recorded when collecting and thrown otherwise (#702,
  sveltejs/acorn-typescript#140).
- A generic arrow function read past its `=>` throws its own error instead of
  `Unexpected token` at its type parameters (#703,
  sveltejs/acorn-typescript#141).
- A call after `async (…)` followed by `=>` is a syntax error, as in
  TypeScript (#704, acornjs/acorn#1460).
- A parameter's default in a signature is recorded as TS2371 when collecting
  and thrown otherwise (#705).
- A type assertion in an arrow function's parameters is a syntax error, as in
  TypeScript (#706, sveltejs/acorn-typescript#142).

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 e3388de. Configure here.

Comment thread packages/tsrx/src/plugin.js Resolved
…e members in every mode

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