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

New rule: Report usages of class methods besides render returning JSX #1580

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

jomasti
Copy link
Contributor

@jomasti jomasti commented Dec 2, 2017

I saw #578 and thought that the requested rule was useful. I personally don't like components that define extra render* methods that return JSX. It is a way to clean up the main render method, but the better choice is to move the logic to separate components. This rule will try to enforce that.

I wasn't sure about the rule name, so I'm open to a better name if it's offered.

Closes #578

@@ -303,6 +303,16 @@ function componentRule(rule, context) {
return calledOnReact;
},

isReturningArrayMap(node) {
Copy link
Member

Choose a reason for hiding this comment

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

i'm concerned about this approach; there's other things that have a .map method. How can we know this is an array?

'}'
].join('\n'),
errors: [{
message: '\'names\' is missing in props validation'
Copy link
Member

Choose a reason for hiding this comment

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

this is a good fix; but this should be able to go in a separate PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a by-product of checking map. Considering this request and your concern above, should I remove the map support for now with it being added later as an enhancement?

Copy link
Member

Choose a reason for hiding this comment

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

In the case of the prop-types rule, names isn't inside a map, so it should be caught regardless.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't have to do with the JSX being returned in map. The reason it's being caught now is because isReturningJSX wasn't considering functions with return statements that are calling Array#map. So, in order to have this test case have an error, it required the same change that was made to check for returning Array#map in a component class method.

Copy link
Member

Choose a reason for hiding this comment

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

Right - I'm saying that the bug fix for prop-types can be to consider these return statements, even if it ignores the code inside the .map callback.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, so in order to still support the Array#map support for this rule, you want me to find a solution that makes it work while also not resolving the issue with that particular test case?

Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking that the prop-types issue can be solved without having to guess that .map is "array map".

Then separately, this rule may be able to avoid making the same guess.

@jseminck
Copy link
Contributor

jseminck commented Dec 4, 2017

I like this rule also.

For reference, I have a PR that adds similar functionality to the existing prefer-stateless-function rule, but there were a few cases that I didn't manage to catch: #1313

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.

Rule Proposal: functions returning JSX should be refactored into React components
3 participants