All files / apps/developer-portal/layers/snapshot/shared audits.ts

100% Statements 61/61
91.89% Branches 34/37
100% Functions 10/10
100% Lines 52/52

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  38x 38x                             38x 38x   38x 26x         78x   38x             38x 32x   11x     38x 11x   38x         15x   12x 15x   11x 15x   15x                 38x 8x 8x 8x 8x   8x 113x 2x 2x     111x   108x   108x 9x 9x 9x     99x 62x 62x     37x   26x   26x 11x 11x     15x 15x     8x       38x 12x 20x     12x 7x   4x    
/** The vocabulary `docs/audits/README.md` documents, as the one copy every other spelling of it derives from. */
export const FINDING_STATUSES = ["open", "fixed", "wont-fix"] as const;
export const AUDIT_STATES = ["open", "in-progress", "closed"] as const;
 
export type FindingStatus = (typeof FINDING_STATUSES)[number];
export type AuditState = (typeof AUDIT_STATES)[number];
 
export interface AuditFinding {
    id: string;
    /** The `##` heading the finding's table sits under, emoji stripped — `Bugs`, `Duplication`. */
    category: string;
    /** Null when the cell's first word isn't in the vocabulary, which `auditProblems` reports. */
    status: FindingStatus | null;
    /** Whatever follows the status word — `fixed #152` carries `#152`. */
    ref: string | null;
}
 
const FENCE = /^(?:```|~~~)/;
const HEADING = /^##[^\S\n]+(\S.*)$/;
 
const cellsOf = (line: string): string[] =>
    line
        .trim()
        .replace(/^\|/, "")
        .replace(/\|$/, "")
        .split(/(?<!\\)\|/)
        .map((cell) => cell.trim());
 
const isSeparator = (line: string): boolean => /^\|?\s*:?-{3,}/.test(line.trim());
 
interface FindingColumns {
    id: number;
    status: number;
}
 
const findingColumns = (cells: string[]): FindingColumns => {
    const header = cells.map((cell) => cell.toLowerCase());
 
    return { id: header.indexOf("id"), status: header.indexOf("status") };
};
 
const isFindingStatus = (value: string): value is FindingStatus =>
    (FINDING_STATUSES as readonly string[]).includes(value);
 
const findingOf = (
    cells: string[],
    columns: FindingColumns,
    category: string,
): AuditFinding | null => {
    if (columns.id === -1 || columns.status === -1) return null;
 
    const id = (cells[columns.id] ?? "").replace(/[*`]/g, "").trim();
    if (!id) return null;
 
    const [word = "", ...rest] = (cells[columns.status] ?? "").split(/\s+/);
    const status = word.toLowerCase();
 
    return {
        id,
        category,
        status: isFindingStatus(status) ? status : null,
        ref: rest.join(" ") || null,
    };
};
 
/** Every row of every table whose header has both an `ID` and a `Status` column; any other table is prose. */
export const parseFindings = (markdown: string): AuditFinding[] => {
    const findings: AuditFinding[] = [];
    let category = "";
    let inFence = false;
    let columns: FindingColumns | null = null;
 
    for (const line of markdown.split("\n")) {
        if (FENCE.test(line.trim())) {
            inFence = !inFence;
            continue;
        }
 
        if (inFence) continue;
 
        const heading = HEADING.exec(line);
 
        if (heading) {
            category = (heading[1] ?? "").replace(/^[^\p{L}\p{N}]+/u, "").trim();
            columns = null;
            continue;
        }
 
        if (!line.trim().startsWith("|")) {
            columns = null;
            continue;
        }
 
        if (isSeparator(line)) continue;
 
        const cells = cellsOf(line);
 
        if (columns === null) {
            columns = findingColumns(cells);
            continue;
        }
 
        const finding = findingOf(cells, columns, category);
        if (finding) findings.push(finding);
    }
 
    return findings;
};
 
/** An unparsed status counts as open, so a typo keeps an audit on the list instead of quietly closing it. */
export const auditStateOf = (findings: AuditFinding[]): AuditState => {
    const open = findings.filter(
        (finding) => finding.status === "open" || finding.status === null,
    ).length;
 
    if (open === 0) return "closed";
    if (open === findings.length) return "open";
 
    return "in-progress";
};