直接回答:ESLint 管代码质量、Prettier 管排版,两者通过 eslint-config-prettier 关掉冲突规则即可共存;extends 数组里后面的配置会覆盖前面的,把 prettier 放最后。

安装与初始化

第二步:使用命令

1
npm eslint --init

第三步:根据提示选择,就会自动安装airbnb以及他的依赖

通常安装的时候需要安装的东西

1
2
3
4
5
6
7
8
9
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^1.7.0",
"prettier": "^1.19.1"

这里安装里主要使用eslint来进行格式化,用eslint-config-airbnb规则查错,和prettier规则来格式化,这个时候美化任务就交给eslint了,安装eslint-config-airbnb的时候有一定的版本依赖,这个时候可以先安装eslint,eslint-config-airbnb,然后通过

1
npx install-peerdeps --D eslint-config-airbnb

这个命令将会自动安装airbnb所需要的各个依赖模块,并且符合版本要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
module.exports = {
/**
* 默认情况下,ESLint会在所有父级目录里寻找配置文件,一直到根目录。
* 为了将ESLint限制在一个特定的项目,设置root: true;
* ESLint一旦发现配置文件中有 root: true,就会停止在父级目录中寻找。
*/
root: true,
// 指定解析器
// babel-ESLint: 一个对Babel解析器的包装,使其能够与ESLint兼容。
// parser: 'babel-eslint',
// 设置解析器能帮助ESLint确定什么是解析错误。
parserOptions: {
parser: 'babel-eslint',
// 指定js版本。语法上的支持
ecmaVersion: 6
},
// 脚本在执行期间访问的额外的全局变量
// globals: {},
// env: 指定脚本的运行环境
env: {
// 一个环境定义了一组预定义的全局变量。
browser: true,
// 会自动开启es6语法支持。
es6: true,
node: true
},
// 使用第三方插件。全局安装的 ESLint 实例只能使用全局安装的ESLint插件。本地同理,不支持混用。
plugins: [
'html',
'vue'
],
// 配置文件从基础配置中继承已启用的规则。
/**
* eslint:recommended 启用核心规则,在规则页面中被标记为 √ 的。
*/
extends: [
// plugin:(此处不能有空格)包名/配置名称。解析时plugin是解析成 eslint-plugin-vue。如果有空格会解析失败,eslint-plugin- vue。
// plugin可以省略包名的前缀 eslint-plugin-
// 'plugin:vue/essential',
// '@vue/standard',
'eslint:recommended'
// 也可以指定另一个基本配置文件的绝对路径或相对路径
],

/**
* 每个规则有【3】个错误级别。
* off或0: 关闭该规则;
* warn或1: 开启规则,使用警告级别的错误,不会导致程序退出;
* error或2: 开启规则,使用错误级别的错误,当被触发的时候,程序会退出。
*/
rules: {
/**
* 【================================================ Possible Errors ================================================】
* 这些规则与JavaScript代码中可能的错误或逻辑错误有关。
*/
// 强制"for"循环中更新子句的计算器朝着正确的方向移动
'for-direction': 2,
// 禁止function定义中出现重名参数
'no-dupe-args': 2,
// 禁止对象字面量中出现重复的key
'no-dupe-keys': 2,
// 禁止出现重复的case标签
'no-duplicate-case': 2,
// 禁止对catch子句的参数重新赋值
'no-ex-assign': 2,
// 禁止对关系运算符的左操作数使用否定操作符
'no-unsafe-negation': 2,
// 禁止出现令人困惑的多行表达式
'no-unexpected-multiline': 2,
// 禁止在return、throw、continue、break语句之后出现不可达代码
'no-unreachable': 2,
// 禁止在finally语句块中出现控制流语句
'no-unsafe-finally': 2,
// 要求使用isNaN()检查NaN
'use-isnan': 2,
// 强制typeof表达式与有效的字符串进行比较
'valid-typeof': 2,
// 还可以写表达式,厉害了~
'no-debugger': process.env.NODE_ENV === 'production' ? 'error': 'off',
'no-console': process.env.NODE_ENV === 'production' ? 'error': 'off',

/**
* 【================================================ Best Practices ================================================】
* 这些规则是关于最佳实践的,帮助你避免一些问题。
*/
// 强制 getter 和 setter在对象中成对出现
'accessor-pairs': 2,
// 强制所有控制语句使用一致的括号风格
'curly': [2, 'multi-line'],
// 强制在点号之前和之后一致的换行
'dot-location': [2, 'property'],
// 要求使用 ===和 !==
'eqeqeq': [2, 'allow-null'],
// 禁用arguments.caller 或 arguments.callee
'no-caller': 2,
// 禁止使用空解构模式
'no-empty-pattern': 2,
// 禁止eval()
'no-eval': 2,
// 禁止使用类似eval()的方法
'no-implied-eval': 2,
// 禁止扩展原生类型
'no-extend-native': 2,
// 禁止不必要的.bind()调用
'no-extra-bind': 2,
// 禁止case语句落空
'no-fallthrough': 2,
// 禁止数字字面量中使用前导和末尾小数点
'no-floating-decimal': 2,
// 禁用__iterator__属性
'no-iterator': 2,
// 禁用标签语句
'no-labels': [2, {
'allowLoop' : false,
'allowSwitch': false
}],
// 禁用不必要嵌套块
'no-lone-blocks': 2,
// 禁止使用多个空格
'no-multi-spaces': 2,
// 禁止使用多行字符串
'no-multi-str': 2,
// 禁止对String,Number 和 Boolean 使用new操作符
'no-new-wrappers': 2,
// 禁用八进制字面量
'no-octal': 2,
// 禁止在字符串中使用八进制转义序列
'no-octal-escape': 2,
// 禁止使用__proto__属性
'no-proto': 2,
// 禁止多次声明同一变量
'no-redeclare': 2,
// 禁止在return语句中使用赋值语句
'no-return-assign': [2, 'except-parens'],
// 禁止自我赋值
'no-self-assign': 2,
// 禁止自我比较
'no-self-compare': 2,
// 禁用逗号操作符
'no-sequences': 2,
// 禁止抛出异常字面量
'no-throw-literal': 2,
// 禁止一成不变的循环条件
'no-unmodified-loop-condition': 2,
// 禁止不必要的.call()和.apply()
'no-useless-call': 2,
// 禁止不必要的转义字符
'no-useless-escape': 2,
// 禁用with语句
'no-with': 2,
// 要求IIFE使用括号括起来
'wrap-iife': 2,
// 要求或禁止Yoda条件。 if("red" === color) { //字面量在前,变量在后 }
'yoda': [2, 'never'], // 比较绝不能是Yoda条件(需要变量在前,字面量在后)

/**
* 【================================================ ECMAScript 6 ================================================】
* 这些规则只与ES6有关,即通常所说的ES2015。
*/
// 强制箭头函数前后使用一致的空格
'arrow-spacing': [2, {
'before': true,
'after' : true
}],
// 要求在构造函数中有super()调用
'constructor-super': 2,
// 强制generator函数中*号周围使用一致的空格
'generator-star-spacing': [2, {
'before': true,
'after' : true
}],
// 禁止修改类声明的变量
'no-class-assign': 2,
// 禁止修改const声明的变量
'no-const-assign': 2,
// 禁止类成员中出现重复的名称
'no-dupe-class-members': 2,
// 禁止 Symbolnew 操作符和 new 一起使用
'no-new-symbol': 2,
// 禁止在构造函数中,在调用super()之前使用 this 或 super
'no-this-before-super': 2,
// 禁止在对象中使用不必要的计算属性
'no-useless-computed-key': 2,
// 禁止不必要的构造函数
'no-useless-constructor': 2,
// 禁止模板字符串中嵌入表达式周围空格的使用
'template-curly-spacing': [2, 'never'],
// 强制yield*表达式中的*周围使用空格
'yield-star-spacing': [2, 'both'],
// 要求使用const声明那些声明后不再被修改的变量
'prefer-const': 2,

/**
* 【================================================ Stylistic Issues ================================================】
* 这些规则是关于代码风格的。
*/
// 获取当前执行环境的上下文时,强制使用一致的命名(此处强制使用 'that')。
'consistent-this': [2, 'that'],
// 禁止或强制在代码块中开括号前和闭括号后有空格 { return 11 }
'block-spacing': [2, 'always'],
// 强制在代码块中使用一致的大括号风格
'brace-style': [2, '1tbs', {
'allowSingleLine': true
}],
// 强制使用驼峰拼写法命名规定
'camelcase': [0, {
'properties': 'always'
}],
// 要求或禁止末尾逗号
'comma-dangle': [2, 'never'],
// 强制在逗号前后使用一致的空格
'comma-spacing': [2, {
'before': false,
'after' : true
}],
// 强制在逗号前后使用一致的空格
'comma-style': [2, 'last'],
// 要求或禁止文件末尾存在空行
'eol-last': 2,
// 强制使用一致的缩进
'indent': [2, 2, {
'SwitchCase': 1
}],
// 强制在JSX属性中一致地使用双引号或单引号
'jsx-quotes': [2, 'prefer-single'],
// 要求构造函数首字母大写
'new-cap': [2, {
'newIsCap': true,
'capIsNew': false
}],
// 要求构造无参构造函数时有圆括号
'new-parens': 2,
// 禁用Array构造函数
'no-array-constructor': 2,
// 禁止空格和tab的混合缩进
'no-mixed-spaces-and-tabs': 2,
// 禁止出现多行空行
'no-multiple-empty-lines': [2, {
// 最大连续空行数
max: 2
}],
// 禁止在函数标识符和其调用之间有空格
'func-call-spacing': 2,
// 禁止行尾空格
'no-trailing-spaces': 2,
// 禁止可以在有更简单的可替代的表达式时使用三元操作符
'no-unneeded-ternary': [2, {
// 允许表达式作为默认的赋值模式
'defaultAssignment': true
}],
// 禁止属性前有空白
'no-whitespace-before-property': 2,
// 强制函数中的变量要么一起声明要么分开声明
'one-var': [2, {
'initialized': 'never'
}],
// 强制操作符使用一致的换行符
'operator-linebreak': [2, 'after', {
'overrides': {
'?': 'before',
':': 'before'
}
}],
// 要求或禁止块内填充
'padded-blocks': [2, 'never'],
// 强烈使用一致的反勾号``、双引号""或单引号''
'quotes': [2, 'single', {
// 允许字符串使用单引号或者双引号,只要字符串中包含了一个其他引号,否则需要转义
'avoidEscape': true,
// 允许字符串使用反勾号
'allowTemplateLiterals': true
}],
// 禁止使用分号代替ASI(自动分号插入)
'semi': [2, 'never'],
// 强制分号之前和之后使用一致的空格
'semi-spacing': [2, {
'before': false,
'after' : true
}],
// 强制在块之前使用一致的空格
'space-before-blocks': [2, 'always'],
// 强制在圆括号内使用一致的空格
'space-in-parens': [2, 'never'],
// 要求操作符周围有空格
'space-infix-ops': 2,
// 强制在一元操作符前后使用一致的空格
'space-unary-ops': [2, {
'words' : true,
'nonwords': false
}],
// 强制在注释// 或/*使用一致的空格
'spaced-comment': [1, 'always', {
'markers': ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',']
}],
// 强制在大括号中使用一致的空格
'object-curly-spacing': [2, 'always', {
'objectsInObjects': false
}],
// 禁止或强制在括号内使用空格
'array-bracket-spacing': [2, 'never'],
// 强制要求在对象字面量的属性中键和值之间使用一致的间距
'key-spacing': [2, {
'beforeColon': false,
'afterColon' : true
}],

/**
* 【================================================ Node.js and CommonJS ================================================】
* 这些规则是关于Node.js 或 在浏览器中使用CommonJS的。
*/
// 要求回调函数中有容错处理
'handle-callback-err': [2, '^(err|error)$'],
// 禁止调用 require 时使用new操作符
'no-new-require': 2,
// 禁止对__dirname和__filename进行字符串连接
'no-path-concat': 1,
// 强制在function的左括号之前使用一致的空格
'space-before-function-paren': [2, 'never'],

/**
* 【================================================ Possible Errors ================================================】
* 这些规则与JavaScript代码中可能的错误或逻辑错误有关。
*/
// 禁止条件表达式中出现赋值操作符
'no-cond-assign': 2,
// 禁止在正则表达式中使用控制字符
'no-control-regex': 0,
// 禁止在正则表达式中使用空字符集
'no-empty-character-class': 2,
// 禁止不必要的布尔转换
'no-extra-boolean-cast': 2,
// 禁止不必要的括号
'no-extra-parens': [2, 'functions'],
// 禁止对function声明重新赋值
'no-func-assign': 2,
// 禁止在嵌套块中出现变量声明或function声明
'no-inner-declarations': [2, 'functions'],
// 禁止RegExp构造函数中存在无效的正则表达式字符串
'no-invalid-regexp': 2,
// 禁止在字符串和注释之外不规则的空白
'no-irregular-whitespace': 2,
// 禁止把全局对象作为函数调用
'no-obj-calls': 2,
// 禁止正则表达式字面量中出现多个空格
'no-regex-spaces': 2,
// 禁用稀疏数组
'no-sparse-arrays': 2,

/**
* 【================================================ Variables ================================================】
* 这些规则与变量声明有关。
*/
// 禁止删除变量
'no-delete-var': 2,
// 不允许标签与变量同名
'no-label-var': 2,
// 禁止将标识符定义为受限的名字
'no-shadow-restricted-names': 2,
// 禁止未声明的变量,除非它们在/*global */注释中被提到
'no-undef': 2,
// 禁止将变量初始化为undefined
'no-undef-init': 2,
// 禁止出现未使用的变量
'no-unused-vars': [2, {
'var' : 'all',
'args': 'none'
}],

/**
* 【================================================ 配置定义在插件中的规则 ================================================】
* 格式: 插件名/规则ID
*/
//
'vue/no-parsing-error': [2, { 'x-invalid-end-tag': false }]
}

版权声明:本文为CSDN博主「兔子零84」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_43764814/article/details/90416506

配置文件逐项说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module.exports = {
parser: 'babel-eslint', // 这个是指定解析器
extends: ['airbnb', 'prettier'], // 这个是制定规则,名字都省略了eslint-config- ,后面的规则继承覆盖前面的
env: {
browser: true,
node: true,
es6: true,
mocha: true,
jest: true,
jasmine: true,
},
rules: {
'generator-star-spacing': [0],
'consistent-return': [0],
'react/forbid-prop-types': [0],
'react/jsx-filename-extension': [1, { extensions: ['.js'] }],
'global-require': [1],
'import/prefer-default-export': [0],
'react/jsx-no-bind': [0],
'react/prop-types': [0],
'react/prefer-stateless-function': [0],
'react/jsx-one-expression-per-line': [0],
'react/jsx-wrap-multilines': [
'error',
{
declaration: 'parens-new-line',
assignment: 'parens-new-line',
return: 'parens-new-line',
arrow: 'parens-new-line',
condition: 'parens-new-line',
logical: 'parens-new-line',
prop: 'ignore',
},
],
'no-else-return': [0],
'no-restricted-syntax': [0],
'import/no-extraneous-dependencies': [0],
'no-use-before-define': [0],
'jsx-a11y/no-static-element-interactions': [0],
'jsx-a11y/no-noninteractive-element-interactions': [0],
'jsx-a11y/click-events-have-key-events': [0],
'jsx-a11y/anchor-is-valid': [0],
'no-nested-ternary': [0],
'arrow-body-style': [0],
'import/extensions': [0],
'no-bitwise': [0],
'no-cond-assign': [0],
'import/no-unresolved': [0],
'comma-dangle': [
'error',
{
arrays: 'always-multiline',
objects: 'always-multiline',
imports: 'always-multiline',
exports: 'always-multiline',
functions: 'ignore',
},
],
'object-curly-newline': [0],
'function-paren-newline': [0],
'no-restricted-globals': [0],
'require-yield': [1],
},
parserOptions: {
ecmaFeatures: {
experimentalObjectRestSpread: true,
},
},
settings: {
polyfills: ['fetch', 'promises'],
},
};


这篇笔记整理自我自己的实践记录,如果做法有出入,或者你踩过别的坑,欢迎到留言板一起聊聊。

站内搜索

没有找到内容!