All files / apps/developer-portal/tools/lib decisions.ts

91.83% Statements 45/49
74.35% Branches 29/39
100% Functions 7/7
95.34% Lines 41/43

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                4x 4x           4x 30x   30x   28x 28x   28x   27x   73x 73x     1x       4x 13x   13x 1x     13x   13x   13x 1x     13x 1x         13x           13x 1x   1x 1x       12x 1x     13x             4x     13x 10x 10x   10x 13x 13x   13x 11x   13x     10x    
import { parse } from "yaml";
import { DECISION_OUTCOMES, DECISION_STATUSES } from "#layers/snapshot/shared/decisions.ts";
 
interface DecisionProblem {
    file: string;
    message: string;
}
 
const FRONTMATTER = /^---\n([\s\S]*?)\n---/;
const FILENAME = /^\d{4}-[a-z0-9]+(?:-[a-z0-9]+)*\.md$/;
 
/**
 * Scalars flattened to strings, since `issue: 106` parses as a number. Null means the block is there
 * but isn't valid YAML — `@nuxt/content` parses the same bytes, so the checker must agree with it.
 */
export const frontmatterFields = (source: string): Record<string, string> | null => {
    const block = FRONTMATTER.exec(source)?.[1];
 
    if (block === undefined) return {};
 
    try {
        const parsed: unknown = parse(block);
 
        Iif (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null;
 
        return Object.fromEntries(
            Object.entries(parsed)
                .filter(([, value]) => value !== null && typeof value !== "object")
                .map(([key, value]) => [key, String(value)]),
        );
    } catch {
        return null;
    }
};
 
const reportProblems = (file: string, source: string, filed: ReadonlySet<string>): string[] => {
    const problems: string[] = [];
 
    if (!FILENAME.test(file)) {
        problems.push("filename doesn't match <NNNN>-<slug>.md");
    }
 
    const fields = frontmatterFields(source);
 
    Iif (fields === null) return [...problems, "frontmatter isn't valid YAML"];
 
    if (!/^\d+$/.test(fields.issue ?? "")) {
        problems.push("frontmatter is missing a numeric `issue:`");
    }
 
    if (!(DECISION_STATUSES as readonly string[]).includes(fields.status ?? "")) {
        problems.push(
            `\`status: ${fields.status ?? "(missing)"}\` isn't one of ${DECISION_STATUSES.join(", ")}`,
        );
    }
 
    Iif (!(DECISION_OUTCOMES as readonly string[]).includes(fields.decision ?? "")) {
        problems.push(
            `\`decision: ${fields.decision ?? "(missing)"}\` isn't one of ${DECISION_OUTCOMES.join(", ")}`,
        );
    }
 
    if (fields.decision === "superseded") {
        Iif (!fields.supersededBy) {
            problems.push("`decision: superseded` needs a `supersededBy:` file");
        } else Eif (!filed.has(fields.supersededBy)) {
            problems.push(
                `\`supersededBy: ${fields.supersededBy}\` names a file that isn't a filed decision`,
            );
        }
    } else if (fields.supersededBy) {
        problems.push("`supersededBy:` is set but `decision` isn't `superseded`");
    }
 
    return problems;
};
 
/**
 * Every rule `docs/decisions/README.md` states about a report's filename and frontmatter. A reused
 * `issue:` is allowed; a reused number is not.
 */
export const decisionProblems = (
    reports: { file: string; source: string }[],
): DecisionProblem[] => {
    const filed = new Set(reports.map((report) => report.file));
    const seen = new Map<string, string>();
    const problems: DecisionProblem[] = [];
 
    for (const { file, source } of reports) {
        const number = file.slice(0, 4);
        const taken = seen.get(number);
 
        if (taken) problems.push({ file, message: `number ${number} is already ${taken}` });
        else seen.set(number, file);
 
        problems.push(...reportProblems(file, source, filed).map((message) => ({ file, message })));
    }
 
    return problems;
};