ShtukbaseStandard
包文件→ .shtukbase/packages/shtukbase-style/checks/shadow-even.ts

checks/shadow-even.ts

48 行, 2.7 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // SBS-R04. Edges and shadows are even on all sides: a `box-shadow` (and `filter: drop-shadow`) is not pushed to one
  2. // side. Every layer written with numbers has an offset of at most 1 px on both axes; layers that are a var(--…) are
  3. // fine (the style's own tokens are built that way). A shadow pushed down by 4 px, or pulled in with a negative
  4. // spread so that only the bottom shows, is a violation. Lengths in px, rem and em are read; others are skipped.
  5. import { splitTop } from '../../design-base/lib/css.ts';
  6. import { report } from '../../design-base/lib/files.ts';
  7. import { type Unit, allUnits, lenPx } from '../lib/scan.ts';
  8. export const RULE = { id: 'sbs-r04', title: 'Shadows are even on all sides' };
  9. export const MAX_OFFSET_PX = 1;
  10. /** The layers of a shadow value with their two offsets in px; layers that are not plain numbers are left out. */
  11. export function offsets(value: string): { layer: string; x: number; y: number }[] {
  12. const out: { layer: string; x: number; y: number }[] = [];
  13. for (const layer of splitTop(value.replace(/\s+/g, ' ').trim(), ',')) {
  14. if (/^(none|inherit|initial|unset|revert)$/i.test(layer) || /^var\(/.test(layer)) continue;
  15. // Take the lengths that stand outside of parentheses: colours like rgb(15 15 15 / .1) must not be read as lengths.
  16. let depth = 0, flat = '';
  17. for (const c of layer) { if (c === '(') depth++; else if (c === ')') depth--; else if (depth === 0) flat += c; }
  18. const lens = flat.split(' ').filter((t) => /^-?\d*\.?\d+(px|rem|em)?$/.test(t));
  19. if (lens.length < 2) continue;
  20. const x = lenPx(lens[0]), y = lenPx(lens[1]);
  21. if (x !== null && y !== null) out.push({ layer: layer.trim(), x, y });
  22. }
  23. return out;
  24. }
  25. export function checkUnits(units: Unit[]): string[] {
  26. const out: string[] = [];
  27. for (const u of units) {
  28. for (const d of u.decls) {
  29. let value: string | null = null;
  30. if (d.prop === 'box-shadow') value = d.value;
  31. else if (d.prop === 'filter' || d.prop === 'backdrop-filter') value = [...d.value.matchAll(/drop-shadow\(((?:[^()]|\([^()]*\))*)\)/gi)].map((m) => m[1]).join(', ') || null;
  32. if (!value || u.allowed(d.line)) continue;
  33. for (const o of offsets(value))
  34. if (Math.abs(o.x) > MAX_OFFSET_PX || Math.abs(o.y) > MAX_OFFSET_PX)
  35. out.push(`${u.file}:${d.line}: shadow "${o.layer.slice(0, 70)}" is pushed ${Math.abs(o.y) > MAX_OFFSET_PX ? (o.y > 0 ? 'down' : 'up') : o.x > 0 ? 'right' : 'left'} by ${Math.max(Math.abs(o.x), Math.abs(o.y))} px — an edge or a shadow is even on all sides: offset 0, or use var(--sb-edge), var(--sb-shadow-sheet)`);
  36. }
  37. }
  38. return out;
  39. }
  40. export function check(): string[] {
  41. return checkUnits(allUnits());
  42. }
  43. if (import.meta.main) report(check());