Skip to content

fix(core): read modifiers before declarations, intrinsic, import type options and escaped type as TypeScript does (#715, #716, #717, #718, #719, #720) - #788

Merged
leonidaz merged 4 commits into
mainfrom
fix/core-keyword-follow-ups
Sep 26, 2026

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Six @tsrx/core parser follow-ups from #721, following the user's decision for this group: TSRX reports what TypeScript reports and accepts what TypeScript's parser accepts, even where code compiled before, with TypeScript's checker errors recorded in collect/loose mode and thrown by a strict parse (CHECKER_LEVEL_ERRORS). All six causes are in @sveltejs/acorn-typescript 1.0.13 (or in TSRX's overrides that stand in for it), worked around in TSRXPlugin (packages/tsrx/src/plugin.js) and marked // UPSTREAM(sveltejs/acorn-typescript#<n>): remove once a release includes the fix.

The bugs and the fixes

#715: a TypeScript word in parentheses at the start of a statement

(abstract) class A {} compiled as an abstract class, (declare) class A {} as an ambient one and (type) T = 1; as a type alias, while a collecting parse (which keeps the parentheses) failed. acorn-typescript's parseExpressionStatement hands any name to tsParseExpressionStatement, and without preserveParens the name in (abstract) is the expression. Fix: a tsParseExpressionStatement override declines a name that starts after the statement does. Every mode now fails at the token after the parentheses (Unexpected token, TypeScript's TS1005 position). #721's #wordWasParenthesized guards go.

#716: intrinsic and interface after a type alias's =

type T = intrinsic; gave a reference to a type named intrinsic, and type T = interface; the TSIntrinsicKeyword, which compiled to type T = intrinsic;. Fix: the tsParseTypeAliasDeclaration override is now acorn-typescript's method with TypeScript's check: the word intrinsic not followed by . is the keyword (TS1260 when escaped); interface is a type name, as in TypeScript (whose checker reports TS1214 for it). @tsrx/prettier-plugin printed TSIntrinsicKeyword as /* Unknown: TSIntrinsicKeyword */, so it now prints intrinsic.

#717: the options of an import type

TSRX's tsParseImportType override (from sveltejs/acorn-typescript#110) read them as any object literal, so import("m", { foo: {} }), import("m", { with: {}, foo: 1 }) and import("m", { ...o }) compiled. Fix: #parseImportTypeOptions reads TypeScript's grammar (parseImportType, parseImportAttributes): {, with or assert without an escape, :, the attributes in braces (names, keywords or strings, :, any value), an optional comma and }, with TypeScript's messages at its positions (TS1005 'with' expected., ':' expected., '{' expected., ',' expected., '}' expected., TS1478, TS1260). For valid input the tree is the same ObjectExpression as before.

#718: type written with an escape in an import or export clause

import \u0074ype { a } from "m";, import { \u0074ype a } from "m";, export { \u0074ype a } from "m"; and import \u0074ype a = require("m"); failed at the name after type. TypeScript compares the identifier's text there and reports nothing. Fix: TSRX's parseImport takes an escaped type token; parseImportSpecifier and parseExportSpecifier clear containsEsc on a leading escaped type before acorn-typescript's ts_isContextual check; a tsParseImportEqualsDeclaration override does what parseExport does for export import \u0074ype a = ….

#719: modifiers before a declaration

TypeScript's isDeclaration/parseModifiers read any run of modifiers before a declaration and its checker (checkGrammarModifiers) reports the first one the declaration can't take. acorn-typescript reads only abstract and declare, so these failed in every mode:

public class A {}
readonly function f() {}
accessor class A {}
async class A {}
declare async function f(): void;
declare declare class A {}
abstract export class A {}
declare import x from "m";

Fix: at the start of a statement (parseStatement) and after export (shouldParseExportStatement, parseExportDeclaration), #readDeclarationModifiers reads the modifiers by TypeScript's rules (same line except static; export, and export default before a class, function or interface). Only lists acorn-typescript doesn't read go this way (#readsModifiedDeclaration): a class member's modifier, a repeated one, export after another one, a misplaced async, declare with async or before an import or using, a second static. The declaration is parsed with the modifiers the tree keeps (export, default, declare, abstract on a class, async on a function); before export the modifiers go to parseExport (#pendingModifiers). Afterwards #modifierError, a port of checkGrammarModifiers for statement-level declarations (including reportObviousModifierErrors' TS1184 in blocks, TS1038 in an ambient namespace, TS1079 and TS1042 after the loop), gives TypeScript's error; a strict parse throws it, a collecting one records it, and records for each modifier the tree leaves out the error TypeScript reports for it once the first is fixed, so nothing is dropped without an error. A second static is TS1146 Declaration expected. and an escaped modifier TS1260, both parser errors, in every mode. An exported global augmentation also gets #721's TS2668. tsrx-prettier-plugin refuses the errors whose modifier the tree leaves out (TS1044, TS1024, TS1275, TS1042, TS1184, TS1030, TS1079, TS1491/TS1495). TS1029/TS1243 positions for class members are untouched; the new statement-level ones are at the modifier.

#720: export default @dec declare class A {}

After export default and decorators, declare was read as the exported expression. Fix: #614's parseExportDefaultDeclaration override reads declare, with abstract before or after it, as the modifiers of an ambient class with an optional name (as typescript-estree gives). Without decorators declare stays the exported value (TS1005), as in TypeScript.

Overrides

Override in plugin.js tsrx issue Upstream Why it's narrow
tsParseExpressionStatement (new) #715 sveltejs/acorn-typescript#151 Declines only a name that starts after its statement.
tsParseTypeAliasDeclaration (#721's, now a full copy) #716 #152, #147 acorn-typescript's method; only the intrinsic check differs.
tsParseImportType (#110's), #parseImportTypeOptions (new) #717 #153, #110 Only the options after the comma.
parseImport (TSRX's), parseImportSpecifier, parseExportSpecifier, tsParseImportEqualsDeclaration (new) #718 #154 Only a type token with containsEsc.
parseStatement, shouldParseExportStatement, parseExportDeclaration, parseExportDefaultDeclaration (extended) #719 #155, #143 Only the modifier lists #readsModifiedDeclaration takes; every other list keeps acorn-typescript's reading and #721's overrides.
parseExportDefaultDeclaration (#614's, extended), canHaveLeadingDecorator (refactored) #720 #156, #124 Only after decorators, before declare/abstract and class on one line.

Upstream first: none of these is covered by an open issue or PR, and #110 doesn't change them (its tsParseImportType reads any expression atom as the options). I filed sveltejs/acorn-typescript#151 to #156, each with a repro using only acorn and @sveltejs/acorn-typescript.

Behavior changes to review

All follow TypeScript 5.9.3 (ts.createSourceFile(…, ScriptKind.TSX) for parser errors, a Program for checker errors).

Code that parses today and is now rejected in every mode (TypeScript's parser errors):

Code that parses today and is now recorded when collecting, and thrown by a strict parse (#719): export declare async function f(): void; (TS1040 'async' modifier cannot be used in an ambient context.). It kept declare and async, and still does.

A different AST for code that parses today (#716): type T = intrinsic; gives TSIntrinsicKeyword instead of a TSTypeReference (same compile output), and type T = interface; a TSTypeReference instead of TSIntrinsicKeyword (it now compiles to type T = interface; as written, where TypeScript's checker reports TS1214).

Code that failed and now parses: #718's and #720's forms; and, recorded when collecting (a strict parse throws), #719's forms.

Error messages that change (inputs that already failed): import("m", x) and import("m",) report TS1005 '{' expected. instead of Unexpected token, and import("m", { with }) ':' expected. instead of Unexpected keyword 'with'; a second static is Declaration expected. at TypeScript's position; an escaped class member modifier before a declaration is TS1260; #696's export abstract export class A {} is TS1030 'export' modifier already seen. at the second export, as TypeScript reports, instead of TS1128.

Verification

AST comparison. I compared main (b51a77e0) with this branch, parsing each input strictly, with { collect: true, errors: [], comments: [], preserveParens: true } and in loose mode, and comparing the full AST (ranges included), collected errors, comments and thrown messages, in 4 worker threads with a hard timeout. The inputs: the 70 tracked .tsrx files, the 2,040 Prettier snapshot inputs and 2,040 outputs, and 14,478 distinct string literals from test files, this PR's included: 17,653 in all. 94 changed, all intended, and no parse hung: one Prettier snapshot input (typescript/intrinsic.ts, now TSIntrinsicKeyword, with the same output), three existing test strings (the two import types and #696's string above), and 90 of this PR's test strings. By kind:

Against TypeScript. For #719, every list of one or two modifiers (of abstract, accessor, async, declare, private, protected, public, readonly, static, export) before 23 kinds of declaration, at the top level, in a namespace, in a declare namespace and in a function body (8,932 sources), and every list of three at the top level and in a namespace (45,066 sources), each compared with TypeScript's first parser error, or first checkGrammarModifiers error, against the strict throw and the first collected error. Every source this PR changes now matches TypeScript except where another error comes first, listed under findings. For #715 to #718 and #720, 94 forms against TypeScript, typescript-estree and both TSRX modes.

Compile output. compile and compile_to_volar_mappings for react, preact, solid, vue and hono: type T = intrinsic;, let x: import("m", { with: { type: "json" } }); and export default @dec declare class A {} compile as written; public class A {}, declare async function f(): void; and abstract export class A {} throw TypeScript's error from compile and give it from compile_to_volar_mappings.

Formatters. The valid forms format as Prettier's typescript parser does (type Uppercase<S extends string> = intrinsic;, import type attributes, import type { a } from "m"; for the escaped type, export default @dec declare abstract class A {}). tsrx-prettier-plugin refuses the modifiers the tree leaves out, and prints modifiers out of order in order (abstract export class A {} prints export abstract class A {}; Prettier's typescript parser drops the export there); @tsrx/prettier-plugin throws the checker errors, as it parses strictly.

Tests.

Commands.

  • pnpm vitest run --project tsrx-utils --project tsrx-analyze --project prettier-plugin --project tsrx-prettier-plugin --maxWorkers=4: 34 files, 6,575 passed, 17 skipped.
  • tsrx-react, tsrx-preact, tsrx-solid, tsrx-vue and tsrx-hono: 3,179 passed, 60 skipped.
  • prettier-known-failures.json doesn't change.
  • pnpm typecheck: every step passes. tsrx-tsc isn't linked after --ignore-scripts, so vite-plugin-hono's typecheck:tests ran through packages/typescript-plugin/dist/tsc.js after building @tsrx/typescript-plugin. types/parse.d.ts gains the acorn-typescript methods the overrides use.
  • pnpm format and pnpm changeset:check: clean.

The changeset is a patch for @tsrx/core and @tsrx/prettier-plugin. tsrx-prettier-plugin is private.

Downstream. The only AST change for code that parses today is TSIntrinsicKeyword for type T = intrinsic; (and back for interface). Ripple (packages/tsrx-ripple) and Octane (packages/octane/src/compiler) read a type alias's typeAnnotation only to resolve text-primitive types, where neither node resolves, before or after; both print through @tsrx/core, which prints the keyword. No upgrade work is needed.

Findings

Filed upstream (causes of these issues): sveltejs/acorn-typescript#151, #152, #153, #154, #155, #156.

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

Same causes, not changed here:

Not changed, per the open questions: TS2668 isn't added for export declare module "m" {} (nor for declare export module "m" {}, export accessor module "m" {} and the like, where TypeScript reports it first), and TS1029/TS1243 keep their positions for class members.

Closes #715
Closes #716
Closes #717
Closes #718
Closes #719
Closes #720

🤖 Generated with Claude Code


Note

Medium Risk
Changes core parser acceptance rules: previously compiling invalid TypeScript may now error at compile/format time, and type T = intrinsic AST shape changes to TSIntrinsicKeyword.

Overview
Aligns @tsrx/core parsing and formatting with TypeScript for six keyword/modifier edge cases that previously compiled or failed incorrectly.

Parser (plugin.js): Parenthesized soft keywords at statement start (e.g. (abstract) class) are treated as expressions, not declarations. Type aliases use intrinsic as a keyword (not confused with interface). Import-type second arguments must be { with: … } or { assert: … }, not arbitrary object literals. Unicode-escaped type in import/export clauses is recognized as type-only. A large #readDeclarationModifiers / #parseModifiedStatement path greedily reads declaration modifiers and raises TypeScript checker-style errors (recorded in collect/loose, thrown in strict)—including export declare async function. export default @dec declare [abstract] class parses as a default ambient class.

Formatters: @tsrx/prettier-plugin prints TSIntrinsicKeyword as intrinsic. tsrx-prettier-plugin rejects the new modifier diagnostics so invalid modifiers are not silently dropped from output.

Tests and a patch changeset cover the new behavior; several fixes are marked upstream for @sveltejs/acorn-typescript.

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

leonidaz and others added 3 commits September 26, 2026 03:46
…pe options and escaped `type` as TypeScript does (#715, #716, #717, #718, #719, #720)

- A TypeScript word in parentheses at the start of a statement is an
  expression (#715).
- `intrinsic` after a type alias's `=` is the keyword, and `interface` a type
  name (#716); `@tsrx/prettier-plugin` prints the keyword.
- An import type's options are `{ with: … }` or `{ assert: … }` around import
  attributes, with TypeScript's errors for anything else (#717).
- `type` written with an escape in an import or export clause is `type` (#718).
- A class member's modifier, or a repeated or misplaced one, before a
  declaration is read and reported as TypeScript's checker reports it (#719).
- `export default @dec declare class A {}` is an ambient class (#720).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ow-ups

# Conflicts:
#	packages/tsrx-prettier-plugin/src/parse.js
#	packages/tsrx/src/plugin.js

@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.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 3e6ac5a. Configure here.

Comment thread packages/tsrx/src/plugin.js Resolved
@leonidaz
leonidaz merged commit 5508243 into main Sep 26, 2026
9 checks passed
@leonidaz
leonidaz deleted the fix/core-keyword-follow-ups branch September 26, 2026 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment