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

Fix: Add function type parameters (fixes #52) #56

Merged
merged 1 commit into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions lib/ast-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ module.exports = function(ast, extra) {

/**
* Converts a TSNode's typeArguments array to a flow-like typeParameters node
* @param {Array} typeArguments TSNode typeArguments
* @param {TSNode[]} typeArguments TSNode typeArguments
* @returns {TypeParameterInstantiation} TypeParameterInstantiation node
*/
function convertTypeArgumentsToTypeParameters(typeArguments) {
Expand Down Expand Up @@ -488,6 +488,42 @@ module.exports = function(ast, extra) {
};
}

/**
* Converts a TSNode's typeParameters array to a flow-like TypeParameterDeclaration node
* @param {TSNode[]} typeParameters TSNode typeParameters
* @returns {TypeParameterDeclaration} TypeParameterDeclaration node
*/
function convertTSTypeParametersToTypeParametersDeclaration(typeParameters) {
var firstTypeParameter = typeParameters[0];
var lastTypeParameter = typeParameters[typeParameters.length - 1];
return {
type: "TypeParameterDeclaration",
range: [
firstTypeParameter.pos - 1,
lastTypeParameter.end + 1
],
loc: getLocFor(firstTypeParameter.pos - 1, lastTypeParameter.end + 1, ast),
params: typeParameters.map(function(typeParameter) {
/**
* Have to manually calculate the start of the range,
* because TypeScript includes leading whitespace but Flow does not
*/
var typeParameterStart = (typeParameter.typeName && typeParameter.typeName.text)
? typeParameter.end - typeParameter.typeName.text.length
: typeParameter.pos;
return {
type: "TypeParameter",
range: [
typeParameterStart,
typeParameter.end
],
loc: getLocFor(typeParameterStart, typeParameter.end, ast),
name: typeParameter.name.text
};
})
};
}

/**
* Converts a child into a class implements node. This creates an intermediary
* ClassImplements node to match what Flow does.
Expand Down Expand Up @@ -737,11 +773,14 @@ module.exports = function(ast, extra) {
params: node.parameters.map(convertChild),
body: convertChild(node.body)
});

// Process returnType
if (node.type) {
result.returnType = convertTypeAnnotation(node.type);
}

// Process typeParameters
if (node.typeParameters && node.typeParameters.length) {
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
}
// check for exports
result = fixExports(node, result, ast);

Expand Down Expand Up @@ -1050,10 +1089,14 @@ module.exports = function(ast, extra) {
body: convertChild(node.body),
expression: false
});

// Process returnType
if (node.type) {
result.returnType = convertTypeAnnotation(node.type);
}
// Process typeParameters
if (node.typeParameters && node.typeParameters.length) {
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
}
break;

case SyntaxKind.SuperKeyword:
Expand Down Expand Up @@ -1128,11 +1171,14 @@ module.exports = function(ast, extra) {
body: convertChild(node.body),
expression: node.body.kind !== SyntaxKind.Block
});

// Process returnType
if (node.type) {
result.returnType = convertTypeAnnotation(node.type);
}

// Process typeParameters
if (node.typeParameters && node.typeParameters.length) {
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
}
break;

case SyntaxKind.YieldExpression:
Expand Down
Loading