@@ -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