ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/keer-ui/checks/states.ts

checks/states.ts

32 líneas, 1.6 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

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