包文件→ .shtukbase/packages/keer-ui/checks/states.ts
checks/states.ts
32 行, 1.6 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // KU-R02. Every component of the library folder has a states file: next to `<name>.tsx` lies `<name>.states.tsx`
- // that exports `meta` and `states`. Looked at: the component folders of the target (primitives, composites and any
- // folder the project adds there), except lib/, hooks/, styles/ and test, story and states files themselves.
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { config, files, readManifest } from '../actions/kitlib.ts';
- export const RULE = { id: 'ku-r02', title: 'A component has a states file' };
- const NOT_COMPONENTS = /^(lib|hooks|styles)\//;
- export function check(root = process.cwd()): string[] {
- const { target } = config(root);
- if (!readManifest(root, target)) return []; // not placed: KU-R01 says so
- const out: string[] = [];
- for (const rel of files(join(root, target))) {
- if (!rel.endsWith('.tsx') || NOT_COMPONENTS.test(rel) || /\.(states|test|spec|stories)\.tsx$/.test(rel)) continue;
- const states = rel.replace(/\.tsx$/, '.states.tsx');
- const p = join(root, target, states);
- if (!existsSync(p)) { out.push(`${target}/${rel}: no states file ${states} — describe meta and states (default and its own)`); continue; }
- const text = readFileSync(p, 'utf8');
- if (!/export\s+const\s+meta\b/.test(text) || !/export\s+const\s+states\b/.test(text))
- out.push(`${target}/${states}: the states file has no export const meta or export const states`);
- }
- return out;
- }
- if (import.meta.main) {
- const out = check();
- if (out.length) console.log(out.join('\n'));
- process.exitCode = out.length ? 1 : 0;
- }