Skip to content

fix(jsx-sort-props): enforce prop sort when reservedFirst is enabled #1340

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

Merged
merged 1 commit into from
Nov 6, 2017
Merged

fix(jsx-sort-props): enforce prop sort when reservedFirst is enabled #1340

merged 1 commit into from
Nov 6, 2017

Conversation

tagoro9
Copy link
Contributor

@tagoro9 tagoro9 commented Aug 3, 2017

If reservedFirst is enabled and noSortAlphabetically is off, then alphabetical order is only enforced in the reserved props but not in the other props. This PR fixes this.

If existing behavior is the desired, please ignore this PR.

Fixes #1320

If reservedFirst is enabled and noSortAlphabetically is off, then
alphabetical order is only enforced in the reserved props but not
in the other props.

* Update generateFixerFunction to accept the reservedList.
* Rename alphabeticalCompare to be propNameCompare. Accept a
reservedFirst and reservedList parameters in order to be able
to compare prop names according to the configuration.
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.

I've just merged a PR that adds an autofixer for sort-props, can you add expected output to these three tests?

code: '<App dangerouslySetInnerHTML={{__html: "EPR"}} key={2} b />',
options: reservedFirstAsBooleanArgs,
errors: [expectedReservedFirstError]
errors: [expectedReservedFirstError, expectedError]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would this suddenly start giving two errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that after the changes this is triggering the expectedError as the || (!previousIsReserved && !currentIsReserved) check and it is true when evaluating dangerouslySetInnerHTML and b.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should try to avoid generating the extra error.

@tagoro9
Copy link
Contributor Author

tagoro9 commented Aug 7, 2017

I updated the tests with the expected output, but I the fix is not taking into account the reserved words:

Expected: <App key={2} b dangerouslySetInnerHTML={{__html: "EPR"}} />
Actual : <App b dangerouslySetInnerHTML={{__html: "EPR"}} key={2} />

I'm gonna try to see if I can add a fix for this.

{
code: '<App b a />',
options: reservedFirstAsBooleanArgs,
errors: [expectedError]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add output: '<App a b />'

@@ -202,9 +202,19 @@ ruleTester.run('jsx-sort-props', rule, {
errors: [expectedError]
},
{
code: '<App key={2} b a />',
options: reservedFirstAsBooleanArgs,
errors: [expectedError]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add output: <App key={2} a b /> (assuming that's the expected output)

@tagoro9
Copy link
Contributor Author

tagoro9 commented Aug 11, 2017

Sorry I took so long to take a look at the change requests.

  • This test is no longer expecting 2 errors.
  • Added expected output for the new test cases.
  • With the expected output the tests were failing, so I updated the generateFixerFunction to take into account the reservedFirst config and sort props accordingly. Is this something wanted?

@ljharb
Copy link
Member

ljharb commented Aug 11, 2017

Definitely wanted, thanks :-)

code: '<App dangerouslySetInnerHTML={{__html: "EPR"}} key={2} b />',
options: reservedFirstAsBooleanArgs,
output: '<App key={2} b dangerouslySetInnerHTML={{__html: "EPR"}} />',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm - with reservedFirst tho, the expected output should be <App dangerouslySetInnerHTML={{__html: "EPR"}} key={2} b />, no?

Copy link
Contributor Author

@tagoro9 tagoro9 Aug 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think dangerouslySetInnerHTML is not a reserved prop name for this component. I think it is only considered reserved for DOM components and not react components. This code is deleting dangerouslySetInnerHTML from the reserved list for the cases where the component is not a DOM component.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhhh true! good call :-) could you add an "e" prop as well, so it's clear that it's being sorted as a regular property?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

@ljharb
Copy link
Member

ljharb commented Oct 1, 2017

Planning to merge this soon.

@tobyf93
Copy link

tobyf93 commented Nov 6, 2017

Any idea on when the merge will happen @ljharb?

@ljharb ljharb added the bug label Nov 6, 2017
@ljharb ljharb merged commit baa939c into jsx-eslint:master Nov 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

react/jsx-sort-props not working
3 participants