@codelaw/rules-js 0.1.0 → 0.2.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/NOTICE +0 -9
- package/README.md +14 -47
- package/codelaw-pack.toml +3 -3
- package/corpus.toml +76 -0
- package/package.json +2 -2
- package/presets/js/conventions.toml +0 -4
- package/presets/js/project.toml +4 -0
- package/presets/js/typescript.toml +7 -0
- package/data/node/deprecated-api.json +0 -83
- package/data/react/dom-property-aliases.json +0 -131
- package/presets/node/recommended.toml +0 -13
- package/presets/react/recommended.toml +0 -22
- package/presets/stencil/recommended.toml +0 -18
- package/presets/ts/recommended.toml +0 -27
- package/rules/node/hashbang.md +0 -71
- package/rules/node/no-deprecated-api.md +0 -91
- package/rules/node/no-exports-assign.md +0 -92
- package/rules/node/no-extraneous-import.md +0 -118
- package/rules/node/no-extraneous-require.md +0 -117
- package/rules/node/no-missing-import.md +0 -118
- package/rules/node/no-missing-require.md +0 -117
- package/rules/node/no-process-exit.md +0 -99
- package/rules/node/no-unpublished-import.md +0 -134
- package/rules/node/no-unpublished-require.md +0 -133
- package/rules/react/display-name.md +0 -91
- package/rules/react/exhaustive-deps.md +0 -116
- package/rules/react/hook-prefix.md +0 -108
- package/rules/react/jsx-key.md +0 -99
- package/rules/react/jsx-no-comment-textnodes.md +0 -87
- package/rules/react/jsx-no-duplicate-props.md +0 -66
- package/rules/react/jsx-no-target-blank.md +0 -78
- package/rules/react/jsx-no-undef.md +0 -78
- package/rules/react/no-children-prop.md +0 -80
- package/rules/react/no-danger-with-children.md +0 -83
- package/rules/react/no-deprecated.md +0 -84
- package/rules/react/no-direct-mutation-state.md +0 -102
- package/rules/react/no-find-dom-node.md +0 -72
- package/rules/react/no-is-mounted.md +0 -109
- package/rules/react/no-render-return-value.md +0 -75
- package/rules/react/no-string-refs.md +0 -72
- package/rules/react/no-unescaped-entities.md +0 -69
- package/rules/react/no-unknown-property.md +0 -69
- package/rules/react/props-type-name.md +0 -43
- package/rules/react/require-render-return.md +0 -91
- package/rules/react/rules-of-hooks.md +0 -128
- package/rules/stencil/ban-default-true.md +0 -70
- package/rules/stencil/ban-exported-const-enums.md +0 -60
- package/rules/stencil/ban-prefix.md +0 -76
- package/rules/stencil/decorators-context.md +0 -61
- package/rules/stencil/host-data-deprecated.md +0 -73
- package/rules/stencil/methods-must-be-public.md +0 -69
- package/rules/stencil/no-unused-watch.md +0 -68
- package/rules/stencil/own-methods-must-be-private.md +0 -68
- package/rules/stencil/own-props-must-be-private.md +0 -60
- package/rules/stencil/prefer-vdom-listener.md +0 -67
- package/rules/stencil/prop-readonly.md +0 -63
- package/rules/stencil/props-must-be-public.md +0 -68
- package/rules/stencil/required-jsdoc.md +0 -66
- package/rules/stencil/reserved-member-names.md +0 -70
- package/rules/stencil/strict-mutable.md +0 -75
- package/rules/stencil/tag-prefix.md +0 -75
- package/rules/ts/ban-ts-comment.md +0 -64
- package/rules/ts/no-array-constructor.md +0 -58
- package/rules/ts/no-duplicate-enum-values.md +0 -65
- package/rules/ts/no-empty-object-type.md +0 -65
- package/rules/ts/no-explicit-any.md +0 -61
- package/rules/ts/no-extra-non-null-assertion.md +0 -50
- package/rules/ts/no-misused-new.md +0 -70
- package/rules/ts/no-namespace.md +0 -65
- package/rules/ts/no-non-null-asserted-optional-chain.md +0 -55
- package/rules/ts/no-require-imports.md +0 -54
- package/rules/ts/no-this-alias.md +0 -68
- package/rules/ts/no-unnecessary-type-constraint.md +0 -64
- package/rules/ts/no-unsafe-declaration-merging.md +0 -73
- package/rules/ts/no-unsafe-function-type.md +0 -57
- package/rules/ts/no-unused-expressions.md +0 -61
- package/rules/ts/no-unused-vars.md +0 -68
- package/rules/ts/no-wrapper-object-types.md +0 -49
- package/rules/ts/prefer-as-const.md +0 -47
- package/rules/ts/prefer-namespace-keyword.md +0 -63
- package/rules/ts/triple-slash-reference.md +0 -60
- package/rules/ts/type-only-imports.md +0 -57
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
id: node/no-process-exit
|
|
3
|
-
title: "No process.exit()"
|
|
4
|
-
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
-
fix: manual
|
|
6
|
-
fix_reason: "how to end the program cleanly depends on what is still running"
|
|
7
|
-
stability: preview
|
|
8
|
-
ported_from: [eslint-plugin-n/no-process-exit]
|
|
9
|
-
since: 0.1.0
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
Code doesn't call `process.exit()`.
|
|
13
|
-
|
|
14
|
-
**Why:** `process.exit()` ends the process at once, dropping pending writes and skipping cleanup;
|
|
15
|
-
libraries that call it take that choice away from the program.
|
|
16
|
-
|
|
17
|
-
```codelaw
|
|
18
|
-
match:
|
|
19
|
-
pattern: process.exit($$$ARGS)
|
|
20
|
-
message: "Don't end the process from here; throw, or set `process.exitCode`"
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## How to fix
|
|
24
|
-
|
|
25
|
-
1. Set `process.exitCode` and let the program finish, or throw an error the caller handles.
|
|
26
|
-
|
|
27
|
-
## Examples
|
|
28
|
-
|
|
29
|
-
### JavaScript
|
|
30
|
-
|
|
31
|
-
#### Don't
|
|
32
|
-
|
|
33
|
-
```js
|
|
34
|
-
if (failed) {
|
|
35
|
-
process.exit(1);
|
|
36
|
-
}
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
#### Do
|
|
40
|
-
|
|
41
|
-
```js
|
|
42
|
-
if (failed) {
|
|
43
|
-
process.exitCode = 1;
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### JSX
|
|
48
|
-
|
|
49
|
-
#### Don't
|
|
50
|
-
|
|
51
|
-
```jsx
|
|
52
|
-
if (failed) {
|
|
53
|
-
process.exit(1);
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
#### Do
|
|
58
|
-
|
|
59
|
-
```jsx
|
|
60
|
-
if (failed) {
|
|
61
|
-
process.exitCode = 1;
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### TypeScript
|
|
66
|
-
|
|
67
|
-
#### Don't
|
|
68
|
-
|
|
69
|
-
```ts
|
|
70
|
-
if (failed) {
|
|
71
|
-
process.exit(1);
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
#### Do
|
|
76
|
-
|
|
77
|
-
```ts
|
|
78
|
-
if (failed) {
|
|
79
|
-
process.exitCode = 1;
|
|
80
|
-
}
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
### TSX
|
|
84
|
-
|
|
85
|
-
#### Don't
|
|
86
|
-
|
|
87
|
-
```tsx
|
|
88
|
-
if (failed) {
|
|
89
|
-
process.exit(1);
|
|
90
|
-
}
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
#### Do
|
|
94
|
-
|
|
95
|
-
```tsx
|
|
96
|
-
if (failed) {
|
|
97
|
-
process.exitCode = 1;
|
|
98
|
-
}
|
|
99
|
-
```
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
id: node/no-unpublished-import
|
|
3
|
-
title: "Published files import only what is published"
|
|
4
|
-
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
-
scope: project
|
|
6
|
-
fix: manual
|
|
7
|
-
fix_reason: "whether to publish the file, move the dependency or move the code is the package owner's choice"
|
|
8
|
-
stability: preview
|
|
9
|
-
ported_from: [eslint-plugin-n/no-unpublished-import]
|
|
10
|
-
since: 0.1.0
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
In a package that is not `private` and lists `files`, a file `npm publish` packs imports neither a project file it doesn't pack nor a package that is only a `devDependency`.
|
|
14
|
-
|
|
15
|
-
**Why:** the published package then fails to load for its users: the file isn't in the tarball, or the devDependency isn't installed.
|
|
16
|
-
|
|
17
|
-
```codelaw
|
|
18
|
-
graph:
|
|
19
|
-
import:
|
|
20
|
-
form: [import, export, dynamic]
|
|
21
|
-
published: false
|
|
22
|
-
message: "`$SOURCE` is not shipped to this package's users (`$MANIFEST`), but a published file imports it"
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## How to fix
|
|
26
|
-
|
|
27
|
-
1. Add the imported file to `files` in the nearest `package.json`, move the package from `devDependencies` to `dependencies`, or move the import out of the published code.
|
|
28
|
-
2. A type-only import is not reported: it is erased from the published JavaScript.
|
|
29
|
-
|
|
30
|
-
## Examples
|
|
31
|
-
|
|
32
|
-
### JavaScript
|
|
33
|
-
|
|
34
|
-
#### Don't
|
|
35
|
-
|
|
36
|
-
```js files
|
|
37
|
-
// package.json
|
|
38
|
-
{ "name": "lib", "main": "lib/index.js", "files": ["lib"] }
|
|
39
|
-
// lib/index.js
|
|
40
|
-
import { fixtures } from "../test/fixtures";
|
|
41
|
-
console.log(fixtures);
|
|
42
|
-
// test/fixtures.js
|
|
43
|
-
export const fixtures = [];
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
#### Do
|
|
47
|
-
|
|
48
|
-
```js files
|
|
49
|
-
// package.json
|
|
50
|
-
{ "name": "lib", "main": "lib/index.js", "files": ["lib"] }
|
|
51
|
-
// lib/index.js
|
|
52
|
-
import { fixtures } from "./fixtures";
|
|
53
|
-
console.log(fixtures);
|
|
54
|
-
// lib/fixtures.js
|
|
55
|
-
export const fixtures = [];
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### JSX
|
|
59
|
-
|
|
60
|
-
#### Don't
|
|
61
|
-
|
|
62
|
-
```jsx files
|
|
63
|
-
// package.json
|
|
64
|
-
{ "name": "lib", "main": "lib/index.jsx", "files": ["lib"] }
|
|
65
|
-
// lib/index.jsx
|
|
66
|
-
import { fixtures } from "../test/fixtures";
|
|
67
|
-
export const App = () => <p>{String(fixtures)}</p>;
|
|
68
|
-
// test/fixtures.jsx
|
|
69
|
-
export const fixtures = [];
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
#### Do
|
|
73
|
-
|
|
74
|
-
```jsx files
|
|
75
|
-
// package.json
|
|
76
|
-
{ "name": "lib", "main": "lib/index.jsx", "files": ["lib"] }
|
|
77
|
-
// lib/index.jsx
|
|
78
|
-
import { fixtures } from "./fixtures";
|
|
79
|
-
export const App = () => <p>{String(fixtures)}</p>;
|
|
80
|
-
// lib/fixtures.jsx
|
|
81
|
-
export const fixtures = [];
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### TypeScript
|
|
85
|
-
|
|
86
|
-
#### Don't
|
|
87
|
-
|
|
88
|
-
```ts files
|
|
89
|
-
// package.json
|
|
90
|
-
{ "name": "lib", "main": "lib/index.ts", "files": ["lib"] }
|
|
91
|
-
// lib/index.ts
|
|
92
|
-
import { fixtures } from "../test/fixtures";
|
|
93
|
-
console.log(fixtures);
|
|
94
|
-
// test/fixtures.ts
|
|
95
|
-
export const fixtures = [];
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
#### Do
|
|
99
|
-
|
|
100
|
-
```ts files
|
|
101
|
-
// package.json
|
|
102
|
-
{ "name": "lib", "main": "lib/index.ts", "files": ["lib"] }
|
|
103
|
-
// lib/index.ts
|
|
104
|
-
import { fixtures } from "./fixtures";
|
|
105
|
-
console.log(fixtures);
|
|
106
|
-
// lib/fixtures.ts
|
|
107
|
-
export const fixtures = [];
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
### TSX
|
|
111
|
-
|
|
112
|
-
#### Don't
|
|
113
|
-
|
|
114
|
-
```tsx files
|
|
115
|
-
// package.json
|
|
116
|
-
{ "name": "lib", "main": "lib/index.tsx", "files": ["lib"] }
|
|
117
|
-
// lib/index.tsx
|
|
118
|
-
import { fixtures } from "../test/fixtures";
|
|
119
|
-
export const App = () => <p>{String(fixtures)}</p>;
|
|
120
|
-
// test/fixtures.tsx
|
|
121
|
-
export const fixtures = [];
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
#### Do
|
|
125
|
-
|
|
126
|
-
```tsx files
|
|
127
|
-
// package.json
|
|
128
|
-
{ "name": "lib", "main": "lib/index.tsx", "files": ["lib"] }
|
|
129
|
-
// lib/index.tsx
|
|
130
|
-
import { fixtures } from "./fixtures";
|
|
131
|
-
export const App = () => <p>{String(fixtures)}</p>;
|
|
132
|
-
// lib/fixtures.tsx
|
|
133
|
-
export const fixtures = [];
|
|
134
|
-
```
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
id: node/no-unpublished-require
|
|
3
|
-
title: "Published files require only what is published"
|
|
4
|
-
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
-
scope: project
|
|
6
|
-
fix: manual
|
|
7
|
-
fix_reason: "whether to publish the file, move the dependency or move the code is the package owner's choice"
|
|
8
|
-
stability: preview
|
|
9
|
-
ported_from: [eslint-plugin-n/no-unpublished-require]
|
|
10
|
-
since: 0.1.0
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
In a package that is not `private` and lists `files`, a file `npm publish` packs requires neither a project file it doesn't pack nor a package that is only a `devDependency`.
|
|
14
|
-
|
|
15
|
-
**Why:** the published package then fails to load for its users: the file isn't in the tarball, or the devDependency isn't installed.
|
|
16
|
-
|
|
17
|
-
```codelaw
|
|
18
|
-
graph:
|
|
19
|
-
import:
|
|
20
|
-
form: [require]
|
|
21
|
-
published: false
|
|
22
|
-
message: "`$SOURCE` is not shipped to this package's users (`$MANIFEST`), but a published file requires it"
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## How to fix
|
|
26
|
-
|
|
27
|
-
1. Add the required file to `files` in the nearest `package.json`, move the package from `devDependencies` to `dependencies`, or move the `require()` out of the published code.
|
|
28
|
-
|
|
29
|
-
## Examples
|
|
30
|
-
|
|
31
|
-
### JavaScript
|
|
32
|
-
|
|
33
|
-
#### Don't
|
|
34
|
-
|
|
35
|
-
```js files
|
|
36
|
-
// package.json
|
|
37
|
-
{ "name": "lib", "main": "lib/index.js", "files": ["lib"] }
|
|
38
|
-
// lib/index.js
|
|
39
|
-
const { fixtures } = require("../test/fixtures");
|
|
40
|
-
console.log(fixtures);
|
|
41
|
-
// test/fixtures.js
|
|
42
|
-
export const fixtures = [];
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
#### Do
|
|
46
|
-
|
|
47
|
-
```js files
|
|
48
|
-
// package.json
|
|
49
|
-
{ "name": "lib", "main": "lib/index.js", "files": ["lib"] }
|
|
50
|
-
// lib/index.js
|
|
51
|
-
const { fixtures } = require("./fixtures");
|
|
52
|
-
console.log(fixtures);
|
|
53
|
-
// lib/fixtures.js
|
|
54
|
-
export const fixtures = [];
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### JSX
|
|
58
|
-
|
|
59
|
-
#### Don't
|
|
60
|
-
|
|
61
|
-
```jsx files
|
|
62
|
-
// package.json
|
|
63
|
-
{ "name": "lib", "main": "lib/index.jsx", "files": ["lib"] }
|
|
64
|
-
// lib/index.jsx
|
|
65
|
-
const { fixtures } = require("../test/fixtures");
|
|
66
|
-
export const App = () => <p>{String(fixtures)}</p>;
|
|
67
|
-
// test/fixtures.jsx
|
|
68
|
-
export const fixtures = [];
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
#### Do
|
|
72
|
-
|
|
73
|
-
```jsx files
|
|
74
|
-
// package.json
|
|
75
|
-
{ "name": "lib", "main": "lib/index.jsx", "files": ["lib"] }
|
|
76
|
-
// lib/index.jsx
|
|
77
|
-
const { fixtures } = require("./fixtures");
|
|
78
|
-
export const App = () => <p>{String(fixtures)}</p>;
|
|
79
|
-
// lib/fixtures.jsx
|
|
80
|
-
export const fixtures = [];
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
### TypeScript
|
|
84
|
-
|
|
85
|
-
#### Don't
|
|
86
|
-
|
|
87
|
-
```ts files
|
|
88
|
-
// package.json
|
|
89
|
-
{ "name": "lib", "main": "lib/index.ts", "files": ["lib"] }
|
|
90
|
-
// lib/index.ts
|
|
91
|
-
const { fixtures } = require("../test/fixtures");
|
|
92
|
-
console.log(fixtures);
|
|
93
|
-
// test/fixtures.ts
|
|
94
|
-
export const fixtures = [];
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
#### Do
|
|
98
|
-
|
|
99
|
-
```ts files
|
|
100
|
-
// package.json
|
|
101
|
-
{ "name": "lib", "main": "lib/index.ts", "files": ["lib"] }
|
|
102
|
-
// lib/index.ts
|
|
103
|
-
const { fixtures } = require("./fixtures");
|
|
104
|
-
console.log(fixtures);
|
|
105
|
-
// lib/fixtures.ts
|
|
106
|
-
export const fixtures = [];
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### TSX
|
|
110
|
-
|
|
111
|
-
#### Don't
|
|
112
|
-
|
|
113
|
-
```tsx files
|
|
114
|
-
// package.json
|
|
115
|
-
{ "name": "lib", "main": "lib/index.tsx", "files": ["lib"] }
|
|
116
|
-
// lib/index.tsx
|
|
117
|
-
const { fixtures } = require("../test/fixtures");
|
|
118
|
-
export const App = () => <p>{String(fixtures)}</p>;
|
|
119
|
-
// test/fixtures.tsx
|
|
120
|
-
export const fixtures = [];
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
#### Do
|
|
124
|
-
|
|
125
|
-
```tsx files
|
|
126
|
-
// package.json
|
|
127
|
-
{ "name": "lib", "main": "lib/index.tsx", "files": ["lib"] }
|
|
128
|
-
// lib/index.tsx
|
|
129
|
-
const { fixtures } = require("./fixtures");
|
|
130
|
-
export const App = () => <p>{String(fixtures)}</p>;
|
|
131
|
-
// lib/fixtures.tsx
|
|
132
|
-
export const fixtures = [];
|
|
133
|
-
```
|
|
@@ -1,91 +0,0 @@
|
|
|
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.
|
|
@@ -1,116 +0,0 @@
|
|
|
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`).
|
|
@@ -1,108 +0,0 @@
|
|
|
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
|
-
```
|