verify-package-readme-limitations.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Doc-sync gate: every package README carries the standard
  3. * `## Known Limitations and Deferred Work` section — the per-package home for
  4. * consumer-visible gaps and consciously postponed work that the
  5. * [documentation standard](../docs/AGENTS.md) assigns to the package-README
  6. * tier. One canonical heading instead of per-package variants ("Limitations",
  7. * "What is NOT here", …) keeps the section greppable across the repo and makes
  8. * its absence a gate failure rather than an oversight.
  9. *
  10. * A package with genuinely nothing to declare is listed in NO_LIMITATIONS
  11. * below and must NOT carry the section — an empty section invites boilerplate,
  12. * and a whitelisted package that gains real limitations leaves the whitelist
  13. * in the same change. Whitelist entries are validated against the scanned
  14. * package set, so a rename or removal fails loud instead of silently
  15. * un-gating a README.
  16. *
  17. * The package set comes from `packages/<group>/<package>/package.json`, so a manifest with no
  18. * sibling README fails instead of escaping a README-only glob. Checks, per
  19. * package README (fenced code excluded):
  20. * 1. Non-whitelisted: exactly one limitations-like heading, byte-equal to the
  21. * canonical h2, with at least one top-level `- ` bullet before the next
  22. * heading.
  23. * 2. Whitelisted: no limitations-like heading at all.
  24. * 3. Every whitelist entry names a scanned package.
  25. *
  26. * "Limitations-like" also matches near-miss headings at any level ("known
  27. * limitations", "deferred work", "what is not here", a heading starting with
  28. * "limitations"/"deferred") so a drifted heading cannot impersonate the
  29. * canonical section and a second competing section cannot coexist with it.
  30. *
  31. * Checker, not fixer: it reports and never rewrites.
  32. * Run: `tsx scripts/verify-package-readme-limitations.ts`.
  33. */
  34. import { existsSync, globSync, readFileSync } from 'node:fs'
  35. import { resolve } from 'node:path'
  36. const root = resolve(import.meta.dirname, '..')
  37. /** The one canonical section heading, required verbatim as an h2. */
  38. const CANONICAL = '## Known Limitations and Deferred Work'
  39. /**
  40. * Packages with genuinely no known limitations or deferred work (keyed by
  41. * package directory relative to the repo root). Their READMEs must NOT carry
  42. * the section; adding one moves the package off this list in the same change.
  43. */
  44. const NO_LIMITATIONS: Readonly<Record<string, string>> = {
  45. 'packages/util/brand': 'Type-only nominal-branding primitive with no runtime behavior or deferred work.',
  46. }
  47. /** A heading that reads as a limitations section — canonical or drifted. */
  48. function isLimitationsLike(headingText: string): boolean {
  49. return (
  50. /\blimitations?\b/i.test(headingText)
  51. || /deferred work/i.test(headingText)
  52. || /what is not here/i.test(headingText)
  53. || /^deferred\b/i.test(headingText)
  54. || /^non-goals?\b/i.test(headingText)
  55. )
  56. }
  57. interface Line {
  58. index: number
  59. raw: string
  60. }
  61. const ATX_HEADING = /^ {0,3}#{1,6}[ \t]+/
  62. /** Split a README into prose lines (fenced code dropped), keeping 1-based line numbers. */
  63. function proseLines(text: string): Line[] {
  64. let fence: { marker: '`' | '~'; length: number } | undefined
  65. const kept: Line[] = []
  66. text.split('\n').forEach((raw, i) => {
  67. const token = /^ {0,3}(`{3,}|~{3,})/.exec(raw)?.[1]
  68. if (token !== undefined) {
  69. const marker = token[0] as '`' | '~'
  70. if (fence === undefined) {
  71. fence = { marker, length: token.length }
  72. } else if (marker === fence.marker && token.length >= fence.length) {
  73. fence = undefined
  74. }
  75. return
  76. }
  77. if (fence === undefined) kept.push({ index: i + 1, raw })
  78. })
  79. return kept
  80. }
  81. const packageJsons = globSync('packages/*/*/package.json', { cwd: root }).sort()
  82. const scannedPackages = new Set(packageJsons.map(path => path.slice(0, -'/package.json'.length)))
  83. const failures: string[] = []
  84. for (const [entry, reason] of Object.entries(NO_LIMITATIONS)) {
  85. if (!scannedPackages.has(entry)) {
  86. failures.push(`whitelist entry ${entry} does not name a scanned package — renamed or removed? update NO_LIMITATIONS in scripts/verify-package-readme-limitations.ts in the same change`)
  87. }
  88. if (reason.trim().length === 0) {
  89. failures.push(`whitelist entry ${entry} has no justification — state why a limitations section would be empty boilerplate`)
  90. }
  91. }
  92. for (const pkg of scannedPackages) {
  93. const readme = `${pkg}/README.md`
  94. if (!existsSync(resolve(root, readme))) {
  95. failures.push(`${readme}: package manifest has no sibling README with the \`${CANONICAL}\` section`)
  96. continue
  97. }
  98. const lines = proseLines(readFileSync(resolve(root, readme), 'utf8'))
  99. const headings = lines.filter(line => ATX_HEADING.test(line.raw))
  100. const limitations = headings.filter(line => isLimitationsLike(line.raw.replace(ATX_HEADING, '')))
  101. if (Object.hasOwn(NO_LIMITATIONS, pkg)) {
  102. for (const heading of limitations) {
  103. failures.push(`${readme}:${heading.index}: whitelisted as having no known limitations, but carries ${JSON.stringify(heading.raw)} — drop the section or remove the package from NO_LIMITATIONS`)
  104. }
  105. continue
  106. }
  107. const heading = limitations.at(0)
  108. if (heading === undefined) {
  109. failures.push(`${readme}: missing the \`${CANONICAL}\` section (a package with genuinely nothing to declare joins NO_LIMITATIONS in scripts/verify-package-readme-limitations.ts instead)`)
  110. continue
  111. }
  112. if (limitations.length > 1) {
  113. failures.push(`${readme}: ${limitations.length} limitations-like headings (lines ${limitations.map(line => line.index).join(', ')}) — keep exactly one \`${CANONICAL}\` section`)
  114. continue
  115. }
  116. if (heading.raw.trimEnd() !== CANONICAL) {
  117. failures.push(`${readme}:${heading.index}: non-canonical heading ${JSON.stringify(heading.raw)} — use \`${CANONICAL}\``)
  118. continue
  119. }
  120. const headingAt = lines.indexOf(heading)
  121. const body = lines.slice(headingAt + 1)
  122. const end = body.findIndex(line => ATX_HEADING.test(line.raw))
  123. const section = end === -1 ? body : body.slice(0, end)
  124. if (!section.some(line => /^- /.test(line.raw))) {
  125. failures.push(`${readme}:${heading.index}: the \`${CANONICAL}\` section has no top-level \`- \` bullet — state the limitations, or whitelist the package if there are genuinely none`)
  126. }
  127. }
  128. if (failures.length > 0) {
  129. console.error('verify-package-readme-limitations: violations found:')
  130. for (const failure of failures) console.error(` ${failure}`)
  131. process.exit(1)
  132. }
  133. console.log(`verify-package-readme-limitations: ${scannedPackages.size} package READMEs checked (${Object.keys(NO_LIMITATIONS).length} whitelisted), all conform.`)