Skip to content

Commit aad4a37

Browse files
authored
Merge pull request #1481 from jomasti/issue-1476
Add `sortShapeProp` option to sort-prop-types rule
2 parents 52c603e + 3ef908d commit aad4a37

File tree

3 files changed

+330
-4
lines changed

3 files changed

+330
-4
lines changed

docs/rules/sort-prop-types.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class Component extends React.Component {
8080
"callbacksLast": <boolean>,
8181
"ignoreCase": <boolean>,
8282
"requiredFirst": <boolean>,
83+
"sortShapeProp": <boolean>
8384
}]
8485
...
8586
```
@@ -120,6 +121,25 @@ var Component = createReactClass({
120121
});
121122
```
122123

124+
### `sortShapeProp`
125+
126+
When `true`, props defined in `PropTypes.shape` must be sorted via the same rules as the top-level props:
127+
128+
```js
129+
var Component = createReactClass({
130+
propTypes: {
131+
a: PropTypes.number,
132+
b: PropTypes.shape({
133+
d: PropTypes.number,
134+
e: PropTypes.func,
135+
f: PropTypes.bool,
136+
}),
137+
c: PropTypes.string,
138+
},
139+
...
140+
});
141+
```
142+
123143
## When not to use
124144

125145
This rule is a formatting preference and not following it won't negatively affect the quality of your code. If alphabetizing props declarations isn't a part of your coding standards, then you can leave this rule off.

lib/rules/sort-prop-types.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ module.exports = {
2828
},
2929
ignoreCase: {
3030
type: 'boolean'
31+
},
32+
sortShapeProp: {
33+
type: 'boolean'
3134
}
3235
},
3336
additionalProperties: false
@@ -40,6 +43,7 @@ module.exports = {
4043
const requiredFirst = configuration.requiredFirst || false;
4144
const callbacksLast = configuration.callbacksLast || false;
4245
const ignoreCase = configuration.ignoreCase || false;
46+
const sortShapeProp = configuration.sortShapeProp || false;
4347
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
4448

4549
/**
@@ -78,6 +82,12 @@ module.exports = {
7882
return getValueName(node) === 'isRequired';
7983
}
8084

85+
function isShapeProp(node) {
86+
return Boolean(
87+
node && node.callee && node.callee.property && node.callee.property.name === 'shape'
88+
);
89+
}
90+
8191
/**
8292
* Find a variable by name in the current scope.
8393
* @param {string} name Name of the variable to look for.
@@ -185,6 +195,13 @@ module.exports = {
185195
}
186196

187197
return {
198+
CallExpression: function(node) {
199+
if (!sortShapeProp || !isShapeProp(node) || (!node.arguments && !node.arguments[0])) {
200+
return;
201+
}
202+
checkSorted(node.arguments[0].properties);
203+
},
204+
188205
ClassProperty: function(node) {
189206
if (!isPropTypesDeclaration(node)) {
190207
return;

0 commit comments

Comments
 (0)