Skip to content

Commit 99cd049

Browse files
ulrichstarkRel1cx
andauthored
feat(react-x): add 'no-unused-props' rule (#1161)
Signed-off-by: Ulrich Stark <[email protected]> Signed-off-by: REL1CX <[email protected]> Co-authored-by: REL1CX <[email protected]>
1 parent 8858bc2 commit 99cd049

File tree

10 files changed

+1136
-1
lines changed

10 files changed

+1136
-1
lines changed

apps/website/content/docs/migration.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ The following table compares all rules from `eslint-plugin-react` with their ESL
125125
| [`no-unsafe`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unsafe.md) | [`no-unsafe-component-will-mount`](/docs/rules/no-unsafe-component-will-mount) + [`no-unsafe-component-will-receive-props`](/docs/rules/no-unsafe-component-will-receive-props) + [`no-unsafe-component-will-update`](/docs/rules/no-unsafe-component-will-update) || 🟡 |
126126
| [`no-unstable-nested-components`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unstable-nested-components.md) | [`no-nested-component-definitions`](/docs/rules/no-nested-component-definitions) |||
127127
| [`no-unused-class-component-methods`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unused-class-component-methods.md) | [`no-unused-class-component-members`](/docs/rules/no-unused-class-component-members) || 🚫 |
128-
| [`no-unused-prop-types`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unused-prop-types.md) | [`no-prop-types`](/docs/rules/no-prop-types) || 🚫 |
128+
| [`no-unused-prop-types`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unused-prop-types.md) | [`no-unused-props`](/docs/rules/no-unused-props) || 🚫 |
129129
| [`no-unused-state`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unused-state.md) | [`no-unused-state`](/docs/rules/no-unused-state) || 🚫 |
130130
| [`no-will-update-set-state`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-will-update-set-state.md) | [`no-set-state-in-component-will-update`](/docs/rules/no-set-state-in-component-will-update) |||
131131
| [`prefer-es6-class`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/prefer-es6-class.md) | [`no-prop-types`](/docs/rules/no-prop-types) || 🚫 |

apps/website/content/docs/rules/meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"no-unstable-context-value",
5656
"no-unstable-default-props",
5757
"no-unused-class-component-members",
58+
"no-unused-props",
5859
"no-unused-state",
5960
"no-use-context",
6061
"no-useless-forward-ref",

apps/website/content/docs/rules/overview.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ The `jsx-*` rules check for issues exclusive to JSX syntax, which are absent fro
8282
| [`no-unstable-context-value`](./no-unstable-context-value) | 1️⃣ | | Prevents non-stable values (i.e. object literals) from being used as a value for `Context.Provider` | |
8383
| [`no-unstable-default-props`](./no-unstable-default-props) | 1️⃣ | | Prevents using referential-type values as default props in object destructuring | |
8484
| [`no-unused-class-component-members`](./no-unused-class-component-members) | 1️⃣ | | Warns unused class component methods and properties | |
85+
| [`no-unused-props`](./no-unused-props) | 0️⃣ | | Warns about unused component prop declarations | |
8586
| [`no-unused-state`](./no-unused-state) | 1️⃣ | | Warns unused class component state | |
8687
| [`no-use-context`](./no-use-context) | 1️⃣ | `🔄` | Replaces usages of `useContext` with `use` | >=19.0.0 |
8788
| [`no-useless-forward-ref`](./no-useless-forward-ref) | 1️⃣ | | Disallow useless `forwardRef` calls on components that don't use `ref`s | |

packages/plugins/eslint-plugin-react-x/src/configs/recommended.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const rules = {
5656
"react-x/no-unstable-context-value": "warn",
5757
"react-x/no-unstable-default-props": "warn",
5858
"react-x/no-unused-class-component-members": "warn",
59+
// "react-x/no-unused-props": "warn",
5960
"react-x/no-unused-state": "warn",
6061
"react-x/no-use-context": "warn",
6162
"react-x/no-useless-forward-ref": "warn",

packages/plugins/eslint-plugin-react-x/src/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import noUnsafeComponentWillUpdate from "./rules/no-unsafe-component-will-update
5151
import noUnstableContextValue from "./rules/no-unstable-context-value";
5252
import noUnstableDefaultProps from "./rules/no-unstable-default-props";
5353
import noUnusedClassComponentMembers from "./rules/no-unused-class-component-members";
54+
import noUnusedProps from "./rules/no-unused-props";
5455
import noUnusedState from "./rules/no-unused-state";
5556
import noUseContext from "./rules/no-use-context";
5657
import noUselessForwardRef from "./rules/no-useless-forward-ref";
@@ -127,6 +128,7 @@ export const plugin = {
127128
"no-unstable-context-value": noUnstableContextValue,
128129
"no-unstable-default-props": noUnstableDefaultProps,
129130
"no-unused-class-component-members": noUnusedClassComponentMembers,
131+
"no-unused-props": noUnusedProps,
130132
"no-unused-state": noUnusedState,
131133
"no-use-context": noUseContext,
132134
"no-useless-forward-ref": noUselessForwardRef,
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: no-unused-props
3+
---
4+
5+
**Full Name in `eslint-plugin-react-x`**
6+
7+
```sh copy
8+
react-x/no-unused-props
9+
```
10+
11+
**Full Name in `@eslint-react/eslint-plugin`**
12+
13+
```sh copy
14+
@eslint-react/no-unused-props
15+
```
16+
17+
## Description
18+
19+
Warns about unused component prop declarations.
20+
21+
Unused props increase maintenance overhead and may mislead consumers of the component into thinking the prop is required or meaningful, even when it has no effect.
22+
23+
This is the TypeScript-only version of [`eslint-plugin-react/no-unused-prop-types`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unused-prop-types.md). In contrast to the original rule, this rule
24+
25+
- doesn't support the legacy propTypes syntax
26+
- combines the used props of one type definition declared by multiple components
27+
28+
## Examples
29+
30+
### Failing
31+
32+
```tsx
33+
interface Props {
34+
abc: string; // used
35+
hello: string; // NOT used
36+
}
37+
38+
function Component(props: Props) {
39+
const { abc } = props; // `hello` isn't accessed from `props`
40+
return null;
41+
}
42+
```
43+
44+
### Passing
45+
46+
```tsx
47+
interface Props {
48+
abc: string; // used
49+
hello: string; // used
50+
}
51+
52+
function Component(props: Props) {
53+
const { abc, hello } = props;
54+
return null;
55+
}
56+
```
57+
58+
```tsx
59+
interface Props {
60+
abc: string; // used by Component1
61+
hello: string; // used by Component2
62+
}
63+
64+
function Component1({ abc }: Props) {
65+
return null;
66+
}
67+
68+
function Component2({ hello }: Props) {
69+
return null;
70+
}
71+
```
72+
73+
## Implementation
74+
75+
- [Rule source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x/src/rules/no-unused-props.ts)
76+
- [Test source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x/src/rules/no-unused-props.spec.ts)
77+
78+
## See Also
79+
80+
- [`no-prop-types`](/docs/rules/no-prop-types)\
81+
Disallows `propTypes`

0 commit comments

Comments
 (0)