ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/design-scales/checks/type.ts

checks/type.ts

52 líneas, 2.7 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // SCL-R02. Font sizes come from a short type scale: every font size written as a length is one of the sizes of
  2. // `typeScale` on /design/scales.md (default: the Tailwind steps and the Material 3 type scale), and the project
  3. // uses no more than `maxFontSizes` distinct sizes (default 10).
  4. // Looked at: CSS font-size and size custom properties (--text-*, --font-size-*, --fs-*, --ds-font-body-size), Tailwind classes
  5. // (text-sm, text-[13px]) and inline styles (fontSize: 13). var(), calc(), clamp() and em are skipped.
  6. import { report } from '../../design-base/lib/files.ts';
  7. import { TAILWIND_TEXT, classes, config, cssDecls, fmt, styleValues, utility } from './scaleslib.ts';
  8. import { px } from '../../design-base/lib/css.ts';
  9. export const RULE = { id: 'scl-r02', title: 'Font sizes from a short scale' };
  10. const TOKEN = /^--(text|font-size|fs|type)-[\w-]+$|^--[\w-]*(font|text|type)[\w-]*-size$/;
  11. const NOT_SIZE = /(line-height|leading|tracking|letter-spacing|weight|family|shadow|color)/;
  12. export function sizes(root = process.cwd()): { at: string; what: string; px: number }[] {
  13. const out: { at: string; what: string; px: number }[] = [];
  14. const lit = (v: string) => (/^-?\d*\.?\d+(px|rem)$/.test(v.trim()) ? px(v) : null);
  15. for (const d of cssDecls(root)) {
  16. if (d.prop !== 'font-size' && !(TOKEN.test(d.prop) && !NOT_SIZE.test(d.prop))) continue;
  17. const p = lit(d.value);
  18. if (p !== null) out.push({ at: d.at, what: `${d.prop}: ${d.value}`, px: p });
  19. }
  20. for (const k of classes(root)) {
  21. const u = utility(k.cls).replace(/\/.*$/, '');
  22. const named = /^text-(xs|sm|base|lg|xl|[2-9]xl)$/.exec(u);
  23. if (named) { out.push({ at: k.at, what: k.cls, px: TAILWIND_TEXT[named[1]] }); continue; }
  24. const arb = /^text-\[(?:length:)?([^\]]+)\]$/.exec(u);
  25. const p = arb ? lit(arb[1]) : null;
  26. if (p !== null) out.push({ at: k.at, what: k.cls, px: p });
  27. }
  28. for (const s of styleValues(/fontSize/, root)) {
  29. const p = lit(s.value);
  30. if (p !== null) out.push({ at: s.at, what: `${s.prop}: ${s.value}`, px: p });
  31. }
  32. return out;
  33. }
  34. export function check(root = process.cwd()): string[] {
  35. const c = config(root);
  36. const out: string[] = [];
  37. const all = sizes(root);
  38. for (const s of all) {
  39. if (!c.typeScale.some((t) => Math.abs(t - s.px) < 0.01))
  40. out.push(`${s.at}: font size ${s.what} = ${fmt(s.px)} — not from the scale (${c.typeScale.join(', ')})`);
  41. }
  42. const distinct = [...new Set(all.map((s) => Math.round(s.px * 100) / 100))].sort((a, b) => a - b);
  43. if (distinct.length > c.maxFontSizes)
  44. out.push(`${'design/scales.md'}: the project has ${distinct.length} different font sizes (${distinct.join(', ')}) — more than ${c.maxFontSizes}; reduce them to the scale`);
  45. return out;
  46. }
  47. if (import.meta.main) report(check());