Skip to content
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

Add fixer for sort-prop-types #1891

Merged
merged 2 commits into from Jul 21, 2018
Merged

Add fixer for sort-prop-types #1891

merged 2 commits into from Jul 21, 2018

Conversation

finnp
Copy link
Contributor

@finnp finnp commented Jul 20, 2018

This adds adds a fixer for the react/sort-prop-types rule.

It fixes ordering issues including the sortShapeProp and the ignoreCase option.

It doesn't work yet for callbacksLast or requiredFirst, but that could be added later. (Right now those errors would just be ignored)

Copy link
Member

@ljharb ljharb left a 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)

@finnp
Copy link
Contributor Author

finnp commented Jul 21, 2018

@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.

Copy link
Member

@ljharb ljharb left a 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

@@ -83,6 +85,78 @@ module.exports = {
return;
}

function fix(fixer) {
function sortInSource (allNodes, source) {
Copy link
Member

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

}, [[]]);

nodeGroups.forEach(nodes => {
const sortedAttributes = nodes.slice(0).sort((a, b) => {
Copy link
Member

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?

);
sortedAttrText = attrSource.slice(sortedAttr.range[0], sortedAttr.range[1]);
}
source = `${source.substr(0, attr.range[0])}${sortedAttrText}${source.substr(attr.range[1])}`;
Copy link
Member

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

@finnp
Copy link
Contributor Author

finnp commented Jul 21, 2018

@ljharb Thanks for the review, I incorporated your feedback.

let sortedAttrText = sourceCode.getText(sortedAttr);
if (sortShapeProp && isShapeProp(sortedAttr.value)) {
const attrSource = sortInSource(
sortedAttr.value.arguments[0].properties,
Copy link
Member

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()?

Copy link
Contributor Author

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

}, [[]]);

nodeGroups.forEach(nodes => {
const sortedAttributes = nodes.slice(0).sort(sorter);
Copy link
Member

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


const rangeStart = declarations[0].range[0];
const rangeEnd = declarations[declarations.length - 1].range[1];
return fixer.replaceTextRange([rangeStart, rangeEnd], source.substr(rangeStart, rangeEnd - rangeStart));
Copy link
Member

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

@finnp
Copy link
Contributor Author

finnp commented Jul 21, 2018

@ljharb Good catch with PropTypes.shape. I added test cases and a fix.

Copy link
Member

@ljharb ljharb left a 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.

@ljharb ljharb merged commit a0100f3 into jsx-eslint:master Jul 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants