файл пакета→ .shtukbase/packages/contours/checks/c02_contour.ts
checks/c02_contour.ts
40 строк, 2.8 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // C2. A contour is connected as a whole: the contour collection exists; every phase from `phases` is a page of
- // `type: Phase` in the contour folder; the entry (`entry`) is one of its phases; the exits of phases (`next`) lead
- // to phases of the same contour. The rule base-r09 "A contour is connected as a whole" of the standard package base.
- import { addr, ctx, hole, values } from '../../collections/checks/lib/schemalib.ts';
- import { type Project, type Result, pystr, truthy } from '../../collections/checks/lib/speclib.ts';
- import { contourPages, exits, headOf, main, norm, phases } from './contourlib.ts';
- export const RULE = { id: 'c02', title: 'Contours: a contour is connected as a whole', scope: 'all pages of type: Contour and their phases' };
- export function check(p: Project, res: Result): void {
- const c = ctx(p);
- const pages = contourPages(c);
- res.scope = pages.map((x) => addr(x + '.md'));
- if (!res.part('contours', pages.length ? null : 'the project has no contours')) return;
- const colls = new Set(c.collections());
- for (const cp of pages) {
- const a = addr(cp + '.md');
- const h = headOf(c.root, cp + '.md');
- const coll = norm(h.collection);
- 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');
- const have = phases(c.root, cp) ?? new Map();
- const own = new Set([...have.keys()].map((n) => cp + '/' + n));
- const listed = values(truthy(h.phases) ? h.phases : []).map(norm);
- for (const ph of listed)
- 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');
- const known = new Set([...own, ...listed]); // a listed phase without a page is reported once, above
- for (const [n, ph] of [...have.entries()].sort((x, y) => (x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : 0))) {
- if (pystr(truthy(ph.type) ? ph.type : '').trim() !== 'Phase')
- 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');
- if (listed.length && !listed.includes(cp + '/' + n))
- hole(res, a, 'phases', 'unlisted-phase', `phase ${n} is in the contour folder, but is not named in phases`, 'add the phase to phases');
- for (const to of exits(ph))
- 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');
- }
- const entry = norm(h.entry);
- 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');
- }
- }
- if (import.meta.main) main({ RULE, check });