Skip to content

Feature sort alpabetically props option #789

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

Closed
wants to merge 2 commits into from
Closed
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
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [6.2.0] - 2016-08-28
### Added
* Adds `no-unused-prop-types` rule ([#226][] @EvNaverniouk)
* Add `style-prop-object` rule ([#715][] @petersendidit)
* Add auto fix for `self-closing-comp` ([#770][] @pl12133)
* Add support for `typeAnnotations` in `sort-comp` ([#235][] @dozoisch)
* Add support for `PureComponent` in `prefer-stateless-function` ([#781][] @tiemevanveen)

## Fixed
* Fix `jsx-uses-vars` to work better with `prefer-const`. You'll need to upgrade to ESLint 3.4.0 to completely fix the compatibility issue ([#716][])
* Fix `require-render-return` crash ([#784][])
* Fix related components detection in `prop-types` ([#735][])
* Fix component detection to ignore functions expression without a parent component

### Changed
* Update dependencies
* Documentation improvements (@lencioni)

[6.2.0]: https://github.com/yannickcr/eslint-plugin-react/compare/v6.1.2...v6.2.0
[#226]: https://github.com/yannickcr/eslint-plugin-react/issues/226
[#715]: https://github.com/yannickcr/eslint-plugin-react/issues/715
[#770]: https://github.com/yannickcr/eslint-plugin-react/pull/770
[#235]: https://github.com/yannickcr/eslint-plugin-react/issues/235
[#781]: https://github.com/yannickcr/eslint-plugin-react/pull/781
[#716]: https://github.com/yannickcr/eslint-plugin-react/issues/716
[#784]: https://github.com/yannickcr/eslint-plugin-react/issues/784
[#735]: https://github.com/yannickcr/eslint-plugin-react/issues/735

## [6.1.2] - 2016-08-17
### Fixed
* Fix nested spread handling in `no-danger-with-children` ([#771][] @petersendidit)
Expand Down
16 changes: 16 additions & 0 deletions docs/rules/sort-prop-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Component extends React.Component {
"callbacksLast": <boolean>,
"ignoreCase": <boolean>,
"requiredFirst": <boolean>,
"noSortAlphabetically": <boolean>,
}]
...
```
Expand Down Expand Up @@ -120,6 +121,21 @@ var Component = React.createClass({
});
```

### `noSortAlphabetically`

When `true`, prop types will not be sorted alphabetically

```js
var Component = React.createClass({
propTypes: {
b: React.PropTypes.bool,
a: React.PropTypes.number,
z: React.PropTypes.string,
},
...
});
```

## 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 declarations isn't a part of your coding standards, then you can leave this rule off.
18 changes: 12 additions & 6 deletions lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ module.exports = {
},
ignoreCase: {
type: 'boolean'
},
noSortAlphabetically: {
type: 'boolean'
}
},
additionalProperties: false
Expand All @@ -39,6 +42,7 @@ module.exports = {
var requiredFirst = configuration.requiredFirst || false;
var callbacksLast = configuration.callbacksLast || false;
var ignoreCase = configuration.ignoreCase || false;
var noSortAlphabetically = configuration.noSortAlphabetically || false;

/**
* Checks if node is `propTypes` declaration
Expand Down Expand Up @@ -130,12 +134,14 @@ module.exports = {
}
}

if (currentPropName < prevPropName) {
context.report({
node: curr,
message: 'Prop types declarations should be sorted alphabetically'
});
return prev;
if (!noSortAlphabetically) {
if (currentPropName < prevPropName) {
context.report({
node: curr,
message: 'Prop types declarations should be sorted alphabetically'
});
return prev;
}
}

return curr;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-react",
"version": "6.1.2",
"version": "6.2.0",
"author": "Yannick Croissant <[email protected]>",
"description": "React specific linting rules for ESLint",
"main": "index.js",
Expand Down
47 changes: 47 additions & 0 deletions tests/lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,53 @@ ruleTester.run('sort-prop-types', rule, {
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'export default class ClassWithSpreadInPropTypes extends BaseClass {',
' static propTypes = {',
' z: PropTypes.string,',
' b: PropTypes.func,',
' a: PropTypes.string,',
' }',
'}'
].join('\n'),
options: [{
noSortAlphabetically: true
}],
parser: 'babel-eslint'
}, {
code: [
'export default class ClassWithSpreadInPropTypes extends BaseClass {',
' static propTypes = {',
' z: PropTypes.string.isRequired,',
' b: PropTypes.func,',
' a: PropTypes.string,',
' }',
'}'
].join('\n'),
options: [{
noSortAlphabetically: true,
requiredFirst: true
}],
parser: 'babel-eslint'
}, {
code: [
'export default class ClassWithSpreadInPropTypes extends BaseClass {',
' static propTypes = {',
' l: PropTypes.bool.isRequired,',
' k: PropTypes.string.isRequired,',
' g: PropTypes.string,',
' b: PropTypes.bool,',
' a: PropTypes.func,',
' }',
'}'
].join('\n'),
options: [{
noSortAlphabetically: true,
requiredFirst: true,
callbacksLast: true
}],
parser: 'babel-eslint'
}],

invalid: [{
Expand Down