lib/util/apply-disable-directives.js
function applyDirectives(options) {
    const problems = [];
    let nextDirectiveIndex = 0;
    let currentGlobalDisableDirective = null;
    const disabledRuleMap = new Map();

    // enabledRules is only used when there is a current global disable directive.
    const enabledRules = new Set();
    const usedDisableDirectives = new Set();

    for (const problem of options.problems) {
        while (
            nextDirectiveIndex < options.directives.length &&
            compareLocations(options.directives[nextDirectiveIndex], problem) <= 0
        ) {
            const directive = options.directives[nextDirectiveIndex++];

            switch (directive.type) {
                case "disable":
                    if (directive.ruleId === null) {
                        currentGlobalDisableDirective = directive;
                        disabledRuleMap.clear();
                        enabledRules.clear();
                    } else if (currentGlobalDisableDirective) {
                        enabledRules.delete(directive.ruleId);
                        disabledRuleMap.set(directive.ruleId, directive);
                    } else {
                        disabledRuleMap.set(directive.ruleId, directive);
                    }
                    break;

                case "enable":
                    if (directive.ruleId === null) {
                        currentGlobalDisableDirective = null;
                        disabledRuleMap.clear();
                    } else if (currentGlobalDisableDirective) {
                        enabledRules.add(directive.ruleId);
                        disabledRuleMap.delete(directive.ruleId);
                    } else {
                        disabledRuleMap.delete(directive.ruleId);
                    }
                    break;

                // no default
            }
        }

        if (disabledRuleMap.has(problem.ruleId)) {
            usedDisableDirectives.add(disabledRuleMap.get(problem.ruleId));
        } else if (currentGlobalDisableDirective && !enabledRules.has(problem.ruleId)) {
            usedDisableDirectives.add(currentGlobalDisableDirective);
        } else {
            problems.push(problem);
        }
    }

    const unusedDisableDirectives = options.directives
        .filter(directive => directive.type === "disable" && !usedDisableDirectives.has(directive))
        .map(directive => ({
            ruleId: null,
            message: directive.ruleId
                ? `Unused eslint-disable directive (no problems were reported from '${directive.ruleId}').`
                : "Unused eslint-disable directive (no problems were reported).",
            line: directive.unprocessedDirective.line,
            column: directive.unprocessedDirective.column,
            severity: 2,
            nodeType: null
        }));

    return { problems, unusedDisableDirectives };
}
function applyDirectives(options) {
    const problems = [];
    let nextDirectiveIndex = 0;
    let currentGlobalDisableDirective = null;
    const disabledRuleMap = new Map();

    // enabledRules is only used when there is a current global disable directive.
    const enabledRules = new Set();
    const usedDisableDirectives = new Set();

    for (const problem of options.problems) {
        while (
            nextDirectiveIndex < options.directives.length &&
            compareLocations(options.directives[nextDirectiveIndex], problem) <= 0
        ) {
            const directive = options.directives[nextDirectiveIndex++];

            switch (directive.type) {
                case "disable":
                    if (directive.ruleId === null) {
                        currentGlobalDisableDirective = directive;
                        disabledRuleMap.clear();
                        enabledRules.clear();
                    } else if (currentGlobalDisableDirective) {
                        enabledRules.delete(directive.ruleId);
                        disabledRuleMap.set(directive.ruleId, directive);
                    } else {
                        disabledRuleMap.set(directive.ruleId, directive);
                    }
                    break;

                case "enable":
                    if (directive.ruleId === null) {
                        currentGlobalDisableDirective = null;
                        disabledRuleMap.clear();
                    } else if (currentGlobalDisableDirective) {
                        enabledRules.add(directive.ruleId);
                        disabledRuleMap.delete(directive.ruleId);
                    } else {
                        disabledRuleMap.delete(directive.ruleId);
                    }
                    break;

                // no default
            }
        }

        if (disabledRuleMap.has(problem.ruleId)) {
            usedDisableDirectives.add(disabledRuleMap.get(problem.ruleId));
        } else if (currentGlobalDisableDirective && !enabledRules.has(problem.ruleId)) {
            usedDisableDirectives.add(currentGlobalDisableDirective);
        } else {
            problems.push(problem);
        }
    }

    const unusedDisableDirectives = options.directives
        .filter(directive => directive.type === "disable" && !usedDisableDirectives.has(directive))
        .map(directive => ({
            ruleId: null,
            message: directive.ruleId
                ? `Unused eslint-disable directive (no problems were reported from '${directive.ruleId}').`
                : "Unused eslint-disable directive (no problems were reported).",
            line: directive.unprocessedDirective.line,
            column: directive.unprocessedDirective.column,
            severity: 2,
            nodeType: null
        }));

    return { problems, unusedDisableDirectives };
}
function applyDirectives ( options ) { const problems = [ ] ; let nextDirectiveIndex = 0 ; let currentGlobalDisableDirective = null ; const disabledRuleMap = new Map ( ) ; // enabledRules is only used when there is a current global disable directive. const enabledRules = new Set ( ) ; const usedDisableDirectives = new Set ( ) ; for ( const problem of options . problems ) { while ( nextDirectiveIndex < options . directives . length && compareLocations ( options . directives [ nextDirectiveIndex ] , problem ) <= 0 ) { const directive = options . directives [ nextDirectiveIndex ++ ] ; switch ( directive . type ) { case "disable" : if ( directive . ruleId === null ) { currentGlobalDisableDirective = directive ; disabledRuleMap . clear ( ) ; enabledRules . clear ( ) ; } else if ( currentGlobalDisableDirective ) { enabledRules . delete ( directive . ruleId ) ; disabledRuleMap . set ( directive . ruleId , directive ) ; } else { disabledRuleMap . set ( directive . ruleId , directive ) ; } break ; case "enable" : if ( directive . ruleId === null ) { currentGlobalDisableDirective = null ; disabledRuleMap . clear ( ) ; } else if ( currentGlobalDisableDirective ) { enabledRules . add ( directive . ruleId ) ; disabledRuleMap . delete ( directive . ruleId ) ; } else { disabledRuleMap . delete ( directive . ruleId ) ; } break ; // no default } } if ( disabledRuleMap . has ( problem . ruleId ) ) { usedDisableDirectives . add ( disabledRuleMap . get ( problem . ruleId ) ) ; } else if ( currentGlobalDisableDirective && ! enabledRules . has ( problem . ruleId ) ) { usedDisableDirectives . add ( currentGlobalDisableDirective ) ; } else { problems . push ( problem ) ; } } const unusedDisableDirectives = options . directives . filter ( directive => directive . type === "disable" && ! usedDisableDirectives . has ( directive ) ) . map ( directive => ( { ruleId : null , message : directive . ruleId ? ` ${ directive . ruleId } ` : "Unused eslint-disable directive (no problems were reported)." , line : directive . unprocessedDirective . line , column : directive . unprocessedDirective . column , severity : 2 , nodeType : null } ) ) ; return { problems , unusedDisableDirectives } ; }
This is the same as the exported function, except that it
doesn't handle disable-line and disable-next-line directives, and it always reports unused
disable directives.
@param {Object} options options for applying directives. This is the same as the options
for the exported function, except that `reportUnusedDisableDirectives` is not supported
(this function always reports unused disable directives).
@returns {{problems: Problem[], unusedDisableDirectives: Problem[]}} An object with a list
of filtered problems and unused eslint-disable directives
This is the same as the exported function except that it doesn t handle disable - line and disable - next - line directives and it always reports unused disable directives .