Something went wrong. Try again.
Tools for building Offline-first Web Apps
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109#!/usr/bin/env -S deno run --allow-read/** * @module Component class-name check * * Enforces the light-DOM contract from spec §1.4: a component's **rendered * shape** is its styling API, expressed in semantic elements, sub-elements and * attributes — never invented class names. * * The one thing a component may render is a class the **framework itself * defines**, in `_patterns.css`. That is the real invariant: the failure this * exists to prevent was components emitting `bm-btn-danger` — a name defined * in exactly one app's stylesheet — not classes as such. A pattern class is * shipped in the same bundle as the component, so it can never be missing. * The allowlist is read from `_patterns.css` rather than hardcoded, so it * cannot drift from what is actually defined. * * The rule exists because the class-based version failed in a specific, * traceable way. `@inro/ui` shipped components whose markup carried * `bm-dialog`, `bm-btn-danger` and `bm-form-actions` — names defined in * exactly one place in the world, `maps/www/static/styles/theme.css`. Every * other app rendered those surfaces unstyled. Nothing caught it, because * nothing was looking. * * Shadow-DOM components are exempt: their classes are private to a root * nothing outside can select into, so they are implementation, not API. * * Run via `deno task test:classes`. *//** * Both directories render light DOM, so both are bound by the contract. A * scene is a component that happens to be page-sized — the split is about * scale, not about rules. */const COMPONENT_DIRS = ['packages/ui/components', 'packages/ui/scenes']const PATTERNS = 'packages/ui/_patterns.css'
/** Class names the framework defines, and which components may therefore use. */async function frameworkClasses(): Promise<Set<string>> { const css = (await Deno.readTextFile(PATTERNS)) .replace(/\/\*[\s\S]*?\*\//g, '') return new Set( [...css.matchAll(/\.([a-zA-Z][\w-]*)/g)].map((m) => m[1]), )}
/** `class="…"` in a template literal — the thing this check forbids. */const CLASS_ATTR = /class\s*=\s*"[^"]*"/g
/** A component that renders into a shadow root styles it with `static styles`. */const SHADOW_DOM = /static\s+(?:override\s+)?styles\b/
interface Finding { file: string line: number text: string}
const allowed = await frameworkClasses()const findings: Finding[] = []let checked = 0let exempt = 0
const files: string[] = []for (const dir of COMPONENT_DIRS) { for await (const entry of Deno.readDir(dir)) { if (!entry.isFile || !entry.name.endsWith('.ts')) continue if (entry.name.endsWith('.test.ts')) continue files.push(`${dir}/${entry.name}`) }}
for (const path of files.sort()) { const source = await Deno.readTextFile(path)
if (SHADOW_DOM.test(source)) { exempt++ continue } checked++
source.split('\n').forEach((line, i) => { for (const match of line.matchAll(CLASS_ATTR)) { const names = match[0].slice(7, -1).split(/\s+/).filter(Boolean) const invented = names.filter((n) => !allowed.has(n)) if (invented.length > 0) { findings.push({ file: path, line: i + 1, text: invented.join(' ') }) } } })}
if (findings.length > 0) { console.error( `${findings.length} class name(s) in light-DOM components that the ` + `framework does not define.\n` + `Use element, sub-element or attribute selectors instead — or, if this ` + `really is shared vocabulary, define it in _patterns.css. See §1.4.5.\n`, ) for (const f of findings) { console.error(` ${f.file}:${f.line} ${f.text}`) } Deno.exit(1)}
console.log( `No invented class names in ${checked} light-DOM component(s) ` + `(${exempt} shadow-DOM exempt; ${allowed.size} framework classes allowed).`,)