package file→ .shtukbase/packages/keer-style/checks/shadows.ts
checks/shadows.ts
45 lines, 2.1 KB.
- // KS-R03. Shadows only from tokens: box-shadow is `none` or a list of var(--ds-shadow-…)-like vars; the one
- // composed shape allowed is a ring `[inset] 0 0 0 <width> <colour var>` (focus and hairlines) with width 0–2px or a var.
- // Tailwind: `shadow-<name>` from the theme; an arbitrary `shadow-[…]` follows the same shape.
- import { allowed, classTokens, declarations, run } from './stylelib.ts';
- export const RULE = { id: 'ks-r03', title: 'Shadow only from tokens' };
- /** Splits a value by top-level commas. */
- function parts(v: string): string[] {
- const out: string[] = [];
- let depth = 0, cur = '';
- for (const c of v) {
- if (c === '(') depth++;
- if (c === ')') depth--;
- if (c === ',' && depth === 0) { out.push(cur); cur = ''; } else cur += c;
- }
- return [...out, cur].map((s) => s.trim()).filter(Boolean);
- }
- export function shadowOk(value: string): boolean {
- const v = value.trim();
- if (/^(none|inherit|initial|unset|revert)$/.test(v)) return true;
- return parts(v).every((p) => {
- if (/^var\(--[\w-]+(?:,\s*[^)]*)?\)$/.test(p)) return true;
- const ring = p.match(/^(?:inset\s+)?0(?:px)?\s+0(?:px)?\s+0(?:px)?\s+(\S+)\s+(var\(--[\w-]+\)|transparent|currentColor)$/i);
- return !!ring && (/^var\(--[\w-]+\)$/.test(ring[1]) || /^(0|0?\.\d+px|[12](\.\d+)?px)$/.test(ring[1]));
- });
- }
- export function check(f: { file: string; kind: string; text: string; reduced: string }): string[] {
- const out: string[] = [];
- const hit = (line: number, what: string) => { if (!allowed(f.text, line)) out.push(`${f.file}:${line}: shadow ${what} is not from tokens — var(--ds-shadow-…) or a ring 0 0 0 1px var(--…)`); };
- if (f.kind === 'css' || f.kind === 'html') {
- for (const d of declarations(f.reduced)) if (d.prop === 'box-shadow' && !shadowOk(d.value)) hit(d.line, `${d.prop}: ${d.value.trim().replace(/\s+/g, ' ')}`);
- }
- if (f.kind === 'code' || f.kind === 'html') {
- for (const { token, line } of classTokens(f.reduced)) {
- const m = token.match(/^(?:[a-z0-9-[\]=&_]+:)*!?shadow-\[(.+)\]$/);
- if (m && !shadowOk(m[1].replace(/_/g, ' '))) hit(line, token);
- }
- }
- return [...new Set(out)];
- }
- if (import.meta.main) run(check);