我如何获得一个自定义eslint规则来做context.在没有节点的情况下进行报告

How can I get a custom eslint rule to do context.report without having a node?

本文关键字:context 节点 报告 情况下 何获得 规则 eslint 自定义 一个      更新时间:2023-09-26

我想要一个非常简单的eslint规则,确保每个文件的顶部行都写着'ouch'。像这样的东西似乎是有效的-但我如何上下文。在没有节点的情况下报告它?或者我如何获得要使用的文件的第一行的节点?我是否应该使用在"返回"事件上声明的回调(如果是这样,哪个事件)?谢谢!

'use strict';
function checkFirstLine (context) {
    if (context.getSourceCode().lines[0] !== 'ouch') {
        // How do I context.report without a node?
    }
}
module.exports = {
    meta : {
        docs : {
            description : 'check first line',
            category : 'Possible Errors',
            recommended : true
        },
    },
    create : function (context) {
        checkFirstLine (context);
        return {};
    }
};

刚刚想好了,我可以用:

context.report(context.getSourceCode().ast, 'Cripes');