ShtukbaseStandard
包文件→ .shtukbase/packages/keer-style/checks/shadows.ts

checks/shadows.ts

45 行, 2.1 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // KS-R03. Shadows only from tokens: box-shadow is `none` or a list of var(--ds-shadow-…)-like vars; the one
  2. // composed shape allowed is a ring `[inset] 0 0 0 <width> <colour var>` (focus and hairlines) with width 0–2px or a var.
  3. // Tailwind: `shadow-<name>` from the theme; an arbitrary `shadow-[…]` follows the same shape.
  4. import { allowed, classTokens, declarations, run } from './stylelib.ts';
  5. export const RULE = { id: 'ks-r03', title: 'Shadow only from tokens' };
  6. /** Splits a value by top-level commas. */
  7. function parts(v: string): string[] {
  8. const out: string[] = [];
  9. let depth = 0, cur = '';
  10. for (const c of v) {
  11. if (c === '(') depth++;
  12. if (c === ')') depth--;
  13. if (c === ',' && depth === 0) { out.push(cur); cur = ''; } else cur += c;
  14. }
  15. return [...out, cur].map((s) => s.trim()).filter(Boolean);
  16. }
  17. export function shadowOk(value: string): boolean {
  18. const v = value.trim();
  19. if (/^(none|inherit|initial|unset|revert)$/.test(v)) return true;
  20. return parts(v).every((p) => {
  21. if (/^var\(--[\w-]+(?:,\s*[^)]*)?\)$/.test(p)) return true;
  22. const ring = p.match(/^(?:inset\s+)?0(?:px)?\s+0(?:px)?\s+0(?:px)?\s+(\S+)\s+(var\(--[\w-]+\)|transparent|currentColor)$/i);
  23. return !!ring && (/^var\(--[\w-]+\)$/.test(ring[1]) || /^(0|0?\.\d+px|[12](\.\d+)?px)$/.test(ring[1]));
  24. });
  25. }
  26. export function check(f: { file: string; kind: string; text: string; reduced: string }): string[] {
  27. const out: string[] = [];
  28. 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(--…)`); };
  29. if (f.kind === 'css' || f.kind === 'html') {
  30. 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, ' ')}`);
  31. }
  32. if (f.kind === 'code' || f.kind === 'html') {
  33. for (const { token, line } of classTokens(f.reduced)) {
  34. const m = token.match(/^(?:[a-z0-9-[\]=&_]+:)*!?shadow-\[(.+)\]$/);
  35. if (m && !shadowOk(m[1].replace(/_/g, ' '))) hit(line, token);
  36. }
  37. }
  38. return [...new Set(out)];
  39. }
  40. if (import.meta.main) run(check);