Skip to content

Commit 47adbc4

Browse files
committed
feat: add 'no-prop-types' and 'no-default-props', closes #597
1 parent b1a8028 commit 47adbc4

File tree

14 files changed

+502
-120
lines changed

14 files changed

+502
-120
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v1.5.29 (Draft)
2+
3+
### ✨ New
4+
5+
- Add rule `no-prop-types`.
6+
- Add rule `no-default-props`.
7+
18
## v1.5.28 (Sat 20 Jul 2024)
29

310
### 🐞 Fixes

packages/plugins/eslint-plugin-react-x/README.md

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ export default [
4444
"react-x/no-component-will-receive-props": "error",
4545
"react-x/no-component-will-update": "error",
4646
"react-x/no-create-ref": "error",
47+
"react-x/no-default-props": "error",
4748
"react-x/no-direct-mutation-state": "error",
4849
"react-x/no-duplicate-key": "error",
4950
"react-x/no-missing-key": "error",
5051
"react-x/no-nested-components": "warn",
52+
"react-x/no-prop-types": "error",
5153
"react-x/no-redundant-should-component-update": "error",
5254
"react-x/no-set-state-in-component-did-mount": "warn",
5355
"react-x/no-set-state-in-component-did-update": "warn",
@@ -69,46 +71,46 @@ export default [
6971

7072
## Rules
7173

72-
| Rule | Description | 💼 | 💭 | |
73-
| :--------------------------------------- | :--------------------------------------------------------------------------------------------------- | :-: | :-: | :-: |
74-
| `avoid-shorthand-boolean` | Enforces the use of shorthand syntax for boolean attributes. | 🎨 | | |
75-
| `avoid-shorthand-fragment` | Enforces the use of shorthand syntax for fragments. | 🎨 | | |
76-
| `ensure-forward-ref-using-ref` | Requires that components wrapped with `forwardRef` must have a `ref` parameter. | ✔️ | | |
77-
| `no-access-state-in-setstate` | Prevents accessing `this.state` inside `setState` calls. | ✔️ | | |
78-
| `no-array-index-key` | Warns when an array `index` is used as a `key` prop. | 👀 | | |
79-
| `no-children-count` | Prevents usage of `Children.count`. || | |
80-
| `no-children-for-each` | Prevents usage of `Children.forEach`. || | |
81-
| `no-children-map` | Prevents usage of `Children.map`. || | |
82-
| `no-children-only` | Prevents usage of `Children.only`. || | |
83-
| `no-children-prop` | Prevents usage of `children` as a prop. || | |
84-
| `no-children-to-array` | Prevents usage of `Children.toArray`. || | |
85-
| `no-class-component` | Prevents usage of class component. || | |
86-
| `no-clone-element` | Prevents usage of `cloneElement`. || | |
87-
| `no-comment-textnodes` | Prevents comments from being inserted as text nodes. | 👀 | | |
88-
| `no-component-will-mount` | Prevents usage of `componentWillMount`. || | |
89-
| `no-component-will-receive-props` | Prevents usage of `componentWillReceiveProps`. || | |
90-
| `no-component-will-update` | Prevents usage of `componentWillUpdate`. || | |
91-
| `no-create-ref` | Prevents usage of `createRef`. || | |
92-
| `no-direct-mutation-state` | Prevents direct mutation of `this.state`. | ✔️ | | |
93-
| `no-duplicate-key` | Prevents duplicate `key` props on elements in the same array or a list of `children`. | ✔️ | | |
94-
| `no-leaked-conditional-rendering` | Prevents problematic leaked values from being rendered. | 👀 | 💭 | |
95-
| `no-missing-component-display-name` | Enforces that all components have a `displayName` which can be used in devtools. | 🐞 | | |
96-
| `no-missing-key` | Prevents missing `key` prop on items in list rendering. | ✔️ | | |
97-
| `no-nested-components` | Prevents nesting component definitions inside other components. | ✔️ | | |
98-
| `no-redundant-should-component-update` | Prevents usage of `shouldComponentUpdate` when extending `React.PureComponent`. | ✔️ | | |
99-
| `no-set-state-in-component-did-mount` | Disallows calling `this.setState` in `componentDidMount` outside of functions, such as callbacks. | 👀 | | |
100-
| `no-set-state-in-component-did-update` | Disallows calling `this.setState` in `componentDidUpdate` outside of functions, such as callbacks. | 👀 | | |
101-
| `no-set-state-in-component-will-update` | Disallows calling `this.setState` in `componentWillUpdate` outside of functions, such as callbacks. | 👀 | | |
102-
| `no-string-refs` | Disallows using deprecated string `refs`. || | |
103-
| `no-unsafe-component-will-mount` | Warns usage of `UNSAFE_componentWillMount` in class components. | 👀 | | |
104-
| `no-unsafe-component-will-receive-props` | Warns usage of `UNSAFE_componentWillReceiveProps` in class components. | 👀 | | |
105-
| `no-unsafe-component-will-update` | Warns usage of `UNSAFE_componentWillUpdate` in class components. | 👀 | | |
106-
| `no-unstable-context-value` | Prevents non-stable values (i.e. object literals) from being used as a value for `Context.Provider`. | 🚀 | | |
107-
| `no-unstable-default-props` | Prevents usage of referential-type values as default props in object destructuring. | 🚀 | | |
108-
| `no-unused-class-component-members` | Warns unused class component methods and properties. | ✔️ | | |
109-
| `no-unused-state` | Warns unused class component state. | ✔️ | | |
110-
| `no-useless-fragment` | Prevents the use of useless `fragment` components or `<>` syntax. | ✔️ | | |
111-
| `prefer-read-only-props` | Enforces that function components props are readonly. | ✔️ | 💭 | |
112-
| `prefer-destructuring-assignment` | Enforces the use of destructuring assignment over property assignment. | 🎨 | | |
113-
| `prefer-shorthand-boolean` | Enforces the use of shorthand syntax for boolean attributes. | 🎨 | | |
114-
| `prefer-shorthand-fragment` | Enforces the use of shorthand syntax for fragments. | 🎨 | | |
74+
| Rule | Description | 💼 | 💭 | |
75+
| :--------------------------------------- | :--------------------------------------------------------------------------------------------------- | :---: | :---: | :---: |
76+
| `avoid-shorthand-boolean` | Enforces the use of shorthand syntax for boolean attributes. | 🎨 | | |
77+
| `avoid-shorthand-fragment` | Enforces the use of shorthand syntax for fragments. | 🎨 | | |
78+
| `ensure-forward-ref-using-ref` | Requires that components wrapped with `forwardRef` must have a `ref` parameter. | ✔️ | | |
79+
| `no-access-state-in-setstate` | Prevents accessing `this.state` inside `setState` calls. | ✔️ | | |
80+
| `no-array-index-key` | Warns when an array `index` is used as a `key` prop. | 👀 | | |
81+
| `no-children-count` | Prevents usage of `Children.count`. | | | |
82+
| `no-children-for-each` | Prevents usage of `Children.forEach`. | | | |
83+
| `no-children-map` | Prevents usage of `Children.map`. | | | |
84+
| `no-children-only` | Prevents usage of `Children.only`. | | | |
85+
| `no-children-prop` | Prevents usage of `children` as a prop. | | | |
86+
| `no-children-to-array` | Prevents usage of `Children.toArray`. | | | |
87+
| `no-class-component` | Prevents usage of class component. | | | |
88+
| `no-clone-element` | Prevents usage of `cloneElement`. | | | |
89+
| `no-comment-textnodes` | Prevents comments from being inserted as text nodes. | 👀 | | |
90+
| `no-component-will-mount` | Prevents usage of `componentWillMount`. | | | |
91+
| `no-component-will-receive-props` | Prevents usage of `componentWillReceiveProps`. | | | |
92+
| `no-component-will-update` | Prevents usage of `componentWillUpdate`. | | | |
93+
| `no-create-ref` | Prevents usage of `createRef`. | | | |
94+
| `no-direct-mutation-state` | Prevents direct mutation of `this.state`. | ✔️ | | |
95+
| `no-duplicate-key` | Prevents duplicate `key` props on elements in the same array or a list of `children`. | ✔️ | | |
96+
| `no-leaked-conditional-rendering` | Prevents problematic leaked values from being rendered. | 👀 | 💭 | |
97+
| `no-missing-component-display-name` | Enforces that all components have a `displayName` which can be used in devtools. | 🐞 | | |
98+
| `no-missing-key` | Prevents missing `key` prop on items in list rendering. | ✔️ | | |
99+
| `no-nested-components` | Prevents nesting component definitions inside other components. | ✔️ | | |
100+
| `no-redundant-should-component-update` | Prevents usage of `shouldComponentUpdate` when extending `React.PureComponent`. | ✔️ | | |
101+
| `no-set-state-in-component-did-mount` | Disallows calling `this.setState` in `componentDidMount` outside of functions, such as callbacks. | 👀 | | |
102+
| `no-set-state-in-component-did-update` | Disallows calling `this.setState` in `componentDidUpdate` outside of functions, such as callbacks. | 👀 | | |
103+
| `no-set-state-in-component-will-update` | Disallows calling `this.setState` in `componentWillUpdate` outside of functions, such as callbacks. | 👀 | | |
104+
| `no-string-refs` | Disallows using deprecated string `refs`. | | | |
105+
| `no-unsafe-component-will-mount` | Warns usage of `UNSAFE_componentWillMount` in class components. | 👀 | | |
106+
| `no-unsafe-component-will-receive-props` | Warns usage of `UNSAFE_componentWillReceiveProps` in class components. | 👀 | | |
107+
| `no-unsafe-component-will-update` | Warns usage of `UNSAFE_componentWillUpdate` in class components. | 👀 | | |
108+
| `no-unstable-context-value` | Prevents non-stable values (i.e. object literals) from being used as a value for `Context.Provider`. | 🚀 | | |
109+
| `no-unstable-default-props` | Prevents usage of referential-type values as default props in object destructuring. | 🚀 | | |
110+
| `no-unused-class-component-members` | Warns unused class component methods and properties. | ✔️ | | |
111+
| `no-unused-state` | Warns unused class component state. | ✔️ | | |
112+
| `no-useless-fragment` | Prevents the use of useless `fragment` components or `<>` syntax. | ✔️ | | |
113+
| `prefer-read-only-props` | Enforces that function components props are readonly. | ✔️ | 💭 | |
114+
| `prefer-destructuring-assignment` | Enforces the use of destructuring assignment over property assignment. | 🎨 | | |
115+
| `prefer-shorthand-boolean` | Enforces the use of shorthand syntax for boolean attributes. | 🎨 | | |
116+
| `prefer-shorthand-fragment` | Enforces the use of shorthand syntax for fragments. | 🎨 | | |

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import noComponentWillMount from "./rules/no-component-will-mount";
1818
import noComponentWillReceiveProps from "./rules/no-component-will-receive-props";
1919
import noComponentWillUpdate from "./rules/no-component-will-update";
2020
import noCreateRef from "./rules/no-create-ref";
21+
import noDefaultProps from "./rules/no-default-props";
2122
import noDirectMutationState from "./rules/no-direct-mutation-state";
2223
import noDuplicateKey from "./rules/no-duplicate-key";
2324
import noImplicitKey from "./rules/no-implicit-key";
2425
import noLeakedConditionalRendering from "./rules/no-leaked-conditional-rendering";
2526
import noMissingComponentDisplayName from "./rules/no-missing-component-display-name";
2627
import noMissingKey from "./rules/no-missing-key";
2728
import noNestedComponents from "./rules/no-nested-components";
29+
import noPropTypes from "./rules/no-prop-types";
2830
import noRedundantShouldComponentUpdate from "./rules/no-redundant-should-component-update";
2931
import noSetStateInComponentDidMount from "./rules/no-set-state-in-component-did-mount";
3032
import noSetStateInComponentDidUpdate from "./rules/no-set-state-in-component-did-update";
@@ -68,13 +70,15 @@ export const rules = {
6870
"no-component-will-receive-props": noComponentWillReceiveProps,
6971
"no-component-will-update": noComponentWillUpdate,
7072
"no-create-ref": noCreateRef,
73+
"no-default-props": noDefaultProps,
7174
"no-direct-mutation-state": noDirectMutationState,
7275
"no-duplicate-key": noDuplicateKey,
7376
"no-implicit-key": noImplicitKey,
7477
"no-leaked-conditional-rendering": noLeakedConditionalRendering,
7578
"no-missing-component-display-name": noMissingComponentDisplayName,
7679
"no-missing-key": noMissingKey,
7780
"no-nested-components": noNestedComponents,
81+
"no-prop-types": noPropTypes,
7882
"no-redundant-should-component-update": noRedundantShouldComponentUpdate,
7983
"no-set-state-in-component-did-mount": noSetStateInComponentDidMount,
8084
"no-set-state-in-component-did-update": noSetStateInComponentDidUpdate,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { allValid, ruleTester } from "../../../../../test";
2+
import rule, { RULE_NAME } from "./no-default-props";
3+
4+
ruleTester.run(RULE_NAME, rule, {
5+
invalid: [
6+
{
7+
code: /* tsx */ `
8+
function App() {
9+
return <div />
10+
}
11+
App.defaultProps = {};
12+
`,
13+
errors: [{ messageId: "NO_DEFAULT_PROPS" }],
14+
},
15+
{
16+
code: /* tsx */ `
17+
class Input extends React.Component {
18+
render() {
19+
return <input />;
20+
}
21+
}
22+
Input.defaultProps = {};
23+
`,
24+
errors: [{ messageId: "NO_DEFAULT_PROPS" }],
25+
},
26+
{
27+
code: /* tsx */ `
28+
class Input extends React.Component {
29+
static defaultProps = {};
30+
31+
render() {
32+
return <input />;
33+
}
34+
}
35+
`,
36+
errors: [{ messageId: "NO_DEFAULT_PROPS" }],
37+
},
38+
],
39+
valid: [
40+
...allValid,
41+
/* tsx */ `
42+
function App() {
43+
return <div />
44+
}
45+
`,
46+
/* tsx */ `
47+
function App() {
48+
return <div />;
49+
}
50+
`,
51+
/* tsx */ `
52+
class Input extends React.Component {
53+
render() {
54+
return <input />;
55+
}
56+
}
57+
`,
58+
],
59+
});

0 commit comments

Comments
 (0)