verify-doc-refs.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Verify root-relative documentation paths in repo-authored TypeScript. The
  3. * textual scan covers `docs/*.md` and `.agents/notes/*.md`, requires the
  4. * extension, checks matching string literals too, and excludes built
  5. * declarations and vendored source.
  6. */
  7. import { existsSync } from 'node:fs'
  8. import { resolve } from 'node:path'
  9. import { findReferenceViolations, uniqueRepoFiles, type ReferenceViolation as Violation } from './repo-files.ts'
  10. const root = resolve(import.meta.dirname, '..')
  11. /** Repo-authored TypeScript that may cite docs in comments. */
  12. const PATTERNS = ['packages/**/*.ts']
  13. /** Paths excluded from the scan: built output and vendored upstream source. */
  14. const isExcluded = (p: string): boolean =>
  15. p.includes('/lib/') || p.endsWith('.d.ts') || p.startsWith('vendor/')
  16. /** Root-relative Markdown path token, excluding trailing prose. */
  17. const DOC_REF = /(?:\bdocs|\.agents\/notes)\/[A-Za-z0-9._/-]+\.md/g
  18. /** Find every broken root-relative documentation reference in one TypeScript file. */
  19. function findViolations(absPath: string): Violation[] {
  20. return findReferenceViolations(root, absPath, DOC_REF, ref => ref, ref => !existsSync(resolve(root, ref)))
  21. }
  22. const files = uniqueRepoFiles(root, PATTERNS, isExcluded)
  23. const all = files.flatMap(file => findViolations(file.abs))
  24. const checked = files.length
  25. if (all.length === 0) {
  26. console.log(`verify-doc-refs: ${checked} file(s) checked, all documentation references resolve.`)
  27. process.exit(0)
  28. }
  29. console.error('verify-doc-refs: broken documentation references found in source comments (target does not exist):')
  30. for (const v of all) {
  31. console.error(` ${v.file}:${v.line} ${v.ref}`)
  32. }
  33. process.exit(1)