Skip to content

Commit c62dccf

Browse files
authored
Merge pull request #979 from markus101/jsx-sort-props-no-sort-alphabetically
jsx-sort-props noSortAlphabetically option
2 parents 4df3bab + 340b93c commit c62dccf

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

docs/rules/jsx-sort-props.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ The following patterns are considered okay and do not cause warnings:
2727
"callbacksLast": <boolean>,
2828
"shorthandFirst": <boolean>,
2929
"shorthandLast": <boolean>,
30-
"ignoreCase": <boolean>
30+
"ignoreCase": <boolean>,
31+
"noSortAlphabetically": <boolean>
3132
}]
3233
...
3334
```
@@ -66,6 +67,14 @@ When `true`, short hand props must be listed after all other props (unless `call
6667
<Hello name="John" tel={5555555} active validate />
6768
```
6869

70+
### `noSortAlphabetically`
71+
72+
When `true`, alphabetical order is not enforced:
73+
74+
```js
75+
<Hello tel={5555555} name="John" />
76+
```
77+
6978
## When not to use
7079

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

lib/rules/jsx-sort-props.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ module.exports = {
4040
},
4141
ignoreCase: {
4242
type: 'boolean'
43+
},
44+
// Whether alphabetical sorting should be enforced
45+
noSortAlphabetically: {
46+
type: 'boolean'
4347
}
4448
},
4549
additionalProperties: false
@@ -53,6 +57,7 @@ module.exports = {
5357
var callbacksLast = configuration.callbacksLast || false;
5458
var shorthandFirst = configuration.shorthandFirst || false;
5559
var shorthandLast = configuration.shorthandLast || false;
60+
var noSortAlphabetically = configuration.noSortAlphabetically || false;
5661

5762
return {
5863
JSXOpeningElement: function(node) {
@@ -114,7 +119,7 @@ module.exports = {
114119
}
115120
}
116121

117-
if (currentPropName < previousPropName) {
122+
if (!noSortAlphabetically && currentPropName < previousPropName) {
118123
context.report({
119124
node: decl,
120125
message: 'Props should be sorted alphabetically'

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ var shorthandAndCallbackLastArgs = [{
5757
var ignoreCaseArgs = [{
5858
ignoreCase: true
5959
}];
60+
var noSortAlphabeticallyArgs = [{
61+
noSortAlphabetically: true
62+
}];
63+
var sortAlphabeticallyArgs = [{
64+
noSortAlphabetically: false
65+
}];
6066

6167
ruleTester.run('jsx-sort-props', rule, {
6268
valid: [
@@ -84,7 +90,10 @@ ruleTester.run('jsx-sort-props', rule, {
8490
code: '<App a="a" b="b" x y z onBar onFoo />;',
8591
options: shorthandAndCallbackLastArgs,
8692
parserOptions: parserOptions
87-
}
93+
},
94+
// noSortAlphabetically
95+
{code: '<App a b />;', options: noSortAlphabeticallyArgs, parserOptions: parserOptions},
96+
{code: '<App b a />;', options: noSortAlphabeticallyArgs, parserOptions: parserOptions}
8897
],
8998
invalid: [
9099
{code: '<App b a />;', errors: [expectedError], parserOptions: parserOptions},
@@ -122,6 +131,7 @@ ruleTester.run('jsx-sort-props', rule, {
122131
errors: [shorthandAndCallbackLastArgs],
123132
options: shorthandLastArgs,
124133
parserOptions: parserOptions
125-
}
134+
},
135+
{code: '<App b a />;', errors: [expectedError], options: sortAlphabeticallyArgs, parserOptions: parserOptions}
126136
]
127137
});

0 commit comments

Comments
 (0)