接上篇文章,我说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`,
});
}
},
};
}
看效果