diff --git a/lib/ast-converter.js b/lib/ast-converter.js index c7132e1..ea8a187 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -546,7 +546,7 @@ module.exports = function(ast, extra) { typeArgument.end ], loc: getLocFor(typeArgumentStart, typeArgument.end, ast), - id: convertChild(typeArgument.typeName) + id: convertChild(typeArgument.typeName || typeArgument) }; }) }; @@ -1518,6 +1518,7 @@ module.exports = function(ast, extra) { if (!lastClassToken || lastTypeParameter.pos > lastClassToken.pos) { lastClassToken = ts.findNextToken(lastTypeParameter, ast); } + result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters); } if (node.modifiers && node.modifiers.length) { @@ -1816,6 +1817,9 @@ module.exports = function(ast, extra) { callee: convertChild(node.expression), arguments: node.arguments.map(convertChild) }); + if (node.typeArguments && node.typeArguments.length) { + result.typeParameters = convertTypeArgumentsToTypeParameters(node.typeArguments); + } break; case SyntaxKind.NewExpression: @@ -1824,6 +1828,9 @@ module.exports = function(ast, extra) { callee: convertChild(node.expression), arguments: (node.arguments) ? node.arguments.map(convertChild) : [] }); + if (node.typeArguments && node.typeArguments.length) { + result.typeParameters = convertTypeArgumentsToTypeParameters(node.typeArguments); + } break; case SyntaxKind.MetaProperty: 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 index 870644f..6b048df 100644 --- a/tests/fixtures/typescript/basics/export-default-class-with-generic.result.js +++ b/tests/fixtures/typescript/basics/export-default-class-with-generic.result.js @@ -34,6 +34,44 @@ module.exports = { } }, "id": null, + "typeParameters": { + "loc": { + "end": { + "column": 23, + "line": 1 + }, + "start": { + "column": 20, + "line": 1 + } + }, + "params": [ + { + "loc": { + "end": { + "column": 22, + "line": 1 + }, + "start": { + "column": 21, + "line": 1 + } + }, + "name": "T", + "range": [ + 21, + 22 + ], + "type": "TypeParameter", + "constraint": null + } + ], + "range": [ + 20, + 23 + ], + "type": "TypeParameterDeclaration" + }, "body": { "type": "ClassBody", "body": [], 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 index 582090a..e858b34 100644 --- 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 @@ -34,6 +34,63 @@ module.exports = { } }, "id": null, + "typeParameters": { + "loc": { + "end": { + "column": 26, + "line": 1 + }, + "start": { + "column": 20, + "line": 1 + } + }, + "params": [ + { + "loc": { + "end": { + "column": 22, + "line": 1 + }, + "start": { + "column": 21, + "line": 1 + } + }, + "name": "T", + "range": [ + 21, + 22 + ], + "type": "TypeParameter", + "constraint": null + }, + { + "loc": { + "end": { + "column": 25, + "line": 1 + }, + "start": { + "column": 23, + "line": 1 + } + }, + "name": "U", + "range": [ + 23, + 25 + ], + "type": "TypeParameter", + "constraint": null + } + ], + "range": [ + 20, + 26 + ], + "type": "TypeParameterDeclaration" + }, "body": { "type": "ClassBody", "body": [], 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 index 510f54d..cfbd485 100644 --- a/tests/fixtures/typescript/basics/export-named-class-with-generic.result.js +++ b/tests/fixtures/typescript/basics/export-named-class-with-generic.result.js @@ -51,6 +51,44 @@ module.exports = { }, "name": "Foo" }, + "typeParameters": { + "loc": { + "end": { + "column": 19, + "line": 1 + }, + "start": { + "column": 16, + "line": 1 + } + }, + "params": [ + { + "loc": { + "end": { + "column": 18, + "line": 1 + }, + "start": { + "column": 17, + "line": 1 + } + }, + "name": "T", + "range": [ + 17, + 18 + ], + "type": "TypeParameter", + "constraint": null + } + ], + "range": [ + 16, + 19 + ], + "type": "TypeParameterDeclaration" + }, "body": { "type": "ClassBody", "body": [], 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 index 653ba52..759414a 100644 --- 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 @@ -51,6 +51,63 @@ module.exports = { }, "name": "Foo" }, + "typeParameters": { + "loc": { + "end": { + "column": 22, + "line": 1 + }, + "start": { + "column": 16, + "line": 1 + } + }, + "params": [ + { + "loc": { + "end": { + "column": 18, + "line": 1 + }, + "start": { + "column": 17, + "line": 1 + } + }, + "name": "T", + "range": [ + 17, + 18 + ], + "type": "TypeParameter", + "constraint": null + }, + { + "loc": { + "end": { + "column": 21, + "line": 1 + }, + "start": { + "column": 19, + "line": 1 + } + }, + "name": "U", + "range": [ + 19, + 21 + ], + "type": "TypeParameter", + "constraint": null + } + ], + "range": [ + 16, + 22 + ], + "type": "TypeParameterDeclaration" + }, "body": { "type": "ClassBody", "body": [], diff --git a/tests/fixtures/typescript/expressions/call-expression-type-arguments.result.js b/tests/fixtures/typescript/expressions/call-expression-type-arguments.result.js new file mode 100644 index 0000000..e8077a5 --- /dev/null +++ b/tests/fixtures/typescript/expressions/call-expression-type-arguments.result.js @@ -0,0 +1,487 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "body": [ + { + "expression": { + "arguments": [], + "callee": { + "loc": { + "end": { + "column": 3, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "name": "foo", + "range": [ + 0, + 3 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 8, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 8 + ], + "type": "CallExpression", + "typeParameters": { + "loc": { + "end": { + "column": 6, + "line": 1 + }, + "start": { + "column": 3, + "line": 1 + } + }, + "params": [ + { + "id": { + "loc": { + "end": { + "column": 5, + "line": 1 + }, + "start": { + "column": 4, + "line": 1 + } + }, + "name": "A", + "range": [ + 4, + 5 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 5, + "line": 1 + }, + "start": { + "column": 4, + "line": 1 + } + }, + "range": [ + 4, + 5 + ], + "type": "GenericTypeAnnotation" + } + ], + "range": [ + 3, + 6 + ], + "type": "TypeParameterInstantiation" + } + }, + "loc": { + "end": { + "column": 9, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 9 + ], + "type": "ExpressionStatement" + }, + { + "expression": { + "arguments": [], + "callee": { + "loc": { + "end": { + "column": 3, + "line": 2 + }, + "start": { + "column": 0, + "line": 2 + } + }, + "name": "foo", + "range": [ + 10, + 13 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 13, + "line": 2 + }, + "start": { + "column": 0, + "line": 2 + } + }, + "range": [ + 10, + 23 + ], + "type": "CallExpression", + "typeParameters": { + "loc": { + "end": { + "column": 11, + "line": 2 + }, + "start": { + "column": 3, + "line": 2 + } + }, + "params": [ + { + "id": { + "loc": { + "end": { + "column": 10, + "line": 2 + }, + "start": { + "column": 4, + "line": 2 + } + }, + "range": [ + 14, + 20 + ], + "type": "TSNumberKeyword" + }, + "loc": { + "end": { + "column": 10, + "line": 2 + }, + "start": { + "column": 4, + "line": 2 + } + }, + "range": [ + 14, + 20 + ], + "type": "GenericTypeAnnotation" + } + ], + "range": [ + 13, + 21 + ], + "type": "TypeParameterInstantiation" + } + }, + "loc": { + "end": { + "column": 14, + "line": 2 + }, + "start": { + "column": 0, + "line": 2 + } + }, + "range": [ + 10, + 24 + ], + "type": "ExpressionStatement" + } + ], + "sourceType": "script", + "tokens": [ + { + "loc": { + "end": { + "column": 3, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 3 + ], + "type": "Identifier", + "value": "foo" + }, + { + "loc": { + "end": { + "column": 4, + "line": 1 + }, + "start": { + "column": 3, + "line": 1 + } + }, + "range": [ + 3, + 4 + ], + "type": "Punctuator", + "value": "<" + }, + { + "loc": { + "end": { + "column": 5, + "line": 1 + }, + "start": { + "column": 4, + "line": 1 + } + }, + "range": [ + 4, + 5 + ], + "type": "Identifier", + "value": "A" + }, + { + "loc": { + "end": { + "column": 6, + "line": 1 + }, + "start": { + "column": 5, + "line": 1 + } + }, + "range": [ + 5, + 6 + ], + "type": "Punctuator", + "value": ">" + }, + { + "loc": { + "end": { + "column": 7, + "line": 1 + }, + "start": { + "column": 6, + "line": 1 + } + }, + "range": [ + 6, + 7 + ], + "type": "Punctuator", + "value": "(" + }, + { + "loc": { + "end": { + "column": 8, + "line": 1 + }, + "start": { + "column": 7, + "line": 1 + } + }, + "range": [ + 7, + 8 + ], + "type": "Punctuator", + "value": ")" + }, + { + "loc": { + "end": { + "column": 9, + "line": 1 + }, + "start": { + "column": 8, + "line": 1 + } + }, + "range": [ + 8, + 9 + ], + "type": "Punctuator", + "value": ";" + }, + { + "loc": { + "end": { + "column": 3, + "line": 2 + }, + "start": { + "column": 0, + "line": 2 + } + }, + "range": [ + 10, + 13 + ], + "type": "Identifier", + "value": "foo" + }, + { + "loc": { + "end": { + "column": 4, + "line": 2 + }, + "start": { + "column": 3, + "line": 2 + } + }, + "range": [ + 13, + 14 + ], + "type": "Punctuator", + "value": "<" + }, + { + "loc": { + "end": { + "column": 10, + "line": 2 + }, + "start": { + "column": 4, + "line": 2 + } + }, + "range": [ + 14, + 20 + ], + "type": "Identifier", + "value": "number" + }, + { + "loc": { + "end": { + "column": 11, + "line": 2 + }, + "start": { + "column": 10, + "line": 2 + } + }, + "range": [ + 20, + 21 + ], + "type": "Punctuator", + "value": ">" + }, + { + "loc": { + "end": { + "column": 12, + "line": 2 + }, + "start": { + "column": 11, + "line": 2 + } + }, + "range": [ + 21, + 22 + ], + "type": "Punctuator", + "value": "(" + }, + { + "loc": { + "end": { + "column": 13, + "line": 2 + }, + "start": { + "column": 12, + "line": 2 + } + }, + "range": [ + 22, + 23 + ], + "type": "Punctuator", + "value": ")" + }, + { + "loc": { + "end": { + "column": 14, + "line": 2 + }, + "start": { + "column": 13, + "line": 2 + } + }, + "range": [ + 23, + 24 + ], + "type": "Punctuator", + "value": ";" + } + ] +}; diff --git a/tests/fixtures/typescript/expressions/call-expression-type-arguments.src.ts b/tests/fixtures/typescript/expressions/call-expression-type-arguments.src.ts new file mode 100644 index 0000000..2421c8b --- /dev/null +++ b/tests/fixtures/typescript/expressions/call-expression-type-arguments.src.ts @@ -0,0 +1,2 @@ +foo(); +foo(); \ No newline at end of file diff --git a/tests/fixtures/typescript/expressions/new-expression-type-arguments.result.js b/tests/fixtures/typescript/expressions/new-expression-type-arguments.result.js new file mode 100644 index 0000000..5110a6f --- /dev/null +++ b/tests/fixtures/typescript/expressions/new-expression-type-arguments.result.js @@ -0,0 +1,365 @@ +module.exports = { + "body": [ + { + "declarations": [ + { + "id": { + "loc": { + "end": { + "column": 7, + "line": 1 + }, + "start": { + "column": 6, + "line": 1 + } + }, + "name": "a", + "range": [ + 6, + 7 + ], + "type": "Identifier" + }, + "init": { + "arguments": [], + "callee": { + "loc": { + "end": { + "column": 15, + "line": 1 + }, + "start": { + "column": 14, + "line": 1 + } + }, + "name": "A", + "range": [ + 14, + 15 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 20, + "line": 1 + }, + "start": { + "column": 10, + "line": 1 + } + }, + "range": [ + 10, + 20 + ], + "type": "NewExpression", + "typeParameters": { + "loc": { + "end": { + "column": 18, + "line": 1 + }, + "start": { + "column": 15, + "line": 1 + } + }, + "params": [ + { + "id": { + "loc": { + "end": { + "column": 17, + "line": 1 + }, + "start": { + "column": 16, + "line": 1 + } + }, + "name": "B", + "range": [ + 16, + 17 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 17, + "line": 1 + }, + "start": { + "column": 16, + "line": 1 + } + }, + "range": [ + 16, + 17 + ], + "type": "GenericTypeAnnotation" + } + ], + "range": [ + 15, + 18 + ], + "type": "TypeParameterInstantiation" + } + }, + "loc": { + "end": { + "column": 20, + "line": 1 + }, + "start": { + "column": 6, + "line": 1 + } + }, + "range": [ + 6, + 20 + ], + "type": "VariableDeclarator" + } + ], + "kind": "const", + "loc": { + "end": { + "column": 21, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 21 + ], + "type": "VariableDeclaration" + } + ], + "loc": { + "end": { + "column": 21, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 21 + ], + "sourceType": "script", + "tokens": [ + { + "loc": { + "end": { + "column": 5, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 5 + ], + "type": "Keyword", + "value": "const" + }, + { + "loc": { + "end": { + "column": 7, + "line": 1 + }, + "start": { + "column": 6, + "line": 1 + } + }, + "range": [ + 6, + 7 + ], + "type": "Identifier", + "value": "a" + }, + { + "loc": { + "end": { + "column": 9, + "line": 1 + }, + "start": { + "column": 8, + "line": 1 + } + }, + "range": [ + 8, + 9 + ], + "type": "Punctuator", + "value": "=" + }, + { + "loc": { + "end": { + "column": 13, + "line": 1 + }, + "start": { + "column": 10, + "line": 1 + } + }, + "range": [ + 10, + 13 + ], + "type": "Keyword", + "value": "new" + }, + { + "loc": { + "end": { + "column": 15, + "line": 1 + }, + "start": { + "column": 14, + "line": 1 + } + }, + "range": [ + 14, + 15 + ], + "type": "Identifier", + "value": "A" + }, + { + "loc": { + "end": { + "column": 16, + "line": 1 + }, + "start": { + "column": 15, + "line": 1 + } + }, + "range": [ + 15, + 16 + ], + "type": "Punctuator", + "value": "<" + }, + { + "loc": { + "end": { + "column": 17, + "line": 1 + }, + "start": { + "column": 16, + "line": 1 + } + }, + "range": [ + 16, + 17 + ], + "type": "Identifier", + "value": "B" + }, + { + "loc": { + "end": { + "column": 18, + "line": 1 + }, + "start": { + "column": 17, + "line": 1 + } + }, + "range": [ + 17, + 18 + ], + "type": "Punctuator", + "value": ">" + }, + { + "loc": { + "end": { + "column": 19, + "line": 1 + }, + "start": { + "column": 18, + "line": 1 + } + }, + "range": [ + 18, + 19 + ], + "type": "Punctuator", + "value": "(" + }, + { + "loc": { + "end": { + "column": 20, + "line": 1 + }, + "start": { + "column": 19, + "line": 1 + } + }, + "range": [ + 19, + 20 + ], + "type": "Punctuator", + "value": ")" + }, + { + "loc": { + "end": { + "column": 21, + "line": 1 + }, + "start": { + "column": 20, + "line": 1 + } + }, + "range": [ + 20, + 21 + ], + "type": "Punctuator", + "value": ";" + } + ], + "type": "Program" +}; diff --git a/tests/fixtures/typescript/expressions/new-expression-type-arguments.src.ts b/tests/fixtures/typescript/expressions/new-expression-type-arguments.src.ts new file mode 100644 index 0000000..7e38b6d --- /dev/null +++ b/tests/fixtures/typescript/expressions/new-expression-type-arguments.src.ts @@ -0,0 +1 @@ +const a = new A(); \ No newline at end of file