Skip to content

Commit

Permalink
jsx-sort-props noSortAlphabetically option
Browse files Browse the repository at this point in the history
Closes #541
Closes #786
  • Loading branch information
markus101 committed Nov 25, 2016
1 parent 0a40c27 commit 340b93c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
11 changes: 10 additions & 1 deletion docs/rules/jsx-sort-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ The following patterns are considered okay and do not cause warnings:
"callbacksLast": <boolean>,
"shorthandFirst": <boolean>,
"shorthandLast": <boolean>,
"ignoreCase": <boolean>
"ignoreCase": <boolean>,
"noSortAlphabetically": <boolean>
}]
...
```
Expand Down Expand Up @@ -66,6 +67,14 @@ When `true`, short hand props must be listed after all other props (unless `call
<Hello name="John" tel={5555555} active validate />
```

### `noSortAlphabetically`

When `true`, alphabetical order is not enforced:

```js
<Hello tel={5555555} name="John" />
```

## When not to use

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.
7 changes: 6 additions & 1 deletion lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ module.exports = {
},
ignoreCase: {
type: 'boolean'
},
// Whether alphabetical sorting should be enforced
noSortAlphabetically: {
type: 'boolean'
}
},
additionalProperties: false
Expand All @@ -53,6 +57,7 @@ module.exports = {
var callbacksLast = configuration.callbacksLast || false;
var shorthandFirst = configuration.shorthandFirst || false;
var shorthandLast = configuration.shorthandLast || false;
var noSortAlphabetically = configuration.noSortAlphabetically || false;

return {
JSXOpeningElement: function(node) {
Expand Down Expand Up @@ -114,7 +119,7 @@ module.exports = {
}
}

if (currentPropName < previousPropName) {
if (!noSortAlphabetically && currentPropName < previousPropName) {
context.report({
node: decl,
message: 'Props should be sorted alphabetically'
Expand Down
14 changes: 12 additions & 2 deletions tests/lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ var shorthandAndCallbackLastArgs = [{
var ignoreCaseArgs = [{
ignoreCase: true
}];
var noSortAlphabeticallyArgs = [{
noSortAlphabetically: true
}];
var sortAlphabeticallyArgs = [{
noSortAlphabetically: false
}];

ruleTester.run('jsx-sort-props', rule, {
valid: [
Expand Down Expand Up @@ -84,7 +90,10 @@ ruleTester.run('jsx-sort-props', rule, {
code: '<App a="a" b="b" x y z onBar onFoo />;',
options: shorthandAndCallbackLastArgs,
parserOptions: parserOptions
}
},
// noSortAlphabetically
{code: '<App a b />;', options: noSortAlphabeticallyArgs, parserOptions: parserOptions},
{code: '<App b a />;', options: noSortAlphabeticallyArgs, parserOptions: parserOptions}
],
invalid: [
{code: '<App b a />;', errors: [expectedError], parserOptions: parserOptions},
Expand Down Expand Up @@ -122,6 +131,7 @@ ruleTester.run('jsx-sort-props', rule, {
errors: [shorthandAndCallbackLastArgs],
options: shorthandLastArgs,
parserOptions: parserOptions
}
},
{code: '<App b a />;', errors: [expectedError], options: sortAlphabeticallyArgs, parserOptions: parserOptions}
]
});

0 comments on commit 340b93c

Please sign in to comment.