-
Notifications
You must be signed in to change notification settings - Fork 2
/
.eslintrc.js
213 lines (211 loc) · 6.46 KB
/
.eslintrc.js
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
/**
* @file eslint config
* @author Mingze Ma
*/
const TS_OVERRIDE = {
files: ['**/*.ts', '**/*.tsx'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
plugins: ['@typescript-eslint'], // typescript语法检查
rules: {
'@typescript-eslint/naming-convention': [
'error',
{
selector: 'variableLike',
format: [
'camelCase',
'PascalCase',
'UPPER_CASE',
],
leadingUnderscore: 'allow', // for unused function parameter
trailingUnderscore: 'forbid',
},
{
selector: 'typeLike',
format: [
'camelCase',
'PascalCase',
'UPPER_CASE',
],
leadingUnderscore: 'forbid',
trailingUnderscore: 'forbid',
},
],
'@typescript-eslint/adjacent-overload-signatures': ['error'],
'@typescript-eslint/class-literal-property-style': [
'error',
'fields',
],
'@typescript-eslint/consistent-type-assertions': [
'error',
{
assertionStyle: 'as',
objectLiteralTypeAssertions: 'never',
},
],
'@typescript-eslint/consistent-type-definitions': ['off'],
'@typescript-eslint/explicit-member-accessibility': ['warn'],
'@typescript-eslint/member-ordering': [
'error',
{
default: [
'public-static-field',
'protected-static-field',
'private-static-field',
'static-field',
'public-static-method',
'protected-static-method',
'private-static-method',
'static-method',
'public-instance-field',
'protected-instance-field',
'private-instance-field',
'public-field',
'protected-field',
'private-field',
'instance-field',
'field',
'constructor',
'public-instance-method',
'protected-instance-method',
'private-instance-method',
'public-method',
'protected-method',
'private-method',
'instance-method',
'method',
],
},
],
'@typescript-eslint/method-signature-style': ['off'],
'@typescript-eslint/no-empty-interface': ['error'],
'@typescript-eslint/no-inferrable-types': ['warn'],
'@typescript-eslint/no-namespace': [
'error',
{
allowDeclarations: true,
allowDefinitionFiles: true,
},
],
'@typescript-eslint/no-non-null-asserted-optional-chain': ['error'],
'@typescript-eslint/no-parameter-properties': ['off'],
'@typescript-eslint/no-require-imports': ['error'],
'@typescript-eslint/no-this-alias': [
'error',
{
allowDestructuring: true,
},
],
'@typescript-eslint/no-unused-expressions': [
'error',
{
allowShortCircuit: true,
allowTernary: true,
allowTaggedTemplates: true,
},
],
'@typescript-eslint/no-useless-constructor': ['warn'],
'@typescript-eslint/prefer-for-of': ['warn'],
'@typescript-eslint/prefer-function-type': ['warn'],
'@typescript-eslint/prefer-namespace-keyword': ['error'],
'@typescript-eslint/prefer-optional-chain': ['error'],
'@typescript-eslint/triple-slash-reference': [
'error',
{
path: 'never',
types: 'always',
lib: 'always',
},
],
'@typescript-eslint/type-annotation-spacing': ['error'],
'@typescript-eslint/typedef': [
'error',
{
arrayDestructuring: false,
arrowParameter: false,
memberVariableDeclaration: false,
objectDestructuring: false,
parameter: false,
propertyDeclaration: true,
variableDeclaration: false,
},
],
'@typescript-eslint/unified-signatures': ['error'],
},
};
module.exports = {
env: { // 指定要启用的环境,保证在进行代码检测时不会把这些环境预定义的全局变量识别成未定义的变量而报错
browser: true,
commonjs: true,
es6: true,
},
extends: [ // 数组中每个配置项继承它前面的配置
"react-app",
"react-app/jest"
],
settings: {
react: {
version: 'detect', // 为eslint-plugin-react 指定 React version. "detect" automatically picks the version you have installed.
},
},
overrides: [TS_OVERRIDE], // 为特定类型的文件指定处理器
parserOptions: { // 设置语言选项
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 11,
sourceType: 'module',
},
plugins: [
'react', // react语法检查
'react-hooks', // hooks语法检查
],
// 配置规则,优先级高于extends的共享规则,配置个别定制化规则
// "off" 或 0 - 关闭规则
// "warn" 或 1 - 将规则视为一个警告
// "error" 或 2 - 将规则视为一个错误
rules: {
indent: ['error', 2, { SwitchCase: 1 }], // 强制2格风格
'no-unused-vars': 'off', // 关掉eslint no-unused-vars
'import/no-unresolved': 'off',
'import/order': 'off', // 不需要引入顺序验证
'max-params': ['warn', 8], // 方法最多8个参数
'no-use-before-define': ['error', { functions: false, variables: false}], // 方法和变量可以在使用之后定义
'max-nested-callbacks': ['warn', 4], // 循环最多4层,超过4层警告
/** code cc */
'max-len': [ // 单行最长120
'error',
{
code: 120,
ignoreStrings: true,
ignoreUrls: true,
ignoreRegExpLiterals: true,
ignoreTemplateLiterals: true,
},
],
'operator-linebreak': ['error', 'before'], // 要求把换行符放在操作符前面
'no-mixed-operators': [ // 禁止混合使用操作符
'error',
{
allowSamePrecedence: false,
},
],
/** react */
'react/jsx-no-undef': 'error', // jsx中禁止使用未声明变量
'react/jsx-uses-vars': 'error', // 防止在jsx中使用的变量被标记为未使用
'react/jsx-uses-react': 'error', // 屏蔽'React' is defined but never used错误
'react-hooks/rules-of-hooks': 'error', // 检查Hook的规则
'react-hooks/exhaustive-deps': 'warn', // 检查effect的依赖
'react/jsx-no-bind': [
'warn',
{
allowArrowFunctions: true, // 允许箭头函数,来提升代码可读性
},
],
'react/require-default-props': 'off', // 组件的非必填属性不要求一定有默认值
'react/no-find-dom-node': 'off', // 允许使用react-dom的findDOMNode方法
},
};