@codelaw/rules-react 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +23 -0
- package/NOTICE +10 -0
- package/README.md +46 -2
- package/codelaw-pack.toml +6 -0
- package/data/react/dom-property-aliases.json +131 -0
- package/package.json +14 -4
- package/presets/react/conventions.toml +5 -0
- package/presets/react/recommended.toml +22 -0
- package/rules/react/display-name.md +91 -0
- package/rules/react/exhaustive-deps.md +116 -0
- package/rules/react/hook-prefix.md +108 -0
- package/rules/react/jsx-key.md +99 -0
- package/rules/react/jsx-no-comment-textnodes.md +87 -0
- package/rules/react/jsx-no-duplicate-props.md +66 -0
- package/rules/react/jsx-no-target-blank.md +78 -0
- package/rules/react/jsx-no-undef.md +78 -0
- package/rules/react/no-children-prop.md +80 -0
- package/rules/react/no-danger-with-children.md +83 -0
- package/rules/react/no-deprecated.md +84 -0
- package/rules/react/no-direct-mutation-state.md +102 -0
- package/rules/react/no-find-dom-node.md +72 -0
- package/rules/react/no-is-mounted.md +109 -0
- package/rules/react/no-render-return-value.md +75 -0
- package/rules/react/no-string-refs.md +72 -0
- package/rules/react/no-unescaped-entities.md +69 -0
- package/rules/react/no-unknown-property.md +69 -0
- package/rules/react/props-type-name.md +43 -0
- package/rules/react/require-render-return.md +91 -0
- package/rules/react/rules-of-hooks.md +128 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arpit Arya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
SPDX-License-Identifier: MIT
|
package/NOTICE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
@codelaw/rules-react / codelaw-rules-react
|
|
2
|
+
|
|
3
|
+
data/react/dom-property-aliases.json was written for this pack: HTML attribute
|
|
4
|
+
names and the React DOM prop names that replace them (`class` -> `className`),
|
|
5
|
+
as React documents them. No React code is included.
|
|
6
|
+
|
|
7
|
+
The rules are reimplementations written for codelaw. Their `ported_from:`
|
|
8
|
+
fields name the eslint-plugin-react and eslint-plugin-react-hooks rules (MIT
|
|
9
|
+
License) whose documented behaviour they reproduce; no code or documentation
|
|
10
|
+
text from either is included.
|
package/README.md
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @codelaw/rules-react · codelaw-rules-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
codelaw's React rules — one of codelaw's first-party rule packs
|
|
4
|
+
([SR-RULE-PACKS](../../records/0101_rule-packs.md)). The
|
|
5
|
+
same files ship on npm as `@codelaw/rules-react` and on PyPI as `codelaw-rules-react`.
|
|
6
|
+
|
|
7
|
+
```toml
|
|
8
|
+
# codelaw.toml
|
|
9
|
+
extends = ["codelaw:js/recommended", "codelaw:react/recommended"]
|
|
10
|
+
preview = true # every rule is `preview` until a corpus run promotes it
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Presets
|
|
14
|
+
|
|
15
|
+
| preset | rules | ported from |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| `react/recommended` | 19 | eslint-plugin-react 7.37.5 `recommended` + `jsx-runtime` (17 of its 20 active rules), and eslint-plugin-react-hooks 7.1.1's `rules-of-hooks` and `exhaustive-deps` |
|
|
18
|
+
| `react/conventions` | 2 | codelaw's starter catalogue: `react/hook-prefix`, `react/props-type-name` — opt in |
|
|
19
|
+
|
|
20
|
+
Every rule is **reimplemented**, not vendored: the matchers, messages and docs
|
|
21
|
+
are written fresh, and each rule's `ported_from:` names the source rule.
|
|
22
|
+
|
|
23
|
+
## How the ports differ
|
|
24
|
+
|
|
25
|
+
Every rule matches its source's documented behaviour on the source's own
|
|
26
|
+
examples. Where codelaw differs, the rule's `## Differences from the source`
|
|
27
|
+
section says so. The larger ones:
|
|
28
|
+
|
|
29
|
+
| rule | difference |
|
|
30
|
+
|---|---|
|
|
31
|
+
| `react/jsx-key` | elements returned from a `.map()` callback's block body are not checked yet |
|
|
32
|
+
| `react/no-unknown-property` | renames HTML spellings (`class`, `onclick`, `tabindex`); in `.js`/`.jsx` the JavaScript grammar can't parse `class=` and `for=` |
|
|
33
|
+
| `react/exhaustive-deps` | dependencies compared by root name; setters, dispatchers and refs are stable |
|
|
34
|
+
| `react/rules-of-hooks` | an early `return` anywhere before a hook counts as making it conditional |
|
|
35
|
+
|
|
36
|
+
## Not ported, and why
|
|
37
|
+
|
|
38
|
+
| rule | why |
|
|
39
|
+
|---|---|
|
|
40
|
+
| `react/prop-types` | needs the component's declared prop types matched to its uses; TypeScript types do this job |
|
|
41
|
+
| `react/jsx-uses-vars` | marks JSX names as used for `no-unused-vars`; codelaw's semantic model already counts them |
|
|
42
|
+
| `react/react-in-jsx-scope`, `react/jsx-uses-react` | turned off by `jsx-runtime` |
|
|
43
|
+
| eslint-plugin-react-hooks' 14 React Compiler rules | need the React Compiler's analysis |
|
|
44
|
+
|
|
45
|
+
## Data
|
|
46
|
+
|
|
47
|
+
`data/react/dom-property-aliases.json` — HTML attribute names and the React prop names that replace them, written for this pack.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
name = "@codelaw/rules-react"
|
|
2
|
+
version = "0.1.0"
|
|
3
|
+
min_codelaw = "0.1.0"
|
|
4
|
+
languages = ["javascript", "jsx", "typescript", "tsx"]
|
|
5
|
+
presets = ["react/recommended", "react/conventions"]
|
|
6
|
+
description = "codelaw's React rules, from eslint-plugin-react and eslint-plugin-react-hooks (SR-RULE-PACKS)"
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
{
|
|
2
|
+
"accept-charset": "acceptCharset",
|
|
3
|
+
"accesskey": "accessKey",
|
|
4
|
+
"autocapitalize": "autoCapitalize",
|
|
5
|
+
"autocomplete": "autoComplete",
|
|
6
|
+
"autofocus": "autoFocus",
|
|
7
|
+
"autoplay": "autoPlay",
|
|
8
|
+
"cellpadding": "cellPadding",
|
|
9
|
+
"cellspacing": "cellSpacing",
|
|
10
|
+
"charset": "charSet",
|
|
11
|
+
"class": "className",
|
|
12
|
+
"colspan": "colSpan",
|
|
13
|
+
"contenteditable": "contentEditable",
|
|
14
|
+
"crossorigin": "crossOrigin",
|
|
15
|
+
"datetime": "dateTime",
|
|
16
|
+
"enctype": "encType",
|
|
17
|
+
"enterkeyhint": "enterKeyHint",
|
|
18
|
+
"fetchpriority": "fetchPriority",
|
|
19
|
+
"for": "htmlFor",
|
|
20
|
+
"formaction": "formAction",
|
|
21
|
+
"formenctype": "formEncType",
|
|
22
|
+
"formmethod": "formMethod",
|
|
23
|
+
"formnovalidate": "formNoValidate",
|
|
24
|
+
"formtarget": "formTarget",
|
|
25
|
+
"frameborder": "frameBorder",
|
|
26
|
+
"hreflang": "hrefLang",
|
|
27
|
+
"http-equiv": "httpEquiv",
|
|
28
|
+
"inputmode": "inputMode",
|
|
29
|
+
"itemprop": "itemProp",
|
|
30
|
+
"itemscope": "itemScope",
|
|
31
|
+
"itemtype": "itemType",
|
|
32
|
+
"marginheight": "marginHeight",
|
|
33
|
+
"marginwidth": "marginWidth",
|
|
34
|
+
"maxlength": "maxLength",
|
|
35
|
+
"minlength": "minLength",
|
|
36
|
+
"nomodule": "noModule",
|
|
37
|
+
"novalidate": "noValidate",
|
|
38
|
+
"onabort": "onAbort",
|
|
39
|
+
"onanimationend": "onAnimationEnd",
|
|
40
|
+
"onanimationiteration": "onAnimationIteration",
|
|
41
|
+
"onanimationstart": "onAnimationStart",
|
|
42
|
+
"onauxclick": "onAuxClick",
|
|
43
|
+
"onbeforeinput": "onBeforeInput",
|
|
44
|
+
"onblur": "onBlur",
|
|
45
|
+
"oncancel": "onCancel",
|
|
46
|
+
"oncanplay": "onCanPlay",
|
|
47
|
+
"oncanplaythrough": "onCanPlayThrough",
|
|
48
|
+
"onchange": "onChange",
|
|
49
|
+
"onclick": "onClick",
|
|
50
|
+
"onclose": "onClose",
|
|
51
|
+
"oncompositionend": "onCompositionEnd",
|
|
52
|
+
"oncompositionstart": "onCompositionStart",
|
|
53
|
+
"oncompositionupdate": "onCompositionUpdate",
|
|
54
|
+
"oncontextmenu": "onContextMenu",
|
|
55
|
+
"oncopy": "onCopy",
|
|
56
|
+
"oncut": "onCut",
|
|
57
|
+
"ondblclick": "onDoubleClick",
|
|
58
|
+
"ondrag": "onDrag",
|
|
59
|
+
"ondragend": "onDragEnd",
|
|
60
|
+
"ondragenter": "onDragEnter",
|
|
61
|
+
"ondragleave": "onDragLeave",
|
|
62
|
+
"ondragover": "onDragOver",
|
|
63
|
+
"ondragstart": "onDragStart",
|
|
64
|
+
"ondrop": "onDrop",
|
|
65
|
+
"ondurationchange": "onDurationChange",
|
|
66
|
+
"onemptied": "onEmptied",
|
|
67
|
+
"onended": "onEnded",
|
|
68
|
+
"onerror": "onError",
|
|
69
|
+
"onfocus": "onFocus",
|
|
70
|
+
"ongotpointercapture": "onGotPointerCapture",
|
|
71
|
+
"oninput": "onInput",
|
|
72
|
+
"oninvalid": "onInvalid",
|
|
73
|
+
"onkeydown": "onKeyDown",
|
|
74
|
+
"onkeypress": "onKeyPress",
|
|
75
|
+
"onkeyup": "onKeyUp",
|
|
76
|
+
"onload": "onLoad",
|
|
77
|
+
"onloadeddata": "onLoadedData",
|
|
78
|
+
"onloadedmetadata": "onLoadedMetadata",
|
|
79
|
+
"onloadstart": "onLoadStart",
|
|
80
|
+
"onlostpointercapture": "onLostPointerCapture",
|
|
81
|
+
"onmousedown": "onMouseDown",
|
|
82
|
+
"onmouseenter": "onMouseEnter",
|
|
83
|
+
"onmouseleave": "onMouseLeave",
|
|
84
|
+
"onmousemove": "onMouseMove",
|
|
85
|
+
"onmouseout": "onMouseOut",
|
|
86
|
+
"onmouseover": "onMouseOver",
|
|
87
|
+
"onmouseup": "onMouseUp",
|
|
88
|
+
"onpaste": "onPaste",
|
|
89
|
+
"onpause": "onPause",
|
|
90
|
+
"onplay": "onPlay",
|
|
91
|
+
"onplaying": "onPlaying",
|
|
92
|
+
"onpointercancel": "onPointerCancel",
|
|
93
|
+
"onpointerdown": "onPointerDown",
|
|
94
|
+
"onpointerenter": "onPointerEnter",
|
|
95
|
+
"onpointerleave": "onPointerLeave",
|
|
96
|
+
"onpointermove": "onPointerMove",
|
|
97
|
+
"onpointerout": "onPointerOut",
|
|
98
|
+
"onpointerover": "onPointerOver",
|
|
99
|
+
"onpointerup": "onPointerUp",
|
|
100
|
+
"onprogress": "onProgress",
|
|
101
|
+
"onratechange": "onRateChange",
|
|
102
|
+
"onreset": "onReset",
|
|
103
|
+
"onresize": "onResize",
|
|
104
|
+
"onscroll": "onScroll",
|
|
105
|
+
"onseeked": "onSeeked",
|
|
106
|
+
"onseeking": "onSeeking",
|
|
107
|
+
"onselect": "onSelect",
|
|
108
|
+
"onstalled": "onStalled",
|
|
109
|
+
"onsubmit": "onSubmit",
|
|
110
|
+
"onsuspend": "onSuspend",
|
|
111
|
+
"ontimeupdate": "onTimeUpdate",
|
|
112
|
+
"ontoggle": "onToggle",
|
|
113
|
+
"ontouchcancel": "onTouchCancel",
|
|
114
|
+
"ontouchend": "onTouchEnd",
|
|
115
|
+
"ontouchmove": "onTouchMove",
|
|
116
|
+
"ontouchstart": "onTouchStart",
|
|
117
|
+
"ontransitionend": "onTransitionEnd",
|
|
118
|
+
"onvolumechange": "onVolumeChange",
|
|
119
|
+
"onwaiting": "onWaiting",
|
|
120
|
+
"onwheel": "onWheel",
|
|
121
|
+
"playsinline": "playsInline",
|
|
122
|
+
"readonly": "readOnly",
|
|
123
|
+
"referrerpolicy": "referrerPolicy",
|
|
124
|
+
"rowspan": "rowSpan",
|
|
125
|
+
"spellcheck": "spellCheck",
|
|
126
|
+
"srcdoc": "srcDoc",
|
|
127
|
+
"srclang": "srcLang",
|
|
128
|
+
"srcset": "srcSet",
|
|
129
|
+
"tabindex": "tabIndex",
|
|
130
|
+
"usemap": "useMap"
|
|
131
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codelaw/rules-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "codelaw's React rules, from eslint-plugin-react and eslint-plugin-react-hooks (SR-RULE-PACKS)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/arpitarya/codelaw.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/arpitarya/codelaw",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"codelaw",
|
|
13
|
+
"codelaw-pack",
|
|
14
|
+
"lint-rules"
|
|
15
|
+
]
|
|
16
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
description = "eslint-plugin-react 7.37.5 recommended with jsx-runtime, and eslint-plugin-react-hooks 7.1.1 rules-of-hooks and exhaustive-deps, reimplemented"
|
|
2
|
+
|
|
3
|
+
[rules]
|
|
4
|
+
"react/display-name" = "error"
|
|
5
|
+
"react/exhaustive-deps" = "error"
|
|
6
|
+
"react/jsx-key" = "error"
|
|
7
|
+
"react/jsx-no-comment-textnodes" = "error"
|
|
8
|
+
"react/jsx-no-duplicate-props" = "error"
|
|
9
|
+
"react/jsx-no-target-blank" = "error"
|
|
10
|
+
"react/jsx-no-undef" = "error"
|
|
11
|
+
"react/no-children-prop" = "error"
|
|
12
|
+
"react/no-danger-with-children" = "error"
|
|
13
|
+
"react/no-deprecated" = "error"
|
|
14
|
+
"react/no-direct-mutation-state" = "error"
|
|
15
|
+
"react/no-find-dom-node" = "error"
|
|
16
|
+
"react/no-is-mounted" = "error"
|
|
17
|
+
"react/no-render-return-value" = "error"
|
|
18
|
+
"react/no-string-refs" = "error"
|
|
19
|
+
"react/no-unescaped-entities" = "error"
|
|
20
|
+
"react/no-unknown-property" = "error"
|
|
21
|
+
"react/require-render-return" = "error"
|
|
22
|
+
"react/rules-of-hooks" = "error"
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: react/display-name
|
|
3
|
+
title: "A component passed to memo or forwardRef has a name"
|
|
4
|
+
languages: [javascript, jsx, tsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "the name is the author's choice"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint-plugin-react/display-name]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
A component wrapped in `memo` or `forwardRef` is a named function, so it has a display name.
|
|
13
|
+
|
|
14
|
+
**Why:** An anonymous component shows up as `Anonymous` in React DevTools and in error messages,
|
|
15
|
+
which makes both harder to read.
|
|
16
|
+
|
|
17
|
+
```codelaw
|
|
18
|
+
match:
|
|
19
|
+
any:
|
|
20
|
+
- pattern: memo($F)
|
|
21
|
+
- pattern: React.memo($F)
|
|
22
|
+
- pattern: forwardRef($F)
|
|
23
|
+
- pattern: React.forwardRef($F)
|
|
24
|
+
constraints:
|
|
25
|
+
F:
|
|
26
|
+
any:
|
|
27
|
+
- kind: arrow_function
|
|
28
|
+
- kind: function_expression
|
|
29
|
+
not: { has: { field: name, pattern: $NAME } }
|
|
30
|
+
message: "This component has no name, so React DevTools and error messages show it as `Anonymous`"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## How to fix
|
|
34
|
+
|
|
35
|
+
1. Pass a named function: `memo(function Card(props) { … })`.
|
|
36
|
+
2. Or set `Card.displayName = "Card"`.
|
|
37
|
+
|
|
38
|
+
## Examples
|
|
39
|
+
|
|
40
|
+
### JavaScript
|
|
41
|
+
|
|
42
|
+
#### Don't
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
export const Card = memo((props) => <div>{props.title}</div>);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### Do
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
export const Card = memo(function Card(props) {
|
|
52
|
+
return <div>{props.title}</div>;
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### JSX
|
|
57
|
+
|
|
58
|
+
#### Don't
|
|
59
|
+
|
|
60
|
+
```jsx
|
|
61
|
+
export const Card = memo((props) => <div>{props.title}</div>);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
#### Do
|
|
65
|
+
|
|
66
|
+
```jsx
|
|
67
|
+
export const Card = memo(function Card(props) {
|
|
68
|
+
return <div>{props.title}</div>;
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### TSX
|
|
73
|
+
|
|
74
|
+
#### Don't
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
export const Card = memo((props) => <div>{props.title}</div>);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### Do
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
export const Card = memo(function Card(props) {
|
|
84
|
+
return <div>{props.title}</div>;
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Differences from the source
|
|
89
|
+
|
|
90
|
+
Components defined some other way (`createReactClass`, anonymous default exports) are not checked
|
|
91
|
+
yet.
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: react/exhaustive-deps
|
|
3
|
+
title: "Effect and memo dependency lists name every value they read"
|
|
4
|
+
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
+
fix: suggest
|
|
6
|
+
fix_reason: "adding a dependency can make an effect run more often"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint-plugin-react-hooks/exhaustive-deps]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
The dependency list of `useEffect`, `useLayoutEffect`, `useCallback`, `useMemo` and
|
|
13
|
+
`useImperativeHandle` names every prop, state value and local the callback reads, and nothing from
|
|
14
|
+
outside the component.
|
|
15
|
+
|
|
16
|
+
**Why:** A missing dependency makes the hook keep a stale value; an outside value in the list does
|
|
17
|
+
nothing.
|
|
18
|
+
|
|
19
|
+
```codelaw
|
|
20
|
+
native: react/exhaustive-deps
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Examples
|
|
24
|
+
|
|
25
|
+
### JavaScript
|
|
26
|
+
|
|
27
|
+
#### Don't
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
function Search({ query }) {
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
fetchResults(query);
|
|
33
|
+
}, []);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
#### Do
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
function Search({ query }) {
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
fetchResults(query);
|
|
43
|
+
}, [query]);
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### JSX
|
|
48
|
+
|
|
49
|
+
#### Don't
|
|
50
|
+
|
|
51
|
+
```jsx
|
|
52
|
+
function Search({ query }) {
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
fetchResults(query);
|
|
55
|
+
}, []);
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### Do
|
|
60
|
+
|
|
61
|
+
```jsx
|
|
62
|
+
function Search({ query }) {
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
fetchResults(query);
|
|
65
|
+
}, [query]);
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### TypeScript
|
|
70
|
+
|
|
71
|
+
#### Don't
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
function Search({ query }) {
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
fetchResults(query);
|
|
77
|
+
}, []);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### Do
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
function Search({ query }) {
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
fetchResults(query);
|
|
87
|
+
}, [query]);
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### TSX
|
|
92
|
+
|
|
93
|
+
#### Don't
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
function Search({ query }) {
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
fetchResults(query);
|
|
99
|
+
}, []);
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### Do
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
function Search({ query }) {
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
fetchResults(query);
|
|
109
|
+
}, [query]);
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Differences from the source
|
|
114
|
+
|
|
115
|
+
State setters, reducer dispatchers and refs are treated as stable, as upstream does. Dependencies
|
|
116
|
+
are compared by their root name (`props` covers `props.id`).
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: react/hook-prefix
|
|
3
|
+
title: "Functions that call hooks are named use…"
|
|
4
|
+
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "every caller of the function must be renamed too"
|
|
7
|
+
stability: preview
|
|
8
|
+
since: 0.1.0
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
A function that isn't a component (PascalCase) and calls a hook is a custom hook, and is named
|
|
12
|
+
`use…`.
|
|
13
|
+
|
|
14
|
+
**Why:** React and its tools recognise hooks by the `use` prefix; without it the Rules of Hooks
|
|
15
|
+
can't be checked for the function's callers.
|
|
16
|
+
|
|
17
|
+
```codelaw
|
|
18
|
+
match:
|
|
19
|
+
kind: function_declaration
|
|
20
|
+
all:
|
|
21
|
+
- has: { field: name, regex: '^[a-z]', not: { regex: '^use[A-Z]' }, pattern: $NAME }
|
|
22
|
+
- has:
|
|
23
|
+
field: body
|
|
24
|
+
has:
|
|
25
|
+
stopBy: { any: [{ kind: function_declaration }, { kind: arrow_function }, { kind: function_expression }] }
|
|
26
|
+
kind: call_expression
|
|
27
|
+
has: { field: function, regex: '^(React\.)?use[A-Z]' }
|
|
28
|
+
message: "`$NAME` calls hooks, so it is a hook: name it `use…`"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## How to fix
|
|
32
|
+
|
|
33
|
+
1. Rename the function to start with `use` (e.g. `loadUser` → `useUser`).
|
|
34
|
+
2. Rename its callers.
|
|
35
|
+
|
|
36
|
+
## Examples
|
|
37
|
+
|
|
38
|
+
### JavaScript
|
|
39
|
+
|
|
40
|
+
#### Don't
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
function currentUser() {
|
|
44
|
+
return useContext(UserContext);
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### Do
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
function useCurrentUser() {
|
|
52
|
+
return useContext(UserContext);
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### JSX
|
|
57
|
+
|
|
58
|
+
#### Don't
|
|
59
|
+
|
|
60
|
+
```jsx
|
|
61
|
+
function currentUser() {
|
|
62
|
+
return useContext(UserContext);
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
#### Do
|
|
67
|
+
|
|
68
|
+
```jsx
|
|
69
|
+
function useCurrentUser() {
|
|
70
|
+
return useContext(UserContext);
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### TypeScript
|
|
75
|
+
|
|
76
|
+
#### Don't
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
function currentUser() {
|
|
80
|
+
return useContext(UserContext);
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### Do
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
function useCurrentUser() {
|
|
88
|
+
return useContext(UserContext);
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### TSX
|
|
93
|
+
|
|
94
|
+
#### Don't
|
|
95
|
+
|
|
96
|
+
```tsx
|
|
97
|
+
function currentUser() {
|
|
98
|
+
return useContext(UserContext);
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
#### Do
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
function useCurrentUser() {
|
|
106
|
+
return useContext(UserContext);
|
|
107
|
+
}
|
|
108
|
+
```
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: react/jsx-key
|
|
3
|
+
title: "Elements in an array or a map() callback have a key"
|
|
4
|
+
languages: [javascript, jsx, tsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "the key must be a stable identity of the item, which only the author knows"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint-plugin-react/jsx-key]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
An element written in an array literal, or returned by a `.map()` callback, has a `key` prop.
|
|
13
|
+
|
|
14
|
+
**Why:** Without keys, React can't match list items between renders, which loses state and re-
|
|
15
|
+
renders more than it must.
|
|
16
|
+
|
|
17
|
+
```codelaw
|
|
18
|
+
match:
|
|
19
|
+
any:
|
|
20
|
+
- kind: jsx_element
|
|
21
|
+
- kind: jsx_self_closing_element
|
|
22
|
+
not:
|
|
23
|
+
any:
|
|
24
|
+
- has: { stopBy: neighbor, kind: jsx_attribute, regex: '^key\b' }
|
|
25
|
+
- has: { field: open_tag, has: { stopBy: neighbor, kind: jsx_attribute, regex: '^key\b' } }
|
|
26
|
+
- has: { stopBy: neighbor, kind: jsx_expression, regex: '^\{\s*\.\.\.' }
|
|
27
|
+
- has: { field: open_tag, has: { stopBy: neighbor, kind: jsx_expression, regex: '^\{\s*\.\.\.' } }
|
|
28
|
+
inside:
|
|
29
|
+
stopBy: neighbor
|
|
30
|
+
any:
|
|
31
|
+
- kind: array
|
|
32
|
+
- kind: arrow_function
|
|
33
|
+
inside:
|
|
34
|
+
stopBy: neighbor
|
|
35
|
+
kind: arguments
|
|
36
|
+
inside: { stopBy: neighbor, kind: call_expression, has: { field: function, kind: member_expression, regex: '\.map$' } }
|
|
37
|
+
- kind: parenthesized_expression
|
|
38
|
+
inside:
|
|
39
|
+
stopBy: neighbor
|
|
40
|
+
kind: arrow_function
|
|
41
|
+
inside:
|
|
42
|
+
stopBy: neighbor
|
|
43
|
+
kind: arguments
|
|
44
|
+
inside: { stopBy: neighbor, kind: call_expression, has: { field: function, kind: member_expression, regex: '\.map$' } }
|
|
45
|
+
message: "An element in a list needs a `key` so React can tell its items apart"
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## How to fix
|
|
49
|
+
|
|
50
|
+
1. Give the element a `key` that identifies the item, such as its id.
|
|
51
|
+
2. Avoid the array index if items can be reordered or removed.
|
|
52
|
+
|
|
53
|
+
## Examples
|
|
54
|
+
|
|
55
|
+
### JavaScript
|
|
56
|
+
|
|
57
|
+
#### Don't
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
const items = todos.map((todo) => <li>{todo.text}</li>);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### Do
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
const items = todos.map((todo) => <li key={todo.id}>{todo.text}</li>);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### JSX
|
|
70
|
+
|
|
71
|
+
#### Don't
|
|
72
|
+
|
|
73
|
+
```jsx
|
|
74
|
+
const items = todos.map((todo) => <li>{todo.text}</li>);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
#### Do
|
|
78
|
+
|
|
79
|
+
```jsx
|
|
80
|
+
const items = todos.map((todo) => <li key={todo.id}>{todo.text}</li>);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### TSX
|
|
84
|
+
|
|
85
|
+
#### Don't
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
const items = todos.map((todo) => <li>{todo.text}</li>);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
#### Do
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
const items = todos.map((todo) => <li key={todo.id}>{todo.text}</li>);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Differences from the source
|
|
98
|
+
|
|
99
|
+
Elements returned from a `.map()` callback's block body (`return <li/>`) are not checked yet.
|