|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * Check for Error Class Misuse Patterns |
| 4 | + * |
| 5 | + * Scans source files for common anti-patterns in error class usage: |
| 6 | + * |
| 7 | + * 1. `new ContextError(resource, command)` where command contains `\n` |
| 8 | + * → Should use ResolutionError for resolution failures |
| 9 | + * |
| 10 | + * 2. `new CliError(... "Try:" ...)` — ad-hoc "Try:" strings |
| 11 | + * → Should use ResolutionError with structured hint/suggestions |
| 12 | + * |
| 13 | + * Usage: |
| 14 | + * bun run script/check-error-patterns.ts |
| 15 | + * |
| 16 | + * Exit codes: |
| 17 | + * 0 - No anti-patterns found |
| 18 | + * 1 - Anti-patterns detected |
| 19 | + */ |
| 20 | + |
| 21 | +export {}; |
| 22 | + |
| 23 | +type Violation = { file: string; line: number; message: string }; |
| 24 | + |
| 25 | +const CONTEXT_ERROR_RE = /new ContextError\(/g; |
| 26 | +const TRY_PATTERN_RE = /["'`]Try:/; |
| 27 | + |
| 28 | +const glob = new Bun.Glob("src/**/*.ts"); |
| 29 | +const violations: Violation[] = []; |
| 30 | + |
| 31 | +/** Characters that open a nesting level in JavaScript source. */ |
| 32 | +function isOpener(ch: string): boolean { |
| 33 | + return ch === "(" || ch === "[" || ch === "{"; |
| 34 | +} |
| 35 | + |
| 36 | +/** Characters that close a nesting level in JavaScript source. */ |
| 37 | +function isCloser(ch: string): boolean { |
| 38 | + return ch === ")" || ch === "]" || ch === "}"; |
| 39 | +} |
| 40 | + |
| 41 | +/** Characters that start a string literal in JavaScript source. */ |
| 42 | +function isQuote(ch: string): boolean { |
| 43 | + return ch === '"' || ch === "'" || ch === "`"; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Skip past a `${...}` expression inside a template literal. |
| 48 | + * @param content - Full source text |
| 49 | + * @param start - Index right after the `{` in `${` |
| 50 | + * @returns Index right after the closing `}` |
| 51 | + */ |
| 52 | +function skipTemplateExpression(content: string, start: number): number { |
| 53 | + let braceDepth = 1; |
| 54 | + let i = start; |
| 55 | + while (i < content.length && braceDepth > 0) { |
| 56 | + const ec = content[i]; |
| 57 | + if (ec === "\\") { |
| 58 | + i += 2; |
| 59 | + } else if (ec === "`") { |
| 60 | + i = skipTemplateLiteral(content, i + 1); |
| 61 | + } else if (ec === "{") { |
| 62 | + braceDepth += 1; |
| 63 | + i += 1; |
| 64 | + } else if (ec === "}") { |
| 65 | + braceDepth -= 1; |
| 66 | + i += 1; |
| 67 | + } else { |
| 68 | + i += 1; |
| 69 | + } |
| 70 | + } |
| 71 | + return i; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Skip past a template literal, handling nested `${...}` expressions. |
| 76 | + * @param content - Full source text |
| 77 | + * @param start - Index right after the opening backtick |
| 78 | + * @returns Index right after the closing backtick |
| 79 | + */ |
| 80 | +function skipTemplateLiteral(content: string, start: number): number { |
| 81 | + let i = start; |
| 82 | + while (i < content.length) { |
| 83 | + const ch = content[i]; |
| 84 | + if (ch === "\\") { |
| 85 | + i += 2; |
| 86 | + } else if (ch === "`") { |
| 87 | + return i + 1; |
| 88 | + } else if (ch === "$" && content[i + 1] === "{") { |
| 89 | + i = skipTemplateExpression(content, i + 2); |
| 90 | + } else { |
| 91 | + i += 1; |
| 92 | + } |
| 93 | + } |
| 94 | + return i; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Advance past a string literal (single-quoted, double-quoted, or template). |
| 99 | + * @param content - Full source text |
| 100 | + * @param start - Index of the opening quote character |
| 101 | + * @returns Index right after the closing quote |
| 102 | + */ |
| 103 | +function skipString(content: string, start: number): number { |
| 104 | + const quote = content[start]; |
| 105 | + if (quote === "`") { |
| 106 | + return skipTemplateLiteral(content, start + 1); |
| 107 | + } |
| 108 | + let i = start + 1; |
| 109 | + while (i < content.length) { |
| 110 | + const ch = content[i]; |
| 111 | + if (ch === "\\") { |
| 112 | + i += 2; |
| 113 | + } else if (ch === quote) { |
| 114 | + return i + 1; |
| 115 | + } else { |
| 116 | + i += 1; |
| 117 | + } |
| 118 | + } |
| 119 | + return i; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Advance one token in JS source, skipping strings as atomic units. |
| 124 | + * @returns The next index and the character at position `i` (or the string span's first char). |
| 125 | + */ |
| 126 | +function advanceToken( |
| 127 | + content: string, |
| 128 | + i: number |
| 129 | +): { next: number; ch: string } { |
| 130 | + const ch = content[i] ?? ""; |
| 131 | + if (isQuote(ch)) { |
| 132 | + return { next: skipString(content, i), ch }; |
| 133 | + } |
| 134 | + return { next: i + 1, ch }; |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Walk from `startIdx` (just inside the opening `(`) to find the matching `)`, |
| 139 | + * tracking commas at depth 1. |
| 140 | + * @returns The index of the first comma (between arg1 and arg2) and the closing paren index. |
| 141 | + */ |
| 142 | +function findCallBounds( |
| 143 | + content: string, |
| 144 | + startIdx: number |
| 145 | +): { commaIdx: number; closingIdx: number } | null { |
| 146 | + let depth = 1; |
| 147 | + let commaCount = 0; |
| 148 | + let commaIdx = -1; |
| 149 | + let i = startIdx; |
| 150 | + |
| 151 | + while (i < content.length && depth > 0) { |
| 152 | + const { next, ch } = advanceToken(content, i); |
| 153 | + if (isOpener(ch)) { |
| 154 | + depth += 1; |
| 155 | + } else if (isCloser(ch)) { |
| 156 | + depth -= 1; |
| 157 | + } else if (ch === "," && depth === 1) { |
| 158 | + commaCount += 1; |
| 159 | + if (commaCount === 1) { |
| 160 | + commaIdx = i; |
| 161 | + } |
| 162 | + } |
| 163 | + i = next; |
| 164 | + } |
| 165 | + |
| 166 | + if (commaIdx === -1) { |
| 167 | + return null; |
| 168 | + } |
| 169 | + return { commaIdx, closingIdx: i - 1 }; |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Extract the second argument of a `new ContextError(...)` call from source text. |
| 174 | + * Properly handles template literals so backticks don't break depth tracking. |
| 175 | + * @returns The raw source text of the second argument, or null if not found. |
| 176 | + */ |
| 177 | +function extractSecondArg(content: string, startIdx: number): string | null { |
| 178 | + const bounds = findCallBounds(content, startIdx); |
| 179 | + if (!bounds) { |
| 180 | + return null; |
| 181 | + } |
| 182 | + |
| 183 | + const { commaIdx, closingIdx } = bounds; |
| 184 | + |
| 185 | + // Find end of second arg: next comma at depth 1 or closing paren |
| 186 | + let endIdx = closingIdx; |
| 187 | + let d = 1; |
| 188 | + for (let j = commaIdx + 1; j < closingIdx; j += 1) { |
| 189 | + const { next, ch } = advanceToken(content, j); |
| 190 | + if (isOpener(ch)) { |
| 191 | + d += 1; |
| 192 | + } else if (isCloser(ch)) { |
| 193 | + d -= 1; |
| 194 | + } else if (ch === "," && d === 1) { |
| 195 | + endIdx = j; |
| 196 | + break; |
| 197 | + } |
| 198 | + // advanceToken may skip multiple chars (strings), adjust loop var |
| 199 | + j = next - 1; // -1 because for-loop increments |
| 200 | + } |
| 201 | + |
| 202 | + return content.slice(commaIdx + 1, endIdx).trim(); |
| 203 | +} |
| 204 | + |
| 205 | +/** |
| 206 | + * Detect `new ContextError(` where the second argument contains `\n`. |
| 207 | + * This catches resolution-failure prose stuffed into the command parameter. |
| 208 | + */ |
| 209 | +function checkContextErrorNewlines(content: string, filePath: string): void { |
| 210 | + let match = CONTEXT_ERROR_RE.exec(content); |
| 211 | + while (match !== null) { |
| 212 | + const startIdx = match.index + match[0].length; |
| 213 | + const secondArg = extractSecondArg(content, startIdx); |
| 214 | + |
| 215 | + if (secondArg?.includes("\\n")) { |
| 216 | + const line = content.slice(0, match.index).split("\n").length; |
| 217 | + violations.push({ |
| 218 | + file: filePath, |
| 219 | + line, |
| 220 | + message: |
| 221 | + "ContextError command contains '\\n'. Use ResolutionError for multi-line resolution failures.", |
| 222 | + }); |
| 223 | + } |
| 224 | + match = CONTEXT_ERROR_RE.exec(content); |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +/** |
| 229 | + * Detect `new CliError(... "Try:" ...)` — ad-hoc "Try:" strings that bypass |
| 230 | + * the structured ResolutionError pattern. |
| 231 | + */ |
| 232 | +function checkAdHocTryPatterns(content: string, filePath: string): void { |
| 233 | + const lines = content.split("\n"); |
| 234 | + let inCliError = false; |
| 235 | + |
| 236 | + for (let i = 0; i < lines.length; i += 1) { |
| 237 | + const line = lines[i] ?? ""; |
| 238 | + if (line.includes("new CliError(")) { |
| 239 | + inCliError = true; |
| 240 | + } |
| 241 | + if (inCliError && TRY_PATTERN_RE.test(line)) { |
| 242 | + violations.push({ |
| 243 | + file: filePath, |
| 244 | + line: i + 1, |
| 245 | + message: |
| 246 | + 'CliError contains "Try:" — use ResolutionError with structured hint/suggestions instead.', |
| 247 | + }); |
| 248 | + inCliError = false; |
| 249 | + } |
| 250 | + // Reset after a reasonable window (closing paren) |
| 251 | + if (inCliError && line.includes(");")) { |
| 252 | + inCliError = false; |
| 253 | + } |
| 254 | + } |
| 255 | +} |
| 256 | + |
| 257 | +for await (const filePath of glob.scan(".")) { |
| 258 | + const content = await Bun.file(filePath).text(); |
| 259 | + checkContextErrorNewlines(content, filePath); |
| 260 | + checkAdHocTryPatterns(content, filePath); |
| 261 | +} |
| 262 | + |
| 263 | +if (violations.length === 0) { |
| 264 | + console.log("✓ No error class anti-patterns found"); |
| 265 | + process.exit(0); |
| 266 | +} |
| 267 | + |
| 268 | +console.error(`✗ Found ${violations.length} error class anti-pattern(s):\n`); |
| 269 | +for (const v of violations) { |
| 270 | + console.error(` ${v.file}:${v.line}`); |
| 271 | + console.error(` ${v.message}\n`); |
| 272 | +} |
| 273 | +console.error( |
| 274 | + "Fix: Use ResolutionError for resolution failures, ValidationError for input errors." |
| 275 | +); |
| 276 | +console.error( |
| 277 | + "See ContextError JSDoc in src/lib/errors.ts for usage guidance." |
| 278 | +); |
| 279 | + |
| 280 | +process.exit(1); |
0 commit comments