Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 48x 835x 48x 495x 495x 1x 495x 1x 495x 110x 495x 2x 495x 48x 191x 272x 191x 438x 191x 191x 68x 1x 272x 438x 191x 191x 191x 191x 495x 48x 191x 191x 191x 191x 272x 272x 272x 272x 191x 68x 68x 68x 68x 68x 191x 438x 191x 57x 191x 191x 48x 4x | import { cycleProblem, readCapability, withStageNeeds } from "./capability.ts";
import { STAGE_NEED_PREFIX } from "./constants.ts";
import { isRecord, Reader } from "./reader.ts";
import type { Capability } from "./types/Capability.ts";
import type { Template } from "./types/Template.ts";
const duplicates = (ids: string[]): string[] =>
[...new Set(ids.filter((id, index) => ids.indexOf(id) !== index))].sort((a, b) =>
a.localeCompare(b),
);
const referenceProblems = (
capability: Capability,
known: { stageIds: Set<string>; groupIds: Set<string>; capabilityIds: Set<string> },
): string[] => {
const problems: string[] = [];
if (capability.stage && !known.stageIds.has(capability.stage)) {
problems.push(
`capability \`${capability.id}\` names stage \`${capability.stage}\`, which the template doesn't have`,
);
}
if (capability.group && !known.groupIds.has(capability.group)) {
problems.push(
`capability \`${capability.id}\` names group \`${capability.group}\`, which the template doesn't have`,
);
}
const isKnown = (id: string) =>
known.capabilityIds.has(id) ||
(id.startsWith(STAGE_NEED_PREFIX) &&
known.stageIds.has(id.slice(STAGE_NEED_PREFIX.length)));
for (const dependency of capability.dependsOn.filter((id) => !isKnown(id))) {
problems.push(
`capability \`${capability.id}\` depends on \`${dependency}\`, which the template doesn't have`,
);
}
return problems;
};
const templateRuleProblems = ({ stages, groups, capabilities, types }: Template): string[] => {
const problems: string[] = [];
const stageIds = new Set(stages.map((stage) => stage.id));
const groupIds = new Set(groups.map((group) => group.id));
const capabilityIds = new Set(capabilities.map((capability) => capability.id));
for (const id of duplicates(groups.map((group) => group.id))) {
problems.push(`group \`${id}\` is declared twice`);
}
for (const id of groupIds) {
if (capabilityIds.has(id)) {
problems.push(
`group \`${id}\` shares its id with a capability, so a switch can't tell them apart`,
);
}
}
for (const id of duplicates(stages.map((stage) => stage.id))) {
problems.push(`stage \`${id}\` is declared twice`);
}
for (const id of duplicates(capabilities.map((capability) => capability.id))) {
problems.push(`capability \`${id}\` is declared twice`);
}
for (const id of duplicates(types.map((type) => type.id))) {
problems.push(`type \`${id}\` is declared twice`);
}
for (const type of types.filter(({ id }) => capabilityIds.has(id))) {
problems.push(`type \`${type.id}\` shares its id with a capability`);
}
const known = { stageIds, groupIds, capabilityIds };
return [
...problems,
...[...capabilities, ...types].flatMap((capability) =>
referenceProblems(capability, known),
),
...cycleProblem(withStageNeeds(capabilities)),
];
};
/** A template read from parsed YAML, and everything about it that breaks the rules. */
export const readTemplate = (raw: unknown): { template: Template; problems: string[] } => {
const reader = new Reader();
const source = isRecord(raw) ? raw : {};
Iif (!isRecord(raw)) reader.problems.push("the file isn't a mapping");
const stages = reader.list(source, "stages", "template").flatMap((stage, index) => {
Iif (!isRecord(stage)) {
reader.problems.push(`stage #${index + 1} isn't a mapping`);
return [];
}
const id = reader.text(stage, "id", `stage #${index + 1}`);
const where = `stage \`${id || index + 1}\``;
return [
{
id,
title: reader.text(stage, "title", where),
goal: reader.text(stage, "goal", where, ""),
},
];
});
const groups = reader.list(source, "groups", "template").flatMap((group, index) => {
Iif (!isRecord(group)) {
reader.problems.push(`group #${index + 1} isn't a mapping`);
return [];
}
const id = reader.text(group, "id", `group #${index + 1}`);
const where = `group \`${id || index + 1}\``;
const optional = group.optional === true;
return [
{
id,
title: reader.text(group, "title", where),
summary: reader.text(group, "summary", where, ""),
optional,
default: optional ? group.default === true : true,
},
];
});
const capabilities = reader
.list(source, "capabilities", "template")
.flatMap((capability, index) => readCapability(reader, capability, index) ?? []);
const types = reader
.list(source, "types", "template")
.flatMap((type, index) => readCapability(reader, type, index) ?? []);
const template: Template = {
id: reader.text(source, "id", "template"),
title: reader.text(source, "title", "template"),
summary: reader.text(source, "summary", "template", ""),
stages,
groups,
capabilities,
types,
};
return { template, problems: [...reader.problems, ...templateRuleProblems(template)] };
};
/** Lessons written into the templates, on fixed and repeatable blocks alike. */
export const lessonCount = (templates: readonly Template[]): number =>
templates.reduce(
(sum, template) =>
[...template.capabilities, ...template.types].reduce(
(count, block) => count + block.lessons.length,
sum,
),
0,
);
|