Skip to content

Commit 61c369c

Browse files
alexzherdevljharb
authored andcommitted
[New] add sort-default-props and deprecate jsx-sort-default-props
Pt. 1 of jsx-eslint#1834
1 parent 8beb2aa commit 61c369c

8 files changed

+1321
-1
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
55

66
## Unreleased
77

8+
### Added
9+
* add [`sort-default-props`] and deprecate [`jsx-sort-default-props`] ([#1861][] @alexzherdev)
10+
811
### Fixed
912
* [`no-unknown-property`]: add `dialog` attributes ([#3436][] @ljharb)
1013
* [`no-arrow-function-lifecycle`]: when converting from an arrow, remove the semi and wrapping parens ([#3337][] @ljharb)
1114
* [`jsx-key`]: Ignore elements inside `React.Children.toArray()` ([#1591][] @silvenon)
1215

1316
[#3436]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3436
1417
[#3337]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3337
18+
[#1861]: https://github.com/jsx-eslint/eslint-plugin-react/pull/1861
1519
[#1591]: https://github.com/jsx-eslint/eslint-plugin-react/pull/1591
1620

1721
## [7.31.8] - 2022.09.08
@@ -4000,6 +4004,7 @@ If you're still not using React 15 you can keep the old behavior by setting the
40004004
[`require-render-return`]: docs/rules/require-render-return.md
40014005
[`self-closing-comp`]: docs/rules/self-closing-comp.md
40024006
[`sort-comp`]: docs/rules/sort-comp.md
4007+
[`sort-default-props`]: docs/rules/sort-default-props.md
40034008
[`sort-prop-types`]: docs/rules/sort-prop-types.md
40044009
[`state-in-constructor`]: docs/rules/state-in-constructor.md
40054010
[`static-property-placement`]: docs/rules/static-property-placement.md

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ Enable the rules that you would like to use.
172172
|| | | [react/require-render-return](docs/rules/require-render-return.md) | Enforce ES5 or ES6 class for returning value in render function |
173173
| | 🔧 | | [react/self-closing-comp](docs/rules/self-closing-comp.md) | Disallow extra closing tags for components without children |
174174
| | | | [react/sort-comp](docs/rules/sort-comp.md) | Enforce component methods order |
175+
| | | | [react/sort-default-props](docs/rules/sort-default-props.md) | Enforce defaultProps declarations alphabetical sorting |
175176
| | | | [react/sort-prop-types](docs/rules/sort-prop-types.md) | Enforce propTypes declarations alphabetical sorting |
176177
| | | | [react/state-in-constructor](docs/rules/state-in-constructor.md) | Enforce class component state initialization style |
177178
| | | | [react/static-property-placement](docs/rules/static-property-placement.md) | Enforces where React component static properties should be positioned. |
@@ -216,7 +217,7 @@ Enable the rules that you would like to use.
216217
| | | | [react/jsx-pascal-case](docs/rules/jsx-pascal-case.md) | Enforce PascalCase for user-defined JSX components |
217218
| | 🔧 | | [react/jsx-props-no-multi-spaces](docs/rules/jsx-props-no-multi-spaces.md) | Disallow multiple spaces between inline JSX props |
218219
| | | | [react/jsx-props-no-spreading](docs/rules/jsx-props-no-spreading.md) | Disallow JSX prop spreading |
219-
| | | | [react/jsx-sort-default-props](docs/rules/jsx-sort-default-props.md) | Enforce defaultProps declarations alphabetical sorting |
220+
| | | | [react/jsx-sort-default-props](docs/rules/jsx-sort-default-props.md) | Enforce defaultProps declarations alphabetical sorting. ❌ This rule is deprecated. |
220221
| | 🔧 | | [react/jsx-sort-props](docs/rules/jsx-sort-props.md) | Enforce props alphabetical sorting |
221222
| | 🔧 | | [react/jsx-space-before-closing](docs/rules/jsx-space-before-closing.md) | Enforce spacing before closing bracket in JSX. ❌ This rule is deprecated. |
222223
| | 🔧 | | [react/jsx-tag-spacing](docs/rules/jsx-tag-spacing.md) | Enforce whitespace in and around the JSX opening and closing brackets |

docs/rules/jsx-sort-default-props.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Enforce defaultProps declarations alphabetical sorting (react/jsx-sort-default-props)
22

3+
❌ This rule is deprecated. Please use the ]`sort-default-props`](https://github.com/jsx-eslint/eslint-plugin-react/blob/HEAD/docs/rules/sort-default-props.md) rule instead.
4+
35
💼 This rule is enabled in the following [configs](https://github.com/jsx-eslint/eslint-plugin-react#shareable-configurations): `all`.
46

57
Some developers prefer to sort `defaultProps` declarations alphabetically to be able to find necessary declarations easier at a later time. Others feel that it adds complexity and becomes a burden to maintain.

docs/rules/sort-default-props.md

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Enforce defaultProps declarations alphabetical sorting (react/sort-default-props)
2+
3+
Some developers prefer to sort `defaultProps` declarations alphabetically to be able to find necessary declarations easier at a later time. Others feel that it adds complexity and becomes a burden to maintain.
4+
5+
## Rule Details
6+
7+
This rule checks all components and verifies that all `defaultProps` declarations are sorted alphabetically. A spread attribute resets the verification. The default configuration of the rule is case-sensitive.
8+
9+
The following patterns are considered warnings:
10+
11+
```jsx
12+
var Component = createReactClass({
13+
...
14+
getDefaultProps: function() {
15+
return {
16+
z: "z",
17+
a: "a",
18+
b: "b"
19+
};
20+
},
21+
...
22+
});
23+
24+
class Component extends React.Component {
25+
...
26+
}
27+
Component.defaultProps = {
28+
z: "z",
29+
a: "a",
30+
b: "b"
31+
};
32+
33+
class Component extends React.Component {
34+
static defaultProps = {
35+
z: "z",
36+
y: "y",
37+
a: "a"
38+
}
39+
render() {
40+
return <div />;
41+
}
42+
}
43+
44+
const Component = (props) => (...);
45+
Component.defaultProps = {
46+
z: "z",
47+
y: "y",
48+
a: "a"
49+
};
50+
51+
const defaults = {
52+
b: "b"
53+
};
54+
const types = {
55+
a: PropTypes.string,
56+
b: PropTypes.string,
57+
c: PropTypes.string'
58+
};
59+
function StatelessComponentWithSpreadInPropTypes({ a, b, c }) {
60+
return <div>{a}{b}{c}</div>;
61+
}
62+
StatelessComponentWithSpreadInPropTypes.propTypes = types;
63+
StatelessComponentWithSpreadInPropTypes.defaultProps = {
64+
c: "c",
65+
a: "a",
66+
...defaults,
67+
};
68+
69+
export default class ClassWithSpreadInPropTypes extends BaseClass {
70+
static propTypes = {
71+
a: PropTypes.string,
72+
b: PropTypes.string,
73+
c: PropTypes.string,
74+
d: PropTypes.string,
75+
e: PropTypes.string,
76+
f: PropTypes.string
77+
}
78+
static defaultProps = {
79+
b: "b",
80+
a: "a",
81+
...c.defaultProps,
82+
f: "f",
83+
e: "e",
84+
...d.defaultProps
85+
}
86+
}
87+
```
88+
89+
The following patterns are considered okay and do **not** cause warnings:
90+
91+
```jsx
92+
var Component = createReactClass({
93+
...
94+
getDefaultProps: function() {
95+
return {
96+
a: "a",
97+
b: "b",
98+
c: "c"
99+
};
100+
},
101+
...
102+
});
103+
104+
class Component extends React.Component {
105+
...
106+
}
107+
Component.defaultProps = {
108+
a: "a",
109+
b: "b",
110+
c: "c"
111+
};
112+
113+
class Component extends React.Component {
114+
static defaultProps = {
115+
a: PropTypes.any,
116+
b: PropTypes.any,
117+
c: PropTypes.any
118+
}
119+
render() {
120+
return <div />;
121+
}
122+
}
123+
124+
const Component = (props) => (...);
125+
Component.defaultProps = {
126+
a: "a",
127+
y: "y",
128+
z: "z"
129+
};
130+
131+
const defaults = {
132+
b: "b"
133+
};
134+
const types = {
135+
a: PropTypes.string,
136+
b: PropTypes.string,
137+
c: PropTypes.string'
138+
};
139+
function StatelessComponentWithSpreadInPropTypes({ a, b, c }) {
140+
return <div>{a}{b}{c}</div>;
141+
}
142+
StatelessComponentWithSpreadInPropTypes.propTypes = types;
143+
StatelessComponentWithSpreadInPropTypes.defaultProps = {
144+
a: "a",
145+
c: "c",
146+
...defaults,
147+
};
148+
149+
export default class ClassWithSpreadInPropTypes extends BaseClass {
150+
static propTypes = {
151+
a: PropTypes.string,
152+
b: PropTypes.string,
153+
c: PropTypes.string,
154+
d: PropTypes.string,
155+
e: PropTypes.string,
156+
f: PropTypes.string
157+
}
158+
static defaultProps = {
159+
a: "a",
160+
b: "b",
161+
...c.defaultProps,
162+
e: "e",
163+
f: "f",
164+
...d.defaultProps
165+
}
166+
}
167+
```
168+
169+
## Rule Options
170+
171+
```js
172+
...
173+
"react/sort-default-props": [<enabled>, {
174+
"ignoreCase": <boolean>,
175+
}]
176+
...
177+
```
178+
179+
### `ignoreCase`
180+
181+
When `true` the rule ignores the case-sensitivity of the declarations order.
182+
183+
## When not to use
184+
185+
This rule is a formatting preference and not following it won't negatively affect the quality of your code. If alphabetizing `defaultProps` declarations isn't a part of your coding standards, then you can leave this rule off.

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const allRules = {
9898
'require-render-return': require('./lib/rules/require-render-return'),
9999
'self-closing-comp': require('./lib/rules/self-closing-comp'),
100100
'sort-comp': require('./lib/rules/sort-comp'),
101+
'sort-default-props': require('./lib/rules/sort-default-props'),
101102
'sort-prop-types': require('./lib/rules/sort-prop-types'),
102103
'state-in-constructor': require('./lib/rules/state-in-constructor'),
103104
'static-property-placement': require('./lib/rules/static-property-placement'),

lib/rules/jsx-sort-default-props.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
/**
22
* @fileoverview Enforce default props alphabetical sorting
33
* @author Vladimir Kattsov
4+
* @deprecated
45
*/
56

67
'use strict';
78

89
const variableUtil = require('../util/variable');
910
const docsUrl = require('../util/docsUrl');
1011
const report = require('../util/report');
12+
const log = require('../util/log');
13+
14+
let isWarnedForDeprecation = false;
1115

1216
// ------------------------------------------------------------------------------
1317
// Rule Definition
@@ -19,6 +23,7 @@ const messages = {
1923

2024
module.exports = {
2125
meta: {
26+
deprecated: true,
2227
docs: {
2328
description: 'Enforce defaultProps declarations alphabetical sorting',
2429
category: 'Stylistic Issues',
@@ -168,6 +173,15 @@ module.exports = {
168173

169174
checkNode(node.parent.right);
170175
},
176+
177+
Program() {
178+
if (isWarnedForDeprecation) {
179+
return;
180+
}
181+
182+
log('The react/jsx-sort-default-props rule is deprecated. It has been renamed to `react/sort-default-props`.');
183+
isWarnedForDeprecation = true;
184+
},
171185
};
172186
},
173187
};

0 commit comments

Comments
 (0)