ESLint 允许你指定你想要支持的 JavaScript 语言选项。
默认情况下,ESLint 支持 ECMAScript 5 语法。你可以覆盖该设置,以启用对 ECMAScript 其它版本和 JSX 的支持。react需要eslint-plugin-react,因为react包含很多eslint无法识别的语法。

指定es版本

1
2
3
4
5
6
7
8
{
parserOptions:{
ecmaVersion:6 //es6, 默认设置为3,5
},
env:{
es6:true //env只包含es6选项
}
}

其他选项

1
2
3
4
5
6
7
8
9
10
11
12
{
parserOptions:{
ecmaVersion:6,
sourceType:'module',//默认‘’script
ecmaFeatures:{
//额外语法特性
globalReturn:true,
impliedStrict:true,//启用全局严格模式
jsx:true,
}
},
}

指定编译器

1
2
3
{
parser:"babel-eslint",//默认为Espree
}

定义全局环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
env:{
browser:true,
node:true,
es6:true,
...
// https://eslint.bootcss.com/docs/user-guide/configuring/#specifying-environments
}
}

//也可以利用插件指定env
{
env:{
"example/custom":true
},
plugins:["example"]
}

全局变量

1
2
3
4
5
6
{
global:{
"window":true,//允许重写
"global":false,//不允许重写
}
}

插件

1
2
3
4
5
6
{
plugins:[
"plugin1",//名称为eslint-plugin-plugin1,可以省略eslint-plugin-这个前缀
"eslint-plugin-plugin2"
]
}

配置规则

1
2
3
4
5
6
7
{
rules:[
eqeqeq:"off", //关闭该规则
curly:"warn",//启用,警告
quotes:['error','double'],//启用,错误,参数为“double”,指双引号
]
}

暂时禁止规则

可以在代码中通过注释来暂时禁止规则发出警告

1.指定块

1
2
3
4
/* eslint-disable no-alert, no-console */
alert('foo');
console.log('bar');
/* eslint-enable no-alert, no-console */

2.指定某一行

1
2
3
/* eslint-disable no-alert */
alert('foo');
alert('foo');//no-alert

3.指定否个文件

可以将注释放在文件开头

4.禁用多个规则

1
2
// eslint-disable-next-line no-alert, quotes, semi
alert('foo');

继承已有规则

1
2
3
4
5
6
{
extends: [
'plugin:vue/essential',
'@vue/airbnb', //可以省略eslint-config-前缀
]
}

.eslintIgnore文件

忽略某些文件,不参与到eslint中