import ts from "typescript"; import { normalizePath } from "./project"; import type { ExportedMemberSummary, ExportedSymbolSummary, SourceLocation } from "./types"; export interface CollectedExport { summary: ExportedSymbolSummary; symbols: Set; members: CollectedMember[]; } export interface CollectedMember { summary: ExportedMemberSummary; symbols: Set; } export function collectExports( checker: ts.TypeChecker, sourceFile: ts.SourceFile, ): CollectedExport[] { const moduleSymbol = checker.getSymbolAtLocation(sourceFile); if (!moduleSymbol) { return []; } return checker .getExportsOfModule(moduleSymbol) .map((exportSymbol) => collectExport(checker, sourceFile, exportSymbol)) .sort((left, right) => compareExports(left.summary, right.summary)); } function collectExport( checker: ts.TypeChecker, sourceFile: ts.SourceFile, exportSymbol: ts.Symbol, ): CollectedExport { const aliasedSymbol = isAliasSymbol(exportSymbol) ? checker.getAliasedSymbol(exportSymbol) : exportSymbol; const symbols = new Set([exportSymbol, aliasedSymbol]); const declarations = aliasedSymbol.declarations ?? exportSymbol.declarations ?? []; const exportDeclarations = exportSymbol.declarations ?? []; const declaration = declarations[0] ?? exportDeclarations[0]; const exportName = exportSymbol.getName() === "default" ? "default" : exportSymbol.getName(); const localName = aliasedSymbol.getName() !== exportName ? aliasedSymbol.getName() : undefined; const isReExport = declarations.some( (item) => normalizePath(item.getSourceFile().fileName) !== normalizePath(sourceFile.fileName), ); const kind = isReExport ? "re-export" : getExportKind(aliasedSymbol, declaration); const summary: ExportedSymbolSummary = { exportName, localName, kind, isDefault: exportName === "default", isTypeOnly: isTypeOnlyExport(exportSymbol, aliasedSymbol, exportDeclarations), declarationLocation: declaration ? getLocation(declaration) : undefined, references: [], members: [], }; const members = collectMembers(checker, aliasedSymbol); summary.members = members.map((member) => member.summary); return { summary, symbols, members }; } function collectMembers(checker: ts.TypeChecker, symbol: ts.Symbol): CollectedMember[] { const declarations = symbol.declarations ?? []; const declaration = declarations[0]; if (!declaration) { return []; } const type = getMemberContainerType(checker, declaration, symbol); if (!type) { return []; } return checker .getPropertiesOfType(type) .filter((memberSymbol) => isPublicMember(memberSymbol)) .map((memberSymbol) => { const summary: ExportedMemberSummary = { name: memberSymbol.getName(), kind: getMemberKind(memberSymbol), references: [], }; return { summary, symbols: new Set([memberSymbol]), }; }) .sort((left, right) => left.summary.name.localeCompare(right.summary.name)); } function getMemberContainerType( checker: ts.TypeChecker, declaration: ts.Declaration, symbol: ts.Symbol, ): ts.Type | undefined { if (ts.isClassDeclaration(declaration) || ts.isInterfaceDeclaration(declaration)) { return checker.getDeclaredTypeOfSymbol(symbol); } if ( ts.isVariableDeclaration(declaration) && declaration.initializer && ts.isObjectLiteralExpression(declaration.initializer) ) { return checker.getTypeAtLocation(declaration.initializer); } if (ts.isTypeAliasDeclaration(declaration) && ts.isTypeLiteralNode(declaration.type)) { return checker.getTypeOfSymbolAtLocation(symbol, declaration); } return undefined; } function isPublicMember(symbol: ts.Symbol): boolean { return (symbol.declarations ?? []).every((declaration) => { const modifiers = ts.canHaveModifiers(declaration) ? ts.getModifiers(declaration) : undefined; if (!modifiers) { return true; } return !modifiers.some( (modifier) => modifier.kind === ts.SyntaxKind.PrivateKeyword || modifier.kind === ts.SyntaxKind.ProtectedKeyword, ); }); } function getExportKind( symbol: ts.Symbol, declaration: ts.Declaration | undefined, ): ExportedSymbolSummary["kind"] { if (!declaration) { return "unknown"; } if (ts.isFunctionDeclaration(declaration)) return "function"; if (ts.isClassDeclaration(declaration)) return "class"; if (ts.isInterfaceDeclaration(declaration)) return "interface"; if (ts.isTypeAliasDeclaration(declaration)) return "type"; if (ts.isEnumDeclaration(declaration)) return "enum"; if (ts.isModuleDeclaration(declaration)) return "namespace"; if (ts.isMethodDeclaration(declaration) || ts.isMethodSignature(declaration)) return "method"; if (ts.isPropertyDeclaration(declaration) || ts.isPropertySignature(declaration)) return "property"; if (ts.isVariableDeclaration(declaration)) { const declarationList = declaration.parent; const flags = ts.getCombinedNodeFlags(declarationList); if ((flags & ts.NodeFlags.Const) !== 0) return "const"; if ((flags & ts.NodeFlags.Let) !== 0) return "let"; return "var"; } if ((symbol.flags & ts.SymbolFlags.Class) !== 0) return "class"; if ((symbol.flags & ts.SymbolFlags.Function) !== 0) return "function"; if ((symbol.flags & ts.SymbolFlags.Interface) !== 0) return "interface"; if ((symbol.flags & ts.SymbolFlags.TypeAlias) !== 0) return "type"; if ((symbol.flags & ts.SymbolFlags.Enum) !== 0) return "enum"; if ((symbol.flags & ts.SymbolFlags.Value) !== 0) return "const"; return "unknown"; } function getMemberKind(symbol: ts.Symbol): ExportedMemberSummary["kind"] { const declaration = symbol.declarations?.[0]; if (!declaration) return "unknown"; if (ts.isMethodDeclaration(declaration) || ts.isMethodSignature(declaration)) return "method"; if (ts.isPropertyDeclaration(declaration)) return "property"; if (ts.isGetAccessorDeclaration(declaration) || ts.isSetAccessorDeclaration(declaration)) return "accessor"; if (ts.isPropertySignature(declaration)) return "type-member"; return "unknown"; } function isAliasSymbol(symbol: ts.Symbol): boolean { return (symbol.flags & ts.SymbolFlags.Alias) !== 0; } function isTypeOnlyExport( exportSymbol: ts.Symbol, aliasedSymbol: ts.Symbol, declarations: readonly ts.Declaration[], ): boolean { if ( declarations.some( (declaration) => ts.isExportSpecifier(declaration) && (declaration.isTypeOnly || (ts.isNamedExports(declaration.parent) && ts.isExportDeclaration(declaration.parent.parent) && declaration.parent.parent.isTypeOnly)), ) ) { return true; } const valueFlags = ts.SymbolFlags.Value | ts.SymbolFlags.Function | ts.SymbolFlags.Class | ts.SymbolFlags.Enum | ts.SymbolFlags.ValueModule; return (exportSymbol.flags & valueFlags) === 0 && (aliasedSymbol.flags & valueFlags) === 0; } function getLocation(node: ts.Node): SourceLocation { const sourceFile = node.getSourceFile(); const position = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)); return { filePath: normalizePath(sourceFile.fileName), line: position.line + 1, column: position.character + 1, }; } function compareExports(left: ExportedSymbolSummary, right: ExportedSymbolSummary): number { const leftLocation = left.declarationLocation; const rightLocation = right.declarationLocation; if (leftLocation && rightLocation) { const file = leftLocation.filePath.localeCompare(rightLocation.filePath); if (file !== 0) return file; const line = leftLocation.line - rightLocation.line; if (line !== 0) return line; const column = leftLocation.column - rightLocation.column; if (column !== 0) return column; } return left.exportName.localeCompare(right.exportName); }