diff --git a/lib/ast-converter.js b/lib/ast-converter.js index b63aa32..545aa80 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1778,6 +1778,19 @@ module.exports = function(ast, extra) { case SyntaxKind.ParenthesizedExpression: return convert(node.expression, parent); + /** + * Convert TypeAliasDeclaration node into VariableDeclaration + * to allow core rules such as "semi" to work automatically + */ + case SyntaxKind.TypeAliasDeclaration: + deeplyCopy(); + assign(result, { + type: "VariableDeclaration", + kind: "type", + declarations: [] + }); + break; + default: deeplyCopy(); } diff --git a/tests/fixtures/typescript/basics/type-alias-declaration.result.js b/tests/fixtures/typescript/basics/type-alias-declaration.result.js new file mode 100644 index 0000000..af8e9a7 --- /dev/null +++ b/tests/fixtures/typescript/basics/type-alias-declaration.result.js @@ -0,0 +1,461 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "body": [ + { + "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": [ + { + "type": "TSTypeParameter", + "range": [ + 12, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "flags": 0, + "name": { + "type": "Identifier", + "range": [ + 12, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "name": "T" + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "range": [ + 17, + 37 + ], + "typeAnnotation": { + "type": "TSUnionType", + "range": [ + 17, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "flags": 0, + "types": [ + { + "type": "TSTypeReference", + "range": [ + 17, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "flags": 0, + "typeName": { + "type": "Identifier", + "range": [ + 17, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "name": "Success" + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "range": [ + 25, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "flags": 0, + "typeName": { + "type": "Identifier", + "range": [ + 25, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "name": "T" + } + } + ] + }, + { + "type": "TSTypeReference", + "range": [ + 30, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "flags": 0, + "typeName": { + "type": "Identifier", + "range": [ + 30, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "name": "Failure" + } + } + ] + } + } + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Identifier", + "value": "type", + "range": [ + 0, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 4 + } + } + }, + { + "type": "Identifier", + "value": "Result", + "range": [ + 5, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 11, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Identifier", + "value": "T", + "range": [ + 12, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 13, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + } + } + }, + { + "type": "Identifier", + "value": "Success", + "range": [ + 17, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + } + }, + { + "type": "Identifier", + "value": "T", + "range": [ + 25, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 26, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + } + } + }, + { + "type": "Punctuator", + "value": "|", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 29 + } + } + }, + { + "type": "Identifier", + "value": "Failure", + "range": [ + 30, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 37 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/type-alias-declaration.src.ts b/tests/fixtures/typescript/basics/type-alias-declaration.src.ts new file mode 100644 index 0000000..c2067d8 --- /dev/null +++ b/tests/fixtures/typescript/basics/type-alias-declaration.src.ts @@ -0,0 +1 @@ +type Result = Success | Failure \ No newline at end of file