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

96.29% Statements 26/27
87.5% Branches 14/16
100% Functions 4/4
100% Lines 21/21

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                2x   2x 5x   5x   5x   5x 5x 5x 1x   5x   5x   5x   5x 5x 5x   5x 1x     5x       2x 5x 6x    
import { FINDING_STATUSES, parseFindings } from "#layers/snapshot/shared/audits.ts";
import { frontmatterFields } from "./decisions.ts";
 
interface AuditProblem {
    file: string;
    message: string;
}
 
const FILENAME = /^\d{4}-\d{2}-\d{2}-[a-z0-9]+(?:-[a-z0-9]+)*\.md$/;
 
const problemsOf = (file: string, source: string): string[] => {
    const problems: string[] = [];
 
    if (!FILENAME.test(file)) problems.push("filename doesn't match <YYYY-MM-DD>-<scope-slug>.md");
 
    const fields = frontmatterFields(source);
 
    Iif (fields === null) return [...problems, "frontmatter isn't valid YAML"];
    if (!fields.scope) problems.push("frontmatter is missing `scope:`");
    if (!/^[0-9a-f]{7,40}$/.test(fields.commit ?? ""))
        problems.push("frontmatter is missing a short-sha `commit:`");
 
    const findings = parseFindings(source);
 
    if (findings.length === 0) problems.push("no table with an `ID` and a `Status` column");
 
    const seen = new Set<string>();
 
    for (const finding of findings) {
        if (seen.has(finding.id)) problems.push(`${finding.id} is used twice`);
        seen.add(finding.id);
 
        if (finding.status === null)
            problems.push(`${finding.id}'s status isn't one of ${FINDING_STATUSES.join(", ")}`);
    }
 
    return problems;
};
 
/** Every rule `docs/audits/README.md` states about an audit's filename, frontmatter and findings tables. */
export const auditProblems = (audits: { file: string; source: string }[]): AuditProblem[] =>
    audits.flatMap(({ file, source }) =>
        problemsOf(file, source).map((message) => ({ file, message })),
    );