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

Support lifecycle methods with nextProps/prevProps in no-unused-prop-types #1232

Merged
merged 6 commits into from Jun 2, 2017

Conversation

jseminck
Copy link
Contributor

#1213 #1142 (comment)

This makes the no-unused-prop-types work when a prop is used in React lifecycle components other than componentWillReceiveProps using the "dot-operator". E.g:

shouldComponentUpdate(nextProps) {
   if (nextProps.foo) { // This will now be detected
       ...
  }

There is one thing to keep in mind: under the current implementation the rule will only work if the parameter is named nextProps, prevProps or props. This logic already existed and I did not want to touch it, though I thought it was kind of odd. Do we want to keep it that way?

Perhaps we could introduce a rule that validates the parameter names in lifecycle methods - sometimes it's confusing anyway whether shouldComponentUpdate has prevProps or nextProps as the parameter, and what is the order of nextProps or nextState. Anyway, this would be for another issue.

Joachim Seminck added 2 commits May 30, 2017 15:29
The valid tests have two props, one is used in the render() function and the other in nextProps using
the dot operator in the folllowing lifecycle methods:

* shouldComponentUpdate
* componentWillUpdate
* componentDidUpdate (uses prevProps instead of nextProps).

The invalid tests are the same as the valid tests but without the render() method. In here there should be
an error that one of the props is unused, but there currently is no error.
* Extending the list of lifecycle methods
* Checking also for the `prevProps` parameter name

Currently for this rule to work, the user has to name the parameter either:

* nextProps
* prevProps
* props
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.

For each of the test cases using babel-eslint, could you make a duplicate test case that uses the default parser?

var scope = context.getScope();
while (scope) {
if (
scope.block && scope.block.parent &&
scope.block.parent.key && scope.block.parent.key.name === 'componentWillReceiveProps'
scope.block.parent.key &&
LIFE_CYCLE_METHODS.includes(scope.block.parent.key.name)
Copy link
Member

Choose a reason for hiding this comment

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

arrays don't have .includes until node 6; we need to use indexOf or https://npmjs.com/array-includes here.

@jseminck
Copy link
Contributor Author

jseminck commented Jun 2, 2017

Used .indexOf(). Thanks for that, I did not notice that or even think about it.

I also fixed the invalid test cases, as the lines and column values were incorrect. Hopefully CI should be green now. Sorry for the problems!

https://npmjs.com/array-includes would be nice to use, actually. There's a lot of places that use indexOf() and they all use it differently: !== -1, > -1, >= 0. However I am not comfortable making the decision of adding a new dependency.

@ljharb
Copy link
Member

ljharb commented Jun 2, 2017

Adding a new dep is fine imo, but it should definitely be in a separate PR :-)

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.

LGTM

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.

Looks good. Thanks a lot for these changes. This should address a lot of open issues right now.

@jseminck
Copy link
Contributor Author

jseminck commented Jun 3, 2017

Is there any change needed in the docs for this? It's for example important that the parameter names are correct (only works with nextProps, props and prevProps)

@ljharb
Copy link
Member

ljharb commented Jun 3, 2017

Sure, it's worth making an additional PR for docs.

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