ShtukbaseStandard
包文件→ .shtukbase/packages/shadcn/checks/colors.ts

checks/colors.ts

54 行, 3.7 KB.

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

  1. // SHD-R03. Colors come only from the theme. In TSX/JSX: no Tailwind palette colors (bg-red-500, text-zinc-900),
  2. // no arbitrary colors (bg-[#fff], text-[rgb(…)]), no color literals in style={{…}} or in fill/stroke/color
  3. // attributes. In CSS: a declaration that is not a custom property does not hold a raw color (hex, rgb(), hsl(),
  4. // oklch()…). Custom property definitions (the theme itself) may. Allowed names: white, black, transparent, current,
  5. // inherit — the shadcn components use black/50 for the overlay and white on destructive. A relative color made from
  6. // a theme variable (oklch(from var(--primary) …)) follows the theme and is allowed.
  7. import { RAW_COLOR } from '../../design-base/lib/color.ts';
  8. import { parseCss } from '../../design-base/lib/css.ts';
  9. import { lineAt, projectFiles, read, report } from '../../design-base/lib/files.ts';
  10. import { blankComments, strings, tokens } from '../../design-base/lib/markup.ts';
  11. export const RULE = { id: 'shd-r03', title: 'Colors only from the theme' };
  12. const PALETTE = 'slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose|taupe|mauve|mist|olive';
  13. const UTIL = 'bg|text|border(?:-[xytrblse])?|ring|ring-offset|outline|fill|stroke|from|via|to|decoration|divide|placeholder|caret|accent|shadow|inset-shadow|drop-shadow|inset-ring';
  14. const PALETTE_CLASS = new RegExp(`^(?:.*:)?!?-?(?:${UTIL})-(?:${PALETTE})-(?:50|[1-9]00|950)(?:/[\\w.%\\[\\]]+)?!?$`);
  15. const ARBITRARY = new RegExp(`^(?:.*:)?!?(?:${UTIL})-\\[(?:color:)?(?:#[0-9a-fA-F]{3,8}|(?:rgba?|hsla?|oklch|oklab|lab|lch|hwb|color)\\()`);
  16. // A relative color built from a theme variable (oklch(from var(--primary) …)) follows the theme: allowed.
  17. const RELATIVE = /\(from[\s_]+var\(--/;
  18. const STYLE_COLOR = /\b(color|background|backgroundColor|borderColor|borderTopColor|borderBottomColor|outlineColor|fill|stroke|caretColor|accentColor|boxShadow|textShadow|textDecorationColor)\s*:\s*["'`]([^"'`\n]*)["'`]/g;
  19. const ATTR_COLOR = /\b(fill|stroke|color|stopColor|stop-color|floodColor|lightingColor)=["'](#[0-9a-fA-F]{3,8}|(?:rgba?|hsla?|oklch)\([^"']*\))["']/g;
  20. export function check(root = process.cwd()): string[] {
  21. const out: string[] = [];
  22. for (const rel of projectFiles(['tsx', 'jsx'])) {
  23. const text = blankComments(read(rel, root), false);
  24. for (const s of strings(text)) {
  25. for (const t of tokens(s.value)) {
  26. if (PALETTE_CLASS.test(t)) out.push(`${rel}:${lineAt(text, s.at)}: Tailwind palette color ${t} — use a theme color (bg-primary, text-muted-foreground, border-border…)`);
  27. else if (ARBITRARY.test(t) && !RELATIVE.test(t)) out.push(`${rel}:${lineAt(text, s.at)}: own color ${t} — use a theme color or add a variable to the theme`);
  28. }
  29. }
  30. for (const re of [STYLE_COLOR, ATTR_COLOR]) {
  31. re.lastIndex = 0;
  32. let m: RegExpExecArray | null;
  33. while ((m = re.exec(text))) {
  34. if (re === STYLE_COLOR && !RAW_COLOR.test(m[2])) continue;
  35. out.push(`${rel}:${lineAt(text, m.index)}: own color ${m[0].slice(0, 60)} — use a theme var(--…)`);
  36. }
  37. }
  38. }
  39. for (const rel of projectFiles(['css', 'scss'])) {
  40. for (const b of parseCss(read(rel, root))) {
  41. if (b.prelude.startsWith('@theme') || b.at.some((a) => a.startsWith('@keyframes')) || b.prelude.startsWith('@keyframes')) continue;
  42. for (const d of b.decls) {
  43. if (d.prop.startsWith('--')) continue;
  44. if (RAW_COLOR.test(d.value.replace(/var\([^)]*\)/g, '')) && !(RELATIVE.test(d.value) && !/#[0-9a-fA-F]{3,8}\b/.test(d.value))) out.push(`${rel}:${d.line}: ${d.prop}: ${d.value.slice(0, 50)} — own color in a rule; use a theme var(--…)`);
  45. }
  46. }
  47. }
  48. return out;
  49. }
  50. if (import.meta.main) report(check());