Skip to content

Commit 8d775a6

Browse files
committed
Add sort-default-props and deprecate jsx-sort-default-props
Pt. 1 of jsx-eslint#1834
1 parent f27285f commit 8d775a6

File tree

7 files changed

+975
-0
lines changed

7 files changed

+975
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Enable the rules that you would like to use.
130130
* [react/require-render-return](docs/rules/require-render-return.md): Enforce ES5 or ES6 class for returning value in render function
131131
* [react/self-closing-comp](docs/rules/self-closing-comp.md): Prevent extra closing tags for components without children (fixable)
132132
* [react/sort-comp](docs/rules/sort-comp.md): Enforce component methods order (fixable)
133+
* [react/sort-default-props](docs/rules/sort-default-props.md): Enforce default props alphabetical sorting
133134
* [react/sort-prop-types](docs/rules/sort-prop-types.md): Enforce propTypes declarations alphabetical sorting
134135
* [react/style-prop-object](docs/rules/style-prop-object.md): Enforce style prop value being an object
135136
* [react/void-dom-elements-no-children](docs/rules/void-dom-elements-no-children.md): Prevent void DOM elements (e.g. `<img />`, `<br />`) from receiving children

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+
**Deprecation notice**: This rule is deprecated. It has been renamed to [sort-default-props](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-default-props.md).
4+
35
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.
46

57
## Rule Details

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
@@ -77,6 +77,7 @@ const allRules = {
7777
'require-render-return': require('./lib/rules/require-render-return'),
7878
'self-closing-comp': require('./lib/rules/self-closing-comp'),
7979
'sort-comp': require('./lib/rules/sort-comp'),
80+
'sort-default-props': require('./lib/rules/sort-default-props'),
8081
'sort-prop-types': require('./lib/rules/sort-prop-types'),
8182
'style-prop-object': require('./lib/rules/style-prop-object'),
8283
'void-dom-elements-no-children': require('./lib/rules/void-dom-elements-no-children')

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

+15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
/**
22
* @fileoverview Enforce default props alphabetical sorting
33
* @author Vladimir Kattsov
4+
* @deprecated
45
*/
56
'use strict';
67

78
const variableUtil = require('../util/variable');
9+
const log = require('../util/log');
10+
11+
let isWarnedForDeprecation = false;
812

913
// ------------------------------------------------------------------------------
1014
// Rule Definition
1115
// ------------------------------------------------------------------------------
1216

1317
module.exports = {
1418
meta: {
19+
deprecated: true,
1520
docs: {
1621
description: 'Enforce default props alphabetical sorting',
1722
category: 'Stylistic Issues',
@@ -160,6 +165,16 @@ module.exports = {
160165
}
161166

162167
checkNode(node.parent.right);
168+
},
169+
170+
Program: function() {
171+
if (isWarnedForDeprecation) {
172+
return;
173+
}
174+
175+
log('The react/jsx-sort-default-props rule is deprecated. ' +
176+
'It has been renamed to react/sort-default-props.');
177+
isWarnedForDeprecation = true;
163178
}
164179
};
165180
}

0 commit comments

Comments
 (0)