教程参考:https://www.jianshu.com/p/ad1e46faaea2
Eslint官方规则文档:http://eslint.cn/docs/rules/
Eslint官方自定义规则文档:http://eslint.cn/docs/developer-guide/shareable-configs
Eslint官方忽略文件和目录:https://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories
创建Eslint:
1、npm init 。创建package.js。
2、npm install eslint --save-dev。创建Eslint。
3、修改package.js。
"scripts": { "test": "react-scripts test --env=jsdom", "lint": "eslint src", // 校验与.eslint.js同级的src "lint:create": "eslint --init"//创建Eslint文件 }
错误处理:
1、创建Eslint文件时,遇到
Which file(s), path(s), or glob(s) should be examined? 若是要校验path,需要写上相对路径。例如要检验同级别的src文件夹。则需要./src。若是其他,则上述的第三点需要进行相应的更改。
2、.eslint.js中的rule为空的时候,在执行校验的时候会出错。
部分需要记住的:
1、绝大部分的规则遵循以下要求。
- "off" 或 0 - 关闭规则
- "warn" 或 1 - 开启规则,使用警告级别的错误:warn (不会导致程序退出)
- "error" 或 2 - 开启规则,使用错误级别的错误:error (当被触发的时候,程序会退出)
2、单行跳过校验。在代码末尾加上 // eslint-disable-line
module.exports = {
"env": {
"browser": true,
"es6": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"no-console": 1, // 禁止console。1禁止,0允许
"no-unused-vars": 1, // 禁止变量定义但未使用,
"no-dupe-args": 1,// 禁止函数定义中出现重名参数
"no-trailing-spaces": 1,//禁止行尾空格
"no-shadow": 1,//禁止变量声明与外层作用域的变量同名
// "no-param-reassign": ['error', { 'props': true, 'ignorePropertyModificationsFor': ['res'] }],//禁止对函数的参数重新赋值,除了ajax回调的res
}
};