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

New: Class implements generic syntax (fixes #44) #53

Merged
merged 1 commit into from
Aug 10, 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
42 changes: 41 additions & 1 deletion lib/ast-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,42 @@ module.exports = function(ast, extra) {
};
}

/**
* Converts a TSNode's typeArguments array to a flow-like typeParameters node
* @param {Array} typeArguments TSNode typeArguments
* @returns {TypeParameterInstantiation} TypeParameterInstantiation node
*/
function convertTypeArgumentsToTypeParameters(typeArguments) {
var firstTypeArgument = typeArguments[0];
var lastTypeArgument = typeArguments[typeArguments.length - 1];
return {
type: "TypeParameterInstantiation",
range: [
firstTypeArgument.pos - 1,
lastTypeArgument.end + 1
],
loc: getLocFor(firstTypeArgument.pos - 1, lastTypeArgument.end + 1, ast),
params: typeArguments.map(function(typeArgument) {
/**
* Have to manually calculate the start of the range,
* because TypeScript includes leading whitespace but Flow does not
*/
var typeArgumentStart = (typeArgument.typeName && typeArgument.typeName.text)
? typeArgument.end - typeArgument.typeName.text.length
: typeArgument.pos;
return {
type: "GenericTypeAnnotation",
range: [
typeArgumentStart,
typeArgument.end
],
loc: getLocFor(typeArgumentStart, typeArgument.end, ast),
id: convertChild(typeArgument.typeName)
};
})
};
}

/**
* Converts a child into a class implements node. This creates an intermediary
* ClassImplements node to match what Flow does.
Expand All @@ -460,12 +496,16 @@ module.exports = function(ast, extra) {
*/
function convertClassImplements(child) {
var id = convertChild(child.expression);
return {
var classImplementsNode = {
type: "ClassImplements",
loc: id.loc,
range: id.range,
id: id
};
if (child.typeArguments && child.typeArguments.length) {
classImplementsNode.typeParameters = convertTypeArgumentsToTypeParameters(child.typeArguments);
}
return classImplementsNode;
}

/**
Expand Down
Loading