|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Top-level declarations that require @since or @unstable annotations. |
| 8 | + * These are matched at the start of a line (with optional leading whitespace). |
| 9 | + */ |
| 10 | +const DECLARATION_PATTERNS = [ |
| 11 | + { name: 'interface', regex: /^\s*interface\s+([a-z][a-z0-9-]*)\s*\{/i }, |
| 12 | + { name: 'world', regex: /^\s*world\s+([a-z][a-z0-9-]*)\s*\{/i }, |
| 13 | + { name: 'type', regex: /^\s*type\s+([a-z][a-z0-9-]*)\s*=/i }, |
| 14 | + { name: 'record', regex: /^\s*record\s+([a-z][a-z0-9-]*)\s*\{/i }, |
| 15 | + { name: 'variant', regex: /^\s*variant\s+([a-z][a-z0-9-]*)\s*\{/i }, |
| 16 | + { name: 'enum', regex: /^\s*enum\s+([a-z][a-z0-9-]*)\s*\{/i }, |
| 17 | + { name: 'flags', regex: /^\s*flags\s+([a-z][a-z0-9-]*)\s*\{/i }, |
| 18 | + { name: 'resource', regex: /^\s*resource\s+([a-z][a-z0-9-]*)\s*[{;]/i }, |
| 19 | +]; |
| 20 | + |
| 21 | +/** |
| 22 | + * Annotation patterns that satisfy the @since requirement. |
| 23 | + */ |
| 24 | +const SINCE_PATTERN = /@since\s*\(\s*version\s*=\s*[0-9a-z.\-]+\s*\)/i; |
| 25 | +const UNSTABLE_PATTERN = /@unstable\s*\(\s*feature\s*=\s*[a-z][a-z0-9-]*\s*\)/i; |
| 26 | + |
| 27 | +/** |
| 28 | + * Check if a line has a preceding @since or @unstable annotation. |
| 29 | + * Looks backward through lines, skipping doc comments (///). |
| 30 | + */ |
| 31 | +function hasVersionAnnotation(lines, lineIndex, maxLookback = 20) { |
| 32 | + for (let i = 1; i <= Math.min(lineIndex, maxLookback); i++) { |
| 33 | + const prevLine = lines[lineIndex - i]; |
| 34 | + if (!prevLine) continue; |
| 35 | + |
| 36 | + const trimmed = prevLine.trim(); |
| 37 | + |
| 38 | + // Found @since annotation |
| 39 | + if (SINCE_PATTERN.test(trimmed)) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + // Found @unstable annotation (accepted alternative) |
| 44 | + if (UNSTABLE_PATTERN.test(trimmed)) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + // Skip doc comments - continue looking |
| 49 | + if (trimmed.startsWith('///')) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + // Skip other annotations - continue looking |
| 54 | + if (trimmed.startsWith('@')) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + // Skip empty lines - continue looking |
| 59 | + if (trimmed === '') { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + // Hit non-annotation, non-comment content - stop looking |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + return false; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Validate a single WIT file for @since annotations. |
| 72 | + * @param {string} filePath - Path to the WIT file |
| 73 | + * @returns {Array} Array of error objects { file, line, declaration, name, message } |
| 74 | + */ |
| 75 | +function validateFile(filePath) { |
| 76 | + const errors = []; |
| 77 | + |
| 78 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 79 | + const lines = content.split('\n'); |
| 80 | + |
| 81 | + for (let i = 0; i < lines.length; i++) { |
| 82 | + const line = lines[i]; |
| 83 | + |
| 84 | + for (const { name, regex } of DECLARATION_PATTERNS) { |
| 85 | + const match = line.match(regex); |
| 86 | + if (match) { |
| 87 | + if (!hasVersionAnnotation(lines, i)) { |
| 88 | + errors.push({ |
| 89 | + file: filePath, |
| 90 | + line: i + 1, // 1-indexed for display |
| 91 | + declaration: name, |
| 92 | + name: match[1], |
| 93 | + message: `Missing @since annotation for ${name} '${match[1]}'`, |
| 94 | + }); |
| 95 | + } |
| 96 | + break; // Only match one pattern per line |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return errors; |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Validate all WIT files in a directory recursively. |
| 106 | + * Excludes deps/ directories. |
| 107 | + * @param {string} dirPath - Directory to validate |
| 108 | + * @returns {Array} Array of all errors |
| 109 | + */ |
| 110 | +function validateDirectory(dirPath) { |
| 111 | + const errors = []; |
| 112 | + |
| 113 | + function walkDir(dir) { |
| 114 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 115 | + for (const entry of entries) { |
| 116 | + const fullPath = path.join(dir, entry.name); |
| 117 | + |
| 118 | + if (entry.isDirectory()) { |
| 119 | + // Skip deps directories |
| 120 | + if (entry.name === 'deps') { |
| 121 | + continue; |
| 122 | + } |
| 123 | + walkDir(fullPath); |
| 124 | + } else if (entry.name.endsWith('.wit')) { |
| 125 | + errors.push(...validateFile(fullPath)); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + walkDir(dirPath); |
| 131 | + return errors; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Format errors for GitHub Actions output (clickable annotations). |
| 136 | + * @param {Array} errors - Array of error objects |
| 137 | + * @returns {string} Formatted error output |
| 138 | + */ |
| 139 | +function formatErrors(errors) { |
| 140 | + return errors.map(err => { |
| 141 | + const relPath = path.relative(process.cwd(), err.file); |
| 142 | + return `::error file=${relPath},line=${err.line}::${err.message}`; |
| 143 | + }).join('\n'); |
| 144 | +} |
| 145 | + |
| 146 | +// CLI usage: node validate-since.js <directory> |
| 147 | +if (require.main === module) { |
| 148 | + const args = process.argv.slice(2); |
| 149 | + |
| 150 | + if (args.length === 0) { |
| 151 | + console.log('Usage: node validate-since.js <directory>'); |
| 152 | + console.log('Example: node validate-since.js proposals/io/wit'); |
| 153 | + process.exit(1); |
| 154 | + } |
| 155 | + |
| 156 | + const targetDir = args[0]; |
| 157 | + |
| 158 | + if (!fs.existsSync(targetDir)) { |
| 159 | + console.error(`Directory not found: ${targetDir}`); |
| 160 | + process.exit(1); |
| 161 | + } |
| 162 | + |
| 163 | + console.log(`Validating @since annotations in ${targetDir}...\n`); |
| 164 | + |
| 165 | + const errors = validateDirectory(targetDir); |
| 166 | + |
| 167 | + if (errors.length > 0) { |
| 168 | + console.log(formatErrors(errors)); |
| 169 | + console.log(`\n${errors.length} missing @since annotation(s) found.`); |
| 170 | + process.exit(1); |
| 171 | + } else { |
| 172 | + console.log('All declarations have @since annotations.'); |
| 173 | + process.exit(0); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +module.exports = { |
| 178 | + validateFile, |
| 179 | + validateDirectory, |
| 180 | + formatErrors, |
| 181 | + DECLARATION_PATTERNS, |
| 182 | +}; |
0 commit comments