Skip to content

fix(core): read abstract and declare before a line break as TypeScript does, and report the words after export that start no declaration (#608, #651) - #696

Merged
leonidaz merged 4 commits into
mainfrom
fix/core-export-keywords
Sep 25, 2026

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Two @tsrx/core parser bugs around the words after export, both with user decisions: #608 follows TypeScript, and #651 rejects. Both are caused in @sveltejs/acorn-typescript 1.0.13 and worked around by overriding the responsible methods in TSRXPlugin (packages/tsrx/src/plugin.js), marked // UPSTREAM(sveltejs/acorn-typescript#<n>): remove once a release includes the fix.

The bugs and the fixes

#608: abstract or declare before a line break was read as a modifier

TypeScript reads a modifier only before a token on the same line. acorn-typescript's isAbstractClass and isDeclareClass ignored a line break, and its parseExportDeclaration ate declare wherever the next token was. So:

  • export default abstract with class A {} on the next line parsed as one abstract class, the default export. TypeScript reads export default abstract; (exporting a value named abstract) and then a separate class A, which isn't abstract.
  • export declare with a declaration on the next line parsed as an ambient declaration. TypeScript reports TS1128 Declaration or statement expected. at export.
  • Decorators before abstract or declare and a line break (export default @dec abstract, export @dec declare, @dec abstract, @dec declare, each with class A {} on the next line) went to the class. TypeScript reports TS1146 Declaration expected..

Fix:

Statement-level abstract and declare before a line break (without decorators) already parsed as TypeScript does.

#651: a word after export was dropped before a declaration it doesn't start

acorn-typescript's tsParseDeclaration reads abstract, type, namespace or module after export (or export declare) before it checks what follows. When that isn't a class or a name (or a string after module), it declines, and parseExportDeclaration parses the rest as a statement without the word. export abstract function f() {} compiled to export function f() {}, and export type const x = 1; to export const x = 1;, in every mode.

Fix: #checkExportDeclarationStart, called by #650's parseExportDeclaration override before acorn-typescript's, looks at the word and the token after it, and reports what TypeScript reports, where it reports it:

In collect mode the tree for the TS1242 forms has no abstract, so tsrx-prettier-plugin now refuses that error (REJECTED_MISTAKES in packages/tsrx-prettier-plugin/src/parse.js, #618's rule) instead of printing the declaration without the word. Prettier's typescript parser rejects these too. @tsrx/prettier-plugin parses strictly and throws.

TypeScript's error for #650's forms

#650 made export abstract before a line break, export abstract; and the like Unexpected token at the word, to stop a crash. #608 asks for TypeScript's errors, which is TS1128 at export for all of them, so parseExportDeclaration's check now raises Declaration or statement expected. at export. #650's test pins these, and this PR changes the expected message and position.

Overrides

Override in plugin.js tsrx issue Upstream Why it's narrow
isAbstractClass (new) #608 sveltejs/acorn-typescript#137 (filed) super and no line break after abstract.
isDeclareClass (new) #608 sveltejs/acorn-typescript#137 super and no line break after declare, or after abstract in declare abstract class. Its only caller is canHaveLeadingDecorator.
#checkDecoratorsBeforeExport (#614's, extended) #608 sveltejs/acorn-typescript#137, #125 Skips declare and abstract only before a token on the same line.
parseExportDeclaration (#650's, extended) #608, #651 sveltejs/acorn-typescript#132, #137 Calls #checkExportDeclarationStart before super; its check after super now reports TS1128 at export.
#checkExportDeclarationStart (new, called by the one above) #608, #651 sveltejs/acorn-typescript#132, #137 Only looks at export declare before a line break, and at abstract, type, namespace or module where tsParseDeclaration would read and decline it. It raises and never changes what's parsed.

Upstream first: I checked the issues and PRs (gh issue list / gh pr list -R sveltejs/acorn-typescript --state all) and a build of #110's head (65376678). sveltejs/acorn-typescript#132 (filed for #607 in #650) already describes #651's cause, the word tsParseDeclaration reads before it declines, so that part references it. Nothing covered #608's cause, and #110 doesn't change it, so I filed sveltejs/acorn-typescript#137, with a repro using only acorn and @sveltejs/acorn-typescript. #137's fix gives TypeScript's AST for export default abstract on its own line (an ExportDefaultDeclaration of the identifier and a separate ClassDeclaration), which is what the override gives; the rest are errors.

Behavior changes to review

All follow TypeScript 5.9 (ts.createSourceFile(…, ScriptKind.TSX) for parser errors, a Program for checker errors), checked for every form below.

Code that parses today and now means something else (#608). export default abstract followed by class A {} on the next line, also with a comment or a multi-line comment between them and inside declare module: the default export is now the value of abstract, and A is a class of its own that isn't abstract. Compiled and editor output go from export default abstract class A {} to export default abstract; and class A {}. Both formatters print export default abstract;\nclass A {}\n, as Prettier does.

Code that parses today and is now rejected:

  • [Bug]: tsrx-core: abstract or declare before a line break after export is read as a modifier, unlike TypeScript #608, TS1128 at export: export declare followed on the next line by class A {}, abstract class A {}, function f(): void;, const/let/var x: number;, enum E {}, const enum E {}, namespace N {}, module 'm' {}, interface I {} or type T = 1;, also with a comment before the line break and inside declare namespace/declare module.
  • [Bug]: tsrx-core: abstract or declare before a line break after export is read as a modifier, unlike TypeScript #608, Leading decorators must be attached to a class declaration. at the word (TypeScript's TS1146): export default @dec abstract, export default @dec declare, @dec abstract, @dec declare, @dec declare then abstract class A {}, and @dec export declare, each with the class on the next line (when collecting, @dec export declare records the decorators and then fails with TS1128 at export). @dec export default abstract with the class on the next line throws in a strict parse, and when collecting records the error at @dec (TS1206 in TypeScript) and parses export default abstract; and the class without the decorators.
  • [Bug]: tsrx-core: abstract, type, namespace or module after export is dropped before a declaration it does not start #651, TS1242 (a strict parse throws; collecting records it): export abstract function f() {}, export abstract function* g() {}, export abstract const x = 1;, export abstract var x = 1;, export abstract const enum E {}, export declare abstract function f(): void;, export declare abstract const x: number;, export declare abstract const enum E {}, and the same inside declare module/namespace. export abstract import x = y; used to throw 'import' and 'export' may only appear at the top level in a strict parse and record it when collecting; it now reports TS1242 first.
  • [Bug]: tsrx-core: abstract, type, namespace or module after export is dropped before a declaration it does not start #651, TypeScript's parser errors in every mode: export type, export namespace, export module, export declare namespace and export declare module before function, function*, const, var, class, const enum or a decorated class (TS1128); export abstract @dec class A {} and export declare abstract @dec class A {} (TS1128); export type @dec class A {} (TS1003); export declare type before a reserved word or @ (TS1359, TS1003); export \u0061bstract function f() {} (TS1260). When collecting, export type import x = y; and export namespace import x = y; (and module) used to parse with only the top-level error; they're TS1128 now.

Error messages that change (inputs that already failed): export abstract before a line break, ;, a number, the end of the file, {, *, default or import(…), export type/namespace/module before a line break, and export declare abstract/namespace/module before a line break report TS1128 at export instead of Unexpected token at the word or after it, and so does export declare before global {} on the next line, instead of acorn-typescript's 'export declare' must be followed by an ambient declaration.. export type = 1; reports TS1005 '{' expected., export type default … TS1359, export namespace "m" {} TS1003, export declare type with the name on the next line TS1142, export @dec abstract/declare with the class on the next line the decorators error; each at TypeScript's position.

Verification

AST comparison. I compared main (3a33f715) with this branch, parsing each input strictly, with { collect: true, errors: [], comments: [], preserveParens: true }, and in loose mode. The comparison covers 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 (readSnapshotEntries / readFormatCase);
  • 15,039 distinct string literals from test files, this PR's included.

Out of 19,189 inputs, 87 changed, all intended, and no parse hung. None is a .tsrx file or a Prettier snapshot. 12 are #650's test strings for #132 and #608 (the message change above, and the two #608 inputs #650 pinned); 75 are this PR's own test strings.

Against TypeScript. Beyond the tests, I parsed 375 forms after export (each of abstract, type, namespace, module, declare, and declare with each of those, before 30 kinds of statement and declaration, on the same line and the next, plus decorators before and after export) with TypeScript, typescript-estree and both TSRX modes. Every form this PR touches now parses as TypeScript does, or fails with TypeScript's message at its position (see the findings for the ones TSRX reports with acorn's wording, which this PR doesn't change).

Compile output. compile and compile_to_volar_mappings for every target (react, preact, solid, vue, hono) on the #608 and #651 examples: export default abstract on its own line compiles to export default abstract; and class A {} (also inside declare module), and export abstract function f() {} throws TS1242 from compile and gives it as an error from compile_to_volar_mappings.

Formatters. No formatter code changes, apart from the rejected-mistake entry. For #608's valid form, both print what Prettier's typescript parser prints, and the output is stable.

Tests.

Without the fix, 8 of the new or changed parser tests fail; the other 2 guard behavior that already worked.

Commands.

  • pnpm vitest run --project tsrx-utils --project tsrx-analyze --project prettier-plugin --project tsrx-prettier-plugin --maxWorkers=4: 34 files, 6,248 passed, 17 skipped (after merging main at 3a33f715).
  • tsrx-react, tsrx-preact, tsrx-solid, tsrx-vue and tsrx-hono: 3,030 passed, 60 skipped.
  • prettier-known-failures.json doesn't change (UPDATE_KNOWN_FAILURES=1).
  • 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 isDeclareClass.
  • pnpm format and pnpm changeset:check: clean.

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

Findings

Filed upstream:

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

Not filed, as they're wording rather than bugs: where TypeScript reports TS1128 for other tokens after export (export foo, export async before a line break) TSRX keeps acorn's Unexpected token at the token, and export declare before a non-declaration keeps acorn-typescript's 'export declare' must be followed by an ambient declaration.. Inside a declaration that a word does start (export namespace x;, export type x;), TSRX reports acorn's Unexpected token or The keyword 'let' is reserved at the position where TypeScript reports '{' expected. or '=' expected.. export export class A {} and export import { a } from "m"; fail in every mode, where TypeScript's parser reads the second word as a modifier and reports it from its checker (TS1030, TS1191); the tree has no place for an import or a second export there, and export abstract import { a } from "m"; and export abstract export class A {} behave the same way, with TS1242 and TS1128.

Closes #608
Closes #651

🤖 Generated with Claude Code

…cript does, and report the words after `export` that start no declaration (#608, #651)

- #608: `isAbstractClass` and `isDeclareClass` require the class on the same
  line, as TypeScript reads a modifier only before a token on its line.
  `export default abstract` with `class A {}` on the next line exports the
  value of `abstract` and declares `A` on its own. Decorators before
  `abstract` or `declare` and a line break are an error (TS1146), also before
  `export` (`#checkDecoratorsBeforeExport`). `export declare` before a line
  break is TS1128 at `export`. UPSTREAM sveltejs/acorn-typescript#137.
- #651: `parseExportDeclaration` reports `abstract`, `type`, `namespace` or
  `module` before `tsParseDeclaration` reads and drops it: TS1242 for
  `abstract` before a function, variable or import declaration (a checker
  error, recorded when collecting), TypeScript's parser errors otherwise
  (TS1128, TS1003, TS1359, TS1005, TS1142). UPSTREAM
  sveltejs/acorn-typescript#132.
- What follows `export` and starts no declaration is TS1128 `Declaration or
  statement expected.` at `export`, instead of `Unexpected token` at the word.
- tsrx-prettier-plugin refuses TS1242 instead of leaving `abstract` out.

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.

Stale Bugbot comment from a previous run.

Comment thread packages/tsrx/src/plugin.js Resolved

@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 prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Escaped keywords treated as modifiers
    • Escaped abstract, type, namespace, and module after export on the same line now raise TypeScript's parser error instead of being recovered as modifiers, and isDeclareClass measures any following line break from the abstract token's end.

Create PR

Or push these changes by commenting:

@cursor push c878b08893
Preview (c878b08893)
diff --git a/.changeset/parser-escaped-keywords.md b/.changeset/parser-escaped-keywords.md
new file mode 100644
--- /dev/null
+++ b/.changeset/parser-escaped-keywords.md
@@ -1,0 +1,5 @@
+---
+'@tsrx/core': patch
+---
+
+An escaped `abstract`, `type`, `namespace`, or `module` after `export` is a parser error, `Keywords cannot contain escape characters.`, on the same line as the next token. `export \u0061bstract function f() {}` used to be recorded as a checker error in `collect` mode and emitted as `export function f() {}`.

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
@@ -104,6 +104,9 @@
 const IDENTIFIER_EXPECTED = 'Identifier expected.';
 const LINE_BREAK_NOT_PERMITTED = 'Line break not permitted here.';
 const OPENING_BRACE_EXPECTED = "'{' expected.";
+// TypeScript's parser error when a keyword token contains a unicode escape
+// (TS1260). The token is still that keyword; the escape is not an identifier.
+const KEYWORDS_CANNOT_CONTAIN_ESCAPE = 'Keywords cannot contain escape characters.';
 // TypeScript's checker error for `abstract` before a declaration other than a
 // class (TS1242).
 const ABSTRACT_MODIFIER_NOT_ALLOWED =
@@ -4219,10 +4222,12 @@
 			isDeclareClass() {
 				if (!super.isDeclareClass() || this.#lineBreakAfter(this.end)) return false;
 				const next = this.nextTokenStart();
-				// `class`, or `abstract` and then `class`.
-				return (
-					this.input.startsWith('class', next) || !this.#lineBreakAfter(next + 'abstract'.length)
-				);
+				// `class`, or `abstract` and then `class`. An escaped `abstract`
+				// is a longer token than those eight letters, so a line break after
+				// it is past the token, not `next + 'abstract'.length`.
+				if (this.input.startsWith('class', next)) return true;
+				const abstract = this.lookahead(1);
+				return !abstract.containsEsc && !this.#lineBreakAfter(abstract.end);
 			}
 
 			/**
@@ -4396,7 +4401,9 @@
 			 * - `abstract` before a function, a variable or an import declaration is
 			 *   a modifier of that declaration, which TypeScript's checker reports
 			 *   (TS1242). A strict parse throws it; a collecting parse records it and
-			 *   parses the declaration without the modifier.
+			 *   parses the declaration without the modifier. An escape in the word
+			 *   is not that recovery: TypeScript reports TS1260, a parser error, and
+			 *   the declaration is not kept without the word.
 			 * - `type` after `declare`, or before `default` or `@`, starts a type
 			 *   alias, whose name is missing (TS1003, or TS1359 for a reserved word),
 			 *   or on the next line (TS1142). Before `=`, TypeScript expects the
@@ -4443,14 +4450,21 @@
 				// After `abstract`, it takes a name to `tsParseAbstractDeclaration`,
 				// which reads `interface` and rejects any other name at `abstract`
 				// (#697).
-				if (
+				//
+				// acorn unescapes `value`, so `\u0061bstract` has the same value as
+				// `abstract`. TypeScript still scans that token as the keyword and
+				// reports TS1260; it does not drop the word and keep the declaration.
+				// A line break above leaves the word an identifier (no TS1260).
+				const keeps_word =
 					is_identifier(next.type) ||
 					(value === 'abstract' && next.type === tt._class) ||
-					(value === 'module' && next.type === tt.string)
+					(value === 'module' && next.type === tt.string);
+				if (
+					keeps_word ||
+					(value === 'abstract' && this.#isDeclarationAfterModifier(next.type, ahead))
 				) {
-					return;
-				}
-				if (value === 'abstract' && this.#isDeclarationAfterModifier(next.type, ahead)) {
+					if (word.containsEsc) this.raise(word.start, KEYWORDS_CANNOT_CONTAIN_ESCAPE);
+					if (keeps_word) return;
 					this.raise(word.start, ABSTRACT_MODIFIER_NOT_ALLOWED);
 					return;
 				}

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
@@ -1596,6 +1596,10 @@
 			'export declare abstract\nclass A {}',
 			'export declare namespace\nN {}',
 			'declare module "m" {\n\texport abstract\n\tclass A {}\n}',
+			// An escape does not make the line break a modifier (no TS1260).
+			'export \\u0061bstract\nclass A {}',
+			'export \\u0074ype\nFoo = 1;',
+			'export \\u006Eamespace\nN {}',
 		];
 		const outcomes = await parse_in_worker(in_every_mode(sources));
 		expect(outcomes).toEqual(
@@ -1711,6 +1715,33 @@
 		);
 	});
 
+	it('rejects an escaped `abstract`, `type`, `namespace` or `module` (TS1260)', async () => {
+		// The escape is still that keyword to the scanner. TypeScript reports
+		// TS1260, a parser error, and does not keep the declaration with the word
+		// removed. A collecting parse must not record TS1242 and emit
+		// `export function f() {}`.
+		const message = 'Keywords cannot contain escape characters.';
+		const sources = [
+			'export \\u0061bstract function f() {}',
+			'export \\u0061bstract const x = 1;',
+			'export \\u0061bstract var x = 1;',
+			'export \\u0061bstract import x = y;',
+			'export declare \\u0061bstract function f(): void;',
+			'export \\u0061bstract class A {}',
+			'export \\u0061bstract interface I {}',
+			'export \\u0074ype Foo = 1;',
+			'export \\u006Eamespace N {}',
+			'export \\u006Dodule M {}',
+			'export \\u006Dodule "m" {}',
+			'export declare \\u0061bstract class A {}',
+			'export declare \\u0074ype Foo = 1;',
+		];
+		const outcomes = await parse_in_worker(in_every_mode(sources));
+		expect(outcomes).toEqual(
+			sources.flatMap((source) => thrown_in_every_mode(source, source.indexOf('\\u'), message)),
+		);
+	});
+
 	it('records `abstract` before a function, variable or import declaration, which TypeScript reports from its checker (TS1242)', async () => {
 		const message =
 			"'abstract' modifier can only appear on a class, method, or property declaration.";

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

Reviewed by Cursor Bugbot for commit 0d36a3e. Configure here.

Comment thread packages/tsrx/src/plugin.js Resolved
@cursor

cursor Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

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

  • ✅ Fixed: Abstract check skips some declarations
    • The export check no longer treats an identifier after abstract as a kept declaration, and reports TS1242 for let, async function, interface, enum, type, namespace, module, and global while a collecting parse keeps the declaration.

Create PR

Or push these changes by commenting:

@cursor push ec117fb37e
Preview (ec117fb37e)
diff --git a/.changeset/parser-export-keywords.md b/.changeset/parser-export-keywords.md
--- a/.changeset/parser-export-keywords.md
+++ b/.changeset/parser-export-keywords.md
@@ -14,9 +14,11 @@
 - `abstract`, `type`, `namespace` or `module` after `export`, before a
   declaration it doesn't start, is an error instead of being left out of the
   output. `export abstract function f() {}` used to compile to
-  `export function f() {}`. `abstract` before a function, variable, or import
-  declaration is an error that TypeScript reports from its checker, so it's
-  recorded in `collect` and `loose` mode, and a compile throws it. The others,
+  `export function f() {}`. `abstract` before a function (including
+  `async function`), variable (`const`, `let`, or `var`), import, interface,
+  enum, type, namespace, module, or `global` declaration is an error that
+  TypeScript reports from its checker, so it's recorded in `collect` and
+  `loose` mode, and a compile throws it. The others,
   such as `export type const x = 1;`, are syntax errors.
 - The syntax errors are TypeScript's, at its positions. Where what follows
   `export` starts no declaration, including `export abstract` before a line

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
@@ -786,6 +786,9 @@
 			#commentContextId = 0;
 			#collect = false;
 			#loose = false;
+			// Set while `parseExportDeclaration` reads the declaration after `export`,
+			// so `tsParseAbstractDeclaration` can tell that call from a statement.
+			#readingExportDeclaration = false;
 			// Set while `parseVarStatement` or `parseVar` lets `const` declarators omit
 			// the initializer (see `#parseConstWithoutInitializer`).
 			#collectingConstWithoutInitializer = false;
@@ -4367,17 +4370,44 @@
 				// `parseExport` has just read `export`.
 				const export_start = this.lastTokStart;
 				this.#checkExportDeclarationStart(export_start);
-				const declaration = super.parseExportDeclaration(node);
-				if (
-					declaration?.type !== 'VariableDeclaration' &&
-					!(/** @type {{ id?: unknown } | null} */ (declaration)?.id)
-				) {
-					this.raise(export_start, DECLARATION_OR_STATEMENT_EXPECTED);
+				const reading = this.#readingExportDeclaration;
+				this.#readingExportDeclaration = true;
+				try {
+					const declaration = super.parseExportDeclaration(node);
+					if (
+						declaration?.type !== 'VariableDeclaration' &&
+						!(/** @type {{ id?: unknown } | null} */ (declaration)?.id)
+					) {
+						this.raise(export_start, DECLARATION_OR_STATEMENT_EXPECTED);
+					}
+					return declaration;
+				} finally {
+					this.#readingExportDeclaration = reading;
 				}
-				return declaration;
 			}
 
 			// UPSTREAM(sveltejs/acorn-typescript#132): remove once a release includes the fix
+			/**
+			 * `export abstract` consumes `abstract` and then parses a class. An
+			 * interface is parsed with `abstract` set and no error. Any other
+			 * identifier (`let`, `async`, `enum`, `type`, `namespace`, `module`,
+			 * `global`) hits `unexpected` at `abstract`, so a collecting parse kept
+			 * no tree. Decline for those, and for the interface: the word is already
+			 * consumed, and the declaration parses without the modifier.
+			 * `#checkExportDeclarationStart` has reported TS1242. Outside an export,
+			 * including `abstract class` and `abstract interface`, acorn-typescript's
+			 * parse stays.
+			 * @param {AST.Node & { abstract?: boolean }} node
+			 */
+			tsParseAbstractDeclaration(node) {
+				if (this.type === tt._class || !this.#readingExportDeclaration) {
+					// @ts-ignore — acorn-typescript method, absent from the parser type
+					return super.tsParseAbstractDeclaration(node);
+				}
+				return undefined;
+			}
+
+			// UPSTREAM(sveltejs/acorn-typescript#132): remove once a release includes the fix
 			// UPSTREAM(sveltejs/acorn-typescript#137): remove once a release includes the fix
 			/**
 			 * Report `export declare` before a line break (see
@@ -4393,10 +4423,13 @@
 			 * class A {}` gave the plain exported declaration. Report the word
 			 * before it's lost, as TypeScript's parser does:
 			 *
-			 * - `abstract` before a function, a variable or an import declaration is
-			 *   a modifier of that declaration, which TypeScript's checker reports
-			 *   (TS1242). A strict parse throws it; a collecting parse records it and
-			 *   parses the declaration without the modifier.
+			 * - `abstract` before a function (including `async function`), a variable
+			 *   (`const`, `let`, or `var`), an import, or an `interface`, `enum`,
+			 *   `type`, `namespace`, `module`, or `global` declaration is a modifier
+			 *   of that declaration, which TypeScript's checker reports (TS1242). A
+			 *   strict parse throws it; a collecting parse records it and parses the
+			 *   declaration without the modifier. `class` is the declaration
+			 *   `abstract` may modify.
 			 * - `type` after `declare`, or before `default` or `@`, starts a type
 			 *   alias, whose name is missing (TS1003, or TS1359 for a reserved word),
 			 *   or on the next line (TS1142). Before `=`, TypeScript expects the
@@ -4439,15 +4472,19 @@
 					if (alias && !is_identifier(next.type)) this.#raiseIdentifierExpected(next);
 				}
 				if (line_break) return;
-				// What `tsParseDeclaration` reads after the word.
+				// What `tsParseDeclaration` keeps after the word. A name keeps `type`,
+				// `namespace`, and `module`. `abstract` is kept only before `class`:
+				// `async`, `let`, `interface`, `enum`, `type`, `namespace`, `module`,
+				// and `global` are identifier tokens too, and treating them as a name
+				// left `abstract` unreported.
 				if (
-					is_identifier(next.type) ||
+					(value !== 'abstract' && is_identifier(next.type)) ||
 					(value === 'abstract' && next.type === tt._class) ||
 					(value === 'module' && next.type === tt.string)
 				) {
 					return;
 				}
-				if (value === 'abstract' && this.#isDeclarationAfterModifier(next.type, ahead)) {
+				if (value === 'abstract' && this.#isDeclarationAfterModifier(next, ahead)) {
 					this.raise(word.start, ABSTRACT_MODIFIER_NOT_ALLOWED);
 					return;
 				}
@@ -4470,26 +4507,62 @@
 			}
 
 			/**
-			 * Whether the token `ahead` tokens on, of type `type`, starts a
-			 * declaration that TypeScript's parser reads after a modifier, and that
-			 * can follow `export` here: a function, a variable, or an import
-			 * declaration (not `import(…)` or `import.meta`). TypeScript's parser
-			 * reads no declaration at decorators after a modifier, and the tree has
-			 * no place for a second `export`.
-			 * @param {acorn.TokenType} type
+			 * Whether `token`, `ahead` tokens on, starts a declaration that
+			 * TypeScript's parser reads after a modifier, and that can follow
+			 * `export` here. TypeScript's checker reports `abstract` before one
+			 * (TS1242): a function (including `async function`), a variable
+			 * (`const`, `let`, or `var`), an import (not `import(…)` or
+			 * `import.meta`), or `interface`, `enum`, `type`, `namespace`,
+			 * `module`, or `global`. `let`, `async`, and those words are
+			 * identifier tokens; `let` has no keyword token. A name after
+			 * `interface`, `enum`, `type`, `namespace`, or `module` has to be on
+			 * the same line, as in TypeScript's `isDeclaration`. TypeScript's
+			 * parser reads no declaration at decorators after a modifier, and the
+			 * tree has no place for a second `export`.
+			 * @param {Parse.LookaheadState} token
 			 * @param {number} ahead
 			 */
-			#isDeclarationAfterModifier(type, ahead) {
+			#isDeclarationAfterModifier(token, ahead) {
+				const type = token.type;
 				if (type === tt._function || type === tt._const || type === tt._var) return true;
-				if (type !== tt._import) return false;
-				const after = this.lookahead(ahead + 1).type;
-				return (
-					after === tt.string ||
-					after === tt.star ||
-					after === tt.braceL ||
-					!!after.keyword ||
-					Parser.acornTypeScript.tokenIsIdentifier(after)
-				);
+				if (type === tt._import) {
+					const after = this.lookahead(ahead + 1).type;
+					return (
+						after === tt.string ||
+						after === tt.star ||
+						after === tt.braceL ||
+						!!after.keyword ||
+						Parser.acornTypeScript.tokenIsIdentifier(after)
+					);
+				}
+				if (token.containsEsc) return false;
+				const after = this.lookahead(ahead + 1);
+				const is_identifier = Parser.acornTypeScript.tokenIsIdentifier;
+				// `let` is a keyword when a binding follows, on this line or the
+				// next (`export abstract let x` / `export abstract let\nx`).
+				if (token.value === 'let') {
+					return (
+						is_identifier(after.type) || after.type === tt.braceL || after.type === tt.bracketL
+					);
+				}
+				const line_break = regex_line_break.test(this.input.slice(token.end, after.start));
+				// `async function` is a function declaration only without a line break.
+				if (token.value === 'async') return !line_break && after.type === tt._function;
+				if (line_break) return false;
+				if (
+					type === tstt.interface ||
+					type === tstt.enum ||
+					type === tstt.type ||
+					type === tstt.namespace
+				) {
+					return is_identifier(after.type);
+				}
+				if (type === tstt.module) return is_identifier(after.type) || after.type === tt.string;
+				// `global { … }` or `global` before a name.
+				if (type === tstt.global) {
+					return after.type === tt.braceL || is_identifier(after.type) || after.type === tt._export;
+				}
+				return false;
 			}
 
 			// UPSTREAM(sveltejs/acorn-typescript#125): remove once a release includes the fix

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
@@ -1668,6 +1668,14 @@
 			['export abstract * from "m";', 'export', 'Declaration or statement expected.'],
 			['export abstract {}', 'export', 'Declaration or statement expected.'],
 			['export abstract import("m");', 'export', 'Declaration or statement expected.'],
+			// An identifier after `abstract` is not a name it keeps. These start
+			// no declaration (a bare `async` or `interface`, or a line break
+			// before the declaration's name).
+			['export abstract foo', 'export', 'Declaration or statement expected.'],
+			['export abstract async', 'export', 'Declaration or statement expected.'],
+			['export abstract async\nfunction f() {}', 'export', 'Declaration or statement expected.'],
+			['export abstract interface', 'export', 'Declaration or statement expected.'],
+			['export abstract interface\nI {}', 'export', 'Declaration or statement expected.'],
 			[
 				'declare module "m" {\n\texport type const x: number;\n}',
 				'export',
@@ -1718,11 +1726,25 @@
 		const cases = [
 			['export abstract function f() {}', 'FunctionDeclaration'],
 			['export abstract function* g() {}', 'FunctionDeclaration'],
+			['export abstract async function f() {}', 'FunctionDeclaration'],
+			['export abstract async function* g() {}', 'FunctionDeclaration'],
 			['export abstract const x = 1;', 'VariableDeclaration'],
 			['export abstract var x = 1;', 'VariableDeclaration'],
+			['export abstract let x = 1;', 'VariableDeclaration'],
+			['export abstract let\nx = 1;', 'VariableDeclaration'],
 			['export abstract const enum E {}', 'TSEnumDeclaration'],
+			['export abstract enum E {}', 'TSEnumDeclaration'],
+			['export abstract interface I {}', 'TSInterfaceDeclaration'],
+			['export abstract type T = number;', 'TSTypeAliasDeclaration'],
+			['export abstract namespace N {}', 'TSModuleDeclaration'],
+			['export abstract module M {}', 'TSModuleDeclaration'],
+			['export abstract module "m" {}', 'TSModuleDeclaration'],
+			['export abstract global {}', 'TSModuleDeclaration'],
 			['export declare abstract function f(): void;', 'TSDeclareFunction'],
+			['export declare abstract async function f(): void;', 'TSDeclareFunction'],
 			['export declare abstract const x: number;', 'VariableDeclaration'],
+			['export declare abstract let x: number;', 'VariableDeclaration'],
+			['export declare abstract interface I {}', 'TSInterfaceDeclaration'],
 			['export abstract import x = y;', 'TSImportEqualsDeclaration'],
 		];
 		const outcomes = await parseBothModes(cases.map(([source]) => source));

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

…xport` as TypeScript does (#651)

TypeScript's parser reads `export abstract function f() {}` with the
modifier and reports TS1260 `Keywords cannot contain escape characters.` at
it, a syntax error, instead of the checker's TS1242. Escaped TypeScript
keywords elsewhere are #709.

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

Labels

None yet

Projects

None yet

1 participant