ShtukbaseStandard
файл пакета→ .shtukbase/packages/keer-style/checks/radii.ts

checks/radii.ts

35 строк, 2.0 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // KS-R02. Radii only from the scale: border-radius takes var(--ds-radius-…) (or the Tailwind/shadcn --radius…
  2. // built on it), 0 or 50 %; Tailwind `rounded-…` takes a step of the scale, an arbitrary `rounded-[…]` only a radius var.
  3. import { allowed, classTokens, declarations, run } from './stylelib.ts';
  4. export const RULE = { id: 'ks-r02', title: 'Corner radius only from the scale' };
  5. const STEPS = new Set(['', 'none', 'xs', 'sm', 'md', 'lg', 'xl', '2xl', '3xl', '4xl', 'full']);
  6. const SIDES = /^(?:[a-z0-9-[\]=&_]+:)*!?rounded(?:-(?:t|r|b|l|s|e|tl|tr|br|bl|ss|se|es|ee))?(?:-(.+))?$/;
  7. /** One part of a radius value is fine: a radius var (or calc on it), 0, 50 %, inherit. */
  8. export function radiusOk(value: string): boolean {
  9. const v = value.trim();
  10. if (/var\(--(?:ds-)?radius/.test(v)) return true; // a token, or calc() derived from one
  11. return v.split(/[\s/]+/).filter(Boolean).every((p) => /^(0(px|rem|em)?|50%|100%|inherit|initial|unset|revert)$/.test(p));
  12. }
  13. export function check(f: { file: string; kind: string; text: string; reduced: string }): string[] {
  14. const out: string[] = [];
  15. const hit = (line: number, what: string) => { if (!allowed(f.text, line)) out.push(`${f.file}:${line}: corner radius ${what} is not from the scale — var(--ds-radius-xs|sm|md|lg|xl|round) or rounded-sm|md|lg|xl|full`); };
  16. if (f.kind === 'css' || f.kind === 'html') {
  17. for (const d of declarations(f.reduced)) if (/(^|-)radius$/.test(d.prop) && !d.prop.startsWith('--') && !radiusOk(d.value)) hit(d.line, `${d.prop}: ${d.value.trim()}`);
  18. }
  19. if (f.kind === 'code' || f.kind === 'html') {
  20. for (const { token, line } of classTokens(f.reduced)) {
  21. const m = token.match(SIDES);
  22. if (!m) continue;
  23. const step = m[1] ?? '';
  24. if (step.startsWith('[')) { const inner = step.slice(1, -1).replace(/_/g, ' '); if (!radiusOk(inner)) hit(line, token); }
  25. else if (!STEPS.has(step)) hit(line, token);
  26. }
  27. }
  28. return [...new Set(out)];
  29. }
  30. if (import.meta.main) run(check);