ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/contours/checks/c02_contour.ts

checks/c02_contour.ts

40 líneas, 2.8 KB.

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

  1. // C2. A contour is connected as a whole: the contour collection exists; every phase from `phases` is a page of
  2. // `type: Phase` in the contour folder; the entry (`entry`) is one of its phases; the exits of phases (`next`) lead
  3. // to phases of the same contour. The rule base-r09 "A contour is connected as a whole" of the standard package base.
  4. import { addr, ctx, hole, values } from '../../collections/checks/lib/schemalib.ts';
  5. import { type Project, type Result, pystr, truthy } from '../../collections/checks/lib/speclib.ts';
  6. import { contourPages, exits, headOf, main, norm, phases } from './contourlib.ts';
  7. export const RULE = { id: 'c02', title: 'Contours: a contour is connected as a whole', scope: 'all pages of type: Contour and their phases' };
  8. export function check(p: Project, res: Result): void {
  9. const c = ctx(p);
  10. const pages = contourPages(c);
  11. res.scope = pages.map((x) => addr(x + '.md'));
  12. if (!res.part('contours', pages.length ? null : 'the project has no contours')) return;
  13. const colls = new Set(c.collections());
  14. for (const cp of pages) {
  15. const a = addr(cp + '.md');
  16. const h = headOf(c.root, cp + '.md');
  17. const coll = norm(h.collection);
  18. if (coll && !colls.has(coll)) hole(res, a, 'collection', 'no-collection', `the contour leads collection ${coll}, which does not exist`, 'fix collection or remove the field');
  19. const have = phases(c.root, cp) ?? new Map();
  20. const own = new Set([...have.keys()].map((n) => cp + '/' + n));
  21. const listed = values(truthy(h.phases) ? h.phases : []).map(norm);
  22. for (const ph of listed)
  23. if (!own.has(ph)) hole(res, a, 'phases', 'no-phase', `phase ${ph} is named in the contour, but there is no phase page in ${cp}/`, 'create the phase page or remove it from phases');
  24. const known = new Set([...own, ...listed]); // a listed phase without a page is reported once, above
  25. for (const [n, ph] of [...have.entries()].sort((x, y) => (x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : 0))) {
  26. if (pystr(truthy(ph.type) ? ph.type : '').trim() !== 'Phase')
  27. hole(res, addr(cp + '/' + n + '.md'), 'type', 'not-phase', 'a page in the contour folder without type: Phase', 'set type: Phase or move the page away');
  28. if (listed.length && !listed.includes(cp + '/' + n))
  29. hole(res, a, 'phases', 'unlisted-phase', `phase ${n} is in the contour folder, but is not named in phases`, 'add the phase to phases');
  30. for (const to of exits(ph))
  31. if (!known.has(to)) hole(res, addr(cp + '/' + n + '.md'), 'next', 'foreign-exit', `exit to ${to} is not a phase of the contour ${cp}`, 'lead the exit to a phase of this contour');
  32. }
  33. const entry = norm(h.entry);
  34. if (entry && !own.has(entry)) hole(res, a, 'entry', 'bad-entry', `entry ${entry} is not a phase of this contour`, 'make one of the phases the entry');
  35. }
  36. }
  37. if (import.meta.main) main({ RULE, check });