迅速入门
ESLint 是一个语法规则和代码风格的检查工具,可以用来保证写出语法正确、风格统一的代码。
首先,安装 ESLint。
npm i -g eslint
然后,安装 Airbnb 语法规则,以及 import、a11y、react 插件。
$ npm i -g eslint-config-airbnb
$ npm i -g eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react
最后,在项目的根目录下新建一个.eslintrc文件,配置ESLint。
{
"extends": "eslint-config-airbnb"
}
现在就可以检查,当前项目的代码是否符合预设的规则。
index.js文件的代码如下。
-------------------------------------------
var unusued = 'I have no purpose!';
function greet() {
var message = 'Hello, World!';
alert(message);
}
greet();
-------------------------------------------
使用 ESLint 检查这个文件,就会报出错误。
$ eslint index.js index.js 1:1 error Unexpected var, use let or const instead no-var 1:5 error unusued is defined but never used no-unused-vars 4:5 error Expected indentation of 2 characters but found 4 indent 4:5 error Unexpected var, use let or const instead no-var 5:5 error Expected indentation of 2 characters but found 4 indent ✖ 5 problems (5 errors, 0 warnings)
上面代码说明,原文件有五个错误,其中两个是不应该使用var命令,而要使用let或const;一个是定义了变量,却没有使用;另外两个是行首缩进为4个空格,而不是规定的2个空格。
webpack4系列教程(八):使用Eslint审查代码
- 配置
在项目根目录下创建 .eslintrc 文件:
{
"extends": "standard",
"rules": {
"no-new": "off"
}
}
在vue项目中,.vue文件中的 script标签内的代码,eslint 是无法识别的,这时就需要使用插件: eslint-plugin-html
npm i eslint-plugin-html -D
{
"extends": "standard",
"plugins": [
"html"
],
"rules": {
"no-new": "off"
}
}
这样就能解析 .vue文件中的JS代码了,官方也是如此推荐。
配置完成,如何使用呢?
在 package.json 文件中添加一条 script:
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config config/webpack.config.js --progress --inline --colors",
"lint": "eslint --ext .js --ext .vue src/"
}
- -ext 代表需要解析的文件格式,最后接上文件路径,由于我们的主要代码都在src 目录下,这里就配置 src 文件夹。
npm run lint
可见控制台给出了很多错误:

在项目前期没有加入eslint的情况下,后期加入必然会审查出许多错误。出现这么多错误之后,如果我们逐条手动去解决会非常耗时,此时可以借助eslint自动修复,方法也很简单。
只需要添加一条命令即可:
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config config/webpack.config.js --progress --inline --colors",
"lint": "eslint --ext .js --ext .vue src/",
"lint-fix": "eslint --fix --ext .js --ext .jsx --ext .vue src/"
}
然后执行
npm run lint-fix
我们希望在开发过程中能够实时进行eslint代码审查,需要安装两个依赖:
npm i eslint-loader babel-eslint -D
修改 .eslintrc:
{
"extends": "standard",
"plugins": [
"html"
],
"rules": {
"no-new": "off"
},
"parserOptions":{
"parser": "babel-eslint"
}
}
由于我们的项目使用了webpack并且代码都是经过Babel编译的,但是Babel处理过的代码有些语法可能对于eslint支持性不好,所以需要指定一个 parser。
下一步,在webpack.config.js中添加loader:
{
test: /\.(vue|js)$/,
loader: 'eslint-loader',
exclude: /node_modules/,
enforce: 'pre'
}
enforce: 'pre' 表示预处理,因为我们只是希望eslint来审查我们的代码,并不是去改变它,在真正的loader(比如:vue-loader)发挥作用前用eslint去检查代码。
记得在你的IDE中安装并开启eslint插件功能,这样就会有错误提示了。
比如:

图中的错误是未使用的变量。
- editorconfig:
editorconfig是用来规范我们的IDE配置的,在根目录创建 .editorconfig文件:
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
这样就能在各种IDE使用相同的配置了。
同样需要在IDE中安装editorconfig插件
以上就是eslint的配置方法了。
关闭Eslint语法校验
1、config 中 index.js修改以下代码
useEslint: false,
2、万能方法,就是在报错的JS文件中第一行写上
/* eslint-disable */
3、[eslint] Extra semicolon. (semi)
在根目录下.eslintrc.js文件中配置
rules: {
semi: ["error", "always"],//强行加分号
indent: 0//强行缩进
}
4、space-before-function-paren
在根目录下.eslintrc.js文件中配置
rules: {
'space-before-function-paren': [
'error',
{
anonymous: 'always',
named: 'always',
asyncArrow: 'always',
},
]
}


1万+

被折叠的 条评论
为什么被折叠?



