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

Commit 62d14b4

Browse files
JamesHenrynzakas
authored andcommitted
Fix: Class implements generic syntax (fixes #44) (#53)
1 parent c1e016d commit 62d14b4

5 files changed

+776
-1
lines changed

lib/ast-converter.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,42 @@ module.exports = function(ast, extra) {
452452
};
453453
}
454454

455+
/**
456+
* Converts a TSNode's typeArguments array to a flow-like typeParameters node
457+
* @param {Array} typeArguments TSNode typeArguments
458+
* @returns {TypeParameterInstantiation} TypeParameterInstantiation node
459+
*/
460+
function convertTypeArgumentsToTypeParameters(typeArguments) {
461+
var firstTypeArgument = typeArguments[0];
462+
var lastTypeArgument = typeArguments[typeArguments.length - 1];
463+
return {
464+
type: "TypeParameterInstantiation",
465+
range: [
466+
firstTypeArgument.pos - 1,
467+
lastTypeArgument.end + 1
468+
],
469+
loc: getLocFor(firstTypeArgument.pos - 1, lastTypeArgument.end + 1, ast),
470+
params: typeArguments.map(function(typeArgument) {
471+
/**
472+
* Have to manually calculate the start of the range,
473+
* because TypeScript includes leading whitespace but Flow does not
474+
*/
475+
var typeArgumentStart = (typeArgument.typeName && typeArgument.typeName.text)
476+
? typeArgument.end - typeArgument.typeName.text.length
477+
: typeArgument.pos;
478+
return {
479+
type: "GenericTypeAnnotation",
480+
range: [
481+
typeArgumentStart,
482+
typeArgument.end
483+
],
484+
loc: getLocFor(typeArgumentStart, typeArgument.end, ast),
485+
id: convertChild(typeArgument.typeName)
486+
};
487+
})
488+
};
489+
}
490+
455491
/**
456492
* Converts a child into a class implements node. This creates an intermediary
457493
* ClassImplements node to match what Flow does.
@@ -460,12 +496,16 @@ module.exports = function(ast, extra) {
460496
*/
461497
function convertClassImplements(child) {
462498
var id = convertChild(child.expression);
463-
return {
499+
var classImplementsNode = {
464500
type: "ClassImplements",
465501
loc: id.loc,
466502
range: id.range,
467503
id: id
468504
};
505+
if (child.typeArguments && child.typeArguments.length) {
506+
classImplementsNode.typeParameters = convertTypeArgumentsToTypeParameters(child.typeArguments);
507+
}
508+
return classImplementsNode;
469509
}
470510

471511
/**

0 commit comments

Comments
 (0)