-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
sort-comp: enforcing static lifecycle methods order #1795
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
Conversation
lib/rules/sort-comp.js
Outdated
@@ -172,7 +172,7 @@ module.exports = { | |||
} | |||
} | |||
|
|||
if (method.static) { | |||
if (method.static && method.name !== 'getDerivedStateFromProps') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to just add indexes.length === 0
or something like that here so all named methods would be caught here rather than just getDerivedStateFromProps
for future extensibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, checking for indexes.length === 0
will do the job. Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks
tests/lib/rules/sort-comp.js
Outdated
' constructor() {}', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the default parser should be sufficient here; please either add a duplicate test (one using default, one using babel-eslint) or, since this is all ES6 syntax, just have the one using the default parser.
Does this really make sense? Usually people group static methods together, and put them before the constructor since they are not related to a specific instance. |
Also it's impossible to remove something from the lifecycle list without hardcoding the whole list in my config (and thus having to maintain it whenever react adds new lifecycle methods)... |
This appears to have caused #1821. Any ideas as to how to fix that case? |
The way it is now, this is requiring us to put static methods in the middle of other methods. That can't be right, can it? It's breaking all our builds. |
Static method declarations should be on top but it's not the case for It was done that way because |
Fixes bug: #1793
Example of incorrect code with
"react/sort-comp": "error"
:Example of correct code with
"react/sort-comp": "error"
: