// Rules-as-data validator: `runRules` is generic over any rule list and any // record list — it doesn't know anything about provinces/countries/ // encoding specifically. Each rule carries an `id`, a `level` // (error/warn/info), a `when` guard, a `check`, a `message` builder, and a // `note` explaining WHY the rule exists (rationale is mandatory so a future // reader never has to guess whether a rule is load-bearing or someone's // guess). // // A rule's `check` returning `true` means "this record is FINE" (no // finding); returning `false` produces one finding. This mirrors how the // concrete rules below read most naturally ("is the owner tag defined?" // rather than "is the owner tag undefined?"). /** * @typedef {Object} Rule * @property {string} id * @property {'error'|'warn'|'info'} level * @property {(record: any, ctx: any) => boolean} when - whether this rule applies to this record at all * @property {(record: any, ctx: any) => boolean} check - true = OK, false = finding * @property {(record: any, ctx: any) => string} message * @property {string} note - rationale for why this rule exists */ /** * @typedef {Object} Finding * @property {string} id * @property {'error'|'warn'|'info'} level * @property {string} message * @property {string} [file] * @property {number} [line] * @property {number} [col] */ /** * @param {Rule[]} rules * @param {any[]} records * @param {any} ctx * @returns {Finding[]} */ export function runRules(rules, records, ctx = {}) { const findings = []; for (const rule of rules) { for (const record of records) { if (!rule.when(record, ctx)) continue; if (rule.check(record, ctx)) continue; const finding = { id: rule.id, level: rule.level, message: rule.message(record, ctx) }; const position = locateRecord(record); if (position) Object.assign(finding, position); findings.push(finding); } } return findings; } // Records passed to runRules are usually { node, id, ... } shapes (a // sugared or plain Node plus whatever metadata a caller attached) — if a // `.node` with a `.raw`/`.file` is present, surface file:line:col // automatically so individual rules never have to do this themselves. function locateRecord(record) { const node = record?.node; if (!node) return null; try { const raw = node.raw; const file = node.file; if (!raw || !file) return null; return { file: file.path, line: raw.line, col: raw.col }; } catch { return null; } } /** * Summarize findings by level, for the CLI's summary line. * @param {Finding[]} findings */ export function summarize(findings) { const summary = { error: 0, warn: 0, info: 0 }; for (const f of findings) summary[f.level] += 1; return summary; } /** * Generic "does this ref field's value resolve in its defined-set" rule * generator, driven entirely by a registry's `refs` declaration — this is * what backs the error-level "owner-tag-defined" / * "culture-defined" / etc. checks for EVERY registry, without any * per-field rule being hand-written. One rule id per (registry, field) * pair is generated so CLI output can still group/report per field. * * Expects `ctx.definedSets` (from src/refs.mjs's buildDefinedSets) and * records shaped `{ node, id, field, value, target, ok }` — i.e. one * record PER (record, field, value) triple, matching the flat shape * `buildRefGraph`'s `forward` array already produces. This function * doesn't build that array itself (buildRefGraph does); it only turns * each entry into a rule check. * * @param {string} registryAs * @param {import('./registry.mjs').RegistryDescriptor} registry * @returns {Rule[]} */ export function refFieldRules(registry) { return Object.keys(registry.refs ?? {}).map((field) => ({ id: `${registry.as}-${field}-defined`, level: 'error', when: (fwd) => fwd.registry === registry.as && fwd.field === field, check: (fwd) => fwd.ok, message: (fwd) => `${registry.as} ${fwd.id}: ${field} "${fwd.value}" is not a defined ${fwd.target}`, note: 'An undefined reference means the game silently drops the field at start — ' + 'this is the exact shape of the write_country_support/write_history incident ' + '(122+ provinces lost their owner when common/country_tags was not copied).', })); }