@@ -454,7 +454,7 @@ module.exports = function(ast, extra) {
454
454
455
455
/**
456
456
* Converts a TSNode's typeArguments array to a flow-like typeParameters node
457
- * @param {Array } typeArguments TSNode typeArguments
457
+ * @param {TSNode[] } typeArguments TSNode typeArguments
458
458
* @returns {TypeParameterInstantiation } TypeParameterInstantiation node
459
459
*/
460
460
function convertTypeArgumentsToTypeParameters ( typeArguments ) {
@@ -488,6 +488,42 @@ module.exports = function(ast, extra) {
488
488
} ;
489
489
}
490
490
491
+ /**
492
+ * Converts a TSNode's typeParameters array to a flow-like TypeParameterDeclaration node
493
+ * @param {TSNode[] } typeParameters TSNode typeParameters
494
+ * @returns {TypeParameterDeclaration } TypeParameterDeclaration node
495
+ */
496
+ function convertTSTypeParametersToTypeParametersDeclaration ( typeParameters ) {
497
+ var firstTypeParameter = typeParameters [ 0 ] ;
498
+ var lastTypeParameter = typeParameters [ typeParameters . length - 1 ] ;
499
+ return {
500
+ type : "TypeParameterDeclaration" ,
501
+ range : [
502
+ firstTypeParameter . pos - 1 ,
503
+ lastTypeParameter . end + 1
504
+ ] ,
505
+ loc : getLocFor ( firstTypeParameter . pos - 1 , lastTypeParameter . end + 1 , ast ) ,
506
+ params : typeParameters . map ( function ( typeParameter ) {
507
+ /**
508
+ * Have to manually calculate the start of the range,
509
+ * because TypeScript includes leading whitespace but Flow does not
510
+ */
511
+ var typeParameterStart = ( typeParameter . typeName && typeParameter . typeName . text )
512
+ ? typeParameter . end - typeParameter . typeName . text . length
513
+ : typeParameter . pos ;
514
+ return {
515
+ type : "TypeParameter" ,
516
+ range : [
517
+ typeParameterStart ,
518
+ typeParameter . end
519
+ ] ,
520
+ loc : getLocFor ( typeParameterStart , typeParameter . end , ast ) ,
521
+ name : typeParameter . name . text
522
+ } ;
523
+ } )
524
+ } ;
525
+ }
526
+
491
527
/**
492
528
* Converts a child into a class implements node. This creates an intermediary
493
529
* ClassImplements node to match what Flow does.
@@ -737,11 +773,14 @@ module.exports = function(ast, extra) {
737
773
params : node . parameters . map ( convertChild ) ,
738
774
body : convertChild ( node . body )
739
775
} ) ;
740
-
776
+ // Process returnType
741
777
if ( node . type ) {
742
778
result . returnType = convertTypeAnnotation ( node . type ) ;
743
779
}
744
-
780
+ // Process typeParameters
781
+ if ( node . typeParameters && node . typeParameters . length ) {
782
+ result . typeParameters = convertTSTypeParametersToTypeParametersDeclaration ( node . typeParameters ) ;
783
+ }
745
784
// check for exports
746
785
result = fixExports ( node , result , ast ) ;
747
786
@@ -1050,10 +1089,14 @@ module.exports = function(ast, extra) {
1050
1089
body : convertChild ( node . body ) ,
1051
1090
expression : false
1052
1091
} ) ;
1053
-
1092
+ // Process returnType
1054
1093
if ( node . type ) {
1055
1094
result . returnType = convertTypeAnnotation ( node . type ) ;
1056
1095
}
1096
+ // Process typeParameters
1097
+ if ( node . typeParameters && node . typeParameters . length ) {
1098
+ result . typeParameters = convertTSTypeParametersToTypeParametersDeclaration ( node . typeParameters ) ;
1099
+ }
1057
1100
break ;
1058
1101
1059
1102
case SyntaxKind . SuperKeyword :
@@ -1128,11 +1171,14 @@ module.exports = function(ast, extra) {
1128
1171
body : convertChild ( node . body ) ,
1129
1172
expression : node . body . kind !== SyntaxKind . Block
1130
1173
} ) ;
1131
-
1174
+ // Process returnType
1132
1175
if ( node . type ) {
1133
1176
result . returnType = convertTypeAnnotation ( node . type ) ;
1134
1177
}
1135
-
1178
+ // Process typeParameters
1179
+ if ( node . typeParameters && node . typeParameters . length ) {
1180
+ result . typeParameters = convertTSTypeParametersToTypeParametersDeclaration ( node . typeParameters ) ;
1181
+ }
1136
1182
break ;
1137
1183
1138
1184
case SyntaxKind . YieldExpression :
0 commit comments