@@ -786,6 +786,9 @@ export function TSRXPlugin(config) {
786786 #commentContextId = 0 ;
787787 #collect = false ;
788788 #loose = false ;
789+ // Set while `parseExportDeclaration` reads the declaration after `export`,
790+ // so `tsParseAbstractDeclaration` can tell that call from a statement.
791+ #readingExportDeclaration = false ;
789792 // Set while `parseVarStatement` or `parseVar` lets `const` declarators omit
790793 // the initializer (see `#parseConstWithoutInitializer`).
791794 #collectingConstWithoutInitializer = false ;
@@ -4367,14 +4370,41 @@ export function TSRXPlugin(config) {
43674370 // `parseExport` has just read `export`.
43684371 const export_start = this . lastTokStart ;
43694372 this . #checkExportDeclarationStart( export_start ) ;
4370- const declaration = super . parseExportDeclaration ( node ) ;
4371- if (
4372- declaration ?. type !== 'VariableDeclaration' &&
4373- ! ( /** @type {{ id?: unknown } | null } */ ( declaration ) ?. id )
4374- ) {
4375- this . raise ( export_start , DECLARATION_OR_STATEMENT_EXPECTED ) ;
4373+ const reading = this . #readingExportDeclaration;
4374+ this . #readingExportDeclaration = true ;
4375+ try {
4376+ const declaration = super . parseExportDeclaration ( node ) ;
4377+ if (
4378+ declaration ?. type !== 'VariableDeclaration' &&
4379+ ! ( /** @type {{ id?: unknown } | null } */ ( declaration ) ?. id )
4380+ ) {
4381+ this . raise ( export_start , DECLARATION_OR_STATEMENT_EXPECTED ) ;
4382+ }
4383+ return declaration ;
4384+ } finally {
4385+ this . #readingExportDeclaration = reading ;
43764386 }
4377- return declaration ;
4387+ }
4388+
4389+ // UPSTREAM(sveltejs/acorn-typescript#132): remove once a release includes the fix
4390+ /**
4391+ * `export abstract` consumes `abstract` and then parses a class. An
4392+ * interface is parsed with `abstract` set and no error. Any other
4393+ * identifier (`let`, `async`, `enum`, `type`, `namespace`, `module`,
4394+ * `global`) hits `unexpected` at `abstract`, so a collecting parse kept
4395+ * no tree. Decline for those, and for the interface: the word is already
4396+ * consumed, and the declaration parses without the modifier.
4397+ * `#checkExportDeclarationStart` has reported TS1242. Outside an export,
4398+ * including `abstract class` and `abstract interface`, acorn-typescript's
4399+ * parse stays.
4400+ * @param {AST.Node & { abstract?: boolean } } node
4401+ */
4402+ tsParseAbstractDeclaration ( node ) {
4403+ if ( this . type === tt . _class || ! this . #readingExportDeclaration) {
4404+ // @ts -ignore — acorn-typescript method, absent from the parser type
4405+ return super . tsParseAbstractDeclaration ( node ) ;
4406+ }
4407+ return undefined ;
43784408 }
43794409
43804410 // UPSTREAM(sveltejs/acorn-typescript#132): remove once a release includes the fix
@@ -4393,10 +4423,13 @@ export function TSRXPlugin(config) {
43934423 * class A {}` gave the plain exported declaration. Report the word
43944424 * before it's lost, as TypeScript's parser does:
43954425 *
4396- * - `abstract` before a function, a variable or an import declaration is
4397- * a modifier of that declaration, which TypeScript's checker reports
4398- * (TS1242). A strict parse throws it; a collecting parse records it and
4399- * parses the declaration without the modifier.
4426+ * - `abstract` before a function (including `async function`), a variable
4427+ * (`const`, `let`, or `var`), an import, or an `interface`, `enum`,
4428+ * `type`, `namespace`, `module`, or `global` declaration is a modifier
4429+ * of that declaration, which TypeScript's checker reports (TS1242). A
4430+ * strict parse throws it; a collecting parse records it and parses the
4431+ * declaration without the modifier. `class` is the declaration
4432+ * `abstract` may modify.
44004433 * - `type` after `declare`, or before `default` or `@`, starts a type
44014434 * alias, whose name is missing (TS1003, or TS1359 for a reserved word),
44024435 * or on the next line (TS1142). Before `=`, TypeScript expects the
@@ -4439,15 +4472,19 @@ export function TSRXPlugin(config) {
44394472 if ( alias && ! is_identifier ( next . type ) ) this . #raiseIdentifierExpected( next ) ;
44404473 }
44414474 if ( line_break ) return ;
4442- // What `tsParseDeclaration` reads after the word.
4475+ // What `tsParseDeclaration` keeps after the word. A name keeps `type`,
4476+ // `namespace`, and `module`. `abstract` is kept only before `class`:
4477+ // `async`, `let`, `interface`, `enum`, `type`, `namespace`, `module`,
4478+ // and `global` are identifier tokens too, and treating them as a name
4479+ // left `abstract` unreported.
44434480 if (
4444- is_identifier ( next . type ) ||
4481+ ( value !== 'abstract' && is_identifier ( next . type ) ) ||
44454482 ( value === 'abstract' && next . type === tt . _class ) ||
44464483 ( value === 'module' && next . type === tt . string )
44474484 ) {
44484485 return ;
44494486 }
4450- if ( value === 'abstract' && this . #isDeclarationAfterModifier( next . type , ahead ) ) {
4487+ if ( value === 'abstract' && this . #isDeclarationAfterModifier( next , ahead ) ) {
44514488 this . raise ( word . start , ABSTRACT_MODIFIER_NOT_ALLOWED ) ;
44524489 return ;
44534490 }
@@ -4470,26 +4507,62 @@ export function TSRXPlugin(config) {
44704507 }
44714508
44724509 /**
4473- * Whether the token `ahead` tokens on, of type `type`, starts a
4474- * declaration that TypeScript's parser reads after a modifier, and that
4475- * can follow `export` here: a function, a variable, or an import
4476- * declaration (not `import(…)` or `import.meta`). TypeScript's parser
4477- * reads no declaration at decorators after a modifier, and the tree has
4478- * no place for a second `export`.
4479- * @param {acorn.TokenType } type
4510+ * Whether `token`, `ahead` tokens on, starts a declaration that
4511+ * TypeScript's parser reads after a modifier, and that can follow
4512+ * `export` here. TypeScript's checker reports `abstract` before one
4513+ * (TS1242): a function (including `async function`), a variable
4514+ * (`const`, `let`, or `var`), an import (not `import(…)` or
4515+ * `import.meta`), or `interface`, `enum`, `type`, `namespace`,
4516+ * `module`, or `global`. `let`, `async`, and those words are
4517+ * identifier tokens; `let` has no keyword token. A name after
4518+ * `interface`, `enum`, `type`, `namespace`, or `module` has to be on
4519+ * the same line, as in TypeScript's `isDeclaration`. TypeScript's
4520+ * parser reads no declaration at decorators after a modifier, and the
4521+ * tree has no place for a second `export`.
4522+ * @param {Parse.LookaheadState } token
44804523 * @param {number } ahead
44814524 */
4482- #isDeclarationAfterModifier( type , ahead ) {
4525+ #isDeclarationAfterModifier( token , ahead ) {
4526+ const type = token . type ;
44834527 if ( type === tt . _function || type === tt . _const || type === tt . _var ) return true ;
4484- if ( type !== tt . _import ) return false ;
4485- const after = this . lookahead ( ahead + 1 ) . type ;
4486- return (
4487- after === tt . string ||
4488- after === tt . star ||
4489- after === tt . braceL ||
4490- ! ! after . keyword ||
4491- Parser . acornTypeScript . tokenIsIdentifier ( after )
4492- ) ;
4528+ if ( type === tt . _import ) {
4529+ const after = this . lookahead ( ahead + 1 ) . type ;
4530+ return (
4531+ after === tt . string ||
4532+ after === tt . star ||
4533+ after === tt . braceL ||
4534+ ! ! after . keyword ||
4535+ Parser . acornTypeScript . tokenIsIdentifier ( after )
4536+ ) ;
4537+ }
4538+ if ( token . containsEsc ) return false ;
4539+ const after = this . lookahead ( ahead + 1 ) ;
4540+ const is_identifier = Parser . acornTypeScript . tokenIsIdentifier ;
4541+ // `let` is a keyword when a binding follows, on this line or the
4542+ // next (`export abstract let x` / `export abstract let\nx`).
4543+ if ( token . value === 'let' ) {
4544+ return (
4545+ is_identifier ( after . type ) || after . type === tt . braceL || after . type === tt . bracketL
4546+ ) ;
4547+ }
4548+ const line_break = regex_line_break . test ( this . input . slice ( token . end , after . start ) ) ;
4549+ // `async function` is a function declaration only without a line break.
4550+ if ( token . value === 'async' ) return ! line_break && after . type === tt . _function ;
4551+ if ( line_break ) return false ;
4552+ if (
4553+ type === tstt . interface ||
4554+ type === tstt . enum ||
4555+ type === tstt . type ||
4556+ type === tstt . namespace
4557+ ) {
4558+ return is_identifier ( after . type ) ;
4559+ }
4560+ if ( type === tstt . module ) return is_identifier ( after . type ) || after . type === tt . string ;
4561+ // `global { … }` or `global` before a name.
4562+ if ( type === tstt . global ) {
4563+ return after . type === tt . braceL || is_identifier ( after . type ) || after . type === tt . _export ;
4564+ }
4565+ return false ;
44934566 }
44944567
44954568 // UPSTREAM(sveltejs/acorn-typescript#125): remove once a release includes the fix
0 commit comments