diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 2af0cfd..eed6498 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1465,6 +1465,13 @@ module.exports = function(ast, extra) { var lastClassToken = heritageClauses.length ? heritageClauses[heritageClauses.length - 1] : node.name; var classNodeType = SyntaxKind[node.kind]; + if (node.typeParameters && node.typeParameters.length) { + var lastTypeParameter = node.typeParameters[node.typeParameters.length - 1]; + if (!lastClassToken || lastTypeParameter.pos > lastClassToken.pos) { + lastClassToken = ts.findNextToken(lastTypeParameter, ast); + } + } + if (node.modifiers && node.modifiers.length) { /** diff --git a/tests/fixtures/typescript/basics/export-default-class-with-generic.result.js b/tests/fixtures/typescript/basics/export-default-class-with-generic.result.js new file mode 100644 index 0000000..870644f --- /dev/null +++ b/tests/fixtures/typescript/basics/export-default-class-with-generic.result.js @@ -0,0 +1,222 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 28 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ExportDefaultDeclaration", + "declaration": { + "type": "ClassDeclaration", + "range": [ + 15, + 28 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": null, + "body": { + "type": "ClassBody", + "body": [], + "range": [ + 24, + 28 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "decorators": [] + }, + "range": [ + 0, + 28 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ], + "sourceType": "module", + "tokens": [ + { + "type": "Keyword", + "value": "export", + "range": [ + 0, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + { + "type": "Keyword", + "value": "default", + "range": [ + 7, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + { + "type": "Keyword", + "value": "class", + "range": [ + 15, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + } + }, + { + "type": "Identifier", + "value": "T", + "range": [ + 21, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 27, + 28 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/export-default-class-with-generic.src.ts b/tests/fixtures/typescript/basics/export-default-class-with-generic.src.ts new file mode 100644 index 0000000..8b0d9ac --- /dev/null +++ b/tests/fixtures/typescript/basics/export-default-class-with-generic.src.ts @@ -0,0 +1,3 @@ +export default class { + +} diff --git a/tests/fixtures/typescript/basics/export-default-class-with-multiple-generics.result.js b/tests/fixtures/typescript/basics/export-default-class-with-multiple-generics.result.js new file mode 100644 index 0000000..582090a --- /dev/null +++ b/tests/fixtures/typescript/basics/export-default-class-with-multiple-generics.result.js @@ -0,0 +1,258 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 31 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ExportDefaultDeclaration", + "declaration": { + "type": "ClassDeclaration", + "range": [ + 15, + 31 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": null, + "body": { + "type": "ClassBody", + "body": [], + "range": [ + 27, + 31 + ], + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "decorators": [] + }, + "range": [ + 0, + 31 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ], + "sourceType": "module", + "tokens": [ + { + "type": "Keyword", + "value": "export", + "range": [ + 0, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + { + "type": "Keyword", + "value": "default", + "range": [ + 7, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + { + "type": "Keyword", + "value": "class", + "range": [ + 15, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + } + }, + { + "type": "Identifier", + "value": "T", + "range": [ + 21, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + } + } + }, + { + "type": "Punctuator", + "value": ",", + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + { + "type": "Identifier", + "value": "U", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 25, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 27, + 28 + ], + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 28 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 30, + 31 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/export-default-class-with-multiple-generics.src.ts b/tests/fixtures/typescript/basics/export-default-class-with-multiple-generics.src.ts new file mode 100644 index 0000000..7eb59ca --- /dev/null +++ b/tests/fixtures/typescript/basics/export-default-class-with-multiple-generics.src.ts @@ -0,0 +1,3 @@ +export default class { + +} diff --git a/tests/fixtures/typescript/basics/export-named-class-with-generic.result.js b/tests/fixtures/typescript/basics/export-named-class-with-generic.result.js new file mode 100644 index 0000000..510f54d --- /dev/null +++ b/tests/fixtures/typescript/basics/export-named-class-with-generic.result.js @@ -0,0 +1,241 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ExportNamedDeclaration", + "declaration": { + "type": "ClassDeclaration", + "range": [ + 7, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 13, + 16 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "name": "Foo" + }, + "body": { + "type": "ClassBody", + "body": [], + "range": [ + 20, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "decorators": [] + }, + "range": [ + 0, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "specifiers": [], + "source": null + } + ], + "sourceType": "module", + "tokens": [ + { + "type": "Keyword", + "value": "export", + "range": [ + 0, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + { + "type": "Keyword", + "value": "class", + "range": [ + 7, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Identifier", + "value": "Foo", + "range": [ + 13, + 16 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 16, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + } + }, + { + "type": "Identifier", + "value": "T", + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 18, + 19 + ], + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/export-named-class-with-generic.src.ts b/tests/fixtures/typescript/basics/export-named-class-with-generic.src.ts new file mode 100644 index 0000000..ad37e87 --- /dev/null +++ b/tests/fixtures/typescript/basics/export-named-class-with-generic.src.ts @@ -0,0 +1,3 @@ +export class Foo { + +} diff --git a/tests/fixtures/typescript/basics/export-named-class-with-multiple-generics.result.js b/tests/fixtures/typescript/basics/export-named-class-with-multiple-generics.result.js new file mode 100644 index 0000000..653ba52 --- /dev/null +++ b/tests/fixtures/typescript/basics/export-named-class-with-multiple-generics.result.js @@ -0,0 +1,277 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ExportNamedDeclaration", + "declaration": { + "type": "ClassDeclaration", + "range": [ + 7, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 13, + 16 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "name": "Foo" + }, + "body": { + "type": "ClassBody", + "body": [], + "range": [ + 23, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "decorators": [] + }, + "range": [ + 0, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "specifiers": [], + "source": null + } + ], + "sourceType": "module", + "tokens": [ + { + "type": "Keyword", + "value": "export", + "range": [ + 0, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + { + "type": "Keyword", + "value": "class", + "range": [ + 7, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Identifier", + "value": "Foo", + "range": [ + 13, + 16 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 16, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + } + }, + { + "type": "Identifier", + "value": "T", + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + } + } + }, + { + "type": "Punctuator", + "value": ",", + "range": [ + 18, + 19 + ], + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + } + } + }, + { + "type": "Identifier", + "value": "U", + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 21, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 26, + 27 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/export-named-class-with-multiple-generics.src.ts b/tests/fixtures/typescript/basics/export-named-class-with-multiple-generics.src.ts new file mode 100644 index 0000000..9abd7bb --- /dev/null +++ b/tests/fixtures/typescript/basics/export-named-class-with-multiple-generics.src.ts @@ -0,0 +1,3 @@ +export class Foo { + +}