|
| 1 | +import type { OpenPkg, SpecExportKind } from '@openpkg-ts/spec'; |
| 2 | + |
| 3 | +export type SignalStats = { covered: number; total: number; pct: number }; |
| 4 | + |
| 5 | +export type ReportStats = { |
| 6 | + packageName: string; |
| 7 | + version: string; |
| 8 | + coverageScore: number; |
| 9 | + totalExports: number; |
| 10 | + fullyDocumented: number; |
| 11 | + partiallyDocumented: number; |
| 12 | + undocumented: number; |
| 13 | + driftCount: number; |
| 14 | + signalCoverage: Record<'description' | 'params' | 'returns' | 'examples', SignalStats>; |
| 15 | + byKind: Array<{ kind: SpecExportKind; count: number; avgScore: number }>; |
| 16 | + exports: Array<{ name: string; kind: SpecExportKind; score: number; missing: string[] }>; |
| 17 | + driftIssues: Array<{ exportName: string; type: string; issue: string; suggestion?: string }>; |
| 18 | +}; |
| 19 | + |
| 20 | +export function computeStats(spec: OpenPkg): ReportStats { |
| 21 | + const exports = spec.exports ?? []; |
| 22 | + const signals = { |
| 23 | + description: { covered: 0, total: 0 }, |
| 24 | + params: { covered: 0, total: 0 }, |
| 25 | + returns: { covered: 0, total: 0 }, |
| 26 | + examples: { covered: 0, total: 0 }, |
| 27 | + }; |
| 28 | + const kindMap = new Map<SpecExportKind, { count: number; totalScore: number }>(); |
| 29 | + const driftIssues: ReportStats['driftIssues'] = []; |
| 30 | + let fullyDocumented = 0; |
| 31 | + let partiallyDocumented = 0; |
| 32 | + let undocumented = 0; |
| 33 | + |
| 34 | + for (const exp of exports) { |
| 35 | + const score = exp.docs?.coverageScore ?? 0; |
| 36 | + const missing = exp.docs?.missing ?? []; |
| 37 | + |
| 38 | + // Tally signals |
| 39 | + for (const sig of ['description', 'params', 'returns', 'examples'] as const) { |
| 40 | + signals[sig].total++; |
| 41 | + if (!missing.includes(sig)) signals[sig].covered++; |
| 42 | + } |
| 43 | + |
| 44 | + // Tally by kind |
| 45 | + const kindEntry = kindMap.get(exp.kind) ?? { count: 0, totalScore: 0 }; |
| 46 | + kindEntry.count++; |
| 47 | + kindEntry.totalScore += score; |
| 48 | + kindMap.set(exp.kind, kindEntry); |
| 49 | + |
| 50 | + // Categorize |
| 51 | + if (score === 100) fullyDocumented++; |
| 52 | + else if (score > 0) partiallyDocumented++; |
| 53 | + else undocumented++; |
| 54 | + |
| 55 | + // Collect drift |
| 56 | + for (const d of exp.docs?.drift ?? []) { |
| 57 | + driftIssues.push({ |
| 58 | + exportName: exp.name, |
| 59 | + type: d.type, |
| 60 | + issue: d.issue, |
| 61 | + suggestion: d.suggestion, |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + const signalCoverage = Object.fromEntries( |
| 67 | + Object.entries(signals).map(([k, v]) => [ |
| 68 | + k, |
| 69 | + { ...v, pct: v.total ? Math.round((v.covered / v.total) * 100) : 0 }, |
| 70 | + ]), |
| 71 | + ) as ReportStats['signalCoverage']; |
| 72 | + |
| 73 | + const byKind = Array.from(kindMap.entries()) |
| 74 | + .map(([kind, { count, totalScore }]) => ({ |
| 75 | + kind, |
| 76 | + count, |
| 77 | + avgScore: Math.round(totalScore / count), |
| 78 | + })) |
| 79 | + .sort((a, b) => b.count - a.count); |
| 80 | + |
| 81 | + const sortedExports = exports |
| 82 | + .map((e) => ({ |
| 83 | + name: e.name, |
| 84 | + kind: e.kind, |
| 85 | + score: e.docs?.coverageScore ?? 0, |
| 86 | + missing: e.docs?.missing ?? [], |
| 87 | + })) |
| 88 | + .sort((a, b) => a.score - b.score); |
| 89 | + |
| 90 | + return { |
| 91 | + packageName: spec.meta.name ?? 'unknown', |
| 92 | + version: spec.meta.version ?? '0.0.0', |
| 93 | + coverageScore: spec.docs?.coverageScore ?? 0, |
| 94 | + totalExports: exports.length, |
| 95 | + fullyDocumented, |
| 96 | + partiallyDocumented, |
| 97 | + undocumented, |
| 98 | + driftCount: driftIssues.length, |
| 99 | + signalCoverage, |
| 100 | + byKind, |
| 101 | + exports: sortedExports, |
| 102 | + driftIssues, |
| 103 | + }; |
| 104 | +} |
0 commit comments