verify-package-readme-limitations.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 and HTML comments 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. import { markdownHeadingLines, markdownProseLines } from './markdown.ts'
  37. const root = resolve(import.meta.dirname, '..')
  38. /** The one canonical section heading, required verbatim as an h2. */
  39. const CANONICAL = '## Known Limitations and Deferred Work'
  40. /**
  41. * Packages with genuinely no known limitations or deferred work (keyed by
  42. * package directory relative to the repo root). Their READMEs must NOT carry
  43. * the section; adding one moves the package off this list in the same change.
  44. */
  45. const NO_LIMITATIONS: Readonly<Record<string, string>> = {
  46. 'packages/util/brand': 'Type-only nominal-branding primitive with no runtime behavior or deferred work.',
  47. }
  48. /** A heading that reads as a limitations section — canonical or drifted. */
  49. function isLimitationsLike(headingText: string): boolean {
  50. return (
  51. /\blimitations?\b/i.test(headingText)
  52. || /deferred work/i.test(headingText)
  53. || /what is not here/i.test(headingText)
  54. || /^deferred\b/i.test(headingText)
  55. || /^non-goals?\b/i.test(headingText)
  56. )
  57. }
  58. const packageJsons = globSync('packages/*/*/package.json', { cwd: root }).sort()
  59. const scannedPackages = new Set(packageJsons.map(path => path.slice(0, -'/package.json'.length)))
  60. const failures: string[] = []
  61. for (const [entry, reason] of Object.entries(NO_LIMITATIONS)) {
  62. if (!scannedPackages.has(entry)) {
  63. 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`)
  64. }
  65. if (reason.trim().length === 0) {
  66. failures.push(`whitelist entry ${entry} has no justification — state why a limitations section would be empty boilerplate`)
  67. }
  68. }
  69. for (const pkg of scannedPackages) {
  70. const readme = `${pkg}/README.md`
  71. if (!existsSync(resolve(root, readme))) {
  72. failures.push(`${readme}: package manifest has no sibling README with the \`${CANONICAL}\` section`)
  73. continue
  74. }
  75. const source = readFileSync(resolve(root, readme), 'utf8')
  76. const lines = markdownProseLines(source)
  77. const headings = markdownHeadingLines(source)
  78. const limitations = headings.filter(heading => isLimitationsLike(heading.text))
  79. if (Object.hasOwn(NO_LIMITATIONS, pkg)) {
  80. for (const heading of limitations) {
  81. 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`)
  82. }
  83. continue
  84. }
  85. const heading = limitations.at(0)
  86. if (heading === undefined) {
  87. 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)`)
  88. continue
  89. }
  90. if (limitations.length > 1) {
  91. failures.push(`${readme}: ${limitations.length} limitations-like headings (lines ${limitations.map(line => line.index).join(', ')}) — keep exactly one \`${CANONICAL}\` section`)
  92. continue
  93. }
  94. if (heading.depth !== 2 || heading.raw.trimEnd() !== CANONICAL) {
  95. failures.push(`${readme}:${heading.index}: non-canonical heading ${JSON.stringify(heading.raw)} — use \`${CANONICAL}\``)
  96. continue
  97. }
  98. const headingAt = lines.findIndex(line => line.index === heading.index)
  99. const body = lines.slice(headingAt + 1)
  100. const headingLines = new Set(headings.map(entry => entry.index))
  101. const end = body.findIndex(line => headingLines.has(line.index))
  102. const section = end === -1 ? body : body.slice(0, end)
  103. if (!section.some(line => /^- /.test(line.raw))) {
  104. 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`)
  105. }
  106. }
  107. if (failures.length > 0) {
  108. console.error('verify-package-readme-limitations: violations found:')
  109. for (const failure of failures) console.error(` ${failure}`)
  110. process.exit(1)
  111. }
  112. console.log(`verify-package-readme-limitations: ${scannedPackages.size} package READMEs checked (${Object.keys(NO_LIMITATIONS).length} whitelisted), all conform.`)