ShtukbaseStandard
файл пакета→ .shtukbase/packages/norms-core/checks/enforcers.ts

checks/enforcers.ts

32 строки, 1.6 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // NS-R07. A norm always has an enforcer: every rule names `enforcers` (paths of its enforcers: a check, sensor,
  2. // hook or judge) or, until one exists, `plannedEnforcer`.
  3. // A rule with `mode: enforce` has enforcers that exist: `enforcers` is not empty and every path in it is a file or
  4. // folder of the project (paths from the project root). A path written `shtuk:<path>` is an enforcer that lives
  5. // in the tool itself, outside the project: it counts and is not looked up.
  6. import { existsSync } from 'node:fs';
  7. import { posix } from 'node:path';
  8. import { ROOT, empty, main, records, status, values } from './pagelib.ts';
  9. import { pystr, truthy } from '../../collections/checks/lib/pages.ts';
  10. const DEAD = ['rejected'];
  11. export const RULE = { id: 'ns-r07', title: 'A norm has an enforcer; an enforcing norm has its enforcers in the project' };
  12. export function check(): string[] {
  13. const out: string[] = [];
  14. for (const [addr, head] of records('norms/rules')) {
  15. if (DEAD.includes(status(head))) continue;
  16. const enf = values(head.enforcers);
  17. if (!enf.length && empty(head.plannedEnforcer)) out.push(`${addr}: no enforcer named — fill in enforcers (the paths of enforcers) or plannedEnforcer`);
  18. if ((truthy(head.mode) ? pystr(head.mode) : '').trim() === 'enforce') {
  19. if (!enf.length) out.push(`${addr}: enforcing mode (mode: enforce), but enforcers is empty`);
  20. for (const h of enf) {
  21. if (h.startsWith('shtuk:')) continue;
  22. if (!existsSync(posix.join(ROOT, h.replace(/^\/+/, '')))) out.push(`${addr}: the enforcer ${h} is not in the project`);
  23. }
  24. }
  25. }
  26. return out;
  27. }
  28. if (import.meta.main) main({ RULE, check });