From 116782820863d39035be7ff642da21eb5b5c2f57 Mon Sep 17 00:00:00 2001 From: Miguel Palhas Date: Wed, 11 Nov 2026 00:36:24 +0000 Subject: [PATCH] chore(web): extend the token gate to TSX Covers React style keys and Tailwind arbitrary values. Co-Authored-By: Claude Opus 5.5 (1M context) --- web/scripts/check-tokens.mjs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/web/scripts/check-tokens.mjs b/web/scripts/check-tokens.mjs index 2a43104..2c21db2 100644 --- a/web/scripts/check-tokens.mjs +++ b/web/scripts/check-tokens.mjs @@ -4,8 +4,13 @@ // // Every `var(--x)` must name a custom property something defines — a token in // `:root`, a component-local one in the CSS, or one the TS sets with -// `setProperty` — so a mistyped or deleted token fails here rather than -// silently falling back to the browser default. +// `setProperty` or a React `style` key — so a mistyped or deleted token fails +// here rather than silently falling back to the browser default. +// +// Tailwind's theme is reset to the tokens alone (style.css), so a utility can +// only produce a token value. The one way around that is an arbitrary value +// — `p-[3px]`, `bg-[#fff]` — and those are allowed only when they name a +// token: `p-[var(--space-2)]` or the `p-(--space-2)` shorthand. // // Allowed colour literals: inside a `:root` block (token definitions) and // inside `@font-face`. Relative colour derived from a token — @@ -27,7 +32,7 @@ function walk(dir, out = []) { if (!SKIP_DIRS.has(name)) { walk(path, out); } - } else if (/\.(css|ts|html)$/.test(name)) { + } else if (/\.(css|tsx?|html)$/.test(name)) { out.push(path); } } @@ -103,6 +108,8 @@ function checkCss(source, rel) { } } +const ARBITRARY = /(?:^|[\s"'`:])!?-?[a-z][\w-]*-\[([^\]\s]+)\]/g; + function checkPlain(source, rel) { // Comments are prose, not styling — `checkCss` already ignores them, and // without the same treatment here an issue reference like `#140` reads as a @@ -115,6 +122,11 @@ function checkPlain(source, rel) { if (COLOR_LITERAL.test(neutral)) { errors.push(`${rel}:${index + 1}: literal colour outside the token block`); } + for (const match of rawLine.matchAll(ARBITRARY)) { + if (!/^var\(--[\w-]+\)$/.test(match[1])) { + errors.push(`${rel}:${index + 1}: arbitrary utility value "[${match[1]}]" — use a token`); + } + } }); } @@ -139,6 +151,9 @@ for (const { source } of sources) { for (const match of source.matchAll(/setProperty\(\s*["'](--[\w-]+)["']/g)) { defined.add(match[1]); } + for (const match of source.matchAll(/["'](--[\w-]+)["']\s*:/g)) { + defined.add(match[1]); + } } for (const { rel, source } of sources) { source.split("\n").forEach((line, index) => {