@@ -563,6 +563,34 @@ module.exports = function(ast, extra) {
563
563
return null ;
564
564
}
565
565
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
+
566
594
/**
567
595
* Converts a TSNode's typeParameters array to a flow-like TypeParameterDeclaration node
568
596
* @param {TSNode[] } typeParameters TSNode typeParameters
@@ -896,19 +924,10 @@ module.exports = function(ast, extra) {
896
924
break ;
897
925
898
926
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
-
908
927
assign ( result , {
909
928
type : "VariableDeclaration" ,
910
929
declarations : node . declarationList . declarations . map ( convertChild ) ,
911
- kind : varStatementKind
930
+ kind : getDeclarationKind ( node . declarationList )
912
931
} ) ;
913
932
914
933
// check for exports
@@ -917,19 +936,10 @@ module.exports = function(ast, extra) {
917
936
918
937
// mostly for for-of, for-in
919
938
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
-
929
939
assign ( result , {
930
940
type : "VariableDeclaration" ,
931
941
declarations : node . declarations . map ( convertChild ) ,
932
- kind : varDeclarationListKind
942
+ kind : getDeclarationKind ( node )
933
943
} ) ;
934
944
break ;
935
945
@@ -1951,7 +1961,7 @@ module.exports = function(ast, extra) {
1951
1961
1952
1962
assign ( result , {
1953
1963
type : "VariableDeclaration" ,
1954
- kind : "type" ,
1964
+ kind : getDeclarationKind ( node ) ,
1955
1965
declarations : [ typeAliasDeclarator ]
1956
1966
} ) ;
1957
1967
0 commit comments