function getDirectiveComments(filename, ast, ruleMapper) {
    const configuredRules = {};
    const enabledGlobals = {};
    const exportedVariables = {};
    const problems = [];
    const disableDirectives = [];

    ast.comments.filter(token => token.type !== "Shebang").forEach(comment => {
        const trimmedCommentText = comment.value.trim();
        const match = /^(eslint(-\w+){0,3}|exported|globals?)(\s|$)/u.exec(trimmedCommentText);

        if (!match) {
            return;
        }

        const directiveValue = trimmedCommentText.slice(match.index + match[1].length);

        if (/^eslint-disable-(next-)?line$/u.test(match[1])) {
            if (comment.loc.start.line === comment.loc.end.line) {
                const directiveType = match[1].slice("eslint-".length);

                disableDirectives.push(...createDisableDirectives(directiveType, comment.loc.start, directiveValue));
            } else {
                problems.push({
                    ruleId: null,
                    severity: 2,
                    message: `${match[1]} comment should not span multiple lines.`,
                    line: comment.loc.start.line,
                    column: comment.loc.start.column + 1,
                    endLine: comment.loc.end.line,
                    endColumn: comment.loc.end.column + 1,
                    nodeType: null
                });
            }
        } else if (comment.type === "Block") {
            switch (match[1]) {
                case "exported":
                    Object.assign(exportedVariables, commentParser.parseStringConfig(directiveValue, comment));
                    break;

                case "globals":
                case "global":
                    for (const [id, { value }] of Object.entries(commentParser.parseStringConfig(directiveValue, comment))) {
                        let normalizedValue;

                        try {
                            normalizedValue = ConfigOps.normalizeConfigGlobal(value);
                        } catch (err) {
                            problems.push({
                                ruleId: null,
                                severity: 2,
                                message: err.message,
                                line: comment.loc.start.line,
                                column: comment.loc.start.column + 1,
                                endLine: comment.loc.end.line,
                                endColumn: comment.loc.end.column + 1,
                                nodeType: null
                            });
                            continue;
                        }

                        if (enabledGlobals[id]) {
                            enabledGlobals[id].comments.push(comment);
                            enabledGlobals[id].value = normalizedValue;
                        } else {
                            enabledGlobals[id] = {
                                comments: [comment],
                                value: normalizedValue
                            };
                        }
                    }
                    break;

                case "eslint-disable":
                    disableDirectives.push(...createDisableDirectives("disable", comment.loc.start, directiveValue));
                    break;

                case "eslint-enable":
                    disableDirectives.push(...createDisableDirectives("enable", comment.loc.start, directiveValue));
                    break;

                case "eslint": {
                    const parseResult = commentParser.parseJsonConfig(directiveValue, comment.loc);

                    if (parseResult.success) {
                        Object.keys(parseResult.config).forEach(name => {
                            const ruleValue = parseResult.config[name];

                            try {
                                validator.validateRuleOptions(ruleMapper(name), name, ruleValue);
                            } catch (err) {
                                problems.push({
                                    ruleId: name,
                                    severity: 2,
                                    message: err.message,
                                    line: comment.loc.start.line,
                                    column: comment.loc.start.column + 1,
                                    endLine: comment.loc.end.line,
                                    endColumn: comment.loc.end.column + 1,
                                    nodeType: null
                                });
                            }
                            configuredRules[name] = ruleValue;
                        });
                    } else {
                        problems.push(parseResult.error);
                    }

                    break;
                }

                // no default
            }
        }
    });

    return {
        configuredRules,
        enabledGlobals,
        exportedVariables,
        problems,
        disableDirectives
    };
}
function getDirectiveComments(filename, ast, ruleMapper) {
    const configuredRules = {};
    const enabledGlobals = {};
    const exportedVariables = {};
    const problems = [];
    const disableDirectives = [];

    ast.comments.filter(token => token.type !== "Shebang").forEach(comment => {
        const trimmedCommentText = comment.value.trim();
        const match = /^(eslint(-\w+){0,3}|exported|globals?)(\s|$)/u.exec(trimmedCommentText);

        if (!match) {
            return;
        }

        const directiveValue = trimmedCommentText.slice(match.index + match[1].length);

        if (/^eslint-disable-(next-)?line$/u.test(match[1])) {
            if (comment.loc.start.line === comment.loc.end.line) {
                const directiveType = match[1].slice("eslint-".length);

                disableDirectives.push(...createDisableDirectives(directiveType, comment.loc.start, directiveValue));
            } else {
                problems.push({
                    ruleId: null,
                    severity: 2,
                    message: `${match[1]} comment should not span multiple lines.`,
                    line: comment.loc.start.line,
                    column: comment.loc.start.column + 1,
                    endLine: comment.loc.end.line,
                    endColumn: comment.loc.end.column + 1,
                    nodeType: null
                });
            }
        } else if (comment.type === "Block") {
            switch (match[1]) {
                case "exported":
                    Object.assign(exportedVariables, commentParser.parseStringConfig(directiveValue, comment));
                    break;

                case "globals":
                case "global":
                    for (const [id, { value }] of Object.entries(commentParser.parseStringConfig(directiveValue, comment))) {
                        let normalizedValue;

                        try {
                            normalizedValue = ConfigOps.normalizeConfigGlobal(value);
                        } catch (err) {
                            problems.push({
                                ruleId: null,
                                severity: 2,
                                message: err.message,
                                line: comment.loc.start.line,
                                column: comment.loc.start.column + 1,
                                endLine: comment.loc.end.line,
                                endColumn: comment.loc.end.column + 1,
                                nodeType: null
                            });
                            continue;
                        }

                        if (enabledGlobals[id]) {
                            enabledGlobals[id].comments.push(comment);
                            enabledGlobals[id].value = normalizedValue;
                        } else {
                            enabledGlobals[id] = {
                                comments: [comment],
                                value: normalizedValue
                            };
                        }
                    }
                    break;

                case "eslint-disable":
                    disableDirectives.push(...createDisableDirectives("disable", comment.loc.start, directiveValue));
                    break;

                case "eslint-enable":
                    disableDirectives.push(...createDisableDirectives("enable", comment.loc.start, directiveValue));
                    break;

                case "eslint": {
                    const parseResult = commentParser.parseJsonConfig(directiveValue, comment.loc);

                    if (parseResult.success) {
                        Object.keys(parseResult.config).forEach(name => {
                            const ruleValue = parseResult.config[name];

                            try {
                                validator.validateRuleOptions(ruleMapper(name), name, ruleValue);
                            } catch (err) {
                                problems.push({
                                    ruleId: name,
                                    severity: 2,
                                    message: err.message,
                                    line: comment.loc.start.line,
                                    column: comment.loc.start.column + 1,
                                    endLine: comment.loc.end.line,
                                    endColumn: comment.loc.end.column + 1,
                                    nodeType: null
                                });
                            }
                            configuredRules[name] = ruleValue;
                        });
                    } else {
                        problems.push(parseResult.error);
                    }

                    break;
                }

                // no default
            }
        }
    });

    return {
        configuredRules,
        enabledGlobals,
        exportedVariables,
        problems,
        disableDirectives
    };
}
function getDirectiveComments ( filename , ast , ruleMapper ) { const configuredRules = { } ; const enabledGlobals = { } ; const exportedVariables = { } ; const problems = [ ] ; const disableDirectives = [ ] ; ast . comments . filter ( token => token . type !== "Shebang" ) . forEach ( comment => { const trimmedCommentText = comment . value . trim ( ) ; const match = / ^(eslint(-\w+){0,3}|exported|globals?)(\s|$) / u . exec ( trimmedCommentText ) ; if ( ! match ) { return ; } const directiveValue = trimmedCommentText . slice ( match . index + match [ 1 ] . length ) ; if ( / ^eslint-disable-(next-)?line$ / u . test ( match [ 1 ] ) ) { if ( comment . loc . start . line === comment . loc . end . line ) { const directiveType = match [ 1 ] . slice ( "eslint-" . length ) ; disableDirectives . push ( ... createDisableDirectives ( directiveType , comment . loc . start , directiveValue ) ) ; } else { problems . push ( { ruleId : null , severity : 2 , message : ` ${ match [ 1 ] } ` , line : comment . loc . start . line , column : comment . loc . start . column + 1 , endLine : comment . loc . end . line , endColumn : comment . loc . end . column + 1 , nodeType : null } ) ; } } else if ( comment . type === "Block" ) { switch ( match [ 1 ] ) { case "exported" : Object . assign ( exportedVariables , commentParser . parseStringConfig ( directiveValue , comment ) ) ; break ; case "globals" : case "global" : for ( const [ id , { value } ] of Object . entries ( commentParser . parseStringConfig ( directiveValue , comment ) ) ) { let normalizedValue ; try { normalizedValue = ConfigOps . normalizeConfigGlobal ( value ) ; } catch ( err ) { problems . push ( { ruleId : null , severity : 2 , message : err . message , line : comment . loc . start . line , column : comment . loc . start . column + 1 , endLine : comment . loc . end . line , endColumn : comment . loc . end . column + 1 , nodeType : null } ) ; continue ; } if ( enabledGlobals [ id ] ) { enabledGlobals [ id ] . comments . push ( comment ) ; enabledGlobals [ id ] . value = normalizedValue ; } else { enabledGlobals [ id ] = { comments : [ comment ] , value : normalizedValue } ; } } break ; case "eslint-disable" : disableDirectives . push ( ... createDisableDirectives ( "disable" , comment . loc . start , directiveValue ) ) ; break ; case "eslint-enable" : disableDirectives . push ( ... createDisableDirectives ( "enable" , comment . loc . start , directiveValue ) ) ; break ; case "eslint" : { const parseResult = commentParser . parseJsonConfig ( directiveValue , comment . loc ) ; if ( parseResult . success ) { Object . keys ( parseResult . config ) . forEach ( name => { const ruleValue = parseResult . config [ name ] ; try { validator . validateRuleOptions ( ruleMapper ( name ) , name , ruleValue ) ; } catch ( err ) { problems . push ( { ruleId : name , severity : 2 , message : err . message , line : comment . loc . start . line , column : comment . loc . start . column + 1 , endLine : comment . loc . end . line , endColumn : comment . loc . end . column + 1 , nodeType : null } ) ; } configuredRules [ name ] = ruleValue ; } ) ; } else { problems . push ( parseResult . error ) ; } break ; } // no default } } } ) ; return { configuredRules , enabledGlobals , exportedVariables , problems , disableDirectives } ; }
Parses comments in file to extract file-specific config of rules, globals
and environments and merges them with global config; also code blocks
where reporting is disabled or enabled and merges them with reporting config.
@param {string} filename The file being checked.
@param {ASTNode} ast The top node of the AST.
@param {function(string): {create: Function}} ruleMapper A map from rule IDs to defined rules
@returns {{configuredRules: Object, enabledGlobals: {value:string,comment:Token}[], exportedVariables: Object, problems: Problem[], disableDirectives: DisableDirective[]}}
A collection of the directive comments that were found, along with any problems that occurred when parsing
Parses comments in file to extract file - specific config of rules globals and environments and merges them with global config ; also code blocks where reporting is disabled or enabled and merges them with reporting config .
Get all directives from the given file