Repository navigation
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
Conversation
…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]>
This was referenced Sep 25, 2026
Closed
…ter `export abstract` (#651) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
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.
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.
Contributor
|
Bugbot Autofix prepared a fix for the issue found in the latest run.
Or push these changes by commenting: 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. |
2 tasks done
…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]>
This was referenced Sep 26, 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.


Two
@tsrx/coreparser bugs around the words afterexport, both with user decisions: #608 follows TypeScript, and #651 rejects. Both are caused in@sveltejs/acorn-typescript1.0.13 and worked around by overriding the responsible methods inTSRXPlugin(packages/tsrx/src/plugin.js), marked// UPSTREAM(sveltejs/acorn-typescript#<n>): remove once a release includes the fix.The bugs and the fixes
#608:
abstractordeclarebefore a line break was read as a modifierTypeScript reads a modifier only before a token on the same line. acorn-typescript's
isAbstractClassandisDeclareClassignored a line break, and itsparseExportDeclarationatedeclarewherever the next token was. So:export default abstractwithclass A {}on the next line parsed as one abstract class, the default export. TypeScript readsexport default abstract;(exporting a value namedabstract) and then a separate classA, which isn't abstract.export declarewith a declaration on the next line parsed as an ambient declaration. TypeScript reports TS1128Declaration or statement expected.atexport.abstractordeclareand a line break (export default @dec abstract,export @dec declare,@dec abstract,@dec declare, each withclass A {}on the next line) went to the class. TypeScript reports TS1146Declaration expected..Fix:
isAbstractClassandisDeclareClassrequire the class on the same line asabstractanddeclare(anddeclare abstract). They're acorn-typescript's checks, used byparseExportDefaultDeclaration(fix(core): parse anonymous and decorated default-exported classes as declarations, and decorators on a rest parameter (#194, #533, #564) #614's override and acorn-typescript's) and bycanHaveLeadingDecorator, whichparseDecoratorsasks. With the fix,export default abstracton its own line falls through to the expression it is, and decorators beforeabstractordeclareand a line break getLeading decorators must be attached to a class declaration.at that word, which is how TSRX already reports TS1146 after decorators (@dec x;,export @dec x;).#checkDecoratorsBeforeExport(from fix(core): parse anonymous and decorated default-exported classes as declarations, and decorators on a rest parameter (#194, #533, #564) #614, for decorators beforeexport) applies the same rule when it looks pastdeclareandabstract.@dec export default abstractwithclass A {}on the next line reports the decorators, and when collecting records them at the@, as TypeScript's checker does (TS1206), and drops them, instead of giving them to classA.export declarebefore a line break reports TS1128 atexport, before acorn-typescript readsdeclare.Statement-level
abstractanddeclarebefore a line break (without decorators) already parsed as TypeScript does.#651: a word after
exportwas dropped before a declaration it doesn't startacorn-typescript's
tsParseDeclarationreadsabstract,type,namespaceormoduleafterexport(orexport declare) before it checks what follows. When that isn't a class or a name (or a string aftermodule), it declines, andparseExportDeclarationparses the rest as a statement without the word.export abstract function f() {}compiled toexport function f() {}, andexport type const x = 1;toexport const x = 1;, in every mode.Fix:
#checkExportDeclarationStart, called by #650'sparseExportDeclarationoverride before acorn-typescript's, looks at the word and the token after it, and reports what TypeScript reports, where it reports it:abstractbefore a function, a variable or an import declaration: TypeScript's parser reads it as a modifier of that declaration, and its checker reports TS1242'abstract' modifier can only appear on a class, method, or property declaration.atabstract. The message is inCHECKER_LEVEL_ERRORS, as in fix(core): collect type-check-level errors in collect mode instead of throwing (#415) #558, fix(core): collect more checker-level mistakes, report a disallowed modifier's message, and accept an optional binding pattern in an overload (#587, #588, #550, #557) #618 and fix(core): collect decorator, modifier, and rest parameter mistakes that TypeScript reports from its checker (#610, #611, #616, #617) #647, so a strict parse (and a compile) throws it, and a collecting parse records it and parses the declaration without the modifier. An escapedabstract(export \u0061bstract function f() {}) is TypeScript's parser error TS1260Keywords cannot contain escape characters.instead, in every mode (from Bugbot's review; escaped keywords elsewhere are [Bug]: tsrx-core: A TypeScript keyword written with an escape is read as the keyword without an error, unlike TypeScript (TS1260) #709).typeafterdeclare, or beforedefaultor@: TypeScript reads a type alias whose name is missing (TS1003Identifier expected., or TS1359 for a reserved word such asconst), or TS1142Line break not permitted here.when the name is on the next line. Before=, TypeScript expects the braces ofexport type { … }(TS1005'{' expected.).namespacebefore a string is TS1003.export(export type const x = 1;,export namespace class A {},export abstract @dec class A {}).In collect mode the tree for the TS1242 forms has no
abstract, sotsrx-prettier-pluginnow refuses that error (REJECTED_MISTAKESinpackages/tsrx-prettier-plugin/src/parse.js, #618's rule) instead of printing the declaration without the word. Prettier'stypescriptparser rejects these too.@tsrx/prettier-pluginparses strictly and throws.TypeScript's error for #650's forms
#650 made
export abstractbefore a line break,export abstract;and the likeUnexpected tokenat the word, to stop a crash. #608 asks for TypeScript's errors, which is TS1128 atexportfor all of them, soparseExportDeclaration's check now raisesDeclaration or statement expected.atexport. #650's test pins these, and this PR changes the expected message and position.Overrides
plugin.jsisAbstractClass(new)superand no line break afterabstract.isDeclareClass(new)superand no line break afterdeclare, or afterabstractindeclare abstract class. Its only caller iscanHaveLeadingDecorator.#checkDecoratorsBeforeExport(#614's, extended)declareandabstractonly before a token on the same line.parseExportDeclaration(#650's, extended)#checkExportDeclarationStartbeforesuper; its check aftersupernow reports TS1128 atexport.#checkExportDeclarationStart(new, called by the one above)export declarebefore a line break, and atabstract,type,namespaceormodulewheretsParseDeclarationwould 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 wordtsParseDeclarationreads 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 onlyacornand@sveltejs/acorn-typescript. #137's fix gives TypeScript's AST forexport default abstracton its own line (anExportDefaultDeclarationof the identifier and a separateClassDeclaration), 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, aProgramfor checker errors), checked for every form below.Code that parses today and now means something else (#608).
export default abstractfollowed byclass A {}on the next line, also with a comment or a multi-line comment between them and insidedeclare module: the default export is now the value ofabstract, andAis a class of its own that isn't abstract. Compiled and editor output go fromexport default abstract class A {}toexport default abstract;andclass A {}. Both formatters printexport default abstract;\nclass A {}\n, as Prettier does.Code that parses today and is now rejected:
abstractordeclarebefore a line break afterexportis read as a modifier, unlike TypeScript #608, TS1128 atexport:export declarefollowed on the next line byclass A {},abstract class A {},function f(): void;,const/let/var x: number;,enum E {},const enum E {},namespace N {},module 'm' {},interface I {}ortype T = 1;, also with a comment before the line break and insidedeclare namespace/declare module.abstractordeclarebefore a line break afterexportis 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 declarethenabstract class A {}, and@dec export declare, each with the class on the next line (when collecting,@dec export declarerecords the decorators and then fails with TS1128 atexport).@dec export default abstractwith the class on the next line throws in a strict parse, and when collecting records the error at@dec(TS1206 in TypeScript) and parsesexport default abstract;and the class without the decorators.abstract,type,namespaceormoduleafterexportis 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 insidedeclare module/namespace.export abstract import x = y;used to throw'import' and 'export' may only appear at the top levelin a strict parse and record it when collecting; it now reports TS1242 first.abstract,type,namespaceormoduleafterexportis dropped before a declaration it does not start #651, TypeScript's parser errors in every mode:export type,export namespace,export module,export declare namespaceandexport declare modulebeforefunction,function*,const,var,class,const enumor a decorated class (TS1128);export abstract @dec class A {}andexport declare abstract @dec class A {}(TS1128);export type @dec class A {}(TS1003);export declare typebefore a reserved word or@(TS1359, TS1003);export \u0061bstract function f() {}(TS1260). When collecting,export type import x = y;andexport namespace import x = y;(andmodule) used to parse with only the top-level error; they're TS1128 now.Error messages that change (inputs that already failed):
export abstractbefore a line break,;, a number, the end of the file,{,*,defaultorimport(…),export type/namespace/modulebefore a line break, andexport declare abstract/namespace/modulebefore a line break report TS1128 atexportinstead ofUnexpected tokenat the word or after it, and so doesexport declarebeforeglobal {}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 typewith the name on the next line TS1142,export @dec abstract/declarewith 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 inloosemode. The comparison covers the full AST (ranges included), collected errors, comments, and thrown messages, in 4 worker threads with a hard timeout. The inputs:.tsrxfiles;readSnapshotEntries/readFormatCase);Out of 19,189 inputs, 87 changed, all intended, and no parse hung. None is a
.tsrxfile 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 ofabstract,type,namespace,module,declare, anddeclarewith each of those, before 30 kinds of statement and declaration, on the same line and the next, plus decorators before and afterexport) 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.
compileandcompile_to_volar_mappingsfor every target (react,preact,solid,vue,hono) on the #608 and #651 examples:export default abstracton its own line compiles toexport default abstract;andclass A {}(also insidedeclare module), andexport abstract function f() {}throws TS1242 fromcompileand gives it as an error fromcompile_to_volar_mappings.Formatters. No formatter code changes, apart from the rejected-mistake entry. For #608's valid form, both print what Prettier's
typescriptparser prints, and the output is stable.Tests.
upstream-parser-workarounds.test.js: Review/pr 129 #132's describe reports TS1128 atexportand gains [Bug]: tsrx-core:abstract,type,namespaceormoduleafterexportis dropped before a declaration it does not start #651's cases (the TS1128, TS1003, TS1359, TS1005 and TS1142 forms in every mode, TS1242 thrown or recorded, and the import declarations that still throw); a new describe for Declarations inside <script> bodies are not document symbols (Outline, breadcrumbs) since bodies became blocks #137 coversexport default abstract,export declareand decorators before a line break, and the forms on one line that still parse. fix(core): read type arguments after superclasses and class and function expressions, and reject what follows export or decorates an array pattern (#523, #545, #578, #607, #609, #631) #650's pin of [Bug]: tsrx-core:abstractordeclarebefore a line break afterexportis read as a modifier, unlike TypeScript #608's old reading is gone.abstractordeclarebefore a line break afterexportis read as a modifier, unlike TypeScript #608's form formats like Prettier and stays stable, andexport abstract function f() {}andexport abstract const x = 1;are refused.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 mergingmainat3a33f715).tsrx-react,tsrx-preact,tsrx-solid,tsrx-vueandtsrx-hono: 3,030 passed, 60 skipped.prettier-known-failures.jsondoesn't change (UPDATE_KNOWN_FAILURES=1).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.tsgainsisDeclareClass.pnpm formatandpnpm changeset:check: clean.The changeset is a
patchfor@tsrx/core.tsrx-prettier-pluginis private.Findings
Filed upstream:
abstractordeclarefollowed by a line break is read as a modifier afterexport,export defaultor decorators sveltejs/acorn-typescript#137:abstractordeclarefollowed by a line break is read as a modifier afterexport,export defaultor decorators ([Bug]: tsrx-core:abstractordeclarebefore a line break afterexportis read as a modifier, unlike TypeScript #608's cause). Review/pr 129 #132 already covers [Bug]: tsrx-core:abstract,type,namespaceormoduleafterexportis dropped before a declaration it does not start #651's.Filed here, separate causes, not fixed in this PR:
abstractbefore a declaration other than a class is rejected in every mode, or accepted without an error, unlike TypeScript #697:abstractbefore a declaration other than a class, where acorn-typescript'stsParseAbstractDeclarationreads it (before a name or a TypeScript keyword):abstract interface I {}parses with no error (TS1242 in TypeScript, and@tsrx/prettier-pluginprints it withoutabstract),abstract declare class A {}fails although it's valid, andabstract let x,export abstract enum E {},export abstract async function f() {}and statement-levelabstract function f() {}throw in every mode, where TypeScript reports TS1242 only from its checker. This PR handles theabstractthattsParseDeclarationused to drop; these take the other path.export default interfacewith the name on the next line fails to parse #698:export default interfacewith the name on the next line fails to parse. Afterexport default, TypeScript readsinterfaceas a declaration across a line break; acorn-typescript'stsParseInterfaceDeclarationdeclines it.asfails to parse, andexport type as = …parses, unlike TypeScript #699: a type alias namedas(type as = 1;) fails to parse, andexport type as = 1;parses, where TypeScript readsexport typebeforeasasexport type { … }(TS1005). This PR reports TypeScript's error for=,defaultand@afterexport type;asis [Bug]: tsrx-core: A type alias namedasfails to parse, andexport type as = …parses, unlike TypeScript #699's, soexport typewithason the next line still reports TS1128.export global {}andexport declare global {}fail to parse in every mode, where TypeScript reports them from its checker #700:export global {}andexport declare global {}fail to parse in every mode, where TypeScript reads them and reports theexportfrom its checker (TS2668). acorn-typescript'sshouldParseExportStatementdoesn't takeglobalfor the start of a declaration.\u0061bstract class A {},export d\u0065clare class A {},export \u0074ype T = 1;) is read as the keyword without an error, where TypeScript reports TS1260. This PR reports TS1260 only where it would otherwise record TS1242.Not filed, as they're wording rather than bugs: where TypeScript reports TS1128 for other tokens after
export(export foo,export asyncbefore a line break) TSRX keeps acorn'sUnexpected tokenat the token, andexport declarebefore 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'sUnexpected tokenorThe keyword 'let' is reservedat the position where TypeScript reports'{' expected.or'=' expected..export export class A {}andexport 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, andexport abstract import { a } from "m";andexport abstract export class A {}behave the same way, with TS1242 and TS1128.Closes #608
Closes #651
🤖 Generated with Claude Code