All files / apps/developer-portal/layers/telemetry/shared lintUsage.ts

95.83% Statements 69/72
91.11% Branches 41/45
93.75% Functions 15/16
98.21% Lines 55/56

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        37x 37x   37x 37x     37x 8x   8x 51x 51x 51x 50x     8x       37x 4x 10x 8x   10x   7x 7x 7x     8x 8x     37x                   37x 4x 4x 4x   3x 3x 3x       37x           7x 7x   7x 7x 4x 4x 4x 4x 4x 4x     7x 3x 3x     7x       37x 6x 12x 5x     37x     37x     7x 7x   1x   2x      
import type { LintRuleUsage } from "#layers/snapshot/shared/types/LintRuleUsage.ts";
import type { LintUsage } from "#layers/snapshot/shared/types/LintUsage.ts";
import type { UsageRecord } from "./usage.ts";
 
export const FOUGHT_DISABLES = 3;
export const NOISY_SHARE = 0.3;
 
const DIRECTIVE = /(?:\/\/|\/\*|<!--)\s*eslint-disable(?:-next-line|-line)?\s/g;
const RULE_ID = /^@?[\w-]+(?:\/[\w-]+)*$/;
 
/** Whether the line's text up to here is still inside a string, where a `//` is only text. */
const insideString = (before: string): boolean => {
    let quote: string | null = null;
 
    for (let index = 0; index < before.length; index += 1) {
        const char = before[index]!;
        Iif (char === "\\") index += 1;
        else if (quote === char) quote = null;
        else if (quote === null && (char === '"' || char === "'" || char === "`")) quote = char;
    }
 
    return quote !== null;
};
 
/** The rule IDs each `eslint-disable` comment in a file names; a bare directive names none. */
export const parseDisables = (source: string): string[] =>
    source.split("\n").flatMap((line) => {
        const match = [...line.matchAll(DIRECTIVE)].find(
            ({ index }) => !insideString(line.slice(0, index)),
        );
        if (!match) return [];
 
        const rest = line.slice(match.index + match[0].length);
        const [names = ""] = rest.split("--");
        return names
            .replace("*/", "")
            .split(",")
            .map((name) => name.trim())
            .filter((name) => RULE_ID.test(name));
    });
 
const emptyRule = (enabled: boolean): LintRuleUsage => ({
    enabled,
    hits: 0,
    recent: 0,
    fixable: 0,
    last: null,
    projects: {},
    disabled: 0,
});
 
const tally = (usage: LintRuleUsage, { at = "", project, fixable }: UsageRecord, since: string) => {
    usage.hits += 1;
    if (!usage.last || at > usage.last) usage.last = at;
    if (at < since) return;
 
    usage.recent += 1;
    if (fixable === "1") usage.fixable += 1;
    Eif (project) usage.projects[project] = (usage.projects[project] ?? 0) + 1;
};
 
/** `since` is the first day inside the window, as `YYYY-MM-DD`. */
export const summarizeLint = (
    records: UsageRecord[],
    enabled: string[],
    disabled: Record<string, number>,
    since: string,
): LintUsage => {
    const rules: Record<string, LintRuleUsage> = {};
    for (const rule of enabled) rules[rule] = emptyRule(true);
 
    let first: string | null = null;
    for (const record of records) {
        const { at, rule } = record;
        Iif (!at || !rule) continue;
        const usage = rules[rule] ?? emptyRule(false);
        rules[rule] = usage;
        tally(usage, record, since);
        if (!first || at < first) first = at;
    }
 
    for (const [rule, count] of Object.entries(disabled)) {
        const usage = rules[rule];
        if (usage) usage.disabled = count;
    }
 
    return { since: first, rules };
};
 
/** Rules that are on, haven't fired inside the window and that nobody disables either. */
export const neverFired = (lint: LintUsage): string[] =>
    Object.entries(lint.rules)
        .filter(([, usage]) => usage.enabled && usage.recent === 0 && usage.disabled === 0)
        .map(([rule]) => rule)
        .sort((a, b) => a.localeCompare(b));
 
const IN_HOUSE = "joinery/";
 
/** How many rules are on, and how many of those this repo wrote; `null` with none collected. */
export const ruleCounts = (
    lint: LintUsage | undefined,
): { enabled: number; inHouse: number } | null => {
    const enabled = Object.entries(lint?.rules ?? {}).filter(([, usage]) => usage.enabled);
    if (!enabled.length) return null;
 
    return {
        enabled: enabled.length,
        inHouse: enabled.filter(([rule]) => rule.startsWith(IN_HOUSE)).length,
    };
};