Appearance
eslint
首先论证一个问题,eslint 是用来干什么的?这边引入一段官方的解释
ESLint 是一个语法规则和代码风格的检查工具,可以用来保证写出语法正确、风格统一的代码。
那么实际上的意义就更大,不管是多人合作,还是个人完成项目,代码的规范,可以极大避免代码的出错性,那么格式的规范,就可以极大的增加代码的可读性,当你使用 vscode+eslint 的时候就会发现,当 不符合 ESLint 规则的地方,同时还会做一些简单的自我修正。
packjson 的指令配置
"lint": "eslint . --ext .vue,.js,.ts,.jsx,.tsx,.json --max-warnings 0"
1
我们可以通过发布平台校验,当前指令是否正确,来判断这个代码是否可以成为正式的代码。
eslint
安装 eslint 插件
npm i -D vite-plugin-eslint
1
这里可以理解为 eslint 规范的不同的库,同归这些规则的插件配置,可以更好的执行 eslint 的作用。
"eslint": "~8.20.0",
"eslint-config-prettier": "~8.5.0", // 本质上这个工具其实就是禁用掉了一些不必要的以及和 Prettier 相冲突的 ESLint 规则
"eslint-define-config": "~1.5.1", // 配置规则
"eslint-plugin-import": "~2.26.0", // import语句规则
"eslint-plugin-prettier": "~4.2.1",
"eslint-plugin-vue": "~9.3.0" // vue语句的规则
1
2
3
4
5
6
2
3
4
5
6
eslintrc.js
"plugin:vue/base"
... 启用正确 ESLint 解析的设置和规则。
使用 Vue.js 3.x 的配置。
"plugin:vue/vue3-essential"
...base
,以及防止错误或意外行为的规则。"plugin:vue/vue3-strongly-recommended"
... 上面,加上大大提高代码可读性和 / 或开发体验的规则。"plugin:vue/vue3-recommended"
... 上面,加上强制执行主观社区默认值的规则,以确保一致性。
使用 Vue.js 2.x 的配置。
"plugin:vue/essential"
...base
,以及防止错误或意外行为的规则。"plugin:vue/strongly-recommended"
... 上面,加上大大提高代码可读性和 / 或开发体验的规则。"plugin:vue/recommended"
... 以上,加上强制执行主观社区默认值以确保一致性的规则
eslintrc 配置
module.exports = {
root: true,
env: {
browser: true,
node: true,
es6: true,
},
parser: 'vue-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser',
ecmaVersion: 2020,
sourceType: 'module',
jsxPragma: 'React',
ecmaFeatures: {
jsx: true,
tsx: true,
},
},
plugins: ['@typescript-eslint', 'prettier', 'import'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:vue/vue3-recommended',
'prettier',
],
overrides: [
{
files: ['*.ts', '*.tsx', '*.vue'],
rules: {
'no-undef': 'off',
},
},
],
rules: {
// js/ts
// 'no-console': ['warn', { allow: ['error'] }],
'no-restricted-syntax': ['error', 'LabeledStatement', 'WithStatement'],
camelcase: ['error', { properties: 'never' }],
'no-var': 'error',
'no-empty': ['error', { allowEmptyCatch: true }],
'no-void': 'error',
'prefer-const': ['warn', { destructuring: 'all', ignoreReadBeforeAssign: true }],
'prefer-template': 'error',
'object-shorthand': ['error', 'always', { ignoreConstructors: false, avoidQuotes: true }],
'block-scoped-var': 'error',
'no-constant-condition': ['error', { checkLoops: false }],
'no-redeclare': 'off',
'@typescript-eslint/no-redeclare': 'error',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-non-null-asserted-optional-chain': 'off',
// '@typescript-eslint/consistent-type-imports': ['error', { disallowTypeAnnotations: false }],
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],
'no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],
// vue
'vue/no-v-html': 'off',
'vue/require-default-prop': 'off',
'vue/require-explicit-emits': 'off',
'vue/multi-word-component-names': 'off',
// prettier
'prettier/prettier': 'error',
// import
'import/first': 'error',
'import/no-duplicates': 'error',
'import/order': [
'error',
{
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object', 'type'],
pathGroups: [
{
pattern: 'vue',
group: 'external',
position: 'before',
},
{
pattern: '@vue/**',
group: 'external',
position: 'before',
},
{
pattern: 'ant-design-vue',
group: 'internal',
},
],
pathGroupsExcludedImportTypes: ['type'],
},
],
},
};
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
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