All files / packages/configs/src/eslint/rules disableNeedsReason.ts

100% Statements 37/37
95.83% Branches 23/24
100% Functions 13/13
100% Lines 31/31

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                              2x     2x   2x 21x 21x 20x 20x       22x             2x 5x     2x 12x   4x   2x 17x     17x     2x                                 17x 34x 34x 20x 14x 14x 14x 14x   12x   4x         17x   17x     16x   17x              
import type { AST as ESLintAST, Rule } from "eslint";
import type { AST } from "vue-eslint-parser";
 
interface Comment {
    value: string;
    loc: ESLintAST.SourceLocation;
}
 
interface Directive {
    kind: string;
    rules: string[];
    reason: string;
    loc: ESLintAST.SourceLocation;
}
 
const DIRECTIVE = /^\s*eslint-(disable-next-line|disable-line|disable|enable)(?=\s|$)/;
 
// ESLint itself splits a directive's description off at whitespace, two or more dashes, whitespace.
const DESCRIPTION = /\s-{2,}\s/;
 
const parse = ({ value, loc }: Comment): Directive | undefined => {
    const match = DIRECTIVE.exec(value);
    if (!match?.[1]) return undefined;
    const [list = "", ...reason] = ` ${value.slice(match[0].length)} `.split(DESCRIPTION);
    return {
        kind: match[1],
        rules: list
            .split(",")
            .map((rule) => rule.trim())
            .filter(Boolean),
        reason: reason.join(" ").trim(),
        loc,
    };
};
 
const isAfter = (a: ESLintAST.SourceLocation, b: ESLintAST.SourceLocation): boolean =>
    a.start.line > b.start.line ||
    (a.start.line === b.start.line && a.start.column > b.start.column);
 
const closes = (enable: Directive, disable: Directive): boolean =>
    enable.kind === "enable" &&
    isAfter(enable.loc, disable.loc) &&
    (enable.rules.length === 0 || disable.rules.every((rule) => enable.rules.includes(rule)));
 
const templateComments = (context: Rule.RuleContext): Comment[] => {
    const { templateBody } = context.sourceCode.ast as {
        templateBody?: AST.VElement & AST.HasConcreteInfo;
    };
    return (templateBody?.comments ?? []).filter(({ type }) => type === "HTMLComment");
};
 
const disableNeedsReason: Rule.RuleModule = {
    meta: {
        type: "problem",
        docs: {
            description:
                "Require every eslint-disable comment to name its rules, give a reason and, as a block, be closed.",
        },
        schema: [],
        messages: {
            unnamed:
                "Name the rule this silences: `eslint-disable-next-line <rule> -- <why>`; a blanket disable also hides every other rule's report.",
            reason: "Say why after ` -- `: `eslint-disable-next-line <rule> -- <why this case is safe>`.",
            unclosed:
                "Close this with `eslint-enable <rule>` after the lines it covers, or use `eslint-disable-next-line`; left open, it silences the rest of the file.",
        },
    },
    create: (context) => {
        const check = (comments: Comment[]): void => {
            const directives = comments.map(parse).filter((directive) => directive !== undefined);
            for (const directive of directives) {
                if (directive.kind === "enable") continue;
                const { loc } = directive;
                if (directive.rules.length === 0) context.report({ loc, messageId: "unnamed" });
                if (!directive.reason) context.report({ loc, messageId: "reason" });
                if (
                    directive.kind === "disable" &&
                    !directives.some((enable) => closes(enable, directive))
                ) {
                    context.report({ loc, messageId: "unclosed" });
                }
            }
        };
 
        return {
            Program: () => {
                check(
                    context.sourceCode
                        .getAllComments()
                        .flatMap(({ value, loc }) => (loc ? [{ value, loc }] : [])),
                );
                check(templateComments(context));
            },
        };
    },
};
 
export default disableNeedsReason;