import { readdir, readFile } from 'node:fs/promises'; import { join, relative } from 'node:path'; // Mistakes this client has made, each with what to do instead. const rules = [ { pattern: /crypto\.randomUUID/, why: 'crypto.randomUUID only exists in secure contexts (https or localhost); use uuidv7 from api.ts', }, { pattern: /<[a-z][^>]*\sautofocus\b/, why: 'autofocus does not take inside a flow node or a mounted window; focus with an attachment', }, { pattern: /svelte-flow__node\s*>\s*\.window-slot/, why: 'where a window sits is board.placeOf, on its slot as data-place; do not read it off its ancestors', }, { pattern: /\{@html\b|\.(inner|outer)HTML\s*=|insertAdjacentHTML|document\.write/, except: ['lib/chat/Markdown.svelte'], why: 'transcripts carry text tools fetched; HTML reaches the page only through renderMarkdown (markdown.ts), which sanitizes it', }, ]; // File-level invariants: a component that anchors a popover above or below a // button must clamp it with fit/fitted from popover.ts so it is never cut off // by a dialog, window body, or viewport edge. const fileRules = [ { test: (text) => /(?:top|bottom):\s*calc\(100%\s*\+/.test(text) && !/\b(?:fit|fitted)\b/.test(text), except: ['lib/chat/Chat.svelte'], why: 'anchored popovers (top/bottom: calc(100% + ...)) must use fit or fitted from popover.ts so they stay inside their clipping container and viewport', }, ]; const root = new URL('../src', import.meta.url).pathname; async function* sources(dir) { for (const entry of await readdir(dir, { withFileTypes: true })) { const path = join(dir, entry.name); if (entry.isDirectory()) yield* sources(path); else if ( /\.(svelte|ts|css)$/.test(entry.name) && !entry.name.endsWith('.gen.d.ts') ) yield path; } } for await (const path of sources(root)) { const rel = relative(root, path); const text = await readFile(path, 'utf8'); const lines = text.split('\n'); lines.forEach((line, at) => { for (const { pattern, why, except = [] } of rules) { if (!pattern.test(line) || except.includes(rel)) continue; console.error(`src/${rel}:${at + 1}: ${why}`); process.exitCode = 1; } }); for (const { test, why, except = [] } of fileRules) { if (except.includes(rel) || !test(text)) continue; console.error(`src/${rel}: ${why}`); process.exitCode = 1; } }