From 2f073f62b36ac4d654b869ede43ef0410a7beebf Mon Sep 17 00:00:00 2001 From: Reyad Attiyat Date: Sat, 27 May 2017 14:52:10 -0500 Subject: [PATCH] Fix: Unescape type parameter names (fixes #296) --- lib/convert.js | 50 ++-- ...s-with-type-parameter-underscore.result.js | 242 ++++++++++++++++++ ...lass-with-type-parameter-underscore.src.ts | 1 + .../basics/nested-type-arguments.result.js | 8 +- 4 files changed, 264 insertions(+), 37 deletions(-) create mode 100644 tests/fixtures/typescript/basics/class-with-type-parameter-underscore.result.js create mode 100644 tests/fixtures/typescript/basics/class-with-type-parameter-underscore.src.ts diff --git a/lib/convert.js b/lib/convert.js index d9d7626..326a64a 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -108,27 +108,18 @@ module.exports = function convert(config) { lastTypeArgument.end + 1 ], loc: nodeUtils.getLocFor(firstTypeArgument.pos - 1, lastTypeArgument.end + 1, ast), - params: typeArguments.map(typeArgument => { - /** - * Have to manually calculate the start of the range, - * because TypeScript includes leading whitespace but Flow does not - */ - const typeArgumentStart = (typeArgument.typeName && typeArgument.typeName.text) - ? typeArgument.end - typeArgument.typeName.text.length - : typeArgument.pos; - return { - type: AST_NODE_TYPES.GenericTypeAnnotation, - range: [ - typeArgumentStart, - typeArgument.end - ], - loc: nodeUtils.getLocFor(typeArgumentStart, typeArgument.end, ast), - id: convertChild(typeArgument.typeName || typeArgument), - typeParameters: (typeArgument.typeArguments) - ? convertTypeArgumentsToTypeParameters(typeArgument.typeArguments) - : null - }; - }) + params: typeArguments.map(typeArgument => ({ + type: AST_NODE_TYPES.GenericTypeAnnotation, + range: [ + typeArgument.getStart(), + typeArgument.getEnd() + ], + loc: nodeUtils.getLoc(typeArgument, ast), + id: convertChild(typeArgument.typeName || typeArgument), + typeParameters: (typeArgument.typeArguments) + ? convertTypeArgumentsToTypeParameters(typeArgument.typeArguments) + : null + })) }; } @@ -148,14 +139,7 @@ module.exports = function convert(config) { ], loc: nodeUtils.getLocFor(firstTypeParameter.pos - 1, lastTypeParameter.end + 1, ast), params: typeParameters.map(typeParameter => { - - /** - * Have to manually calculate the start of the range, - * because TypeScript includes leading whitespace but Flow does not - */ - const typeParameterStart = (typeParameter.name && typeParameter.name.text) - ? typeParameter.name.end - typeParameter.name.text.length - : typeParameter.pos; + const name = nodeUtils.unescapeIdentifier(typeParameter.name.text); const defaultParameter = typeParameter.default ? convert({ node: typeParameter.default, parent: typeParameter, ast, additionalOptions }) @@ -164,11 +148,11 @@ module.exports = function convert(config) { return { type: AST_NODE_TYPES.TypeParameter, range: [ - typeParameterStart, - typeParameter.end + typeParameter.getStart(), + typeParameter.getEnd() ], - loc: nodeUtils.getLocFor(typeParameterStart, typeParameter.end, ast), - name: typeParameter.name.text, + loc: nodeUtils.getLoc(typeParameter, ast), + name, constraint: (typeParameter.constraint) ? convertTypeAnnotation(typeParameter.constraint) : null, diff --git a/tests/fixtures/typescript/basics/class-with-type-parameter-underscore.result.js b/tests/fixtures/typescript/basics/class-with-type-parameter-underscore.result.js new file mode 100644 index 0000000..ca918b8 --- /dev/null +++ b/tests/fixtures/typescript/basics/class-with-type-parameter-underscore.result.js @@ -0,0 +1,242 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "range": [ + 0, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "range": [ + 7, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "params": [ + { + "type": "TypeParameter", + "range": [ + 8, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "name": "__P", + "constraint": null + } + ] + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "name": "A" + }, + "body": { + "type": "ClassBody", + "body": [], + "range": [ + 13, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 15 + } + } + }, + "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": "A", + "range": [ + 6, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + { + "type": "Identifier", + "value": "__P", + "range": [ + 8, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "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": [ + 14, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/class-with-type-parameter-underscore.src.ts b/tests/fixtures/typescript/basics/class-with-type-parameter-underscore.src.ts new file mode 100644 index 0000000..f68d76c --- /dev/null +++ b/tests/fixtures/typescript/basics/class-with-type-parameter-underscore.src.ts @@ -0,0 +1 @@ +class A<__P> {} diff --git a/tests/fixtures/typescript/basics/nested-type-arguments.result.js b/tests/fixtures/typescript/basics/nested-type-arguments.result.js index 12a1a89..e8c62c8 100644 --- a/tests/fixtures/typescript/basics/nested-type-arguments.result.js +++ b/tests/fixtures/typescript/basics/nested-type-arguments.result.js @@ -89,12 +89,12 @@ module.exports = { "line": 1 }, "start": { - "column": 38, + "column": 23, "line": 1 } }, "range": [ - 38, + 23, 43 ], "type": "GenericTypeAnnotation", @@ -135,12 +135,12 @@ module.exports = { "line": 1 }, "start": { - "column": 37, + "column": 29, "line": 1 } }, "range": [ - 37, + 29, 42 ], "type": "GenericTypeAnnotation",