|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Verify that all dynamic import() paths in src/ resolve to existing files. |
| 5 | + * |
| 6 | + * Catches stale paths left behind after moves/renames — the class of bug |
| 7 | + * that caused the ast-command crash (see roadmap 10.3). |
| 8 | + * |
| 9 | + * Exit codes: |
| 10 | + * 0 — all imports resolve |
| 11 | + * 1 — one or more broken imports found |
| 12 | + */ |
| 13 | + |
| 14 | +import { readFileSync, existsSync, readdirSync, statSync } from 'node:fs'; |
| 15 | +import { resolve, dirname, join, extname } from 'node:path'; |
| 16 | +import { fileURLToPath } from 'node:url'; |
| 17 | + |
| 18 | +const __filename = fileURLToPath(import.meta.url); |
| 19 | +const __dirname = dirname(__filename); |
| 20 | +const srcDir = resolve(__dirname, '..', 'src'); |
| 21 | + |
| 22 | +// ── collect source files ──────────────────────────────────────────────── |
| 23 | +function walk(dir) { |
| 24 | + const results = []; |
| 25 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 26 | + const full = join(dir, entry.name); |
| 27 | + if (entry.isDirectory()) { |
| 28 | + if (entry.name === 'node_modules') continue; |
| 29 | + results.push(...walk(full)); |
| 30 | + } else if (/\.[jt]sx?$/.test(entry.name)) { |
| 31 | + results.push(full); |
| 32 | + } |
| 33 | + } |
| 34 | + return results; |
| 35 | +} |
| 36 | + |
| 37 | +// ── extract dynamic import specifiers ─────────────────────────────────── |
| 38 | +// Matches: import('...') and import("...") — with or without await |
| 39 | +const DYNAMIC_IMPORT_RE = /(?:await\s+)?import\(\s*(['"])(.+?)\1\s*\)/g; |
| 40 | + |
| 41 | +/** |
| 42 | + * Check whether the text contains a `//` line-comment marker that is NOT |
| 43 | + * inside a string literal. Walks character-by-character tracking quote state. |
| 44 | + */ |
| 45 | +function isInsideLineComment(text) { |
| 46 | + let inStr = null; // null | "'" | '"' | '`' |
| 47 | + for (let i = 0; i < text.length; i++) { |
| 48 | + const ch = text[i]; |
| 49 | + if (ch === '\\' && inStr) { i++; continue; } // skip escaped char |
| 50 | + if (inStr) { |
| 51 | + if (ch === inStr) inStr = null; |
| 52 | + continue; |
| 53 | + } |
| 54 | + if (ch === "'" || ch === '"' || ch === '`') { inStr = ch; continue; } |
| 55 | + if (ch === '/' && text[i + 1] === '/') return true; |
| 56 | + } |
| 57 | + return false; |
| 58 | +} |
| 59 | + |
| 60 | +function extractDynamicImports(filePath) { |
| 61 | + const src = readFileSync(filePath, 'utf8'); |
| 62 | + const imports = []; |
| 63 | + const lines = src.split('\n'); |
| 64 | + |
| 65 | + let inBlockComment = false; |
| 66 | + for (let i = 0; i < lines.length; i++) { |
| 67 | + const line = lines[i]; |
| 68 | + |
| 69 | + // Track block comments (/** ... */ and /* ... */) |
| 70 | + let scanLine = line; |
| 71 | + if (inBlockComment) { |
| 72 | + const closeIdx = scanLine.indexOf('*/'); |
| 73 | + if (closeIdx === -1) continue; // still fully inside a block comment |
| 74 | + inBlockComment = false; |
| 75 | + scanLine = scanLine.slice(closeIdx + 2); // scan content after */ |
| 76 | + } |
| 77 | + // Skip single-line comments |
| 78 | + if (/^\s*\/\//.test(scanLine)) continue; |
| 79 | + if (scanLine.includes('/*')) { |
| 80 | + // Remove fully closed inline block comments: code /* ... */ more code |
| 81 | + scanLine = scanLine.replace(/\/\*.*?\*\//g, ''); |
| 82 | + // If an unclosed /* remains, keep only the part before it and enter block mode |
| 83 | + const openIdx = scanLine.indexOf('/*'); |
| 84 | + if (openIdx !== -1) { |
| 85 | + scanLine = scanLine.slice(0, openIdx); |
| 86 | + inBlockComment = true; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + let match; |
| 91 | + DYNAMIC_IMPORT_RE.lastIndex = 0; |
| 92 | + while ((match = DYNAMIC_IMPORT_RE.exec(scanLine)) !== null) { |
| 93 | + // Skip if the match is inside a trailing line comment (// outside quotes) |
| 94 | + const before = scanLine.slice(0, match.index); |
| 95 | + if (isInsideLineComment(before)) continue; |
| 96 | + |
| 97 | + imports.push({ specifier: match[2], line: i + 1 }); |
| 98 | + } |
| 99 | + } |
| 100 | + return imports; |
| 101 | +} |
| 102 | + |
| 103 | +// ── resolve a specifier to a file on disk ─────────────────────────────── |
| 104 | +function resolveSpecifier(specifier, fromFile) { |
| 105 | + // Skip bare specifiers (packages): 'node:*', '@scope/pkg', 'pkg' |
| 106 | + if (!specifier.startsWith('.') && !specifier.startsWith('/')) return null; |
| 107 | + |
| 108 | + const base = dirname(fromFile); |
| 109 | + const target = resolve(base, specifier); |
| 110 | + |
| 111 | + // Exact file exists |
| 112 | + if (existsSync(target) && statSync(target).isFile()) return null; |
| 113 | + |
| 114 | + // Try implicit extensions (.js, .ts, .mjs, .cjs) |
| 115 | + for (const ext of ['.js', '.ts', '.mjs', '.cjs']) { |
| 116 | + if (!extname(target) && existsSync(target + ext)) return null; |
| 117 | + } |
| 118 | + |
| 119 | + // Try index files (directory import) |
| 120 | + if (existsSync(target) && statSync(target).isDirectory()) { |
| 121 | + for (const idx of ['index.js', 'index.ts', 'index.mjs']) { |
| 122 | + if (existsSync(join(target, idx))) return null; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Not resolved — broken |
| 127 | + return specifier; |
| 128 | +} |
| 129 | + |
| 130 | +// ── main ──────────────────────────────────────────────────────────────── |
| 131 | +const files = walk(srcDir); |
| 132 | +const broken = []; |
| 133 | + |
| 134 | +for (const file of files) { |
| 135 | + const imports = extractDynamicImports(file); |
| 136 | + for (const { specifier, line } of imports) { |
| 137 | + const bad = resolveSpecifier(specifier, file); |
| 138 | + if (bad !== null) { |
| 139 | + const rel = file.replace(resolve(srcDir, '..') + '/', '').replace(/\\/g, '/'); |
| 140 | + broken.push({ file: rel, line, specifier: bad }); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +if (broken.length === 0) { |
| 146 | + console.log(`✓ All dynamic imports in src/ resolve (${files.length} files scanned)`); |
| 147 | + process.exit(0); |
| 148 | +} else { |
| 149 | + console.error(`✗ ${broken.length} broken dynamic import(s) found:\n`); |
| 150 | + for (const { file, line, specifier } of broken) { |
| 151 | + console.error(` ${file}:${line} → ${specifier}`); |
| 152 | + } |
| 153 | + console.error('\nFix the import paths and re-run.'); |
| 154 | + process.exit(1); |
| 155 | +} |
0 commit comments