@huangjunsen/eslint-config 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.editorconfig +17 -0
  2. package/.eslintignore +17 -0
  3. package/.eslintrc.js +5 -0
  4. package/LICENSE +21 -0
  5. package/README.md +286 -0
  6. package/__tests__/fixtures/es5.js +4 -0
  7. package/__tests__/fixtures/index.js +3 -0
  8. package/__tests__/fixtures/node.js +24 -0
  9. package/__tests__/fixtures/react-display-name.js +7 -0
  10. package/__tests__/fixtures/react.jsx +132 -0
  11. package/__tests__/fixtures/ts-import-a.ts +3 -0
  12. package/__tests__/fixtures/ts-import-b.ts +3 -0
  13. package/__tests__/fixtures/ts-node.ts +20 -0
  14. package/__tests__/fixtures/ts-react.tsx +27 -0
  15. package/__tests__/fixtures/ts-vue.vue +36 -0
  16. package/__tests__/fixtures/ts.ts +17 -0
  17. package/__tests__/fixtures/tsconfig.json +8 -0
  18. package/__tests__/fixtures/use-babel-eslint.jsx +170 -0
  19. package/__tests__/fixtures/vue.vue +16 -0
  20. package/__tests__/use-babel-eslint.test.js +47 -0
  21. package/__tests__/validate-js-configs.test.js +259 -0
  22. package/__tests__/validate-ts-configs.test.js +263 -0
  23. package/es5.js +10 -0
  24. package/essential/es5.js +12 -0
  25. package/essential/index.js +12 -0
  26. package/essential/react.js +76 -0
  27. package/essential/rules/blacklist.js +48 -0
  28. package/essential/rules/es6-blacklist.js +62 -0
  29. package/essential/rules/set-style-to-warn.js +30 -0
  30. package/essential/rules/ts-blacklist.js +15 -0
  31. package/essential/typescript/index.js +7 -0
  32. package/essential/typescript/react.js +7 -0
  33. package/essential/typescript/vue.js +9 -0
  34. package/essential/vue.js +8 -0
  35. package/index.js +23 -0
  36. package/jsx-a11y.js +5 -0
  37. package/node.js +6 -0
  38. package/package.json +48 -0
  39. package/react.js +11 -0
  40. package/rules/base/best-practices.js +284 -0
  41. package/rules/base/es6.js +168 -0
  42. package/rules/base/possible-errors.js +126 -0
  43. package/rules/base/strict.js +6 -0
  44. package/rules/base/style.js +445 -0
  45. package/rules/base/variables.js +48 -0
  46. package/rules/es5.js +11 -0
  47. package/rules/imports.js +167 -0
  48. package/rules/jsx-a11y.js +23 -0
  49. package/rules/node.js +11 -0
  50. package/rules/react.js +354 -0
  51. package/rules/typescript.js +807 -0
  52. package/rules/vue.js +96 -0
  53. package/typescript/index.js +6 -0
  54. package/typescript/node.js +6 -0
  55. package/typescript/react.js +6 -0
  56. package/typescript/vue.js +10 -0
  57. package/vue.js +10 -0
@@ -0,0 +1,284 @@
1
+ module.exports = {
2
+ rules: {
3
+ // 强制 getter 和 setter 在对象中成对出现
4
+ 'accessor-pairs': 'off',
5
+
6
+ // 某些数组方法的回调函数中必须包含 return 语句
7
+ 'array-callback-return': ['error', { allowImplicit: true }],
8
+
9
+ // 把 var 语句看作是在块级作用域范围之内,不能在块外使用
10
+ 'block-scoped-var': 'error',
11
+
12
+ // 强制类的方法使用 this
13
+ 'class-methods-use-this': [
14
+ 'off',
15
+ {
16
+ exceptMethods: [],
17
+ },
18
+ ],
19
+
20
+ // 设置圈复杂度最大值
21
+ // @reason IDE 插件会把大段代码标红,影响编码体验,此类问题适合做后置检查
22
+ complexity: ['off', 10],
23
+
24
+ // 要求 return 语句要么总是指定返回的值,要么不指定
25
+ 'consistent-return': 'off',
26
+
27
+ // 多行语句必须用大括号包裹,单行语句推荐用大括号包裹
28
+ curly: ['error', 'multi-line'],
29
+
30
+ // switch 语句需要始终包含 default 分支
31
+ 'default-case': ['warn', { commentPattern: '^no default$' }],
32
+
33
+ // 统一在点号之前换行
34
+ // @unessential
35
+ 'dot-location': ['error', 'property'],
36
+
37
+ // 优先使用 . 访问对象的属性
38
+ 'dot-notation': ['error', { allowKeywords: true }],
39
+
40
+ // 使用严格相等运算符
41
+ eqeqeq: ['warn', 'always', { null: 'ignore' }],
42
+
43
+ // for-in 循环中需要对 key 进行验证
44
+ 'guard-for-in': 'warn',
45
+
46
+ // enforce a maximum number of classes per file
47
+ 'max-classes-per-file': 'off',
48
+
49
+ // 禁止使用 alert
50
+ 'no-alert': 'warn',
51
+
52
+ // 禁止使用 arguments.caller 和 arguments.callee
53
+ 'no-caller': 'error',
54
+
55
+ // case 或 default 字句出现词法声明时,必须用块包裹
56
+ 'no-case-declarations': 'error',
57
+
58
+ // 禁止使用看起来像除法的正则表达式
59
+ 'no-div-regex': 'off',
60
+
61
+ // 如果一个 if 语句的结果总是返回一个 return 语句,那么最后的 else 是不必要的
62
+ // @reason 很多人习惯写 else return
63
+ 'no-else-return': 'off',
64
+
65
+ // 不要出现空函数
66
+ 'no-empty-function': [
67
+ 'error',
68
+ {
69
+ allow: ['arrowFunctions', 'functions', 'methods'],
70
+ },
71
+ ],
72
+
73
+ // 不要在解构中出现空模式,即 {} 或 []
74
+ 'no-empty-pattern': 'error',
75
+
76
+ // 与 null 的比较必须使用严格等于操作符
77
+ 'no-eq-null': 'off',
78
+
79
+ // 禁止使用 eval
80
+ // @unessential 部分场景必须使用 eval
81
+ 'no-eval': 'error',
82
+
83
+ // 禁止扩展原生对象
84
+ 'no-extend-native': 'error',
85
+
86
+ // 禁止不必要的 .bind() 调用
87
+ 'no-extra-bind': 'error',
88
+
89
+ // 禁止不必要的 label
90
+ 'no-extra-label': 'error',
91
+
92
+ // 不要让 case 语句落空
93
+ 'no-fallthrough': 'error',
94
+
95
+ // 不要省略小数点前或小数点后的 0
96
+ 'no-floating-decimal': 'error',
97
+
98
+ // 禁止对原生对象或只读的全局对象进行赋值
99
+ 'no-global-assign': ['error', { exceptions: [] }],
100
+
101
+ // 禁止使用较短的符号实现类型转换
102
+ 'no-implicit-coercion': [
103
+ 'off',
104
+ {
105
+ boolean: false,
106
+ number: true,
107
+ string: true,
108
+ allow: [],
109
+ },
110
+ ],
111
+
112
+ // 禁止在全局范围使用变量和函数声明
113
+ // @reason 这条在 "parserOptions": { "sourceType": "module" } 下不起作用
114
+ 'no-implicit-globals': 'off',
115
+
116
+ // 禁止使用类 eval 的方法,如 setTimeout 传入字符串
117
+ // @unessential
118
+ 'no-implied-eval': 'error',
119
+
120
+ // 禁止在 class 外使用 this
121
+ 'no-invalid-this': 'off',
122
+
123
+ // 禁止使用 __iterator__ 属性
124
+ 'no-iterator': 'error',
125
+
126
+ // 不要使用 label
127
+ 'no-labels': ['warn', { allowLoop: false, allowSwitch: false }],
128
+
129
+ // 禁止使用不必要的代码块
130
+ 'no-lone-blocks': 'error',
131
+
132
+ // 禁止在循环中的函数内出现外部作用域中定义且会发生变化的变量,以防止闭包副作用
133
+ 'no-loop-func': 'error',
134
+
135
+ // 禁用魔术数字
136
+ 'no-magic-numbers': [
137
+ 'off',
138
+ {
139
+ ignore: [],
140
+ ignoreArrayIndexes: true,
141
+ enforceConst: true,
142
+ detectObjects: false,
143
+ },
144
+ ],
145
+
146
+ // 禁止出现多个连续空格
147
+ // @unessential
148
+ 'no-multi-spaces': [
149
+ 'error',
150
+ {
151
+ ignoreEOLComments: false,
152
+ },
153
+ ],
154
+
155
+ // 禁止使用多行字符串
156
+ 'no-multi-str': 'error',
157
+
158
+ // 禁止单独 new 一个构造函数而不用于赋值或比较
159
+ 'no-new': 'error',
160
+
161
+ // 不要使用 Function 构造函数创建函数
162
+ 'no-new-func': 'error',
163
+
164
+ // 不要使用 new Number/String/Boolean
165
+ 'no-new-wrappers': 'error',
166
+
167
+ // 禁用八进制字面量
168
+ 'no-octal': 'error',
169
+
170
+ // 禁止在字符串字面量中使用八进制转义序列,如 var foo = 'Copyright \251';
171
+ 'no-octal-escape': 'error',
172
+
173
+ // 不要修改函数参数
174
+ 'no-param-reassign': [
175
+ 'warn',
176
+ {
177
+ props: true,
178
+ ignorePropertyModificationsFor: [
179
+ 'acc', // for reduce accumulators
180
+ 'e', // for e.returnvalue
181
+ 'ctx', // for Koa routing
182
+ 'draft', // for immer
183
+ 'req', // for Express requests
184
+ 'request', // for Express requests
185
+ 'res', // for Express responses
186
+ 'response', // for Express responses
187
+ '$scope', // for Angular 1 scopes
188
+ ],
189
+ },
190
+ ],
191
+
192
+ // 禁止使用 __proto__ 属性
193
+ 'no-proto': 'error',
194
+
195
+ // 不要重复声明变量和函数
196
+ 'no-redeclare': 'error',
197
+
198
+ // 禁止使用某些对象的某些属性
199
+ 'no-restricted-properties': 'off',
200
+
201
+ // 禁止在 return 语句中赋值
202
+ 'no-return-assign': ['error', 'always'],
203
+
204
+ // 禁止不必要的 return await
205
+ 'no-return-await': 'off',
206
+
207
+ // 禁止使用 javascript:url,如 location.href = 'javascript:void(0)';
208
+ // @unessential
209
+ 'no-script-url': 'error',
210
+
211
+ // 禁止自我赋值
212
+ 'no-self-assign': 'error',
213
+
214
+ // 禁止自我比较
215
+ 'no-self-compare': 'error',
216
+
217
+ // 禁止使用逗号操作符,除非用于 for 循环条件或明确用小括号包裹
218
+ 'no-sequences': 'error',
219
+
220
+ // 不要抛出字面量异常
221
+ 'no-throw-literal': 'warn',
222
+
223
+ // 禁用不变的循环条件
224
+ 'no-unmodified-loop-condition': 'off',
225
+
226
+ // 禁止出现未使用的表达式
227
+ 'no-unused-expressions': [
228
+ 'error',
229
+ {
230
+ allowShortCircuit: true,
231
+ allowTernary: true,
232
+ allowTaggedTemplates: true,
233
+ },
234
+ ],
235
+
236
+ // 禁止未使用的标签
237
+ 'no-unused-labels': 'error',
238
+
239
+ // 禁用不必要的 .call() 和 .apply()
240
+ 'no-useless-call': 'off',
241
+
242
+ // 禁用不必要的 catch
243
+ 'no-useless-catch': 'error',
244
+
245
+ // 禁止不必要的字符串拼接
246
+ 'no-useless-concat': 'error',
247
+
248
+ // 禁止不必要的转义字符
249
+ 'no-useless-escape': 'error',
250
+
251
+ // 禁止多余的 return; 语句
252
+ 'no-useless-return': 'error',
253
+
254
+ // 不要使用 void 运算符
255
+ 'no-void': 'error',
256
+
257
+ // 禁止在注释中使用特定的警告术语
258
+ 'no-warning-comments': ['off', { terms: ['todo', 'fixme'], location: 'start' }],
259
+
260
+ // 禁止使用 with
261
+ 'no-with': 'error',
262
+
263
+ // Promise 的 reject 需要传入 Error 对象
264
+ 'prefer-promise-reject-errors': ['warn', { allowEmptyReject: true }],
265
+
266
+ // 使用 parseInt() 方法时总是带上基数
267
+ radix: 'warn',
268
+
269
+ // 禁止使用不带 await 表达式的 async 函数
270
+ 'require-await': 'off',
271
+
272
+ // 强制正则使用 u 参数
273
+ 'require-unicode-regexp': 'off',
274
+
275
+ // 要求所有的 var 声明出现在它们所在的作用域顶部
276
+ 'vars-on-top': 'off',
277
+
278
+ // 将立即执行函数表达式(IIFE)用小括号包裹
279
+ 'wrap-iife': ['error', 'any', { functionPrototypeMethods: false }],
280
+
281
+ // 使用 color === 'red' 而不是 'red' === color
282
+ yoda: 'warn',
283
+ },
284
+ };
@@ -0,0 +1,168 @@
1
+ module.exports = {
2
+ rules: {
3
+ // 箭头函数-函数体风格
4
+ // @reason 允许灵活使用
5
+ 'arrow-body-style': [
6
+ 'off',
7
+ 'as-needed',
8
+ {
9
+ requireReturnForObjectLiteral: false,
10
+ },
11
+ ],
12
+
13
+ // 箭头函数-函数参数始终加上小括号
14
+ // @reason 同 prettier 默认配置值一致:https://prettier.io/docs/en/options.html#arrow-function-parentheses
15
+ 'arrow-parens': ['warn', 'always'],
16
+
17
+ // 箭头函数的箭头前后各留一个空格
18
+ // @unessential
19
+ 'arrow-spacing': ['error', { before: true, after: true }],
20
+
21
+ // 子类的 constructor 中必须使用 super,非子类的 constructor 中不能使用 super
22
+ 'constructor-super': 'error',
23
+
24
+ // generator 函数的 * 号前面无空格,后面有一个空格
25
+ // @unessential
26
+ 'generator-star-spacing': ['error', { before: false, after: true }],
27
+
28
+ // 禁止对类声明变量重新赋值
29
+ 'no-class-assign': 'error',
30
+
31
+ // 避免箭头函数与比较操作符产生混淆
32
+ // @unessential
33
+ 'no-confusing-arrow': 'error',
34
+
35
+ // 禁止修改 const 声明的变量
36
+ 'no-const-assign': 'error',
37
+
38
+ // 避免重复的类成员命名
39
+ 'no-dupe-class-members': 'error',
40
+
41
+ // 不要用多个 import 引入同一模块
42
+ // @reason replacedBy import/no-duplicates
43
+ 'no-duplicate-imports': 'off',
44
+
45
+ // 禁止使用 new Symbol
46
+ 'no-new-symbol': 'error',
47
+
48
+ // 禁止特定的 import
49
+ 'no-restricted-imports': [
50
+ 'off',
51
+ {
52
+ paths: [],
53
+ patterns: [],
54
+ },
55
+ ],
56
+
57
+ // 在 constructor 中,禁止在调用 super() 前使用 this 或 super 关键字
58
+ 'no-this-before-super': 'error',
59
+
60
+ // 对象的属性名不要使用无必要的计算属性
61
+ 'no-useless-computed-key': 'error',
62
+
63
+ // 避免不必要的 constructor
64
+ 'no-useless-constructor': 'error',
65
+
66
+ // 禁止在解构/import/export时进行无用的重命名
67
+ 'no-useless-rename': [
68
+ 'error',
69
+ {
70
+ ignoreDestructuring: false,
71
+ ignoreImport: false,
72
+ ignoreExport: false,
73
+ },
74
+ ],
75
+
76
+ // 使用 const 或 let 声明变量,不要使用 var
77
+ // @unessential
78
+ 'no-var': 'error',
79
+
80
+ // 使用对象属性和方法的简写语法
81
+ // @unessential
82
+ 'object-shorthand': [
83
+ 'error',
84
+ 'always',
85
+ {
86
+ ignoreConstructors: false,
87
+ avoidQuotes: true,
88
+ },
89
+ ],
90
+
91
+ // 回调函数使用箭头函数而不是匿名函数
92
+ // @unessential
93
+ 'prefer-arrow-callback': [
94
+ 'error',
95
+ {
96
+ allowNamedFunctions: false,
97
+ allowUnboundThis: true,
98
+ },
99
+ ],
100
+
101
+ // 优先使用 const,只有当变量会被重新赋值时才使用 let
102
+ // @unessential
103
+ 'prefer-const': [
104
+ 'error',
105
+ {
106
+ destructuring: 'any',
107
+ ignoreReadBeforeAssign: true,
108
+ },
109
+ ],
110
+
111
+ // 使用对象和数组的解构
112
+ 'prefer-destructuring': [
113
+ 'warn',
114
+ {
115
+ VariableDeclarator: {
116
+ array: false,
117
+ object: true,
118
+ },
119
+ AssignmentExpression: {
120
+ array: false,
121
+ object: false,
122
+ },
123
+ },
124
+ {
125
+ enforceForRenamedProperties: false,
126
+ },
127
+ ],
128
+
129
+ // 禁止使用 parseInt() 进行二、八、十六禁止转换
130
+ 'prefer-numeric-literals': 'off',
131
+
132
+ // 使用 rest 操作符替代 arguments 对象
133
+ 'prefer-rest-params': 'warn',
134
+
135
+ // 使用扩展运算符 ... 替代 apply()
136
+ 'prefer-spread': 'warn',
137
+
138
+ // 使用模板字符串替代字符串拼接
139
+ 'prefer-template': 'warn',
140
+
141
+ // generator 函数内必须有 yield 语句
142
+ 'require-yield': 'error',
143
+
144
+ // 剩余和扩展操作符与操作对象间不应有空格
145
+ 'rest-spread-spacing': ['error', 'never'],
146
+
147
+ // 为 import 排序
148
+ 'sort-imports': [
149
+ 'off',
150
+ {
151
+ ignoreCase: false,
152
+ ignoreMemberSort: false,
153
+ memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
154
+ },
155
+ ],
156
+
157
+ // 创建 Symbol 时必须传入参数
158
+ 'symbol-description': 'warn',
159
+
160
+ // 模板字符串中的大括号内部两侧无空格
161
+ // @unessential
162
+ 'template-curly-spacing': 'warn',
163
+
164
+ // yield* 表达式的 * 号前面无空格,后面有一个空格
165
+ // @unessential
166
+ 'yield-star-spacing': ['error', 'after'],
167
+ },
168
+ };
@@ -0,0 +1,126 @@
1
+ module.exports = {
2
+ rules: {
3
+ // for 循环中的计数器应朝着正确方向移动
4
+ 'for-direction': 'error',
5
+
6
+ // getter 需要有返回值
7
+ 'getter-return': ['error', { allowImplicit: true }],
8
+
9
+ // 不要使用 async 函数作为 Promise 的 executor
10
+ 'no-async-promise-executor': 'error',
11
+
12
+ // 不要在循环中使用 await,应使用 Promise.all()
13
+ 'no-await-in-loop': 'warn',
14
+
15
+ // 不要与负零进行比较
16
+ 'no-compare-neg-zero': 'error',
17
+
18
+ // 不要在条件表达式中使用赋值语句
19
+ 'no-cond-assign': ['error', 'always'],
20
+
21
+ // 生产环境禁止使用 console
22
+ 'no-console': 'warn',
23
+
24
+ // 不要在条件表达式中使用常量
25
+ 'no-constant-condition': 'warn',
26
+
27
+ // 禁止在正则中使用 ctrl 键的 ASCII 码
28
+ 'no-control-regex': 'off',
29
+
30
+ // 禁止使用 debugger
31
+ 'no-debugger': 'error',
32
+
33
+ // 函数的参数列表中禁止出现重复命名的参数
34
+ 'no-dupe-args': 'error',
35
+
36
+ // 对象中禁止出现重复命名的 key
37
+ 'no-dupe-keys': 'error',
38
+
39
+ // switch 语句中禁止出现重复的 case
40
+ 'no-duplicate-case': 'error',
41
+
42
+ // 不要出现空代码块
43
+ 'no-empty': 'error',
44
+
45
+ // 禁止在正则中使用空字符集 [],这不能匹配任何字符,可能是 typo
46
+ 'no-empty-character-class': 'error',
47
+
48
+ // 禁止对 catch 的入参重新赋值
49
+ 'no-ex-assign': 'error',
50
+
51
+ // 避免不必要的布尔类型转换
52
+ 'no-extra-boolean-cast': 'error',
53
+
54
+ // 禁止不必要的小括号
55
+ 'no-extra-parens': [
56
+ 'off',
57
+ 'all',
58
+ {
59
+ conditionalAssign: true,
60
+ nestedBinaryExpressions: false,
61
+ returnAssign: false,
62
+ ignoreJSX: 'all', // delegate to eslint-plugin-react
63
+ enforceForArrowConditionals: false,
64
+ },
65
+ ],
66
+
67
+ // 禁止不必要的分号
68
+ 'no-extra-semi': 'error',
69
+
70
+ // 不要对函数声明重新赋值
71
+ 'no-func-assign': 'error',
72
+
73
+ // 不要在块中使用函数声明
74
+ 'no-inner-declarations': 'error',
75
+
76
+ // 禁止在 RegExp 构造函数中使用无效的正则表达式
77
+ 'no-invalid-regexp': 'error',
78
+
79
+ // 禁止不规则的空白符
80
+ 'no-irregular-whitespace': 'error',
81
+
82
+ // 禁止在正则的字符集语法 [] 中使用由多个字符点构成的字符
83
+ 'no-misleading-character-class': 'error',
84
+
85
+ // 禁止将全局对象 Math、JSON、Reflect 当作函数进行调用
86
+ 'no-obj-calls': 'error',
87
+
88
+ // 不要直接在对象上调用 Object.prototypes 上的方法
89
+ // @unessential
90
+ 'no-prototype-builtins': 'error',
91
+
92
+ // 禁止在正则表达式中出现多个连续空格
93
+ 'no-regex-spaces': 'error',
94
+
95
+ // 禁用稀疏数组,如 var items = [,,];
96
+ 'no-sparse-arrays': 'error',
97
+
98
+ // 不要在普通字符串中出现模板字符串占位语法,如 "Hello ${name}!",旨在防错写。
99
+ // 但不排除有时普通字符串内容就是这样,因此这条开为 warn 级别
100
+ 'no-template-curly-in-string': 'warn',
101
+
102
+ // 避免令人困惑的多行表达式,多是由不加分号导致
103
+ 'no-unexpected-multiline': 'error',
104
+
105
+ // 不要在 return 等语句之后出现不可达的代码
106
+ 'no-unreachable': 'error',
107
+
108
+ // 禁止在 finally 中出现控制流语句,如 return, throw, break 或 continue
109
+ 'no-unsafe-finally': 'error',
110
+
111
+ // 禁止对关系运算符左边的运算元使用否定操作符,包括 in 和 instanceof
112
+ 'no-unsafe-negation': 'error',
113
+
114
+ // 避免因使用 await 或 yield 导致的竞争性赋值
115
+ 'require-atomic-updates': 'warn',
116
+
117
+ // 使用 Number.isNaN(),而不是直接与 NaN 进行比较
118
+ 'use-isnan': 'error',
119
+
120
+ // 使用有效的 JSDoc 注释
121
+ 'valid-jsdoc': 'off',
122
+
123
+ // 同 typeof 表达式结果进行比较的值必须是有效的字符串,即 'undefined', 'object', 'boolean', 'number', 'string', 'function' 或 'symbol'
124
+ 'valid-typeof': ['error', { requireStringLiterals: true }],
125
+ },
126
+ };
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ rules: {
3
+ // 不限制严格模式的使用
4
+ strict: 'off',
5
+ },
6
+ };