Skip to content

fix(core): parse anonymous and decorated default-exported classes as declarations, and decorators on a rest parameter (#194, #533, #564) - #614

Merged
leonidaz merged 5 commits into
mainfrom
fix/core-default-class-decorators
Sep 25, 2026
Merged

leonidaz merged 5 commits into
mainfrom
fix/core-default-class-decorators

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes three parser bugs around default-exported classes and decorators that come from @sveltejs/acorn-typescript 1.0.13. Each is worked around by overriding the responsible acorn-typescript method in TSRXPlugin (packages/tsrx/src/plugin.js), marked // UPSTREAM(sveltejs/acorn-typescript#<n>): remove once a release includes the fix, with the AST of the upstream fix where one exists.

The bugs

  • [Bug]: parser rejects anonymous export default class implements and export default abstract class #194. export default class implements I {} failed with The keyword 'implements' is reserved, and export default abstract class {} (also with implements, extends or type parameters) with Unexpected token. acorn-typescript skips implements in parseClassId only when isStatement is falsy, but export default class passes 'nullableID'. Its export default abstract class path passes true, which requires a name.
  • [Bug]: core reads export default @dec class as a class expression and rejects export default @dec abstract class #533. export default @dec class B {} parsed as a ClassExpression, so B wasn't a module binding and the class didn't end the statement, and export default @dec abstract class {} failed. acorn-typescript's parseExportDefaultDeclaration leaves @ to acorn, which parses an expression. The issue's last row, @dec export default (class {});, hung the decorators on the parenthesized class expression. The cause there is different: decorators written before export were never checked against what follows, so the next class parsed took them, and with no class they were dropped (@dec export function f() {} compiled to a function without them).
  • [Bug]: core rejects a decorator on a rest parameter #564. m(@a ...rest: unknown[]) {} failed with Unexpected token. acorn's parseBindingList checks for ... before parseAssignableListItem, where acorn-typescript reads a parameter's decorators.

Overrides

Override in plugin.js tsrx issue Upstream Why it's narrow
parseClassId (extended) #194 sveltejs/acorn-typescript#113 (my open PR) Adds #113's change as it is: where the name is optional (isStatement !== true), implements starts the heritage clause and id is null. The #421 part (contextual keywords as names, sveltejs/acorn-typescript#110) is unchanged. A class statement still rejects implements as a name.
parseExportDefaultDeclaration (new) #194, #533 sveltejs/acorn-typescript#113 for abstract (also in #110); sveltejs/acorn-typescript#124 (filed) for decorators Handles only @ (not TSRX's @{, @if, @for, @switch, @try, which stay expressions) and abstract class; everything else goes to super. After @ it calls acorn-typescript's own parseDecorators(), which already rejects anything but a class, then parseClass(node, 'nullableID'), as acorn does for class.
parseDecorators (new) #533, last row sveltejs/acorn-typescript#125 (filed) Calls super, and only when export follows the decorators looks at the declaration after export / export default: class, abstract class, declare [abstract] class (not after default), or more decorators (checked when they're read, as today; TSRX's @if, @{ and the other at-sign constructs aren't decorators). Anything else raises acorn-typescript's existing Leading decorators must be attached to a class declaration. at the declaration.
parseAssignableListItem (new) #564 sveltejs/acorn-typescript#126 (filed) Only for an item that starts with @. The decorators are read first; before ... they go on the RestElement (except in an array pattern, where TypeScript takes no decorators and ... stays an error, using #618's #bindingListClose), otherwise the item is parsed by super and gets them where acorn-typescript puts them (the parameter, or a parameter property's parameter), so the AST of every other decorated parameter is unchanged. Because the list takes the rest element for an ordinary one, a comma after it is reported as for an undecorated rest parameter: thrown in a strict parse, recorded when collecting (as the existing parseBindingList override does), and allowed as a trailing comma in an ambient context. parseBindingList isn't touched.

Upstream first: sveltejs/acorn-typescript#113 is my open PR for #194's cause, and the override mirrors it. sveltejs/acorn-typescript#110 contains the same abstract change, and also parses export default @dec abstract class C {}, but as a ClassExpression (in parseExprAtom, fixture decorators_export_default_abstract). #533 asks for the declaration that TypeScript and typescript-estree produce, so the override doesn't mirror #110 there, and sveltejs/acorn-typescript#124 says that #110's fixture would change. I checked a build of #110's head (65376678): none of the three new upstream issues is fixed there.

#117 and #118. They're about the range of a class whose decorators come before export (#118 starts it at class instead of the decorator), not about #533's node type; #118 leaves the ClassExpression difference for after #110. This PR doesn't adopt #118's range: node ranges don't change anywhere (the comparison below found no start/end change). A decorated default-exported class still starts at its first decorator, which locStart in @tsrx/prettier-plugin and tsrx-prettier-plugin count on, and @dec export default class {} still starts at the decorator before export.

#564: accepted, not a collected error

TypeScript's parser accepts a decorator on a rest parameter. Its checker reports TS1206 Decorators are not valid here. only without experimentalDecorators: with it, a method's or constructor's decorated rest parameter has no diagnostic (checked with TypeScript 5.9.3), exactly like a decorated ordinary parameter. The parser here already takes decorators on ordinary parameters (and on function parameters, where TypeScript always reports TS1206) without any error. So a decorated rest parameter now parses the same way: no error in any mode, nothing to add to CHECKER_LEVEL_ERRORS, and a strict compile doesn't throw. Whether the decorator is allowed stays with TypeScript's checker, which sees the file through the editor and tsrx-tsc.

Behavior changes to review

All match TypeScript and typescript-estree.

Formatters

  • tsrx-prettier-plugin retyped a decorated default-exported ClassExpression as a ClassDeclaration in its adapter. The parser now produces the declaration, and a ClassExpression directly under export default is always parenthesized, so the block is removed.
  • @tsrx/prettier-plugin: two comments that said a decorated default-exported class parses as a ClassExpression are updated. No code change: it already printed that class as a declaration, and its tests pass unchanged.
  • Both format the new forms (export default class implements I {}, export default @dec abstract class {}, m(@a ...rest: unknown[]), …) stably and like Prettier's typescript parser.

Verification

AST comparison. Every input parsed on main (03de6568, after #602, #615, #595, #618, #633 and #605) and on this branch, strict and with { collect: true, errors: [], comments: [], preserveParens: true }, comparing the full AST (ranges included), collected errors, comments, and thrown messages:

  • the 70 tracked .tsrx files
  • the 2,040 Prettier snapshot inputs and 2,040 outputs under packages/tsrx-prettier-plugin/tests/prettier/ (readSnapshotEntries / readFormatCase)
  • 13,467 distinct string and template literals from the repository's test files

Out of 17,617 inputs, 24 changed, all intended, and no error, thrown message, or range changed. No parse hung.

Change Inputs
export default @dec class …: ClassExpression → ClassDeclaration 13: js/decorators-export.snap :: after_export.js (input and output), and 11 strings in prettier-plugin/src/index.test.js
const X = class implements …: id absent → null 11: 10 strings in prettier-plugin/src/index.test.js, 1 in upstream-parser-workarounds.test.js

The new strings in upstream-parser-workarounds.test.js change as the tests describe.

Compile output. For every target (react, preact, solid, vue, hono), compile and compile_to_volar_mappings give the same code before and after for the inputs that parsed before (a decorated default-exported class, a class expression with implements, a component module with one). The inputs that used to throw now compile. Parameter decorators, rest or not, don't appear in any compiled output because esrap 2.3.2 doesn't print them; that's #606, fixed in esrap 2.3.10.

Tests. packages/tsrx/tests/utils/upstream-parser-workarounds.test.js gets 18 tests in four describes, one per upstream cause, all through the parse worker (parse_in_worker_with_ast), strict and collecting:

Without the fix, 11 of the 18 fail; the other 7 guard behavior that worked before (or, for the optional pattern, that #618 added). Bugbot found that an at-sign construct after export (@dec export default @if (a) { … }) slipped past the check for decorators before export; 392785b fixes that and adds three cases.

  • pnpm vitest run --project tsrx-utils --project tsrx-analyze --project prettier-plugin --project tsrx-prettier-plugin --maxWorkers=4: 33 files, 5,714 passed, 17 skipped (with main at 03de6568). prettier-known-failures.json doesn't change (UPDATE_KNOWN_FAILURES=1). The target compiler projects weren't run locally, since their output doesn't change.
  • pnpm typecheck: every step passes. vite-plugin-hono's typecheck:tests ran through packages/typescript-plugin/dist/tsc.js, since tsrx-tsc wasn't linked in the new worktree.
  • pnpm format: clean.

A patch changeset for @tsrx/core. @tsrx/prettier-plugin only has comment changes, and tsrx-prettier-plugin is private.

Findings

Filed upstream (each with a repro using only acorn and @sveltejs/acorn-typescript, checked against #110's head):

Filed here, separate causes, not fixed in this PR:

Same cause, noted here: with the parser change, a class or function expression directly under export default in @tsrx/prettier-plugin is always a parenthesized one, so isParenthesizedDefaultExport's source check is now redundant; it's left as it is.

Closes #194
Closes #533
Closes #564

🤖 Generated with Claude Code

…declarations, and decorators on a rest parameter (#194, #533, #564)

Four parser bugs caused upstream in @sveltejs/acorn-typescript 1.0.13, each
worked around by overriding the responsible method in TSRXPlugin, marked
`UPSTREAM(sveltejs/acorn-typescript#<n>)`, with the AST of the upstream fix:

- #194: `parseClassId` skips `implements` wherever the class name is
  optional, with `id: null`, and `parseExportDefaultDeclaration` parses
  `export default abstract class` with an optional name. Upstream:
  sveltejs/acorn-typescript#113 (also #110 for the abstract part).
- #533: `parseExportDefaultDeclaration` reads the decorators after
  `export default` and parses `class` or `abstract class` as a declaration,
  so `export default @dec class B {}` is a ClassDeclaration that binds `B`
  and `export default @dec abstract class {}` parses. Node ranges don't
  change. Upstream: sveltejs/acorn-typescript#124.
- #533 (last row): `parseDecorators` checks that decorators written before
  `export` are followed by an exported class, and reports them otherwise with
  the existing "Leading decorators must be attached to a class declaration."
  error, instead of dropping them or moving them onto a class expression.
  Upstream: sveltejs/acorn-typescript#125.
- #564: `parseAssignableListItem` reads the decorators before `...` and hangs
  them off the RestElement, as typescript-estree does. Like any parameter
  decorator this is not an error: TypeScript's checker decides (TS1206 unless
  experimentalDecorators). Upstream: sveltejs/acorn-typescript#126.

tsrx-prettier-plugin no longer retypes a decorated default-exported class
expression as a declaration, which the parser now produces.

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.

Autofix Details

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Leading decorators slip past TSRX exports
    • parseDecorators now rejects @if, @{, @for, @switch, and @try after a leading decorator's export, so those expressions no longer leave the decorator on the stack for a later class.

Create PR

Or push these changes by commenting:

@cursor push c68456c9ab
Preview (c68456c9ab)
diff --git a/.changeset/parser-default-class-decorators.md b/.changeset/parser-default-class-decorators.md
--- a/.changeset/parser-default-class-decorators.md
+++ b/.changeset/parser-default-class-decorators.md
@@ -19,8 +19,9 @@
   failing with `Unexpected token`.
 - Decorators written before `export` must be followed by an exported class,
   as TypeScript requires. Before anything else (`@dec export function f() {}`,
-  `@dec export const A = class {}`, `@dec export default (class {})`) they used
-  to be dropped or moved onto a class expression inside; they are now an error,
+  `@dec export const A = class {}`, `@dec export default (class {})`,
+  `@dec export default @if (a) { … }`) they used to be dropped or moved onto a
+  class expression inside or a following class; they are now an error,
   `Leading decorators must be attached to a class declaration.`, as they already
   were before a statement that isn't a class.
 - A rest parameter can have decorators (`m(@dec ...rest: T[]) {}`), like any

diff --git a/packages/tsrx/src/plugin.js b/packages/tsrx/src/plugin.js
--- a/packages/tsrx/src/plugin.js
+++ b/packages/tsrx/src/plugin.js
@@ -3763,6 +3763,11 @@
 			 * With no class, as in `@dec export function f() {}`, they were dropped.
 			 * Report them at the exported declaration with acorn-typescript's error
 			 * for leading decorators before something else (TypeScript's TS1206).
+			 * An `@` that opens `@if`, `@{`, `@for`, `@switch`, or `@try` is an
+			 * expression, not a decorator. `parseExportDefaultDeclaration` parses
+			 * those without calling `parseDecorators`, so reject them here;
+			 * otherwise the leading decorators stay on the stack and a later class
+			 * takes them.
 			 * @type {Parse.Parser['parseDecorators']}
 			 */
 			parseDecorators(allowExport) {
@@ -3773,8 +3778,16 @@
 				const is_default = next.type === tt._default;
 				if (is_default) next = this.lookahead(++ahead);
 				const declaration_start = next.start;
-				// Decorators after `export` as well are checked when they are read.
-				if (next.type === tstt.at) return;
+				// A later decorator is checked when it is read. An at-sign construct
+				// (`@if`, `@{`, `@for`, `@switch`, `@try`) is an expression, so that
+				// check never runs.
+				if (
+					next.type === tstt.at &&
+					!this.#isCodeBlockStart(next.start) &&
+					!this.#isJSXControlFlowDirectiveAt(next.start)
+				) {
+					return;
+				}
 				if (!is_default && next.type === tstt.declare && !next.containsEsc) {
 					next = this.lookahead(++ahead);
 				}

diff --git a/packages/tsrx/tests/utils/upstream-parser-workarounds.test.js b/packages/tsrx/tests/utils/upstream-parser-workarounds.test.js
--- a/packages/tsrx/tests/utils/upstream-parser-workarounds.test.js
+++ b/packages/tsrx/tests/utils/upstream-parser-workarounds.test.js
@@ -590,6 +590,14 @@
 			['@dec export interface I {}', 'interface'],
 			['@dec export { a };', '{ a }'],
 			['@dec export default abstract;', 'abstract'],
+			['@dec export default @if (a) { <div /> }', '@if'],
+			['@dec export default @{ <div /> }', '@{'],
+			['@dec export default @for (const x of y) { <div /> }', '@for'],
+			['@dec export default @for await (const x of y) { <div /> }', '@for'],
+			['@dec export default @switch (a) { @case 1: { <div /> } }', '@switch'],
+			['@dec export default @try { <div /> } @catch (e) { <span /> }', '@try'],
+			['@dec export @if (a) { <div /> }', '@if'],
+			['@a @b export default @if (a) { <b /> }', '@if'],
 		]);
 		const outcomes = await parseBothModes(cases.map(([source]) => source));
 		for (const [index, [source, declaration]] of cases.entries()) {

You can send follow-ups to the cloud agent here.

Comment thread packages/tsrx/src/plugin.js Resolved
…ecorator (#533)

The check for decorators written before `export` deferred to the later
decorator check when `@` followed `export` or `export default`. An at-sign
construct there (`@if (a) { … }`, `@{ … }`) is an expression, not a
decorator, so nothing checked the leading decorators, and the next class took
them: `@dec export default @if (a) { … }` followed by `class A {}`
decorated `A`. Report them at the construct instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz and others added 2 commits September 25, 2026 15:40
Resolves a conflict in upstream-parser-workarounds.test.js, where main (#602)
and this branch both added tests after the sveltejs/acorn-typescript#121
block: both sets are kept, and the import takes main's parse_in_worker.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]>

@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 c4235bf. Configure here.

Comment thread packages/tsrx/src/plugin.js Resolved
@leonidaz
leonidaz merged commit d746930 into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-default-class-decorators branch September 25, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant