Skip to content

Commit ec117fb

Browse files
committed
fix(core): report abstract on exported declarations it cannot modify
`export abstract` before `let`, `async function`, `interface`, `enum`, `type`, `namespace`, `module`, or `global` skipped TypeScript's TS1242 check, because those words are identifier tokens. A collecting parse then threw `Unexpected token` or kept `abstract` on an interface.
1 parent 870ca7f commit ec117fb

3 files changed

Lines changed: 131 additions & 34 deletions

File tree

‎.changeset/parser-export-keywords.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ The parser reads the words after `export` as TypeScript does:
1414
- `abstract`, `type`, `namespace` or `module` after `export`, before a
1515
declaration it doesn't start, is an error instead of being left out of the
1616
output. `export abstract function f() {}` used to compile to
17-
`export function f() {}`. `abstract` before a function, variable, or import
18-
declaration is an error that TypeScript reports from its checker, so it's
19-
recorded in `collect` and `loose` mode, and a compile throws it. The others,
17+
`export function f() {}`. `abstract` before a function (including
18+
`async function`), variable (`const`, `let`, or `var`), import, interface,
19+
enum, type, namespace, module, or `global` declaration is an error that
20+
TypeScript reports from its checker, so it's recorded in `collect` and
21+
`loose` mode, and a compile throws it. The others,
2022
such as `export type const x = 1;`, are syntax errors.
2123
- The syntax errors are TypeScript's, at its positions. Where what follows
2224
`export` starts no declaration, including `export abstract` before a line

‎packages/tsrx/src/plugin.js‎

Lines changed: 104 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎packages/tsrx/tests/utils/upstream-parser-workarounds.test.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,14 @@ describe('`abstract`, `module`, `namespace` or `type` after `export` that starts
16681668
['export abstract * from "m";', 'export', 'Declaration or statement expected.'],
16691669
['export abstract {}', 'export', 'Declaration or statement expected.'],
16701670
['export abstract import("m");', 'export', 'Declaration or statement expected.'],
1671+
// An identifier after `abstract` is not a name it keeps. These start
1672+
// no declaration (a bare `async` or `interface`, or a line break
1673+
// before the declaration's name).
1674+
['export abstract foo', 'export', 'Declaration or statement expected.'],
1675+
['export abstract async', 'export', 'Declaration or statement expected.'],
1676+
['export abstract async\nfunction f() {}', 'export', 'Declaration or statement expected.'],
1677+
['export abstract interface', 'export', 'Declaration or statement expected.'],
1678+
['export abstract interface\nI {}', 'export', 'Declaration or statement expected.'],
16711679
[
16721680
'declare module "m" {\n\texport type const x: number;\n}',
16731681
'export',
@@ -1718,11 +1726,25 @@ describe('`abstract`, `module`, `namespace` or `type` after `export` that starts
17181726
const cases = [
17191727
['export abstract function f() {}', 'FunctionDeclaration'],
17201728
['export abstract function* g() {}', 'FunctionDeclaration'],
1729+
['export abstract async function f() {}', 'FunctionDeclaration'],
1730+
['export abstract async function* g() {}', 'FunctionDeclaration'],
17211731
['export abstract const x = 1;', 'VariableDeclaration'],
17221732
['export abstract var x = 1;', 'VariableDeclaration'],
1733+
['export abstract let x = 1;', 'VariableDeclaration'],
1734+
['export abstract let\nx = 1;', 'VariableDeclaration'],
17231735
['export abstract const enum E {}', 'TSEnumDeclaration'],
1736+
['export abstract enum E {}', 'TSEnumDeclaration'],
1737+
['export abstract interface I {}', 'TSInterfaceDeclaration'],
1738+
['export abstract type T = number;', 'TSTypeAliasDeclaration'],
1739+
['export abstract namespace N {}', 'TSModuleDeclaration'],
1740+
['export abstract module M {}', 'TSModuleDeclaration'],
1741+
['export abstract module "m" {}', 'TSModuleDeclaration'],
1742+
['export abstract global {}', 'TSModuleDeclaration'],
17241743
['export declare abstract function f(): void;', 'TSDeclareFunction'],
1744+
['export declare abstract async function f(): void;', 'TSDeclareFunction'],
17251745
['export declare abstract const x: number;', 'VariableDeclaration'],
1746+
['export declare abstract let x: number;', 'VariableDeclaration'],
1747+
['export declare abstract interface I {}', 'TSInterfaceDeclaration'],
17261748
['export abstract import x = y;', 'TSImportEqualsDeclaration'],
17271749
];
17281750
const outcomes = await parseBothModes(cases.map(([source]) => source));

0 commit comments

Comments
 (0)