Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsx-sort-props noSortAlphabetically option #979

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be using localeCompare, not <, but that's out of scope for this PR

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}
]
});