|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | +const DEFAULT_ROOT = path.resolve(__dirname, '..'); |
| 10 | +const INTERFACES_DIR = path.join(DEFAULT_ROOT, 'docs', 'interfaces'); |
| 11 | + |
| 12 | +const KNOWN_NON_CONTRACT_DOCS = new Set(['README.md', 'runbook.md']); |
| 13 | + |
| 14 | +function readText(filePath) { |
| 15 | + return fs.readFileSync(filePath, 'utf8'); |
| 16 | +} |
| 17 | + |
| 18 | +function parseSourcePathFromHeading(markdown) { |
| 19 | + const heading = markdown.match(/^# .*\(([^)]+)\)/m); |
| 20 | + if (!heading) return null; |
| 21 | + return heading[1].trim(); |
| 22 | +} |
| 23 | + |
| 24 | +function parseDocumentedMethods(markdown) { |
| 25 | + const lines = markdown.split(/\r?\n/); |
| 26 | + const methods = new Set(); |
| 27 | + let inMethodsTable = false; |
| 28 | + |
| 29 | + for (const line of lines) { |
| 30 | + if (/^\s*\|\s*Method\s*\|/i.test(line)) { |
| 31 | + inMethodsTable = true; |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + if (!inMethodsTable) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + if (!line.trim().startsWith('|')) { |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + if (/^\s*\|\s*-+\s*\|/.test(line)) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + const columns = line.split('|'); |
| 48 | + if (columns.length < 3) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + const rawMethodCell = columns[1].trim(); |
| 53 | + if (!rawMethodCell) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + const unquoted = rawMethodCell.replace(/`/g, ''); |
| 58 | + // Support grouped rows such as "pause / unpause" |
| 59 | + const grouped = unquoted.split('/').map((part) => part.trim()); |
| 60 | + for (const methodName of grouped) { |
| 61 | + if (!methodName || /\s/.test(methodName)) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + methods.add(methodName); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return methods; |
| 69 | +} |
| 70 | + |
| 71 | +function parseContractMethods(rustSource) { |
| 72 | + const blocks = extractContractImplBlocks(rustSource); |
| 73 | + const methods = new Set(); |
| 74 | + if (blocks.length === 0) { |
| 75 | + return methods; |
| 76 | + } |
| 77 | + |
| 78 | + // Production contract entrypoints are expected in the first #[contractimpl] block. |
| 79 | + // This intentionally ignores test-only helper contracts declared later in #[cfg(test)] modules. |
| 80 | + const targetBlock = blocks[0]; |
| 81 | + const regex = /\bpub\s+fn\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/g; |
| 82 | + let match; |
| 83 | + while ((match = regex.exec(targetBlock)) !== null) { |
| 84 | + methods.add(match[1]); |
| 85 | + } |
| 86 | + return methods; |
| 87 | +} |
| 88 | + |
| 89 | +function extractContractImplBlocks(rustSource) { |
| 90 | + const blocks = []; |
| 91 | + const marker = '#[contractimpl]'; |
| 92 | + let cursor = 0; |
| 93 | + |
| 94 | + while (cursor < rustSource.length) { |
| 95 | + const markerIdx = rustSource.indexOf(marker, cursor); |
| 96 | + if (markerIdx === -1) { |
| 97 | + break; |
| 98 | + } |
| 99 | + |
| 100 | + const implIdx = rustSource.indexOf('impl', markerIdx + marker.length); |
| 101 | + if (implIdx === -1) { |
| 102 | + break; |
| 103 | + } |
| 104 | + |
| 105 | + const braceStart = rustSource.indexOf('{', implIdx); |
| 106 | + if (braceStart === -1) { |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + let depth = 0; |
| 111 | + let end = braceStart; |
| 112 | + for (; end < rustSource.length; end++) { |
| 113 | + const ch = rustSource[end]; |
| 114 | + if (ch === '{') { |
| 115 | + depth += 1; |
| 116 | + } else if (ch === '}') { |
| 117 | + depth -= 1; |
| 118 | + if (depth === 0) { |
| 119 | + end += 1; |
| 120 | + break; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (depth === 0) { |
| 126 | + blocks.push(rustSource.slice(braceStart, end)); |
| 127 | + cursor = end; |
| 128 | + } else { |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return blocks; |
| 134 | +} |
| 135 | + |
| 136 | +function toSortedArray(set) { |
| 137 | + return Array.from(set).sort(); |
| 138 | +} |
| 139 | + |
| 140 | +function setDiff(left, right) { |
| 141 | + const diff = new Set(); |
| 142 | + for (const item of left) { |
| 143 | + if (!right.has(item)) { |
| 144 | + diff.add(item); |
| 145 | + } |
| 146 | + } |
| 147 | + return diff; |
| 148 | +} |
| 149 | + |
| 150 | +function collectInterfaceMarkdownFiles(interfacesDir) { |
| 151 | + return fs |
| 152 | + .readdirSync(interfacesDir) |
| 153 | + .filter((entry) => entry.endsWith('.md')) |
| 154 | + .filter((entry) => !KNOWN_NON_CONTRACT_DOCS.has(entry)) |
| 155 | + .sort() |
| 156 | + .map((entry) => path.join(interfacesDir, entry)); |
| 157 | +} |
| 158 | + |
| 159 | +function validateDocFile(rootDir, docPath) { |
| 160 | + const markdown = readText(docPath); |
| 161 | + const headingSourcePath = parseSourcePathFromHeading(markdown); |
| 162 | + if (!headingSourcePath) { |
| 163 | + return { |
| 164 | + ok: false, |
| 165 | + docPath, |
| 166 | + error: |
| 167 | + 'Missing source path in H1 heading, expected e.g. "# Name (contracts/.../src/lib.rs)".', |
| 168 | + }; |
| 169 | + } |
| 170 | + |
| 171 | + const sourcePath = path.join(rootDir, headingSourcePath); |
| 172 | + if (!fs.existsSync(sourcePath)) { |
| 173 | + return { |
| 174 | + ok: false, |
| 175 | + docPath, |
| 176 | + sourcePath, |
| 177 | + error: `Referenced source file does not exist: ${headingSourcePath}`, |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + const documentedMethods = parseDocumentedMethods(markdown); |
| 182 | + if (documentedMethods.size === 0) { |
| 183 | + return { |
| 184 | + ok: false, |
| 185 | + docPath, |
| 186 | + sourcePath, |
| 187 | + error: 'No methods found in Methods table.', |
| 188 | + }; |
| 189 | + } |
| 190 | + |
| 191 | + const contractMethods = parseContractMethods(readText(sourcePath)); |
| 192 | + const undocumented = setDiff(contractMethods, documentedMethods); |
| 193 | + const staleDocs = setDiff(documentedMethods, contractMethods); |
| 194 | + |
| 195 | + return { |
| 196 | + ok: undocumented.size === 0 && staleDocs.size === 0, |
| 197 | + docPath, |
| 198 | + sourcePath, |
| 199 | + undocumented: toSortedArray(undocumented), |
| 200 | + staleDocs: toSortedArray(staleDocs), |
| 201 | + }; |
| 202 | +} |
| 203 | + |
| 204 | +export function checkInterfaceDocsDrift(rootDir = DEFAULT_ROOT) { |
| 205 | + const interfacesDir = path.join(rootDir, 'docs', 'interfaces'); |
| 206 | + const docFiles = collectInterfaceMarkdownFiles(interfacesDir); |
| 207 | + |
| 208 | + const results = docFiles.map((docPath) => validateDocFile(rootDir, docPath)); |
| 209 | + const failed = results.filter((result) => !result.ok); |
| 210 | + |
| 211 | + return { |
| 212 | + ok: failed.length === 0, |
| 213 | + checked: results.length, |
| 214 | + results, |
| 215 | + failed, |
| 216 | + }; |
| 217 | +} |
| 218 | + |
| 219 | +function relativeFromRoot(filePath, rootDir) { |
| 220 | + return path.relative(rootDir, filePath) || filePath; |
| 221 | +} |
| 222 | + |
| 223 | +function printFailures(summary, rootDir) { |
| 224 | + for (const result of summary.failed) { |
| 225 | + const label = relativeFromRoot(result.docPath, rootDir); |
| 226 | + console.error(`- ${label}`); |
| 227 | + if (result.error) { |
| 228 | + console.error(` error: ${result.error}`); |
| 229 | + continue; |
| 230 | + } |
| 231 | + if (result.undocumented?.length) { |
| 232 | + console.error(` undocumented methods: ${result.undocumented.join(', ')}`); |
| 233 | + } |
| 234 | + if (result.staleDocs?.length) { |
| 235 | + console.error(` stale docs methods: ${result.staleDocs.join(', ')}`); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +function runCli() { |
| 241 | + if (!fs.existsSync(INTERFACES_DIR)) { |
| 242 | + console.error(`interfaces dir not found: ${INTERFACES_DIR}`); |
| 243 | + process.exit(1); |
| 244 | + } |
| 245 | + |
| 246 | + const summary = checkInterfaceDocsDrift(DEFAULT_ROOT); |
| 247 | + if (summary.ok) { |
| 248 | + console.log( |
| 249 | + `Interface docs drift check passed (${summary.checked} files).`, |
| 250 | + ); |
| 251 | + return; |
| 252 | + } |
| 253 | + |
| 254 | + console.error( |
| 255 | + `Interface docs drift check failed (${summary.failed.length}/${summary.checked} files).`, |
| 256 | + ); |
| 257 | + printFailures(summary, DEFAULT_ROOT); |
| 258 | + process.exit(1); |
| 259 | +} |
| 260 | + |
| 261 | +const invokedDirectly = |
| 262 | + process.argv[1] && path.resolve(process.argv[1]) === __filename; |
| 263 | + |
| 264 | +if (invokedDirectly) { |
| 265 | + runCli(); |
| 266 | +} |
| 267 | + |
| 268 | +export { |
| 269 | + parseSourcePathFromHeading, |
| 270 | + parseDocumentedMethods, |
| 271 | + parseContractMethods, |
| 272 | + extractContractImplBlocks, |
| 273 | + collectInterfaceMarkdownFiles, |
| 274 | + validateDocFile, |
| 275 | +}; |
0 commit comments