in build-scripts/doc-gen/parser.ts [364:450]
export function serializeMethodOrFunction(
checker: ts.TypeChecker, node: ts.MethodDeclaration|ts.FunctionDeclaration,
docInfo: util.DocInfo, sourceFile: ts.SourceFile, repoPath: string,
srcRoot: string, githubRoot: string): DocFunction {
const stripSymbolUnderscoreSuffix = ts.isFunctionDeclaration(node);
if (!sourceFile.fileName.startsWith(repoPath)) {
throw new Error(
`Error: source file ${sourceFile.fileName} ` +
`does not start with srcPath provided ${repoPath}.`);
}
const symbol = checker.getSymbolAtLocation(node.name);
const type =
checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration!);
const signature = type.getCallSignatures()[0];
let symbolName = symbol.name;
if (stripSymbolUnderscoreSuffix) {
if (symbolName.endsWith('_')) {
symbolName = symbolName.substring(0, symbolName.length - 1);
}
}
const identifierGenericMap = util.getIdentifierGenericMap(node);
const isConfigParam = false;
const parameters = signature.parameters.map(
(param: ts.Symbol) => serializeParameter(
checker, param, identifierGenericMap, isConfigParam));
const paramStr = '(' +
parameters.map(param => param.name + (param.optional ? '?' : ''))
.join(', ') +
')';
const {displayFilename, githubUrl} =
util.getFileInfo(node, sourceFile, repoPath, srcRoot, githubRoot);
// Find a type node in the method signature. This is a return type. If
// it cannot be found (no return type), fall back to the standard way of
// getting the type. We do this because getting the full text of the
// type node is better than using the signature return type.
let returnType;
node.forEachChild(child => {
if (ts.isTypeNode(child)) {
returnType = child.getText();
}
});
if (returnType == null) {
// Fall back the the standard way of getting the type, which sometimes
// gives up and returns 'any' or '{}' for complex types.
returnType = checker.typeToString(signature.getReturnType());
}
returnType = util.sanitizeTypeString(returnType, identifierGenericMap);
let documentation =
ts.displayPartsToString(signature.getDocumentationComment(checker));
documentation = util.removeStarsFromCommentString(documentation);
const method: DocFunction = {
docInfo: docInfo,
symbolName,
namespace: docInfo.namespace,
paramStr,
parameters,
returnType,
documentation,
fileName: displayFilename,
githubUrl,
isFunction: true
};
// Unpack return types.
const docUnpackReturnTypes =
util.getDocDecoratorOrAnnotation(
checker, node, DOCUMENTATION_UNPACK_RETURN) as {} as string[];
if (docUnpackReturnTypes != null) {
method.unpackedReturnTypes = docUnpackReturnTypes.map(t => {
return {
description: '`' + t + '`',
symbol: t,
};
})
}
return method;
}