Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Fix: Use ts utilities function to determine variable type (#136) #138

Merged
merged 1 commit into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions lib/ast-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -1951,7 +1961,7 @@ module.exports = function(ast, extra) {

assign(result, {
type: "VariableDeclaration",
kind: "type",
kind: getDeclarationKind(node),
declarations: [typeAliasDeclarator]
});

Expand Down
Loading