Something went wrong. Try again.
tsref is a Bun CLI for finding references to exported TypeScript APIs.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123import ts from "typescript";import type { CollectedExport } from "./exportCollector";import { normalizePath } from "./project";
export function collectImportReferences( program: ts.Program, checker: ts.TypeChecker, targetFilePath: string, exports: CollectedExport[],): void { const exportByName = new Map(exports.map((item) => [item.summary.exportName, item])); const defaultExport = exportByName.get("default");
for (const sourceFile of program.getSourceFiles()) { if (sourceFile.isDeclarationFile || normalizePath(sourceFile.fileName) === targetFilePath) { continue; }
for (const statement of sourceFile.statements) { if (ts.isImportDeclaration(statement)) { collectImportDeclaration(statement, checker, targetFilePath, exportByName, defaultExport); } else if (ts.isExportDeclaration(statement)) { collectExportDeclaration(statement, checker, targetFilePath, exports, exportByName); } } }}
function collectImportDeclaration( statement: ts.ImportDeclaration, checker: ts.TypeChecker, targetFilePath: string, exportByName: Map<string, CollectedExport>, defaultExport: CollectedExport | undefined,): void { if (!moduleSpecifierTargetsFile(statement.moduleSpecifier, checker, targetFilePath)) { return; }
if (statement.importClause?.name && defaultExport) { incrementExternal(defaultExport); }
const namedBindings = statement.importClause?.namedBindings;
if (!namedBindings) { return; }
if (ts.isNamespaceImport(namedBindings)) { for (const item of exportByName.values()) { incrementExternal(item); }
return; }
for (const specifier of namedBindings.elements) { const exportName = (specifier.propertyName ?? specifier.name).text; const item = exportByName.get(exportName);
if (item) { incrementExternal(item); } }}
function collectExportDeclaration( statement: ts.ExportDeclaration, checker: ts.TypeChecker, targetFilePath: string, exports: CollectedExport[], exportByName: Map<string, CollectedExport>,): void { if ( !statement.moduleSpecifier || !moduleSpecifierTargetsFile(statement.moduleSpecifier, checker, targetFilePath) ) { return; }
if (!statement.exportClause) { for (const item of exports) { incrementExternal(item); }
return; }
if (!ts.isNamedExports(statement.exportClause)) { return; }
for (const specifier of statement.exportClause.elements) { const exportName = (specifier.propertyName ?? specifier.name).text; const item = exportByName.get(exportName);
if (item) { incrementExternal(item); } }}
function moduleSpecifierTargetsFile( moduleSpecifier: ts.Expression, checker: ts.TypeChecker, targetFilePath: string,): boolean { if (!ts.isStringLiteralLike(moduleSpecifier)) { return false; }
const symbol = checker.getSymbolAtLocation(moduleSpecifier);
return (symbol?.declarations ?? []).some( (declaration) => normalizePath(declaration.getSourceFile().fileName) === targetFilePath, );}
function incrementExternal(item: CollectedExport): void { item.summary.externalReferenceCount = (item.summary.externalReferenceCount ?? 0) + 1;}