包文件→ .shtukbase/packages/shadcn/checks/colors.ts
checks/colors.ts
54 行, 3.7 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // SHD-R03. Colors come only from the theme. In TSX/JSX: no Tailwind palette colors (bg-red-500, text-zinc-900),
- // no arbitrary colors (bg-[#fff], text-[rgb(…)]), no color literals in style={{…}} or in fill/stroke/color
- // attributes. In CSS: a declaration that is not a custom property does not hold a raw color (hex, rgb(), hsl(),
- // oklch()…). Custom property definitions (the theme itself) may. Allowed names: white, black, transparent, current,
- // inherit — the shadcn components use black/50 for the overlay and white on destructive. A relative color made from
- // a theme variable (oklch(from var(--primary) …)) follows the theme and is allowed.
- import { RAW_COLOR } from '../../design-base/lib/color.ts';
- import { parseCss } from '../../design-base/lib/css.ts';
- import { lineAt, projectFiles, read, report } from '../../design-base/lib/files.ts';
- import { blankComments, strings, tokens } from '../../design-base/lib/markup.ts';
- export const RULE = { id: 'shd-r03', title: 'Colors only from the theme' };
- 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';
- 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';
- const PALETTE_CLASS = new RegExp(`^(?:.*:)?!?-?(?:${UTIL})-(?:${PALETTE})-(?:50|[1-9]00|950)(?:/[\\w.%\\[\\]]+)?!?$`);
- const ARBITRARY = new RegExp(`^(?:.*:)?!?(?:${UTIL})-\\[(?:color:)?(?:#[0-9a-fA-F]{3,8}|(?:rgba?|hsla?|oklch|oklab|lab|lch|hwb|color)\\()`);
- // A relative color built from a theme variable (oklch(from var(--primary) …)) follows the theme: allowed.
- const RELATIVE = /\(from[\s_]+var\(--/;
- const STYLE_COLOR = /\b(color|background|backgroundColor|borderColor|borderTopColor|borderBottomColor|outlineColor|fill|stroke|caretColor|accentColor|boxShadow|textShadow|textDecorationColor)\s*:\s*["'`]([^"'`\n]*)["'`]/g;
- const ATTR_COLOR = /\b(fill|stroke|color|stopColor|stop-color|floodColor|lightingColor)=["'](#[0-9a-fA-F]{3,8}|(?:rgba?|hsla?|oklch)\([^"']*\))["']/g;
- export function check(root = process.cwd()): string[] {
- const out: string[] = [];
- for (const rel of projectFiles(['tsx', 'jsx'])) {
- const text = blankComments(read(rel, root), false);
- for (const s of strings(text)) {
- for (const t of tokens(s.value)) {
- 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…)`);
- 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`);
- }
- }
- for (const re of [STYLE_COLOR, ATTR_COLOR]) {
- re.lastIndex = 0;
- let m: RegExpExecArray | null;
- while ((m = re.exec(text))) {
- if (re === STYLE_COLOR && !RAW_COLOR.test(m[2])) continue;
- out.push(`${rel}:${lineAt(text, m.index)}: own color ${m[0].slice(0, 60)} — use a theme var(--…)`);
- }
- }
- }
- for (const rel of projectFiles(['css', 'scss'])) {
- for (const b of parseCss(read(rel, root))) {
- if (b.prelude.startsWith('@theme') || b.at.some((a) => a.startsWith('@keyframes')) || b.prelude.startsWith('@keyframes')) continue;
- for (const d of b.decls) {
- if (d.prop.startsWith('--')) continue;
- 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(--…)`);
- }
- }
- }
- return out;
- }
- if (import.meta.main) report(check());