From 3109134bcf952e0959dc074074b8ecf5a3118d0e Mon Sep 17 00:00:00 2001 From: weirdpattern Date: Mon, 23 Jan 2017 23:08:04 -0600 Subject: [PATCH 1/2] Fix: Parameter with assignation provide type annotations (fixes #146) --- lib/ast-converter.js | 5 + .../function-with-types-assignation.result.js | 525 ++++++++++++++++++ .../function-with-types-assignation.src.ts | 3 + 3 files changed, 533 insertions(+) create mode 100644 tests/fixtures/typescript/basics/function-with-types-assignation.result.js create mode 100644 tests/fixtures/typescript/basics/function-with-types-assignation.src.ts diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 865023e..9c91b70 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1438,6 +1438,11 @@ module.exports = function(ast, extra) { left: convertChild(node.name), right: convertChild(node.initializer) }); + if (node.type) { + assign(result.left, { + typeAnnotation: convertTypeAnnotation(node.type) + }); + } } else { var convertedParameter = convert(node.name, parent); if (node.type) { 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..af69b4f --- /dev/null +++ b/tests/fixtures/typescript/basics/function-with-types-assignation.result.js @@ -0,0 +1,525 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 63 + ], + "loc": { + "end": { + "line": 3, + "column": 1 + }, + "start": { + "line": 1, + "column": 0 + } + }, + "body": [ + { + "async": false, + "body": { + "body": [ + { + "argument": { + "type": "Identifier", + "range": [ + 56, + 60 + ], + "loc": { + "end": { + "line": 2, + "column": 13 + }, + "start": { + "line": 2, + "column": 9 + } + }, + "name": "name" + }, + "type": "ReturnStatement", + "range": [ + 49, + 61 + ], + "loc": { + "end": { + "line": 2, + "column": 14 + }, + "start": { + "line": 2, + "column": 2 + } + } + } + ], + "type": "BlockStatement", + "range": [ + 45, + 63 + ], + "loc": { + "end": { + "line": 3, + "column": 1 + }, + "start": { + "line": 1, + "column": 45 + } + } + }, + "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, + 63 + ], + "loc": { + "end": { + "line": 3, + "column": 1 + }, + "start": { + "line": 1, + "column": 0 + } + }, + "params": [ + { + "type": "AssignmentPattern", + "range": [ + 17, + 36 + ], + "loc": { + "end": { + "line": 1, + "column": 36 + }, + "start": { + "line": 1, + "column": 17 + } + }, + "left": { + "type": "Identifier", + "range": [ + 17, + 21 + ], + "loc": { + "end": { + "line": 1, + "column": 21 + }, + "start": { + "line": 1, + "column": 17 + } + }, + "name": "name", + "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 + } + } + } + } + }, + "right": { + "type": "Literal", + "range": [ + 31, + 36 + ], + "loc": { + "end": { + "line": 1, + "column": 36 + }, + "start": { + "line": 1, + "column": 31 + } + }, + "raw": "\"foo\"", + "value": "foo" + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "range": [ + 38, + 44 + ], + "loc": { + "end": { + "line": 1, + "column": 44 + }, + "start": { + "line": 1, + "column": 38 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "range": [ + 38, + 44 + ], + "loc": { + "end": { + "line": 1, + "column": 44 + }, + "start": { + "line": 1, + "column": 38 + } + } + } + }, + } + ], + "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": [ + 29, + 30 + ], + "loc": { + "end": { + "line": 1, + "column": 30 + }, + "start": { + "column": 29, + "line": 1 + } + }, + "value": "=" + }, + { + "type": "String", + "range": [ + 31, + 36 + ], + "loc": { + "end": { + "line": 1, + "column": 36 + }, + "start": { + "line": 1, + "column": 31 + } + }, + "value": "\"foo\"" + }, + { + "type": "Punctuator", + "range": [ + 36, + 37 + ], + "loc": { + "end": { + "line": 1, + "column": 37 + }, + "start": { + "line": 1, + "column": 36 + } + }, + "value": ")" + }, + { + "type": "Punctuator", + "range": [ + 37, + 38 + ], + "loc": { + "end": { + "line": 1, + "column": 38 + }, + "start": { + "line": 1, + "column": 37 + } + }, + "value": ":" + }, + { + "type": "Identifier", + "range": [ + 38, + 44 + ], + "loc": { + "end": { + "line": 1, + "column": 44 + }, + "start": { + "line": 1, + "column": 38 + } + }, + "value": "string" + }, + { + "type": "Punctuator", + "range": [ + 45, + 46 + ], + "loc": { + "end": { + "line": 1, + "column": 46 + }, + "start": { + "line": 1, + "column": 45 + } + }, + "value": "{" + }, + { + "type": "Keyword", + "range": [ + 49, + 55 + ], + "loc": { + "end": { + "line": 2, + "column": 8 + }, + "start": { + "line": 2, + "column": 2 + } + }, + "value": "return" + }, + { + "type": "Identifier", + "range": [ + 56, + 60 + ], + "loc": { + "end": { + "line": 2, + "column": 13 + }, + "start": { + "line": 2, + "column": 9 + } + }, + "value": "name" + }, + { + "type": "Punctuator", + "range": [ + 60, + 61 + ], + "loc": { + "end": { + "line": 2, + "column": 14 + }, + "start": { + "line": 2, + "column": 13 + } + }, + "value": ";" + }, + { + "type": "Punctuator", + "range": [ + 62, + 63 + ], + "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..1767487 --- /dev/null +++ b/tests/fixtures/typescript/basics/function-with-types-assignation.src.ts @@ -0,0 +1,3 @@ +function message(name:string = "foo"):string { + return name; +} From 4f94b6036b9f867ad8b48127cead1b931ab69e92 Mon Sep 17 00:00:00 2001 From: weirdpattern Date: Thu, 2 Feb 2017 11:10:35 -0600 Subject: [PATCH 2/2] Adding support for rest arguments --- lib/ast-converter.js | 19 +- .../function-with-types-assignation.result.js | 528 +++++++++++++++--- .../function-with-types-assignation.src.ts | 2 +- 3 files changed, 463 insertions(+), 86 deletions(-) diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 9c91b70..f9a18c7 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1426,23 +1426,20 @@ 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) }); - if (node.type) { - assign(result.left, { - typeAnnotation: convertTypeAnnotation(node.type) - }); - } } else { var convertedParameter = convert(node.name, parent); if (node.type) { @@ -1451,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 index af69b4f..4ca5b48 100644 --- a/tests/fixtures/typescript/basics/function-with-types-assignation.result.js +++ b/tests/fixtures/typescript/basics/function-with-types-assignation.result.js @@ -2,7 +2,7 @@ module.exports = { "type": "Program", "range": [ 0, - 63 + 96 ], "loc": { "end": { @@ -23,8 +23,8 @@ module.exports = { "argument": { "type": "Identifier", "range": [ - 56, - 60 + 89, + 93 ], "loc": { "end": { @@ -40,8 +40,8 @@ module.exports = { }, "type": "ReturnStatement", "range": [ - 49, - 61 + 82, + 94 ], "loc": { "end": { @@ -57,8 +57,8 @@ module.exports = { ], "type": "BlockStatement", "range": [ - 45, - 63 + 78, + 96 ], "loc": { "end": { @@ -67,7 +67,7 @@ module.exports = { }, "start": { "line": 1, - "column": 45 + "column": 78 } } }, @@ -94,7 +94,7 @@ module.exports = { "type": "FunctionDeclaration", "range": [ 0, - 63 + 96 ], "loc": { "end": { @@ -108,67 +108,119 @@ module.exports = { }, "params": [ { - "type": "AssignmentPattern", + "loc": { + "end": { + "line": 1, + "column": 21 + }, + "start": { + "line": 1, + "column": 17 + } + }, + "name": "name", "range": [ 17, - 36 + 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": 36 + "column": 46 }, "start": { "line": 1, - "column": 17 + "column": 30 } }, "left": { "type": "Identifier", "range": [ - 17, - 21 + 30, + 33 ], "loc": { "end": { "line": 1, - "column": 21 + "column": 33 }, "start": { "line": 1, - "column": 17 + "column": 30 } }, - "name": "name", + "name": "age", "typeAnnotation": { "type": "TypeAnnotation", "range": [ - 22, - 28 + 34, + 40 ], "loc": { "end": { "line": 1, - "column": 28 + "column": 40 }, "start": { "line": 1, - "column": 22 + "column": 34 } }, "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TSNumberKeyword", "range": [ - 22, - 28 + 34, + 40 ],"loc": { "end": { "line": 1, - "column": 28 + "column": 40 }, "start": { "line": 1, - "column": 22 + "column": 34 } } } @@ -177,54 +229,160 @@ module.exports = { "right": { "type": "Literal", "range": [ - 31, - 36 + 43, + 46 ], "loc": { "end": { "line": 1, - "column": 36 + "column": 46 }, "start": { "line": 1, - "column": 31 + "column": 43 } }, - "raw": "\"foo\"", - "value": "foo" + "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": [ - 38, - 44 + 71, + 77 ], "loc": { "end": { "line": 1, - "column": 44 + "column": 77 }, "start": { "line": 1, - "column": 38 + "column": 71 } }, "typeAnnotation": { "type": "TSStringKeyword", "range": [ - 38, - 44 + 71, + 77 ], "loc": { "end": { "line": 1, - "column": 44 + "column": 77 }, "start": { "line": 1, - "column": 38 + "column": 71 } } } @@ -233,7 +391,7 @@ module.exports = { ], "sourceType": "script", "tokens": [ - { + { "type": "Keyword", "range": [ 0, @@ -344,53 +502,269 @@ module.exports = { { "type": "Punctuator", "range": [ - 29, - 30 + 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": { - "column": 29, - "line": 1 + "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": "String", + "type": "Numeric", "range": [ - 31, - 36 + 43, + 46 ], "loc": { "end": { "line": 1, - "column": 36 + "column": 46 }, "start": { "line": 1, - "column": 31 + "column": 43 } }, - "value": "\"foo\"" + "value": "100" }, { "type": "Punctuator", "range": [ - 36, - 37 + 46, + 47 ], "loc": { "end": { "line": 1, - "column": 37 + "column": 47 }, "start": { "line": 1, - "column": 36 + "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": ")" @@ -398,17 +772,17 @@ module.exports = { { "type": "Punctuator", "range": [ - 37, - 38 + 70, + 71 ], "loc": { "end": { "line": 1, - "column": 38 + "column": 71 }, "start": { "line": 1, - "column": 37 + "column": 70 } }, "value": ":" @@ -416,17 +790,17 @@ module.exports = { { "type": "Identifier", "range": [ - 38, - 44 + 71, + 77 ], "loc": { "end": { "line": 1, - "column": 44 + "column": 77 }, "start": { "line": 1, - "column": 38 + "column": 71 } }, "value": "string" @@ -434,17 +808,17 @@ module.exports = { { "type": "Punctuator", "range": [ - 45, - 46 + 78, + 79 ], "loc": { "end": { "line": 1, - "column": 46 + "column": 79 }, "start": { "line": 1, - "column": 45 + "column": 78 } }, "value": "{" @@ -452,8 +826,8 @@ module.exports = { { "type": "Keyword", "range": [ - 49, - 55 + 82, + 88 ], "loc": { "end": { @@ -470,8 +844,8 @@ module.exports = { { "type": "Identifier", "range": [ - 56, - 60 + 89, + 93 ], "loc": { "end": { @@ -488,8 +862,8 @@ module.exports = { { "type": "Punctuator", "range": [ - 60, - 61 + 93, + 94 ], "loc": { "end": { @@ -506,8 +880,8 @@ module.exports = { { "type": "Punctuator", "range": [ - 62, - 63 + 95, + 96 ], "loc": { "end": { @@ -522,4 +896,4 @@ module.exports = { "value": "}" } ] -} \ No newline at end of file +}; \ 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 index 1767487..b6d3f08 100644 --- a/tests/fixtures/typescript/basics/function-with-types-assignation.src.ts +++ b/tests/fixtures/typescript/basics/function-with-types-assignation.src.ts @@ -1,3 +1,3 @@ -function message(name:string = "foo"):string { +function message(name:string, age:number = 100, ...args:Array):string { return name; }