diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 268ada7..7064f26 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -563,6 +563,34 @@ module.exports = function(ast, extra) { return null; } + /** + * Returns the declaration kind of the given TSNode + * @param {TSNode} tsNode TypeScript AST node + * @returns {string} declaration kind + */ + function getDeclarationKind(tsNode) { + var varDeclarationKind; + + switch (tsNode.kind) { + case SyntaxKind.TypeAliasDeclaration: + varDeclarationKind = "type"; + break; + case SyntaxKind.VariableDeclarationList: + if (ts.isLet(tsNode)) { + varDeclarationKind = "let"; + } else if (ts.isConst(tsNode)) { + varDeclarationKind = "const"; + } else { + varDeclarationKind = "var"; + } + break; + default: + throw "Unable to determine declaration kind."; + } + + return varDeclarationKind; + } + /** * Converts a TSNode's typeParameters array to a flow-like TypeParameterDeclaration node * @param {TSNode[]} typeParameters TSNode typeParameters @@ -896,19 +924,10 @@ module.exports = function(ast, extra) { break; case SyntaxKind.VariableStatement: - - var varStatementKind; - - if (node.declarationList.flags) { - varStatementKind = (node.declarationList.flags === ts.NodeFlags.Let) ? "let" : "const"; - } else { - varStatementKind = "var"; - } - assign(result, { type: "VariableDeclaration", declarations: node.declarationList.declarations.map(convertChild), - kind: varStatementKind + kind: getDeclarationKind(node.declarationList) }); // check for exports @@ -917,19 +936,10 @@ module.exports = function(ast, extra) { // mostly for for-of, for-in case SyntaxKind.VariableDeclarationList: - - var varDeclarationListKind; - - if (node.flags) { - varDeclarationListKind = (node.flags === ts.NodeFlags.Let) ? "let" : "const"; - } else { - varDeclarationListKind = "var"; - } - assign(result, { type: "VariableDeclaration", declarations: node.declarations.map(convertChild), - kind: varDeclarationListKind + kind: getDeclarationKind(node) }); break; @@ -1951,7 +1961,7 @@ module.exports = function(ast, extra) { assign(result, { type: "VariableDeclaration", - kind: "type", + kind: getDeclarationKind(node), declarations: [typeAliasDeclarator] }); diff --git a/tests/fixtures/typescript/basics/async-function-with-var-declaration.result.js b/tests/fixtures/typescript/basics/async-function-with-var-declaration.result.js new file mode 100644 index 0000000..65bee37 --- /dev/null +++ b/tests/fixtures/typescript/basics/async-function-with-var-declaration.result.js @@ -0,0 +1,698 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 96 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "FunctionDeclaration", + "range": [ + 0, + 96 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 15, + 19 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "name": "test" + }, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "range": [ + 22, + 96 + ], + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "range": [ + 28, + 44 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 32, + 43 + ], + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "range": [ + 32, + 35 + ], + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "name": "foo" + }, + "init": { + "type": "Literal", + "range": [ + 38, + 43 + ], + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "value": "foo", + "raw": "'foo'" + } + } + ], + "kind": "var" + }, + { + "type": "VariableDeclaration", + "range": [ + 49, + 65 + ], + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 53, + 64 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "range": [ + 53, + 56 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 11 + } + }, + "name": "bar" + }, + "init": { + "type": "Literal", + "range": [ + 59, + 64 + ], + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 19 + } + }, + "value": "bar", + "raw": "'bar'" + } + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "range": [ + 70, + 94 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 28 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 76, + 93 + ], + "loc": { + "start": { + "line": 4, + "column": 10 + }, + "end": { + "line": 4, + "column": 27 + } + }, + "id": { + "type": "Identifier", + "range": [ + 76, + 82 + ], + "loc": { + "start": { + "line": 4, + "column": 10 + }, + "end": { + "line": 4, + "column": 16 + } + }, + "name": "fooBar" + }, + "init": { + "type": "Literal", + "range": [ + 85, + 93 + ], + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 27 + } + }, + "value": "fooBar", + "raw": "'fooBar'" + } + } + ], + "kind": "const" + } + ] + } + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Identifier", + "value": "async", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Keyword", + "value": "function", + "range": [ + 6, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + { + "type": "Identifier", + "value": "test", + "range": [ + 15, + 19 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 19 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 19, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + { + "type": "Keyword", + "value": "var", + "range": [ + 28, + 31 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + { + "type": "Identifier", + "value": "foo", + "range": [ + 32, + 35 + ], + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 36, + 37 + ], + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 13 + } + } + }, + { + "type": "String", + "value": "'foo'", + "range": [ + 38, + 43 + ], + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 19 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 43, + 44 + ], + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + } + } + }, + { + "type": "Keyword", + "value": "let", + "range": [ + 49, + 52 + ], + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + { + "type": "Identifier", + "value": "bar", + "range": [ + 53, + 56 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 57, + 58 + ], + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + { + "type": "String", + "value": "'bar'", + "range": [ + 59, + 64 + ], + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 19 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 64, + 65 + ], + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 20 + } + } + }, + { + "type": "Keyword", + "value": "const", + "range": [ + 70, + 75 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 9 + } + } + }, + { + "type": "Identifier", + "value": "fooBar", + "range": [ + 76, + 82 + ], + "loc": { + "start": { + "line": 4, + "column": 10 + }, + "end": { + "line": 4, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 83, + 84 + ], + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 18 + } + } + }, + { + "type": "String", + "value": "'fooBar'", + "range": [ + 85, + 93 + ], + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 27 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 93, + 94 + ], + "loc": { + "start": { + "line": 4, + "column": 27 + }, + "end": { + "line": 4, + "column": 28 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 95, + 96 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/async-function-with-var-declaration.src.ts b/tests/fixtures/typescript/basics/async-function-with-var-declaration.src.ts new file mode 100644 index 0000000..e691825 --- /dev/null +++ b/tests/fixtures/typescript/basics/async-function-with-var-declaration.src.ts @@ -0,0 +1,5 @@ +async function test() { + var foo = 'foo'; + let bar = 'bar'; + const fooBar = 'fooBar'; +}