包文件→ .shtukbase/packages/design-scales/checks/type.ts
checks/type.ts
52 行, 2.7 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // SCL-R02. Font sizes come from a short type scale: every font size written as a length is one of the sizes of
- // `typeScale` on /design/scales.md (default: the Tailwind steps and the Material 3 type scale), and the project
- // uses no more than `maxFontSizes` distinct sizes (default 10).
- // Looked at: CSS font-size and size custom properties (--text-*, --font-size-*, --fs-*, --ds-font-body-size), Tailwind classes
- // (text-sm, text-[13px]) and inline styles (fontSize: 13). var(), calc(), clamp() and em are skipped.
- import { report } from '../../design-base/lib/files.ts';
- import { TAILWIND_TEXT, classes, config, cssDecls, fmt, styleValues, utility } from './scaleslib.ts';
- import { px } from '../../design-base/lib/css.ts';
- export const RULE = { id: 'scl-r02', title: 'Font sizes from a short scale' };
- const TOKEN = /^--(text|font-size|fs|type)-[\w-]+$|^--[\w-]*(font|text|type)[\w-]*-size$/;
- const NOT_SIZE = /(line-height|leading|tracking|letter-spacing|weight|family|shadow|color)/;
- export function sizes(root = process.cwd()): { at: string; what: string; px: number }[] {
- const out: { at: string; what: string; px: number }[] = [];
- const lit = (v: string) => (/^-?\d*\.?\d+(px|rem)$/.test(v.trim()) ? px(v) : null);
- for (const d of cssDecls(root)) {
- if (d.prop !== 'font-size' && !(TOKEN.test(d.prop) && !NOT_SIZE.test(d.prop))) continue;
- const p = lit(d.value);
- if (p !== null) out.push({ at: d.at, what: `${d.prop}: ${d.value}`, px: p });
- }
- for (const k of classes(root)) {
- const u = utility(k.cls).replace(/\/.*$/, '');
- const named = /^text-(xs|sm|base|lg|xl|[2-9]xl)$/.exec(u);
- if (named) { out.push({ at: k.at, what: k.cls, px: TAILWIND_TEXT[named[1]] }); continue; }
- const arb = /^text-\[(?:length:)?([^\]]+)\]$/.exec(u);
- const p = arb ? lit(arb[1]) : null;
- if (p !== null) out.push({ at: k.at, what: k.cls, px: p });
- }
- for (const s of styleValues(/fontSize/, root)) {
- const p = lit(s.value);
- if (p !== null) out.push({ at: s.at, what: `${s.prop}: ${s.value}`, px: p });
- }
- return out;
- }
- export function check(root = process.cwd()): string[] {
- const c = config(root);
- const out: string[] = [];
- const all = sizes(root);
- for (const s of all) {
- if (!c.typeScale.some((t) => Math.abs(t - s.px) < 0.01))
- out.push(`${s.at}: font size ${s.what} = ${fmt(s.px)} — not from the scale (${c.typeScale.join(', ')})`);
- }
- const distinct = [...new Set(all.map((s) => Math.round(s.px * 100) / 100))].sort((a, b) => a - b);
- if (distinct.length > c.maxFontSizes)
- out.push(`${'design/scales.md'}: the project has ${distinct.length} different font sizes (${distinct.join(', ')}) — more than ${c.maxFontSizes}; reduce them to the scale`);
- return out;
- }
- if (import.meta.main) report(check());