From 9e793090c5746a665d6aa2fdfd3e7cbd1cad4568 Mon Sep 17 00:00:00 2001 From: Reyad Attiyat Date: Sat, 29 Apr 2017 15:10:53 -0500 Subject: [PATCH] New: Add support for default type parameters (fixes #235) --- lib/ast-converter.js | 9 +- ...lass-with-generic-method-default.result.js | 484 ++++++++++++++++++ .../class-with-generic-method-default.src.ts | 3 + ...lass-with-type-parameter-default.result.js | 313 +++++++++++ .../class-with-type-parameter-default.src.ts | 3 + .../class-with-type-parameter.result.js | 242 +++++++++ .../basics/class-with-type-parameter.src.ts | 3 + 7 files changed, 1056 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/typescript/basics/class-with-generic-method-default.result.js create mode 100644 tests/fixtures/typescript/basics/class-with-generic-method-default.src.ts create mode 100644 tests/fixtures/typescript/basics/class-with-type-parameter-default.result.js create mode 100644 tests/fixtures/typescript/basics/class-with-type-parameter-default.src.ts create mode 100644 tests/fixtures/typescript/basics/class-with-type-parameter.result.js create mode 100644 tests/fixtures/typescript/basics/class-with-type-parameter.src.ts diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 76805fc..a5f3063 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -618,6 +618,7 @@ module.exports = function(ast, extra) { function convertTSTypeParametersToTypeParametersDeclaration(typeParameters) { var firstTypeParameter = typeParameters[0]; var lastTypeParameter = typeParameters[typeParameters.length - 1]; + return { type: "TypeParameterDeclaration", range: [ @@ -633,6 +634,11 @@ module.exports = function(ast, extra) { var typeParameterStart = (typeParameter.typeName && typeParameter.typeName.text) ? typeParameter.end - typeParameter.typeName.text.length : typeParameter.pos; + + var defaultParameter = typeParameter.default + ? convert(typeParameter.default, typeParameter) + : typeParameter.default; + return { type: "TypeParameter", range: [ @@ -643,7 +649,8 @@ module.exports = function(ast, extra) { name: typeParameter.name.text, constraint: (typeParameter.constraint) ? convertTypeAnnotation(typeParameter.constraint) - : null + : null, + default: defaultParameter }; }) }; diff --git a/tests/fixtures/typescript/basics/class-with-generic-method-default.result.js b/tests/fixtures/typescript/basics/class-with-generic-method-default.result.js new file mode 100644 index 0000000..a93d926 --- /dev/null +++ b/tests/fixtures/typescript/basics/class-with-generic-method-default.result.js @@ -0,0 +1,484 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 36 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "range": [ + 0, + 36 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": "Foo" + }, + "body": { + "type": "ClassBody", + "body": [ + { + "type": "MethodDefinition", + "range": [ + 14, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "key": { + "type": "Identifier", + "range": [ + 14, + 20 + ], + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "name": "getBar" + }, + "value": { + "type": "FunctionExpression", + "id": null, + "generator": false, + "expression": false, + "async": false, + "body": { + "type": "BlockStatement", + "range": [ + 32, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "body": [] + }, + "range": [ + 29, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "params": [], + "typeParameters": { + "type": "TypeParameterDeclaration", + "range": [ + 20, + 29 + ], + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "params": [ + { + "type": "TypeParameter", + "range": [ + 21, + 28 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "name": "T", + "constraint": null, + "default": { + "type": "TSTypeReference", + "range": [ + 25, + 28 + ], + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "typeName": { + "type": "Identifier", + "range": [ + 25, + 28 + ], + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "name": "Bar" + } + } + } + ] + } + }, + "computed": false, + "static": false, + "kind": "method", + "accessibility": null, + "decorators": [] + } + ], + "range": [ + 10, + 36 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "decorators": [] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "class", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "Foo", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "Identifier", + "value": "getBar", + "range": [ + 14, + 20 + ], + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + } + } + }, + { + "type": "Identifier", + "value": "T", + "range": [ + 21, + 22 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + { + "type": "Identifier", + "value": "Bar", + "range": [ + 25, + 28 + ], + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 29, + 30 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 30, + 31 + ], + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 19 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 21 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 35, + 36 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/class-with-generic-method-default.src.ts b/tests/fixtures/typescript/basics/class-with-generic-method-default.src.ts new file mode 100644 index 0000000..957a382 --- /dev/null +++ b/tests/fixtures/typescript/basics/class-with-generic-method-default.src.ts @@ -0,0 +1,3 @@ +class Foo { + getBar() {} +} diff --git a/tests/fixtures/typescript/basics/class-with-type-parameter-default.result.js b/tests/fixtures/typescript/basics/class-with-type-parameter-default.result.js new file mode 100644 index 0000000..a86cefb --- /dev/null +++ b/tests/fixtures/typescript/basics/class-with-type-parameter-default.result.js @@ -0,0 +1,313 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "range": [ + 0, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "range": [ + 9, + 18 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "params": [ + { + "type": "TypeParameter", + "range": [ + 10, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "name": "T", + "constraint": null, + "default": { + "type": "TSTypeReference", + "range": [ + 14, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "typeName": { + "type": "Identifier", + "range": [ + 14, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "name": "Bar" + } + } + } + ] + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": "Foo" + }, + "body": { + "type": "ClassBody", + "body": [], + "range": [ + 19, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "decorators": [] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "class", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "Foo", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 9, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + { + "type": "Identifier", + "value": "T", + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 12, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + } + }, + { + "type": "Identifier", + "value": "Bar", + "range": [ + 14, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 19, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/class-with-type-parameter-default.src.ts b/tests/fixtures/typescript/basics/class-with-type-parameter-default.src.ts new file mode 100644 index 0000000..b416539 --- /dev/null +++ b/tests/fixtures/typescript/basics/class-with-type-parameter-default.src.ts @@ -0,0 +1,3 @@ +class Foo { + +} diff --git a/tests/fixtures/typescript/basics/class-with-type-parameter.result.js b/tests/fixtures/typescript/basics/class-with-type-parameter.result.js new file mode 100644 index 0000000..dac4973 --- /dev/null +++ b/tests/fixtures/typescript/basics/class-with-type-parameter.result.js @@ -0,0 +1,242 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "range": [ + 0, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "range": [ + 9, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "params": [ + { + "type": "TypeParameter", + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "name": "T", + "constraint": null + } + ] + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": "Foo" + }, + "body": { + "type": "ClassBody", + "body": [], + "range": [ + 13, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "decorators": [] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "class", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "Foo", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 9, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + { + "type": "Identifier", + "value": "T", + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 11, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 13, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 16, + 17 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/class-with-type-parameter.src.ts b/tests/fixtures/typescript/basics/class-with-type-parameter.src.ts new file mode 100644 index 0000000..4c6ce17 --- /dev/null +++ b/tests/fixtures/typescript/basics/class-with-type-parameter.src.ts @@ -0,0 +1,3 @@ +class Foo { + +}