archivo del paquete→ .shtukbase/packages/design-scales/checks/shape.ts
checks/shape.ts
72 líneas, 4.3 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // SCL-R03. Radii and shadows come from a short set.
- // Radii: every radius written as a length is in `radii` of /design/scales.md (default 0 2 4 6 8 10 12 16 20 24 32)
- // or is a pill (≥ 999 px) or a percentage; the project uses no more than `maxRadii` distinct radii (default 6).
- // Shadows: a box-shadow in a rule is a theme shadow (var(--shadow-…)) or none; a ring (0 0 0 Npx …: offsets and blur
- // zero) is not a shadow and is fine; the project defines no more than `maxShadows` shadow custom properties
- // (--shadow*, --ds-shadow-soft; only those holding a whole shadow, not its color or a ring; default 6). Tailwind: rounded-[13px], shadow-[0_2px_3px_…] count the same way.
- import { report } from '../../design-base/lib/files.ts';
- import { px, splitTop } from '../../design-base/lib/css.ts';
- import { classes, config, cssDecls, fmt, styleValues, utility } from './scaleslib.ts';
- export const RULE = { id: 'scl-r03', title: 'Radii and shadows from a short set' };
- const RADIUS = /^border(-(top|bottom|start|end)-(left|right|start|end))?-radius$|^--([\w-]*-)?radius(-[\w-]*)?$/;
- const TW_RADIUS = /^rounded(-(t|r|b|l|s|e|tl|tr|br|bl|ss|se|es|ee))?-\[([^\]]+)\]$/;
- /** A shadow value that is not taken from the theme: has a length or a color of its own, and is not a ring. */
- export function adHocShadow(value: string): boolean {
- const v = value.trim();
- if (/^(none|inherit|initial|unset|revert)$/.test(v)) return false;
- return splitTop(v).some((layer) => {
- const l = layer.trim();
- if (/^var\(--[\w-]+\)$/.test(l)) return false;
- const lens = l.replace(/var\([^)]*\)|\w+\([^)]*\)/g, ' ').split(/\s+/).filter((t) => /^-?\d*\.?\d+(px|rem|em)?$/.test(t)).map((t) => px(t) ?? 0);
- if (lens.length >= 4 && lens[0] === 0 && lens[1] === 0 && lens[2] === 0) return false; // a ring
- return true;
- });
- }
- /** A custom property that holds a whole shadow (offsets and blur), not a ring or a color of a shadow. */
- export function isShadow(value: string): boolean {
- return splitTop(value).some((layer) => {
- const lens = layer.replace(/var\([^)]*\)|\w+\([^)]*\)/g, ' ').split(/\s+/).filter((t) => /^-?\d*\.?\d+(px|rem|em)?$/.test(t)).map((t) => px(t) ?? 0);
- return lens.length >= 2 && !(lens.length >= 4 && lens[0] === 0 && lens[1] === 0 && lens[2] === 0);
- });
- }
- export function check(root = process.cwd()): string[] {
- const c = config(root);
- const out: string[] = [];
- const radii: number[] = [];
- const radius = (at: string, what: string, value: string) => {
- for (const t of value.trim().split(/[\s/]+/)) {
- if (t.endsWith('%')) continue;
- const p = px(t);
- if (p === null || p >= 999) continue;
- radii.push(p);
- if (!c.radii.some((r) => Math.abs(r - p) < 0.01)) out.push(`${at}: radius ${what} = ${fmt(p)} — not from the set (${c.radii.join(', ')})`);
- }
- };
- const shadows = new Set<string>();
- for (const d of cssDecls(root)) {
- if (RADIUS.test(d.prop)) radius(d.at, `${d.prop}: ${d.value}`, d.value);
- else if (d.prop === 'box-shadow' && adHocShadow(d.value))
- out.push(`${d.at}: shadow box-shadow: ${d.value.slice(0, 60)} — own, not from the theme; move it to --shadow-… and use var()`);
- else if (/^--([\w-]*-)?shadow(-[\w-]*)?$/.test(d.prop) && isShadow(d.value)) shadows.add(`${d.prop}`);
- }
- for (const k of classes(root)) {
- const u = utility(k.cls);
- const r = TW_RADIUS.exec(u);
- if (r) radius(k.at, k.cls, r[3].replace(/_/g, ' '));
- const s = /^shadow-\[([^\]]+)\]$/.exec(u);
- if (s && /\d/.test(s[1]) && adHocShadow(s[1].replace(/_/g, ' '))) out.push(`${k.at}: shadow ${k.cls} — own, not from the theme; use shadow-… from the theme`);
- }
- for (const s of styleValues(/borderRadius/, root)) radius(s.at, `${s.prop}: ${s.value}`, s.value);
- for (const s of styleValues(/boxShadow/, root)) if (adHocShadow(s.value)) out.push(`${s.at}: shadow boxShadow: ${s.value.slice(0, 60)} — own, not from the theme`);
- const distinct = [...new Set(radii.map((r) => Math.round(r * 100) / 100))].sort((a, b) => a - b);
- if (distinct.length > c.maxRadii) out.push(`design/scales.md: the project has ${distinct.length} different radii (${distinct.join(', ')}) — more than ${c.maxRadii}`);
- if (shadows.size > c.maxShadows) out.push(`design/scales.md: the theme has ${shadows.size} shadows (${[...shadows].join(', ')}) — more than ${c.maxShadows}`);
- return out;
- }
- if (import.meta.main) report(check());