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

Add react/no-redundant-should-component-update #1084

Merged
merged 1 commit into from May 24, 2017

Conversation

jomasti
Copy link
Contributor

@jomasti jomasti commented Feb 22, 2017

This will check for shouldComponentUpdate defined in a class that extends React.PureComponent.

In regards to the name: I went with what is in the proposal, but perhaps something more informative can be found. Also, I realized that the functions dealing with component properties are duplicated in a few spots, but since there were some differences, I figured they could be refactored out at some other point. That's ultimately up to the reviewers, though.

This PR implements the rule proposed in #985.

```jsx
class Foo extends React.PureComponent {
shouldComponentUpdate() {
return true;
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 "return true" important? if not, i'd maybe put a comment here or something to indicate "any code can go here" (same throughout)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I was thinking this could come up when writing it. I'll update.

return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
}

return node.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.

does node always have a key 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.

From what I can see, yes. It's only ever checking properties from the class. Unless there's some other possible node that I'm not aware of for a class.

Copy link
Member

Choose a reason for hiding this comment

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

Might there be one in the future? It would probably be good to be defensible here.

if (utils.isPureComponent(node)) {
var hasScu = hasShouldComponentUpdate(node);
if (hasScu) {
var className = node.id.name;
Copy link
Member

Choose a reason for hiding this comment

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

does node always have an id 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.

I'll update this.

@lencioni
Copy link
Collaborator

I think "redundant" might be more specific than "useless". What do folks think?

Also, I'm not sure if we should abbreviate shouldComponentUpdate as scu. I think it might be best if the rule was named react/no-redundant-should-component-update.

@ljharb
Copy link
Member

ljharb commented Feb 22, 2017

I agree with @lencioni's naming comments.

@jomasti
Copy link
Contributor Author

jomasti commented Feb 23, 2017

I've updated the code based on the feedback given. I'll squash at the end after everything checks out.

@ljharb ljharb changed the title Add react/no-useless-scu rule Add react/no-redundant-should-component-update Feb 23, 2017
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.

Should this rule obey the React version specified by the user? In other words, for 0.14 and 0.13, should it be a noop?

This will check for `shouldComponentUpdate` defined in a class that
extends React.PureComponent.
@ljharb
Copy link
Member

ljharb commented May 23, 2017

@jomasti what do you think about my v14/v13/v15.0/v15.1 question?

@jomasti
Copy link
Contributor Author

jomasti commented May 23, 2017

@ljharb It makes sense. Would you expect a test with settings for a lower version? All I can think of would be using the syntax with a lower version and having it not catch the error. It doesn't seem useful, though.

@ljharb
Copy link
Member

ljharb commented May 24, 2017

@jomasti hm, that's a fair point - considering it'd just "do nothing" in a React where PureComponent also does nothing, it's probably not worth doing.

@ljharb
Copy link
Member

ljharb commented May 24, 2017

I'll merge this tomorrow if there's no objections.

@ljharb ljharb merged commit 7ebcd48 into jsx-eslint:master May 24, 2017
@jomasti jomasti deleted the no-useless-scu branch May 24, 2017 19:25
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