-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add jsx-sort-default-props rule #1483
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
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.
Please add some SFC examples in the documentation and tests as well.
docs/rules/jsx-sort-default-props.md
Outdated
@@ -0,0 +1,97 @@ | |||
# Enforce propTypes declarations alphabetical sorting (react/jsx-sort-default-props) | |||
|
|||
Some developers prefer to sort propTypes declarations alphabetically to be able to find necessary declaration easier at the later time. Others feel that it adds complexity and becomes burden to maintain. |
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.
let's make sure all propTypes
and defaultProps
mentions are surrounded by backticks.
Also, "to be able to find necessary declarations easier at a later time" and "becomes a burden to maintain"
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.
Done! Should I fix these typos in sort-prop-types
and jsx-sort-props
documentation too? Their introduction are identical.
docs/rules/jsx-sort-default-props.md
Outdated
|
||
## Rule Details | ||
|
||
This rule checks all components and verifies that all defaultProps declarations are sorted alphabetically. A spread attribute resets the verification. The default configuration of the rule is case-sensitive. |
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.
Can you elaborate about the spread attribute?
I would expect that sorts do not cross a spread boundary, but d, a, ...obj, c, b
would be sorted into a, d, ...obj, b, c
Any advances? I have some spare time, may I help? |
@fsmaia sorry, haven't free time for a while. So help is welcome :) |
Added some SFC examples in the documentation and tests. Had to change the rule source, 'cause unnecessary/commented code was useful right in SFC scenario. |
@ljharb: Are we ready to merge? Any further work or change needed? Thanks |
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.
Can we add some good and bad examples in the docs that use spreads?
docs/rules/jsx-sort-default-props.md
Outdated
}; | ||
``` | ||
|
||
The following patterns are considered okay and do not cause warnings: |
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.
Is the wording of this sentence consistent with the other rules?
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.
Can we add some good and bad examples in the docs that use spreads? (submitted too soon)
Also, a few more tests using spreads, including multiple interleaved ones?
Also, please rebase and force push this so as to remove merge commits. |
88fb212
to
4f00455
Compare
@ljharb do you mean something like this? const defaults = {
foo: "foo"
};
const types = {
foo: PropTypes.string,
bar: PropTypes.string
};
function MyStatelessComponent({ foo, bar }) {
return <div>{foo}{bar}</div>;
}
MyStatelessComponent.propTypes = types;
MyStatelessComponent.defaultProps = {
...defaults,
bar: "bar"
}; |
Yes, just like that. |
Added more tests with spreads and examples. |
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.
Seems good. Other collabs should review, tho.
Just curious, why not update the existing |
"sort-props" imo applies only to props, not to defaultProps - #281 (comment) |
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
Related to #281