archivo del paquete→ .shtukbase/packages/keer-style/checks/colors.ts
checks/colors.ts
38 líneas, 2.5 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // KS-R01. Colours only from tokens: no raw hex, rgb()/hsl()/oklch()… with numbers, named colours in colour
- // properties or Tailwind palette classes outside a file of values (`@tokens`).
- import { allowed, classTokens, declarations, run } from './stylelib.ts';
- export const RULE = { id: 'ks-r01', title: 'Color only from tokens' };
- const HEX = /(?<![\w&/-])#(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{6}|[0-9a-fA-F]{3,4})\b/g;
- const FN = /\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\(([^()]*(?:\([^()]*\)[^()]*)*)\)/g;
- const NAMED = /\b(white|black|red|green|blue|gray|grey|orange|yellow|purple|pink|brown|silver|navy|teal|maroon|olive|lime|aqua|fuchsia)\b/i;
- const COLOUR_PROPS = /^(color|background|background-color|border(-[a-z]+)*-color|border(-(top|right|bottom|left))?|outline(-color)?|fill|stroke|caret-color|accent-color|text-decoration(-color)?|column-rule(-color)?|box-shadow|text-shadow)$/;
- const PALETTE = /^(?:[a-z0-9-]+:)*!?-?(?:bg|text|border(?:-[trblxyse])?|ring(?:-offset)?|fill|stroke|from|via|to|outline|decoration|shadow|divide|accent|caret|placeholder)-(?:(?:slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-\d{2,3}|black|white)(?:\/\d+)?$/;
- /** Raw colour values in a piece of text: hex and colour functions without a var(). */
- export function rawColours(s: string): string[] {
- const out: string[] = [];
- for (const m of s.matchAll(HEX)) out.push(m[0]);
- for (const m of s.matchAll(FN)) if (!m[1].includes('var(')) out.push(m[0]);
- return out;
- }
- 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}: raw color ${what} — use a token var(--ds-…)`); };
- if (f.kind === 'css' || f.kind === 'html') {
- for (const d of declarations(f.reduced)) {
- if (d.prop.startsWith('--')) { for (const c of rawColours(d.value)) hit(d.line, c); continue; }
- for (const c of rawColours(d.value)) hit(d.line, c);
- if (COLOUR_PROPS.test(d.prop)) { const n = d.value.match(NAMED); if (n && !d.value.includes('var(')) hit(d.line, n[0]); }
- }
- }
- if (f.kind === 'code' || f.kind === 'html') {
- for (const { token, line } of classTokens(f.reduced)) if (PALETTE.test(token)) hit(line, token);
- }
- if (f.kind === 'code') f.reduced.split('\n').forEach((l, i) => { for (const c of rawColours(l)) hit(i + 1, c); });
- return [...new Set(out)];
- }
- if (import.meta.main) run(check);