ShtukbaseStandard
файл пакета→ .shtukbase/packages/design-scales/checks/shape.ts

checks/shape.ts

72 строки, 4.3 КБ.

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

  1. // SCL-R03. Radii and shadows come from a short set.
  2. // Radii: every radius written as a length is in `radii` of /design/scales.md (default 0 2 4 6 8 10 12 16 20 24 32)
  3. // or is a pill (≥ 999 px) or a percentage; the project uses no more than `maxRadii` distinct radii (default 6).
  4. // Shadows: a box-shadow in a rule is a theme shadow (var(--shadow-…)) or none; a ring (0 0 0 Npx …: offsets and blur
  5. // zero) is not a shadow and is fine; the project defines no more than `maxShadows` shadow custom properties
  6. // (--shadow*, --ds-shadow-soft; only those holding a whole shadow, not its color or a ring; default 6). Tailwind: rounded-[13px], shadow-[0_2px_3px_…] count the same way.
  7. import { report } from '../../design-base/lib/files.ts';
  8. import { px, splitTop } from '../../design-base/lib/css.ts';
  9. import { classes, config, cssDecls, fmt, styleValues, utility } from './scaleslib.ts';
  10. export const RULE = { id: 'scl-r03', title: 'Radii and shadows from a short set' };
  11. const RADIUS = /^border(-(top|bottom|start|end)-(left|right|start|end))?-radius$|^--([\w-]*-)?radius(-[\w-]*)?$/;
  12. const TW_RADIUS = /^rounded(-(t|r|b|l|s|e|tl|tr|br|bl|ss|se|es|ee))?-\[([^\]]+)\]$/;
  13. /** A shadow value that is not taken from the theme: has a length or a color of its own, and is not a ring. */
  14. export function adHocShadow(value: string): boolean {
  15. const v = value.trim();
  16. if (/^(none|inherit|initial|unset|revert)$/.test(v)) return false;
  17. return splitTop(v).some((layer) => {
  18. const l = layer.trim();
  19. if (/^var\(--[\w-]+\)$/.test(l)) return false;
  20. const lens = l.replace(/var\([^)]*\)|\w+\([^)]*\)/g, ' ').split(/\s+/).filter((t) => /^-?\d*\.?\d+(px|rem|em)?$/.test(t)).map((t) => px(t) ?? 0);
  21. if (lens.length >= 4 && lens[0] === 0 && lens[1] === 0 && lens[2] === 0) return false; // a ring
  22. return true;
  23. });
  24. }
  25. /** A custom property that holds a whole shadow (offsets and blur), not a ring or a color of a shadow. */
  26. export function isShadow(value: string): boolean {
  27. return splitTop(value).some((layer) => {
  28. const lens = layer.replace(/var\([^)]*\)|\w+\([^)]*\)/g, ' ').split(/\s+/).filter((t) => /^-?\d*\.?\d+(px|rem|em)?$/.test(t)).map((t) => px(t) ?? 0);
  29. return lens.length >= 2 && !(lens.length >= 4 && lens[0] === 0 && lens[1] === 0 && lens[2] === 0);
  30. });
  31. }
  32. export function check(root = process.cwd()): string[] {
  33. const c = config(root);
  34. const out: string[] = [];
  35. const radii: number[] = [];
  36. const radius = (at: string, what: string, value: string) => {
  37. for (const t of value.trim().split(/[\s/]+/)) {
  38. if (t.endsWith('%')) continue;
  39. const p = px(t);
  40. if (p === null || p >= 999) continue;
  41. radii.push(p);
  42. if (!c.radii.some((r) => Math.abs(r - p) < 0.01)) out.push(`${at}: radius ${what} = ${fmt(p)} — not from the set (${c.radii.join(', ')})`);
  43. }
  44. };
  45. const shadows = new Set<string>();
  46. for (const d of cssDecls(root)) {
  47. if (RADIUS.test(d.prop)) radius(d.at, `${d.prop}: ${d.value}`, d.value);
  48. else if (d.prop === 'box-shadow' && adHocShadow(d.value))
  49. out.push(`${d.at}: shadow box-shadow: ${d.value.slice(0, 60)} — own, not from the theme; move it to --shadow-… and use var()`);
  50. else if (/^--([\w-]*-)?shadow(-[\w-]*)?$/.test(d.prop) && isShadow(d.value)) shadows.add(`${d.prop}`);
  51. }
  52. for (const k of classes(root)) {
  53. const u = utility(k.cls);
  54. const r = TW_RADIUS.exec(u);
  55. if (r) radius(k.at, k.cls, r[3].replace(/_/g, ' '));
  56. const s = /^shadow-\[([^\]]+)\]$/.exec(u);
  57. if (s && /\d/.test(s[1]) && adHocShadow(s[1].replace(/_/g, ' '))) out.push(`${k.at}: shadow ${k.cls} — own, not from the theme; use shadow-… from the theme`);
  58. }
  59. for (const s of styleValues(/borderRadius/, root)) radius(s.at, `${s.prop}: ${s.value}`, s.value);
  60. for (const s of styleValues(/boxShadow/, root)) if (adHocShadow(s.value)) out.push(`${s.at}: shadow boxShadow: ${s.value.slice(0, 60)} — own, not from the theme`);
  61. const distinct = [...new Set(radii.map((r) => Math.round(r * 100) / 100))].sort((a, b) => a - b);
  62. if (distinct.length > c.maxRadii) out.push(`design/scales.md: the project has ${distinct.length} different radii (${distinct.join(', ')}) — more than ${c.maxRadii}`);
  63. if (shadows.size > c.maxShadows) out.push(`design/scales.md: the theme has ${shadows.size} shadows (${[...shadows].join(', ')}) — more than ${c.maxShadows}`);
  64. return out;
  65. }
  66. if (import.meta.main) report(check());