verify-doc-refs.ts 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, globSync, readFileSync } from 'node:fs'
  29. import { relative, resolve } from 'node:path'
  30. const root = resolve(import.meta.dirname, '..')
  31. /** Repo-authored TypeScript that may cite docs in comments. */
  32. const PATTERNS = ['packages/**/*.ts', 'examples/**/*.ts']
  33. /** Paths excluded from the scan: built output and vendored upstream source. */
  34. const isExcluded = (p: string): boolean =>
  35. p.includes('/lib/') || p.endsWith('.d.ts') || p.startsWith('vendor/')
  36. /**
  37. * Match a `docs/…​.md` reference token. The `.md` extension is required so a
  38. * bare `docs/postmortem/0001` (no extension) does not register as a path. The
  39. * character class stops at whitespace, backticks, parens, and the section sign,
  40. * so trailing prose (`… .md § Where New Behavior Goes`) is not swallowed into the path.
  41. */
  42. const DOC_REF = /\bdocs\/[A-Za-z0-9._/-]+\.md/g
  43. /** A broken doc reference: a root-relative `docs/….md` token with no file. */
  44. interface Violation {
  45. file: string
  46. /** 1-based line where the reference appears. */
  47. line: number
  48. ref: string
  49. }
  50. /** Find every broken `docs/….md` reference in one TypeScript file. */
  51. function findViolations(absPath: string): Violation[] {
  52. const file = relative(root, absPath)
  53. const source = readFileSync(absPath, 'utf8')
  54. const out: Violation[] = []
  55. const lines = source.split('\n')
  56. for (let i = 0; i < lines.length; i++) {
  57. const line = lines[i]
  58. if (line === undefined) continue
  59. for (const m of line.matchAll(DOC_REF)) {
  60. const ref = m[0]
  61. if (!existsSync(resolve(root, ref))) {
  62. out.push({ file, line: i + 1, ref })
  63. }
  64. }
  65. }
  66. return out
  67. }
  68. const all: Violation[] = []
  69. let checked = 0
  70. for (const pattern of PATTERNS) {
  71. for (const match of globSync(pattern, { cwd: root })) {
  72. if (isExcluded(match)) continue
  73. checked++
  74. all.push(...findViolations(resolve(root, match)))
  75. }
  76. }
  77. if (all.length === 0) {
  78. console.log(`verify-doc-refs: ${checked} file(s) checked, all docs/*.md references resolve.`)
  79. process.exit(0)
  80. }
  81. console.error('verify-doc-refs: broken docs/*.md references found in source comments (target does not exist):')
  82. for (const v of all) {
  83. console.error(` ${v.file}:${v.line} ${v.ref}`)
  84. }
  85. process.exit(1)