diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 7064f26..a2928e2 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -659,7 +659,7 @@ module.exports = function(ast, extra) { return !(/^(?:kind|parent|pos|end|flags)$/.test(key)); }).forEach(function(key) { if (key === "type") { - result.typeAnnotation = convertTypeAnnotation(node.type); + result.typeAnnotation = (node.type) ? convertTypeAnnotation(node.type) : null; } else { if (Array.isArray(node[key])) { result[key] = node[key].map(convertChild); diff --git a/tests/fixtures/typescript/basics/function-with-object-type-without-annotation.result.js b/tests/fixtures/typescript/basics/function-with-object-type-without-annotation.result.js new file mode 100644 index 0000000..1cd0e17 --- /dev/null +++ b/tests/fixtures/typescript/basics/function-with-object-type-without-annotation.result.js @@ -0,0 +1,696 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 49 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "FunctionDeclaration", + "range": [ + 0, + 49 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 9, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "name": "foo" + }, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "ObjectPattern", + "range": [ + 13, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "properties": [ + { + "type": "Property", + "range": [ + 14, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "key": { + "type": "Identifier", + "range": [ + 14, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "name": "bar" + }, + "value": { + "type": "Identifier", + "range": [ + 14, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "name": "bar" + }, + "computed": false, + "method": false, + "shorthand": true, + "kind": "init" + }, + { + "type": "Property", + "range": [ + 19, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "key": { + "type": "Identifier", + "range": [ + 19, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "name": "baz" + }, + "value": { + "type": "Identifier", + "range": [ + 19, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "name": "baz" + }, + "computed": false, + "method": false, + "shorthand": true, + "kind": "init" + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "range": [ + 25, + 43 + ], + "typeAnnotation": { + "type": "TSTypeLiteral", + "range": [ + 25, + 43 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "range": [ + 26, + 38 + ], + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "name": { + "type": "Identifier", + "range": [ + 26, + 29 + ], + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "name": "bar" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "range": [ + 31, + 37 + ], + "typeAnnotation": { + "type": "TSStringKeyword", + "range": [ + 31, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 37 + } + } + } + } + }, + { + "type": "TSPropertySignature", + "range": [ + 39, + 42 + ], + "loc": { + "start": { + "line": 1, + "column": 39 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "name": { + "type": "Identifier", + "range": [ + 39, + 42 + ], + "loc": { + "start": { + "line": 1, + "column": 39 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "name": "baz" + }, + "typeAnnotation": null + } + ] + } + } + } + ], + "body": { + "type": "BlockStatement", + "range": [ + 45, + 49 + ], + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [] + } + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "function", + "range": [ + 0, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + { + "type": "Identifier", + "value": "foo", + "range": [ + 9, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "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": "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": "Identifier", + "value": "baz", + "range": [ + 19, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 25, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + { + "type": "Identifier", + "value": "bar", + "range": [ + 26, + 29 + ], + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 29 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 29, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 30 + } + } + }, + { + "type": "Identifier", + "value": "string", + "range": [ + 31, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 37 + } + } + }, + { + "type": "Punctuator", + "value": ",", + "range": [ + 37, + 38 + ], + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 38 + } + } + }, + { + "type": "Identifier", + "value": "baz", + "range": [ + 39, + 42 + ], + "loc": { + "start": { + "line": 1, + "column": 39 + }, + "end": { + "line": 1, + "column": 42 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 42, + 43 + ], + "loc": { + "start": { + "line": 1, + "column": 42 + }, + "end": { + "line": 1, + "column": 43 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 43, + 44 + ], + "loc": { + "start": { + "line": 1, + "column": 43 + }, + "end": { + "line": 1, + "column": 44 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 45, + 46 + ], + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 1, + "column": 46 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 48, + 49 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/function-with-object-type-without-annotation.src.ts b/tests/fixtures/typescript/basics/function-with-object-type-without-annotation.src.ts new file mode 100644 index 0000000..bd3604d --- /dev/null +++ b/tests/fixtures/typescript/basics/function-with-object-type-without-annotation.src.ts @@ -0,0 +1,3 @@ +function foo({bar, baz}: {bar: string, baz}) { + +} diff --git a/tests/fixtures/typescript/basics/interface-without-type-annotation.result.js b/tests/fixtures/typescript/basics/interface-without-type-annotation.result.js new file mode 100644 index 0000000..40c8fbc --- /dev/null +++ b/tests/fixtures/typescript/basics/interface-without-type-annotation.result.js @@ -0,0 +1,203 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSInterfaceDeclaration", + "range": [ + 0, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "range": [ + 10, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "name": "test" + }, + "members": [ + { + "type": "TSPropertySignature", + "range": [ + 21, + 25 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "name": { + "type": "Identifier", + "range": [ + 21, + 24 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "name": "foo" + }, + "typeAnnotation": null + } + ] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "interface", + "range": [ + 0, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Identifier", + "value": "test", + "range": [ + 10, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "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": "foo", + "range": [ + 21, + 24 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 26, + 27 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/interface-without-type-annotation.src.ts b/tests/fixtures/typescript/basics/interface-without-type-annotation.src.ts new file mode 100644 index 0000000..31f184e --- /dev/null +++ b/tests/fixtures/typescript/basics/interface-without-type-annotation.src.ts @@ -0,0 +1,3 @@ +interface test { + foo; +} diff --git a/tests/fixtures/typescript/basics/type-alias-object-without-annotation.result.js b/tests/fixtures/typescript/basics/type-alias-object-without-annotation.result.js new file mode 100644 index 0000000..d3ab285 --- /dev/null +++ b/tests/fixtures/typescript/basics/type-alias-object-without-annotation.result.js @@ -0,0 +1,399 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "range": [ + 0, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "kind": "type", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "range": [ + 5, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "name": "foo" + }, + "init": { + "type": "TSTypeLiteral", + "range": [ + 11, + 29 + ], + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "range": [ + 12, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "name": { + "type": "Identifier", + "range": [ + 12, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "name": "bar" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "range": [ + 17, + 23 + ], + "typeAnnotation": { + "type": "TSStringKeyword", + "range": [ + 17, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + } + } + }, + { + "type": "TSPropertySignature", + "range": [ + 25, + 28 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "name": { + "type": "Identifier", + "range": [ + 25, + 28 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "name": "baz" + }, + "typeAnnotation": null + } + ] + }, + "range": [ + 5, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 30 + } + } + } + ] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Identifier", + "value": "type", + "range": [ + 0, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 4 + } + } + }, + { + "type": "Identifier", + "value": "foo", + "range": [ + 5, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 9, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 11, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Identifier", + "value": "bar", + "range": [ + 12, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + } + } + }, + { + "type": "Identifier", + "value": "string", + "range": [ + 17, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + { + "type": "Punctuator", + "value": ",", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "Identifier", + "value": "baz", + "range": [ + 25, + 28 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 28 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 29 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 29, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 30 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/type-alias-object-without-annotation.src.ts b/tests/fixtures/typescript/basics/type-alias-object-without-annotation.src.ts new file mode 100644 index 0000000..5c542c9 --- /dev/null +++ b/tests/fixtures/typescript/basics/type-alias-object-without-annotation.src.ts @@ -0,0 +1 @@ +type foo = {bar: string, baz};