ShtukbaseStandard
package file→ .shtukbase/packages/shtukbase-ui-react/checks/states.ts

checks/states.ts

29 lines, 1.4 KB.

  1. // SBR-R02. Every component of the library folder has a states file: next to `<name>.tsx` lies `<name>.states.tsx`
  2. // that exports `meta` and `states`. Looked at: the component folders of the target (primitives, composites, blocks and any
  3. // folder the project adds there), except lib/, styles/ and test, story and states files themselves.
  4. import { existsSync, readFileSync } from 'node:fs';
  5. import { join } from 'node:path';
  6. import { componentFiles, config, readManifest } from '../actions/kitlib.ts';
  7. export const RULE = { id: 'sbr-r02', title: 'A component has a states file' };
  8. export function check(root = process.cwd()): string[] {
  9. const { target } = config(root);
  10. if (!readManifest(root, target)) return []; // not placed: SBR-R01 says so
  11. const out: string[] = [];
  12. for (const rel of componentFiles(join(root, target))) {
  13. const states = rel.replace(/\.tsx$/, '.states.tsx');
  14. const p = join(root, target, states);
  15. if (!existsSync(p)) { out.push(`${target}/${rel}: no states file ${states} — describe meta and states (default and its own)`); continue; }
  16. const text = readFileSync(p, 'utf8');
  17. if (!/export\s+const\s+meta\b/.test(text) || !/export\s+const\s+states\b/.test(text))
  18. out.push(`${target}/${states}: the states file has no export const meta or export const states`);
  19. }
  20. return out;
  21. }
  22. if (import.meta.main) {
  23. const out = check();
  24. if (out.length) console.log(out.join('\n'));
  25. process.exitCode = out.length ? 1 : 0;
  26. }