Skip to content

Commit c878b08

Browse files
committed
fix(core): reject escaped keywords after export
An escaped abstract, type, namespace, or module on the same line as the next token is a parser error (TS1260). Collect mode used to record TS1242 and emit the declaration with the word removed.
1 parent 0d36a3e commit c878b08

3 files changed

Lines changed: 60 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tsrx/core': patch
3+
---
4+
5+
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() {}`.

‎packages/tsrx/src/plugin.js‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ const DECLARATION_OR_STATEMENT_EXPECTED = 'Declaration or statement expected.';
104104
const IDENTIFIER_EXPECTED = 'Identifier expected.';
105105
const LINE_BREAK_NOT_PERMITTED = 'Line break not permitted here.';
106106
const OPENING_BRACE_EXPECTED = "'{' expected.";
107+
// TypeScript's parser error when a keyword token contains a unicode escape
108+
// (TS1260). The token is still that keyword; the escape is not an identifier.
109+
const KEYWORDS_CANNOT_CONTAIN_ESCAPE = 'Keywords cannot contain escape characters.';
107110
// TypeScript's checker error for `abstract` before a declaration other than a
108111
// class (TS1242).
109112
const ABSTRACT_MODIFIER_NOT_ALLOWED =
@@ -4219,10 +4222,12 @@ export function TSRXPlugin(config) {
42194222
isDeclareClass() {
42204223
if (!super.isDeclareClass() || this.#lineBreakAfter(this.end)) return false;
42214224
const next = this.nextTokenStart();
4222-
// `class`, or `abstract` and then `class`.
4223-
return (
4224-
this.input.startsWith('class', next) || !this.#lineBreakAfter(next + 'abstract'.length)
4225-
);
4225+
// `class`, or `abstract` and then `class`. An escaped `abstract`
4226+
// is a longer token than those eight letters, so a line break after
4227+
// it is past the token, not `next + 'abstract'.length`.
4228+
if (this.input.startsWith('class', next)) return true;
4229+
const abstract = this.lookahead(1);
4230+
return !abstract.containsEsc && !this.#lineBreakAfter(abstract.end);
42264231
}
42274232

42284233
/**
@@ -4396,7 +4401,9 @@ export function TSRXPlugin(config) {
43964401
* - `abstract` before a function, a variable or an import declaration is
43974402
* a modifier of that declaration, which TypeScript's checker reports
43984403
* (TS1242). A strict parse throws it; a collecting parse records it and
4399-
* parses the declaration without the modifier.
4404+
* parses the declaration without the modifier. An escape in the word
4405+
* is not that recovery: TypeScript reports TS1260, a parser error, and
4406+
* the declaration is not kept without the word.
44004407
* - `type` after `declare`, or before `default` or `@`, starts a type
44014408
* alias, whose name is missing (TS1003, or TS1359 for a reserved word),
44024409
* or on the next line (TS1142). Before `=`, TypeScript expects the
@@ -4443,14 +4450,21 @@ export function TSRXPlugin(config) {
44434450
// After `abstract`, it takes a name to `tsParseAbstractDeclaration`,
44444451
// which reads `interface` and rejects any other name at `abstract`
44454452
// (#697).
4446-
if (
4453+
//
4454+
// acorn unescapes `value`, so `\u0061bstract` has the same value as
4455+
// `abstract`. TypeScript still scans that token as the keyword and
4456+
// reports TS1260; it does not drop the word and keep the declaration.
4457+
// A line break above leaves the word an identifier (no TS1260).
4458+
const keeps_word =
44474459
is_identifier(next.type) ||
44484460
(value === 'abstract' && next.type === tt._class) ||
4449-
(value === 'module' && next.type === tt.string)
4461+
(value === 'module' && next.type === tt.string);
4462+
if (
4463+
keeps_word ||
4464+
(value === 'abstract' && this.#isDeclarationAfterModifier(next.type, ahead))
44504465
) {
4451-
return;
4452-
}
4453-
if (value === 'abstract' && this.#isDeclarationAfterModifier(next.type, ahead)) {
4466+
if (word.containsEsc) this.raise(word.start, KEYWORDS_CANNOT_CONTAIN_ESCAPE);
4467+
if (keeps_word) return;
44544468
this.raise(word.start, ABSTRACT_MODIFIER_NOT_ALLOWED);
44554469
return;
44564470
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,10 @@ describe('`abstract`, `module`, `namespace` or `type` after `export` that starts
15961596
'export declare abstract\nclass A {}',
15971597
'export declare namespace\nN {}',
15981598
'declare module "m" {\n\texport abstract\n\tclass A {}\n}',
1599+
// An escape does not make the line break a modifier (no TS1260).
1600+
'export \\u0061bstract\nclass A {}',
1601+
'export \\u0074ype\nFoo = 1;',
1602+
'export \\u006Eamespace\nN {}',
15991603
];
16001604
const outcomes = await parse_in_worker(in_every_mode(sources));
16011605
expect(outcomes).toEqual(
@@ -1711,6 +1715,33 @@ describe('`abstract`, `module`, `namespace` or `type` after `export` that starts
17111715
);
17121716
});
17131717

1718+
it('rejects an escaped `abstract`, `type`, `namespace` or `module` (TS1260)', async () => {
1719+
// The escape is still that keyword to the scanner. TypeScript reports
1720+
// TS1260, a parser error, and does not keep the declaration with the word
1721+
// removed. A collecting parse must not record TS1242 and emit
1722+
// `export function f() {}`.
1723+
const message = 'Keywords cannot contain escape characters.';
1724+
const sources = [
1725+
'export \\u0061bstract function f() {}',
1726+
'export \\u0061bstract const x = 1;',
1727+
'export \\u0061bstract var x = 1;',
1728+
'export \\u0061bstract import x = y;',
1729+
'export declare \\u0061bstract function f(): void;',
1730+
'export \\u0061bstract class A {}',
1731+
'export \\u0061bstract interface I {}',
1732+
'export \\u0074ype Foo = 1;',
1733+
'export \\u006Eamespace N {}',
1734+
'export \\u006Dodule M {}',
1735+
'export \\u006Dodule "m" {}',
1736+
'export declare \\u0061bstract class A {}',
1737+
'export declare \\u0074ype Foo = 1;',
1738+
];
1739+
const outcomes = await parse_in_worker(in_every_mode(sources));
1740+
expect(outcomes).toEqual(
1741+
sources.flatMap((source) => thrown_in_every_mode(source, source.indexOf('\\u'), message)),
1742+
);
1743+
});
1744+
17141745
it('records `abstract` before a function, variable or import declaration, which TypeScript reports from its checker (TS1242)', async () => {
17151746
const message =
17161747
"'abstract' modifier can only appear on a class, method, or property declaration.";

0 commit comments

Comments
 (0)