From 217541adf7747623419426bbf9e8a7768ac7f31a Mon Sep 17 00:00:00 2001 From: Rasmus Eneman Date: Mon, 20 Mar 2017 12:14:39 +0100 Subject: [PATCH] Fix: Preserve type parameters for methods (fixes #202) --- lib/ast-converter.js | 6 + .../class-with-generic-method.result.js | 413 ++++++++++++++++++ .../basics/class-with-generic-method.src.ts | 3 + 3 files changed, 422 insertions(+) create mode 100644 tests/fixtures/typescript/basics/class-with-generic-method.result.js create mode 100644 tests/fixtures/typescript/basics/class-with-generic-method.src.ts diff --git a/lib/ast-converter.js b/lib/ast-converter.js index ea8a187..e612cf3 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1190,6 +1190,12 @@ module.exports = function(ast, extra) { } else if (!result.static && node.name.kind === SyntaxKind.StringLiteral && node.name.text === "constructor") { result.kind = "constructor"; } + + // Process typeParameters + if (node.typeParameters && node.typeParameters.length) { + method.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters); + } + break; // TypeScript uses this even for static methods named "constructor" diff --git a/tests/fixtures/typescript/basics/class-with-generic-method.result.js b/tests/fixtures/typescript/basics/class-with-generic-method.result.js new file mode 100644 index 0000000..fcdcbb2 --- /dev/null +++ b/tests/fixtures/typescript/basics/class-with-generic-method.result.js @@ -0,0 +1,413 @@ +module.exports = { + "body": [ + { + "body": { + "body": [ + { + "accessibility": null, + "computed": false, + "decorators": [], + "key": { + "loc": { + "end": { + "column": 8, + "line": 2 + }, + "start": { + "column": 2, + "line": 2 + } + }, + "name": "getBar", + "range": [ + 14, + 20 + ], + "type": "Identifier" + }, + "kind": "method", + "loc": { + "end": { + "column": 16, + "line": 2 + }, + "start": { + "column": 2, + "line": 2 + } + }, + "range": [ + 14, + 28 + ], + "static": false, + "type": "MethodDefinition", + "value": { + "async": false, + "body": { + "body": [], + "loc": { + "end": { + "column": 16, + "line": 2 + }, + "start": { + "column": 14, + "line": 2 + } + }, + "range": [ + 26, + 28 + ], + "type": "BlockStatement" + }, + "expression": false, + "generator": false, + "id": null, + "loc": { + "end": { + "column": 16, + "line": 2 + }, + "start": { + "column": 8, + "line": 2 + } + }, + "params": [], + "range": [ + 23, + 28 + ], + "type": "FunctionExpression", + "typeParameters": { + "loc": { + "end": { + "column": 11, + "line": 2 + }, + "start": { + "column": 8, + "line": 2 + } + }, + "params": [ + { + "constraint": null, + "loc": { + "end": { + "column": 10, + "line": 2 + }, + "start": { + "column": 9, + "line": 2 + } + }, + "name": "T", + "range": [ + 21, + 22 + ], + "type": "TypeParameter" + } + ], + "range": [ + 20, + 23 + ], + "type": "TypeParameterDeclaration" + } + } + } + ], + "loc": { + "end": { + "column": 1, + "line": 3 + }, + "start": { + "column": 10, + "line": 1 + } + }, + "range": [ + 10, + 30 + ], + "type": "ClassBody" + }, + "decorators": [], + "id": { + "loc": { + "end": { + "column": 9, + "line": 1 + }, + "start": { + "column": 6, + "line": 1 + } + }, + "name": "Foo", + "range": [ + 6, + 9 + ], + "type": "Identifier" + }, + "implements": [], + "loc": { + "end": { + "column": 1, + "line": 3 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 30 + ], + "superClass": null, + "type": "ClassDeclaration" + } + ], + "loc": { + "end": { + "column": 1, + "line": 3 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 30 + ], + "sourceType": "script", + "tokens": [ + { + "loc": { + "end": { + "column": 5, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 5 + ], + "type": "Keyword", + "value": "class" + }, + { + "loc": { + "end": { + "column": 9, + "line": 1 + }, + "start": { + "column": 6, + "line": 1 + } + }, + "range": [ + 6, + 9 + ], + "type": "Identifier", + "value": "Foo" + }, + { + "loc": { + "end": { + "column": 11, + "line": 1 + }, + "start": { + "column": 10, + "line": 1 + } + }, + "range": [ + 10, + 11 + ], + "type": "Punctuator", + "value": "{" + }, + { + "loc": { + "end": { + "column": 8, + "line": 2 + }, + "start": { + "column": 2, + "line": 2 + } + }, + "range": [ + 14, + 20 + ], + "type": "Identifier", + "value": "getBar" + }, + { + "loc": { + "end": { + "column": 9, + "line": 2 + }, + "start": { + "column": 8, + "line": 2 + } + }, + "range": [ + 20, + 21 + ], + "type": "Punctuator", + "value": "<" + }, + { + "loc": { + "end": { + "column": 10, + "line": 2 + }, + "start": { + "column": 9, + "line": 2 + } + }, + "range": [ + 21, + 22 + ], + "type": "Identifier", + "value": "T" + }, + { + "loc": { + "end": { + "column": 11, + "line": 2 + }, + "start": { + "column": 10, + "line": 2 + } + }, + "range": [ + 22, + 23 + ], + "type": "Punctuator", + "value": ">" + }, + { + "loc": { + "end": { + "column": 12, + "line": 2 + }, + "start": { + "column": 11, + "line": 2 + } + }, + "range": [ + 23, + 24 + ], + "type": "Punctuator", + "value": "(" + }, + { + "loc": { + "end": { + "column": 13, + "line": 2 + }, + "start": { + "column": 12, + "line": 2 + } + }, + "range": [ + 24, + 25 + ], + "type": "Punctuator", + "value": ")" + }, + { + "loc": { + "end": { + "column": 15, + "line": 2 + }, + "start": { + "column": 14, + "line": 2 + } + }, + "range": [ + 26, + 27 + ], + "type": "Punctuator", + "value": "{" + }, + { + "loc": { + "end": { + "column": 16, + "line": 2 + }, + "start": { + "column": 15, + "line": 2 + } + }, + "range": [ + 27, + 28 + ], + "type": "Punctuator", + "value": "}" + }, + { + "loc": { + "end": { + "column": 1, + "line": 3 + }, + "start": { + "column": 0, + "line": 3 + } + }, + "range": [ + 29, + 30 + ], + "type": "Punctuator", + "value": "}" + } + ], + "type": "Program" +}; diff --git a/tests/fixtures/typescript/basics/class-with-generic-method.src.ts b/tests/fixtures/typescript/basics/class-with-generic-method.src.ts new file mode 100644 index 0000000..abef5e5 --- /dev/null +++ b/tests/fixtures/typescript/basics/class-with-generic-method.src.ts @@ -0,0 +1,3 @@ +class Foo { + getBar() {} +}