Skip to content

Commit 564107c

Browse files
authored
Merge pull request #1897 from christophehurpeau/patch-1
Rule sort-prop-types add noSortAlphabetically
2 parents 599c028 + 622470f commit 564107c

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

docs/rules/sort-prop-types.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class Component extends React.Component {
8383
"callbacksLast": <boolean>,
8484
"ignoreCase": <boolean>,
8585
"requiredFirst": <boolean>,
86-
"sortShapeProp": <boolean>
86+
"sortShapeProp": <boolean>,
87+
"noSortAlphabetically": <boolean>
8788
}]
8889
...
8990
```
@@ -142,6 +143,20 @@ var Component = createReactClass({
142143
...
143144
});
144145
```
146+
### `noSortAlphabetically`
147+
148+
When `true`, alphabetical order is not enforced:
149+
150+
```js
151+
var Component = createReactClass({
152+
propTypes: {
153+
barRequired: PropTypes.any.isRequired,
154+
z: PropTypes.string,
155+
a: PropTypes.number,
156+
},
157+
...
158+
});
159+
```
145160

146161
## When not to use
147162

lib/rules/sort-prop-types.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ module.exports = {
3434
ignoreCase: {
3535
type: 'boolean'
3636
},
37+
// Whether alphabetical sorting should be enforced
38+
noSortAlphabetically: {
39+
type: 'boolean'
40+
},
3741
sortShapeProp: {
3842
type: 'boolean'
3943
}
@@ -48,6 +52,7 @@ module.exports = {
4852
const requiredFirst = configuration.requiredFirst || false;
4953
const callbacksLast = configuration.callbacksLast || false;
5054
const ignoreCase = configuration.ignoreCase || false;
55+
const noSortAlphabetically = configuration.noSortAlphabetically || false;
5156
const sortShapeProp = configuration.sortShapeProp || false;
5257
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
5358

@@ -216,7 +221,7 @@ module.exports = {
216221
}
217222
}
218223

219-
if (currentPropName < prevPropName) {
224+
if (!noSortAlphabetically && currentPropName < prevPropName) {
220225
context.report({
221226
node: curr,
222227
message: 'Prop types declarations should be sorted alphabetically',

tests/lib/rules/sort-prop-types.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,36 @@ ruleTester.run('sort-prop-types', rule, {
382382
options: [{
383383
sortShapeProp: true
384384
}]
385+
}, {
386+
code: `
387+
class Component extends React.Component {
388+
render() {
389+
return <div />;
390+
}
391+
}
392+
Component.propTypes = {
393+
a: PropTypes.any,
394+
z: PropTypes.any,
395+
};
396+
`,
397+
options: [{
398+
noSortAlphabetically: true
399+
}]
400+
}, {
401+
code: `
402+
class Component extends React.Component {
403+
render() {
404+
return <div />;
405+
}
406+
}
407+
Component.propTypes = {
408+
z: PropTypes.any,
409+
a: PropTypes.any,
410+
};
411+
`,
412+
options: [{
413+
noSortAlphabetically: true
414+
}]
385415
}],
386416

387417
invalid: [{
@@ -1471,5 +1501,37 @@ ruleTester.run('sort-prop-types', rule, {
14711501
}
14721502
}
14731503
`
1504+
}, {
1505+
code: [
1506+
'var First = createReactClass({',
1507+
' propTypes: {',
1508+
' z: PropTypes.string,',
1509+
' a: PropTypes.any',
1510+
' },',
1511+
' render: function() {',
1512+
' return <div />;',
1513+
' }',
1514+
'});'
1515+
].join('\n'),
1516+
options: [{
1517+
noSortAlphabetically: false
1518+
}],
1519+
errors: [{
1520+
message: ERROR_MESSAGE,
1521+
line: 4,
1522+
column: 5,
1523+
type: 'Property'
1524+
}],
1525+
output: [
1526+
'var First = createReactClass({',
1527+
' propTypes: {',
1528+
' a: PropTypes.any,',
1529+
' z: PropTypes.string',
1530+
' },',
1531+
' render: function() {',
1532+
' return <div />;',
1533+
' }',
1534+
'});'
1535+
].join('\n')
14741536
}]
14751537
});

0 commit comments

Comments
 (0)