Repository navigation
fix(core): read modifiers before declarations, intrinsic, import type options and escaped type as TypeScript does (#715, #716, #717, #718, #719, #720) - #788
Merged
Conversation
…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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ow-ups # Conflicts: # packages/tsrx-prettier-plugin/src/parse.js # packages/tsrx/src/plugin.js
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
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.
This was referenced Sep 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Six
@tsrx/coreparser 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 incollect/loosemode and thrown by a strict parse (CHECKER_LEVEL_ERRORS). All six causes are in@sveltejs/acorn-typescript1.0.13 (or in TSRX's overrides that stand in for it), worked around inTSRXPlugin(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'sparseExpressionStatementhands any name totsParseExpressionStatement, and withoutpreserveParensthe name in(abstract)is the expression. Fix: atsParseExpressionStatementoverride 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#wordWasParenthesizedguards go.#716:
intrinsicandinterfaceafter a type alias's=type T = intrinsic;gave a reference to a type namedintrinsic, andtype T = interface;theTSIntrinsicKeyword, which compiled totype T = intrinsic;. Fix: thetsParseTypeAliasDeclarationoverride is now acorn-typescript's method with TypeScript's check: the wordintrinsicnot followed by.is the keyword (TS1260 when escaped);interfaceis a type name, as in TypeScript (whose checker reports TS1214 for it).@tsrx/prettier-pluginprintedTSIntrinsicKeywordas/* Unknown: TSIntrinsicKeyword */, so it now printsintrinsic.#717: the options of an import type
TSRX's
tsParseImportTypeoverride (from sveltejs/acorn-typescript#110) read them as any object literal, soimport("m", { foo: {} }),import("m", { with: {}, foo: 1 })andimport("m", { ...o })compiled. Fix:#parseImportTypeOptionsreads TypeScript's grammar (parseImportType,parseImportAttributes):{,withorassertwithout 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 sameObjectExpressionas before.#718:
typewritten with an escape in an import or export clauseimport \u0074ype { a } from "m";,import { \u0074ype a } from "m";,export { \u0074ype a } from "m";andimport \u0074ype a = require("m");failed at the name aftertype. TypeScript compares the identifier's text there and reports nothing. Fix: TSRX'sparseImporttakes an escapedtypetoken;parseImportSpecifierandparseExportSpecifierclearcontainsEscon a leading escapedtypebefore acorn-typescript'sts_isContextualcheck; atsParseImportEqualsDeclarationoverride does whatparseExportdoes forexport import \u0074ype a = ….#719: modifiers before a declaration
TypeScript's
isDeclaration/parseModifiersread any run of modifiers before a declaration and its checker (checkGrammarModifiers) reports the first one the declaration can't take. acorn-typescript reads onlyabstractanddeclare, so these failed in every mode:Fix: at the start of a statement (
parseStatement) and afterexport(shouldParseExportStatement,parseExportDeclaration),#readDeclarationModifiersreads the modifiers by TypeScript's rules (same line exceptstatic;export, andexport defaultbefore a class, function or interface). Only lists acorn-typescript doesn't read go this way (#readsModifiedDeclaration): a class member's modifier, a repeated one,exportafter another one, a misplacedasync,declarewithasyncor before an import orusing, a secondstatic. The declaration is parsed with the modifiers the tree keeps (export,default,declare,abstracton a class,asyncon a function); beforeexportthe modifiers go toparseExport(#pendingModifiers). Afterwards#modifierError, a port ofcheckGrammarModifiersfor statement-level declarations (includingreportObviousModifierErrors' 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 secondstaticis TS1146Declaration expected.and an escaped modifier TS1260, both parser errors, in every mode. An exported global augmentation also gets #721's TS2668.tsrx-prettier-pluginrefuses 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 defaultand decorators,declarewas read as the exported expression. Fix: #614'sparseExportDefaultDeclarationoverride readsdeclare, withabstractbefore or after it, as the modifiers of an ambient class with an optional name (as typescript-estree gives). Without decoratorsdeclarestays the exported value (TS1005), as in TypeScript.Overrides
plugin.jstsParseExpressionStatement(new)tsParseTypeAliasDeclaration(#721's, now a full copy)intrinsiccheck differs.tsParseImportType(#110's),#parseImportTypeOptions(new)parseImport(TSRX's),parseImportSpecifier,parseExportSpecifier,tsParseImportEqualsDeclaration(new)typetoken withcontainsEsc.parseStatement,shouldParseExportStatement,parseExportDeclaration,parseExportDefaultDeclaration(extended)#readsModifiedDeclarationtakes; every other list keeps acorn-typescript's reading and #721's overrides.parseExportDefaultDeclaration(#614's, extended),canHaveLeadingDecorator(refactored)declare/abstractandclasson one line.Upstream first: none of these is covered by an open issue or PR, and #110 doesn't change them (its
tsParseImportTypereads any expression atom as the options). I filed sveltejs/acorn-typescript#151 to #156, each with a repro using onlyacornand@sveltejs/acorn-typescript.Behavior changes to review
All follow TypeScript 5.9.3 (
ts.createSourceFile(…, ScriptKind.TSX)for parser errors, aProgramfor checker errors).Code that parses today and is now rejected in every mode (TypeScript's parser errors):
(abstract) class A {},(declare) class A {},(declare) function f(): void;,(type) T = 1;,(namespace) N {},(module) "m" {},(global) {}.type T = intrinsicis read as a type name, andtype T = interfaceas theintrinsickeyword #716: anything after the keyword but;(type T = intrinsic[];,type T = intrinsic | X;,type T = intrinsic<X>;), andtype T = \u0069ntrinsic;(TS1260).withorassert#717: import type options other than{ with: … }/{ assert: … }around attributes:{ foo: {} },{},{ ...o },{ "with": {} },{ \u0077ith: {} },{ with() {} },{ with: x },{ with: {}, foo: 1 }, and attributes that aren'tname: value({ a },{ a() {} },{ [a]: 1 },{ ...a },{ 1: "x" }).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 keptdeclareandasync, and still does.A different AST for code that parses today (#716):
type T = intrinsic;givesTSIntrinsicKeywordinstead of aTSTypeReference(same compile output), andtype T = interface;aTSTypeReferenceinstead ofTSIntrinsicKeyword(it now compiles totype 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)andimport("m",)report TS1005'{' expected.instead ofUnexpected token, andimport("m", { with })':' expected.instead ofUnexpected keyword 'with'; a secondstaticisDeclaration expected.at TypeScript's position; an escaped class member modifier before a declaration is TS1260; #696'sexport abstract export class A {}is TS1030'export' modifier already seen.at the secondexport, 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 inloosemode, 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.tsrxfiles, 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, nowTSIntrinsicKeyword, 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:type T = intrinsicis read as a type name, andtype T = interfaceas theintrinsickeyword #716);typewritten with an escape in an import or export clause fails to parse, where TypeScript reads it astype#718, [Bug]: tsrx-core:export default @dec declare class A {}fails to parse #720), and 36 failed and are now recorded when collecting ([Bug]: tsrx-core: A class member modifier, or a repeated or misplaced modifier, before a declaration fails in every mode, where TypeScript reports it from its checker #719);export declare async function f(): void;);type T = intrinsicis read as a type name, andtype T = interfaceas theintrinsickeyword #716, [Bug]: tsrx-core: The options of an import type accept any object literal, where TypeScript expectswithorassert#717);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 adeclare namespaceand 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 firstcheckGrammarModifierserror, 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.
compileandcompile_to_volar_mappingsforreact,preact,solid,vueandhono:type T = intrinsic;,let x: import("m", { with: { type: "json" } });andexport default @dec declare class A {}compile as written;public class A {},declare async function f(): void;andabstract export class A {}throw TypeScript's error fromcompileand give it fromcompile_to_volar_mappings.Formatters. The valid forms format as Prettier's
typescriptparser does (type Uppercase<S extends string> = intrinsic;, import type attributes,import type { a } from "m";for the escapedtype,export default @dec declare abstract class A {}).tsrx-prettier-pluginrefuses the modifiers the tree leaves out, and prints modifiers out of order in order (abstract export class A {}printsexport abstract class A {}; Prettier'stypescriptparser drops theexportthere);@tsrx/prettier-pluginthrows the checker errors, as it parses strictly.Tests.
upstream-parser-workarounds.test.js: a describe per upstream issue ([Bug]: Conditional host attribute spreads throw when the condition is false #151 to Restart TSRX Server [vscode] #156), 17 tests. Without the fix, 11 fail; the other 6 guard the forms that stay names or expressions.parser.test.js: the two import types expect TypeScript's'{' expected.; fix(core): readabstractanddeclarebefore a line break as TypeScript does, and report the words afterexportthat start no declaration (#608, #651) #696's list losesexport abstract export class A {}, now in [Bug]: tsrx-core: A class member modifier, or a repeated or misplaced modifier, before a declaration fails in every mode, where TypeScript reports it from its checker #719's 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-vueandtsrx-hono: 3,179 passed, 60 skipped.prettier-known-failures.jsondoesn't change.pnpm typecheck: every step passes.tsrx-tscisn't linked after--ignore-scripts, sovite-plugin-hono'stypecheck:testsran throughpackages/typescript-plugin/dist/tsc.jsafter building@tsrx/typescript-plugin.types/parse.d.tsgains the acorn-typescript methods the overrides use.pnpm formatandpnpm changeset:check: clean.The changeset is a
patchfor@tsrx/coreand@tsrx/prettier-plugin.tsrx-prettier-pluginis private.Downstream. The only AST change for code that parses today is
TSIntrinsicKeywordfortype T = intrinsic;(and back forinterface). Ripple (packages/tsrx-ripple) and Octane (packages/octane/src/compiler) read a type alias'stypeAnnotationonly 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:
abstractbefore a declaration inside a block reports TS1242, where TypeScript reports TS1184 #783:abstractbefore a declaration inside a block reports TS1242 (fix(core): readabstractbefore any declaration,export default interfaceacross a line break,type as,export globaland escaped keywords as TypeScript does (#697, #698, #699, #700, #709) #721's overrides), where TypeScript reports TS1184.import typeon a namespace import alias, which TypeScript reports from its checker #785:import type A = B.C;throws when collecting, where TypeScript reports TS1392 from its checker ([Bug]: tsrx-core:typewritten with an escape in an import or export clause fails to parse, where TypeScript reads it astype#718 makes the escaped form do the same).class public {},let interface = 1;,publicbefore a line break) throws when collecting, where TypeScript reports TS1212 to TS1214.Same causes, not changed here:
import \u0064efer * as ns from "m";fails: TSRX'sparseImportdeclines an escapeddeferas it didtype; TypeScript reads it asdefer.import("m", { with: { #a: 1 } })) is TS1478 here; TypeScript's parser takes it.@dec public class A {}, TS1044) still fail withLeading decorators must be attached to a class declaration., andexport default @dec declare interface I {}(TS1206) still fails when collecting.export defaultafter other modifiers withdeclare, after a secondexportor inside a namespace; andexportbefore an import after other modifiers. These keep failing as before.export using x = y;(TS1491 from TypeScript's checker) keeps acorn'sUnexpected token, the ECMAScript grammar's error.abstractbefore any declaration,export default interfaceacross a line break,type as,export globaland escaped keywords as TypeScript does (#697, #698, #699, #700, #709) #721's TS1242.'declare' modifier already seen.); class members keep acorn-typescript'sDuplicate modifier: 'x'..intrinsicoutside its own declaration files; TSRX doesn't, and TypeScript reports it on the generated code.Not changed, per the open questions: TS2668 isn't added for
export declare module "m" {}(nor fordeclare 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 = intrinsicAST shape changes toTSIntrinsicKeyword.Overview
Aligns
@tsrx/coreparsing 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 useintrinsicas a keyword (not confused withinterface). Import-type second arguments must be{ with: … }or{ assert: … }, not arbitrary object literals. Unicode-escapedtypein import/export clauses is recognized as type-only. A large#readDeclarationModifiers/#parseModifiedStatementpath greedily reads declaration modifiers and raises TypeScript checker-style errors (recorded in collect/loose, thrown in strict)—includingexport declare async function.export default @dec declare [abstract] classparses as a default ambient class.Formatters:
@tsrx/prettier-pluginprintsTSIntrinsicKeywordasintrinsic.tsrx-prettier-pluginrejects 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.