findDeclarationName
tcmts.cli.mirror.findDeclarationName · #5xo3jk6f · term · runs anywhere
import {
isClassDeclaration as ts_isClassDeclaration, isFunctionDeclaration as ts_isFunctionDeclaration,
isIdentifier as ts_isIdentifier, isInterfaceDeclaration as ts_isInterfaceDeclaration,
isTypeAliasDeclaration as ts_isTypeAliasDeclaration, isVariableStatement as ts_isVariableStatement,
} from "typescript/ast";
import type { Identifier as ts_Identifier, SourceFile as ts_SourceFile } from "typescript/ast";
export function findDeclarationName(sf: ts_SourceFile, name: string): ts_Identifier | undefined {
for (const st of sf.statements) {
if ((ts_isFunctionDeclaration(st) || ts_isClassDeclaration(st) || ts_isInterfaceDeclaration(st) || ts_isTypeAliasDeclaration(st)) && st.name?.text === name) return st.name;
if (ts_isVariableStatement(st)) for (const d of st.declarationList.declarations) if (ts_isIdentifier(d.name) && d.name.text === name) return d.name;
}
return undefined;
}
used by