| 1 | // Enforces the mechanical rules of docs/code-standards.md. Each error names the rule to read. |
| import { existsSync, readFileSync } from "node:fs"; |
| 3 |
| 4 | process.chdir(`${import.meta.dir}/..`); |
| 5 |
| 6 | type Check = { |
| 7 | rule: string; |
| 8 | applies: (path: string) => boolean; |
| 9 | find: (source: string) => number[]; |
| 10 | }; |
| 11 |
| 12 | const linesMatching = (source: string, pattern: RegExp) => |
| 13 | source.split("\n").flatMap((line, i) => (pattern.test(line) ? [i + 1] : [])); |
| 14 |
| 15 | const checks: Check[] = [ |
| 16 | { |
| 17 | rule: "3. Strings are never concatenated for layout", |
| 18 | applies: () => true, |
| 19 | find: (source) => linesMatching(source, /["'`]\s*\+\s*$/), |
| 20 | }, |
| 21 | { |
| 22 | rule: "5. No wrapped comments or strings", |
| 23 | applies: () => true, |
| 24 | find: (source) => |
| 25 | source.split("\n").flatMap((line, i, lines) => { |
| 26 | const comment = /^\s*\/\/(?!\s*(biome|eslint|oxlint|@ts-))/; |
| 27 | const wrappedLineComment = |
| 28 | i > 0 && comment.test(line) && comment.test(lines[i - 1]); |
| 29 | const wrappedJsxComment = line.includes("{/*") && !line.includes("*/}"); |
| 30 | return wrappedLineComment || wrappedJsxComment ? [i + 1] : []; |
| 31 | }), |
| 32 | }, |
| 33 | { |
| 34 | rule: "6. `services/` holds services", |
| 35 | applies: (path) => |
| 36 | path.includes("/services/") && !/\.(spec|test)\.ts$/.test(path), |
| 37 | find: (source) => [ |
| 38 | ...(source.includes("@Injectable(") ? [] : [1]), |
| 39 | ...linesMatching(source, /^export (async )?function |^export const /), |
| 40 | ], |
| 41 | }, |
| 42 | { |
| 43 | rule: "6. Error classes live under `lib/`", |
| 44 | applies: (path) => |
| 45 | path.startsWith("apps/api/src/") && |
| 46 | path.endsWith(".errors.ts") && |
| 47 | !path.startsWith("apps/api/src/lib/"), |
| 48 | find: () => [1], |
| 49 | }, |
| 50 | { |
| 51 | rule: "7. Production quality is the default (no `any`)", |
| 52 | applies: () => true, |
| 53 | find: (source) => |
| 54 | linesMatching(source, /^(?!\s*(\/\/|\*)).*(:\s*any\b|\bas any\b|<any>)/), |
| 55 | }, |
| 56 | { |
| 57 | rule: "8. One file holds one component family (a page is not a family)", |
| 58 | applies: (path) => /\/app\/(.*\/)?(page|layout)\.tsx$/.test(path), |
| 59 | find: (source) => { |
| 60 | const components = linesMatching( |
| 61 | source, |
| 62 | /^(export\s+)?(default\s+)?(async\s+)?function [A-Z]/, |
| 63 | ); |
| 64 | return components.length > 1 ? components.slice(1) : []; |
| 65 | }, |
| 66 | }, |
| 67 | { |
| 68 | rule: "10. Every suspended boundary has a loading state", |
| 69 | applies: (path) => path.endsWith(".tsx"), |
| 70 | find: (source) => linesMatching(source, /fallback=\{null\}/), |
| 71 | }, |
| 72 | ]; |
| 73 |
| 74 | const files = Bun.spawnSync(["git", "ls-files", "*.ts", "*.tsx"]) |
| 75 | .stdout.toString() |
| 76 | .trim() |
| 77 | .split("\n") |
| 78 | .filter( |
| 79 | (path) => |
| 80 | !/(^|\/)(node_modules|drizzle|dist)\/|\/lib\/api\/v1\.d\.ts$|next-env\.d\.ts$|^scripts\/check-standards\.ts$/.test( |
| 81 | path, |
| 82 | ), |
| 83 | ); |
| 84 |
| 85 | const failures: string[] = []; |
| 86 | for (const path of files) { |
| 87 | if (!existsSync(path)) continue; |
| 88 | const source = readFileSync(path, "utf8"); |
| 89 | for (const check of checks) { |
| 90 | if (!check.applies(path)) continue; |
| 91 | failures.push( |
| 92 | ...check |
| 93 | .find(source) |
| 94 | .map((line) => `${path}:${line} breaks rule ${check.rule}`), |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 |
| 99 | if (failures.length > 0) { |
| 100 | console.error(`${failures.join("\n")}\n\nSee docs/code-standards.md.`); |
| 101 | process.exit(1); |
| 102 | } |