diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 865023e..f9a18c7 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1426,16 +1426,18 @@ module.exports = function(ast, extra) { break; case SyntaxKind.Parameter: - + var parameter; if (node.dotDotDotToken) { + parameter = convertChild(node.name); assign(result, { type: "RestElement", - argument: convertChild(node.name) + argument: parameter }); } else if (node.initializer) { + parameter = convertChild(node.name); assign(result, { type: "AssignmentPattern", - left: convertChild(node.name), + left: parameter, right: convertChild(node.initializer) }); } else { @@ -1446,6 +1448,12 @@ module.exports = function(ast, extra) { return convertedParameter; } + if (node.type) { + assign(parameter, { + typeAnnotation: convertTypeAnnotation(node.type) + }); + } + break; // Classes diff --git a/tests/fixtures/typescript/basics/function-with-types-assignation.result.js b/tests/fixtures/typescript/basics/function-with-types-assignation.result.js new file mode 100644 index 0000000..4ca5b48 --- /dev/null +++ b/tests/fixtures/typescript/basics/function-with-types-assignation.result.js @@ -0,0 +1,899 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 96 + ], + "loc": { + "end": { + "line": 3, + "column": 1 + }, + "start": { + "line": 1, + "column": 0 + } + }, + "body": [ + { + "async": false, + "body": { + "body": [ + { + "argument": { + "type": "Identifier", + "range": [ + 89, + 93 + ], + "loc": { + "end": { + "line": 2, + "column": 13 + }, + "start": { + "line": 2, + "column": 9 + } + }, + "name": "name" + }, + "type": "ReturnStatement", + "range": [ + 82, + 94 + ], + "loc": { + "end": { + "line": 2, + "column": 14 + }, + "start": { + "line": 2, + "column": 2 + } + } + } + ], + "type": "BlockStatement", + "range": [ + 78, + 96 + ], + "loc": { + "end": { + "line": 3, + "column": 1 + }, + "start": { + "line": 1, + "column": 78 + } + } + }, + "expression": false, + "generator": false, + "id": { + "type": "Identifier", + "range": [ + 9, + 16 + ], + "loc": { + "end": { + "line": 1, + "column": 16 + }, + "start": { + "line": 1, + "column": 9 + } + }, + "name": "message" + }, + "type": "FunctionDeclaration", + "range": [ + 0, + 96 + ], + "loc": { + "end": { + "line": 3, + "column": 1 + }, + "start": { + "line": 1, + "column": 0 + } + }, + "params": [ + { + "loc": { + "end": { + "line": 1, + "column": 21 + }, + "start": { + "line": 1, + "column": 17 + } + }, + "name": "name", + "range": [ + 17, + 21 + ], + "type": "Identifier", + "typeAnnotation": { + "type": "TypeAnnotation", + "range": [ + 22, + 28 + ], + "loc": { + "end": { + "line": 1, + "column": 28 + }, + "start": { + "line": 1, + "column": 22 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "range": [ + 22, + 28 + ], + "loc": { + "end": { + "line": 1, + "column": 28 + }, + "start": { + "line": 1, + "column": 22 + } + } + } + } + }, + { + "type": "AssignmentPattern", + "range": [ + 30, + 46 + ], + "loc": { + "end": { + "line": 1, + "column": 46 + }, + "start": { + "line": 1, + "column": 30 + } + }, + "left": { + "type": "Identifier", + "range": [ + 30, + 33 + ], + "loc": { + "end": { + "line": 1, + "column": 33 + }, + "start": { + "line": 1, + "column": 30 + } + }, + "name": "age", + "typeAnnotation": { + "type": "TypeAnnotation", + "range": [ + 34, + 40 + ], + "loc": { + "end": { + "line": 1, + "column": 40 + }, + "start": { + "line": 1, + "column": 34 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "range": [ + 34, + 40 + ],"loc": { + "end": { + "line": 1, + "column": 40 + }, + "start": { + "line": 1, + "column": 34 + } + } + } + } + }, + "right": { + "type": "Literal", + "range": [ + 43, + 46 + ], + "loc": { + "end": { + "line": 1, + "column": 46 + }, + "start": { + "line": 1, + "column": 43 + } + }, + "raw": "100", + "value": 100 + } + }, + { + "argument": { + "loc": { + "end": { + "line": 1, + "column": 55 + }, + "start": { + "line": 1, + "column": 51 + } + }, + "name": "args", + "range": [ + 51, + 55 + ], + "type": "Identifier", + "typeAnnotation": { + "loc": { + "end": { + "line": 1, + "column": 69 + }, + "start": { + "line": 1, + "column": 56 + } + }, + "range": [ + 56, + 69 + ], + "type": "TypeAnnotation", + "typeAnnotation": { + "loc": { + "end": { + "line": 1, + "column": 69 + }, + "start": { + "line": 1, + "column": 56 + } + }, + "range": [ + 56, + 69 + ], + "type": "TSTypeReference", + "typeArguments": [ + { + "loc": { + "end": { + "line": 1, + "column": 68 + }, + "start": { + "line": 1, + "column": 62 + } + }, + "range": [ + 62, + 68 + ], + "type": "TSStringKeyword" + } + ], + "typeName": { + "loc": { + "end": { + "line": 1, + "column": 61 + }, + "start": { + "line": 1, + "column": 56 + } + }, + "name": "Array", + "range": [ + 56, + 61 + ], + "type": "Identifier" + } + } + } + }, + "loc": { + "end": { + "line": 1, + "column": 69 + }, + "start": { + "line": 1, + "column": 48 + } + }, + "range": [ + 48, + 69 + ], + "type": "RestElement" + } + ], + "returnType": { + "type": "TypeAnnotation", + "range": [ + 71, + 77 + ], + "loc": { + "end": { + "line": 1, + "column": 77 + }, + "start": { + "line": 1, + "column": 71 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "range": [ + 71, + 77 + ], + "loc": { + "end": { + "line": 1, + "column": 77 + }, + "start": { + "line": 1, + "column": 71 + } + } + } + }, + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "range": [ + 0, + 8 + ], + "loc": { + "end": { + "line": 1, + "column": 8 + }, + "start": { + "line": 1, + "column": 0 + } + }, + "value": "function" + }, + { + "type": "Identifier", + "range": [ + 9, + 16 + ], + "loc": { + "end": { + "line": 1, + "column": 16 + }, + "start": { + "line": 1, + "column": 9 + } + }, + "value": "message" + }, + { + "type": "Punctuator", + "range": [ + 16, + 17 + ], + "loc": { + "end": { + "line": 1, + "column": 17 + }, + "start": { + "line": 1, + "column": 16 + } + }, + "value": "(" + }, + { + "type": "Identifier", + "range": [ + 17, + 21 + ], + "loc": { + "end": { + "line": 1, + "column": 21 + }, + "start": { + "line": 1, + "column": 17 + } + }, + "value": "name" + }, + { + "type": "Punctuator", + "range": [ + 21, + 22 + ], + "loc": { + "end": { + "line": 1, + "column": 22 + }, + "start": { + "line": 1, + "column": 21 + } + }, + "value": ":" + }, + { + "type": "Identifier", + "range": [ + 22, + 28 + ], + "loc": { + "end": { + "line": 1, + "column": 28 + }, + "start": { + "line": 1, + "column": 22 + } + }, + "value": "string" + }, + { + "type": "Punctuator", + "range": [ + 28, + 29 + ], + "loc": { + "end": { + "line": 1, + "column": 29 + }, + "start": { + "line": 1, + "column": 28 + } + }, + "value": "," + }, + { + "type": "Identifier", + "range": [ + 30, + 33 + ], + "loc": { + "end": { + "line": 1, + "column": 33 + }, + "start": { + "line": 1, + "column": 30 + } + }, + "value": "age" + }, + { + "type": "Punctuator", + "range": [ + 33, + 34 + ], + "loc": { + "end": { + "line": 1, + "column": 34 + }, + "start": { + "line": 1, + "column": 33 + } + }, + "value": ":" + }, + { + "type": "Identifier", + "range": [ + 34, + 40 + ], + "loc": { + "end": { + "line": 1, + "column": 40 + }, + "start": { + "line": 1, + "column": 34 + } + }, + "value": "number" + }, + { + "type": "Punctuator", + "range": [ + 41, + 42 + ], + "loc": { + "end": { + "line": 1, + "column": 42 + }, + "start": { + "line": 1, + "column": 41 + } + }, + "value": "=" + }, + { + "type": "Numeric", + "range": [ + 43, + 46 + ], + "loc": { + "end": { + "line": 1, + "column": 46 + }, + "start": { + "line": 1, + "column": 43 + } + }, + "value": "100" + }, + { + "type": "Punctuator", + "range": [ + 46, + 47 + ], + "loc": { + "end": { + "line": 1, + "column": 47 + }, + "start": { + "line": 1, + "column": 46 + } + }, + "value": "," + }, + { + "type": "Punctuator", + "range": [ + 48, + 51 + ], + "loc": { + "end": { + "line": 1, + "column": 51 + }, + "start": { + "line": 1, + "column": 48 + } + }, + "value": "..." + }, + { + "type": "Identifier", + "range": [ + 51, + 55 + ], + "loc": { + "end": { + "line": 1, + "column": 55 + }, + "start": { + "line": 1, + "column": 51 + } + }, + "value": "args" + }, + { + "type": "Punctuator", + "range": [ + 55, + 56 + ], + "loc": { + "end": { + "line": 1, + "column": 56 + }, + "start": { + "line": 1, + "column": 55 + } + }, + "value": ":" + }, + { + "type": "Identifier", + "range": [ + 56, + 61 + ], + "loc": { + "end": { + "line": 1, + "column": 61 + }, + "start": { + "line": 1, + "column": 56 + } + }, + "value": "Array" + }, + { + "type": "Punctuator", + "range": [ + 61, + 62 + ], + "loc": { + "end": { + "line": 1, + "column": 62 + }, + "start": { + "line": 1, + "column": 61 + } + }, + "value": "<" + }, + { + "type": "Identifier", + "range": [ + 62, + 68 + ], + "loc": { + "end": { + "line": 1, + "column": 68 + }, + "start": { + "line": 1, + "column": 62 + } + }, + "value": "string" + }, + { + "type": "Punctuator", + "range": [ + 68, + 69 + ], + "loc": { + "end": { + "line": 1, + "column": 69 + }, + "start": { + "line": 1, + "column": 68 + } + }, + "value": ">" + }, + { + "type": "Punctuator", + "range": [ + 69, + 70 + ], + "loc": { + "end": { + "line": 1, + "column": 70 + }, + "start": { + "line": 1, + "column": 69 + } + }, + "value": ")" + }, + { + "type": "Punctuator", + "range": [ + 70, + 71 + ], + "loc": { + "end": { + "line": 1, + "column": 71 + }, + "start": { + "line": 1, + "column": 70 + } + }, + "value": ":" + }, + { + "type": "Identifier", + "range": [ + 71, + 77 + ], + "loc": { + "end": { + "line": 1, + "column": 77 + }, + "start": { + "line": 1, + "column": 71 + } + }, + "value": "string" + }, + { + "type": "Punctuator", + "range": [ + 78, + 79 + ], + "loc": { + "end": { + "line": 1, + "column": 79 + }, + "start": { + "line": 1, + "column": 78 + } + }, + "value": "{" + }, + { + "type": "Keyword", + "range": [ + 82, + 88 + ], + "loc": { + "end": { + "line": 2, + "column": 8 + }, + "start": { + "line": 2, + "column": 2 + } + }, + "value": "return" + }, + { + "type": "Identifier", + "range": [ + 89, + 93 + ], + "loc": { + "end": { + "line": 2, + "column": 13 + }, + "start": { + "line": 2, + "column": 9 + } + }, + "value": "name" + }, + { + "type": "Punctuator", + "range": [ + 93, + 94 + ], + "loc": { + "end": { + "line": 2, + "column": 14 + }, + "start": { + "line": 2, + "column": 13 + } + }, + "value": ";" + }, + { + "type": "Punctuator", + "range": [ + 95, + 96 + ], + "loc": { + "end": { + "line": 3, + "column": 1 + }, + "start": { + "line": 3, + "column": 0 + } + }, + "value": "}" + } + ] +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/basics/function-with-types-assignation.src.ts b/tests/fixtures/typescript/basics/function-with-types-assignation.src.ts new file mode 100644 index 0000000..b6d3f08 --- /dev/null +++ b/tests/fixtures/typescript/basics/function-with-types-assignation.src.ts @@ -0,0 +1,3 @@ +function message(name:string, age:number = 100, ...args:Array):string { + return name; +}