файл пакета→ .shtukbase/packages/contours/checks/c03_moves.ts
checks/c03_moves.ts
60 строк, 3.2 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // C3. A record changes state only by an allowed transition: the new phase is one of the exits (`next`)
- // of the previous phase or a phase with `fromAnywhere: true`. The previous phase is the field value in the last commit (git HEAD), the new one is in the working copy.
- // Only records changed since the last commit are looked at; a new record (not in HEAD) is not checked.
- // The project is not in git or has no commits yet: nothing to check.
- import { spawnSync } from 'node:child_process';
- import { addr, ctx, empty, hole, iterFields } from '../../collections/checks/lib/schemalib.ts';
- import { type Project, type Result, pystr } from '../../collections/checks/lib/speclib.ts';
- import { exits, headText, main, norm, phases } from './contourlib.ts';
- export const RULE = {
- id: 'c03',
- title: 'Contours: the state changes only by an allowed transition',
- scope: 'records of collections with a kind: phase field, changed since the last commit',
- };
- /** stdout of a git command in the project folder, or null when git fails. */
- function git(root: string, ...args: string[]): string | null {
- const r = spawnSync('git', args, { cwd: root, encoding: 'utf8', maxBuffer: 1 << 28 });
- if (r.error) return null;
- return r.status === 0 ? r.stdout : null;
- }
- const isObj = (v: unknown): boolean => !!v && typeof v === 'object';
- export function check(p: Project, res: Result): void {
- const c = ctx(p);
- const root = c.root;
- if (git(root, 'rev-parse', '--verify', '-q', 'HEAD') === null) {
- res.part('transitions', 'the project is not in git or has no commits yet');
- return;
- }
- const out = git(root, 'diff', '--name-only', '--relative', 'HEAD', '--', '.') ?? '';
- const changed = new Set(out.split(/\r?\n/).map((x) => x.trim()).filter((x) => x.endsWith('.md')).map((x) => '/' + x));
- res.part('transitions', null);
- const scope: string[] = [];
- for (const [coll, name, spec] of iterFields(c, ['phase'])) {
- const contour = norm(spec.contour);
- const have = phases(root, contour);
- if (!have || !have.size) continue; // no contour: the state check reports it
- for (const r of c.records(coll)) {
- const rel = '/' + (r.id.endsWith('.md') ? r.id : r.id + '.md').replace(/^\/+/, '');
- if (!changed.has(rel)) continue;
- const nw = r.fm[name];
- if (empty(nw) || isObj(nw) || !have.has(pystr(nw))) continue; // empty or unknown state: the required and state checks report it
- const next = pystr(nw);
- scope.push(addr(rel));
- const oldText = git(root, 'show', 'HEAD:./' + rel.replace(/^\/+/, ''));
- if (oldText === null) continue; // a new record: no move to check
- const old0 = headText(oldText)[name];
- if (empty(old0) || isObj(old0) || pystr(old0) === next || !have.has(pystr(old0))) continue;
- const old = pystr(old0);
- const allowed = exits(have.get(old)!).filter((t) => t.startsWith(contour + '/')).map((t) => t.slice(t.lastIndexOf('/') + 1));
- if (!allowed.includes(next) && have.get(next)!.fromAnywhere !== true)
- hole(res, addr(rel), name, 'bad-move', `${old} → ${next}: the contour ${contour} has no such transition`, `from ${old} you can go to: ${allowed.join(', ') || 'nowhere (this is the end)'}`);
- }
- }
- res.scope = scope;
- }
- if (import.meta.main) main({ RULE, check });