Skip to content

Commit c68456c

Browse files
committed
fix(core): reject leading decorators before exported at-sign constructs
`@dec export default @if` (and `@{`, `@for`, `@switch`, `@try`) skipped the class check, so the decorator stayed on the stack and a later class took it.
1 parent 66f2246 commit c68456c

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎.changeset/parser-default-class-decorators.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ release fixes it:
1919
failing with `Unexpected token`.
2020
- Decorators written before `export` must be followed by an exported class,
2121
as TypeScript requires. Before anything else (`@dec export function f() {}`,
22-
`@dec export const A = class {}`, `@dec export default (class {})`) they used
23-
to be dropped or moved onto a class expression inside; they are now an error,
22+
`@dec export const A = class {}`, `@dec export default (class {})`,
23+
`@dec export default @if (a) { … }`) they used to be dropped or moved onto a
24+
class expression inside or a following class; they are now an error,
2425
`Leading decorators must be attached to a class declaration.`, as they already
2526
were before a statement that isn't a class.
2627
- A rest parameter can have decorators (`m(@dec ...rest: T[]) {}`), like any

‎packages/tsrx/src/plugin.js‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,6 +3763,11 @@ export function TSRXPlugin(config) {
37633763
* With no class, as in `@dec export function f() {}`, they were dropped.
37643764
* Report them at the exported declaration with acorn-typescript's error
37653765
* for leading decorators before something else (TypeScript's TS1206).
3766+
* An `@` that opens `@if`, `@{`, `@for`, `@switch`, or `@try` is an
3767+
* expression, not a decorator. `parseExportDefaultDeclaration` parses
3768+
* those without calling `parseDecorators`, so reject them here;
3769+
* otherwise the leading decorators stay on the stack and a later class
3770+
* takes them.
37663771
* @type {Parse.Parser['parseDecorators']}
37673772
*/
37683773
parseDecorators(allowExport) {
@@ -3773,8 +3778,16 @@ export function TSRXPlugin(config) {
37733778
const is_default = next.type === tt._default;
37743779
if (is_default) next = this.lookahead(++ahead);
37753780
const declaration_start = next.start;
3776-
// Decorators after `export` as well are checked when they are read.
3777-
if (next.type === tstt.at) return;
3781+
// A later decorator is checked when it is read. An at-sign construct
3782+
// (`@if`, `@{`, `@for`, `@switch`, `@try`) is an expression, so that
3783+
// check never runs.
3784+
if (
3785+
next.type === tstt.at &&
3786+
!this.#isCodeBlockStart(next.start) &&
3787+
!this.#isJSXControlFlowDirectiveAt(next.start)
3788+
) {
3789+
return;
3790+
}
37783791
if (!is_default && next.type === tstt.declare && !next.containsEsc) {
37793792
next = this.lookahead(++ahead);
37803793
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,14 @@ describe('decorators before `export` (sveltejs/acorn-typescript#125)', () => {
590590
['@dec export interface I {}', 'interface'],
591591
['@dec export { a };', '{ a }'],
592592
['@dec export default abstract;', 'abstract'],
593+
['@dec export default @if (a) { <div /> }', '@if'],
594+
['@dec export default @{ <div /> }', '@{'],
595+
['@dec export default @for (const x of y) { <div /> }', '@for'],
596+
['@dec export default @for await (const x of y) { <div /> }', '@for'],
597+
['@dec export default @switch (a) { @case 1: { <div /> } }', '@switch'],
598+
['@dec export default @try { <div /> } @catch (e) { <span /> }', '@try'],
599+
['@dec export @if (a) { <div /> }', '@if'],
600+
['@a @b export default @if (a) { <b /> }', '@if'],
593601
]);
594602
const outcomes = await parseBothModes(cases.map(([source]) => source));
595603
for (const [index, [source, declaration]] of cases.entries()) {

0 commit comments

Comments
 (0)