in src/typescript/ast-utils.ts [75:104]
export function nodeOfType<S extends keyof CapturableNodes, N extends string, A>(
syntaxKindOrCaptureName: ts.SyntaxKind | N,
nodeTypeOrChildren?: S | AstMatcher<A>,
children?: AstMatcher<A>,
): AstMatcher<A> | AstMatcher<A & { [key in N]: CapturableNodes[S] }> {
const capturing = typeof syntaxKindOrCaptureName === 'string'; // Determine which overload we're in (SyntaxKind is a number)
const realNext = (capturing ? children : (nodeTypeOrChildren as AstMatcher<A>)) ?? DONE;
const realCapture = capturing ? syntaxKindOrCaptureName : undefined;
const realSyntaxKind = capturing ? nodeTypeOrChildren : syntaxKindOrCaptureName;
return (nodes) => {
for (const node of nodes ?? []) {
if (node.kind === realSyntaxKind) {
const ret = realNext(nodeChildren(node));
if (!ret) {
continue;
}
if (realCapture) {
return Object.assign(ret, {
[realCapture]: node as CapturableNodes[S],
}) as any;
}
return ret;
}
}
return undefined;
};
}