@houheaven/eslint-config-helper 2.1.2 → 2.1.4
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/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 +4 -4
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// eslint-plugin-react - dom
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// DOM 元素 / 安全 / 常见错误
|
|
6
|
+
// ==========================================================
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
rules: {
|
|
10
|
+
// <button> 必须显式指定 type
|
|
11
|
+
// ----------------------------------------------------------
|
|
12
|
+
// 官方推荐:无(未进入 recommended)
|
|
13
|
+
// options: { button: true, submit: true, reset: true }
|
|
14
|
+
// 分别控制是否允许该类型
|
|
15
|
+
// 社区最佳:["error", { reset: false }]
|
|
16
|
+
// - <button type="reset"> 会清空所在 <form>,误触发风险高,Airbnb/Next.js 都禁用
|
|
17
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/button-has-type.md
|
|
18
|
+
"react/button-has-type": ["error", { reset: false }],
|
|
19
|
+
|
|
20
|
+
// checked 必须搭配 onChange / readOnly
|
|
21
|
+
// ----------------------------------------------------------
|
|
22
|
+
// 官方推荐:无
|
|
23
|
+
// 社区最佳:["error"]
|
|
24
|
+
// - 只写 checked 会得到"受控组件却不受控"的运行时 warning
|
|
25
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/checked-requires-onchange-or-readonly.md
|
|
26
|
+
"react/checked-requires-onchange-or-readonly": "error",
|
|
27
|
+
|
|
28
|
+
// <iframe> 必须带 sandbox
|
|
29
|
+
// ----------------------------------------------------------
|
|
30
|
+
// 官方推荐:无(安全类规则)
|
|
31
|
+
// 社区最佳:["error"]
|
|
32
|
+
// - Snyk / OWASP 都建议默认给 iframe 加 sandbox 属性
|
|
33
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/iframe-missing-sandbox.md
|
|
34
|
+
"react/iframe-missing-sandbox": "error",
|
|
35
|
+
|
|
36
|
+
// 禁止相邻内联元素(可能被搜索引擎/屏幕阅读器合并)
|
|
37
|
+
// ----------------------------------------------------------
|
|
38
|
+
// 官方推荐:无
|
|
39
|
+
// 社区最佳:["error"]
|
|
40
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-adjacent-inline-elements.md
|
|
41
|
+
"react/no-adjacent-inline-elements": "error",
|
|
42
|
+
|
|
43
|
+
// 禁止用数组 index 作为 key
|
|
44
|
+
// ----------------------------------------------------------
|
|
45
|
+
// 官方推荐:无
|
|
46
|
+
// 社区最佳:["error"]
|
|
47
|
+
// - 极少数场景(静态、只增不删且不重排的列表)用 index 是合理的
|
|
48
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-array-index-key.md
|
|
49
|
+
"react/no-array-index-key": "error",
|
|
50
|
+
|
|
51
|
+
// 禁止将 children 通过 prop 传入
|
|
52
|
+
// ----------------------------------------------------------
|
|
53
|
+
// 官方推荐:recommended(默认 error)
|
|
54
|
+
// options: { allowFunctions: false }(默认 false)
|
|
55
|
+
// - 是否允许函数式 children prop(render props 模式)
|
|
56
|
+
// 社区最佳:["error", { allowFunctions: true }](render props 是合法模式)
|
|
57
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-children-prop.md
|
|
58
|
+
"react/no-children-prop": ["error", { allowFunctions: true }],
|
|
59
|
+
|
|
60
|
+
// 禁止使用 dangerouslySetInnerHTML
|
|
61
|
+
// ----------------------------------------------------------
|
|
62
|
+
// 官方推荐:无
|
|
63
|
+
// 社区最佳:["error"]
|
|
64
|
+
// - 有合法场景(渲染富文本)
|
|
65
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-danger.md
|
|
66
|
+
"react/no-danger": "error",
|
|
67
|
+
|
|
68
|
+
// dangerouslySetInnerHTML 与 children 不能共存
|
|
69
|
+
// ----------------------------------------------------------
|
|
70
|
+
// 官方推荐:recommended(默认 error)
|
|
71
|
+
// 社区最佳:["error"](与 recommended 一致)
|
|
72
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-danger-with-children.md
|
|
73
|
+
"react/no-danger-with-children": "error",
|
|
74
|
+
|
|
75
|
+
// 禁止使用已废弃的 API
|
|
76
|
+
// ----------------------------------------------------------
|
|
77
|
+
// 官方推荐:recommended(默认 error)
|
|
78
|
+
// 社区最佳:["error"]
|
|
79
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-deprecated.md
|
|
80
|
+
"react/no-deprecated": "error",
|
|
81
|
+
|
|
82
|
+
// 禁止 findDOMNode
|
|
83
|
+
// ----------------------------------------------------------
|
|
84
|
+
// 官方推荐:recommended(默认 error)
|
|
85
|
+
// 社区最佳:["error"](React 18 已 deprecated,React 19 移除)
|
|
86
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-find-dom-node.md
|
|
87
|
+
"react/no-find-dom-node": "error",
|
|
88
|
+
|
|
89
|
+
// 禁止使用命名空间标签(如 <ns:tag/>)
|
|
90
|
+
// ----------------------------------------------------------
|
|
91
|
+
// 官方推荐:无
|
|
92
|
+
// 社区最佳:["error"]
|
|
93
|
+
// - React 不支持 XML 命名空间语法,写了必然出错
|
|
94
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-namespace.md
|
|
95
|
+
"react/no-namespace": "error",
|
|
96
|
+
|
|
97
|
+
// 禁止把 ReactDOM.render 的返回值当组件实例使用
|
|
98
|
+
// ----------------------------------------------------------
|
|
99
|
+
// 官方推荐:recommended(默认 error)
|
|
100
|
+
// 社区最佳:["error"]
|
|
101
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-render-return-value.md
|
|
102
|
+
"react/no-render-return-value": "error",
|
|
103
|
+
|
|
104
|
+
// 禁止在 render 里定义嵌套组件
|
|
105
|
+
// ----------------------------------------------------------
|
|
106
|
+
// 官方推荐:无
|
|
107
|
+
// options: { allowAsProps: false, propNamePattern: undefined }
|
|
108
|
+
// - allowAsProps: 是否允许作为 render prop 传入
|
|
109
|
+
// 社区最佳:["error", { allowAsProps: true }](render prop 是合法模式)
|
|
110
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unstable-nested-components.md
|
|
111
|
+
"react/no-unstable-nested-components": ["error", { allowAsProps: true }],
|
|
112
|
+
|
|
113
|
+
// 要求使用 PureComponent / memo
|
|
114
|
+
// ----------------------------------------------------------
|
|
115
|
+
// 官方推荐:无
|
|
116
|
+
// 社区最佳:"off"
|
|
117
|
+
// - 强制所有组件用 PureComponent/memo 反而会让代码更复杂、增加内存
|
|
118
|
+
// - Airbnb / Next.js / CRA 都不启用
|
|
119
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/require-optimization.md
|
|
120
|
+
"react/require-optimization": "off",
|
|
121
|
+
|
|
122
|
+
// style 属性必须是对象
|
|
123
|
+
// ----------------------------------------------------------
|
|
124
|
+
// 官方推荐:无
|
|
125
|
+
// options: { allow: [] }
|
|
126
|
+
// - 排除某些自定义组件(如 styled-components 里可能允许字符串)
|
|
127
|
+
// 社区最佳:["error"]
|
|
128
|
+
// - React 的 style 只接受对象,写字符串是常见新手错误
|
|
129
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/style-prop-object.md
|
|
130
|
+
"react/style-prop-object": "error",
|
|
131
|
+
|
|
132
|
+
// void 元素(如 <br/>)不允许 children
|
|
133
|
+
// ----------------------------------------------------------
|
|
134
|
+
// 官方推荐:无
|
|
135
|
+
// 社区最佳:["error"]
|
|
136
|
+
// - <br>text</br> / <img>content</img> 都是无效 HTML
|
|
137
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/void-dom-elements-no-children.md
|
|
138
|
+
"react/void-dom-elements-no-children": "error",
|
|
139
|
+
},
|
|
140
|
+
};
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// eslint-plugin-react - fixable
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// 支持 --fix 的规则(code / whitespace)
|
|
6
|
+
// ==========================================================
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
rules: {
|
|
10
|
+
// 解构赋值风格
|
|
11
|
+
// ----------------------------------------------------------
|
|
12
|
+
// 官方推荐:无
|
|
13
|
+
// options: [ "always" | "never", { ignoreClassFields, destructureInSignature } ]
|
|
14
|
+
// 社区最佳:Airbnb / Next.js 采用 "always"(组件内 props 必须解构)
|
|
15
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/destructuring-assignment.md
|
|
16
|
+
"react/destructuring-assignment": ["warn", "always"],
|
|
17
|
+
|
|
18
|
+
// 函数组件定义风格(function 声明 / 箭头函数 / 命名表达式)
|
|
19
|
+
// ----------------------------------------------------------
|
|
20
|
+
// 官方推荐:无
|
|
21
|
+
// options: { namedComponents, unnamedComponents }
|
|
22
|
+
// 社区最佳:Airbnb 采用 { namedComponents: "arrow-function", unnamedComponents: "arrow-function" }
|
|
23
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/function-component-definition.md
|
|
24
|
+
"react/function-component-definition": ["warn", { namedComponents: "arrow-function", unnamedComponents: "arrow-function" }],
|
|
25
|
+
|
|
26
|
+
// 布尔属性写法:<Foo disabled /> 还是 <Foo disabled={true} />
|
|
27
|
+
// ----------------------------------------------------------
|
|
28
|
+
// 官方推荐:无
|
|
29
|
+
// options: "never"(默认,禁止显式 ={true})| "always"
|
|
30
|
+
// 社区最佳:Airbnb / React 官方风格指南均用 "never"
|
|
31
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md
|
|
32
|
+
"react/jsx-boolean-value": ["warn", "never"],
|
|
33
|
+
|
|
34
|
+
// JSX 闭合括号位置
|
|
35
|
+
// ----------------------------------------------------------
|
|
36
|
+
// 官方推荐:无(stylistic 规则,v7.34+ 建议交给 formatter)
|
|
37
|
+
// options: "tag-aligned" | "line-aligned" | "after-props" | "props-aligned"
|
|
38
|
+
// 社区最佳:Airbnb 采用 "line-aligned"
|
|
39
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md
|
|
40
|
+
"react/jsx-closing-bracket-location": ["warn", "line-aligned"],
|
|
41
|
+
|
|
42
|
+
// JSX 闭合标签位置
|
|
43
|
+
// ----------------------------------------------------------
|
|
44
|
+
// 官方推荐:无(stylistic)
|
|
45
|
+
// options: "tag-aligned" | "line-aligned"
|
|
46
|
+
// 社区最佳:Airbnb 采用 "line-aligned"
|
|
47
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-closing-tag-location.md
|
|
48
|
+
"react/jsx-closing-tag-location": ["warn", "line-aligned"],
|
|
49
|
+
|
|
50
|
+
// JSX 属性/表达式中花括号的必要性
|
|
51
|
+
// ----------------------------------------------------------
|
|
52
|
+
// 官方推荐:无
|
|
53
|
+
// options: { props: "never" | "always", children: "never" | "always", propElementValues: "always" | "ignore" }
|
|
54
|
+
// 社区最佳:Airbnb 采用 { props: "never", children: "never" }(去掉多余大括号)
|
|
55
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-curly-brace-presence.md
|
|
56
|
+
"react/jsx-curly-brace-presence": ["warn", { props: "never", children: "never" }],
|
|
57
|
+
|
|
58
|
+
// JSX 花括号内换行风格
|
|
59
|
+
// ----------------------------------------------------------
|
|
60
|
+
// 官方推荐:无(stylistic)
|
|
61
|
+
// options: { multiline: "consistent" | "require" | "forbid", singleline: "consistent" | "require" | "forbid" }
|
|
62
|
+
// 社区最佳:Airbnb 采用 { multiline: "consistent", singleline: "consistent" }
|
|
63
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-curly-newline.md
|
|
64
|
+
"react/jsx-curly-newline": ["warn", { multiline: "consistent", singleline: "consistent" }],
|
|
65
|
+
|
|
66
|
+
// JSX 花括号内空格
|
|
67
|
+
// ----------------------------------------------------------
|
|
68
|
+
// 官方推荐:无(stylistic)
|
|
69
|
+
// options: { when: "never" | "always", allowMultiline: true }
|
|
70
|
+
// 社区最佳:Airbnb 采用 { when: "never", allowMultiline: true }
|
|
71
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-curly-spacing.md
|
|
72
|
+
"react/jsx-curly-spacing": ["warn", { when: "never", allowMultiline: true }],
|
|
73
|
+
|
|
74
|
+
// JSX 等号两侧空格
|
|
75
|
+
// ----------------------------------------------------------
|
|
76
|
+
// 官方推荐:无(stylistic)
|
|
77
|
+
// options: "never" | "always"
|
|
78
|
+
// 社区最佳:Airbnb 采用 "never"(<Foo bar="baz" /> 而非 <Foo bar ="baz" />)
|
|
79
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-equals-spacing.md
|
|
80
|
+
"react/jsx-equals-spacing": ["warn", "never"],
|
|
81
|
+
|
|
82
|
+
// JSX 第一个属性是否换行
|
|
83
|
+
// ----------------------------------------------------------
|
|
84
|
+
// 官方推荐:无(stylistic)
|
|
85
|
+
// options: "always" | "never" | "multiline" | "multiline-multiprop"
|
|
86
|
+
// 社区最佳:Airbnb 采用 "multiline-multiprop"(多行且多属性才要求首属性换行)
|
|
87
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-first-prop-new-line.md
|
|
88
|
+
"react/jsx-first-prop-new-line": ["warn", "multiline-multiprop"],
|
|
89
|
+
|
|
90
|
+
// Fragment 语法:<> 还是 <Fragment>
|
|
91
|
+
// ----------------------------------------------------------
|
|
92
|
+
// 官方推荐:无
|
|
93
|
+
// options: "syntax"(默认,强制 <>)| "element"(强制 <Fragment>)
|
|
94
|
+
// 社区最佳:Airbnb / Next.js 采用 "syntax"
|
|
95
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-fragments.md
|
|
96
|
+
"react/jsx-fragments": ["warn", "syntax"],
|
|
97
|
+
|
|
98
|
+
// JSX 缩进
|
|
99
|
+
// ----------------------------------------------------------
|
|
100
|
+
// 官方推荐:无(stylistic,v7.34+ 建议交给 Prettier)
|
|
101
|
+
// options: 2 | 4 | "tab"
|
|
102
|
+
// 社区最佳:Airbnb / React 官方 2 空格
|
|
103
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-indent.md
|
|
104
|
+
"react/jsx-indent": ["warn", 2],
|
|
105
|
+
|
|
106
|
+
// JSX 属性缩进
|
|
107
|
+
// ----------------------------------------------------------
|
|
108
|
+
// 官方推荐:无(stylistic)
|
|
109
|
+
// options: 2 | 4 | "tab"
|
|
110
|
+
// 社区最佳:Airbnb 2 空格
|
|
111
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-indent-props.md
|
|
112
|
+
"react/jsx-indent-props": ["warn", 2],
|
|
113
|
+
|
|
114
|
+
// 每行最多的 JSX 属性数
|
|
115
|
+
// ----------------------------------------------------------
|
|
116
|
+
// 官方推荐:无
|
|
117
|
+
// options: { maximum: <number>, when: "always" | "multiline" }
|
|
118
|
+
// 社区最佳:Airbnb 采用 { maximum: 1, when: "multiline" }(多行时每行一个属性)
|
|
119
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-max-props-per-line.md
|
|
120
|
+
"react/jsx-max-props-per-line": ["warn", { maximum: 1, when: "multiline" }],
|
|
121
|
+
|
|
122
|
+
// JSX 元素之间是否需要空行
|
|
123
|
+
// ----------------------------------------------------------
|
|
124
|
+
// 官方推荐:无(stylistic)
|
|
125
|
+
// options: { prevent: false, allowMultilines: false }
|
|
126
|
+
// 社区最佳:默认关闭;开启 { prevent: true } 可禁止元素间空行
|
|
127
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-newline.md
|
|
128
|
+
"react/jsx-newline": "off",
|
|
129
|
+
|
|
130
|
+
// 禁止 JSX 中出现 && / ?: 短路带来的 0/NaN 渲染
|
|
131
|
+
// ----------------------------------------------------------
|
|
132
|
+
// 官方推荐:无(但强烈推荐启用)
|
|
133
|
+
// options: { validStrategies: ["ternary", "coerce"] }
|
|
134
|
+
// 社区最佳:React 官方文档明确警告这个陷阱,一律 error
|
|
135
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-leaked-render.md
|
|
136
|
+
"react/jsx-no-leaked-render": ["warn", { validStrategies: ["ternary", "coerce"] }],
|
|
137
|
+
|
|
138
|
+
// target="_blank" 必须带 rel="noreferrer"
|
|
139
|
+
// ----------------------------------------------------------
|
|
140
|
+
// 官方推荐:recommended(默认 error)
|
|
141
|
+
// options: { allowReferrer, enforceDynamicLinks: "always", warnOnSpreadAttributes, forms }
|
|
142
|
+
// 社区最佳:保持默认 + warnOnSpreadAttributes:true(覆盖 spread 场景)
|
|
143
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-target-blank.md
|
|
144
|
+
"react/jsx-no-target-blank": ["warn", { warnOnSpreadAttributes: true, forms: true }],
|
|
145
|
+
|
|
146
|
+
// 禁止无意义的 Fragment
|
|
147
|
+
// ----------------------------------------------------------
|
|
148
|
+
// 官方推荐:无
|
|
149
|
+
// options: { allowExpressions: true }
|
|
150
|
+
// 社区最佳:允许 { allowExpressions: true } 以兼容 <>{list.map(...)}</>
|
|
151
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-useless-fragment.md
|
|
152
|
+
"react/jsx-no-useless-fragment": ["warn", { allowExpressions: true }],
|
|
153
|
+
|
|
154
|
+
// 每行只允许一个 JSX 表达式
|
|
155
|
+
// ----------------------------------------------------------
|
|
156
|
+
// 官方推荐:无(stylistic)
|
|
157
|
+
// options: { allow: "none" | "literal" | "single-child" | "single-line" | "non-jsx" }
|
|
158
|
+
// 社区最佳:Airbnb 采用 { allow: "single-child" }
|
|
159
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-one-expression-per-line.md
|
|
160
|
+
"react/jsx-one-expression-per-line": ["warn", { allow: "single-child" }],
|
|
161
|
+
|
|
162
|
+
// JSX 属性之间禁止多余空格
|
|
163
|
+
// ----------------------------------------------------------
|
|
164
|
+
// 官方推荐:无(stylistic)
|
|
165
|
+
// 社区最佳:一律 error
|
|
166
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-props-no-multi-spaces.md
|
|
167
|
+
"react/jsx-props-no-multi-spaces": "warn",
|
|
168
|
+
|
|
169
|
+
// JSX 属性排序
|
|
170
|
+
// ----------------------------------------------------------
|
|
171
|
+
// 官方推荐:无
|
|
172
|
+
// options: { callbacksLast, shorthandFirst, ignoreCase, reservedFirst }
|
|
173
|
+
// 社区最佳:Airbnb 采用 { callbacksLast: true, shorthandFirst: true, ignoreCase: true, reservedFirst: true }
|
|
174
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-sort-props.md
|
|
175
|
+
"react/jsx-sort-props": ["warn", { callbacksLast: true, shorthandFirst: true, ignoreCase: true, reservedFirst: true }],
|
|
176
|
+
|
|
177
|
+
// JSX 标签周围空格风格
|
|
178
|
+
// ----------------------------------------------------------
|
|
179
|
+
// 官方推荐:无(stylistic)
|
|
180
|
+
// options: { closingSlash, beforeSelfClosing, afterOpening, beforeClosing }
|
|
181
|
+
// 社区最佳:Airbnb 默认全部启用
|
|
182
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-tag-spacing.md
|
|
183
|
+
"react/jsx-tag-spacing": ["warn", { closingSlash: "never", beforeSelfClosing: "always", afterOpening: "never", beforeClosing: "never" }],
|
|
184
|
+
|
|
185
|
+
// 多行 JSX 是否用括号包裹
|
|
186
|
+
// ----------------------------------------------------------
|
|
187
|
+
// 官方推荐:无(stylistic)
|
|
188
|
+
// options: { declaration, assignment, return, arrow, condition, logical, prop }
|
|
189
|
+
// 社区最佳:Airbnb 采用 declaration/assignment/return/arrow=parens-new-line
|
|
190
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-wrap-multilines.md
|
|
191
|
+
"react/jsx-wrap-multilines": ["warn", {
|
|
192
|
+
declaration: "parens-new-line",
|
|
193
|
+
assignment: "parens-new-line",
|
|
194
|
+
return: "parens-new-line",
|
|
195
|
+
arrow: "parens-new-line",
|
|
196
|
+
condition: "parens-new-line",
|
|
197
|
+
logical: "parens-new-line",
|
|
198
|
+
prop: "parens-new-line",
|
|
199
|
+
}],
|
|
200
|
+
|
|
201
|
+
// 禁止在生命周期方法上使用箭头函数
|
|
202
|
+
// ----------------------------------------------------------
|
|
203
|
+
// 官方推荐:无(性能相关)
|
|
204
|
+
// 社区最佳:一律 error(避免 shouldComponentUpdate/render 每次生成新引用)
|
|
205
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-arrow-function-lifecycle.md
|
|
206
|
+
"react/no-arrow-function-lifecycle": "warn",
|
|
207
|
+
|
|
208
|
+
// 禁止 JSX 使用未知/无效的 DOM 属性(如 class)
|
|
209
|
+
// ----------------------------------------------------------
|
|
210
|
+
// 官方推荐:recommended(默认 error)
|
|
211
|
+
// options: { ignore, requireDataLowercase }
|
|
212
|
+
// 社区最佳:保持默认;开启 requireDataLowercase:true 强制 data-* 全小写
|
|
213
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unknown-property.md
|
|
214
|
+
"react/no-unknown-property": ["warn", { requireDataLowercase: true }],
|
|
215
|
+
|
|
216
|
+
// 推荐 props 只读
|
|
217
|
+
// ----------------------------------------------------------
|
|
218
|
+
// 官方推荐:无(Flow 项目专用;纯 TS/JS 项目一般不启用)
|
|
219
|
+
// 社区最佳:Flow 项目 error;TS 项目 off
|
|
220
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/prefer-read-only-props.md
|
|
221
|
+
"react/prefer-read-only-props": "off",
|
|
222
|
+
|
|
223
|
+
// 无子节点标签自闭合
|
|
224
|
+
// ----------------------------------------------------------
|
|
225
|
+
// 官方推荐:无
|
|
226
|
+
// options: { component: true, html: true }
|
|
227
|
+
// 社区最佳:Airbnb 默认 component/html 都启用
|
|
228
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/self-closing-comp.md
|
|
229
|
+
"react/self-closing-comp": ["warn", { component: true, html: true }],
|
|
230
|
+
|
|
231
|
+
// propTypes 排序
|
|
232
|
+
// ----------------------------------------------------------
|
|
233
|
+
// 官方推荐:无
|
|
234
|
+
// options: { callbacksLast, ignoreCase, requiredFirst, sortShapeProp, noSortAlphabetically }
|
|
235
|
+
// 社区最佳:Airbnb 采用 { callbacksLast: true, ignoreCase: true, requiredFirst: true, sortShapeProp: true }
|
|
236
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/sort-prop-types.md
|
|
237
|
+
"react/sort-prop-types": ["warn", { callbacksLast: true, ignoreCase: true, requiredFirst: true, sortShapeProp: true }],
|
|
238
|
+
},
|
|
239
|
+
};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// eslint-plugin-react - jsx
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// JSX 语法与最佳实践类规则
|
|
6
|
+
// ==========================================================
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
rules: {
|
|
10
|
+
// JSX 元素之间不允许直接相邻的行内元素
|
|
11
|
+
// ----------------------------------------------------------
|
|
12
|
+
// 官方推荐:无(未进入 recommended),本规则无 options
|
|
13
|
+
// 社区最佳:默认关闭;Airbnb / Next.js 都未启用;对可访问性有强要求时可打开
|
|
14
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-child-element-spacing.md
|
|
15
|
+
"react/jsx-child-element-spacing": "off",
|
|
16
|
+
|
|
17
|
+
// JSX 只允许出现在特定扩展名的文件里(jsx / tsx)
|
|
18
|
+
// ----------------------------------------------------------
|
|
19
|
+
// 官方推荐:无
|
|
20
|
+
// options: { extensions: [".jsx"] }(默认)
|
|
21
|
+
// 社区最佳:Airbnb 采用 { extensions: [".jsx"] };带 TS 场景多用 [".jsx", ".tsx"]
|
|
22
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-filename-extension.md
|
|
23
|
+
"react/jsx-filename-extension": ["error", { extensions: [".jsx", ".tsx"] }],
|
|
24
|
+
|
|
25
|
+
// JSX 事件处理函数命名(onXxx / handleXxx)
|
|
26
|
+
// ----------------------------------------------------------
|
|
27
|
+
// 官方推荐:无
|
|
28
|
+
// options: { eventHandlerPrefix: "handle", eventHandlerPropPrefix: "on" }(默认)
|
|
29
|
+
// 社区最佳:默认配置最贴近约定俗成(handleClick -> onClick)
|
|
30
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-handler-names.md
|
|
31
|
+
"react/jsx-handler-names": ["error", { eventHandlerPrefix: "handle", eventHandlerPropPrefix: "on" }],
|
|
32
|
+
|
|
33
|
+
// 迭代渲染必须带 key
|
|
34
|
+
// ----------------------------------------------------------
|
|
35
|
+
// 官方推荐:recommended(默认 error)
|
|
36
|
+
// options: { checkFragmentShorthand: true, checkKeyMustBeforeSpread: true, warnOnDuplicates: true }
|
|
37
|
+
// 社区最佳:把可选项全部打开可以覆盖 <>...</>、spread 前后顺序、重复 key 等场景
|
|
38
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-key.md
|
|
39
|
+
"react/jsx-key": ["error", { checkFragmentShorthand: true, checkKeyMustBeforeSpread: true, warnOnDuplicates: true }],
|
|
40
|
+
|
|
41
|
+
// JSX 嵌套最大深度
|
|
42
|
+
// ----------------------------------------------------------
|
|
43
|
+
// 官方推荐:无
|
|
44
|
+
// options: { max: <number> }
|
|
45
|
+
// 社区最佳:一般 4-5 层可读性最好;默认 off 是为了避免过度约束
|
|
46
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-max-depth.md
|
|
47
|
+
"react/jsx-max-depth": ["error", { max: 5 }],
|
|
48
|
+
|
|
49
|
+
// 禁止在 JSX 属性里使用 .bind / 箭头函数(造成每次渲染新引用)
|
|
50
|
+
// ----------------------------------------------------------
|
|
51
|
+
// 官方推荐:无
|
|
52
|
+
// options: { allowArrowFunctions: false, allowBind: false, allowFunctions: false, ignoreRefs: true, ignoreDOMComponents: false }
|
|
53
|
+
// 社区最佳:Airbnb 采用默认严格值;性能敏感场景推荐 ignoreRefs:true 减少误报
|
|
54
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md
|
|
55
|
+
"react/jsx-no-bind": ["error", { ignoreRefs: true }],
|
|
56
|
+
|
|
57
|
+
// 禁止把注释文本节点当成 JSX 内容
|
|
58
|
+
// ----------------------------------------------------------
|
|
59
|
+
// 官方推荐:recommended(默认 error)
|
|
60
|
+
// 社区最佳:无 options,直接沿用 recommended
|
|
61
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-comment-textnodes.md
|
|
62
|
+
"react/jsx-no-comment-textnodes": "error",
|
|
63
|
+
|
|
64
|
+
// 禁止在 Context.Provider 的 value 里传新构造的对象/数组/函数
|
|
65
|
+
// ----------------------------------------------------------
|
|
66
|
+
// 官方推荐:无
|
|
67
|
+
// 社区最佳:React 官方性能文档推荐启用,避免 Provider 每次渲染都触发订阅者重渲染
|
|
68
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-constructed-context-values.md
|
|
69
|
+
"react/jsx-no-constructed-context-values": "error",
|
|
70
|
+
|
|
71
|
+
// JSX 同一元素上禁止重复属性
|
|
72
|
+
// ----------------------------------------------------------
|
|
73
|
+
// 官方推荐:recommended(默认 error)
|
|
74
|
+
// options: { ignoreCase: true }(默认忽略大小写差异)
|
|
75
|
+
// 社区最佳:默认值即最佳;ignoreCase:true 避免 className/ClassName 这类漏检
|
|
76
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-duplicate-props.md
|
|
77
|
+
"react/jsx-no-duplicate-props": ["error", { ignoreCase: true }],
|
|
78
|
+
|
|
79
|
+
// 禁止 JSX 里出现字面量文本(要求 i18n)
|
|
80
|
+
// ----------------------------------------------------------
|
|
81
|
+
// 官方推荐:无
|
|
82
|
+
// options 众多,包含 noStrings / allowedStrings / ignoreProps 等
|
|
83
|
+
// 社区最佳:i18n 强要求时启用;一般项目默认 off,误报很多
|
|
84
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-literals.md
|
|
85
|
+
"react/jsx-no-literals": "off",
|
|
86
|
+
|
|
87
|
+
// 禁止 href="javascript:..."
|
|
88
|
+
// ----------------------------------------------------------
|
|
89
|
+
// 官方推荐:无(但 XSS 安全推荐启用)
|
|
90
|
+
// options: [{ name: "Link", props: ["to", "href"] }], { includeFromSettings: true }
|
|
91
|
+
// 社区最佳:一律 error;Airbnb / Next.js 都已启用
|
|
92
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-script-url.md
|
|
93
|
+
"react/jsx-no-script-url": "error",
|
|
94
|
+
|
|
95
|
+
// JSX 中使用了未定义的组件
|
|
96
|
+
// ----------------------------------------------------------
|
|
97
|
+
// 官方推荐:recommended(默认 error)
|
|
98
|
+
// options: { allowGlobals: false }
|
|
99
|
+
// 社区最佳:保持默认;配 no-undef 一起用可覆盖 JSX 与非 JSX 两种场景
|
|
100
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-undef.md
|
|
101
|
+
"react/jsx-no-undef": ["error", { allowGlobals: false }],
|
|
102
|
+
|
|
103
|
+
// 组件必须 PascalCase
|
|
104
|
+
// ----------------------------------------------------------
|
|
105
|
+
// 官方推荐:无
|
|
106
|
+
// options: { allowAllCaps: true, allowNamespace: false, allowLeadingUnderscore: false, ignore: [] }
|
|
107
|
+
// 社区最佳:Airbnb 采用 allowAllCaps:true 便于 <UI.Section /> / <SVG /> 之类
|
|
108
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-pascal-case.md
|
|
109
|
+
"react/jsx-pascal-case": ["error", { allowAllCaps: true, allowNamespace: true }],
|
|
110
|
+
|
|
111
|
+
// 禁止在同一个元素上使用多个 spread 属性
|
|
112
|
+
// ----------------------------------------------------------
|
|
113
|
+
// 官方推荐:v7.34+ 起进入 recommended(默认 error)
|
|
114
|
+
// 社区最佳:直接沿用 recommended
|
|
115
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-props-no-spread-multi.md
|
|
116
|
+
"react/jsx-props-no-spread-multi": "error",
|
|
117
|
+
|
|
118
|
+
// 禁止在 JSX 上使用 spread 属性
|
|
119
|
+
// ----------------------------------------------------------
|
|
120
|
+
// 官方推荐:无
|
|
121
|
+
// options: { html: "enforce" | "ignore", custom: "enforce" | "ignore", exceptions: [] }
|
|
122
|
+
// 社区最佳:多数团队仅对 html 元素启用(避免向 DOM 泼未知属性),自定义组件放行
|
|
123
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-props-no-spreading.md
|
|
124
|
+
"react/jsx-props-no-spreading": ["error", { html: "enforce", custom: "ignore", explicitSpread: "ignore" }],
|
|
125
|
+
|
|
126
|
+
// 使 React 变量在使用 JSX 时不被 no-unused-vars 误判
|
|
127
|
+
// ----------------------------------------------------------
|
|
128
|
+
// 官方推荐:recommended(默认 error),jsx-runtime 里会置为 off
|
|
129
|
+
// 社区最佳:新 JSX transform(React 17+)下建议 off;老项目保持 error
|
|
130
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-uses-react.md
|
|
131
|
+
"react/jsx-uses-react": "error",
|
|
132
|
+
|
|
133
|
+
// 使 JSX 中引用的变量不被 no-unused-vars 误判
|
|
134
|
+
// ----------------------------------------------------------
|
|
135
|
+
// 官方推荐:recommended(默认 error)
|
|
136
|
+
// 社区最佳:无 options,直接沿用 recommended
|
|
137
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-uses-vars.md
|
|
138
|
+
"react/jsx-uses-vars": "error",
|
|
139
|
+
|
|
140
|
+
// 使用 JSX 时必须能在作用域内看到 React
|
|
141
|
+
// ----------------------------------------------------------
|
|
142
|
+
// 官方推荐:recommended(默认 error),jsx-runtime 里会置为 off
|
|
143
|
+
// 社区最佳:新 JSX transform(React 17+)下建议 off;本项目 demo 未启用新 transform,保持 error
|
|
144
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/react-in-jsx-scope.md
|
|
145
|
+
"react/react-in-jsx-scope": "error",
|
|
146
|
+
},
|
|
147
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// eslint-plugin-react - prop-types
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// PropTypes 与组件契约相关
|
|
6
|
+
// ==========================================================
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
rules: {
|
|
10
|
+
// 布尔 prop 命名(is / has 等前缀)
|
|
11
|
+
// ----------------------------------------------------------
|
|
12
|
+
// 官方推荐:无
|
|
13
|
+
// options:
|
|
14
|
+
// - rule: string 正则,匹配"合法"的布尔 prop 名
|
|
15
|
+
// - message: string 报错时的自定义提示
|
|
16
|
+
// - propTypeNames: string[] 需要校验的 propType 名称(默认 ["bool"])
|
|
17
|
+
// - validateNested: boolean 是否深入 shape 内部
|
|
18
|
+
// 社区最佳:Airbnb 规范经常配 rule 强制 is/has/should/can 前缀
|
|
19
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/boolean-prop-naming.md
|
|
20
|
+
// - Airbnb:https://github.com/airbnb/javascript/tree/master/react
|
|
21
|
+
"react/boolean-prop-naming": [
|
|
22
|
+
"error",
|
|
23
|
+
{
|
|
24
|
+
rule: "^(is|has|should|can|will|did)[A-Z]([A-Za-z0-9]?)+",
|
|
25
|
+
message: "布尔 prop 建议使用 is/has/should/can/will/did 前缀",
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
|
|
29
|
+
// defaultProps 必须与 propTypes 对应
|
|
30
|
+
// ----------------------------------------------------------
|
|
31
|
+
// 官方推荐:无
|
|
32
|
+
// options: { allowRequiredDefaults: false }(默认,禁止给 required prop 加 default)
|
|
33
|
+
// 社区最佳:一律 error;React 18.3 起 defaultProps 对函数组件已弃用,规则可继续用于 class 组件
|
|
34
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/default-props-match-prop-types.md
|
|
35
|
+
"react/default-props-match-prop-types": ["error", { allowRequiredDefaults: false }],
|
|
36
|
+
|
|
37
|
+
// 禁止某些属性用在自定义组件上
|
|
38
|
+
// ----------------------------------------------------------
|
|
39
|
+
// 官方推荐:无(需要指定 forbid 列表才有意义)
|
|
40
|
+
// options: { forbid: [<属性名/对象>] }
|
|
41
|
+
// 社区最佳:禁止把 className / style 透传给自定义组件(破坏封装)
|
|
42
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/forbid-component-props.md
|
|
43
|
+
"react/forbid-component-props": ["error", { forbid: ["className", "style"] }],
|
|
44
|
+
|
|
45
|
+
// 禁止某些属性用在 DOM 元素上
|
|
46
|
+
// ----------------------------------------------------------
|
|
47
|
+
// 官方推荐:无(需要指定 forbid 列表才有意义)
|
|
48
|
+
// options: { forbid: [<属性名/对象>] }
|
|
49
|
+
// 社区最佳:可用于封禁危险/自研属性,此处演示 forbid: ["id"]
|
|
50
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/forbid-dom-props.md
|
|
51
|
+
"react/forbid-dom-props": ["error", { forbid: ["id"] }],
|
|
52
|
+
|
|
53
|
+
// 禁止使用某些元素
|
|
54
|
+
// ----------------------------------------------------------
|
|
55
|
+
// 官方推荐:无(需要指定 forbid 列表才有意义)
|
|
56
|
+
// options: { forbid: [<元素名/对象>] }
|
|
57
|
+
// 社区最佳:禁止已废弃 / 危险标签(marquee / blink 等)
|
|
58
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/forbid-elements.md
|
|
59
|
+
"react/forbid-elements": ["error", { forbid: ["marquee", "blink", "font"] }],
|
|
60
|
+
|
|
61
|
+
// 禁止访问其他组件的 propTypes
|
|
62
|
+
// ----------------------------------------------------------
|
|
63
|
+
// 官方推荐:无
|
|
64
|
+
// options: { allowInPropTypes: true } —— 允许在自身 propTypes 定义中引用(默认 false)
|
|
65
|
+
// 社区最佳:Airbnb 用 error 且默认关闭 allowInPropTypes
|
|
66
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/forbid-foreign-prop-types.md
|
|
67
|
+
"react/forbid-foreign-prop-types": ["error", { allowInPropTypes: false }],
|
|
68
|
+
|
|
69
|
+
// 禁止使用过于宽泛的 propTypes(如 any / array / object)
|
|
70
|
+
// ----------------------------------------------------------
|
|
71
|
+
// 官方推荐:无
|
|
72
|
+
// options: { forbid: ["any", "array", "object"] } —— Airbnb 默认值
|
|
73
|
+
// 社区最佳:Airbnb 规范强制此三者
|
|
74
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/forbid-prop-types.md
|
|
75
|
+
// - Airbnb:https://github.com/airbnb/javascript/tree/master/react
|
|
76
|
+
"react/forbid-prop-types": ["error", { forbid: ["any", "array", "object"] }],
|
|
77
|
+
|
|
78
|
+
// 禁止对象作为 defaultProp(会共享引用,可能引发 bug)
|
|
79
|
+
// ----------------------------------------------------------
|
|
80
|
+
// 官方推荐:无
|
|
81
|
+
// 社区最佳:一律 error
|
|
82
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-object-type-as-default-prop.md
|
|
83
|
+
"react/no-object-type-as-default-prop": "error",
|
|
84
|
+
|
|
85
|
+
// 禁止声明但未使用的 propTypes
|
|
86
|
+
// ----------------------------------------------------------
|
|
87
|
+
// 官方推荐:无
|
|
88
|
+
// 社区最佳:Airbnb / Next.js 默认 error
|
|
89
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unused-prop-types.md
|
|
90
|
+
"react/no-unused-prop-types": "error",
|
|
91
|
+
|
|
92
|
+
// 使用 propTypes 时必须为每个 prop 声明类型
|
|
93
|
+
// ----------------------------------------------------------
|
|
94
|
+
// 官方推荐:recommended(默认 error)
|
|
95
|
+
// 社区最佳:Airbnb / CRA / Next.js 都启用;TypeScript 项目下常可关闭以避免重复
|
|
96
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/prop-types.md
|
|
97
|
+
"react/prop-types": "error",
|
|
98
|
+
|
|
99
|
+
// 非必填 prop 必须有 defaultProps
|
|
100
|
+
// ----------------------------------------------------------
|
|
101
|
+
// 官方推荐:无
|
|
102
|
+
// options:
|
|
103
|
+
// - forbidDefaultForRequired: boolean 是否禁止给 required prop 加 default
|
|
104
|
+
// - ignoreFunctionalComponents: boolean 是否忽略函数组件
|
|
105
|
+
// - classes: "defaultProps" | "ignore"(class 组件如何处理)
|
|
106
|
+
// - functions: "defaultArguments" | "defaultProps" | "ignore"
|
|
107
|
+
// 社区最佳:Airbnb 用 error 并要求 forbidDefaultForRequired
|
|
108
|
+
// 说明:为了在示例文件中和 default-props-match-prop-types 演示解耦,
|
|
109
|
+
// 这里默认关闭 forbidDefaultForRequired,让"required + default"这种
|
|
110
|
+
// 组合只触发 default-props-match-prop-types 一条报错。
|
|
111
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/require-default-props.md
|
|
112
|
+
"react/require-default-props": ["error", { forbidDefaultForRequired: false }],
|
|
113
|
+
|
|
114
|
+
// 更严格的 propTypes(exact-props)
|
|
115
|
+
// ----------------------------------------------------------
|
|
116
|
+
// 官方推荐:无
|
|
117
|
+
// 社区最佳:仅在与 flow 配合时较有意义;React + PropTypes 项目下可选
|
|
118
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/prefer-exact-props.md
|
|
119
|
+
"react/prefer-exact-props": "error",
|
|
120
|
+
|
|
121
|
+
// defaultProps 排序
|
|
122
|
+
// ----------------------------------------------------------
|
|
123
|
+
// 官方推荐:无
|
|
124
|
+
// 社区最佳:可选风格约束;启用时统一 error
|
|
125
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/sort-default-props.md
|
|
126
|
+
"react/sort-default-props": "error",
|
|
127
|
+
},
|
|
128
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// eslint-plugin-react - suggestion (hasSuggestions)
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// 不自动 --fix,但会在编辑器里给出修复建议
|
|
6
|
+
// ==========================================================
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
rules: {
|
|
10
|
+
// forwardRef 组件必须接收 ref
|
|
11
|
+
// ----------------------------------------------------------
|
|
12
|
+
// 官方推荐:无(未进入 recommended),本规则无 options
|
|
13
|
+
// 社区最佳:一律 error(React 官方 forwardRef 文档也强调不接收 ref 的组件应改用普通函数组件)
|
|
14
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/forward-ref-uses-ref.md
|
|
15
|
+
// - React 官方:https://react.dev/reference/react/forwardRef#my-component-is-wrapped-in-a-forwardref-but-the-ref-to-it-is-always-null
|
|
16
|
+
"react/forward-ref-uses-ref": "error",
|
|
17
|
+
|
|
18
|
+
// 强制 useState 命名遵循 [value, setValue]
|
|
19
|
+
// ----------------------------------------------------------
|
|
20
|
+
// 官方推荐:无(未进入 recommended)
|
|
21
|
+
// options: { allowDestructuredState: false }(默认,禁止解构初值)
|
|
22
|
+
// 社区最佳:多个知名脚手架(Next.js/Vite React 模板)默认关闭;enforce 命名一致性时用 error
|
|
23
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/hook-use-state.md
|
|
24
|
+
"react/hook-use-state": ["error", { allowDestructuredState: false }],
|
|
25
|
+
|
|
26
|
+
// 禁止使用非法的 HTML 属性(默认只校验 rel 属性)
|
|
27
|
+
// ----------------------------------------------------------
|
|
28
|
+
// 官方推荐:无(未进入 recommended)
|
|
29
|
+
// options: 传入需要校验的属性数组,默认 ["rel"]
|
|
30
|
+
// 社区最佳:默认值即最实用配置(rel 值最容易写错,如 rel="noopener noreferer" 拼写)
|
|
31
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-invalid-html-attribute.md
|
|
32
|
+
"react/no-invalid-html-attribute": ["error", ["rel"]],
|
|
33
|
+
|
|
34
|
+
// 禁止 JSX 中未转义的 HTML 实体(> " ' } 等)
|
|
35
|
+
// ----------------------------------------------------------
|
|
36
|
+
// 官方推荐:recommended(默认 error)
|
|
37
|
+
// options: { forbid: [">", "\"", "'", "}"] }(默认值)
|
|
38
|
+
// 社区最佳:保持默认 forbid 列表;Airbnb / Next.js / CRA 都直接沿用 recommended
|
|
39
|
+
// - 官方文档:https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unescaped-entities.md
|
|
40
|
+
"react/no-unescaped-entities": ["error", { forbid: [">", "\"", "'", "}"] }],
|
|
41
|
+
},
|
|
42
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// eslint-plugin-react-hooks - deprecated
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// 已弃用规则,全部保持 "off";如需查看历史行为,可在示例文件里
|
|
6
|
+
// 通过 /* eslint react-hooks/xxx: "error" */ 临时启用。
|
|
7
|
+
// ==========================================================
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
rules: {
|
|
11
|
+
// 校验"组件工厂函数"内不得再定义组件/Hook
|
|
12
|
+
// ----------------------------------------------------------
|
|
13
|
+
// 已废弃(v7.1.0 移除),插件内部改由 static-components 覆盖同类场景
|
|
14
|
+
// 官方推荐:无(deprecated 规则一律不进入 recommended)
|
|
15
|
+
// 社区最佳:保持 "off",改用 react-hooks/static-components 替代
|
|
16
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/static-components
|
|
17
|
+
"react-hooks/component-hook-factories": "off",
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// eslint-plugin-react-hooks - fixable
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// 支持 --fix 或提供 code action 建议的规则,一律 "warn":
|
|
6
|
+
// 编辑器/CI 一跑 --fix 就能自动纠正,不必阻断开发。
|
|
7
|
+
// ==========================================================
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
rules: {
|
|
11
|
+
// useEffect / useMemo / useCallback 等 Hook 依赖项穷尽性校验
|
|
12
|
+
// ----------------------------------------------------------
|
|
13
|
+
// 官方推荐:recommended(默认 warn)
|
|
14
|
+
// options: { additionalHooks?: string, enableDangerousAutofixThisMayCauseInfiniteLoops?: boolean,
|
|
15
|
+
// experimental_autoDependenciesHooks?: string[], requireExplicitEffectDeps?: boolean }
|
|
16
|
+
// 社区最佳:保持 recommended 默认;如有自定义 hook 如 useDeepCompareEffect,可通过 additionalHooks 正则接入
|
|
17
|
+
// - 官方文档:https://github.com/facebook/react/issues/14920
|
|
18
|
+
"react-hooks/exhaustive-deps": "warn",
|
|
19
|
+
|
|
20
|
+
// Validates the rules of hooks(新版可自动修复的 Rules of Hooks 检查器)
|
|
21
|
+
// ----------------------------------------------------------
|
|
22
|
+
// 官方推荐:无(recommended-latest 未启用,rules-of-hooks 仍是主入口)
|
|
23
|
+
// options: 无
|
|
24
|
+
// 社区最佳:可与 rules-of-hooks 并存作为渐进式迁移开关;正式项目通常保持默认关闭
|
|
25
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/hooks
|
|
26
|
+
"react-hooks/hooks": "off",
|
|
27
|
+
|
|
28
|
+
// 禁止把首字母大写的函数当作普通函数调用(应写为 JSX)
|
|
29
|
+
// ----------------------------------------------------------
|
|
30
|
+
// 官方推荐:无
|
|
31
|
+
// options: 无
|
|
32
|
+
// 社区最佳:React Compiler 场景推荐启用,避免误把组件当普通函数
|
|
33
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/capitalized-calls
|
|
34
|
+
"react-hooks/capitalized-calls": "warn",
|
|
35
|
+
|
|
36
|
+
// 组件必须是静态定义,禁止在渲染过程中动态创建
|
|
37
|
+
// ----------------------------------------------------------
|
|
38
|
+
// 官方推荐:recommended(默认 error)
|
|
39
|
+
// options: 无
|
|
40
|
+
// 社区最佳:一律启用,避免 state 被意外重置或引发过量 re-render
|
|
41
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/static-components
|
|
42
|
+
"react-hooks/static-components": "warn",
|
|
43
|
+
|
|
44
|
+
// useMemo 常见误用检查(如缺失依赖、错误 return)
|
|
45
|
+
// ----------------------------------------------------------
|
|
46
|
+
// 官方推荐:recommended(默认 error)
|
|
47
|
+
// options: 无
|
|
48
|
+
// 社区最佳:保持启用
|
|
49
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/use-memo
|
|
50
|
+
"react-hooks/use-memo": "warn",
|
|
51
|
+
|
|
52
|
+
// useMemo 必须返回值且返回值必须被使用
|
|
53
|
+
// ----------------------------------------------------------
|
|
54
|
+
// 官方推荐:无(recommended-latest 启用为 error)
|
|
55
|
+
// options: 无
|
|
56
|
+
// 社区最佳:新项目直接启用,避免退化为副作用函数
|
|
57
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/void-use-memo
|
|
58
|
+
"react-hooks/void-use-memo": "warn",
|
|
59
|
+
|
|
60
|
+
// React Compiler 场景下保留手工 memo(useMemo / useCallback / React.memo)
|
|
61
|
+
// ----------------------------------------------------------
|
|
62
|
+
// 官方推荐:recommended(默认 error)
|
|
63
|
+
// options: 无
|
|
64
|
+
// 社区最佳:接入 React Compiler 时必开
|
|
65
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/preserve-manual-memoization
|
|
66
|
+
"react-hooks/preserve-manual-memoization": "warn",
|
|
67
|
+
|
|
68
|
+
// useMemo / useCallback 的依赖项穷尽性 & 冗余校验
|
|
69
|
+
// ----------------------------------------------------------
|
|
70
|
+
// 官方推荐:无(未进入 recommended)
|
|
71
|
+
// options: 无
|
|
72
|
+
// 社区最佳:与 exhaustive-deps 覆盖面重叠,通常挑选一个即可
|
|
73
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/memo-dependencies
|
|
74
|
+
"react-hooks/memo-dependencies": "off",
|
|
75
|
+
|
|
76
|
+
// 阻止使用与 memoization 不兼容的第三方库 API
|
|
77
|
+
// ----------------------------------------------------------
|
|
78
|
+
// 官方推荐:recommended(默认 warn)
|
|
79
|
+
// options: 无
|
|
80
|
+
// 社区最佳:保持默认 warn,让开发者感知不兼容 API
|
|
81
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/incompatible-library
|
|
82
|
+
"react-hooks/incompatible-library": "warn",
|
|
83
|
+
|
|
84
|
+
// 禁止修改 props / state 等不可变值
|
|
85
|
+
// ----------------------------------------------------------
|
|
86
|
+
// 官方推荐:recommended(默认 error)
|
|
87
|
+
// options: 无
|
|
88
|
+
// 社区最佳:一律启用;React 官方明确 props/state 不可变
|
|
89
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/immutability
|
|
90
|
+
"react-hooks/immutability": "warn",
|
|
91
|
+
|
|
92
|
+
// 禁止在 render 期间写全局变量
|
|
93
|
+
// ----------------------------------------------------------
|
|
94
|
+
// 官方推荐:recommended(默认 error)
|
|
95
|
+
// options: 无
|
|
96
|
+
// 社区最佳:一律启用;render 必须无副作用
|
|
97
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/globals
|
|
98
|
+
"react-hooks/globals": "warn",
|
|
99
|
+
|
|
100
|
+
// 禁止在 render 期间读/写 ref
|
|
101
|
+
// ----------------------------------------------------------
|
|
102
|
+
// 官方推荐:recommended(默认 error)
|
|
103
|
+
// options: 无
|
|
104
|
+
// 社区最佳:一律启用,避免踩到 useRef 官方警告的 pitfall
|
|
105
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/refs
|
|
106
|
+
"react-hooks/refs": "warn",
|
|
107
|
+
|
|
108
|
+
// effect 依赖项必须是被 memoize 过的引用
|
|
109
|
+
// ----------------------------------------------------------
|
|
110
|
+
// 官方推荐:无
|
|
111
|
+
// options: 无
|
|
112
|
+
// 社区最佳:与 exhaustive-effect-dependencies 组合可换掉 exhaustive-deps;分歧较大,默认关
|
|
113
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/memoized-effect-dependencies
|
|
114
|
+
"react-hooks/memoized-effect-dependencies": "off",
|
|
115
|
+
|
|
116
|
+
// effect 依赖项穷尽 & 无冗余校验(新一代实现,替代 exhaustive-deps)
|
|
117
|
+
// ----------------------------------------------------------
|
|
118
|
+
// 官方推荐:无(尚在实验期)
|
|
119
|
+
// options: 无
|
|
120
|
+
// 社区最佳:与 exhaustive-deps 二选一即可,暂保持关闭
|
|
121
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/exhaustive-effect-dependencies
|
|
122
|
+
"react-hooks/exhaustive-effect-dependencies": "off",
|
|
123
|
+
|
|
124
|
+
// 禁止在 useEffect 中同步 setState
|
|
125
|
+
// ----------------------------------------------------------
|
|
126
|
+
// 官方推荐:recommended(默认 error)
|
|
127
|
+
// options: 无
|
|
128
|
+
// 社区最佳:启用;同步 setState 一般意味着可以下沉到 render 或事件
|
|
129
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/set-state-in-effect
|
|
130
|
+
"react-hooks/set-state-in-effect": "warn",
|
|
131
|
+
|
|
132
|
+
// 禁止在 effect 里从 state 派生 state
|
|
133
|
+
// ----------------------------------------------------------
|
|
134
|
+
// 官方推荐:无
|
|
135
|
+
// options: 无
|
|
136
|
+
// 社区最佳:默认关,推荐用 useMemo 或 render 内派生
|
|
137
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/no-deriving-state-in-effects
|
|
138
|
+
"react-hooks/no-deriving-state-in-effects": "off",
|
|
139
|
+
|
|
140
|
+
// 应使用 error boundary 而非 try/catch 捕获子组件错误
|
|
141
|
+
// ----------------------------------------------------------
|
|
142
|
+
// 官方推荐:recommended(默认 error)
|
|
143
|
+
// options: 无
|
|
144
|
+
// 社区最佳:启用;React 官方推荐 error boundary
|
|
145
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/error-boundaries
|
|
146
|
+
"react-hooks/error-boundaries": "warn",
|
|
147
|
+
|
|
148
|
+
// 校验组件 / Hook 的纯度,禁止调用已知有副作用的函数
|
|
149
|
+
// ----------------------------------------------------------
|
|
150
|
+
// 官方推荐:recommended(默认 error)
|
|
151
|
+
// options: 无
|
|
152
|
+
// 社区最佳:启用;React 官方 "Components and Hooks must be pure" 规范
|
|
153
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/purity
|
|
154
|
+
"react-hooks/purity": "warn",
|
|
155
|
+
|
|
156
|
+
// 禁止在 render 阶段同步 setState
|
|
157
|
+
// ----------------------------------------------------------
|
|
158
|
+
// 官方推荐:recommended(默认 error)
|
|
159
|
+
// options: 无
|
|
160
|
+
// 社区最佳:启用;避免额外的 render 与死循环风险
|
|
161
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/set-state-in-render
|
|
162
|
+
"react-hooks/set-state-in-render": "warn",
|
|
163
|
+
|
|
164
|
+
// 内部不变量校验(Compiler 内部规则)
|
|
165
|
+
// ----------------------------------------------------------
|
|
166
|
+
// 官方推荐:无
|
|
167
|
+
// options: 无
|
|
168
|
+
// 社区最佳:非用户业务规则,保持默认关闭
|
|
169
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/invariant
|
|
170
|
+
"react-hooks/invariant": "off",
|
|
171
|
+
|
|
172
|
+
// 未实现的功能占位规则
|
|
173
|
+
// ----------------------------------------------------------
|
|
174
|
+
// 官方推荐:无
|
|
175
|
+
// options: 无
|
|
176
|
+
// 社区最佳:占位规则,保持默认关闭
|
|
177
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/todo
|
|
178
|
+
"react-hooks/todo": "off",
|
|
179
|
+
|
|
180
|
+
// 校验源码是否包含 React Compiler 无法解析的语法
|
|
181
|
+
// ----------------------------------------------------------
|
|
182
|
+
// 官方推荐:无(Compiler 内部规则)
|
|
183
|
+
// options: 无
|
|
184
|
+
// 社区最佳:仅在启用 React Compiler 时开启
|
|
185
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/syntax
|
|
186
|
+
"react-hooks/syntax": "off",
|
|
187
|
+
|
|
188
|
+
// 校验源码是否使用 React Compiler 明确不支持的语法
|
|
189
|
+
// ----------------------------------------------------------
|
|
190
|
+
// 官方推荐:recommended(默认 warn)
|
|
191
|
+
// options: 无
|
|
192
|
+
// 社区最佳:启用 React Compiler 项目保持 warn
|
|
193
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/unsupported-syntax
|
|
194
|
+
"react-hooks/unsupported-syntax": "warn",
|
|
195
|
+
|
|
196
|
+
// 校验 React Compiler 配置项合法
|
|
197
|
+
// ----------------------------------------------------------
|
|
198
|
+
// 官方推荐:recommended(默认 error)
|
|
199
|
+
// options: 无
|
|
200
|
+
// 社区最佳:启用 React Compiler 时必开
|
|
201
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/config
|
|
202
|
+
"react-hooks/config": "warn",
|
|
203
|
+
|
|
204
|
+
// 校验 React Compiler 的 gating 配置
|
|
205
|
+
// ----------------------------------------------------------
|
|
206
|
+
// 官方推荐:recommended(默认 error)
|
|
207
|
+
// options: 无
|
|
208
|
+
// 社区最佳:启用 React Compiler + gating 时必开
|
|
209
|
+
// - 官方文档:https://react.dev/reference/react-compiler/gating
|
|
210
|
+
"react-hooks/gating": "warn",
|
|
211
|
+
|
|
212
|
+
// 禁止用 eslint-disable 抑制本插件其它规则
|
|
213
|
+
// ----------------------------------------------------------
|
|
214
|
+
// 官方推荐:无(未进入 recommended)
|
|
215
|
+
// options: 无
|
|
216
|
+
// 社区最佳:谨慎启用,业务里可能有临时豁免场景
|
|
217
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/rule-suppression
|
|
218
|
+
"react-hooks/rule-suppression": "off",
|
|
219
|
+
|
|
220
|
+
// 校验 fbt 国际化组件使用(Facebook 内部约定)
|
|
221
|
+
// ----------------------------------------------------------
|
|
222
|
+
// 官方推荐:无
|
|
223
|
+
// options: 无
|
|
224
|
+
// 社区最佳:仅 Facebook / Meta 内部生态启用,其它项目一律关闭
|
|
225
|
+
// - 官方文档:https://react.dev/reference/eslint-plugin-react-hooks/lints/fbt
|
|
226
|
+
"react-hooks/fbt": "off",
|
|
227
|
+
},
|
|
228
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================
|
|
3
|
+
// eslint-plugin-react-hooks - other
|
|
4
|
+
// ----------------------------------------------------------
|
|
5
|
+
// 未匹配到「deprecated / fixable / suggestion」维度的规则;
|
|
6
|
+
// 启用一律 "error"(不可自动修复,必须人工介入)。
|
|
7
|
+
// ==========================================================
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
rules: {
|
|
11
|
+
// Rules of Hooks(顶层 Hook / 仅在函数组件或自定义 Hook 内调用)
|
|
12
|
+
// ----------------------------------------------------------
|
|
13
|
+
// 官方推荐:recommended(默认 error)
|
|
14
|
+
// options: 无
|
|
15
|
+
// 社区最佳:一律 error;React 官方 "Rules of Hooks" 硬性约束,一旦违反会引发 bug
|
|
16
|
+
// - 官方文档:https://react.dev/reference/rules/rules-of-hooks
|
|
17
|
+
"react-hooks/rules-of-hooks": "error",
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -2,7 +2,15 @@
|
|
|
2
2
|
// 统计数据
|
|
3
3
|
// 使用方式:node summary.js
|
|
4
4
|
|
|
5
|
-
const allRules =
|
|
5
|
+
const allRules = {
|
|
6
|
+
...require("./rules/react/_deprecated.js").rules,
|
|
7
|
+
...require("./rules/react/class-component.js").rules,
|
|
8
|
+
...require("./rules/react/dom.js").rules,
|
|
9
|
+
...require("./rules/react/fixable.js").rules,
|
|
10
|
+
...require("./rules/react/jsx.js").rules,
|
|
11
|
+
...require("./rules/react/prop-types.js").rules,
|
|
12
|
+
...require("./rules/react/suggestion.js").rules,
|
|
13
|
+
};
|
|
6
14
|
|
|
7
15
|
// 统计函数
|
|
8
16
|
function countRulesByLevel(rulesObj) {
|
|
@@ -53,6 +53,20 @@ const EslintRules = {
|
|
|
53
53
|
...require("../rules/vue/vue3-strongly-recommended.js").rules,
|
|
54
54
|
...require("../rules/vue/vue3-uncategorized.js").rules,
|
|
55
55
|
},
|
|
56
|
+
react: {
|
|
57
|
+
// ...require("../rules/react/_deprecated.js").rules,
|
|
58
|
+
...require("../rules/react/fixable.js").rules,
|
|
59
|
+
...require("../rules/react/suggestion.js").rules,
|
|
60
|
+
...require("../rules/react/jsx.js").rules,
|
|
61
|
+
...require("../rules/react/class-component.js").rules,
|
|
62
|
+
...require("../rules/react/prop-types.js").rules,
|
|
63
|
+
...require("../rules/react/dom.js").rules,
|
|
64
|
+
},
|
|
65
|
+
reactHooks: {
|
|
66
|
+
// ...require("../rules/react-hooks/_deprecated.js").rules,
|
|
67
|
+
...require("../rules/react-hooks/fixable.js").rules,
|
|
68
|
+
...require("../rules/react-hooks/other.js").rules,
|
|
69
|
+
},
|
|
56
70
|
};
|
|
57
71
|
|
|
58
72
|
|
|
@@ -73,6 +87,10 @@ function getRules(rules) {
|
|
|
73
87
|
...EslintRules.vue,
|
|
74
88
|
...EslintRules.vue2,
|
|
75
89
|
...EslintRules.vue3,
|
|
90
|
+
// react 规则
|
|
91
|
+
...EslintRules.react,
|
|
92
|
+
// react-hooks 规则
|
|
93
|
+
...EslintRules.reactHooks,
|
|
76
94
|
};
|
|
77
95
|
const ValidRules = {};
|
|
78
96
|
const InvalidRules = {};
|
|
@@ -138,6 +156,16 @@ function getWarnRules(type) {
|
|
|
138
156
|
...EslintRules.vue3,
|
|
139
157
|
};
|
|
140
158
|
break;
|
|
159
|
+
case "react":
|
|
160
|
+
rules = {
|
|
161
|
+
...EslintRules.react,
|
|
162
|
+
};
|
|
163
|
+
break;
|
|
164
|
+
case "react-hooks":
|
|
165
|
+
rules = {
|
|
166
|
+
...EslintRules.reactHooks,
|
|
167
|
+
};
|
|
168
|
+
break;
|
|
141
169
|
}
|
|
142
170
|
|
|
143
171
|
// 筛选规则
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@houheaven/eslint-config-helper",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.4",
|
|
4
4
|
"description": "Eslint 规则助手",
|
|
5
5
|
"author": "立树",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,12 +40,12 @@
|
|
|
40
40
|
},
|
|
41
41
|
"repository": {
|
|
42
42
|
"type": "git",
|
|
43
|
-
"url": "https://
|
|
43
|
+
"url": "https://github.com/houheaven/eslint-config-helper.git"
|
|
44
44
|
},
|
|
45
45
|
"bugs": {
|
|
46
|
-
"url": "https://
|
|
46
|
+
"url": "https://github.com/houheaven/eslint-config-helper/issues"
|
|
47
47
|
},
|
|
48
|
-
"homepage": "https://
|
|
48
|
+
"homepage": "https://github.com/houheaven/eslint-config-helper",
|
|
49
49
|
"scripts": {
|
|
50
50
|
"lint": "eslint ."
|
|
51
51
|
},
|