verify-doc-refs.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Doc-sync gate: verify that doc references written in TypeScript COMMENTS
  3. * resolve to a file that exists. Source comments cite docs by root-relative
  4. * prose path — `see docs/rfc/implemented/testing/2026-06-19-acp-snapshot-tests.md`,
  5. * `docs/architecture.md § Where New Behavior Goes`. `verify-md-links` parses Markdown
  6. * link AST and never sees these, so a doc rename or move could silently orphan
  7. * a `.ts` comment that points at it. The RFC classification reorg
  8. * ([the classification RFC](../docs/rfc/implemented/process/2026-06-20-rfc-classification.md))
  9. * is the motivating case: it moved every RFC under a `{class}/` folder, and
  10. * several `.ts` doc comments cite RFC paths that changed.
  11. *
  12. * Detection is a token scan, NOT an AST walk: doc refs live in free prose inside
  13. * comments, not in a structured form. We match `docs/<path>.md` tokens and
  14. * REQUIRE the `.md` extension, so extensionless prose (`docs/postmortem/0001`,
  15. * `docs/architecture.md § Where New Behavior Goes` — the section suffix is outside the
  16. * token) is left alone rather than misread as a path. Each token is resolved
  17. * ROOT-RELATIVE (the way the comments are written) and must exist on disk. This
  18. * is checker, not fixer: it reports and never rewrites.
  19. *
  20. * Scope is repo-authored TypeScript under `packages/**` and `examples/**`,
  21. * excluding built output (`lib/`, `*.d.ts`) and `vendor/` (pinned upstream
  22. * source we do not own). The scan is purely textual, so it does not distinguish
  23. * a token in a comment from one in a string literal — a `docs/….md` string in
  24. * code is checked too, which is harmless (such a path should resolve anyway).
  25. *
  26. * Run: `tsx scripts/verify-doc-refs.ts`.
  27. */
  28. import { existsSync } from 'node:fs'
  29. import { resolve } from 'node:path'
  30. import { findReferenceViolations, uniqueRepoFiles, type ReferenceViolation as Violation } from './repo-files.ts'
  31. const root = resolve(import.meta.dirname, '..')
  32. /** Repo-authored TypeScript that may cite docs in comments. */
  33. const PATTERNS = ['packages/**/*.ts', 'examples/**/*.ts']
  34. /** Paths excluded from the scan: built output and vendored upstream source. */
  35. const isExcluded = (p: string): boolean =>
  36. p.includes('/lib/') || p.endsWith('.d.ts') || p.startsWith('vendor/')
  37. /**
  38. * Match a `docs/…​.md` reference token. The `.md` extension is required so a
  39. * bare `docs/postmortem/0001` (no extension) does not register as a path. The
  40. * character class stops at whitespace, backticks, parens, and the section sign,
  41. * so trailing prose (`… .md § Where New Behavior Goes`) is not swallowed into the path.
  42. */
  43. const DOC_REF = /\bdocs\/[A-Za-z0-9._/-]+\.md/g
  44. /** Find every broken `docs/….md` reference in one TypeScript file. */
  45. function findViolations(absPath: string): Violation[] {
  46. return findReferenceViolations(root, absPath, DOC_REF, ref => ref, ref => !existsSync(resolve(root, ref)))
  47. }
  48. const files = uniqueRepoFiles(root, PATTERNS, isExcluded)
  49. const all = files.flatMap(file => findViolations(file.abs))
  50. const checked = files.length
  51. if (all.length === 0) {
  52. console.log(`verify-doc-refs: ${checked} file(s) checked, all docs/*.md references resolve.`)
  53. process.exit(0)
  54. }
  55. console.error('verify-doc-refs: broken docs/*.md references found in source comments (target does not exist):')
  56. for (const v of all) {
  57. console.error(` ${v.file}:${v.line} ${v.ref}`)
  58. }
  59. process.exit(1)