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

Commit d53f1f8

Browse files
soda0289JamesHenry
authored andcommitted
Fix: Use ts utilities determine variable declaration type (fixes #136) (#138)
1 parent 918190d commit d53f1f8

File tree

3 files changed

+734
-21
lines changed

3 files changed

+734
-21
lines changed

lib/ast-converter.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,34 @@ module.exports = function(ast, extra) {
563563
return null;
564564
}
565565

566+
/**
567+
* Returns the declaration kind of the given TSNode
568+
* @param {TSNode} tsNode TypeScript AST node
569+
* @returns {string} declaration kind
570+
*/
571+
function getDeclarationKind(tsNode) {
572+
var varDeclarationKind;
573+
574+
switch (tsNode.kind) {
575+
case SyntaxKind.TypeAliasDeclaration:
576+
varDeclarationKind = "type";
577+
break;
578+
case SyntaxKind.VariableDeclarationList:
579+
if (ts.isLet(tsNode)) {
580+
varDeclarationKind = "let";
581+
} else if (ts.isConst(tsNode)) {
582+
varDeclarationKind = "const";
583+
} else {
584+
varDeclarationKind = "var";
585+
}
586+
break;
587+
default:
588+
throw "Unable to determine declaration kind.";
589+
}
590+
591+
return varDeclarationKind;
592+
}
593+
566594
/**
567595
* Converts a TSNode's typeParameters array to a flow-like TypeParameterDeclaration node
568596
* @param {TSNode[]} typeParameters TSNode typeParameters
@@ -896,19 +924,10 @@ module.exports = function(ast, extra) {
896924
break;
897925

898926
case SyntaxKind.VariableStatement:
899-
900-
var varStatementKind;
901-
902-
if (node.declarationList.flags) {
903-
varStatementKind = (node.declarationList.flags === ts.NodeFlags.Let) ? "let" : "const";
904-
} else {
905-
varStatementKind = "var";
906-
}
907-
908927
assign(result, {
909928
type: "VariableDeclaration",
910929
declarations: node.declarationList.declarations.map(convertChild),
911-
kind: varStatementKind
930+
kind: getDeclarationKind(node.declarationList)
912931
});
913932

914933
// check for exports
@@ -917,19 +936,10 @@ module.exports = function(ast, extra) {
917936

918937
// mostly for for-of, for-in
919938
case SyntaxKind.VariableDeclarationList:
920-
921-
var varDeclarationListKind;
922-
923-
if (node.flags) {
924-
varDeclarationListKind = (node.flags === ts.NodeFlags.Let) ? "let" : "const";
925-
} else {
926-
varDeclarationListKind = "var";
927-
}
928-
929939
assign(result, {
930940
type: "VariableDeclaration",
931941
declarations: node.declarations.map(convertChild),
932-
kind: varDeclarationListKind
942+
kind: getDeclarationKind(node)
933943
});
934944
break;
935945

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

19521962
assign(result, {
19531963
type: "VariableDeclaration",
1954-
kind: "type",
1964+
kind: getDeclarationKind(node),
19551965
declarations: [typeAliasDeclarator]
19561966
});
19571967

0 commit comments

Comments
 (0)