自定义eslint,并传参

本文介绍了如何配置eslint以检查TypeScript文件,并通过在eslintrc.js的rules中设置参数,指定忽略特定文件及检测特定模块,从而实现更精准的代码质量控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上篇文章,我说client.ts不报错,好像是有误的,因为只检测js文件,没检测ts文件。坐我边上的大佬说可以开检测ts的。
然后说传参,在eslintrc.js rules里面传了一个对象,包含files,哪些文件不检测,检测哪些模块
在这里插入图片描述

meta: {
        docs: {
            description: "not allow import some modules",
            category: "Fill me in",
            recommended: false
        },
        fixable: null,  // or "code" or "whitespace"
        schema: [{
            type:'Object'  //这里参数接受一个对象
        }]
    },

    create: function(context) {
        const name = context.getFilename();
        const module = [...context.options[0].modules];  //传入对象的modules数组,检测哪些模块
        const filesName = [...context.options[0].files] //传入对象的filess数组,哪些文件不检测
        function getFunctionLoc(node) {
            return {
               start: node.source.start,
               end: node.source.end,
            };
        }
        function isImport(node){
            return module.some(item => {
                 return node.source.value == item
            })
         }
        function isFile(){
            return filesName.some(item => {
                 return name.match(item)
            })
        }
        return {
            ImportDeclaration: (node) => {
                let isModule = isImport(node)
                let files = isFile()
                if (!files && isModule) {
                   context.report({
                     loc: getFunctionLoc(node),
                     node,
                     message: `该文件不能import ${module}模块,详情查看eslintrc.js`,
                   });
                 }
            },
        };
    }

看效果
在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值