forked from AlloyTeam/eslint-config-alloy
/
typescript.js
330 lines (330 loc) · 11.2 KB
/
typescript.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
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
/**
* AlloyTeam ESLint 规则
* https://alloyteam.github.io/eslint-config-alloy/
*
* 贡献者:
* xcatliu <xcatliu@gmail.com>
* heyli <lcxfs1991@gmail.com>
* DiamondYuan <admin@diamondyuan.com>
* Dash Chen <noreply@github.com>
* Swan <noreply@github.com>
*
* 依赖版本:
* eslint ^5.15.3
* eslint-plugin-react ^7.12.4
* eslint-plugin-vue ^5.2.2
* vue-eslint-parser ^6.0.3
* babel-eslint ^10.0.1
* @typescript-eslint/eslint-plugin ^1.5.0
*
* 此文件是由脚本 scripts/build.ts 自动生成
*
* @category 此规则属于哪种分类
* @reason 为什么要开启(关闭)此规则
* @fixable 支持自动修复
*/
module.exports = {
extends: ['./index.js'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
rules: {
/**
* 一个缩进必须用四个空格替代
* @category Stylistic Issues
* @fixable
*/
'@typescript-eslint/indent': [
'error',
4,
{
SwitchCase: 1,
flatTernaryExpressions: true
}
],
/**
* 重载的函数必须写在一起
* @category TypeScript
* @reason 增加可读性
*/
'@typescript-eslint/adjacent-overload-signatures': 'error',
/**
* 限制数组类型必须使用 Array<T> 或 T[]
* @category TypeScript
* @reason 允许灵活运用两者
* @fixable
*/
'@typescript-eslint/array-type': 'off',
/**
* 是否允许使用 // @ts-ignore 来忽略编译错误
* @category TypeScript
* @reason 既然已经使用注释来忽略编译错误了,说明已经清楚可能带来的后果
*/
'@typescript-eslint/ban-ts-ignore': 'off',
/**
* 禁用特定的类型
* @category TypeScript
* @reason 该规则用于禁止某个具体的类型的使用
* @fixable
*/
'@typescript-eslint/ban-types': 'off',
/**
* 变量名必须是 camelcase 风格的
* @category TypeScript
* @reason 很多 api 或文件名都不是 camelcase
*/
'@typescript-eslint/camelcase': 'off',
/**
* 类名与接口名必须为驼峰式
* @category TypeScript
*/
'@typescript-eslint/class-name-casing': 'error',
/**
* 函数返回值必须与声明的类型一致
* @category TypeScript
* @reason 编译阶段检查就足够了
*/
'@typescript-eslint/explicit-function-return-type': 'off',
/**
* 必须设置类的成员的可访问性
* @category TypeScript
* @reason 将不需要公开的成员设为私有的,可以增强代码的可理解性,对文档输出也很友好
*/
'@typescript-eslint/explicit-member-accessibility': 'error',
/**
* 约束泛型的命名规则
* @category TypeScript
*/
'@typescript-eslint/generic-type-naming': 'off',
/**
* 接口名称必须已 I 开头
* @category TypeScript
*/
'@typescript-eslint/interface-name-prefix': 'off',
/**
* 接口和类型别名的成员之间必须使用分号分隔
* @category TypeScript
* @fixable
*/
'@typescript-eslint/member-delimiter-style': 'error',
/**
* 私有成员必须以 _ 开头
* @category TypeScript
* @reason 已有 private 修饰符了,没必要限制变量名
*/
'@typescript-eslint/member-naming': 'off',
/**
* 指定类成员的排序规则
* @category TypeScript
* @reason 优先级:
* 1. static > instance
* 2. field > constructor > method
* 3. public > protected > private
*/
'@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'
]
}
],
/**
* 类型断言必须使用 as Type,禁止使用 <Type>
* @category TypeScript
* @reason <Type> 容易被理解为 jsx
*/
'@typescript-eslint/no-angle-bracket-type-assertion': 'error',
/**
* 禁止使用 Array 构造函数
* @category TypeScript
* @fixable
*/
'@typescript-eslint/no-array-constructor': 'off',
/**
* 禁止定义空的接口
* @category TypeScript
*/
'@typescript-eslint/no-empty-interface': 'error',
/**
* 禁止使用 any
* @category TypeScript
*/
'@typescript-eslint/no-explicit-any': 'off',
/**
* 禁止定义没必要的类,比如只有静态方法的类
* @category TypeScript
*/
'@typescript-eslint/no-extraneous-class': 'off',
/**
* 禁止对 array 使用 for in 循环
* @category TypeScript
*/
'@typescript-eslint/no-for-in-array': 'off',
/**
* 禁止给一个初始化时直接赋值为 number, string 的变量显式的指定类型
* @category TypeScript
* @reason 可以简化代码
* @fixable
*/
'@typescript-eslint/no-inferrable-types': 'error',
/**
* 禁止在接口中定义 constructor,或在类中定义 new
* @category TypeScript
*/
'@typescript-eslint/no-misused-new': 'off',
/**
* 禁止使用 namespace 来定义命名空间
* @category TypeScript
* @reason 使用 es6 引入模块,才是更标准的方式。
* 允许使用 declare namespace ... {} 来定义外部命名空间
*/
'@typescript-eslint/no-namespace': [
'error',
{
allowDeclarations: true,
allowDefinitionFiles: true
}
],
/**
* 禁止使用 non-null 断言(感叹号)
* @category TypeScript
* @reason 使用 non-null 断言时就已经清楚了风险
*/
'@typescript-eslint/no-non-null-assertion': 'off',
/**
* 禁止对对象字面量进行类型断言(断言成 any 是允许的)
* @category TypeScript
*/
'@typescript-eslint/no-object-literal-type-assertion': 'error',
/**
* 禁止给类的构造函数的参数添加修饰符
* @category TypeScript
*/
'@typescript-eslint/no-parameter-properties': 'error',
/**
* 禁止使用 require
* @category TypeScript
* @reason 有时需要动态引入,还是需要用 require
*/
'@typescript-eslint/no-require-imports': 'off',
/**
* 禁止将 this 赋值给其他变量,除非是解构赋值
* @category TypeScript
*/
'@typescript-eslint/no-this-alias': 'error',
/**
* 禁止使用三斜杠引入类型定义文件
* @category TypeScript
* @reason 三斜杠是已废弃的语法
*/
'@typescript-eslint/no-triple-slash-reference': 'error',
/**
* 禁止使用类型别名
* @category TypeScript
*/
'@typescript-eslint/no-type-alias': 'off',
/**
* 在命名空间中,可以直接使用内部变量,不需要添加命名空间前缀
* @category TypeScript
* @reason 已经禁止使用命名空间了
* @fixable
*/
'@typescript-eslint/no-unnecessary-qualifier': 'off',
/**
* 禁止无用的类型断言
* @category TypeScript
* @fixable
*/
'@typescript-eslint/no-unnecessary-type-assertion': 'off',
/**
* 禁止出现未使用的变量
* @category TypeScript
* @reason 编译阶段检查就足够了
*/
'@typescript-eslint/no-unused-vars': 'off',
/**
* 禁止在定义变量之前就使用它
* @category TypeScript
* @reason 编译阶段检查就足够了
*/
'@typescript-eslint/no-use-before-define': 'off',
/**
* 禁止出现没必要的 constructor
* @category TypeScript
*/
'@typescript-eslint/no-useless-constructor': 'error',
/**
* 禁止使用 require 来引入模块
* @category TypeScript
* @reason 统一使用 import 来引入模块,特殊情况使用单行注释允许 require 引入
*/
'@typescript-eslint/no-var-requires': 'error',
/**
* 可以简写为函数类型的接口或字面类似,必须简写
* @category TypeScript
* @reason reason
* @fixable
*/
'@typescript-eslint/prefer-function-type': 'error',
/**
* 优先使用接口而不是字面类型
* @category TypeScript
* @reason 接口可以 implement extend 和 merge
* @fixable
*/
'@typescript-eslint/prefer-interface': 'error',
/**
* 禁止使用 module 来定义命名空间
* @category TypeScript
* @reason module 已成为 js 的关键字
* @fixable
*/
'@typescript-eslint/prefer-namespace-keyword': 'error',
/**
* async 函数的返回值必须是 Promise
* @category TypeScript
* @reason 有时 async 函数在某个分支是同步的,不需要返回 Promise
*/
'@typescript-eslint/promise-function-async': 'off',
/**
* 使用加号时,两者必须同为数字或同为字符串
* @category TypeScript
*/
'@typescript-eslint/restrict-plus-operands': 'off',
/**
* 类型定义的冒号前面必须没有空格,后面必须有一个空格
* @category TypeScript
* @fixable
*/
'@typescript-eslint/type-annotation-spacing': 'error',
/**
* 函数重载时,若能通过联合类型将两个函数的类型声明合为一个,则使用联合类型而不是两个函数声明
* @category TypeScript
*/
'@typescript-eslint/unified-signatures': 'error'
}
};