verify-package-readme-limitations.ts 4.4 KB

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