diff --git a/.changeset/major-potatoes-attend.md b/.changeset/major-potatoes-attend.md new file mode 100644 index 0000000..2313b77 --- /dev/null +++ b/.changeset/major-potatoes-attend.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/acorn-typescript': patch +--- + +fix: support decorators on abstract classes diff --git a/src/index.ts b/src/index.ts index c1eec5d..6c6f698 100644 --- a/src/index.ts +++ b/src/index.ts @@ -898,7 +898,7 @@ export function tsPlugin(options?: { } canHaveLeadingDecorator(): boolean { - return this.match(tt._class); + return this.match(tt._class) || this.isAbstractClass(); } eatContextual(name: string) { diff --git a/test/decorators_class_abstract/expected.json b/test/decorators_class_abstract/expected.json new file mode 100644 index 0000000..b33d4aa --- /dev/null +++ b/test/decorators_class_abstract/expected.json @@ -0,0 +1,100 @@ +{ + "type": "Program", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 0 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "abstract": true, + "decorators": [ + { + "type": "Decorator", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "expression": { + "type": "Identifier", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "name": "decorator" + } + } + ], + "id": { + "type": "Identifier", + "start": 26, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "name": "ExampleClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 39, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "body": [] + } + } + ], + "sourceType": "module" +} diff --git a/test/decorators_class_abstract/input.ts b/test/decorators_class_abstract/input.ts new file mode 100644 index 0000000..70924d4 --- /dev/null +++ b/test/decorators_class_abstract/input.ts @@ -0,0 +1,2 @@ +@decorator +abstract class ExampleClass {} diff --git a/test/decorators_class_abstract_export/expected.json b/test/decorators_class_abstract_export/expected.json new file mode 100644 index 0000000..bee9d8c --- /dev/null +++ b/test/decorators_class_abstract_export/expected.json @@ -0,0 +1,118 @@ +{ + "type": "Program", + "start": 0, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 0 + } + }, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 11, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "exportKind": "value", + "declaration": { + "type": "ClassDeclaration", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "abstract": true, + "decorators": [ + { + "type": "Decorator", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "expression": { + "type": "Identifier", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "name": "decorator" + } + } + ], + "id": { + "type": "Identifier", + "start": 33, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 22 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "name": "ExampleClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 46, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "body": [] + } + }, + "specifiers": [], + "source": null + } + ], + "sourceType": "module" +} diff --git a/test/decorators_class_abstract_export/input.ts b/test/decorators_class_abstract_export/input.ts new file mode 100644 index 0000000..5fe1868 --- /dev/null +++ b/test/decorators_class_abstract_export/input.ts @@ -0,0 +1,2 @@ +@decorator +export abstract class ExampleClass {}