ShtukbaseStandard
package file→ .shtukbase/packages/contours/checks/c03_moves.ts

checks/c03_moves.ts

60 lines, 3.2 KB.

  1. // C3. A record changes state only by an allowed transition: the new phase is one of the exits (`next`)
  2. // 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.
  3. // Only records changed since the last commit are looked at; a new record (not in HEAD) is not checked.
  4. // The project is not in git or has no commits yet: nothing to check.
  5. import { spawnSync } from 'node:child_process';
  6. import { addr, ctx, empty, hole, iterFields } from '../../collections/checks/lib/schemalib.ts';
  7. import { type Project, type Result, pystr } from '../../collections/checks/lib/speclib.ts';
  8. import { exits, headText, main, norm, phases } from './contourlib.ts';
  9. export const RULE = {
  10. id: 'c03',
  11. title: 'Contours: the state changes only by an allowed transition',
  12. scope: 'records of collections with a kind: phase field, changed since the last commit',
  13. };
  14. /** stdout of a git command in the project folder, or null when git fails. */
  15. function git(root: string, ...args: string[]): string | null {
  16. const r = spawnSync('git', args, { cwd: root, encoding: 'utf8', maxBuffer: 1 << 28 });
  17. if (r.error) return null;
  18. return r.status === 0 ? r.stdout : null;
  19. }
  20. const isObj = (v: unknown): boolean => !!v && typeof v === 'object';
  21. export function check(p: Project, res: Result): void {
  22. const c = ctx(p);
  23. const root = c.root;
  24. if (git(root, 'rev-parse', '--verify', '-q', 'HEAD') === null) {
  25. res.part('transitions', 'the project is not in git or has no commits yet');
  26. return;
  27. }
  28. const out = git(root, 'diff', '--name-only', '--relative', 'HEAD', '--', '.') ?? '';
  29. const changed = new Set(out.split(/\r?\n/).map((x) => x.trim()).filter((x) => x.endsWith('.md')).map((x) => '/' + x));
  30. res.part('transitions', null);
  31. const scope: string[] = [];
  32. for (const [coll, name, spec] of iterFields(c, ['phase'])) {
  33. const contour = norm(spec.contour);
  34. const have = phases(root, contour);
  35. if (!have || !have.size) continue; // no contour: the state check reports it
  36. for (const r of c.records(coll)) {
  37. const rel = '/' + (r.id.endsWith('.md') ? r.id : r.id + '.md').replace(/^\/+/, '');
  38. if (!changed.has(rel)) continue;
  39. const nw = r.fm[name];
  40. if (empty(nw) || isObj(nw) || !have.has(pystr(nw))) continue; // empty or unknown state: the required and state checks report it
  41. const next = pystr(nw);
  42. scope.push(addr(rel));
  43. const oldText = git(root, 'show', 'HEAD:./' + rel.replace(/^\/+/, ''));
  44. if (oldText === null) continue; // a new record: no move to check
  45. const old0 = headText(oldText)[name];
  46. if (empty(old0) || isObj(old0) || pystr(old0) === next || !have.has(pystr(old0))) continue;
  47. const old = pystr(old0);
  48. const allowed = exits(have.get(old)!).filter((t) => t.startsWith(contour + '/')).map((t) => t.slice(t.lastIndexOf('/') + 1));
  49. if (!allowed.includes(next) && have.get(next)!.fromAnywhere !== true)
  50. 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)'}`);
  51. }
  52. }
  53. res.scope = scope;
  54. }
  55. if (import.meta.main) main({ RULE, check });