包文件→ .shtukbase/packages/norms-core/checks/enforcers.ts
checks/enforcers.ts
32 行, 1.6 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // NS-R07. A norm always has an enforcer: every rule names `enforcers` (paths of its enforcers: a check, sensor,
- // hook or judge) or, until one exists, `plannedEnforcer`.
- // A rule with `mode: enforce` has enforcers that exist: `enforcers` is not empty and every path in it is a file or
- // folder of the project (paths from the project root). A path written `shtuk:<path>` is an enforcer that lives
- // in the tool itself, outside the project: it counts and is not looked up.
- import { existsSync } from 'node:fs';
- import { posix } from 'node:path';
- import { ROOT, empty, main, records, status, values } from './pagelib.ts';
- import { pystr, truthy } from '../../collections/checks/lib/pages.ts';
- const DEAD = ['rejected'];
- export const RULE = { id: 'ns-r07', title: 'A norm has an enforcer; an enforcing norm has its enforcers in the project' };
- export function check(): string[] {
- const out: string[] = [];
- for (const [addr, head] of records('norms/rules')) {
- if (DEAD.includes(status(head))) continue;
- const enf = values(head.enforcers);
- if (!enf.length && empty(head.plannedEnforcer)) out.push(`${addr}: no enforcer named — fill in enforcers (the paths of enforcers) or plannedEnforcer`);
- if ((truthy(head.mode) ? pystr(head.mode) : '').trim() === 'enforce') {
- if (!enf.length) out.push(`${addr}: enforcing mode (mode: enforce), but enforcers is empty`);
- for (const h of enf) {
- if (h.startsWith('shtuk:')) continue;
- if (!existsSync(posix.join(ROOT, h.replace(/^\/+/, '')))) out.push(`${addr}: the enforcer ${h} is not in the project`);
- }
- }
- }
- return out;
- }
- if (import.meta.main) main({ RULE, check });