verify-package-readme-limitations.ts 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Doc-sync gate for the canonical package-README limitations section. It scans
  3. * package manifests, rejects missing or variant sections, and requires one
  4. * top-level bullet; audited packages in {@link NO_LIMITATIONS} must omit it.
  5. * The archived [limitations gate record](../.agents/notes/archived/process/2026-07-10-readme-known-limitations-gate.md)
  6. * documents the original decision.
  7. */
  8. import { existsSync, globSync, readFileSync } from 'node:fs'
  9. import { resolve, sep } from 'node:path'
  10. import { markdownHeadingLines, markdownProseLines } from './markdown.ts'
  11. const root = resolve(import.meta.dirname, '..')
  12. /** The one canonical section heading, required verbatim as an h2. */
  13. const CANONICAL = '## Known Limitations and Deferred Work'
  14. /** Packages audited as having no limitations section, keyed by repo-relative directory. */
  15. const NO_LIMITATIONS: Readonly<Record<string, string>> = {
  16. 'packages/util/brand': 'Stateless nominal-string and canonical-key helpers have no deferred work.',
  17. }
  18. /** A heading that reads as a limitations section — canonical or drifted. */
  19. function isLimitationsLike(headingText: string): boolean {
  20. return (
  21. /\blimitations?\b/i.test(headingText)
  22. || /deferred work/i.test(headingText)
  23. || /what is not here/i.test(headingText)
  24. || /^deferred\b/i.test(headingText)
  25. || /^non-goals?\b/i.test(headingText)
  26. )
  27. }
  28. const packageJsons = globSync('packages/*/*/package.json', { cwd: root }).map(path => path.split(sep).join('/')).sort()
  29. const scannedPackages = new Set(packageJsons.map(path => path.slice(0, -'/package.json'.length)))
  30. const failures: string[] = []
  31. for (const [entry, reason] of Object.entries(NO_LIMITATIONS)) {
  32. if (!scannedPackages.has(entry)) {
  33. 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`)
  34. }
  35. if (reason.trim().length === 0) {
  36. failures.push(`whitelist entry ${entry} has no justification — state why a limitations section would be empty boilerplate`)
  37. }
  38. }
  39. for (const pkg of scannedPackages) {
  40. const readme = `${pkg}/README.md`
  41. if (!existsSync(resolve(root, readme))) {
  42. failures.push(`${readme}: package manifest has no sibling README with the \`${CANONICAL}\` section`)
  43. continue
  44. }
  45. const source = readFileSync(resolve(root, readme), 'utf8')
  46. const lines = markdownProseLines(source)
  47. const headings = markdownHeadingLines(source)
  48. const limitations = headings.filter(heading => isLimitationsLike(heading.text))
  49. if (Object.hasOwn(NO_LIMITATIONS, pkg)) {
  50. for (const heading of limitations) {
  51. 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`)
  52. }
  53. continue
  54. }
  55. const heading = limitations.at(0)
  56. if (heading === undefined) {
  57. 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)`)
  58. continue
  59. }
  60. if (limitations.length > 1) {
  61. failures.push(`${readme}: ${limitations.length} limitations-like headings (lines ${limitations.map(line => line.index).join(', ')}) — keep exactly one \`${CANONICAL}\` section`)
  62. continue
  63. }
  64. if (heading.depth !== 2 || heading.raw.trimEnd() !== CANONICAL) {
  65. failures.push(`${readme}:${heading.index}: non-canonical heading ${JSON.stringify(heading.raw)} — use \`${CANONICAL}\``)
  66. continue
  67. }
  68. const headingAt = lines.findIndex(line => line.index === heading.index)
  69. const body = lines.slice(headingAt + 1)
  70. const headingLines = new Set(headings.map(entry => entry.index))
  71. const end = body.findIndex(line => headingLines.has(line.index))
  72. const section = end === -1 ? body : body.slice(0, end)
  73. if (!section.some(line => /^- /.test(line.raw))) {
  74. 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`)
  75. }
  76. }
  77. if (failures.length > 0) {
  78. console.error('verify-package-readme-limitations: violations found:')
  79. for (const failure of failures) console.error(` ${failure}`)
  80. process.exit(1)
  81. }
  82. console.log(`verify-package-readme-limitations: ${scannedPackages.size} package READMEs checked (${Object.keys(NO_LIMITATIONS).length} whitelisted), all conform.`)