файл пакета→ .shtukbase/packages/design-scales/checks/scaleslib.ts
checks/scaleslib.ts
95 строк, 4.5 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // Shared parts of the design-scales checks: settings from /design/scales.md and the places where sizes are written
- // (CSS declarations, Tailwind classes with arbitrary values, inline style objects in TSX/JSX).
- import { parseCss, px } from '../../design-base/lib/css.ts';
- import { blankComments, strings, tokens } from '../../design-base/lib/markup.ts';
- import { lineAt, projectFiles, read, settings } from '../../design-base/lib/files.ts';
- export const CONFIG = 'design/scales.md';
- const nums = (v: unknown, d: number[]) => (Array.isArray(v) && v.length ? v.map(Number).filter((n) => !Number.isNaN(n)) : d);
- const n = (v: unknown, d: number) => (Number(v) > 0 ? Number(v) : d);
- // Tailwind's named font sizes (px) and the Material 3 type scale: the default list of allowed sizes.
- export const TAILWIND_TEXT: Record<string, number> = {
- 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,
- };
- const M3 = [11, 12, 14, 16, 22, 24, 28, 32, 36, 45, 57];
- export function config(root = process.cwd()) {
- const fm = settings(CONFIG, root);
- return {
- base: n(fm.spacingBase, 4),
- fine: n(fm.spacingFine, 2),
- fineBelow: n(fm.spacingFineBelow, 16),
- typeScale: nums(fm.typeScale, [...new Set([...Object.values(TAILWIND_TEXT), ...M3])].sort((a, b) => a - b)),
- maxFontSizes: n(fm.maxFontSizes, 10),
- radii: nums(fm.radii, [0, 2, 4, 6, 8, 10, 12, 16, 20, 24, 32]),
- maxRadii: n(fm.maxRadii, 6),
- maxShadows: n(fm.maxShadows, 6),
- measure: nums(fm.measure, [45, 75]),
- };
- }
- export type Use = { at: string; value: string; px: number | null; source: 'css' | 'token' | 'class' | 'style' };
- export const SCRIPT = ['tsx', 'jsx', 'ts', 'js'];
- /** Every declaration in the project's CSS, with its place. */
- export function cssDecls(root = process.cwd()): { at: string; prop: string; value: string }[] {
- const out: { at: string; prop: string; value: string }[] = [];
- for (const rel of projectFiles(['css', 'scss'])) {
- 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 });
- }
- return out;
- }
- /** Tailwind classes of script files: every class-like token in string literals, with its place. */
- export function classes(root = process.cwd()): { at: string; cls: string }[] {
- const out: { at: string; cls: string }[] = [];
- for (const rel of projectFiles(SCRIPT)) {
- const text = blankComments(read(rel, root), false);
- 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 });
- }
- return out;
- }
- /** Inline style values of TSX/JSX: `prop: 13` or `prop: "13px"` inside `style={{ … }}`, for the given property names. */
- export function styleValues(props: RegExp, root = process.cwd()): { at: string; prop: string; value: string }[] {
- const out: { at: string; prop: string; value: string }[] = [];
- const re = new RegExp(`\\b(${props.source})\\s*:\\s*(-?\\d*\\.?\\d+|"[^"\\n]*"|'[^'\\n]*')\\s*[,}\\n]`, 'g');
- for (const rel of projectFiles(['tsx', 'jsx'])) {
- const text = blankComments(read(rel, root), false);
- const open = /\bstyle=\{\{/g;
- let o: RegExpExecArray | null;
- while ((o = open.exec(text))) {
- let depth = 2, i = o.index + o[0].length;
- for (; i < text.length && depth > 0; i++) depth += text[i] === '{' ? 1 : text[i] === '}' ? -1 : 0;
- const region = text.slice(o.index, i);
- let m: RegExpExecArray | null;
- re.lastIndex = 0;
- while ((m = re.exec(region))) {
- const raw = m[2].replace(/^["']|["']$/g, '');
- out.push({ at: `${rel}:${lineAt(text, o.index + m.index)}`, prop: m[1], value: /^-?\d*\.?\d+$/.test(raw) ? `${raw}px` : raw });
- }
- }
- }
- return out;
- }
- /** Strips variants (`md:hover:`), important marks and a leading minus from a Tailwind class. */
- export function utility(cls: string): string {
- let depth = 0, cut = 0;
- for (let i = 0; i < cls.length; i++) {
- if (cls[i] === '[') depth++;
- else if (cls[i] === ']') depth--;
- else if (cls[i] === ':' && depth === 0) cut = i + 1;
- }
- return cls.slice(cut).replace(/^!|!$/g, '').replace(/^-/, '');
- }
- /** The lengths of a CSS value (`8px 13px`, `0 auto`) → px each; null for anything that is not a plain length. */
- export function lengths(value: string): (number | null)[] {
- return value.trim().split(/\s+/).map((t) => (/^(auto|inherit|initial|unset|revert)$/.test(t) ? null : px(t)));
- }
- export const fmt = (p: number) => `${Math.round(p * 100) / 100}px`;