@fast-china/eslint-config 2.1.7 → 2.1.8
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/CHANGELOG.md +25 -0
- package/CONTRIBUTING.md +2 -2
- package/README.md +66 -142
- package/README.zh.md +64 -142
- package/dist/configs/environment.mjs +2 -0
- package/dist/configs/environment.mjs.map +1 -1
- package/dist/configs/ignores.mjs +0 -1
- package/dist/configs/ignores.mjs.map +1 -1
- package/dist/configs/import.mjs +4 -1
- package/dist/configs/import.mjs.map +1 -1
- package/dist/configs/index.d.mts +2 -2
- package/dist/configs/index.mjs +2 -2
- package/dist/configs/javascript.mjs +3 -1
- package/dist/configs/javascript.mjs.map +1 -1
- package/dist/configs/json.mjs +3 -1
- package/dist/configs/json.mjs.map +1 -1
- package/dist/configs/markdown.d.mts +2 -1
- package/dist/configs/markdown.mjs +4 -2
- package/dist/configs/markdown.mjs.map +1 -1
- package/dist/configs/prettier.mjs +1 -1
- package/dist/configs/prettier.mjs.map +1 -1
- package/dist/configs/sort-package.d.mts +1 -1
- package/dist/configs/sort-package.mjs +1 -1
- package/dist/configs/sort-package.mjs.map +1 -1
- package/dist/configs/sort-tsconfig.d.mts +2 -2
- package/dist/configs/sort-tsconfig.mjs +2 -2
- package/dist/configs/sort-tsconfig.mjs.map +1 -1
- package/dist/configs/typescript.d.mts +11 -7
- package/dist/configs/typescript.mjs +25 -22
- package/dist/configs/typescript.mjs.map +1 -1
- package/dist/configs/uniapp.mjs +19 -13
- package/dist/configs/uniapp.mjs.map +1 -1
- package/dist/configs/vue.d.mts +18 -3
- package/dist/configs/vue.mjs +59 -9
- package/dist/configs/vue.mjs.map +1 -1
- package/dist/constants/index.d.mts +4 -0
- package/dist/constants/index.mjs +5 -1
- package/dist/constants/index.mjs.map +1 -1
- package/dist/index.d.mts +37 -58
- package/dist/index.mjs +43 -47
- package/dist/index.mjs.map +1 -1
- package/dist/rules/angular.d.mts +28 -0
- package/dist/rules/angular.mjs +28 -0
- package/dist/rules/angular.mjs.map +1 -1
- package/dist/rules/common.d.mts +16 -6
- package/dist/rules/common.mjs +16 -13
- package/dist/rules/common.mjs.map +1 -1
- package/dist/rules/import.d.mts +9 -0
- package/dist/rules/import.mjs +24 -0
- package/dist/rules/import.mjs.map +1 -1
- package/dist/rules/index.d.mts +2 -2
- package/dist/rules/index.mjs +2 -2
- package/dist/rules/javascript.d.mts +18 -0
- package/dist/rules/javascript.mjs +18 -0
- package/dist/rules/javascript.mjs.map +1 -1
- package/dist/rules/lodash.d.mts +2 -0
- package/dist/rules/lodash.mjs +6 -2
- package/dist/rules/lodash.mjs.map +1 -1
- package/dist/rules/react.d.mts +13 -0
- package/dist/rules/react.mjs +13 -0
- package/dist/rules/react.mjs.map +1 -1
- package/dist/rules/regexp.d.mts +25 -0
- package/dist/rules/regexp.mjs +25 -0
- package/dist/rules/regexp.mjs.map +1 -1
- package/dist/rules/sort-package.d.mts +3 -1
- package/dist/rules/sort-package.mjs +3 -1
- package/dist/rules/sort-package.mjs.map +1 -1
- package/dist/rules/sort-tsconfig.d.mts +3 -1
- package/dist/rules/sort-tsconfig.mjs +3 -1
- package/dist/rules/sort-tsconfig.mjs.map +1 -1
- package/dist/rules/typescript.d.mts +81 -13
- package/dist/rules/typescript.mjs +83 -15
- package/dist/rules/typescript.mjs.map +1 -1
- package/dist/rules/vue.d.mts +65 -10
- package/dist/rules/vue.mjs +41 -14
- package/dist/rules/vue.mjs.map +1 -1
- package/docs/engineering-audit.zh.md +45 -49
- package/docs/rules/angular.zh.md +721 -0
- package/docs/rules/core.zh.md +3031 -0
- package/docs/rules/index.zh.md +38 -0
- package/docs/rules/json.zh.md +784 -0
- package/docs/rules/markdown.zh.md +449 -0
- package/docs/rules/react.zh.md +2091 -0
- package/docs/rules/typescript.zh.md +2051 -0
- package/docs/rules/vue.zh.md +3016 -0
- package/docs/rules-risk.md +85 -91
- package/docs/rules-risk.zh.md +76 -82
- package/package.json +4 -2
|
@@ -0,0 +1,2091 @@
|
|
|
1
|
+
<!-- 此文件由 scripts/rules-docs.ts 生成,请勿手工编辑。 -->
|
|
2
|
+
|
|
3
|
+
# React 与 React Hooks
|
|
4
|
+
|
|
5
|
+
本页记录当前依赖版本和仓库配置最终产生的 86 条规则。每条规则均列出实际严重级别、生效范围、上游说明、常见报告以及错误/正确示例。
|
|
6
|
+
|
|
7
|
+
## 仓库显式规则(13 条)
|
|
8
|
+
|
|
9
|
+
### `@eslint-react/dom-no-missing-button-type`
|
|
10
|
+
|
|
11
|
+
`button` 缺少 `type` 时在表单内默认为 `submit`,显式声明可避免意外提交。
|
|
12
|
+
|
|
13
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
14
|
+
- 自动修复:不支持或上游未声明
|
|
15
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-missing-button-type)
|
|
16
|
+
- 常见报告:`Add type attribute with value '{{ type }}'.`;`Missing an explicit type attribute for button.`
|
|
17
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
18
|
+
|
|
19
|
+
错误示例:
|
|
20
|
+
|
|
21
|
+
<!-- prettier-ignore -->
|
|
22
|
+
````tsx
|
|
23
|
+
<button onClick={save}>Save</button>
|
|
24
|
+
````
|
|
25
|
+
|
|
26
|
+
正确示例:
|
|
27
|
+
|
|
28
|
+
<!-- prettier-ignore -->
|
|
29
|
+
````tsx
|
|
30
|
+
<button type="button" onClick={save}>Save</button>
|
|
31
|
+
````
|
|
32
|
+
|
|
33
|
+
### `@eslint-react/dom-no-missing-iframe-sandbox`
|
|
34
|
+
|
|
35
|
+
未受限 `iframe` 权限面较大;以警告提醒评估可信来源和 `sandbox` 策略。
|
|
36
|
+
|
|
37
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
38
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
39
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-missing-iframe-sandbox)
|
|
40
|
+
- 常见报告:`Add sandbox attribute with value '{{ value }}'.`;`Missing an explicit sandbox attribute for iframe.`
|
|
41
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
42
|
+
|
|
43
|
+
错误示例:
|
|
44
|
+
|
|
45
|
+
<!-- prettier-ignore -->
|
|
46
|
+
````tsx
|
|
47
|
+
<iframe src="https://example.com" />
|
|
48
|
+
````
|
|
49
|
+
|
|
50
|
+
正确示例:
|
|
51
|
+
|
|
52
|
+
<!-- prettier-ignore -->
|
|
53
|
+
````tsx
|
|
54
|
+
<iframe src="https://example.com" sandbox="allow-scripts" />
|
|
55
|
+
````
|
|
56
|
+
|
|
57
|
+
### `@eslint-react/dom-no-unknown-property`
|
|
58
|
+
|
|
59
|
+
JSX 属性拼写错误会被 React 忽略或错误透传到 DOM,应在提交前阻断。
|
|
60
|
+
|
|
61
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
62
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
63
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-unknown-property)
|
|
64
|
+
- 常见报告:`React does not recognize data-* props with uppercase characters on a DOM element. Found '{{name}}', use '{{lowerCaseName}}' instead`;`Invalid property '{{name}}' found on tag '{{tagName}}', but it is only allowed on: {{allowedTags}}`;`Unknown property '{{name}}' found`
|
|
65
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
66
|
+
|
|
67
|
+
错误示例:
|
|
68
|
+
|
|
69
|
+
<!-- prettier-ignore -->
|
|
70
|
+
````tsx
|
|
71
|
+
<label class="field" for="name">Name</label>
|
|
72
|
+
````
|
|
73
|
+
|
|
74
|
+
正确示例:
|
|
75
|
+
|
|
76
|
+
<!-- prettier-ignore -->
|
|
77
|
+
````tsx
|
|
78
|
+
<label className="field" htmlFor="name">Name</label>
|
|
79
|
+
````
|
|
80
|
+
|
|
81
|
+
### `@eslint-react/dom-no-unsafe-target-blank`
|
|
82
|
+
|
|
83
|
+
`target="_blank"` 未隔离 `opener` 时可能允许目标页控制来源页。
|
|
84
|
+
|
|
85
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
86
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
87
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-unsafe-target-blank)
|
|
88
|
+
- 常见报告:`Add 'rel="noreferrer noopener"' to the link to prevent security risks.`;`Using 'target="_blank"' on an external link without 'rel="noreferrer noopener"' is a security risk.`
|
|
89
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
90
|
+
|
|
91
|
+
错误示例:
|
|
92
|
+
|
|
93
|
+
<!-- prettier-ignore -->
|
|
94
|
+
````tsx
|
|
95
|
+
<a href="https://example.com" target="_blank">Open</a>
|
|
96
|
+
````
|
|
97
|
+
|
|
98
|
+
正确示例:
|
|
99
|
+
|
|
100
|
+
<!-- prettier-ignore -->
|
|
101
|
+
````tsx
|
|
102
|
+
<a href="https://example.com" target="_blank" rel="noreferrer">Open</a>
|
|
103
|
+
````
|
|
104
|
+
|
|
105
|
+
### `@eslint-react/error-boundaries`
|
|
106
|
+
|
|
107
|
+
子组件渲染错误应交给 Error Boundary,而不是用父组件 `try/catch` 捕获;由官方 `react-hooks/error-boundaries` 统一检查。
|
|
108
|
+
|
|
109
|
+
- 生效级别与范围:默认关闭(本地显式覆写)
|
|
110
|
+
- 关闭范围:React JSX、React TSX
|
|
111
|
+
- 自动修复:不支持或上游未声明
|
|
112
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/error-boundaries)
|
|
113
|
+
- 常见报告:`Use an Error Boundary to catch errors in child components. Try/catch can't catch errors during React's rendering process.`;`Use an Error Boundary instead of try/catch around the 'use' hook. The 'use' hook suspends the component, and its errors can only be caught by Error Boundaries.`
|
|
114
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
115
|
+
|
|
116
|
+
错误示例:
|
|
117
|
+
|
|
118
|
+
<!-- prettier-ignore -->
|
|
119
|
+
````tsx
|
|
120
|
+
function Page() { try { return <Profile />; } catch { return <Fallback />; } }
|
|
121
|
+
````
|
|
122
|
+
|
|
123
|
+
正确示例:
|
|
124
|
+
|
|
125
|
+
<!-- prettier-ignore -->
|
|
126
|
+
````tsx
|
|
127
|
+
function Page() { return <ErrorBoundary fallback={<Fallback />}><Profile /></ErrorBoundary>; }
|
|
128
|
+
````
|
|
129
|
+
|
|
130
|
+
### `@eslint-react/exhaustive-deps`
|
|
131
|
+
|
|
132
|
+
`useEffect`、`useMemo` 和 `useCallback` 等 Hook 的依赖项必须完整准确;由官方 `react-hooks/exhaustive-deps` 统一检查。
|
|
133
|
+
|
|
134
|
+
- 生效级别与范围:默认关闭(本地显式覆写)
|
|
135
|
+
- 关闭范围:React JSX、React TSX
|
|
136
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
137
|
+
- 规则来源:[官方文档](https://github.com/facebook/react/issues/14920)
|
|
138
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
139
|
+
|
|
140
|
+
错误示例:
|
|
141
|
+
|
|
142
|
+
<!-- prettier-ignore -->
|
|
143
|
+
````tsx
|
|
144
|
+
function UserCard({ id }: Props) { useEffect(() => load(id), []); return <div />; }
|
|
145
|
+
````
|
|
146
|
+
|
|
147
|
+
正确示例:
|
|
148
|
+
|
|
149
|
+
<!-- prettier-ignore -->
|
|
150
|
+
````tsx
|
|
151
|
+
function UserCard({ id }: Props) { useEffect(() => load(id), [id]); return <div />; }
|
|
152
|
+
````
|
|
153
|
+
|
|
154
|
+
### `@eslint-react/purity`
|
|
155
|
+
|
|
156
|
+
组件和 Hook 渲染期间不得调用 `Date.now`、`Math.random` 等已知非纯函数;由官方 `react-hooks/purity` 统一检查。
|
|
157
|
+
|
|
158
|
+
- 生效级别与范围:默认关闭(本地显式覆写)
|
|
159
|
+
- 关闭范围:React JSX、React TSX
|
|
160
|
+
- 自动修复:不支持或上游未声明
|
|
161
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/purity)
|
|
162
|
+
- 常见报告:`Do not call '{{name}}' during render. Components and hooks must be pure. Move this call into an event handler, effect, or state initializer.`
|
|
163
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
164
|
+
|
|
165
|
+
错误示例:
|
|
166
|
+
|
|
167
|
+
<!-- prettier-ignore -->
|
|
168
|
+
````tsx
|
|
169
|
+
function Token() { const value = Math.random(); return <span>{value}</span>; }
|
|
170
|
+
````
|
|
171
|
+
|
|
172
|
+
正确示例:
|
|
173
|
+
|
|
174
|
+
<!-- prettier-ignore -->
|
|
175
|
+
````tsx
|
|
176
|
+
function Token() { const [value] = useState(() => Math.random()); return <span>{value}</span>; }
|
|
177
|
+
````
|
|
178
|
+
|
|
179
|
+
### `@eslint-react/rules-of-hooks`
|
|
180
|
+
|
|
181
|
+
Hook 只能在组件或自定义 Hook 顶层调用,不能放入条件、循环或普通函数;由官方 `react-hooks/rules-of-hooks` 统一检查。
|
|
182
|
+
|
|
183
|
+
- 生效级别与范围:默认关闭(本地显式覆写)
|
|
184
|
+
- 关闭范围:React JSX、React TSX
|
|
185
|
+
- 自动修复:不支持或上游未声明
|
|
186
|
+
- 规则来源:[官方文档](https://react.dev/reference/rules/rules-of-hooks)
|
|
187
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
188
|
+
|
|
189
|
+
错误示例:
|
|
190
|
+
|
|
191
|
+
<!-- prettier-ignore -->
|
|
192
|
+
````tsx
|
|
193
|
+
function UserCard({ ready }: Props) { if (ready) { useEffect(load, []); } return <div />; }
|
|
194
|
+
````
|
|
195
|
+
|
|
196
|
+
正确示例:
|
|
197
|
+
|
|
198
|
+
<!-- prettier-ignore -->
|
|
199
|
+
````tsx
|
|
200
|
+
function UserCard({ ready }: Props) { useEffect(() => { if (ready) load(); }, [ready]); return <div />; }
|
|
201
|
+
````
|
|
202
|
+
|
|
203
|
+
### `@eslint-react/set-state-in-effect`
|
|
204
|
+
|
|
205
|
+
Effect 内同步更新状态会触发额外渲染,应改为派生值或外部订阅回调;由官方 `react-hooks/set-state-in-effect` 统一检查。
|
|
206
|
+
|
|
207
|
+
- 生效级别与范围:默认关闭(本地显式覆写)
|
|
208
|
+
- 关闭范围:React JSX、React TSX
|
|
209
|
+
- 自动修复:不支持或上游未声明
|
|
210
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/set-state-in-effect)
|
|
211
|
+
- 常见报告:`Do not call the 'set' function '{{name}}' of 'useState' synchronously in an effect. This can lead to unnecessary re-renders and performance issues.`
|
|
212
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
213
|
+
|
|
214
|
+
错误示例:
|
|
215
|
+
|
|
216
|
+
<!-- prettier-ignore -->
|
|
217
|
+
````tsx
|
|
218
|
+
function User({ name }: Props) { const [label, setLabel] = useState(''); useEffect(() => { setLabel(name.trim()); }, [name]); return <p>{label}</p>; }
|
|
219
|
+
````
|
|
220
|
+
|
|
221
|
+
正确示例:
|
|
222
|
+
|
|
223
|
+
<!-- prettier-ignore -->
|
|
224
|
+
````tsx
|
|
225
|
+
function User({ name }: Props) { const label = name.trim(); return <p>{label}</p>; }
|
|
226
|
+
````
|
|
227
|
+
|
|
228
|
+
### `@eslint-react/set-state-in-render`
|
|
229
|
+
|
|
230
|
+
渲染期间无条件更新状态可能造成重复或无限渲染;由官方 `react-hooks/set-state-in-render` 统一检查。
|
|
231
|
+
|
|
232
|
+
- 生效级别与范围:默认关闭(本地显式覆写)
|
|
233
|
+
- 关闭范围:React JSX、React TSX
|
|
234
|
+
- 自动修复:不支持或上游未声明
|
|
235
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/set-state-in-render)
|
|
236
|
+
- 常见报告:`Do not call the 'set' function '{{name}}' unconditionally during render. This will trigger an infinite render loop.`
|
|
237
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
238
|
+
|
|
239
|
+
错误示例:
|
|
240
|
+
|
|
241
|
+
<!-- prettier-ignore -->
|
|
242
|
+
````tsx
|
|
243
|
+
function Counter() { const [count, setCount] = useState(0); setCount(count + 1); return <span>{count}</span>; }
|
|
244
|
+
````
|
|
245
|
+
|
|
246
|
+
正确示例:
|
|
247
|
+
|
|
248
|
+
<!-- prettier-ignore -->
|
|
249
|
+
````tsx
|
|
250
|
+
function Counter() { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>{count}</button>; }
|
|
251
|
+
````
|
|
252
|
+
|
|
253
|
+
### `@eslint-react/static-components`
|
|
254
|
+
|
|
255
|
+
在渲染函数内部创建组件会使其每次重建并丢失状态;由官方 `react-hooks/static-components` 统一检查。
|
|
256
|
+
|
|
257
|
+
- 生效级别与范围:默认关闭(本地显式覆写)
|
|
258
|
+
- 关闭范围:React JSX、React TSX
|
|
259
|
+
- 自动修复:不支持或上游未声明
|
|
260
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/static-components)
|
|
261
|
+
- 常见报告:`Cannot create components during render. Components created during render will reset their state each time they are created. Declare components outside of render.`;`The component is created during render here.`
|
|
262
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
263
|
+
|
|
264
|
+
错误示例:
|
|
265
|
+
|
|
266
|
+
<!-- prettier-ignore -->
|
|
267
|
+
````tsx
|
|
268
|
+
function Page() { function Header() { return <h1>Title</h1>; } return <Header />; }
|
|
269
|
+
````
|
|
270
|
+
|
|
271
|
+
正确示例:
|
|
272
|
+
|
|
273
|
+
<!-- prettier-ignore -->
|
|
274
|
+
````tsx
|
|
275
|
+
function Header() { return <h1>Title</h1>; }
|
|
276
|
+
function Page() { return <Header />; }
|
|
277
|
+
````
|
|
278
|
+
|
|
279
|
+
### `@eslint-react/unsupported-syntax`
|
|
280
|
+
|
|
281
|
+
React Compiler 无法安全转换的语法需要调整或显式隔离;由官方 `react-hooks/unsupported-syntax` 统一检查。
|
|
282
|
+
|
|
283
|
+
- 生效级别与范围:默认关闭(本地显式覆写)
|
|
284
|
+
- 关闭范围:React JSX、React TSX
|
|
285
|
+
- 自动修复:不支持或上游未声明
|
|
286
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/unsupported-syntax)
|
|
287
|
+
- 常见报告:`Do not use 'eval' inside components or hooks. 'eval' cannot be statically analyzed and is not supported by React Compiler.`;`Do not use 'with' statements inside components or hooks. 'with' changes scope dynamically and is not supported by React Compiler.`
|
|
288
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
289
|
+
|
|
290
|
+
错误示例:
|
|
291
|
+
|
|
292
|
+
<!-- prettier-ignore -->
|
|
293
|
+
````tsx
|
|
294
|
+
function Page({ source }: Props) { return <div>{eval(source)}</div>; }
|
|
295
|
+
````
|
|
296
|
+
|
|
297
|
+
正确示例:
|
|
298
|
+
|
|
299
|
+
<!-- prettier-ignore -->
|
|
300
|
+
````tsx
|
|
301
|
+
function Page({ source }: Props) { return <div>{JSON.parse(source)}</div>; }
|
|
302
|
+
````
|
|
303
|
+
|
|
304
|
+
### `@eslint-react/use-memo`
|
|
305
|
+
|
|
306
|
+
`useMemo` 回调必须返回需要缓存的值,不能把它误当作副作用 Hook;由官方 `react-hooks/use-memo` 统一检查。
|
|
307
|
+
|
|
308
|
+
- 生效级别与范围:默认关闭(本地显式覆写)
|
|
309
|
+
- 关闭范围:React JSX、React TSX
|
|
310
|
+
- 自动修复:不支持或上游未声明
|
|
311
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/use-memo)
|
|
312
|
+
- 常见报告:`useMemo() callbacks must return a value.
|
|
313
|
+
|
|
314
|
+
This useMemo() callback doesn't return a value. useMemo() is for computing and caching values, not for arbitrary side effects.`;`useMemo() callbacks may not be async or generator functions.
|
|
315
|
+
|
|
316
|
+
useMemo() callbacks are called once and must synchronously return a value.`;`useMemo() callbacks may not accept parameters.
|
|
317
|
+
|
|
318
|
+
useMemo() callbacks are called by React to cache calculations across re-renders. They should not take parameters. Instead, directly reference the props, state, or local variables needed for the computation.`
|
|
319
|
+
|
|
320
|
+
- 示例类型:仓库显式规则的直接代码示例
|
|
321
|
+
|
|
322
|
+
错误示例:
|
|
323
|
+
|
|
324
|
+
<!-- prettier-ignore -->
|
|
325
|
+
````tsx
|
|
326
|
+
function User({ name }: Props) { useMemo(() => name.trim(), [name]); return <p>{name}</p>; }
|
|
327
|
+
````
|
|
328
|
+
|
|
329
|
+
正确示例:
|
|
330
|
+
|
|
331
|
+
<!-- prettier-ignore -->
|
|
332
|
+
````tsx
|
|
333
|
+
function User({ name }: Props) { const label = useMemo(() => name.trim(), [name]); return <p>{label}</p>; }
|
|
334
|
+
````
|
|
335
|
+
|
|
336
|
+
## 第三方预置规则(73 条)
|
|
337
|
+
|
|
338
|
+
### `@eslint-react/dom-no-dangerously-set-innerhtml`
|
|
339
|
+
|
|
340
|
+
检查该规则对应的代码约束。上游说明:Disallows DOM elements from using 'dangerouslySetInnerHTML'.
|
|
341
|
+
|
|
342
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
343
|
+
- 自动修复:不支持或上游未声明
|
|
344
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-dangerously-set-innerhtml)
|
|
345
|
+
- 常见报告:`Using 'dangerouslySetInnerHTML' may have security implications.`
|
|
346
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
347
|
+
|
|
348
|
+
错误示例:
|
|
349
|
+
|
|
350
|
+
<!-- prettier-ignore -->
|
|
351
|
+
````tsx
|
|
352
|
+
<div dangerouslySetInnerHTML={{ __html: html }} />
|
|
353
|
+
````
|
|
354
|
+
|
|
355
|
+
正确示例:
|
|
356
|
+
|
|
357
|
+
<!-- prettier-ignore -->
|
|
358
|
+
````tsx
|
|
359
|
+
<div>{plainText}</div>
|
|
360
|
+
````
|
|
361
|
+
|
|
362
|
+
### `@eslint-react/dom-no-dangerously-set-innerhtml-with-children`
|
|
363
|
+
|
|
364
|
+
检查该规则对应的代码约束。上游说明:Disallows DOM elements from using 'dangerouslySetInnerHTML' and 'children' at the same time.
|
|
365
|
+
|
|
366
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
367
|
+
- 自动修复:不支持或上游未声明
|
|
368
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-dangerously-set-innerhtml-with-children)
|
|
369
|
+
- 常见报告:`A DOM component cannot use both children and 'dangerouslySetInnerHTML'.`
|
|
370
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
371
|
+
|
|
372
|
+
错误示例:
|
|
373
|
+
|
|
374
|
+
<!-- prettier-ignore -->
|
|
375
|
+
````tsx
|
|
376
|
+
<div dangerouslySetInnerHTML={{ __html: html }}>Fallback</div>
|
|
377
|
+
````
|
|
378
|
+
|
|
379
|
+
正确示例:
|
|
380
|
+
|
|
381
|
+
<!-- prettier-ignore -->
|
|
382
|
+
````tsx
|
|
383
|
+
<div dangerouslySetInnerHTML={{ __html: html }} />
|
|
384
|
+
````
|
|
385
|
+
|
|
386
|
+
### `@eslint-react/dom-no-find-dom-node`
|
|
387
|
+
|
|
388
|
+
检查该规则对应的代码约束。上游说明:Disallows 'findDOMNode'.
|
|
389
|
+
|
|
390
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
391
|
+
- 自动修复:不支持或上游未声明
|
|
392
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-find-dom-node)
|
|
393
|
+
- 常见报告:`[Deprecated] Use alternatives instead.`
|
|
394
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
395
|
+
|
|
396
|
+
错误示例:
|
|
397
|
+
|
|
398
|
+
<!-- prettier-ignore -->
|
|
399
|
+
````tsx
|
|
400
|
+
const node = findDOMNode(component);
|
|
401
|
+
````
|
|
402
|
+
|
|
403
|
+
正确示例:
|
|
404
|
+
|
|
405
|
+
<!-- prettier-ignore -->
|
|
406
|
+
````tsx
|
|
407
|
+
const nodeRef = useRef<HTMLDivElement>(null);
|
|
408
|
+
````
|
|
409
|
+
|
|
410
|
+
### `@eslint-react/dom-no-flush-sync`
|
|
411
|
+
|
|
412
|
+
检查该规则对应的代码约束。上游说明:Disallows 'flushSync'.
|
|
413
|
+
|
|
414
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
415
|
+
- 自动修复:不支持或上游未声明
|
|
416
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-flush-sync)
|
|
417
|
+
- 常见报告:`Using 'flushSync' is uncommon and can hurt the performance of your app.`
|
|
418
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
419
|
+
|
|
420
|
+
错误示例:
|
|
421
|
+
|
|
422
|
+
<!-- prettier-ignore -->
|
|
423
|
+
````tsx
|
|
424
|
+
flushSync(() => setReady(true));
|
|
425
|
+
````
|
|
426
|
+
|
|
427
|
+
正确示例:
|
|
428
|
+
|
|
429
|
+
<!-- prettier-ignore -->
|
|
430
|
+
````tsx
|
|
431
|
+
setReady(true);
|
|
432
|
+
````
|
|
433
|
+
|
|
434
|
+
### `@eslint-react/dom-no-hydrate`
|
|
435
|
+
|
|
436
|
+
检查该规则对应的代码约束。上游说明:Replaces usage of 'ReactDOM.hydrate()' with 'hydrateRoot()'.
|
|
437
|
+
|
|
438
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
439
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
440
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-hydrate)
|
|
441
|
+
- 常见报告:`[Deprecated] Use 'hydrateRoot()' instead.`
|
|
442
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
443
|
+
|
|
444
|
+
错误示例:
|
|
445
|
+
|
|
446
|
+
<!-- prettier-ignore -->
|
|
447
|
+
````tsx
|
|
448
|
+
import ReactDOM from "react-dom";
|
|
449
|
+
ReactDOM.hydrate(<App />, root);
|
|
450
|
+
````
|
|
451
|
+
|
|
452
|
+
正确示例:
|
|
453
|
+
|
|
454
|
+
<!-- prettier-ignore -->
|
|
455
|
+
````tsx
|
|
456
|
+
import { hydrateRoot } from "react-dom/client";
|
|
457
|
+
hydrateRoot(root, <App />);
|
|
458
|
+
````
|
|
459
|
+
|
|
460
|
+
### `@eslint-react/dom-no-render`
|
|
461
|
+
|
|
462
|
+
检查该规则对应的代码约束。上游说明:Replaces usage of 'ReactDOM.render()' with 'createRoot(node).render()'.
|
|
463
|
+
|
|
464
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
465
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
466
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-render)
|
|
467
|
+
- 常见报告:`[Deprecated] Use 'createRoot(node).render()' instead.`
|
|
468
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
469
|
+
|
|
470
|
+
错误示例:
|
|
471
|
+
|
|
472
|
+
<!-- prettier-ignore -->
|
|
473
|
+
````tsx
|
|
474
|
+
ReactDOM.render(<App />, root);
|
|
475
|
+
````
|
|
476
|
+
|
|
477
|
+
正确示例:
|
|
478
|
+
|
|
479
|
+
<!-- prettier-ignore -->
|
|
480
|
+
````tsx
|
|
481
|
+
createRoot(root).render(<App />);
|
|
482
|
+
````
|
|
483
|
+
|
|
484
|
+
### `@eslint-react/dom-no-render-return-value`
|
|
485
|
+
|
|
486
|
+
检查该规则对应的代码约束。上游说明:Disallows the return value of 'ReactDOM.render'.
|
|
487
|
+
|
|
488
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
489
|
+
- 自动修复:不支持或上游未声明
|
|
490
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-render-return-value)
|
|
491
|
+
- 常见报告:`Do not depend on the return value from 'ReactDOM.render'.`
|
|
492
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
493
|
+
|
|
494
|
+
错误示例:
|
|
495
|
+
|
|
496
|
+
<!-- prettier-ignore -->
|
|
497
|
+
````tsx
|
|
498
|
+
const instance = ReactDOM.render(<App />, root);
|
|
499
|
+
````
|
|
500
|
+
|
|
501
|
+
正确示例:
|
|
502
|
+
|
|
503
|
+
<!-- prettier-ignore -->
|
|
504
|
+
````tsx
|
|
505
|
+
createRoot(root).render(<App />);
|
|
506
|
+
````
|
|
507
|
+
|
|
508
|
+
### `@eslint-react/dom-no-script-url`
|
|
509
|
+
|
|
510
|
+
检查该规则对应的代码约束。上游说明:Disallows 'javascript:' URLs as attribute values.
|
|
511
|
+
|
|
512
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
513
|
+
- 自动修复:不支持或上游未声明
|
|
514
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-script-url)
|
|
515
|
+
- 常见报告:`Using a 'javascript:' URL is a security risk and should be avoided.`
|
|
516
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
517
|
+
|
|
518
|
+
错误示例:
|
|
519
|
+
|
|
520
|
+
<!-- prettier-ignore -->
|
|
521
|
+
````tsx
|
|
522
|
+
<a href="javascript:alert(1)">Open</a>
|
|
523
|
+
````
|
|
524
|
+
|
|
525
|
+
正确示例:
|
|
526
|
+
|
|
527
|
+
<!-- prettier-ignore -->
|
|
528
|
+
````tsx
|
|
529
|
+
<button type="button" onClick={open}>Open</button>
|
|
530
|
+
````
|
|
531
|
+
|
|
532
|
+
### `@eslint-react/dom-no-unsafe-iframe-sandbox`
|
|
533
|
+
|
|
534
|
+
检查该规则对应的代码约束。上游说明:Enforces that the 'sandbox' attribute for 'iframe' elements is not set to unsafe combinations.
|
|
535
|
+
|
|
536
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
537
|
+
- 自动修复:不支持或上游未声明
|
|
538
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-unsafe-iframe-sandbox)
|
|
539
|
+
- 常见报告:`Unsafe 'sandbox' attribute value on 'iframe' component.`
|
|
540
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
541
|
+
|
|
542
|
+
错误示例:
|
|
543
|
+
|
|
544
|
+
<!-- prettier-ignore -->
|
|
545
|
+
````tsx
|
|
546
|
+
<iframe sandbox="allow-scripts allow-same-origin" src={url} />
|
|
547
|
+
````
|
|
548
|
+
|
|
549
|
+
正确示例:
|
|
550
|
+
|
|
551
|
+
<!-- prettier-ignore -->
|
|
552
|
+
````tsx
|
|
553
|
+
<iframe sandbox="allow-scripts" src={url} />
|
|
554
|
+
````
|
|
555
|
+
|
|
556
|
+
### `@eslint-react/dom-no-use-form-state`
|
|
557
|
+
|
|
558
|
+
检查该规则对应的代码约束。上游说明:Replaces usage of 'useFormState' with 'useActionState'.
|
|
559
|
+
|
|
560
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
561
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
562
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-use-form-state)
|
|
563
|
+
- 常见报告:`[Deprecated] Use 'useActionState' from 'react' package instead.`
|
|
564
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
565
|
+
|
|
566
|
+
错误示例:
|
|
567
|
+
|
|
568
|
+
<!-- prettier-ignore -->
|
|
569
|
+
````tsx
|
|
570
|
+
import { useFormState } from 'react-dom';
|
|
571
|
+
function Form() { const [state, action] = useFormState(save, initialState); return <form action={action}>{state.message}</form>; }
|
|
572
|
+
````
|
|
573
|
+
|
|
574
|
+
正确示例:
|
|
575
|
+
|
|
576
|
+
<!-- prettier-ignore -->
|
|
577
|
+
````tsx
|
|
578
|
+
import { useActionState } from 'react';
|
|
579
|
+
function Form() { const [state, action] = useActionState(save, initialState); return <form action={action}>{state.message}</form>; }
|
|
580
|
+
````
|
|
581
|
+
|
|
582
|
+
### `@eslint-react/dom-no-void-elements-with-children`
|
|
583
|
+
|
|
584
|
+
检查该规则对应的代码约束。上游说明:Disallows 'children' in void DOM elements.
|
|
585
|
+
|
|
586
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
587
|
+
- 自动修复:不支持或上游未声明
|
|
588
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/dom-no-void-elements-with-children)
|
|
589
|
+
- 常见报告:`'{{elementType}}' is a void element tag and must not have children.`
|
|
590
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
591
|
+
|
|
592
|
+
错误示例:
|
|
593
|
+
|
|
594
|
+
<!-- prettier-ignore -->
|
|
595
|
+
````tsx
|
|
596
|
+
<img src={url}>Avatar</img>
|
|
597
|
+
````
|
|
598
|
+
|
|
599
|
+
正确示例:
|
|
600
|
+
|
|
601
|
+
<!-- prettier-ignore -->
|
|
602
|
+
````tsx
|
|
603
|
+
<img src={url} alt="Avatar" />
|
|
604
|
+
````
|
|
605
|
+
|
|
606
|
+
### `@eslint-react/jsx-no-children-prop`
|
|
607
|
+
|
|
608
|
+
检查该规则对应的代码约束。上游说明:Disallows passing 'children' as a prop.
|
|
609
|
+
|
|
610
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
611
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
612
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/jsx-no-children-prop)
|
|
613
|
+
- 常见报告:`Do not pass 'children' as props.`;`Move 'children' to element content.`
|
|
614
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
615
|
+
|
|
616
|
+
错误示例:
|
|
617
|
+
|
|
618
|
+
<!-- prettier-ignore -->
|
|
619
|
+
````tsx
|
|
620
|
+
<UserCard children={<span>Name</span>} />
|
|
621
|
+
````
|
|
622
|
+
|
|
623
|
+
正确示例:
|
|
624
|
+
|
|
625
|
+
<!-- prettier-ignore -->
|
|
626
|
+
````tsx
|
|
627
|
+
<UserCard><span>Name</span></UserCard>
|
|
628
|
+
````
|
|
629
|
+
|
|
630
|
+
### `@eslint-react/jsx-no-children-prop-with-children`
|
|
631
|
+
|
|
632
|
+
检查该规则对应的代码约束。上游说明:Disallows passing 'children' as a prop when children are also passed as nested content.
|
|
633
|
+
|
|
634
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
635
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
636
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/jsx-no-children-prop-with-children)
|
|
637
|
+
- 常见报告:`Do not pass 'children' as a prop when the element already has children content.`;`Remove the nested children content.`;`Remove the 'children' prop.`
|
|
638
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
639
|
+
|
|
640
|
+
错误示例:
|
|
641
|
+
|
|
642
|
+
<!-- prettier-ignore -->
|
|
643
|
+
````tsx
|
|
644
|
+
<UserCard children={<span>A</span>}><span>B</span></UserCard>
|
|
645
|
+
````
|
|
646
|
+
|
|
647
|
+
正确示例:
|
|
648
|
+
|
|
649
|
+
<!-- prettier-ignore -->
|
|
650
|
+
````tsx
|
|
651
|
+
<UserCard><span>B</span></UserCard>
|
|
652
|
+
````
|
|
653
|
+
|
|
654
|
+
### `@eslint-react/jsx-no-comment-textnodes`
|
|
655
|
+
|
|
656
|
+
检查该规则对应的代码约束。上游说明:Prevents comment strings from being accidentally inserted into a JSX element's text nodes.
|
|
657
|
+
|
|
658
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
659
|
+
- 自动修复:不支持或上游未声明
|
|
660
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/jsx-no-comment-textnodes)
|
|
661
|
+
- 常见报告:`Possible misused comment in text node. Comments inside children section of tag should be placed inside braces.`
|
|
662
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
663
|
+
|
|
664
|
+
错误示例:
|
|
665
|
+
|
|
666
|
+
<!-- prettier-ignore -->
|
|
667
|
+
````tsx
|
|
668
|
+
<div>// temporary note</div>
|
|
669
|
+
````
|
|
670
|
+
|
|
671
|
+
正确示例:
|
|
672
|
+
|
|
673
|
+
<!-- prettier-ignore -->
|
|
674
|
+
````tsx
|
|
675
|
+
<div>{/* temporary note */}</div>
|
|
676
|
+
````
|
|
677
|
+
|
|
678
|
+
### `@eslint-react/jsx-no-key-after-spread`
|
|
679
|
+
|
|
680
|
+
检查该规则对应的代码约束。上游说明:Prevent patterns that cause deoptimization when using the automatic JSX runtime.
|
|
681
|
+
|
|
682
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
683
|
+
- 自动修复:不支持或上游未声明
|
|
684
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/jsx-no-key-after-spread)
|
|
685
|
+
- 常见报告:`Placing 'key' after spread props causes deoptimization when using the automatic JSX runtime. Put 'key' before any spread props.`
|
|
686
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
687
|
+
|
|
688
|
+
错误示例:
|
|
689
|
+
|
|
690
|
+
<!-- prettier-ignore -->
|
|
691
|
+
````tsx
|
|
692
|
+
const props = { className: 'row' };
|
|
693
|
+
<div {...props} key="row" />;
|
|
694
|
+
````
|
|
695
|
+
|
|
696
|
+
正确示例:
|
|
697
|
+
|
|
698
|
+
<!-- prettier-ignore -->
|
|
699
|
+
````tsx
|
|
700
|
+
const props = { className: 'row' };
|
|
701
|
+
<div key="row" {...props} />;
|
|
702
|
+
````
|
|
703
|
+
|
|
704
|
+
### `@eslint-react/jsx-no-leaked-dollar`
|
|
705
|
+
|
|
706
|
+
检查该规则对应的代码约束。上游说明:Catches `$` before `{expr}` in JSX — typically from template literal `${expr}` being copy-pasted into JSX without removing the `$`. The `$` "leaks" into the rendered output.
|
|
707
|
+
|
|
708
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
709
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
710
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/jsx-no-leaked-dollar)
|
|
711
|
+
- 常见报告:`Leaked '$' in JSX. This '$' will be rendered as text nodes.`;`Remove the text node '$'.`
|
|
712
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
713
|
+
|
|
714
|
+
错误示例:
|
|
715
|
+
|
|
716
|
+
<!-- prettier-ignore -->
|
|
717
|
+
````tsx
|
|
718
|
+
<span>Total: ${total}</span>
|
|
719
|
+
````
|
|
720
|
+
|
|
721
|
+
正确示例:
|
|
722
|
+
|
|
723
|
+
<!-- prettier-ignore -->
|
|
724
|
+
````tsx
|
|
725
|
+
<span>Total: {total}</span>
|
|
726
|
+
````
|
|
727
|
+
|
|
728
|
+
### `@eslint-react/jsx-no-leaked-semicolon`
|
|
729
|
+
|
|
730
|
+
检查该规则对应的代码约束。上游说明:Catches `;` at the start of JSX text nodes — typically from accidentally placing a statement-ending `;` inside JSX. The `;` "leaks" into the rendered output.
|
|
731
|
+
|
|
732
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
733
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
734
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/jsx-no-leaked-semicolon)
|
|
735
|
+
- 常见报告:`Leaked ';' in JSX. This ';' will be rendered as text nodes.`;`Remove the text node ';'.`
|
|
736
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
737
|
+
|
|
738
|
+
错误示例:
|
|
739
|
+
|
|
740
|
+
<!-- prettier-ignore -->
|
|
741
|
+
````tsx
|
|
742
|
+
<span>;
|
|
743
|
+
{label}</span>
|
|
744
|
+
````
|
|
745
|
+
|
|
746
|
+
正确示例:
|
|
747
|
+
|
|
748
|
+
<!-- prettier-ignore -->
|
|
749
|
+
````tsx
|
|
750
|
+
<span>{label}</span>
|
|
751
|
+
````
|
|
752
|
+
|
|
753
|
+
### `@eslint-react/jsx-no-namespace`
|
|
754
|
+
|
|
755
|
+
检查该规则对应的代码约束。上游说明:Disallow JSX namespace syntax, as React does not support them.
|
|
756
|
+
|
|
757
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
758
|
+
- 自动修复:不支持或上游未声明
|
|
759
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/jsx-no-namespace)
|
|
760
|
+
- 常见报告:`A React component '{{name}}' must not be in a namespace, as React does not support them.`
|
|
761
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
762
|
+
|
|
763
|
+
错误示例:
|
|
764
|
+
|
|
765
|
+
<!-- prettier-ignore -->
|
|
766
|
+
````tsx
|
|
767
|
+
<svg:path />
|
|
768
|
+
````
|
|
769
|
+
|
|
770
|
+
正确示例:
|
|
771
|
+
|
|
772
|
+
<!-- prettier-ignore -->
|
|
773
|
+
````tsx
|
|
774
|
+
<path />
|
|
775
|
+
````
|
|
776
|
+
|
|
777
|
+
### `@eslint-react/naming-convention-context-name`
|
|
778
|
+
|
|
779
|
+
检查该规则对应的代码约束。上游说明:Enforces identifier names assigned from `createContext` calls to be a valid component name with the suffix `Context`.
|
|
780
|
+
|
|
781
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
782
|
+
- 自动修复:不支持或上游未声明
|
|
783
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/naming-convention-context-name)
|
|
784
|
+
- 常见报告:`A context name must be a valid component name with the suffix 'Context'.`
|
|
785
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
786
|
+
|
|
787
|
+
错误示例:
|
|
788
|
+
|
|
789
|
+
<!-- prettier-ignore -->
|
|
790
|
+
````tsx
|
|
791
|
+
const Theme = createContext('light');
|
|
792
|
+
````
|
|
793
|
+
|
|
794
|
+
正确示例:
|
|
795
|
+
|
|
796
|
+
<!-- prettier-ignore -->
|
|
797
|
+
````tsx
|
|
798
|
+
const ThemeContext = createContext('light');
|
|
799
|
+
````
|
|
800
|
+
|
|
801
|
+
### `@eslint-react/naming-convention-id-name`
|
|
802
|
+
|
|
803
|
+
检查该规则对应的代码约束。上游说明:Enforces identifier names assigned from 'useId' calls to be either 'id' or end with 'Id'.
|
|
804
|
+
|
|
805
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
806
|
+
- 自动修复:不支持或上游未声明
|
|
807
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/naming-convention-id-name)
|
|
808
|
+
- 常见报告:`An identifier assigned from 'useId' must be named 'id' or end with 'Id'.`
|
|
809
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
810
|
+
|
|
811
|
+
错误示例:
|
|
812
|
+
|
|
813
|
+
<!-- prettier-ignore -->
|
|
814
|
+
````tsx
|
|
815
|
+
const userIdentifier = useId();
|
|
816
|
+
````
|
|
817
|
+
|
|
818
|
+
正确示例:
|
|
819
|
+
|
|
820
|
+
<!-- prettier-ignore -->
|
|
821
|
+
````tsx
|
|
822
|
+
const userId = useId();
|
|
823
|
+
````
|
|
824
|
+
|
|
825
|
+
### `@eslint-react/naming-convention-ref-name`
|
|
826
|
+
|
|
827
|
+
检查该规则对应的代码约束。上游说明:Enforces identifier names assigned from 'useRef' calls to be either 'ref' or end with 'Ref'.
|
|
828
|
+
|
|
829
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
830
|
+
- 自动修复:不支持或上游未声明
|
|
831
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/naming-convention-ref-name)
|
|
832
|
+
- 常见报告:`A ref identifier must be named 'ref' or ending in 'Ref'.`
|
|
833
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
834
|
+
|
|
835
|
+
错误示例:
|
|
836
|
+
|
|
837
|
+
<!-- prettier-ignore -->
|
|
838
|
+
````tsx
|
|
839
|
+
const element = useRef<HTMLDivElement>(null);
|
|
840
|
+
````
|
|
841
|
+
|
|
842
|
+
正确示例:
|
|
843
|
+
|
|
844
|
+
<!-- prettier-ignore -->
|
|
845
|
+
````tsx
|
|
846
|
+
const elementRef = useRef<HTMLDivElement>(null);
|
|
847
|
+
````
|
|
848
|
+
|
|
849
|
+
### `@eslint-react/no-access-state-in-setstate`
|
|
850
|
+
|
|
851
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows accessing 'this.state' inside 'setState' calls.
|
|
852
|
+
|
|
853
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
854
|
+
- 自动修复:不支持或上游未声明
|
|
855
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-access-state-in-setstate)
|
|
856
|
+
- 常见报告:`Do not access 'this.state' within 'setState'. Use the update function instead.`
|
|
857
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
858
|
+
|
|
859
|
+
错误示例:
|
|
860
|
+
|
|
861
|
+
<!-- prettier-ignore -->
|
|
862
|
+
````tsx
|
|
863
|
+
class Counter extends Component { increment() { this.setState({ count: this.state.count + 1 }); } render() { return <span>{this.state.count}</span>; } }
|
|
864
|
+
````
|
|
865
|
+
|
|
866
|
+
正确示例:
|
|
867
|
+
|
|
868
|
+
<!-- prettier-ignore -->
|
|
869
|
+
````tsx
|
|
870
|
+
class Counter extends Component { increment() { this.setState((state) => ({ count: state.count + 1 })); } render() { return <span>{this.state.count}</span>; } }
|
|
871
|
+
````
|
|
872
|
+
|
|
873
|
+
### `@eslint-react/no-array-index-key`
|
|
874
|
+
|
|
875
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows using an item's index in the array as its key.
|
|
876
|
+
|
|
877
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
878
|
+
- 自动修复:不支持或上游未声明
|
|
879
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-array-index-key)
|
|
880
|
+
- 常见报告:`Do not use item index in the array as its key.`
|
|
881
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
882
|
+
|
|
883
|
+
错误示例:
|
|
884
|
+
|
|
885
|
+
<!-- prettier-ignore -->
|
|
886
|
+
````tsx
|
|
887
|
+
items.map((item, index) => <Row key={index} item={item} />)
|
|
888
|
+
````
|
|
889
|
+
|
|
890
|
+
正确示例:
|
|
891
|
+
|
|
892
|
+
<!-- prettier-ignore -->
|
|
893
|
+
````tsx
|
|
894
|
+
items.map((item) => <Row key={item.id} item={item} />)
|
|
895
|
+
````
|
|
896
|
+
|
|
897
|
+
### `@eslint-react/no-children-count`
|
|
898
|
+
|
|
899
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows the use of 'Children.count' from the 'react' package.
|
|
900
|
+
|
|
901
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
902
|
+
- 自动修复:不支持或上游未声明
|
|
903
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-children-count)
|
|
904
|
+
- 常见报告:`Using 'Children.count' is uncommon and can lead to fragile code. Use alternatives instead.`
|
|
905
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
906
|
+
|
|
907
|
+
错误示例:
|
|
908
|
+
|
|
909
|
+
<!-- prettier-ignore -->
|
|
910
|
+
````tsx
|
|
911
|
+
const count = Children.count(children);
|
|
912
|
+
````
|
|
913
|
+
|
|
914
|
+
正确示例:
|
|
915
|
+
|
|
916
|
+
<!-- prettier-ignore -->
|
|
917
|
+
````tsx
|
|
918
|
+
const count = items.length;
|
|
919
|
+
````
|
|
920
|
+
|
|
921
|
+
### `@eslint-react/no-children-for-each`
|
|
922
|
+
|
|
923
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows the use of 'Children.forEach' from the 'react' package.
|
|
924
|
+
|
|
925
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
926
|
+
- 自动修复:不支持或上游未声明
|
|
927
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-children-for-each)
|
|
928
|
+
- 常见报告:`Using 'Children.forEach' is uncommon and can lead to fragile code. Use alternatives instead.`
|
|
929
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
930
|
+
|
|
931
|
+
错误示例:
|
|
932
|
+
|
|
933
|
+
<!-- prettier-ignore -->
|
|
934
|
+
````tsx
|
|
935
|
+
Children.forEach(children, renderChild);
|
|
936
|
+
````
|
|
937
|
+
|
|
938
|
+
正确示例:
|
|
939
|
+
|
|
940
|
+
<!-- prettier-ignore -->
|
|
941
|
+
````tsx
|
|
942
|
+
items.forEach(renderItem);
|
|
943
|
+
````
|
|
944
|
+
|
|
945
|
+
### `@eslint-react/no-children-map`
|
|
946
|
+
|
|
947
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows the use of 'Children.map' from the 'react' package.
|
|
948
|
+
|
|
949
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
950
|
+
- 自动修复:不支持或上游未声明
|
|
951
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-children-map)
|
|
952
|
+
- 常见报告:`Using 'Children.map' is uncommon and can lead to fragile code. Use alternatives instead.`
|
|
953
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
954
|
+
|
|
955
|
+
错误示例:
|
|
956
|
+
|
|
957
|
+
<!-- prettier-ignore -->
|
|
958
|
+
````tsx
|
|
959
|
+
const rows = Children.map(children, wrapChild);
|
|
960
|
+
````
|
|
961
|
+
|
|
962
|
+
正确示例:
|
|
963
|
+
|
|
964
|
+
<!-- prettier-ignore -->
|
|
965
|
+
````tsx
|
|
966
|
+
const rows = items.map(renderItem);
|
|
967
|
+
````
|
|
968
|
+
|
|
969
|
+
### `@eslint-react/no-children-only`
|
|
970
|
+
|
|
971
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows the use of 'Children.only' from the 'react' package.
|
|
972
|
+
|
|
973
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
974
|
+
- 自动修复:不支持或上游未声明
|
|
975
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-children-only)
|
|
976
|
+
- 常见报告:`Using 'Children.only' is uncommon and can lead to fragile code. Use alternatives instead.`
|
|
977
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
978
|
+
|
|
979
|
+
错误示例:
|
|
980
|
+
|
|
981
|
+
<!-- prettier-ignore -->
|
|
982
|
+
````tsx
|
|
983
|
+
const child = Children.only(children);
|
|
984
|
+
````
|
|
985
|
+
|
|
986
|
+
正确示例:
|
|
987
|
+
|
|
988
|
+
<!-- prettier-ignore -->
|
|
989
|
+
````tsx
|
|
990
|
+
const child = Array.isArray(children) ? children[0] : children;
|
|
991
|
+
````
|
|
992
|
+
|
|
993
|
+
### `@eslint-react/no-children-to-array`
|
|
994
|
+
|
|
995
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows the use of 'Children.toArray' from the 'react' package.
|
|
996
|
+
|
|
997
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
998
|
+
- 自动修复:不支持或上游未声明
|
|
999
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-children-to-array)
|
|
1000
|
+
- 常见报告:`Using 'Children.toArray' is uncommon and can lead to fragile code. Use alternatives instead.`
|
|
1001
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1002
|
+
|
|
1003
|
+
错误示例:
|
|
1004
|
+
|
|
1005
|
+
<!-- prettier-ignore -->
|
|
1006
|
+
````tsx
|
|
1007
|
+
const list = Children.toArray(children);
|
|
1008
|
+
````
|
|
1009
|
+
|
|
1010
|
+
正确示例:
|
|
1011
|
+
|
|
1012
|
+
<!-- prettier-ignore -->
|
|
1013
|
+
````tsx
|
|
1014
|
+
const list = items.slice();
|
|
1015
|
+
````
|
|
1016
|
+
|
|
1017
|
+
### `@eslint-react/no-clone-element`
|
|
1018
|
+
|
|
1019
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows 'cloneElement'.
|
|
1020
|
+
|
|
1021
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1022
|
+
- 自动修复:不支持或上游未声明
|
|
1023
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-clone-element)
|
|
1024
|
+
- 常见报告:`Using 'cloneElement' is uncommon and can lead to fragile code. Use alternatives instead.`
|
|
1025
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1026
|
+
|
|
1027
|
+
错误示例:
|
|
1028
|
+
|
|
1029
|
+
<!-- prettier-ignore -->
|
|
1030
|
+
````tsx
|
|
1031
|
+
const button = cloneElement(child, { disabled: true });
|
|
1032
|
+
````
|
|
1033
|
+
|
|
1034
|
+
正确示例:
|
|
1035
|
+
|
|
1036
|
+
<!-- prettier-ignore -->
|
|
1037
|
+
````tsx
|
|
1038
|
+
const button = <Button {...props} disabled />;
|
|
1039
|
+
````
|
|
1040
|
+
|
|
1041
|
+
### `@eslint-react/no-component-will-mount`
|
|
1042
|
+
|
|
1043
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Replaces usage of 'componentWillMount' with 'UNSAFE_componentWillMount'.
|
|
1044
|
+
|
|
1045
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1046
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1047
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-component-will-mount)
|
|
1048
|
+
- 常见报告:`[Deprecated] Use 'UNSAFE_componentWillMount' instead.`
|
|
1049
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1050
|
+
|
|
1051
|
+
错误示例:
|
|
1052
|
+
|
|
1053
|
+
<!-- prettier-ignore -->
|
|
1054
|
+
````tsx
|
|
1055
|
+
class Page extends Component { componentWillMount() { load(); } }
|
|
1056
|
+
````
|
|
1057
|
+
|
|
1058
|
+
正确示例:
|
|
1059
|
+
|
|
1060
|
+
<!-- prettier-ignore -->
|
|
1061
|
+
````tsx
|
|
1062
|
+
class Page extends Component { componentDidMount() { load(); } }
|
|
1063
|
+
````
|
|
1064
|
+
|
|
1065
|
+
### `@eslint-react/no-component-will-receive-props`
|
|
1066
|
+
|
|
1067
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Replaces usage of 'componentWillReceiveProps' with 'UNSAFE_componentWillReceiveProps'.
|
|
1068
|
+
|
|
1069
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1070
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1071
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-component-will-receive-props)
|
|
1072
|
+
- 常见报告:`[Deprecated] Use 'UNSAFE_componentWillReceiveProps' instead.`
|
|
1073
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1074
|
+
|
|
1075
|
+
错误示例:
|
|
1076
|
+
|
|
1077
|
+
<!-- prettier-ignore -->
|
|
1078
|
+
````tsx
|
|
1079
|
+
class Page extends Component { componentWillReceiveProps(next: Props) { sync(next); } }
|
|
1080
|
+
````
|
|
1081
|
+
|
|
1082
|
+
正确示例:
|
|
1083
|
+
|
|
1084
|
+
<!-- prettier-ignore -->
|
|
1085
|
+
````tsx
|
|
1086
|
+
class Page extends Component { componentDidUpdate(previous: Props) { if (previous.id !== this.props.id) sync(this.props); } }
|
|
1087
|
+
````
|
|
1088
|
+
|
|
1089
|
+
### `@eslint-react/no-component-will-update`
|
|
1090
|
+
|
|
1091
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Replaces usage of 'componentWillUpdate' with 'UNSAFE_componentWillUpdate'.
|
|
1092
|
+
|
|
1093
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1094
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1095
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-component-will-update)
|
|
1096
|
+
- 常见报告:`[Deprecated] Use 'UNSAFE_componentWillUpdate' instead.`
|
|
1097
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1098
|
+
|
|
1099
|
+
错误示例:
|
|
1100
|
+
|
|
1101
|
+
<!-- prettier-ignore -->
|
|
1102
|
+
````tsx
|
|
1103
|
+
class Page extends Component { componentWillUpdate() { saveLayout(); } }
|
|
1104
|
+
````
|
|
1105
|
+
|
|
1106
|
+
正确示例:
|
|
1107
|
+
|
|
1108
|
+
<!-- prettier-ignore -->
|
|
1109
|
+
````tsx
|
|
1110
|
+
class Page extends Component { componentDidUpdate() { saveLayout(); } }
|
|
1111
|
+
````
|
|
1112
|
+
|
|
1113
|
+
### `@eslint-react/no-context-provider`
|
|
1114
|
+
|
|
1115
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Replaces usage of '<Context.Provider>' with '<Context>'.
|
|
1116
|
+
|
|
1117
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1118
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1119
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-context-provider)
|
|
1120
|
+
- 常见报告:`In React 19, you can render '<Context>' as a provider instead of '<Context.Provider>'.`;`Replace '<Context.Provider>' with '<Context>'.`
|
|
1121
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1122
|
+
|
|
1123
|
+
错误示例:
|
|
1124
|
+
|
|
1125
|
+
<!-- prettier-ignore -->
|
|
1126
|
+
````tsx
|
|
1127
|
+
<ThemeContext.Provider value={theme}><Page /></ThemeContext.Provider>
|
|
1128
|
+
````
|
|
1129
|
+
|
|
1130
|
+
正确示例:
|
|
1131
|
+
|
|
1132
|
+
<!-- prettier-ignore -->
|
|
1133
|
+
````tsx
|
|
1134
|
+
<ThemeContext value={theme}><Page /></ThemeContext>
|
|
1135
|
+
````
|
|
1136
|
+
|
|
1137
|
+
### `@eslint-react/no-create-ref`
|
|
1138
|
+
|
|
1139
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows 'createRef' in function components and Hooks.
|
|
1140
|
+
|
|
1141
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1142
|
+
- 自动修复:不支持或上游未声明
|
|
1143
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-create-ref)
|
|
1144
|
+
- 常见报告:`[Deprecated] Use 'useRef' instead.`
|
|
1145
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1146
|
+
|
|
1147
|
+
错误示例:
|
|
1148
|
+
|
|
1149
|
+
<!-- prettier-ignore -->
|
|
1150
|
+
````tsx
|
|
1151
|
+
function Input() { const inputRef = createRef<HTMLInputElement>(); return <input ref={inputRef} />; }
|
|
1152
|
+
````
|
|
1153
|
+
|
|
1154
|
+
正确示例:
|
|
1155
|
+
|
|
1156
|
+
<!-- prettier-ignore -->
|
|
1157
|
+
````tsx
|
|
1158
|
+
function Input() { const inputRef = useRef<HTMLInputElement>(null); return <input ref={inputRef} />; }
|
|
1159
|
+
````
|
|
1160
|
+
|
|
1161
|
+
### `@eslint-react/no-direct-mutation-state`
|
|
1162
|
+
|
|
1163
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows direct mutation of 'this.state'.
|
|
1164
|
+
|
|
1165
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1166
|
+
- 自动修复:不支持或上游未声明
|
|
1167
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-direct-mutation-state)
|
|
1168
|
+
- 常见报告:`Do not mutate state directly. Use 'setState()' instead.`
|
|
1169
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1170
|
+
|
|
1171
|
+
错误示例:
|
|
1172
|
+
|
|
1173
|
+
<!-- prettier-ignore -->
|
|
1174
|
+
````tsx
|
|
1175
|
+
class Counter extends Component { increment() { this.state.count += 1; } render() { return <span>{this.state.count}</span>; } }
|
|
1176
|
+
````
|
|
1177
|
+
|
|
1178
|
+
正确示例:
|
|
1179
|
+
|
|
1180
|
+
<!-- prettier-ignore -->
|
|
1181
|
+
````tsx
|
|
1182
|
+
class Counter extends Component { increment() { this.setState((state) => ({ count: state.count + 1 })); } render() { return <span>{this.state.count}</span>; } }
|
|
1183
|
+
````
|
|
1184
|
+
|
|
1185
|
+
### `@eslint-react/no-forward-ref`
|
|
1186
|
+
|
|
1187
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Replaces usage of 'forwardRef' with passing 'ref' as a prop.
|
|
1188
|
+
|
|
1189
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1190
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1191
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-forward-ref)
|
|
1192
|
+
- 常见报告:`In React 19, 'forwardRef' is no longer necessary. Pass 'ref' as a prop instead.`;`Replace 'forwardRef' with passing 'ref' as a prop.`
|
|
1193
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1194
|
+
|
|
1195
|
+
错误示例:
|
|
1196
|
+
|
|
1197
|
+
<!-- prettier-ignore -->
|
|
1198
|
+
````tsx
|
|
1199
|
+
const Input = forwardRef<HTMLInputElement, Props>((props, ref) => <input ref={ref} />);
|
|
1200
|
+
````
|
|
1201
|
+
|
|
1202
|
+
正确示例:
|
|
1203
|
+
|
|
1204
|
+
<!-- prettier-ignore -->
|
|
1205
|
+
````tsx
|
|
1206
|
+
function Input({ ref, ...props }: Props & { ref?: Ref<HTMLInputElement> }) { return <input ref={ref} {...props} />; }
|
|
1207
|
+
````
|
|
1208
|
+
|
|
1209
|
+
### `@eslint-react/no-leaked-conditional-rendering`
|
|
1210
|
+
|
|
1211
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Prevents problematic leaked values from being rendered.
|
|
1212
|
+
|
|
1213
|
+
- 生效级别与范围:error: React TSX
|
|
1214
|
+
- 自动修复:不支持或上游未声明
|
|
1215
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-leaked-conditional-rendering)
|
|
1216
|
+
- 常见报告:`Potential leaked value {{value}} that might cause unintentionally rendered values or rendering crashes.`
|
|
1217
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1218
|
+
|
|
1219
|
+
错误示例:
|
|
1220
|
+
|
|
1221
|
+
<!-- prettier-ignore -->
|
|
1222
|
+
````tsx
|
|
1223
|
+
interface Props { count: number }
|
|
1224
|
+
function List({ count }: Props) { return <div>{count && <Items />}</div>; }
|
|
1225
|
+
````
|
|
1226
|
+
|
|
1227
|
+
正确示例:
|
|
1228
|
+
|
|
1229
|
+
<!-- prettier-ignore -->
|
|
1230
|
+
````tsx
|
|
1231
|
+
interface Props { count: number }
|
|
1232
|
+
function List({ count }: Props) { return <div>{count > 0 ? <Items /> : null}</div>; }
|
|
1233
|
+
````
|
|
1234
|
+
|
|
1235
|
+
### `@eslint-react/no-missing-key`
|
|
1236
|
+
|
|
1237
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows missing 'key' on items in list rendering.
|
|
1238
|
+
|
|
1239
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1240
|
+
- 自动修复:不支持或上游未声明
|
|
1241
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-missing-key)
|
|
1242
|
+
- 常见报告:`Missing 'key' for element when rendering list.`;`Use fragment component instead of '<>' because it does not support 'key'.`
|
|
1243
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1244
|
+
|
|
1245
|
+
错误示例:
|
|
1246
|
+
|
|
1247
|
+
<!-- prettier-ignore -->
|
|
1248
|
+
````tsx
|
|
1249
|
+
items.map((item) => <Row item={item} />)
|
|
1250
|
+
````
|
|
1251
|
+
|
|
1252
|
+
正确示例:
|
|
1253
|
+
|
|
1254
|
+
<!-- prettier-ignore -->
|
|
1255
|
+
````tsx
|
|
1256
|
+
items.map((item) => <Row key={item.id} item={item} />)
|
|
1257
|
+
````
|
|
1258
|
+
|
|
1259
|
+
### `@eslint-react/no-nested-component-definitions`
|
|
1260
|
+
|
|
1261
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows nesting component definitions inside other components.
|
|
1262
|
+
|
|
1263
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1264
|
+
- 自动修复:不支持或上游未声明
|
|
1265
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-nested-component-definitions)
|
|
1266
|
+
- 常见报告:`Do not nest component definitions inside other components or props. {{suggestion}}`
|
|
1267
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1268
|
+
|
|
1269
|
+
错误示例:
|
|
1270
|
+
|
|
1271
|
+
<!-- prettier-ignore -->
|
|
1272
|
+
````tsx
|
|
1273
|
+
function Page() { function Header() { return <h1>Title</h1>; } return <Header />; }
|
|
1274
|
+
````
|
|
1275
|
+
|
|
1276
|
+
正确示例:
|
|
1277
|
+
|
|
1278
|
+
<!-- prettier-ignore -->
|
|
1279
|
+
````tsx
|
|
1280
|
+
function Header() { return <h1>Title</h1>; }
|
|
1281
|
+
function Page() { return <Header />; }
|
|
1282
|
+
````
|
|
1283
|
+
|
|
1284
|
+
### `@eslint-react/no-nested-lazy-component-declarations`
|
|
1285
|
+
|
|
1286
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows nesting lazy component declarations inside other components or hooks.
|
|
1287
|
+
|
|
1288
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1289
|
+
- 自动修复:不支持或上游未声明
|
|
1290
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-nested-lazy-component-declarations)
|
|
1291
|
+
- 常见报告:`Do not declare lazy components inside other components or hooks. Instead, always declare them at the top level of your module.`
|
|
1292
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1293
|
+
|
|
1294
|
+
错误示例:
|
|
1295
|
+
|
|
1296
|
+
<!-- prettier-ignore -->
|
|
1297
|
+
````tsx
|
|
1298
|
+
function Page() { const Settings = lazy(() => import('./Settings')); return <Settings />; }
|
|
1299
|
+
````
|
|
1300
|
+
|
|
1301
|
+
正确示例:
|
|
1302
|
+
|
|
1303
|
+
<!-- prettier-ignore -->
|
|
1304
|
+
````tsx
|
|
1305
|
+
const Settings = lazy(() => import('./Settings'));
|
|
1306
|
+
function Page() { return <Settings />; }
|
|
1307
|
+
````
|
|
1308
|
+
|
|
1309
|
+
### `@eslint-react/no-set-state-in-component-did-mount`
|
|
1310
|
+
|
|
1311
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows calling 'this.setState' in 'componentDidMount' outside functions such as callbacks.
|
|
1312
|
+
|
|
1313
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1314
|
+
- 自动修复:不支持或上游未声明
|
|
1315
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-set-state-in-component-did-mount)
|
|
1316
|
+
- 常见报告:`Do not call 'this.setState' in 'componentDidMount' outside functions such as callbacks.`
|
|
1317
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1318
|
+
|
|
1319
|
+
错误示例:
|
|
1320
|
+
|
|
1321
|
+
<!-- prettier-ignore -->
|
|
1322
|
+
````tsx
|
|
1323
|
+
class Page extends Component { componentDidMount() { this.setState({ ready: true }); } }
|
|
1324
|
+
````
|
|
1325
|
+
|
|
1326
|
+
正确示例:
|
|
1327
|
+
|
|
1328
|
+
<!-- prettier-ignore -->
|
|
1329
|
+
````tsx
|
|
1330
|
+
class Page extends Component { componentDidMount() { subscribe(() => this.setState({ ready: true })); } }
|
|
1331
|
+
````
|
|
1332
|
+
|
|
1333
|
+
### `@eslint-react/no-set-state-in-component-did-update`
|
|
1334
|
+
|
|
1335
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows calling 'this.setState' in 'componentDidUpdate' outside functions such as callbacks.
|
|
1336
|
+
|
|
1337
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1338
|
+
- 自动修复:不支持或上游未声明
|
|
1339
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-set-state-in-component-did-update)
|
|
1340
|
+
- 常见报告:`Do not call 'this.setState' in 'componentDidUpdate' outside functions such as callbacks.`
|
|
1341
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1342
|
+
|
|
1343
|
+
错误示例:
|
|
1344
|
+
|
|
1345
|
+
<!-- prettier-ignore -->
|
|
1346
|
+
````tsx
|
|
1347
|
+
class Page extends Component { componentDidUpdate() { this.setState({ ready: true }); } }
|
|
1348
|
+
````
|
|
1349
|
+
|
|
1350
|
+
正确示例:
|
|
1351
|
+
|
|
1352
|
+
<!-- prettier-ignore -->
|
|
1353
|
+
````tsx
|
|
1354
|
+
class Page extends Component { componentDidUpdate() { schedule(() => this.setState({ ready: true })); } }
|
|
1355
|
+
````
|
|
1356
|
+
|
|
1357
|
+
### `@eslint-react/no-set-state-in-component-will-update`
|
|
1358
|
+
|
|
1359
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Disallows calling 'this.setState' in 'componentWillUpdate' outside functions such as callbacks.
|
|
1360
|
+
|
|
1361
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1362
|
+
- 自动修复:不支持或上游未声明
|
|
1363
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-set-state-in-component-will-update)
|
|
1364
|
+
- 常见报告:`Do not call 'this.setState' in 'componentWillUpdate' outside functions such as callbacks.`
|
|
1365
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1366
|
+
|
|
1367
|
+
错误示例:
|
|
1368
|
+
|
|
1369
|
+
<!-- prettier-ignore -->
|
|
1370
|
+
````tsx
|
|
1371
|
+
class Page extends Component { componentWillUpdate() { this.setState({ ready: true }); } }
|
|
1372
|
+
````
|
|
1373
|
+
|
|
1374
|
+
正确示例:
|
|
1375
|
+
|
|
1376
|
+
<!-- prettier-ignore -->
|
|
1377
|
+
````tsx
|
|
1378
|
+
class Page extends Component { componentWillUpdate() { schedule(() => this.setState({ ready: true })); } }
|
|
1379
|
+
````
|
|
1380
|
+
|
|
1381
|
+
### `@eslint-react/no-unnecessary-use-prefix`
|
|
1382
|
+
|
|
1383
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Enforces that a function with the 'use' prefix uses at least one Hook inside it.
|
|
1384
|
+
|
|
1385
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1386
|
+
- 自动修复:不支持或上游未声明
|
|
1387
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-unnecessary-use-prefix)
|
|
1388
|
+
- 常见报告:`If your function doesn't call any Hooks, avoid the 'use' prefix. Instead, write it as a regular function without the 'use' prefix.`
|
|
1389
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1390
|
+
|
|
1391
|
+
错误示例:
|
|
1392
|
+
|
|
1393
|
+
<!-- prettier-ignore -->
|
|
1394
|
+
````tsx
|
|
1395
|
+
function useFormatter(value: string) { return value.trim(); }
|
|
1396
|
+
````
|
|
1397
|
+
|
|
1398
|
+
正确示例:
|
|
1399
|
+
|
|
1400
|
+
<!-- prettier-ignore -->
|
|
1401
|
+
````tsx
|
|
1402
|
+
function formatValue(value: string) { return value.trim(); }
|
|
1403
|
+
````
|
|
1404
|
+
|
|
1405
|
+
### `@eslint-react/no-unsafe-component-will-mount`
|
|
1406
|
+
|
|
1407
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Warns about the use of 'UNSAFE_componentWillMount' in class components.
|
|
1408
|
+
|
|
1409
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1410
|
+
- 自动修复:不支持或上游未声明
|
|
1411
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-unsafe-component-will-mount)
|
|
1412
|
+
- 常见报告:`Do not use 'UNSAFE_componentWillMount'.`
|
|
1413
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1414
|
+
|
|
1415
|
+
错误示例:
|
|
1416
|
+
|
|
1417
|
+
<!-- prettier-ignore -->
|
|
1418
|
+
````tsx
|
|
1419
|
+
class Page extends Component { UNSAFE_componentWillMount() { load(); } }
|
|
1420
|
+
````
|
|
1421
|
+
|
|
1422
|
+
正确示例:
|
|
1423
|
+
|
|
1424
|
+
<!-- prettier-ignore -->
|
|
1425
|
+
````tsx
|
|
1426
|
+
class Page extends Component { componentDidMount() { load(); } }
|
|
1427
|
+
````
|
|
1428
|
+
|
|
1429
|
+
### `@eslint-react/no-unsafe-component-will-receive-props`
|
|
1430
|
+
|
|
1431
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Warns about the use of 'UNSAFE_componentWillReceiveProps' in class components.
|
|
1432
|
+
|
|
1433
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1434
|
+
- 自动修复:不支持或上游未声明
|
|
1435
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-unsafe-component-will-receive-props)
|
|
1436
|
+
- 常见报告:`Do not use 'UNSAFE_componentWillReceiveProps'.`
|
|
1437
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1438
|
+
|
|
1439
|
+
错误示例:
|
|
1440
|
+
|
|
1441
|
+
<!-- prettier-ignore -->
|
|
1442
|
+
````tsx
|
|
1443
|
+
class Page extends Component { UNSAFE_componentWillReceiveProps(next: Props) { sync(next); } }
|
|
1444
|
+
````
|
|
1445
|
+
|
|
1446
|
+
正确示例:
|
|
1447
|
+
|
|
1448
|
+
<!-- prettier-ignore -->
|
|
1449
|
+
````tsx
|
|
1450
|
+
class Page extends Component { componentDidUpdate(previous: Props) { if (previous.id !== this.props.id) sync(this.props); } }
|
|
1451
|
+
````
|
|
1452
|
+
|
|
1453
|
+
### `@eslint-react/no-unsafe-component-will-update`
|
|
1454
|
+
|
|
1455
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Warns about the use of 'UNSAFE_componentWillUpdate' in class components.
|
|
1456
|
+
|
|
1457
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1458
|
+
- 自动修复:不支持或上游未声明
|
|
1459
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-unsafe-component-will-update)
|
|
1460
|
+
- 常见报告:`Do not use 'UNSAFE_componentWillUpdate'.`
|
|
1461
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1462
|
+
|
|
1463
|
+
错误示例:
|
|
1464
|
+
|
|
1465
|
+
<!-- prettier-ignore -->
|
|
1466
|
+
````tsx
|
|
1467
|
+
class Page extends Component { UNSAFE_componentWillUpdate() { saveLayout(); } }
|
|
1468
|
+
````
|
|
1469
|
+
|
|
1470
|
+
正确示例:
|
|
1471
|
+
|
|
1472
|
+
<!-- prettier-ignore -->
|
|
1473
|
+
````tsx
|
|
1474
|
+
class Page extends Component { componentDidUpdate() { saveLayout(); } }
|
|
1475
|
+
````
|
|
1476
|
+
|
|
1477
|
+
### `@eslint-react/no-unused-class-component-members`
|
|
1478
|
+
|
|
1479
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Warns about unused class component methods and properties.
|
|
1480
|
+
|
|
1481
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1482
|
+
- 自动修复:不支持或上游未声明
|
|
1483
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-unused-class-component-members)
|
|
1484
|
+
- 常见报告:`Unused method or property '{{methodName}}'' of class '{{className}}'.`
|
|
1485
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1486
|
+
|
|
1487
|
+
错误示例:
|
|
1488
|
+
|
|
1489
|
+
<!-- prettier-ignore -->
|
|
1490
|
+
````tsx
|
|
1491
|
+
class Page extends Component { unused = 1; render() { return <div />; } }
|
|
1492
|
+
````
|
|
1493
|
+
|
|
1494
|
+
正确示例:
|
|
1495
|
+
|
|
1496
|
+
<!-- prettier-ignore -->
|
|
1497
|
+
````tsx
|
|
1498
|
+
class Page extends Component { title = 'Page'; render() { return <div>{this.title}</div>; } }
|
|
1499
|
+
````
|
|
1500
|
+
|
|
1501
|
+
### `@eslint-react/no-use-context`
|
|
1502
|
+
|
|
1503
|
+
禁止出现该规则定义的不安全、无效或易误解结构。上游说明:Replaces usage of 'useContext' with 'use'.
|
|
1504
|
+
|
|
1505
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1506
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1507
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/no-use-context)
|
|
1508
|
+
- 常见报告:`In React 19, 'use' is preferred over 'useContext' because it is more flexible.`;`Replace 'useContext' with 'use'.`
|
|
1509
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1510
|
+
|
|
1511
|
+
错误示例:
|
|
1512
|
+
|
|
1513
|
+
<!-- prettier-ignore -->
|
|
1514
|
+
````tsx
|
|
1515
|
+
const theme = useContext(ThemeContext);
|
|
1516
|
+
````
|
|
1517
|
+
|
|
1518
|
+
正确示例:
|
|
1519
|
+
|
|
1520
|
+
<!-- prettier-ignore -->
|
|
1521
|
+
````tsx
|
|
1522
|
+
const theme = use(ThemeContext);
|
|
1523
|
+
````
|
|
1524
|
+
|
|
1525
|
+
### `@eslint-react/rsc-function-definition`
|
|
1526
|
+
|
|
1527
|
+
检查该规则对应的代码约束。上游说明:Validates and transforms React Client/Server Function definitions.
|
|
1528
|
+
|
|
1529
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1530
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1531
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/rsc-function-definition)
|
|
1532
|
+
- 常见报告:`Functions exported from files with 'use server' directive are React Server Functions and therefore must be async.`;`The '{{name}}' directive must be at the very beginning of the file, before any imports or other code.`;`The '{{name}}' directive must be written with single or double quotes, not backticks.`
|
|
1533
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1534
|
+
|
|
1535
|
+
错误示例:
|
|
1536
|
+
|
|
1537
|
+
<!-- prettier-ignore -->
|
|
1538
|
+
````tsx
|
|
1539
|
+
"use server";
|
|
1540
|
+
export function save() { persist(); }
|
|
1541
|
+
````
|
|
1542
|
+
|
|
1543
|
+
正确示例:
|
|
1544
|
+
|
|
1545
|
+
<!-- prettier-ignore -->
|
|
1546
|
+
````tsx
|
|
1547
|
+
"use server";
|
|
1548
|
+
export async function save(): Promise<void> { await persist(); }
|
|
1549
|
+
````
|
|
1550
|
+
|
|
1551
|
+
### `@eslint-react/use-state`
|
|
1552
|
+
|
|
1553
|
+
检查该规则对应的代码约束。上游说明:Enforces correct usage of 'useState', including destructuring, symmetric naming of the value and setter, and wrapping expensive initializers in a lazy initializer function.
|
|
1554
|
+
|
|
1555
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1556
|
+
- 自动修复:不支持或上游未声明
|
|
1557
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/use-state)
|
|
1558
|
+
- 常见报告:`useState should be destructured into a value and setter pair, e.g., const [state, setState] = useState(...).`;`To prevent re-computation, consider using lazy initial state for useState calls that involve function calls. Ex: 'useState(() => getValue())'.`;`The setter should be named 'set' followed by the capitalized state variable name, e.g., 'setState' for 'state'.`
|
|
1559
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1560
|
+
|
|
1561
|
+
错误示例:
|
|
1562
|
+
|
|
1563
|
+
<!-- prettier-ignore -->
|
|
1564
|
+
````tsx
|
|
1565
|
+
const state = useState(0);
|
|
1566
|
+
````
|
|
1567
|
+
|
|
1568
|
+
正确示例:
|
|
1569
|
+
|
|
1570
|
+
<!-- prettier-ignore -->
|
|
1571
|
+
````tsx
|
|
1572
|
+
const [count, setCount] = useState(0);
|
|
1573
|
+
````
|
|
1574
|
+
|
|
1575
|
+
### `@eslint-react/web-api-no-leaked-event-listener`
|
|
1576
|
+
|
|
1577
|
+
检查该规则对应的代码约束。上游说明:Enforces that every 'addEventListener' in a component or custom hook has a corresponding 'removeEventListener'.
|
|
1578
|
+
|
|
1579
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1580
|
+
- 自动修复:不支持或上游未声明
|
|
1581
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/web-api-no-leaked-event-listener)
|
|
1582
|
+
- 常见报告:`An 'addEventListener' in '{{effectMethodKind}}' should have a corresponding 'removeEventListener' in its cleanup function.`;`A/an '{{eventMethodKind}}' should not have an inline listener function.`
|
|
1583
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1584
|
+
|
|
1585
|
+
错误示例:
|
|
1586
|
+
|
|
1587
|
+
<!-- prettier-ignore -->
|
|
1588
|
+
````tsx
|
|
1589
|
+
useEffect(() => { window.addEventListener('resize', resize); }, []);
|
|
1590
|
+
````
|
|
1591
|
+
|
|
1592
|
+
正确示例:
|
|
1593
|
+
|
|
1594
|
+
<!-- prettier-ignore -->
|
|
1595
|
+
````tsx
|
|
1596
|
+
useEffect(() => { window.addEventListener('resize', resize); return () => window.removeEventListener('resize', resize); }, []);
|
|
1597
|
+
````
|
|
1598
|
+
|
|
1599
|
+
### `@eslint-react/web-api-no-leaked-fetch`
|
|
1600
|
+
|
|
1601
|
+
检查该规则对应的代码约束。上游说明:Enforces that every 'fetch' in a component or custom hook has a corresponding 'AbortController' abort in the cleanup function.
|
|
1602
|
+
|
|
1603
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1604
|
+
- 自动修复:不支持或上游未声明
|
|
1605
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/web-api-no-leaked-fetch)
|
|
1606
|
+
- 常见报告:`A 'fetch' must be provided with an 'AbortController' for proper cleanup.`;`A 'fetch' started in effect must be aborted with 'AbortController.abort' in the cleanup function.`
|
|
1607
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1608
|
+
|
|
1609
|
+
错误示例:
|
|
1610
|
+
|
|
1611
|
+
<!-- prettier-ignore -->
|
|
1612
|
+
````tsx
|
|
1613
|
+
useEffect(() => { fetch(url).then(read); }, [url]);
|
|
1614
|
+
````
|
|
1615
|
+
|
|
1616
|
+
正确示例:
|
|
1617
|
+
|
|
1618
|
+
<!-- prettier-ignore -->
|
|
1619
|
+
````tsx
|
|
1620
|
+
useEffect(() => { const controller = new AbortController(); fetch(url, { signal: controller.signal }).then(read); return () => controller.abort(); }, [url]);
|
|
1621
|
+
````
|
|
1622
|
+
|
|
1623
|
+
### `@eslint-react/web-api-no-leaked-intersection-observer`
|
|
1624
|
+
|
|
1625
|
+
检查该规则对应的代码约束。上游说明:Enforces that every 'IntersectionObserver' created in a component or custom hook has a corresponding 'IntersectionObserver.disconnect()'.
|
|
1626
|
+
|
|
1627
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1628
|
+
- 自动修复:不支持或上游未声明
|
|
1629
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/web-api-no-leaked-intersection-observer)
|
|
1630
|
+
- 常见报告:`Dynamically added 'IntersectionObserver.observe' should be cleared all at once using 'IntersectionObserver.disconnect' in the cleanup function.`;`An 'IntersectionObserver' instance created in 'useEffect' must be disconnected in the cleanup function.`;`An 'IntersectionObserver' instance created in component or custom hook must be assigned to a variable for proper cleanup.`
|
|
1631
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1632
|
+
|
|
1633
|
+
错误示例:
|
|
1634
|
+
|
|
1635
|
+
<!-- prettier-ignore -->
|
|
1636
|
+
````tsx
|
|
1637
|
+
useEffect(() => { const observer = new IntersectionObserver(update); observer.observe(node); }, [node]);
|
|
1638
|
+
````
|
|
1639
|
+
|
|
1640
|
+
正确示例:
|
|
1641
|
+
|
|
1642
|
+
<!-- prettier-ignore -->
|
|
1643
|
+
````tsx
|
|
1644
|
+
useEffect(() => { const observer = new IntersectionObserver(update); observer.observe(node); return () => observer.disconnect(); }, [node]);
|
|
1645
|
+
````
|
|
1646
|
+
|
|
1647
|
+
### `@eslint-react/web-api-no-leaked-interval`
|
|
1648
|
+
|
|
1649
|
+
检查该规则对应的代码约束。上游说明:Enforces that every 'setInterval' in a component or custom hook has a corresponding 'clearInterval'.
|
|
1650
|
+
|
|
1651
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1652
|
+
- 自动修复:不支持或上游未声明
|
|
1653
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/web-api-no-leaked-interval)
|
|
1654
|
+
- 常见报告:`A 'setInterval' created in '{{ kind }}' must be cleared with 'clearInterval' in the cleanup function.`;`A 'setInterval' must be assigned to a variable for proper cleanup.`
|
|
1655
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1656
|
+
|
|
1657
|
+
错误示例:
|
|
1658
|
+
|
|
1659
|
+
<!-- prettier-ignore -->
|
|
1660
|
+
````tsx
|
|
1661
|
+
useEffect(() => { setInterval(refresh, 1000); }, []);
|
|
1662
|
+
````
|
|
1663
|
+
|
|
1664
|
+
正确示例:
|
|
1665
|
+
|
|
1666
|
+
<!-- prettier-ignore -->
|
|
1667
|
+
````tsx
|
|
1668
|
+
useEffect(() => { const timer = setInterval(refresh, 1000); return () => clearInterval(timer); }, []);
|
|
1669
|
+
````
|
|
1670
|
+
|
|
1671
|
+
### `@eslint-react/web-api-no-leaked-resize-observer`
|
|
1672
|
+
|
|
1673
|
+
检查该规则对应的代码约束。上游说明:Enforces that every 'ResizeObserver' created in a component or custom hook has a corresponding 'ResizeObserver.disconnect()'.
|
|
1674
|
+
|
|
1675
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1676
|
+
- 自动修复:不支持或上游未声明
|
|
1677
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/web-api-no-leaked-resize-observer)
|
|
1678
|
+
- 常见报告:`Dynamically added 'ResizeObserver.observe' should be cleared all at once using 'ResizeObserver.disconnect' in the cleanup function.`;`A 'ResizeObserver' instance created in 'useEffect' must be disconnected in the cleanup function.`;`A 'ResizeObserver' instance created in component or custom hook must be assigned to a variable for proper cleanup.`
|
|
1679
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1680
|
+
|
|
1681
|
+
错误示例:
|
|
1682
|
+
|
|
1683
|
+
<!-- prettier-ignore -->
|
|
1684
|
+
````tsx
|
|
1685
|
+
useEffect(() => { const observer = new ResizeObserver(update); observer.observe(node); }, [node]);
|
|
1686
|
+
````
|
|
1687
|
+
|
|
1688
|
+
正确示例:
|
|
1689
|
+
|
|
1690
|
+
<!-- prettier-ignore -->
|
|
1691
|
+
````tsx
|
|
1692
|
+
useEffect(() => { const observer = new ResizeObserver(update); observer.observe(node); return () => observer.disconnect(); }, [node]);
|
|
1693
|
+
````
|
|
1694
|
+
|
|
1695
|
+
### `@eslint-react/web-api-no-leaked-timeout`
|
|
1696
|
+
|
|
1697
|
+
检查该规则对应的代码约束。上游说明:Enforces that every 'setTimeout' in a component or custom hook has a corresponding 'clearTimeout'.
|
|
1698
|
+
|
|
1699
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1700
|
+
- 自动修复:不支持或上游未声明
|
|
1701
|
+
- 规则来源:[官方文档](https://eslint-react.xyz/docs/rules/web-api-no-leaked-timeout)
|
|
1702
|
+
- 常见报告:`A 'setTimeout' created in '{{ kind }}' must be cleared with 'clearTimeout' in the cleanup function.`;`A 'setTimeout' must be assigned to a variable for proper cleanup.`
|
|
1703
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1704
|
+
|
|
1705
|
+
错误示例:
|
|
1706
|
+
|
|
1707
|
+
<!-- prettier-ignore -->
|
|
1708
|
+
````tsx
|
|
1709
|
+
useEffect(() => { setTimeout(refresh, 1000); }, []);
|
|
1710
|
+
````
|
|
1711
|
+
|
|
1712
|
+
正确示例:
|
|
1713
|
+
|
|
1714
|
+
<!-- prettier-ignore -->
|
|
1715
|
+
````tsx
|
|
1716
|
+
useEffect(() => { const timer = setTimeout(refresh, 1000); return () => clearTimeout(timer); }, []);
|
|
1717
|
+
````
|
|
1718
|
+
|
|
1719
|
+
### `react-hooks/config`
|
|
1720
|
+
|
|
1721
|
+
检查该规则对应的代码约束。上游说明:Validates the compiler configuration options
|
|
1722
|
+
|
|
1723
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1724
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1725
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/config)
|
|
1726
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1727
|
+
|
|
1728
|
+
错误示例:
|
|
1729
|
+
|
|
1730
|
+
<!-- prettier-ignore -->
|
|
1731
|
+
````js
|
|
1732
|
+
export default [{ rules: { "react-hooks/config": ["error", { compilationMode: "invalid" }] } }];
|
|
1733
|
+
````
|
|
1734
|
+
|
|
1735
|
+
正确示例:
|
|
1736
|
+
|
|
1737
|
+
<!-- prettier-ignore -->
|
|
1738
|
+
````js
|
|
1739
|
+
export default [{ rules: { "react-hooks/config": ["error", { compilationMode: "infer" }] } }];
|
|
1740
|
+
````
|
|
1741
|
+
|
|
1742
|
+
### `react-hooks/error-boundaries`
|
|
1743
|
+
|
|
1744
|
+
检查该规则对应的代码约束。上游说明:Validates usage of error boundaries instead of try/catch for errors in child components
|
|
1745
|
+
|
|
1746
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1747
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1748
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/error-boundaries)
|
|
1749
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1750
|
+
|
|
1751
|
+
错误示例:
|
|
1752
|
+
|
|
1753
|
+
<!-- prettier-ignore -->
|
|
1754
|
+
````tsx
|
|
1755
|
+
function Page() { try { return <Profile />; } catch { return <Fallback />; } }
|
|
1756
|
+
````
|
|
1757
|
+
|
|
1758
|
+
正确示例:
|
|
1759
|
+
|
|
1760
|
+
<!-- prettier-ignore -->
|
|
1761
|
+
````tsx
|
|
1762
|
+
function Page() { return <ErrorBoundary fallback={<Fallback />}><Profile /></ErrorBoundary>; }
|
|
1763
|
+
````
|
|
1764
|
+
|
|
1765
|
+
### `react-hooks/exhaustive-deps`
|
|
1766
|
+
|
|
1767
|
+
检查该规则对应的代码约束。上游说明:verifies the list of dependencies for Hooks like useEffect and similar
|
|
1768
|
+
|
|
1769
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1770
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1771
|
+
- 规则来源:[官方文档](https://github.com/facebook/react/issues/14920)
|
|
1772
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1773
|
+
|
|
1774
|
+
错误示例:
|
|
1775
|
+
|
|
1776
|
+
<!-- prettier-ignore -->
|
|
1777
|
+
````tsx
|
|
1778
|
+
function UserCard({ id }: Props) { useEffect(() => load(id), []); return <div />; }
|
|
1779
|
+
````
|
|
1780
|
+
|
|
1781
|
+
正确示例:
|
|
1782
|
+
|
|
1783
|
+
<!-- prettier-ignore -->
|
|
1784
|
+
````tsx
|
|
1785
|
+
function UserCard({ id }: Props) { useEffect(() => load(id), [id]); return <div />; }
|
|
1786
|
+
````
|
|
1787
|
+
|
|
1788
|
+
### `react-hooks/gating`
|
|
1789
|
+
|
|
1790
|
+
检查该规则对应的代码约束。上游说明:Validates configuration of [gating mode](https://react.dev/reference/react-compiler/gating)
|
|
1791
|
+
|
|
1792
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1793
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1794
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/gating)
|
|
1795
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1796
|
+
|
|
1797
|
+
错误示例:
|
|
1798
|
+
|
|
1799
|
+
<!-- prettier-ignore -->
|
|
1800
|
+
````js
|
|
1801
|
+
export default [{ rules: { "react-hooks/gating": ["error", { gating: { source: "" } }] } }];
|
|
1802
|
+
````
|
|
1803
|
+
|
|
1804
|
+
正确示例:
|
|
1805
|
+
|
|
1806
|
+
<!-- prettier-ignore -->
|
|
1807
|
+
````js
|
|
1808
|
+
export default [{ rules: { "react-hooks/gating": ["error", { gating: { source: "featureFlags", importSpecifierName: "isCompilerEnabled" } }] } }];
|
|
1809
|
+
````
|
|
1810
|
+
|
|
1811
|
+
### `react-hooks/globals`
|
|
1812
|
+
|
|
1813
|
+
检查该规则对应的代码约束。上游说明:Validates against assignment/mutation of globals during render, part of ensuring that [side effects must render outside of render](https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render)
|
|
1814
|
+
|
|
1815
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1816
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1817
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/globals)
|
|
1818
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1819
|
+
|
|
1820
|
+
错误示例:
|
|
1821
|
+
|
|
1822
|
+
<!-- prettier-ignore -->
|
|
1823
|
+
````tsx
|
|
1824
|
+
let currentUser = null;
|
|
1825
|
+
function User({ user }: Props) { currentUser = user; return <p>{user.name}</p>; }
|
|
1826
|
+
````
|
|
1827
|
+
|
|
1828
|
+
正确示例:
|
|
1829
|
+
|
|
1830
|
+
<!-- prettier-ignore -->
|
|
1831
|
+
````tsx
|
|
1832
|
+
function User({ user }: Props) { return <p>{user.name}</p>; }
|
|
1833
|
+
````
|
|
1834
|
+
|
|
1835
|
+
### `react-hooks/immutability`
|
|
1836
|
+
|
|
1837
|
+
检查该规则对应的代码约束。上游说明:Validates against mutating props, state, and other values that [are immutable](https://react.dev/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable)
|
|
1838
|
+
|
|
1839
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1840
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1841
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/immutability)
|
|
1842
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1843
|
+
|
|
1844
|
+
错误示例:
|
|
1845
|
+
|
|
1846
|
+
<!-- prettier-ignore -->
|
|
1847
|
+
````tsx
|
|
1848
|
+
function User({ user }: Props) { user.name = 'Fast'; return <p>{user.name}</p>; }
|
|
1849
|
+
````
|
|
1850
|
+
|
|
1851
|
+
正确示例:
|
|
1852
|
+
|
|
1853
|
+
<!-- prettier-ignore -->
|
|
1854
|
+
````tsx
|
|
1855
|
+
function User({ user }: Props) { const nextUser = { ...user, name: 'Fast' }; return <p>{nextUser.name}</p>; }
|
|
1856
|
+
````
|
|
1857
|
+
|
|
1858
|
+
### `react-hooks/incompatible-library`
|
|
1859
|
+
|
|
1860
|
+
检查该规则对应的代码约束。上游说明:Validates against usage of libraries which are incompatible with memoization (manual or automatic)
|
|
1861
|
+
|
|
1862
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
1863
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1864
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/incompatible-library)
|
|
1865
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1866
|
+
|
|
1867
|
+
错误示例:
|
|
1868
|
+
|
|
1869
|
+
<!-- prettier-ignore -->
|
|
1870
|
+
````tsx
|
|
1871
|
+
import { useForm } from 'react-hook-form';
|
|
1872
|
+
function Form() { const form = useForm(); const name = form.watch('name'); return <p>{name}</p>; }
|
|
1873
|
+
````
|
|
1874
|
+
|
|
1875
|
+
正确示例:
|
|
1876
|
+
|
|
1877
|
+
<!-- prettier-ignore -->
|
|
1878
|
+
````tsx
|
|
1879
|
+
import { useWatch } from 'react-hook-form';
|
|
1880
|
+
function Form() { const name = useWatch({ name: 'name' }); return <p>{name}</p>; }
|
|
1881
|
+
````
|
|
1882
|
+
|
|
1883
|
+
### `react-hooks/preserve-manual-memoization`
|
|
1884
|
+
|
|
1885
|
+
检查该规则对应的代码约束。上游说明:Validates that existing manual memoized is preserved by the compiler. React Compiler will only compile components and hooks if its inference [matches or exceeds the existing manual memoization](https://react.dev/learn/react-compiler/introduction#what-should-i-do-about-usememo-usecallback-and-reactmemo)
|
|
1886
|
+
|
|
1887
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1888
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1889
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/preserve-manual-memoization)
|
|
1890
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1891
|
+
|
|
1892
|
+
错误示例:
|
|
1893
|
+
|
|
1894
|
+
<!-- prettier-ignore -->
|
|
1895
|
+
````tsx
|
|
1896
|
+
import { useCallback } from 'react';
|
|
1897
|
+
function User({ user }: Props) { const open = useCallback(() => { if (user?.id) console.log(user.id); }, [user?.id]); return <button onClick={open}>Open</button>; }
|
|
1898
|
+
````
|
|
1899
|
+
|
|
1900
|
+
正确示例:
|
|
1901
|
+
|
|
1902
|
+
<!-- prettier-ignore -->
|
|
1903
|
+
````tsx
|
|
1904
|
+
import { useCallback } from 'react';
|
|
1905
|
+
function User({ user }: Props) { const id = user?.id; const open = useCallback(() => { if (id) console.log(id); }, [id]); return <button onClick={open}>Open</button>; }
|
|
1906
|
+
````
|
|
1907
|
+
|
|
1908
|
+
### `react-hooks/purity`
|
|
1909
|
+
|
|
1910
|
+
检查该规则对应的代码约束。上游说明:Validates that [components/hooks are pure](https://react.dev/reference/rules/components-and-hooks-must-be-pure) by checking that they do not call known-impure functions
|
|
1911
|
+
|
|
1912
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1913
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1914
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/purity)
|
|
1915
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1916
|
+
|
|
1917
|
+
错误示例:
|
|
1918
|
+
|
|
1919
|
+
<!-- prettier-ignore -->
|
|
1920
|
+
````tsx
|
|
1921
|
+
function Token() { const value = Math.random(); return <span>{value}</span>; }
|
|
1922
|
+
````
|
|
1923
|
+
|
|
1924
|
+
正确示例:
|
|
1925
|
+
|
|
1926
|
+
<!-- prettier-ignore -->
|
|
1927
|
+
````tsx
|
|
1928
|
+
function Token() { const [value] = useState(() => Math.random()); return <span>{value}</span>; }
|
|
1929
|
+
````
|
|
1930
|
+
|
|
1931
|
+
### `react-hooks/refs`
|
|
1932
|
+
|
|
1933
|
+
检查该规则对应的代码约束。上游说明:Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in [`useRef()` usage](https://react.dev/reference/react/useRef#usage)
|
|
1934
|
+
|
|
1935
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1936
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1937
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/refs)
|
|
1938
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1939
|
+
|
|
1940
|
+
错误示例:
|
|
1941
|
+
|
|
1942
|
+
<!-- prettier-ignore -->
|
|
1943
|
+
````tsx
|
|
1944
|
+
function Input() { const inputRef = useRef<HTMLInputElement>(null); return <span>{inputRef.current?.value}</span>; }
|
|
1945
|
+
````
|
|
1946
|
+
|
|
1947
|
+
正确示例:
|
|
1948
|
+
|
|
1949
|
+
<!-- prettier-ignore -->
|
|
1950
|
+
````tsx
|
|
1951
|
+
function Input() { const inputRef = useRef<HTMLInputElement>(null); return <input ref={inputRef} />; }
|
|
1952
|
+
````
|
|
1953
|
+
|
|
1954
|
+
### `react-hooks/rules-of-hooks`
|
|
1955
|
+
|
|
1956
|
+
检查该规则对应的代码约束。上游说明:enforces the Rules of Hooks
|
|
1957
|
+
|
|
1958
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1959
|
+
- 自动修复:不支持或上游未声明
|
|
1960
|
+
- 规则来源:[官方文档](https://react.dev/reference/rules/rules-of-hooks)
|
|
1961
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1962
|
+
|
|
1963
|
+
错误示例:
|
|
1964
|
+
|
|
1965
|
+
<!-- prettier-ignore -->
|
|
1966
|
+
````tsx
|
|
1967
|
+
function UserCard({ ready }: Props) { if (ready) { useEffect(load, []); } return <div />; }
|
|
1968
|
+
````
|
|
1969
|
+
|
|
1970
|
+
正确示例:
|
|
1971
|
+
|
|
1972
|
+
<!-- prettier-ignore -->
|
|
1973
|
+
````tsx
|
|
1974
|
+
function UserCard({ ready }: Props) { useEffect(() => { if (ready) load(); }, [ready]); return <div />; }
|
|
1975
|
+
````
|
|
1976
|
+
|
|
1977
|
+
### `react-hooks/set-state-in-effect`
|
|
1978
|
+
|
|
1979
|
+
检查该规则对应的代码约束。上游说明:Validates against calling setState synchronously in an effect. This can indicate non-local derived data, a derived event pattern, or improper external data synchronization.
|
|
1980
|
+
|
|
1981
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
1982
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
1983
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/set-state-in-effect)
|
|
1984
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
1985
|
+
|
|
1986
|
+
错误示例:
|
|
1987
|
+
|
|
1988
|
+
<!-- prettier-ignore -->
|
|
1989
|
+
````tsx
|
|
1990
|
+
function User({ name }: Props) { const [label, setLabel] = useState(''); useEffect(() => { setLabel(name.trim()); }, [name]); return <p>{label}</p>; }
|
|
1991
|
+
````
|
|
1992
|
+
|
|
1993
|
+
正确示例:
|
|
1994
|
+
|
|
1995
|
+
<!-- prettier-ignore -->
|
|
1996
|
+
````tsx
|
|
1997
|
+
function User({ name }: Props) { const label = name.trim(); return <p>{label}</p>; }
|
|
1998
|
+
````
|
|
1999
|
+
|
|
2000
|
+
### `react-hooks/set-state-in-render`
|
|
2001
|
+
|
|
2002
|
+
检查该规则对应的代码约束。上游说明:Validates against setting state during render, which can trigger additional renders and potential infinite render loops
|
|
2003
|
+
|
|
2004
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
2005
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
2006
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/set-state-in-render)
|
|
2007
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
2008
|
+
|
|
2009
|
+
错误示例:
|
|
2010
|
+
|
|
2011
|
+
<!-- prettier-ignore -->
|
|
2012
|
+
````tsx
|
|
2013
|
+
function Counter() { const [count, setCount] = useState(0); setCount(count + 1); return <span>{count}</span>; }
|
|
2014
|
+
````
|
|
2015
|
+
|
|
2016
|
+
正确示例:
|
|
2017
|
+
|
|
2018
|
+
<!-- prettier-ignore -->
|
|
2019
|
+
````tsx
|
|
2020
|
+
function Counter() { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>{count}</button>; }
|
|
2021
|
+
````
|
|
2022
|
+
|
|
2023
|
+
### `react-hooks/static-components`
|
|
2024
|
+
|
|
2025
|
+
检查该规则对应的代码约束。上游说明:Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering
|
|
2026
|
+
|
|
2027
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
2028
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
2029
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/static-components)
|
|
2030
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
2031
|
+
|
|
2032
|
+
错误示例:
|
|
2033
|
+
|
|
2034
|
+
<!-- prettier-ignore -->
|
|
2035
|
+
````tsx
|
|
2036
|
+
function Page() { function Header() { return <h1>Title</h1>; } return <Header />; }
|
|
2037
|
+
````
|
|
2038
|
+
|
|
2039
|
+
正确示例:
|
|
2040
|
+
|
|
2041
|
+
<!-- prettier-ignore -->
|
|
2042
|
+
````tsx
|
|
2043
|
+
function Header() { return <h1>Title</h1>; }
|
|
2044
|
+
function Page() { return <Header />; }
|
|
2045
|
+
````
|
|
2046
|
+
|
|
2047
|
+
### `react-hooks/unsupported-syntax`
|
|
2048
|
+
|
|
2049
|
+
检查该规则对应的代码约束。上游说明:Validates against syntax that we do not plan to support in React Compiler
|
|
2050
|
+
|
|
2051
|
+
- 生效级别与范围:warn: React JSX、React TSX
|
|
2052
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
2053
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/unsupported-syntax)
|
|
2054
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
2055
|
+
|
|
2056
|
+
错误示例:
|
|
2057
|
+
|
|
2058
|
+
<!-- prettier-ignore -->
|
|
2059
|
+
````tsx
|
|
2060
|
+
function Page({ source }: Props) { return <div>{eval(source)}</div>; }
|
|
2061
|
+
````
|
|
2062
|
+
|
|
2063
|
+
正确示例:
|
|
2064
|
+
|
|
2065
|
+
<!-- prettier-ignore -->
|
|
2066
|
+
````tsx
|
|
2067
|
+
function Page({ source }: Props) { return <div>{JSON.parse(source)}</div>; }
|
|
2068
|
+
````
|
|
2069
|
+
|
|
2070
|
+
### `react-hooks/use-memo`
|
|
2071
|
+
|
|
2072
|
+
检查该规则对应的代码约束。上游说明:Validates usage of the useMemo() hook against common mistakes. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.
|
|
2073
|
+
|
|
2074
|
+
- 生效级别与范围:error: React JSX、React TSX
|
|
2075
|
+
- 自动修复:支持;执行前仍应检查语义和差异
|
|
2076
|
+
- 规则来源:[官方文档](https://react.dev/reference/eslint-plugin-react-hooks/lints/use-memo)
|
|
2077
|
+
- 示例类型:第三方预置规则的直接代码示例
|
|
2078
|
+
|
|
2079
|
+
错误示例:
|
|
2080
|
+
|
|
2081
|
+
<!-- prettier-ignore -->
|
|
2082
|
+
````tsx
|
|
2083
|
+
function User({ name }: Props) { const label = useMemo(async () => name.trim(), [name]); return <p>{label}</p>; }
|
|
2084
|
+
````
|
|
2085
|
+
|
|
2086
|
+
正确示例:
|
|
2087
|
+
|
|
2088
|
+
<!-- prettier-ignore -->
|
|
2089
|
+
````tsx
|
|
2090
|
+
function User({ name }: Props) { const label = useMemo(() => name.trim(), [name]); return <p>{label}</p>; }
|
|
2091
|
+
````
|