@houheaven/eslint-config-helper 3.0.0 → 3.1.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.
- package/README.md +34 -0
- package/exports.js +3 -0
- package/lib/react.js +118 -0
- package/lib/supports/support-react-jsx.js +58 -0
- package/lib/supports/support-react-tsx.js +119 -0
- package/node_modules/@houheaven/eslint-config-helper-core/rules/react/_deprecated.js +29 -0
- package/node_modules/@houheaven/eslint-config-helper-core/rules/react/class-component.js +170 -0
- package/node_modules/@houheaven/eslint-config-helper-core/rules/react/dom.js +140 -0
- package/node_modules/@houheaven/eslint-config-helper-core/rules/react/fixable.js +239 -0
- package/node_modules/@houheaven/eslint-config-helper-core/rules/react/jsx.js +147 -0
- package/node_modules/@houheaven/eslint-config-helper-core/rules/react/prop-types.js +128 -0
- package/node_modules/@houheaven/eslint-config-helper-core/rules/react/suggestion.js +42 -0
- package/node_modules/@houheaven/eslint-config-helper-core/rules/react-hooks/_deprecated.js +19 -0
- package/node_modules/@houheaven/eslint-config-helper-core/rules/react-hooks/fixable.js +228 -0
- package/node_modules/@houheaven/eslint-config-helper-core/rules/react-hooks/other.js +19 -0
- package/node_modules/@houheaven/eslint-config-helper-core/summary.js +9 -1
- package/node_modules/@houheaven/eslint-config-helper-core/utils/utils.js +28 -0
- package/package.json +9 -11
package/README.md
CHANGED
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
- 内置 `@typescript-eslint/eslint-plugin`,提供 TS 代码的校验规则
|
|
13
13
|
- 内置 `eslint-plugin-import`,提供 import 的校验规则
|
|
14
14
|
- 内置 `eslint-plugin-vue`,提供 Vue 代码的校验规则
|
|
15
|
+
- 内置 `eslint-plugin-react`,提供 React 代码的校验规则
|
|
16
|
+
- 内置 `eslint-plugin-react-hooks`,提供 React Hooks 的校验规则
|
|
15
17
|
- 内置 `@html-eslint/eslint-plugin`,提供 html 文件的解析和校验规则
|
|
16
18
|
- 内置 `eslint-plugin-html`,提供 html 文件中 <script> 的 JS 解析能力
|
|
17
19
|
|
|
@@ -38,6 +40,8 @@ pnpm add -D @houheaven/eslint-config-helper@next
|
|
|
38
40
|
| configs.vue2 | 校验 Vue2 + JS(Es6+)+ import 代码 | `.vue` |
|
|
39
41
|
| configs.vue3 | 校验 Vue3 + JS(Es6+)+ import 代码 | `.vue` |
|
|
40
42
|
| configs.vue3Ts | 校验 Vue3 + TS + JS(Es6+)+ import 代码 | `.vue` |
|
|
43
|
+
| configs.reactJsx | 校验 React + JS(Es6+)+ import 代码 | `.jsx` |
|
|
44
|
+
| configs.reactTsx | 校验 React + TS + JS(Es6+)+ import 代码 | `.tsx` |
|
|
41
45
|
| configs.htmlEs5 | 校验 html 和 <script> 中的 JS(Es5) 代码 | `.html` `.htm` `.php` |
|
|
42
46
|
| configs.htmlEs6 | 校验 html 和 <script> 中的 JS(Es6+) 代码 | `.html` `.htm` `.php` |
|
|
43
47
|
|
|
@@ -50,6 +54,8 @@ pnpm add -D @houheaven/eslint-config-helper@next
|
|
|
50
54
|
| supports.vue2 | 校验 Vue2 + JS(Es6+)+ import 代码 | `.vue` |
|
|
51
55
|
| supports.vue3 | 校验 Vue3 + JS(Es6+)+ import 代码 | `.vue` |
|
|
52
56
|
| supports.vue3Ts | 校验 Vue3 + TS + JS(Es6+)+ import 代码 | `.vue` |
|
|
57
|
+
| supports.reactJsx | 校验 React + JS(Es6+)+ import 代码 | `.jsx` |
|
|
58
|
+
| supports.reactTsx | 校验 React + TS + JS(Es6+)+ import 代码 | `.tsx` |
|
|
53
59
|
| supports.htmlEs5 | 校验 html 和 <script> 中的 JS(Es5) 代码 | `.html` `.htm` `.php` |
|
|
54
60
|
| supports.htmlEs6 | 校验 html 和 <script> 中的 JS(Es6+) 代码 | `.html` `.htm` `.php` |
|
|
55
61
|
|
|
@@ -141,6 +147,30 @@ export default [
|
|
|
141
147
|
];
|
|
142
148
|
```
|
|
143
149
|
|
|
150
|
+
### React(JSX)
|
|
151
|
+
|
|
152
|
+
校验文件范围:`.jsx`
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
import hhEslintConfigHelper from "@houheaven/eslint-config-helper";
|
|
156
|
+
|
|
157
|
+
export default [
|
|
158
|
+
...hhEslintConfigHelper.configs.reactJsx,
|
|
159
|
+
];
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### React + TypeScript(TSX,推荐)
|
|
163
|
+
|
|
164
|
+
校验文件范围:`.tsx`
|
|
165
|
+
|
|
166
|
+
```js
|
|
167
|
+
import hhEslintConfigHelper from "@houheaven/eslint-config-helper";
|
|
168
|
+
|
|
169
|
+
export default [
|
|
170
|
+
...hhEslintConfigHelper.configs.reactTsx,
|
|
171
|
+
];
|
|
172
|
+
```
|
|
173
|
+
|
|
144
174
|
### html + ES5
|
|
145
175
|
|
|
146
176
|
校验文件范围:`.html` `.js`
|
|
@@ -215,6 +245,8 @@ export default [
|
|
|
215
245
|
- 查看 [`Eslint 规则文档`](https://www.npmjs.com/package/eslint)
|
|
216
246
|
- 查看 [`eslint-plugin-import 规则文档`](https://www.npmjs.com/package/eslint-plugin-import)
|
|
217
247
|
- 查看 [`eslint-plugin-vue 规则文档`](https://eslint.vuejs.org/rules/)
|
|
248
|
+
- 查看 [`eslint-plugin-react 规则文档`](https://github.com/jsx-eslint/eslint-plugin-react#list-of-supported-rules)
|
|
249
|
+
- 查看 [`eslint-plugin-react-hooks 规则文档`](https://www.npmjs.com/package/eslint-plugin-react-hooks)
|
|
218
250
|
- 查看 [`@html-eslint 规则文档`](https://html-eslint.org/docs/getting-started/)
|
|
219
251
|
- 查看 [`@typescript-eslint/eslint-plugin 规则文档`](https://typescript-eslint.io/rules/)
|
|
220
252
|
|
|
@@ -233,6 +265,8 @@ export default [
|
|
|
233
265
|
- [`eslint`](https://www.npmjs.com/package/eslint)
|
|
234
266
|
- [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import)
|
|
235
267
|
- [`eslint-plugin-vue`](https://www.npmjs.com/package/eslint-plugin-vue)
|
|
268
|
+
- [`eslint-plugin-react`](https://www.npmjs.com/package/eslint-plugin-react)
|
|
269
|
+
- [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks)
|
|
236
270
|
- [`eslint-plugin-html`](https://www.npmjs.com/package/eslint-plugin-html)
|
|
237
271
|
- [`eslint-import-resolver-alias`](https://www.npmjs.com/package/eslint-import-resolver-alias)
|
|
238
272
|
- [`eslint-import-resolver-node`](https://www.npmjs.com/package/eslint-import-resolver-node)
|
package/exports.js
CHANGED
|
@@ -5,6 +5,7 @@ import typescript from "./lib/typescript.js";
|
|
|
5
5
|
import html from "./lib/html.js";
|
|
6
6
|
import vue2 from "./lib/vue2.js";
|
|
7
7
|
import vue3 from "./lib/vue3.js";
|
|
8
|
+
import react from "./lib/react.js";
|
|
8
9
|
|
|
9
10
|
export default {
|
|
10
11
|
configs: {
|
|
@@ -14,6 +15,7 @@ export default {
|
|
|
14
15
|
...html.configs,
|
|
15
16
|
...vue2.configs,
|
|
16
17
|
...vue3.configs,
|
|
18
|
+
...react.configs,
|
|
17
19
|
},
|
|
18
20
|
supports: {
|
|
19
21
|
...es5.supports,
|
|
@@ -22,5 +24,6 @@ export default {
|
|
|
22
24
|
...html.supports,
|
|
23
25
|
...vue2.supports,
|
|
24
26
|
...vue3.supports,
|
|
27
|
+
...react.supports,
|
|
25
28
|
},
|
|
26
29
|
};
|
package/lib/react.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// @houheaven/eslint-config-helper/react
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// - eslint-plugin-react 定制规则
|
|
6
|
+
// - eslint-plugin-react-hooks 定制规则
|
|
7
|
+
// ==========================================================
|
|
8
|
+
|
|
9
|
+
import utils from "@houheaven/eslint-config-helper-core";
|
|
10
|
+
import supportEs6 from "./supports/support-es6.js";
|
|
11
|
+
import supportImport from "./supports/support-import.js";
|
|
12
|
+
import supportReactJsx from "./supports/support-react-jsx.js";
|
|
13
|
+
import supportReactTsx from "./supports/support-react-tsx.js";
|
|
14
|
+
|
|
15
|
+
// 基础配置(JSX)
|
|
16
|
+
const reactJsxCommonConfig = {
|
|
17
|
+
files: ["**/*.jsx"],
|
|
18
|
+
linterOptions: {
|
|
19
|
+
// 当 eslint-disable 注释配置未使用时,是否报错
|
|
20
|
+
reportUnusedDisableDirectives: "warn",
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// 基础配置(TSX)
|
|
25
|
+
const reactTsxCommonConfig = {
|
|
26
|
+
files: ["**/*.tsx"],
|
|
27
|
+
linterOptions: {
|
|
28
|
+
// 当 eslint-disable 注释配置未使用时,是否报错
|
|
29
|
+
reportUnusedDisableDirectives: "warn",
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// 基础支持:校验 ES5、ES6、import、react 规则
|
|
34
|
+
const supportReactJsxConfig = {
|
|
35
|
+
plugins: {
|
|
36
|
+
...supportEs6.plugins,
|
|
37
|
+
...supportImport.plugins,
|
|
38
|
+
...supportReactJsx.plugins,
|
|
39
|
+
},
|
|
40
|
+
languageOptions: {
|
|
41
|
+
...supportEs6.languageOptions,
|
|
42
|
+
...supportReactJsx.languageOptions,
|
|
43
|
+
},
|
|
44
|
+
settings: {
|
|
45
|
+
...supportImport.settings,
|
|
46
|
+
...supportReactJsx.settings,
|
|
47
|
+
},
|
|
48
|
+
rules: {
|
|
49
|
+
// Eslint 基础规则
|
|
50
|
+
...utils.rules.es5,
|
|
51
|
+
...utils.rules.es6,
|
|
52
|
+
// eslint-plugin-import 规则
|
|
53
|
+
...utils.rules.import,
|
|
54
|
+
// React 规则
|
|
55
|
+
...supportReactJsx.rules,
|
|
56
|
+
...utils.rules.react,
|
|
57
|
+
...utils.rules.reactHooks,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// 基础支持:校验 ES5、ES6、TypeScript、import、react 规则
|
|
62
|
+
const supportReactTsxConfig = {
|
|
63
|
+
plugins: {
|
|
64
|
+
...supportEs6.plugins,
|
|
65
|
+
...supportImport.plugins,
|
|
66
|
+
...supportReactTsx.plugins,
|
|
67
|
+
},
|
|
68
|
+
languageOptions: {
|
|
69
|
+
...supportReactTsx.languageOptions,
|
|
70
|
+
...supportEs6.languageOptions,
|
|
71
|
+
},
|
|
72
|
+
settings: {
|
|
73
|
+
...supportImport.settings,
|
|
74
|
+
...supportReactTsx.settings,
|
|
75
|
+
},
|
|
76
|
+
rules: {
|
|
77
|
+
// Eslint 基础规则
|
|
78
|
+
...utils.rules.es5,
|
|
79
|
+
...utils.rules.es6,
|
|
80
|
+
// eslint-plugin-import 规则
|
|
81
|
+
...utils.rules.import,
|
|
82
|
+
// React 规则
|
|
83
|
+
...supportReactTsx.rules,
|
|
84
|
+
...utils.rules.react,
|
|
85
|
+
...utils.rules.reactHooks,
|
|
86
|
+
// @typescript-eslint/eslint-plugin 规则
|
|
87
|
+
...utils.rules.typescript,
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// 预设配置:校验 JSX 文件的 ES5、ES6、import、react 规则
|
|
92
|
+
const reactJsxConfig = {
|
|
93
|
+
name: "@houheaven/eslint-config-helper/react",
|
|
94
|
+
...reactJsxCommonConfig,
|
|
95
|
+
...supportReactJsxConfig,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// 预设配置:校验 TSX 文件的 ES5、ES6、TypeScript、import、react 规则
|
|
99
|
+
const reactTsxConfig = {
|
|
100
|
+
name: "@houheaven/eslint-config-helper/react-tsx",
|
|
101
|
+
...reactTsxCommonConfig,
|
|
102
|
+
...supportReactTsxConfig,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export default {
|
|
106
|
+
supports: {
|
|
107
|
+
reactJsx: supportReactJsxConfig,
|
|
108
|
+
reactTsx: supportReactTsxConfig,
|
|
109
|
+
},
|
|
110
|
+
configs: {
|
|
111
|
+
reactJsx: [
|
|
112
|
+
reactJsxConfig,
|
|
113
|
+
],
|
|
114
|
+
reactTsx: [
|
|
115
|
+
reactTsxConfig,
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// @houheaven/eslint-config-helper/support-react-jsx
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// - 解析 JSX 文件(React 语法)
|
|
6
|
+
// ==========================================================
|
|
7
|
+
|
|
8
|
+
import globals from "globals";
|
|
9
|
+
import pluginStylistic from "@stylistic/eslint-plugin";
|
|
10
|
+
import pluginReact from "eslint-plugin-react";
|
|
11
|
+
import pluginReactHooks from "eslint-plugin-react-hooks";
|
|
12
|
+
|
|
13
|
+
const supportReactJsx = {
|
|
14
|
+
plugins: {
|
|
15
|
+
"react": pluginReact,
|
|
16
|
+
"react-hooks": pluginReactHooks,
|
|
17
|
+
"@stylistic": pluginStylistic,
|
|
18
|
+
},
|
|
19
|
+
languageOptions: {
|
|
20
|
+
sourceType: "module", // 指定代码的来源类型,"module" 表示 ESLint 会在 ECMAScript 模块的上下文中解析代码(即支持 import 和 export 语法)
|
|
21
|
+
ecmaVersion: "latest", // 指定 ECMAScript 版本,"latest" 表示使用最新的 ECMAScript 版本
|
|
22
|
+
parserOptions: {
|
|
23
|
+
sourceType: "module", // 指定代码的来源类型,"module" 表示 ESLint 会在 ECMAScript 模块的上下文中解析代码(即支持 import 和 export 语法)
|
|
24
|
+
ecmaVersion: "latest", // 指定 ECMAScript 版本,"latest" 表示使用最新的 ECMAScript 版本
|
|
25
|
+
ecmaFeatures: {
|
|
26
|
+
jsx: true, // 启用 JSX 全局变量和作用域规则
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
globals: {
|
|
30
|
+
...globals.browser, // 浏览器环境,启用浏览器环境的全局变量(window、document、navigator、alert 等)
|
|
31
|
+
...globals.node, // Node.js 环境,启用 Node.js 环境的全局变量和作用域规则(__dirname、__filename、process、global 等)
|
|
32
|
+
...globals.commonjs, // Node.js 环境,启用 CommonJS 的全局变量和作用域规则(require()、module、exports 等)
|
|
33
|
+
...globals.es6, // 启用 ECMAScript 6 全局变量和作用域规则(let、const、class、import、export 等)
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
settings: {
|
|
37
|
+
// ---------------- eslint-plugin-react 的语义字典 ----------------
|
|
38
|
+
// 这里只是"告诉规则怎么理解你的代码",不产生任何报错。
|
|
39
|
+
"react": {
|
|
40
|
+
// JSX 编译目标:<div/> => React.createElement(...);用 h/preact 等自定义 pragma 时再改
|
|
41
|
+
pragma: "React",
|
|
42
|
+
// Fragment 名称:<>...</> => React.Fragment;改自定义 pragma 时同步改
|
|
43
|
+
fragment: "Fragment",
|
|
44
|
+
// React 版本:默认从 node_modules/react 自动探测,兼容 no-unsupported-features 等版本相关规则
|
|
45
|
+
version: "detect",
|
|
46
|
+
},
|
|
47
|
+
// 供 react/prefer-exact-props 识别的 exact wrapper 函数
|
|
48
|
+
// 注意:只有对象形式并显式声明 `exact: true` 的条目,才会被 prefer-exact-props 视为 "exact wrapper"(源码依据:propWrapper.getExactPropWrapperFunctions 中的 `func.exact === true` 过滤)。
|
|
49
|
+
// 单纯字符串或不带 exact 的对象只会参与 `forbid-foreign-prop-types` 等宽松匹配。
|
|
50
|
+
"propWrapperFunctions": [
|
|
51
|
+
{ property: "exact", exact: true },
|
|
52
|
+
{ property: "forbidExtraProps", exact: true },
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
rules: {},
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export default supportReactJsx;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// @houheaven/eslint-config-helper/support-react-tsx
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// - 解析 TSX 文件(React 语法)
|
|
6
|
+
// ==========================================================
|
|
7
|
+
|
|
8
|
+
import globals from "globals";
|
|
9
|
+
import pluginStylistic from "@stylistic/eslint-plugin";
|
|
10
|
+
import pluginTs from "@typescript-eslint/eslint-plugin";
|
|
11
|
+
import pluginReact from "eslint-plugin-react";
|
|
12
|
+
import pluginReactHooks from "eslint-plugin-react-hooks";
|
|
13
|
+
import parserTs from "@typescript-eslint/parser";
|
|
14
|
+
|
|
15
|
+
const supportReactTsx = {
|
|
16
|
+
plugins: {
|
|
17
|
+
"react": pluginReact,
|
|
18
|
+
"react-hooks": pluginReactHooks,
|
|
19
|
+
"@typescript-eslint": pluginTs,
|
|
20
|
+
"@stylistic": pluginStylistic,
|
|
21
|
+
},
|
|
22
|
+
languageOptions: {
|
|
23
|
+
parser: parserTs,
|
|
24
|
+
sourceType: "module", // 指定代码的来源类型,"module" 表示 ESLint 会在 ECMAScript 模块的上下文中解析代码(即支持 import 和 export 语法)
|
|
25
|
+
ecmaVersion: "latest", // 指定 ECMAScript 版本,"latest" 表示使用最新的 ECMAScript 版本
|
|
26
|
+
parserOptions: {
|
|
27
|
+
sourceType: "module", // 指定代码的来源类型,"module" 表示 ESLint 会在 ECMAScript 模块的上下文中解析代码(即支持 import 和 export 语法)
|
|
28
|
+
ecmaVersion: "latest", // 指定 ECMAScript 版本,"latest" 表示使用最新的 ECMAScript 版本
|
|
29
|
+
ecmaFeatures: {
|
|
30
|
+
jsx: true, // 启用 JSX 全局变量和作用域规则
|
|
31
|
+
},
|
|
32
|
+
// React 添加对 TypeScript 的支持
|
|
33
|
+
extraFileExtensions: [".jsx", ".tsx"],
|
|
34
|
+
project: [
|
|
35
|
+
"**/tsconfig.json",
|
|
36
|
+
"**/tsconfig.**.json",
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
globals: {
|
|
40
|
+
...globals.browser, // 浏览器环境,启用浏览器环境的全局变量(window、document、navigator、alert 等)
|
|
41
|
+
...globals.node, // Node.js 环境,启用 Node.js 环境的全局变量和作用域规则(__dirname、__filename、process、global 等)
|
|
42
|
+
...globals.commonjs, // Node.js 环境,启用 CommonJS 的全局变量和作用域规则(require()、module、exports 等)
|
|
43
|
+
...globals.es6, // 启用 ECMAScript 6 全局变量和作用域规则(let、const、class、import、export 等)
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
settings: {
|
|
47
|
+
// ---------------- eslint-plugin-react 的语义字典 ----------------
|
|
48
|
+
// 这里只是"告诉规则怎么理解你的代码",不产生任何报错。
|
|
49
|
+
"react": {
|
|
50
|
+
// JSX 编译目标:<div/> => React.createElement(...);用 h/preact 等自定义 pragma 时再改
|
|
51
|
+
pragma: "React",
|
|
52
|
+
// Fragment 名称:<>...</> => React.Fragment;改自定义 pragma 时同步改
|
|
53
|
+
fragment: "Fragment",
|
|
54
|
+
// React 版本:默认从 node_modules/react 自动探测,兼容 no-unsupported-features 等版本相关规则
|
|
55
|
+
version: "detect",
|
|
56
|
+
},
|
|
57
|
+
// 供 react/prefer-exact-props 识别的 exact wrapper 函数
|
|
58
|
+
// 注意:只有对象形式并显式声明 `exact: true` 的条目,才会被 prefer-exact-props 视为 "exact wrapper"(源码依据:propWrapper.getExactPropWrapperFunctions 中的 `func.exact === true` 过滤)。
|
|
59
|
+
// 单纯字符串或不带 exact 的对象只会参与 `forbid-foreign-prop-types` 等宽松匹配。
|
|
60
|
+
"propWrapperFunctions": [
|
|
61
|
+
{ property: "exact", exact: true },
|
|
62
|
+
{ property: "forbidExtraProps", exact: true },
|
|
63
|
+
],
|
|
64
|
+
|
|
65
|
+
// 如果组件被 observer / styled / memo 之类高阶函数包一层,就在这里登记
|
|
66
|
+
// 否则 react/display-name、react/prop-types 等规则可能"看不见"里面的组件
|
|
67
|
+
// componentWrapperFunctions: [],
|
|
68
|
+
|
|
69
|
+
// 自定义"表单"组件:影响 react/form-has-action 等规则如何识别提交入口
|
|
70
|
+
// formComponents: [],
|
|
71
|
+
|
|
72
|
+
// 自定义"链接"组件:影响 jsx-no-target-blank、jsx-a11y/anchor-is-valid 等识别
|
|
73
|
+
// 例如:{ name: "Link", linkAttribute: ["to", "href"] }
|
|
74
|
+
// linkComponents: [],
|
|
75
|
+
|
|
76
|
+
// "react": {
|
|
77
|
+
// "createClass": "createReactClass", // 告诉插件"创建组件的工厂函数叫什么"。老代码里 React.createClass 被拆到独立包 create-react-class,插件需要知道这个函数名,才能把 createReactClass({...}) 识别成一个组件。
|
|
78
|
+
// "pragma": "React", // JSX 编译时对应的全局对象名。<div/> 会被编译成 React.createElement("div"),
|
|
79
|
+
// // 改为 "h" 或 "Preact" 等自定义 pragma 时要在这里改,否则 no-unused-vars 之类的规则会误判 React / 自定义 pragma 变量是"没用到的 import"。
|
|
80
|
+
// "fragment": "Fragment", // Fragment 的对应名。<>...</> 会被识别为 React.Fragment;如果你有自定义 pragma(比如 h + Fragment),也在这里改。
|
|
81
|
+
// "version": "detect", // React 版本。"detect" 让插件从 node_modules/react/package.json 自动读版本号。有些规则(如 react/no-unsupported-features、hooks 相关行为)要按版本区分。也可以硬编码成 "18.2"。
|
|
82
|
+
// "defaultVersion": "", // 当 detect 探测不到(比如项目里根本没装 react)时的兜底版本。空字符串意味着不设兜底,插件会用它自带的默认(一般是最新版)。
|
|
83
|
+
// "flowVersion": "0.53", // Flow 类型系统的版本(仅在你用 Flow 而不是 TS 时才有用),影响某些 propTypes / Flow 类型的解析行为。
|
|
84
|
+
// },
|
|
85
|
+
// // propTypes 的包装函数白名单
|
|
86
|
+
// "propWrapperFunctions": [
|
|
87
|
+
// "forbidExtraProps",
|
|
88
|
+
// { "property": "freeze", "object": "Object" },
|
|
89
|
+
// { "property": "myFavoriteWrapper" },
|
|
90
|
+
// { "property": "forbidExtraProps", "exact": true }, // exact 表示这个包装器语义是"禁止未声明的 prop"
|
|
91
|
+
// ],
|
|
92
|
+
// // 组件包装函数,告诉插件"哪些函数返回的还是一个 React 组件"
|
|
93
|
+
// "componentWrapperFunctions": [
|
|
94
|
+
// "observer", // `property`
|
|
95
|
+
// { "property": "styled" }, // `object` is optional
|
|
96
|
+
// { "property": "observer", "object": "Mobx" },
|
|
97
|
+
// { "property": "observer", "object": "<pragma>" }, // sets `object` to whatever value `settings.react.pragma` is set to
|
|
98
|
+
// ],
|
|
99
|
+
// // 自定义"表单"组件
|
|
100
|
+
// // 有些项目不用原生 <form action="...">,而用自定义组件 <CustomForm endpoint="..."/>。像 react/form-has-action、以及安全类规则会关心"这个组件是否指定了提交地址"。formAttribute 就告诉插件"这个组件的『action』等价属性叫什么",可以是字符串,也可以是数组(允许多个字段任选其一)。
|
|
101
|
+
// "formComponents": [
|
|
102
|
+
// // Components used as alternatives to <form> for forms, eg. <Form endpoint={ url } />
|
|
103
|
+
// "CustomForm",
|
|
104
|
+
// { "name": "SimpleForm", "formAttribute": "endpoint" },
|
|
105
|
+
// { "name": "Form", "formAttribute": ["registerEndpoint", "loginEndpoint"] }, // allows specifying multiple properties if necessary
|
|
106
|
+
// ],
|
|
107
|
+
// // 自定义"链接"组件
|
|
108
|
+
// // 很多项目不用 <a>,而是 <Link to="...">(React Router)或自研 <Hyperlink>。react/jsx-no-target-blank、jsx-a11y/anchor-is-valid 等规则要靠这个白名单才能识别"哪些组件本质上是链接",linkAttribute 指明它的 href 对应属性叫什么。
|
|
109
|
+
// "linkComponents": [
|
|
110
|
+
// // Components used as alternatives to <a> for linking, eg. <Link to={ url } />
|
|
111
|
+
// "Hyperlink",
|
|
112
|
+
// { "name": "MyLink", "linkAttribute": "to" },
|
|
113
|
+
// { "name": "Link", "linkAttribute": ["to", "href"] }, // allows specifying multiple properties if necessary
|
|
114
|
+
// ],
|
|
115
|
+
},
|
|
116
|
+
rules: {},
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export default supportReactTsx;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// eslint-plugin-react - deprecated
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// 已弃用规则,会用 replacedBy 迁移到新规则;配置一律保持 "off",
|
|
6
|
+
// 演示时在示例文件里通过 /* eslint react/xxx: "error" */ 临时启用。
|
|
7
|
+
// ==========================================================
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
rules: {
|
|
11
|
+
// 排序 defaultProps
|
|
12
|
+
// ----------------------------------------------------------
|
|
13
|
+
// 已废弃(v7.33.0),替换为 react/sort-default-props
|
|
14
|
+
// 官方推荐:无(deprecated 规则一律不进入 recommended)
|
|
15
|
+
// 社区最佳:保持 "off",改用 react/sort-default-props 或
|
|
16
|
+
// typescript-eslint 的 sort-type-constituents
|
|
17
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-sort-default-props.md
|
|
18
|
+
"react/jsx-sort-default-props": "off",
|
|
19
|
+
|
|
20
|
+
// JSX 闭合前空格
|
|
21
|
+
// ----------------------------------------------------------
|
|
22
|
+
// 已废弃(v7.4.0),替换为 react/jsx-tag-spacing
|
|
23
|
+
// 官方推荐:无(deprecated 规则一律不进入 recommended)
|
|
24
|
+
// 社区最佳:保持 "off",改用 react/jsx-tag-spacing(更完整地覆盖
|
|
25
|
+
// before/after 空格控制)
|
|
26
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-space-before-closing.md
|
|
27
|
+
"react/jsx-space-before-closing": "off",
|
|
28
|
+
},
|
|
29
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// eslint-plugin-react - class-component
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// class 组件 / 生命周期 / this 语义相关
|
|
6
|
+
// ==========================================================
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
rules: {
|
|
10
|
+
// 禁止在 setState 里直接引用 this.state(会导致基于旧值的并发问题)
|
|
11
|
+
// ----------------------------------------------------------
|
|
12
|
+
// 官方推荐:无(未进入 recommended)
|
|
13
|
+
// 社区最佳:Airbnb / Next.js 均设为 error(此规则强烈推荐,React 官方也建议改用函数形式 setState(prev => ...))
|
|
14
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-access-state-in-setstate.md
|
|
15
|
+
"react/no-access-state-in-setstate": "error",
|
|
16
|
+
|
|
17
|
+
// 禁止在 componentDidMount 内 setState(触发额外一次 render,影响性能)
|
|
18
|
+
// ----------------------------------------------------------
|
|
19
|
+
// 官方推荐:无
|
|
20
|
+
// options: "disallow-in-func"(同时禁止 mount 里的回调函数中 setState)
|
|
21
|
+
// 社区最佳:多数场景使用默认值即可;严格团队用 "disallow-in-func"
|
|
22
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-did-mount-set-state.md
|
|
23
|
+
"react/no-did-mount-set-state": "error",
|
|
24
|
+
|
|
25
|
+
// 禁止在 componentDidUpdate 内 setState(无条件调用会形成死循环)
|
|
26
|
+
// ----------------------------------------------------------
|
|
27
|
+
// 官方推荐:无
|
|
28
|
+
// options: "disallow-in-func"
|
|
29
|
+
// 社区最佳:默认即可(无条件的 setState 才会死循环,带条件的合理场景也被拦是常见抱怨)
|
|
30
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-did-update-set-state.md
|
|
31
|
+
"react/no-did-update-set-state": "error",
|
|
32
|
+
|
|
33
|
+
// 禁止在 componentWillUpdate 内 setState(该生命周期已被 UNSAFE_)
|
|
34
|
+
// ----------------------------------------------------------
|
|
35
|
+
// 官方推荐:无
|
|
36
|
+
// options: "disallow-in-func"
|
|
37
|
+
// 社区最佳:默认即可
|
|
38
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-will-update-set-state.md
|
|
39
|
+
"react/no-will-update-set-state": "error",
|
|
40
|
+
|
|
41
|
+
// 禁止使用 setState(面向"纯函数组件 + hooks"的极端严格模式)
|
|
42
|
+
// ----------------------------------------------------------
|
|
43
|
+
// 官方推荐:无
|
|
44
|
+
// 社区最佳:默认关闭;老项目 / 混合项目会导致大量误报
|
|
45
|
+
"react/no-set-state": "off",
|
|
46
|
+
|
|
47
|
+
// 禁止直接修改 this.state(会导致 UI 与 state 不一致,绕过 setState)
|
|
48
|
+
// ----------------------------------------------------------
|
|
49
|
+
// 官方推荐:recommended(默认 error)
|
|
50
|
+
// 社区最佳:所有主流 config 都直接沿用
|
|
51
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-direct-mutation-state.md
|
|
52
|
+
"react/no-direct-mutation-state": "error",
|
|
53
|
+
|
|
54
|
+
// 禁止调用 this.isMounted()(该 API 已废弃)
|
|
55
|
+
// ----------------------------------------------------------
|
|
56
|
+
// 官方推荐:recommended(默认 error)
|
|
57
|
+
"react/no-is-mounted": "error",
|
|
58
|
+
|
|
59
|
+
// 单文件禁止定义多个组件
|
|
60
|
+
// ----------------------------------------------------------
|
|
61
|
+
// 官方推荐:无
|
|
62
|
+
// options: { ignoreStateless: false }(默认,函数组件也算)
|
|
63
|
+
// 社区最佳:{ ignoreStateless: true } —— 允许把小型函数组件写在同一文件(Airbnb 采用)
|
|
64
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-multi-comp.md
|
|
65
|
+
"react/no-multi-comp": ["error", { ignoreStateless: true }],
|
|
66
|
+
|
|
67
|
+
// 禁止冗余的 shouldComponentUpdate(继承自 PureComponent 时再手写就是冗余)
|
|
68
|
+
// ----------------------------------------------------------
|
|
69
|
+
// 官方推荐:无
|
|
70
|
+
// 社区最佳:error(Airbnb 采用)
|
|
71
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-redundant-should-component-update.md
|
|
72
|
+
"react/no-redundant-should-component-update": "error",
|
|
73
|
+
|
|
74
|
+
// 禁止字符串 ref(React 17+ 已完全废弃)
|
|
75
|
+
// ----------------------------------------------------------
|
|
76
|
+
// 官方推荐:recommended(默认 error)
|
|
77
|
+
// options: { noTemplateLiterals: true }(额外禁止模板字符串 ref)
|
|
78
|
+
// 社区最佳:{ noTemplateLiterals: true }(拦得更严)
|
|
79
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md
|
|
80
|
+
"react/no-string-refs": ["error", { noTemplateLiterals: true }],
|
|
81
|
+
|
|
82
|
+
// 禁止在函数组件中使用 this(this 在函数组件里通常是 undefined 或全局对象)
|
|
83
|
+
// ----------------------------------------------------------
|
|
84
|
+
// 官方推荐:无
|
|
85
|
+
// 社区最佳:error(Airbnb / TypeScript 严格模板均采用)
|
|
86
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-this-in-sfc.md
|
|
87
|
+
"react/no-this-in-sfc": "error",
|
|
88
|
+
|
|
89
|
+
// 生命周期名 / propTypes 名拼写错误检测
|
|
90
|
+
// ----------------------------------------------------------
|
|
91
|
+
// 官方推荐:无
|
|
92
|
+
// 社区最佳:error(成本极低,能拦下 componenetDidMount 这种致命拼错)
|
|
93
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-typos.md
|
|
94
|
+
"react/no-typos": "error",
|
|
95
|
+
|
|
96
|
+
// 禁止使用不安全的生命周期(componentWillMount / WillReceiveProps / WillUpdate)
|
|
97
|
+
// ----------------------------------------------------------
|
|
98
|
+
// 官方推荐:recommended(默认 error)
|
|
99
|
+
// options: { checkAliases: true }(连 UNSAFE_ 前缀别名也一起检查)
|
|
100
|
+
// 社区最佳:使用默认即可;严格团队加 { checkAliases: true }
|
|
101
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unsafe.md
|
|
102
|
+
"react/no-unsafe": "error",
|
|
103
|
+
|
|
104
|
+
// 禁止 class 组件里存在未使用的方法
|
|
105
|
+
// ----------------------------------------------------------
|
|
106
|
+
// 官方推荐:无
|
|
107
|
+
// options: { ignore: ["someLifecycle"] }(忽略指定方法)
|
|
108
|
+
// 社区最佳:error(对代码质量提升明显)
|
|
109
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unused-class-component-methods.md
|
|
110
|
+
"react/no-unused-class-component-methods": "error",
|
|
111
|
+
|
|
112
|
+
// 禁止未使用的 state
|
|
113
|
+
// ----------------------------------------------------------
|
|
114
|
+
// 官方推荐:无
|
|
115
|
+
// 社区最佳:error(能揭示"忘记消费的 state 字段")
|
|
116
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unused-state.md
|
|
117
|
+
"react/no-unused-state": "error",
|
|
118
|
+
|
|
119
|
+
// 优先使用 ES6 class 而不是 createReactClass
|
|
120
|
+
// ----------------------------------------------------------
|
|
121
|
+
// 官方推荐:无
|
|
122
|
+
// 社区最佳:error(React 官方已把 createReactClass 抽为独立包,视为遗留 API)
|
|
123
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/prefer-es6-class.md
|
|
124
|
+
"react/prefer-es6-class": ["error", "always"],
|
|
125
|
+
|
|
126
|
+
// 无 state / 生命周期时优先使用函数组件
|
|
127
|
+
// ----------------------------------------------------------
|
|
128
|
+
// 官方推荐:无
|
|
129
|
+
// options: { ignorePureComponents: true }(PureComponent 不强制)
|
|
130
|
+
// 社区最佳:error
|
|
131
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/prefer-stateless-function.md
|
|
132
|
+
"react/prefer-stateless-function": ["error", { ignorePureComponents: true }],
|
|
133
|
+
|
|
134
|
+
// render 必须显式 return
|
|
135
|
+
// ----------------------------------------------------------
|
|
136
|
+
// 官方推荐:recommended(默认 error)
|
|
137
|
+
"react/require-render-return": "error",
|
|
138
|
+
|
|
139
|
+
// 生命周期与方法排序
|
|
140
|
+
// ----------------------------------------------------------
|
|
141
|
+
// 官方推荐:无
|
|
142
|
+
// options: { order: ["static-methods", "lifecycle", "everything-else", "render"] }(默认)
|
|
143
|
+
// 社区最佳:error
|
|
144
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/sort-comp.md
|
|
145
|
+
"react/sort-comp": "error",
|
|
146
|
+
|
|
147
|
+
// state 必须在 constructor 中初始化 / 或字段初始化
|
|
148
|
+
// ----------------------------------------------------------
|
|
149
|
+
// 官方推荐:无
|
|
150
|
+
// options: "always"(默认,只能在 constructor 里初始化)| "never"(只能用 class field)
|
|
151
|
+
// 社区最佳:"never"(React 官方文档和 Airbnb 都倾向 class field 语法)
|
|
152
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/state-in-constructor.md
|
|
153
|
+
"react/state-in-constructor": ["error", "never"],
|
|
154
|
+
|
|
155
|
+
// 静态属性放置方式(static 字段 / 类外赋值)
|
|
156
|
+
// ----------------------------------------------------------
|
|
157
|
+
// 官方推荐:无
|
|
158
|
+
// options: "static public field"(默认,推荐)| "property assignment"
|
|
159
|
+
// 社区最佳:static public field
|
|
160
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/static-property-placement.md
|
|
161
|
+
"react/static-property-placement": ["error", "static public field"],
|
|
162
|
+
|
|
163
|
+
// 组件必须有 displayName(否则 DevTools 显示为 Anonymous)
|
|
164
|
+
// ----------------------------------------------------------
|
|
165
|
+
// 官方推荐:recommended(默认 error)
|
|
166
|
+
// options: { ignoreTranspilerName: false }(默认)
|
|
167
|
+
// 社区最佳:默认即可
|
|
168
|
+
"react/display-name": "error",
|
|
169
|
+
},
|
|
170
|
+
};
|