From 91781173648d8720ba4b1e77483c5368651ade36 Mon Sep 17 00:00:00 2001 From: James Henry Date: Mon, 4 Feb 2019 19:54:11 -0500 Subject: [PATCH 1/2] fix(ts-estree): convert decorators on var and fn decs --- packages/typescript-estree/src/convert.ts | 22 +++++ .../lib/__snapshots__/typescript.ts.snap | 92 +++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index eb2e69a60ece..e93c214eee62 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -696,6 +696,17 @@ export class Converter { ); } + /** + * Semantically, decorators are not allowed on function declarations, + * but the TypeScript compiler will parse them and produce a valid AST, + * so we handle them here too. + */ + if (node.decorators) { + (result as any).decorators = node.decorators.map(el => + this.convertChild(el) + ); + } + // check for exports return fixExports(node, result, this.ast); } @@ -730,6 +741,17 @@ export class Converter { kind: getDeclarationKind(node.declarationList) }); + /** + * Semantically, decorators are not allowed on variable declarations, + * but the TypeScript compiler will parse them and produce a valid AST, + * so we handle them here too. + */ + if (node.decorators) { + (result as any).decorators = node.decorators.map(el => + this.convertChild(el) + ); + } + if (hasModifier(SyntaxKind.DeclareKeyword, node)) { result.declare = true; } diff --git a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap index d1040b3b2957..00bb60718ded 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap @@ -90557,6 +90557,43 @@ Object { ], "type": "BlockStatement", }, + "decorators": Array [ + Object { + "expression": Object { + "loc": Object { + "end": Object { + "column": 4, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "name": "dec", + "range": Array [ + 1, + 4, + ], + "type": "Identifier", + }, + "loc": Object { + "end": Object { + "column": 4, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 4, + ], + "type": "Decorator", + }, + ], "expression": false, "generator": false, "id": Object { @@ -91097,6 +91134,61 @@ Object { "type": "VariableDeclarator", }, ], + "decorators": Array [ + Object { + "expression": Object { + "arguments": Array [], + "callee": Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "name": "deco", + "range": Array [ + 1, + 5, + ], + "type": "Identifier", + }, + "loc": Object { + "end": Object { + "column": 7, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "range": Array [ + 1, + 7, + ], + "type": "CallExpression", + }, + "loc": Object { + "end": Object { + "column": 7, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 7, + ], + "type": "Decorator", + }, + ], "kind": "const", "loc": Object { "end": Object { From f53917258bd4182076ac6919423ec33eea115a2d Mon Sep 17 00:00:00 2001 From: James Henry Date: Wed, 6 Feb 2019 20:57:17 -0500 Subject: [PATCH 2/2] chore: update snapshots --- .../lib/__snapshots__/typescript.ts.snap | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/packages/parser/tests/lib/__snapshots__/typescript.ts.snap b/packages/parser/tests/lib/__snapshots__/typescript.ts.snap index ef757ac0453e..ac457ec0ca3d 100644 --- a/packages/parser/tests/lib/__snapshots__/typescript.ts.snap +++ b/packages/parser/tests/lib/__snapshots__/typescript.ts.snap @@ -91249,6 +91249,43 @@ Object { ], "type": "BlockStatement", }, + "decorators": Array [ + Object { + "expression": Object { + "loc": Object { + "end": Object { + "column": 4, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "name": "dec", + "range": Array [ + 1, + 4, + ], + "type": "Identifier", + }, + "loc": Object { + "end": Object { + "column": 4, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 4, + ], + "type": "Decorator", + }, + ], "expression": false, "generator": false, "id": Object { @@ -91791,6 +91828,61 @@ Object { "type": "VariableDeclarator", }, ], + "decorators": Array [ + Object { + "expression": Object { + "arguments": Array [], + "callee": Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "name": "deco", + "range": Array [ + 1, + 5, + ], + "type": "Identifier", + }, + "loc": Object { + "end": Object { + "column": 7, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "range": Array [ + 1, + 7, + ], + "type": "CallExpression", + }, + "loc": Object { + "end": Object { + "column": 7, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 7, + ], + "type": "Decorator", + }, + ], "kind": "const", "loc": Object { "end": Object {