stocks / scripts /check-inline-styles.mjs
user
python version
ee446e8
Raw History Blame Contribute Delete
5.24 kB
// scripts/check-inline-styles.mjs
// Guardrail 13.4: no inline styles, and no `<style>` outside an allow-listed
// shadow root.
//
// Why this is a script and not a biome rule: biome's `noInlineStyles` reads the
// AST (a JSX `style` prop, a Vue/Svelte style binding), and this codebase
// builds its markup as template literals — `'<span style="width:68px">'` is a
// string to biome, not an element. It is still enabled at "warn" in biome.json
// for any future component-style code; this script is what actually sees the
// 55 sites that exist today.
//
// The line the rule draws is not "inline styles are bad" but "an inline style
// that could have been a class is a bug". An inline value carrying a template
// interpolation (`width:${share}%`) or only custom properties (`--row-cols: 4`)
// is the runtime-data channel and is allowed: it cannot be expressed in a
// stylesheet. `width:68px`, `cursor:pointer` and `display:none` can, and those
// are what this reports — they are how a page ends up with a 4th hover state
// nobody can find in common.css.
//
// node scripts/check-inline-styles.mjs # inventory, exits 0
// node scripts/check-inline-styles.mjs --strict # exit 1 while any site remains
//
// Baseline when written (2026-10): 60 sites — 55 `style="..."` attributes in 15
// files (11 carry runtime data, 44 are static), 3 `cssText` assignments
// (chart-charts, scan-results, chart-card-element — STY-5.9a/b) and 2 `<style>`
// blocks, both in the allow-listed shadow root. 47 are reported, across 13
// files. The `--strict` flip is the last step of those waves, not this one.
import { readdirSync, readFileSync } from "node:fs";
import { join, relative } from "node:path";
import { fileURLToPath } from "node:url";
const REPO = fileURLToPath(new URL("..", import.meta.url));
const STATIC = join(REPO, "static");
// `<stock-chart-card>` renders in a shadow root, so its CSS cannot come from a
// stylesheet. STY-3.4a extracts both blocks to static/css/chart-card.css; drop
// this entry in the same commit.
const SHADOW_ROOT_ALLOWLIST = ["js/features/shared/chart-card-element.ts"];
const RE_INLINE_STYLE = /style="([^"<>]*)"/g;
const RE_CSS_TEXT = /\.cssText\b/g;
const RE_STYLE_TAG = /<style>/g;
const RE_INTERPOLATION = /\$\{/;
/** Is the inline value runtime data rather than a style that could be a class? */
function isRuntimeData(value) {
const declarations = value
.split(";")
.map((part) => part.trim())
.filter(Boolean);
if (declarations.length === 0) return false;
if (RE_INTERPOLATION.test(value)) return true;
// Only custom properties: the sanctioned channel for a computed value
// (--row-cols, --group-tint) that CSS still owns the rule for.
return declarations.every((part) => part.startsWith("--"));
}
function sourceFiles() {
const out = [];
const walk = (dir) => {
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const path = join(dir, entry.name);
if (entry.isDirectory()) walk(path);
else if (/\.(ts|html)$/.test(entry.name)) out.push(path);
}
};
walk(STATIC);
return out.sort();
}
const rows = [];
for (const path of sourceFiles()) {
const src = readFileSync(path, "utf8");
const name = relative(STATIC, path);
const lineAt = (index) => src.slice(0, index).split("\n").length;
for (const m of src.matchAll(RE_INLINE_STYLE)) {
rows.push({
kind: isRuntimeData(m[1]) ? "allowed (runtime data)" : "inline style",
file: name,
line: lineAt(m.index),
detail: m[1].trim().slice(0, 60),
});
}
for (const m of src.matchAll(RE_CSS_TEXT)) {
rows.push({ kind: "cssText assignment", file: name, line: lineAt(m.index), detail: "style written as a string" });
}
for (const m of src.matchAll(RE_STYLE_TAG)) {
const allowed = SHADOW_ROOT_ALLOWLIST.includes(name);
rows.push({
kind: allowed ? "allowed (<style> in shadow root)" : "<style> tag",
file: name,
line: lineAt(m.index),
detail: "STY-3.4a moves this to static/css/",
});
}
}
const reported = rows.filter((r) => !r.kind.startsWith("allowed"));
const byKind = new Map();
const byFile = new Map();
for (const r of rows) {
byKind.set(r.kind, (byKind.get(r.kind) ?? 0) + 1);
if (!r.kind.startsWith("allowed")) byFile.set(r.file, (byFile.get(r.file) ?? 0) + 1);
}
console.log(`scanned ${sourceFiles().length} files under static/`);
for (const [kind, count] of [...byKind.entries()].sort()) console.log(` ${String(count).padStart(3)} ${kind}`);
console.log(`reported: ${reported.length} in ${byFile.size} files`);
for (const [file, count] of [...byFile.entries()].sort((a, b) => b[1] - a[1])) console.log(` ${String(count).padStart(3)} ${file}`);
if (process.argv.includes("--verbose")) {
for (const r of reported) console.log(` ${r.file}:${r.line} ${r.kind}: ${r.detail}`);
}
const strayStyleTags = reported.filter((r) => r.kind === "<style> tag");
if (strayStyleTags.length > 0) {
for (const r of strayStyleTags) console.error(`<style> outside the allow-listed shadow root: ${r.file}:${r.line}`);
process.exit(1);
}
if (reported.length > 0 && process.argv.includes("--strict")) {
console.error(`${reported.length} inline-style site(s) remain; --strict fails until STY-5.9a/b and the class migrations land`);
process.exit(1);
}