@houheaven/eslint-config-helper 2.1.2 → 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.
Files changed (51) hide show
  1. package/README.md +177 -143
  2. package/exports.js +29 -0
  3. package/lib/html.js +88 -13
  4. package/lib/javascript-es5.js +43 -11
  5. package/lib/javascript-es6.js +51 -11
  6. package/lib/react.js +118 -0
  7. package/lib/supports/support-es5.js +25 -0
  8. package/lib/supports/support-es6.js +35 -0
  9. package/lib/supports/support-html.js +41 -0
  10. package/lib/{support/import-support.js → supports/support-import.js} +16 -11
  11. package/lib/supports/support-react-jsx.js +58 -0
  12. package/lib/supports/support-react-tsx.js +119 -0
  13. package/lib/supports/support-typescript.js +34 -0
  14. package/lib/supports/support-vue2.js +46 -0
  15. package/lib/supports/support-vue3-ts.js +65 -0
  16. package/lib/supports/support-vue3.js +46 -0
  17. package/lib/typescript.js +54 -24
  18. package/lib/vue2.js +51 -13
  19. package/lib/vue3.js +95 -14
  20. package/node_modules/@houheaven/eslint-config-helper-core/rules/react/_deprecated.js +29 -0
  21. package/node_modules/@houheaven/eslint-config-helper-core/rules/react/class-component.js +170 -0
  22. package/node_modules/@houheaven/eslint-config-helper-core/rules/react/dom.js +140 -0
  23. package/node_modules/@houheaven/eslint-config-helper-core/rules/react/fixable.js +239 -0
  24. package/node_modules/@houheaven/eslint-config-helper-core/rules/react/jsx.js +147 -0
  25. package/node_modules/@houheaven/eslint-config-helper-core/rules/react/prop-types.js +128 -0
  26. package/node_modules/@houheaven/eslint-config-helper-core/rules/react/suggestion.js +42 -0
  27. package/node_modules/@houheaven/eslint-config-helper-core/rules/react-hooks/_deprecated.js +19 -0
  28. package/node_modules/@houheaven/eslint-config-helper-core/rules/react-hooks/fixable.js +228 -0
  29. package/node_modules/@houheaven/eslint-config-helper-core/rules/react-hooks/other.js +19 -0
  30. package/node_modules/@houheaven/eslint-config-helper-core/summary.js +9 -1
  31. package/node_modules/@houheaven/eslint-config-helper-core/utils/utils.js +28 -0
  32. package/package.json +17 -40
  33. package/lib/configs/format-prettier.js +0 -478
  34. package/lib/format-es5.js +0 -23
  35. package/lib/format-es6.js +0 -25
  36. package/lib/format-html.js +0 -23
  37. package/lib/format-import.js +0 -23
  38. package/lib/format-typescript.js +0 -28
  39. package/lib/format-vue2.js +0 -23
  40. package/lib/format-vue3.js +0 -23
  41. package/lib/import.js +0 -26
  42. package/lib/support/es5-support.js +0 -32
  43. package/lib/support/es6-support.js +0 -42
  44. package/lib/support/html-support.js +0 -46
  45. package/lib/support/import-bugfix.js +0 -65
  46. package/lib/support/typescript-support.js +0 -27
  47. package/lib/support/vue2-support.js +0 -43
  48. package/lib/support/vue3-support.js +0 -43
  49. package/lib/support/vue3-ts-support.js +0 -51
  50. package/lib/utils/utils.js +0 -2
  51. package/lib/vue3-ts.js +0 -32
@@ -0,0 +1,65 @@
1
+
2
+ // ==========================================================
3
+ // @houheaven/eslint-config-helper/support-vue3-ts
4
+ // ----------------------------------------------------------
5
+ // - 解析 Vue 文件(Vue3 + TypeScript 语法)
6
+ // ==========================================================
7
+
8
+ import globals from "globals";
9
+ import pluginStylistic from "@stylistic/eslint-plugin";
10
+ import pluginTs from "@typescript-eslint/eslint-plugin";
11
+ import pluginVue from "eslint-plugin-vue";
12
+ import parserVue from "vue-eslint-parser";
13
+ import parserTs from "@typescript-eslint/parser";
14
+
15
+ const supportVue3Ts = {
16
+ plugins: {
17
+ "vue": pluginVue,
18
+ "@typescript-eslint": pluginTs,
19
+ "@stylistic": pluginStylistic,
20
+ },
21
+ languageOptions: {
22
+ parser: parserVue,
23
+ sourceType: "module", // 指定代码的来源类型,"module" 表示 ESLint 会在 ECMAScript 模块的上下文中解析代码(即支持 import 和 export 语法)
24
+ ecmaVersion: "latest", // 指定 ECMAScript 版本,"latest" 表示使用最新的 ECMAScript 版本
25
+ parserOptions: {
26
+ sourceType: "module", // 指定代码的来源类型,"module" 表示 ESLint 会在 ECMAScript 模块的上下文中解析代码(即支持 import 和 export 语法)
27
+ ecmaVersion: "latest", // 指定 ECMAScript 版本,"latest" 表示使用最新的 ECMAScript 版本
28
+ ecmaFeatures: {
29
+ jsx: true, // 启用 JSX 全局变量和作用域规则
30
+ },
31
+ // Vue 添加对 TypeScript 的支持
32
+ parser: parserTs,
33
+ extraFileExtensions: [".vue"],
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
+ processor: "vue/vue",
47
+ rules: {
48
+ "indent": "off",
49
+ "max-len": "off",
50
+ "no-irregular-whitespace": "off",
51
+ // 禁止在 Vue 指定的版本上使用不支持的语法
52
+ // https://eslint.vuejs.org/rules/no-unsupported-features.html
53
+ "vue/no-unsupported-features": ["error", {
54
+ "version": "^3.4.0",
55
+ "ignores": [],
56
+ }],
57
+ // 禁止在 props 中使用保留关键字
58
+ // https://eslint.vuejs.org/rules/no-reserved-props.html
59
+ "vue/no-reserved-props": ["error", {
60
+ "vueVersion": 3,
61
+ }],
62
+ },
63
+ };
64
+
65
+ export default supportVue3Ts;
@@ -0,0 +1,46 @@
1
+
2
+ // ==========================================================
3
+ // @houheaven/eslint-config-helper/support-vue3
4
+ // ----------------------------------------------------------
5
+ // - 解析 Vue 文件(Vue3 语法)
6
+ // ==========================================================
7
+
8
+ import globals from "globals";
9
+ import pluginVue from "eslint-plugin-vue";
10
+ import parserVue from "vue-eslint-parser";
11
+
12
+ const supportVue3 = {
13
+ plugins: {
14
+ "vue": pluginVue,
15
+ },
16
+ languageOptions: {
17
+ parser: parserVue,
18
+ sourceType: "module", // 指定代码的来源类型,"module" 表示 ESLint 会在 ECMAScript 模块的上下文中解析代码(即支持 import 和 export 语法)
19
+ ecmaVersion: "latest", // 指定 ECMAScript 版本,"latest" 表示使用最新的 ECMAScript 版本
20
+ globals: {
21
+ ...globals.browser, // 浏览器环境,启用浏览器环境的全局变量(window、document、navigator、alert 等)
22
+ ...globals.node, // Node.js 环境,启用 Node.js 环境的全局变量和作用域规则(__dirname、__filename、process、global 等)
23
+ ...globals.commonjs, // Node.js 环境,启用 CommonJS 的全局变量和作用域规则(require()、module、exports 等)
24
+ ...globals.es6, // 启用 ECMAScript 6 全局变量和作用域规则(let、const、class、import、export 等)
25
+ },
26
+ },
27
+ processor: "vue/vue",
28
+ rules: {
29
+ "indent": "off",
30
+ "max-len": "off",
31
+ "no-irregular-whitespace": "off",
32
+ // 禁止在 Vue 指定的版本上使用不支持的语法
33
+ // https://eslint.vuejs.org/rules/no-unsupported-features.html
34
+ "vue/no-unsupported-features": ["error", {
35
+ "version": "^3.4.0",
36
+ "ignores": [],
37
+ }],
38
+ // 禁止在 props 中使用保留关键字
39
+ // https://eslint.vuejs.org/rules/no-reserved-props.html
40
+ "vue/no-reserved-props": ["error", {
41
+ "vueVersion": 3,
42
+ }],
43
+ },
44
+ };
45
+
46
+ export default supportVue3;
package/lib/typescript.js CHANGED
@@ -5,9 +5,6 @@
5
5
  // - @typescript-eslint/eslint-plugin 定制规则
6
6
  // ==========================================================
7
7
 
8
- // 这个插件为当前类库提供”开箱即用“的能力,使用者无需手动安装当前库的依赖项
9
- require("@rushstack/eslint-patch/modern-module-resolution");
10
-
11
8
  // 132 条规则
12
9
  // 官方有很多种分类
13
10
  // 配置分组:recommended、strict、stylistic
@@ -15,26 +12,59 @@ require("@rushstack/eslint-patch/modern-module-resolution");
15
12
  // 两类分组不能覆盖全部规则,部分规则还存在交叉,所以当前规则分类优先级如下:
16
13
  // extension > fixable > type-checked > has-suggestions > other
17
14
 
18
- const utils = require("@houheaven/eslint-config-helper-core");
15
+ import utils from "@houheaven/eslint-config-helper-core";
16
+ import supportEs6 from "./supports/support-es6.js";
17
+ import supportImport from "./supports/support-import.js";
18
+ import supportTypescript from "./supports/support-typescript.js";
19
+
20
+ // 基础配置
21
+ const tsCommonConfig = {
22
+ files: ["**/*.ts"],
23
+ linterOptions: {
24
+ // 当 eslint-disable 注释配置未使用时,是否报错
25
+ reportUnusedDisableDirectives: "warn",
26
+ },
27
+ };
28
+
29
+ // 基础支持:校验 ES5、ES6、import、ts 规则
30
+ const supportTs = {
31
+ plugins: {
32
+ ...supportImport.plugins,
33
+ ...supportTypescript.plugins,
34
+ },
35
+ languageOptions: {
36
+ ...supportTypescript.languageOptions,
37
+ ...supportEs6.languageOptions,
38
+ },
39
+ settings: {
40
+ ...supportImport.settings,
41
+ },
42
+ // 规则
43
+ rules: {
44
+ // Eslint 基础规则
45
+ ...utils.rules.es5,
46
+ ...utils.rules.es6,
47
+ // eslint-plugin-import 规则
48
+ ...utils.rules.import,
49
+ // @typescript-eslint/eslint-plugin 规则
50
+ ...utils.rules.typescript,
51
+ },
52
+ };
53
+
54
+ // 预设配置:校验 ts 文件的 ES5、ES6、import、ts 规则
55
+ const tsConfig = {
56
+ name: "@houheaven/eslint-config-helper/typescript",
57
+ ...tsCommonConfig,
58
+ ...supportTs,
59
+ };
19
60
 
20
- module.exports = {
21
- // Typescript 规则很多是类型感知规则,需要访问 TypeScript 类型系统(即需要配置 parserOptions)
22
- // typescript-support.js 作用域是 TS 文件,如果没有 overrides 限制,utils.rules.typescript 规则会应用到所有文件
23
- // 其他类型的文件没有经过 parserOptions 处理,就会导致报错,因此此处必须使用 overrides 嵌套
24
- overrides: [
25
- {
26
- files: ["**/*.ts"],
27
- // 当 eslint-disable 注释配置未使用时,是否报错
28
- reportUnusedDisableDirectives: true,
29
- // 继承配置文件
30
- extends: [
31
- // Typescript 支持
32
- "./support/typescript-support.js",
33
- ],
34
- rules: {
35
- // @typescript-eslint/eslint-plugin 规则
36
- ...utils.rules.typescript,
37
- },
38
- },
39
- ],
61
+ export default {
62
+ supports: {
63
+ typescript: supportTs,
64
+ },
65
+ configs: {
66
+ typescript: [
67
+ tsConfig,
68
+ ],
69
+ },
40
70
  };
package/lib/vue2.js CHANGED
@@ -1,27 +1,65 @@
1
-
2
1
  // ==========================================================
3
2
  // @houheaven/eslint-config-helper/vue2
4
3
  // ----------------------------------------------------------
5
4
  // - eslint-plugin-vue 定制规则
6
5
  // ==========================================================
7
6
 
8
- // 这个插件为当前类库提供”开箱即用“的能力,使用者无需手动安装当前库的依赖项
9
- require("@rushstack/eslint-patch/modern-module-resolution");
7
+ import utils from "@houheaven/eslint-config-helper-core";
8
+ import supportEs6 from "./supports/support-es6.js";
9
+ import supportImport from "./supports/support-import.js";
10
+ import supportVue2 from "./supports/support-vue2.js";
10
11
 
11
- const utils = require("@houheaven/eslint-config-helper-core");
12
+ // 基础配置
13
+ const vueCommonConfig = {
14
+ files: ["**/*.vue"],
15
+ linterOptions: {
16
+ // 当 eslint-disable 注释配置未使用时,是否报错
17
+ reportUnusedDisableDirectives: "warn",
18
+ },
19
+ };
12
20
 
13
- module.exports = {
14
- // 当 eslint-disable 注释配置未使用时,是否报错
15
- reportUnusedDisableDirectives: true,
16
- // 继承配置文件
17
- extends: [
18
- // Vue2 支持
19
- "./support/vue2-support.js",
20
- ],
21
- // 规则
21
+ // 基础支持:校验 ES5、ES6、import、vue2 规则
22
+ const supportVue2Config = {
23
+ plugins: {
24
+ ...supportEs6.plugins,
25
+ ...supportImport.plugins,
26
+ ...supportVue2.plugins,
27
+ },
28
+ languageOptions: {
29
+ ...supportVue2.languageOptions,
30
+ ...supportEs6.languageOptions,
31
+ },
32
+ settings: {
33
+ ...supportImport.settings,
34
+ },
35
+ processor: supportVue2.processor,
22
36
  rules: {
37
+ // Eslint 基础规则
38
+ ...utils.rules.es5,
39
+ ...utils.rules.es6,
40
+ // eslint-plugin-import 规则
41
+ ...utils.rules.import,
23
42
  // Vue2 规则
43
+ ...supportVue2.rules,
24
44
  ...utils.rules.vue,
25
45
  ...utils.rules.vue2,
26
46
  },
27
47
  };
48
+
49
+ // 预设配置:校验 vue2 文件的 ES5、ES6、import、vue2 规则
50
+ const vue2Config = {
51
+ name: "@houheaven/eslint-config-helper/vue2",
52
+ ...vueCommonConfig,
53
+ ...supportVue2Config,
54
+ };
55
+
56
+ export default {
57
+ supports: {
58
+ vue2: supportVue2Config,
59
+ },
60
+ configs: {
61
+ vue2: [
62
+ vue2Config,
63
+ ],
64
+ },
65
+ };
package/lib/vue3.js CHANGED
@@ -5,23 +5,104 @@
5
5
  // - eslint-plugin-vue 定制规则
6
6
  // ==========================================================
7
7
 
8
- // 这个插件为当前类库提供”开箱即用“的能力,使用者无需手动安装当前库的依赖项
9
- require("@rushstack/eslint-patch/modern-module-resolution");
10
-
11
- const utils = require("@houheaven/eslint-config-helper-core");
12
-
13
- module.exports = {
14
- // 当 eslint-disable 注释配置未使用时,是否报错
15
- reportUnusedDisableDirectives: true,
16
- // 继承配置文件
17
- extends: [
18
- // Vue3 支持
19
- "./support/vue3-support.js",
20
- ],
21
- // 规则
8
+ import utils from "@houheaven/eslint-config-helper-core";
9
+ import supportEs6 from "./supports/support-es6.js";
10
+ import supportImport from "./supports/support-import.js";
11
+ import supportVue3 from "./supports/support-vue3.js";
12
+ import supportVue3Ts from "./supports/support-vue3-ts.js";
13
+
14
+ // 基础配置
15
+ const vueCommonConfig = {
16
+ files: ["**/*.vue"],
17
+ linterOptions: {
18
+ // 当 eslint-disable 注释配置未使用时,是否报错
19
+ reportUnusedDisableDirectives: "warn",
20
+ },
21
+ };
22
+
23
+ // 基础支持:校验 ES5、ES6、import、vue3 规则
24
+ const supportVue3Config = {
25
+ plugins: {
26
+ ...supportEs6.plugins,
27
+ ...supportImport.plugins,
28
+ ...supportVue3.plugins,
29
+ },
30
+ languageOptions: {
31
+ ...supportVue3.languageOptions,
32
+ ...supportEs6.languageOptions,
33
+ },
34
+ settings: {
35
+ ...supportImport.settings,
36
+ },
37
+ processor: supportVue3.processor,
22
38
  rules: {
39
+ // Eslint 基础规则
40
+ ...utils.rules.es5,
41
+ ...utils.rules.es6,
42
+ // eslint-plugin-import 规则
43
+ ...utils.rules.import,
23
44
  // Vue3 规则
45
+ ...supportVue3.rules,
24
46
  ...utils.rules.vue,
25
47
  ...utils.rules.vue3,
26
48
  },
27
49
  };
50
+
51
+ // 基础支持:校验 ES5、ES6、import、vue3-ts 规则
52
+ const supportVue3TsConfig = {
53
+ plugins: {
54
+ ...supportEs6.plugins,
55
+ ...supportImport.plugins,
56
+ ...supportVue3Ts.plugins,
57
+ },
58
+ languageOptions: {
59
+ ...supportEs6.languageOptions,
60
+ ...supportVue3Ts.languageOptions,
61
+ },
62
+ settings: {
63
+ ...supportImport.settings,
64
+ },
65
+ processor: supportVue3Ts.processor,
66
+ rules: {
67
+ // Eslint 基础规则
68
+ ...utils.rules.es5,
69
+ ...utils.rules.es6,
70
+ // eslint-plugin-import 规则
71
+ ...utils.rules.import,
72
+ // Vue3 规则
73
+ ...supportVue3Ts.rules,
74
+ ...utils.rules.vue,
75
+ ...utils.rules.vue3,
76
+ // @typescript-eslint/eslint-plugin 规则
77
+ ...utils.rules.typescript,
78
+ },
79
+ };
80
+
81
+ // 预设配置:校验 Vue3 文件的 ES5、ES6、import、vue3 规则
82
+ const vue3Config = {
83
+ name: "@houheaven/eslint-config-helper/vue3",
84
+ ...vueCommonConfig,
85
+ ...supportVue3Config,
86
+ };
87
+
88
+ // 预设配置:校验 Vue3 + TypeScript 文件的 ES5、ES6、import、vue3-ts 规则
89
+ const vue3TsConfig = {
90
+ name: "@houheaven/eslint-config-helper/vue3-ts",
91
+ ...vueCommonConfig,
92
+ ...supportVue3TsConfig,
93
+ };
94
+
95
+ export default {
96
+ supports: {
97
+ vue3: supportVue3Config,
98
+ vue3Ts: supportVue3TsConfig,
99
+ },
100
+ configs: {
101
+ vue3: [
102
+ vue3Config,
103
+ ],
104
+ vue3Ts: [
105
+ vue3TsConfig,
106
+ ],
107
+ },
108
+ };
@@ -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
+ };