@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
package/rules/node.js ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * 本文件继承了 egg-config-egg 的 node 规则,规则由 eslint-plugin-node 提供
3
+ * @link https://github.com/eggjs/eslint-config-egg/blob/master/lib/rules/node.js
4
+ * @link https://github.com/mysticatea/eslint-plugin-node
5
+ */
6
+
7
+ module.exports = {
8
+ extends: [
9
+ 'eslint-config-egg/lib/rules/node',
10
+ ],
11
+ };
package/rules/react.js ADDED
@@ -0,0 +1,354 @@
1
+ /**
2
+ * 本文件的规则由 eslint-plugin-react 和 eslint-plugin-react-hooks 提供
3
+ * @link https://github.com/yannickcr/eslint-plugin-react
4
+ * @link https://www.npmjs.com/package/eslint-plugin-react-hooks
5
+ */
6
+
7
+ module.exports = {
8
+ plugins: ['react', 'react-hooks'],
9
+ rules: {
10
+ // https://github.com/alibaba/f2e-spec/issues/95
11
+ // 防止 React 组件定义中缺少 displayName
12
+ 'react/display-name': ['warn', { ignoreTranspilerName: false }],
13
+
14
+ // 不要使用模糊的类型检查器 (any, array, object)
15
+ 'react/forbid-prop-types': ['warn', { forbid: ['any', 'array', 'object'] }],
16
+
17
+ // prop 值为 true 时,可以省略它的值
18
+ 'react/jsx-boolean-value': ['error', 'never', { always: [] }],
19
+
20
+ // 标签的属性有多行时,结束标签需另起一行
21
+ // @unessential
22
+ 'react/jsx-closing-bracket-location': ['error', 'line-aligned'],
23
+
24
+ // JSX 语法闭合标签的缩进和换行
25
+ // @unessential
26
+ 'react/jsx-closing-tag-location': 'error',
27
+
28
+ // JSX 行内属性间仅有一个空格
29
+ // @unessential
30
+ 'react/jsx-props-no-multi-spaces': 'error',
31
+
32
+ // JSX 属性的大括号内部两侧无空格
33
+ // @unessential
34
+ 'react/jsx-curly-spacing': ['error', 'never', { allowMultiline: true }],
35
+
36
+ // JSX 中命名事件处理函数的前缀约定
37
+ 'react/jsx-handler-names': [
38
+ 'off',
39
+ {
40
+ eventHandlerPrefix: 'handle',
41
+ eventHandlerPropPrefix: 'on',
42
+ },
43
+ ],
44
+
45
+ // JSX 属性使用 2 个空格缩进
46
+ // @unessential
47
+ 'react/jsx-indent-props': ['error', 2],
48
+
49
+ // JSX 语法检查数组和迭代器的 key
50
+ 'react/jsx-key': 'off',
51
+
52
+ // 标签属性的换行,如果标签有多个属性,且存在换行,则每个属性都需要换行独占一行
53
+ // @unessential
54
+ 'react/jsx-max-props-per-line': ['error', { maximum: 1, when: 'multiline' }],
55
+
56
+ // 不要在 JSX 属性中使用 .bind()
57
+ 'react/jsx-no-bind': [
58
+ 'warn',
59
+ {
60
+ ignoreRefs: false,
61
+ allowArrowFunctions: true,
62
+ allowBind: false,
63
+ },
64
+ ],
65
+
66
+ // 不要声明重复的属性名
67
+ 'react/jsx-no-duplicate-props': ['error', { ignoreCase: true }],
68
+
69
+ // JSX 中不要直接使用字符串,而是通过{'TEXT'}
70
+ 'react/jsx-no-literals': ['off', { noStrings: true }],
71
+
72
+ // 不要使用未声明的组件
73
+ 'react/jsx-no-undef': 'error',
74
+
75
+ // 使用大驼峰风格命名组件
76
+ 'react/jsx-pascal-case': [
77
+ 'error',
78
+ {
79
+ allowAllCaps: true,
80
+ ignore: [],
81
+ },
82
+ ],
83
+
84
+ // 类型检查器的属性名按首字母排序
85
+ 'react/sort-prop-types': [
86
+ 'off',
87
+ {
88
+ ignoreCase: true,
89
+ callbacksLast: false,
90
+ requiredFirst: false,
91
+ },
92
+ ],
93
+
94
+ // 本条废弃,用新规则代替 react/jsx-sort-props
95
+ 'react/jsx-sort-prop-types': 'off',
96
+
97
+ // 属性按首字母排序
98
+ 'react/jsx-sort-props': [
99
+ 'off',
100
+ {
101
+ ignoreCase: true,
102
+ callbacksLast: false,
103
+ shorthandFirst: false,
104
+ shorthandLast: false,
105
+ noSortAlphabetically: false,
106
+ reservedFirst: true,
107
+ },
108
+ ],
109
+
110
+ // 本条是对JS规范 no-unused-vars 的补充,防止React被错误地标记为未使用
111
+ 'react/jsx-uses-react': ['error'],
112
+
113
+ // 本条是对JS规范 no-unused-vars 的补充,防止变量被错误地标记为未使用
114
+ 'react/jsx-uses-vars': 'error',
115
+
116
+ // 不要使用危险属性
117
+ 'react/no-danger': 'warn',
118
+
119
+ // 禁止使用已经废弃的方法
120
+ // @unessential
121
+ 'react/no-deprecated': 'error',
122
+
123
+ // 不要在 componentWillUpdate 内改变 state 值
124
+ 'react/no-will-update-set-state': 'error',
125
+
126
+ // 使用 this.state 获取状态,用 setState 改变状态;不能用 this.state 赋值改变状态
127
+ 'react/no-direct-mutation-state': 'off',
128
+
129
+ // isMounted 已经被废弃
130
+ 'react/no-is-mounted': 'error',
131
+
132
+ // 每个文件只包含一个 React 组件
133
+ 'react/no-multi-comp': ['error', { ignoreStateless: true }],
134
+
135
+ // 禁止使用 setState
136
+ 'react/no-set-state': 'off',
137
+
138
+ // 使用 ref 回调函数或 React.createRef(),不要使用字符串
139
+ 'react/no-string-refs': 'error',
140
+
141
+ // 不要在无状态组件中使用 this
142
+ 'react/no-this-in-sfc': 'error',
143
+
144
+ // 在JSX中,所有DOM属性和属性命名都应该是小驼峰
145
+ 'react/no-unknown-property': 'error',
146
+
147
+ // 使用 class extends React.Component ,而不是 React.createClass
148
+ 'react/prefer-es6-class': ['error', 'always'],
149
+
150
+ // 当未使用生命周期方法、setState 或者 ref,使用无状态函数定义组件
151
+ 'react/prefer-stateless-function': 'off',
152
+
153
+ // prop 需要 propTypes 验证类型
154
+ 'react/prop-types': [
155
+ 'warn',
156
+ {
157
+ ignore: [],
158
+ customValidators: [],
159
+ skipUndeclared: false,
160
+ },
161
+ ],
162
+
163
+ // 防止 JSX 中未引入 React
164
+ 'react/react-in-jsx-scope': 'off',
165
+
166
+ // render 方法必须要有返回值
167
+ 'react/require-render-return': 'error',
168
+
169
+ // 无子元素的标签需写成自闭合标签
170
+ 'react/self-closing-comp': 'error',
171
+
172
+ // 组件方法排序
173
+ 'react/sort-comp': [
174
+ 'off',
175
+ {
176
+ order: [
177
+ 'static-methods',
178
+ 'instance-variables',
179
+ 'lifecycle',
180
+ '/^on.+$/',
181
+ 'getters',
182
+ 'setters',
183
+ '/^(get|set)(?!(InitialState$|DefaultProps$|ChildContext$)).+$/',
184
+ 'instance-methods',
185
+ 'everything-else',
186
+ 'rendering',
187
+ ],
188
+ groups: {
189
+ lifecycle: [
190
+ 'displayName',
191
+ 'propTypes',
192
+ 'contextTypes',
193
+ 'childContextTypes',
194
+ 'mixins',
195
+ 'statics',
196
+ 'defaultProps',
197
+ 'constructor',
198
+ 'getDefaultProps',
199
+ 'getInitialState',
200
+ 'state',
201
+ 'getChildContext',
202
+ 'componentWillMount',
203
+ 'componentDidMount',
204
+ 'componentWillReceiveProps',
205
+ 'shouldComponentUpdate',
206
+ 'componentWillUpdate',
207
+ 'componentDidUpdate',
208
+ 'componentWillUnmount',
209
+ ],
210
+ rendering: ['/^render.+$/', 'render'],
211
+ },
212
+ },
213
+ ],
214
+
215
+ // 多行的 JSX 标签需用小括号包裹
216
+ // @unessential
217
+ 'react/jsx-wrap-multilines': [
218
+ 'error',
219
+ {
220
+ declaration: true,
221
+ assignment: true,
222
+ return: true,
223
+ arrow: true,
224
+ },
225
+ ],
226
+
227
+ // 设置第一个属性的位置。multiline-multiprop:如果JSX标签占用多行并且有多个属性,则第一个属性应始终放在新行上
228
+ // @unessential
229
+ 'react/jsx-first-prop-new-line': ['error', 'multiline-multiprop'],
230
+
231
+ // 不要在 JSX 属性的等号两边加空格
232
+ // @unessential
233
+ 'react/jsx-equals-spacing': ['error', 'never'],
234
+
235
+ // JSX 使用 2 个空格缩进
236
+ // @unessential
237
+ 'react/jsx-indent': ['error', 2],
238
+
239
+ // 不要单独使用 target='_blank'
240
+ 'react/jsx-no-target-blank': 'warn',
241
+
242
+ // 不要在 setState 中使用 this.state
243
+ 'react/no-access-state-in-setstate': 'error',
244
+
245
+ // 指定 React 组件的文件扩展名
246
+ 'react/jsx-filename-extension': [
247
+ 'error',
248
+ { extensions: ['.jsx', '.js', '.tsx', '.ts', '.vue'] },
249
+ ],
250
+
251
+ // JSX 语句的文本节点中不要使用注释字符串(例如,以//或/ *开头)
252
+ 'react/jsx-no-comment-textnodes': 'error',
253
+
254
+ // 禁止使用 ReactDOM.render 的返回值
255
+ 'react/no-render-return-value': 'error',
256
+
257
+ // 组件必须包含 shouldComponentUpdate 或者 PureRenderMixin
258
+ 'react/require-optimization': ['off', { allowDecorators: [] }],
259
+
260
+ // 不要使用 findDOMNode,严格模式下已经弃用
261
+ // @unessential
262
+ 'react/no-find-dom-node': 'error',
263
+
264
+ // 禁止某些特定的 prop 命名,只对组件生效,DOM节点不生效
265
+ 'react/forbid-component-props': ['off', { forbid: [] }],
266
+
267
+ // 禁止某些特定的元素命名
268
+ 'react/forbid-elements': ['off', { forbid: [] }],
269
+
270
+ // 禁止在有子节点的组件或 DOM 元素中使用 dangerouslySetInnerHTML 属性
271
+ 'react/no-danger-with-children': 'error',
272
+
273
+ // 声明的 prop 必须被使用
274
+ 'react/no-unused-prop-types': [
275
+ 'error',
276
+ {
277
+ customValidators: [],
278
+ skipShapeProps: true,
279
+ },
280
+ ],
281
+
282
+ // style 的属性值必须是一个对象
283
+ 'react/style-prop-object': 'error',
284
+
285
+ // 标签中禁止出现无意义字符,比如 > " } '
286
+ 'react/no-unescaped-entities': 'error',
287
+
288
+ // 禁止将 children 作为属性名
289
+ 'react/no-children-prop': 'error',
290
+
291
+ // 自闭合标签的斜线前有且仅有一个空格
292
+ // @unessential
293
+ 'react/jsx-tag-spacing': [
294
+ 'error',
295
+ {
296
+ closingSlash: 'never',
297
+ beforeSelfClosing: 'always',
298
+ afterOpening: 'never',
299
+ },
300
+ ],
301
+
302
+ // 该条废弃,被替代为 jsx-tag-spacing
303
+ 'react/jsx-space-before-closing': ['off', 'always'],
304
+
305
+ // 不要用数组索引作为 map 元素的 key
306
+ 'react/no-array-index-key': 'warn',
307
+
308
+ // 如果属性没有 isRequired 类型检查,需要在 defaultProps 内对其赋值
309
+ 'react/require-default-props': 'off',
310
+
311
+ // 禁止使用其他组件的prop类型,除非有明确的导入/导出
312
+ 'react/forbid-foreign-prop-types': 'off',
313
+
314
+ // HTML 自闭标签不能有子节点
315
+ 'react/void-dom-elements-no-children': 'error',
316
+
317
+ // defaultProps 需要与 propTypes 相匹配
318
+ 'react/default-props-match-prop-types': ['warn', { allowRequiredDefaults: false }],
319
+
320
+ // 在扩展 React.PureComponent 时禁止使用 shouldComponentUpdate
321
+ 'react/no-redundant-should-component-update': 'error',
322
+
323
+ // 声明的 state 必须被使用
324
+ 'react/no-unused-state': 'error',
325
+
326
+ // 布尔类型的属性的命名约定,建议用 is 或 has 前缀
327
+ 'react/boolean-prop-naming': 'off',
328
+
329
+ // 禁止大小写拼写错误,该检测规则未写入规范
330
+ 'react/no-typos': 'error',
331
+
332
+ // JSX 属性或子元素强制使用花括号或禁止使用不必要的花括号
333
+ 'react/jsx-curly-brace-presence': 'off',
334
+
335
+ // Checks rules of Hooks
336
+ // @link https://reactjs.org/docs/hooks-rules.html
337
+ 'react-hooks/rules-of-hooks': 'error',
338
+
339
+ // Checks effect dependencies
340
+ // @link https://reactjs.org/docs/hooks-rules.html
341
+ 'react-hooks/exhaustive-deps': 'warn',
342
+ },
343
+ settings: {
344
+ 'import/resolver': {
345
+ node: {
346
+ extensions: ['.js', '.jsx', '.json'],
347
+ },
348
+ },
349
+ react: {
350
+ pragma: 'React',
351
+ version: 'detect',
352
+ },
353
+ },
354
+ };