-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add fixer for sort-prop-types #1891
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 test cases that use spreading, to ensure that nothing is ever sorted across a spread boundary.
In other words, d, b, ...c, e, a
should become b, d, ...c, a, e
, and nothing should ever change position with respect to c
(due to overriding).
(Note that this kind of problem is why autofixers for any sorting of anything is prohibitively difficult)
@ljharb Yeah, I noticed earlier that this is an issue. I added an additional testcase for this and adapted the fixing code to make sure this doesn't happen. I also ended up adapting the fixing code to also work with the additional options as well. |
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 - i wish this didn’t have to use recursion, but if it works it works
lib/rules/sort-prop-types.js
Outdated
@@ -83,6 +85,78 @@ module.exports = { | |||
return; | |||
} | |||
|
|||
function fix(fixer) { | |||
function sortInSource (allNodes, source) { |
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.
no space after the function name
lib/rules/sort-prop-types.js
Outdated
}, [[]]); | ||
|
||
nodeGroups.forEach(nodes => { | ||
const sortedAttributes = nodes.slice(0).sort((a, b) => { |
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.
This sorting function should probably be defined at file level, since it’s pure?
lib/rules/sort-prop-types.js
Outdated
); | ||
sortedAttrText = attrSource.slice(sortedAttr.range[0], sortedAttr.range[1]); | ||
} | ||
source = `${source.substr(0, attr.range[0])}${sortedAttrText}${source.substr(attr.range[1])}`; |
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 use slice over substring/substr
@ljharb Thanks for the review, I incorporated your feedback. |
lib/rules/sort-prop-types.js
Outdated
let sortedAttrText = sourceCode.getText(sortedAttr); | ||
if (sortShapeProp && isShapeProp(sortedAttr.value)) { | ||
const attrSource = sortInSource( | ||
sortedAttr.value.arguments[0].properties, |
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.
does isShapeProp ensure that the value has a non-empty arguments? What about PropTypes.shape()
?
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.
Good point, I added test cases for this. Also there was already a bug that cause the linting to fail on PropTypes.shape()
. I included this fix: #1842
lib/rules/sort-prop-types.js
Outdated
}, [[]]); | ||
|
||
nodeGroups.forEach(nodes => { | ||
const sortedAttributes = nodes.slice(0).sort(sorter); |
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.
nbd but .slice() works too
lib/rules/sort-prop-types.js
Outdated
|
||
const rangeStart = declarations[0].range[0]; | ||
const rangeEnd = declarations[declarations.length - 1].range[1]; | ||
return fixer.replaceTextRange([rangeStart, rangeEnd], source.substr(rangeStart, rangeEnd - rangeStart)); |
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 use slice over substr
@ljharb Good catch with |
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, I've rebased the PR; once tests pass, I'll merge.
This adds adds a fixer for the
react/sort-prop-types
rule.It fixes ordering issues including the
sortShapeProp
and theignoreCase
option.It doesn't work yet forcallbacksLast
orrequiredFirst
, but that could be added later. (Right now those errors would just be ignored)