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

[Bug]: Fixer of sort-prop-types doesn’t respect noSortAlphabetically #3591

Closed
2 tasks done
kripod opened this issue Jun 26, 2023 · 2 comments · Fixed by #3609 or #3610
Closed
2 tasks done

[Bug]: Fixer of sort-prop-types doesn’t respect noSortAlphabetically #3591

kripod opened this issue Jun 26, 2023 · 2 comments · Fixed by #3609 or #3610

Comments

@kripod
Copy link

kripod commented Jun 26, 2023

Is there an existing issue for this?

  • I have searched the existing issues and my issue is unique
  • My issue appears in the command-line and not only in the text editor

Description Overview

Unlike with jsx-sort-props, the sort-prop-types rule doesn’t respect the { noSortAlphabetically: true } option passed when fixing the order of prop types.

When "react/sort-prop-types": ["warn", { callbacksLast: true, noSortAlphabetically: true }] is set, the example below:

AccordionItem.propTypes = {
  open: PropTypes.bool,
  onClick: PropTypes.func,
  id: PropTypes.string,
};

Gets fixed as follows:

AccordionItem.propTypes = {
  id: PropTypes.string,
  open: PropTypes.bool,
  onClick: PropTypes.func,
};

While the underlying callbacksLast issue is fixed, it appears that noSortAlphabetically isn’t respected by the fixer, just as seen in the source:

The fixPropTypesSort method doesn’t take a noSortAlphabetically parameter.

Expected Behavior

The correct fix for the snippet above would be:

AccordionItem.propTypes = {
  open: PropTypes.bool,
  id: PropTypes.string,
  onClick: PropTypes.func,
};

Only onClick should be moved to the bottom, leaving the order of non-callback props intact.

eslint-plugin-react version

7.32.2

eslint version

v8.43.0

node version

v16.20.0

@caesar1030
Copy link
Contributor

caesar1030 commented Jul 11, 2023

@kripod @ljharb I’d love to work on this

@ljharb
Copy link
Member

ljharb commented Jul 11, 2023

@caesar1030 go for it, you can just put up the PR, no need to comment :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment