Skip to content

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

Merged
merged 6 commits into from
Jan 16, 2018
Merged

Conversation

b0gok
Copy link
Contributor

@b0gok b0gok commented Oct 16, 2017

Related to #281

  • Add support for
var First = createReactClass({
  getDefaultProps: function() {
    return {
      barRequired: "barRequired",
      onBar: "onBar"
    };
  }
});
  • Add more tests
  • Remove useless code

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 some SFC examples in the documentation and tests as well.

@@ -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.
Copy link
Member

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"

Copy link

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.


## 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.
Copy link
Member

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

@fsmaia
Copy link

fsmaia commented Nov 16, 2017

Any advances? I have some spare time, may I help?

@b0gok
Copy link
Contributor Author

b0gok commented Nov 16, 2017

@fsmaia sorry, haven't free time for a while. So help is welcome :)

@ljharb
Copy link
Member

ljharb commented Nov 17, 2017

@b0gok the ideal is for you to add @fsmaia to your fork, so they can update this PR directly - if not, then @fsmaia, please post here a link to your commits on your own fork, and I'll add them for you. Please do not open up a duplicate PR.

@b0gok
Copy link
Contributor Author

b0gok commented Nov 17, 2017

@ljharb ok.
@fsmaia I added you as collaborator in my fork.

@fsmaia
Copy link

fsmaia commented Nov 21, 2017

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.

@fsmaia
Copy link

fsmaia commented Dec 5, 2017

@ljharb: Are we ready to merge? Any further work or change needed? Thanks

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.

Can we add some good and bad examples in the docs that use spreads?

};
```

The following patterns are considered okay and do not cause warnings:
Copy link
Member

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?

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.

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?

@ljharb ljharb changed the title [WIP] Add jsx-sort-default-props rule Add jsx-sort-default-props rule Dec 5, 2017
@ljharb
Copy link
Member

ljharb commented Dec 5, 2017

Also, please rebase and force push this so as to remove merge commits.

@b0gok b0gok force-pushed the jsx-sort-default-props branch from 88fb212 to 4f00455 Compare January 11, 2018 20:26
@b0gok
Copy link
Contributor Author

b0gok commented Jan 11, 2018

Can we add some good and bad examples in the docs that use spreads?

@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"
};

@ljharb
Copy link
Member

ljharb commented Jan 12, 2018

Yes, just like that.

@b0gok
Copy link
Contributor Author

b0gok commented Jan 15, 2018

Added more tests with spreads and examples.

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.

Seems good. Other collabs should review, tho.

@EvHaus
Copy link
Collaborator

EvHaus commented Jan 15, 2018

Just curious, why not update the existing jsx-sort-props rule to sort defaultProps instead of create a separate rule? Is there a situations where you'd want proptypes sorted one way, but defaults sorted a different way?

@ljharb
Copy link
Member

ljharb commented Jan 15, 2018

"sort-props" imo applies only to props, not to defaultProps - #281 (comment)

Copy link
Collaborator

@EvHaus EvHaus left a comment

Choose a reason for hiding this comment

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

LGTM

@ljharb ljharb merged commit 36beb6d into jsx-eslint:master Jan 16, 2018
@b0gok b0gok deleted the jsx-sort-default-props branch January 16, 2018 16:16
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.

4 participants