-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from yannickcr/rule-sort-comp
Add sort-comp rule (fixes #39)
- Loading branch information
Showing
5 changed files
with
770 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
# Enforce component methods order (sort-comp) | ||
|
||
When creating React components it is more convenient to always follow the same organisation for methods order to helps you to easily find lifecyle methods, event handlers, etc. | ||
|
||
## Rule Details | ||
|
||
With default configuration the following organisation must be followed: | ||
|
||
1. lifecycle methods: `displayName` ,`propTypes` ,`mixins` ,`statics` ,`getDefaultProps` ,`getInitialState` ,`componentWillMount` ,`componentDidMount` ,`componentWillReceiveProps` ,`shouldComponentUpdate` ,`componentWillUpdate` ,`componentDidUpdate` ,`componentWillUnmount` (in this order). | ||
2. custom methods | ||
3. `render` method | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
render: function() { | ||
return <div>Hello</div>; | ||
}, | ||
displayName : 'Hello' | ||
}); | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
displayName : 'Hello', | ||
render: function() { | ||
return <div>Hello</div>; | ||
} | ||
}); | ||
``` | ||
|
||
## Rule Options | ||
|
||
This rule can take one argument to customize the components organisation. | ||
|
||
``` | ||
... | ||
"react/sort-comp": [<enabled>, { order: <order>, groups: <groups> }] | ||
... | ||
``` | ||
|
||
* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0. | ||
* `order`: optional array of methods to validate. | ||
* `groups`: optional object of methods groups. | ||
|
||
The default configuration is: | ||
|
||
```js | ||
{ | ||
order: [ | ||
'lifecycle', | ||
'everything-else', | ||
'render' | ||
], | ||
groups: { | ||
lifecycle: [ | ||
'displayName', | ||
'propTypes', | ||
'mixins', | ||
'statics', | ||
'getDefaultProps', | ||
'getInitialState', | ||
'componentWillMount', | ||
'componentDidMount', | ||
'componentWillReceiveProps', | ||
'shouldComponentUpdate', | ||
'componentWillUpdate', | ||
'componentDidUpdate', | ||
'componentWillUnmount' | ||
] | ||
} | ||
} | ||
``` | ||
|
||
* `lifecycle` is refering to the `lifecycle` group defined in `groups`. | ||
* `everything-else` is a special group that match all the methods that do not match any of the other groups. | ||
* `render` is refering to the `render` method. | ||
|
||
You can override this configuration to match your needs. | ||
|
||
For example, if you want to place your event handlers (`onClick`, `onSubmit`, etc.) before `render` but the other methods after it: | ||
|
||
```js | ||
"react/sort-comp": [1, { | ||
order: [ | ||
'lifecycle', | ||
'/^on.+$/', | ||
'render', | ||
'everything-else' | ||
] | ||
}] | ||
``` | ||
|
||
With the above configuration, the following patterns are considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
render: function() { | ||
return <div>Hello</div>; | ||
}, | ||
onClick: function() {} | ||
}); | ||
``` | ||
|
||
With the above configuration, the following patterns are not considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
onClick: function() {}, | ||
render: function() { | ||
return <div>Hello</div>; | ||
} | ||
}); | ||
``` | ||
|
||
If you want to split your `render` method into smaller ones and keep them just before render: | ||
|
||
```js | ||
"react/sort-comp": [1, { | ||
order: [ | ||
'lifecycle', | ||
'everything-else', | ||
'rendering', | ||
], | ||
groups: { | ||
rendering: [ | ||
'/^render.+$/', | ||
'render' | ||
] | ||
} | ||
}] | ||
``` | ||
|
||
With the above configuration, the following patterns are considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
renderButton: function() {}, | ||
onClick: function() {}, | ||
render: function() { | ||
return <div>Hello</div>; | ||
} | ||
}); | ||
``` | ||
|
||
With the above configuration, the following patterns are not considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
onClick: function() {}, | ||
renderButton: function() {}, | ||
render: function() { | ||
return <div>Hello</div>; | ||
} | ||
}); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
This rule is a formatting preference and not following it won't negatively affect the quality of your code. If components organisation isn't a part of your coding standards, then you can leave this rule off. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.