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

Commit e09ebb3

Browse files
JamesHenrynzakas
authored andcommitted
Fix: Add function type parameters (fixes #52) (#56)
1 parent a62ccab commit e09ebb3

5 files changed

+1098
-6
lines changed

lib/ast-converter.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ module.exports = function(ast, extra) {
454454

455455
/**
456456
* Converts a TSNode's typeArguments array to a flow-like typeParameters node
457-
* @param {Array} typeArguments TSNode typeArguments
457+
* @param {TSNode[]} typeArguments TSNode typeArguments
458458
* @returns {TypeParameterInstantiation} TypeParameterInstantiation node
459459
*/
460460
function convertTypeArgumentsToTypeParameters(typeArguments) {
@@ -488,6 +488,42 @@ module.exports = function(ast, extra) {
488488
};
489489
}
490490

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+
491527
/**
492528
* Converts a child into a class implements node. This creates an intermediary
493529
* ClassImplements node to match what Flow does.
@@ -737,11 +773,14 @@ module.exports = function(ast, extra) {
737773
params: node.parameters.map(convertChild),
738774
body: convertChild(node.body)
739775
});
740-
776+
// Process returnType
741777
if (node.type) {
742778
result.returnType = convertTypeAnnotation(node.type);
743779
}
744-
780+
// Process typeParameters
781+
if (node.typeParameters && node.typeParameters.length) {
782+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
783+
}
745784
// check for exports
746785
result = fixExports(node, result, ast);
747786

@@ -1050,10 +1089,14 @@ module.exports = function(ast, extra) {
10501089
body: convertChild(node.body),
10511090
expression: false
10521091
});
1053-
1092+
// Process returnType
10541093
if (node.type) {
10551094
result.returnType = convertTypeAnnotation(node.type);
10561095
}
1096+
// Process typeParameters
1097+
if (node.typeParameters && node.typeParameters.length) {
1098+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1099+
}
10571100
break;
10581101

10591102
case SyntaxKind.SuperKeyword:
@@ -1128,11 +1171,14 @@ module.exports = function(ast, extra) {
11281171
body: convertChild(node.body),
11291172
expression: node.body.kind !== SyntaxKind.Block
11301173
});
1131-
1174+
// Process returnType
11321175
if (node.type) {
11331176
result.returnType = convertTypeAnnotation(node.type);
11341177
}
1135-
1178+
// Process typeParameters
1179+
if (node.typeParameters && node.typeParameters.length) {
1180+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1181+
}
11361182
break;
11371183

11381184
case SyntaxKind.YieldExpression:

0 commit comments

Comments
 (0)