docgen: add support for picking up the type from the declaration instead of initialization

This commit is contained in:
Patrik Oldsberg
2020-10-28 22:50:37 +01:00
parent cbab5bbf8a
commit a3ae6ddfdb
+12 -9
View File
@@ -76,13 +76,7 @@ export default class TypeLocator {
return;
}
docMap.get(decl.constructorType)!.push({
node,
name: decl.name,
source,
args: Array.from(decl.initializer.arguments || []),
typeArgs: Array.from(decl.initializer.typeArguments || []),
});
docMap.get(decl.constructorType)!.push({ node, source, ...decl });
});
});
@@ -103,7 +97,8 @@ export default class TypeLocator {
):
| {
constructorType: ts.Type;
initializer: ts.CallExpression | ts.NewExpression;
args: ts.Expression[];
typeArgs: ts.TypeNode[];
name: string;
}
| undefined {
@@ -137,6 +132,14 @@ export default class TypeLocator {
const constructorType = this.checker.getTypeAtLocation(
initializer.expression,
);
return { constructorType, initializer, name: name.text };
const args = Array.from(initializer.arguments ?? []);
const typeNode = declaration.type;
const typeArgs = Array.from(
(typeNode && ts.isTypeReferenceNode(typeNode)
? typeNode.typeArguments
: initializer.typeArguments) ?? [],
);
return { constructorType, args, typeArgs, name: name.text };
}
}