ShtukbaseStandard
package file→ .shtukbase/packages/design-scales/checks/scaleslib.ts

checks/scaleslib.ts

95 lines, 4.5 KB.

  1. // Shared parts of the design-scales checks: settings from /design/scales.md and the places where sizes are written
  2. // (CSS declarations, Tailwind classes with arbitrary values, inline style objects in TSX/JSX).
  3. import { parseCss, px } from '../../design-base/lib/css.ts';
  4. import { blankComments, strings, tokens } from '../../design-base/lib/markup.ts';
  5. import { lineAt, projectFiles, read, settings } from '../../design-base/lib/files.ts';
  6. export const CONFIG = 'design/scales.md';
  7. const nums = (v: unknown, d: number[]) => (Array.isArray(v) && v.length ? v.map(Number).filter((n) => !Number.isNaN(n)) : d);
  8. const n = (v: unknown, d: number) => (Number(v) > 0 ? Number(v) : d);
  9. // Tailwind's named font sizes (px) and the Material 3 type scale: the default list of allowed sizes.
  10. export const TAILWIND_TEXT: Record<string, number> = {
  11. xs: 12, sm: 14, base: 16, lg: 18, xl: 20, '2xl': 24, '3xl': 30, '4xl': 36, '5xl': 48, '6xl': 60, '7xl': 72, '8xl': 96, '9xl': 128,
  12. };
  13. const M3 = [11, 12, 14, 16, 22, 24, 28, 32, 36, 45, 57];
  14. export function config(root = process.cwd()) {
  15. const fm = settings(CONFIG, root);
  16. return {
  17. base: n(fm.spacingBase, 4),
  18. fine: n(fm.spacingFine, 2),
  19. fineBelow: n(fm.spacingFineBelow, 16),
  20. typeScale: nums(fm.typeScale, [...new Set([...Object.values(TAILWIND_TEXT), ...M3])].sort((a, b) => a - b)),
  21. maxFontSizes: n(fm.maxFontSizes, 10),
  22. radii: nums(fm.radii, [0, 2, 4, 6, 8, 10, 12, 16, 20, 24, 32]),
  23. maxRadii: n(fm.maxRadii, 6),
  24. maxShadows: n(fm.maxShadows, 6),
  25. measure: nums(fm.measure, [45, 75]),
  26. };
  27. }
  28. export type Use = { at: string; value: string; px: number | null; source: 'css' | 'token' | 'class' | 'style' };
  29. export const SCRIPT = ['tsx', 'jsx', 'ts', 'js'];
  30. /** Every declaration in the project's CSS, with its place. */
  31. export function cssDecls(root = process.cwd()): { at: string; prop: string; value: string }[] {
  32. const out: { at: string; prop: string; value: string }[] = [];
  33. for (const rel of projectFiles(['css', 'scss'])) {
  34. for (const b of parseCss(read(rel, root))) for (const d of b.decls) out.push({ at: `${rel}:${d.line}`, prop: d.prop, value: d.value });
  35. }
  36. return out;
  37. }
  38. /** Tailwind classes of script files: every class-like token in string literals, with its place. */
  39. export function classes(root = process.cwd()): { at: string; cls: string }[] {
  40. const out: { at: string; cls: string }[] = [];
  41. for (const rel of projectFiles(SCRIPT)) {
  42. const text = blankComments(read(rel, root), false);
  43. for (const s of strings(text)) for (const t of tokens(s.value)) if (/^[!\w[&:@*-]/.test(t) && t.includes('-')) out.push({ at: `${rel}:${lineAt(text, s.at)}`, cls: t });
  44. }
  45. return out;
  46. }
  47. /** Inline style values of TSX/JSX: `prop: 13` or `prop: "13px"` inside `style={{ … }}`, for the given property names. */
  48. export function styleValues(props: RegExp, root = process.cwd()): { at: string; prop: string; value: string }[] {
  49. const out: { at: string; prop: string; value: string }[] = [];
  50. const re = new RegExp(`\\b(${props.source})\\s*:\\s*(-?\\d*\\.?\\d+|"[^"\\n]*"|'[^'\\n]*')\\s*[,}\\n]`, 'g');
  51. for (const rel of projectFiles(['tsx', 'jsx'])) {
  52. const text = blankComments(read(rel, root), false);
  53. const open = /\bstyle=\{\{/g;
  54. let o: RegExpExecArray | null;
  55. while ((o = open.exec(text))) {
  56. let depth = 2, i = o.index + o[0].length;
  57. for (; i < text.length && depth > 0; i++) depth += text[i] === '{' ? 1 : text[i] === '}' ? -1 : 0;
  58. const region = text.slice(o.index, i);
  59. let m: RegExpExecArray | null;
  60. re.lastIndex = 0;
  61. while ((m = re.exec(region))) {
  62. const raw = m[2].replace(/^["']|["']$/g, '');
  63. out.push({ at: `${rel}:${lineAt(text, o.index + m.index)}`, prop: m[1], value: /^-?\d*\.?\d+$/.test(raw) ? `${raw}px` : raw });
  64. }
  65. }
  66. }
  67. return out;
  68. }
  69. /** Strips variants (`md:hover:`), important marks and a leading minus from a Tailwind class. */
  70. export function utility(cls: string): string {
  71. let depth = 0, cut = 0;
  72. for (let i = 0; i < cls.length; i++) {
  73. if (cls[i] === '[') depth++;
  74. else if (cls[i] === ']') depth--;
  75. else if (cls[i] === ':' && depth === 0) cut = i + 1;
  76. }
  77. return cls.slice(cut).replace(/^!|!$/g, '').replace(/^-/, '');
  78. }
  79. /** The lengths of a CSS value (`8px 13px`, `0 auto`) → px each; null for anything that is not a plain length. */
  80. export function lengths(value: string): (number | null)[] {
  81. return value.trim().split(/\s+/).map((t) => (/^(auto|inherit|initial|unset|revert)$/.test(t) ? null : px(t)));
  82. }
  83. export const fmt = (p: number) => `${Math.round(p * 100) / 100}px`;