From 272ad26115172c1726673edd81ea57b8d8917c3f Mon Sep 17 00:00:00 2001 From: James Henry Date: Fri, 16 Sep 2016 22:15:08 +0100 Subject: [PATCH] Fix: Convert TypeAliasDeclaration into VariableDeclarator (fixes #89) --- lib/ast-converter.js | 12 +- .../basics/type-alias-declaration.result.js | 347 +++++++++--------- 2 files changed, 176 insertions(+), 183 deletions(-) diff --git a/lib/ast-converter.js b/lib/ast-converter.js index dabdb60..4b559fb 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1839,11 +1839,19 @@ module.exports = function(ast, extra) { * to allow core rules such as "semi" to work automatically */ case SyntaxKind.TypeAliasDeclaration: - deeplyCopy(); + var typeAliasDeclarator = { + type: "VariableDeclarator", + id: convertChild(node.name), + init: convertChild(node.type) + }; + // Process typeParameters + if (node.typeParameters && node.typeParameters.length) { + typeAliasDeclarator.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters); + } assign(result, { type: "VariableDeclaration", kind: "type", - declarations: [] + declarations: [typeAliasDeclarator] }); break; diff --git a/tests/fixtures/typescript/basics/type-alias-declaration.result.js b/tests/fixtures/typescript/basics/type-alias-declaration.result.js index af8e9a7..ad81329 100644 --- a/tests/fixtures/typescript/basics/type-alias-declaration.result.js +++ b/tests/fixtures/typescript/basics/type-alias-declaration.result.js @@ -18,223 +18,208 @@ module.exports = { { "type": "VariableDeclaration", "kind": "type", - "declarations": [], - "range": [ - 0, - 37 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 37 - } - }, - "flags": 0, - "name": { - "type": "Identifier", - "range": [ - 5, - 11 - ], - "loc": { - "start": { - "line": 1, - "column": 5 - }, - "end": { - "line": 1, - "column": 11 - } - }, - "name": "Result" - }, - "typeParameters": [ + "declarations": [ { - "type": "TSTypeParameter", - "range": [ - 12, - 13 - ], - "loc": { - "start": { - "line": 1, - "column": 12 - }, - "end": { - "line": 1, - "column": 13 - } - }, - "flags": 0, - "name": { + "type": "VariableDeclarator", + "id": { "type": "Identifier", "range": [ - 12, - 13 + 5, + 11 ], "loc": { "start": { "line": 1, - "column": 12 + "column": 5 }, "end": { "line": 1, - "column": 13 + "column": 11 } }, - "name": "T" - } - } - ], - "typeAnnotation": { - "type": "TypeAnnotation", - "loc": { - "start": { - "line": 1, - "column": 17 + "name": "Result" }, - "end": { - "line": 1, - "column": 37 - } - }, - "range": [ - 17, - 37 - ], - "typeAnnotation": { - "type": "TSUnionType", - "range": [ - 17, - 37 - ], - "loc": { - "start": { - "line": 1, - "column": 17 + "typeParameters": { + "loc": { + "end": { + "column": 14, + "line": 1 + }, + "start": { + "column": 11, + "line": 1 + } }, - "end": { - "line": 1, - "column": 37 - } - }, - "flags": 0, - "types": [ - { - "type": "TSTypeReference", - "range": [ - 17, - 27 - ], - "loc": { - "start": { - "line": 1, - "column": 17 + "params": [ + { + "loc": { + "end": { + "column": 13, + "line": 1 + }, + "start": { + "column": 12, + "line": 1 + } }, - "end": { - "line": 1, - "column": 27 - } - }, - "flags": 0, - "typeName": { - "type": "Identifier", + "name": "T", "range": [ - 17, - 24 + 12, + 13 ], + "type": "TypeParameter" + } + ], + "range": [ + 11, + 14 + ], + "type": "TypeParameterDeclaration" + }, + "init": { + "flags": 0, + "loc": { + "end": { + "column": 37, + "line": 1 + }, + "start": { + "column": 17, + "line": 1 + } + }, + "range": [ + 17, + 37 + ], + "type": "TSUnionType", + "types": [ + { + "flags": 0, "loc": { - "start": { - "line": 1, - "column": 17 - }, "end": { - "line": 1, - "column": 24 + "column": 27, + "line": 1 + }, + "start": { + "column": 17, + "line": 1 } }, - "name": "Success" - }, - "typeArguments": [ - { - "type": "TSTypeReference", - "range": [ - 25, - 26 - ], - "loc": { - "start": { - "line": 1, - "column": 25 + "range": [ + 17, + 27 + ], + "type": "TSTypeReference", + "typeArguments": [ + { + "flags": 0, + "loc": { + "end": { + "column": 26, + "line": 1 + }, + "start": { + "column": 25, + "line": 1 + } }, - "end": { - "line": 1, - "column": 26 - } - }, - "flags": 0, - "typeName": { - "type": "Identifier", "range": [ 25, 26 ], - "loc": { - "start": { - "line": 1, - "column": 25 + "type": "TSTypeReference", + "typeName": { + "loc": { + "end": { + "column": 26, + "line": 1 + }, + "start": { + "column": 25, + "line": 1 + } }, - "end": { - "line": 1, - "column": 26 - } - }, - "name": "T" + "name": "T", + "range": [ + 25, + 26 + ], + "type": "Identifier" + } } - } - ] - }, - { - "type": "TSTypeReference", - "range": [ - 30, - 37 - ], - "loc": { - "start": { - "line": 1, - "column": 30 - }, - "end": { - "line": 1, - "column": 37 + ], + "typeName": { + "loc": { + "end": { + "column": 24, + "line": 1 + }, + "start": { + "column": 17, + "line": 1 + } + }, + "name": "Success", + "range": [ + 17, + 24 + ], + "type": "Identifier" } }, - "flags": 0, - "typeName": { - "type": "Identifier", + { + "flags": 0, + "loc": { + "end": { + "column": 37, + "line": 1 + }, + "start": { + "column": 30, + "line": 1 + } + }, "range": [ 30, 37 ], - "loc": { - "start": { - "line": 1, - "column": 30 + "type": "TSTypeReference", + "typeName": { + "loc": { + "end": { + "column": 37, + "line": 1 + }, + "start": { + "column": 30, + "line": 1 + } }, - "end": { - "line": 1, - "column": 37 - } - }, - "name": "Failure" + "name": "Failure", + "range": [ + 30, + 37 + ], + "type": "Identifier" + } } - } - ] + ] + } + } + ], + "range": [ + 0, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 37 } } }