файл пакета→ .shtukbase/packages/design-scales/checks/spacing.ts
checks/spacing.ts
44 строки, 2.8 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // SCL-R01. Spacing sits on the scale: every margin, padding and gap written as a length is a multiple of the base
- // step (4 px); small values (below 16 px) may use the fine step (2 px). rem counts as 16 px.
- // Looked at: CSS declarations (margin*, padding*, gap, row-gap, column-gap, scroll-margin*, scroll-padding*) and
- // spacing custom properties (--space*, --ds-spacing-4, --gap*), Tailwind classes with arbitrary values (p-[13px],
- // gap-[10px], -mt-[3px]) and inline styles in TSX/JSX (padding: 13). Named Tailwind steps (p-3, gap-1.5) are on
- // the scale by construction. var(), calc(), % and auto are skipped. Below 4 px a value is an optical nudge, not
- // spacing: any is fine.
- import { report } from '../../design-base/lib/files.ts';
- import { classes, config, cssDecls, fmt, lengths, styleValues, utility } from './scaleslib.ts';
- export const RULE = { id: 'scl-r01', title: 'Spacing on a 4 px scale' };
- const CSS_PROP = /^(margin|padding|scroll-margin|scroll-padding)(-(top|right|bottom|left|inline|block|inline-start|inline-end|block-start|block-end))?$|^(gap|row-gap|column-gap|grid-gap)$/;
- const TOKEN = /^--([\w-]*-)?(space|spacing|gap|gutter)(-[\w-]*)?$/;
- const TW = /^(p|px|py|pt|pr|pb|pl|ps|pe|m|mx|my|mt|mr|mb|ml|ms|me|gap|gap-x|gap-y|space-x|space-y|scroll-m|scroll-p|scroll-mx|scroll-my|scroll-px|scroll-py)-\[([^\]]+)\]$/;
- const STYLE = /margin|marginTop|marginRight|marginBottom|marginLeft|marginInline|marginBlock|padding|paddingTop|paddingRight|paddingBottom|paddingLeft|paddingInline|paddingBlock|gap|rowGap|columnGap/;
- export function onScale(p: number, c: ReturnType<typeof config>): boolean {
- const a = Math.abs(p);
- const mult = (x: number, step: number) => Math.abs(x / step - Math.round(x / step)) < 1e-6;
- return a < 4 || mult(a, c.base) || (a < c.fineBelow && mult(a, c.fine));
- }
- export function check(root = process.cwd()): string[] {
- const c = config(root);
- const out: string[] = [];
- const say = (at: string, what: string, p: number) =>
- out.push(`${at}: spacing ${what} = ${fmt(p)} — not on the ${c.base} px scale (below ${c.fineBelow} px the step is ${c.fine}); nearest: ${fmt(Math.floor(p / c.base) * c.base)}, ${fmt(Math.ceil(p / c.base) * c.base)}`);
- for (const d of cssDecls(root)) {
- if (!CSS_PROP.test(d.prop) && !TOKEN.test(d.prop)) continue;
- for (const p of lengths(d.value)) if (p !== null && !onScale(p, c)) say(d.at, `${d.prop}: ${d.value}`, Math.abs(p));
- }
- for (const k of classes(root)) {
- const m = TW.exec(utility(k.cls));
- if (!m) continue;
- for (const p of lengths(m[2].replace(/_/g, ' '))) if (p !== null && !onScale(p, c)) say(k.at, k.cls, Math.abs(p));
- }
- for (const s of styleValues(STYLE, root)) {
- for (const p of lengths(s.value)) if (p !== null && !onScale(p, c)) say(s.at, `${s.prop}: ${s.value}`, Math.abs(p));
- }
- return out;
- }
- if (import.meta.main) report(check());