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

Handle UpdateExpressions in no-direct-mutation-state #1387

Merged
merged 1 commit into from
Sep 11, 2017

Conversation

zpao
Copy link
Contributor

@zpao zpao commented Aug 21, 2017

This rule should also catch increment & decrement operators.

Fixes #1386

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 legit, pending comment. Thanks!

code: [
'class Hello extends React.Component {',
' constructor() {',
' this.state.foo = 1;',
Copy link
Member

Choose a reason for hiding this comment

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

Could we also add a test that makes ++ invalid inside the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh shoot, I must have missed adding that line checking another thing… Sorry.

I had this.state.foo++ right here as a valid case - we're in the constructor and while I think it's awkward, it's legal.

Copy link
Member

Choose a reason for hiding this comment

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

I don't see why it should be legal; it's mutating this.state after it's assigned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Assignment doesn't actually matter until we're out of the constructor method. That's when React takes over.

But that's also a tangential change to what I wanted to make here - I'm bringing ++ to parity with = (you have a test case immediately above this one showing that). If you want to change how you treat assignment as well, that's fine but I think you probably want to do that distinctly.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough.

Choose a reason for hiding this comment

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

This has been my favorite find in our codebase where we were assigning to state.

this.setState({
  foo: this.state.foo++
});

Copy link
Contributor

Choose a reason for hiding this comment

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

@TheSavior (cc: @zpao )

I actually have a PR proposing a rule catching all references to this.state used in at this.setState in #1374. Does this rule cover that as well? If not, I would be really happy for feedback.

This rule should also catch increment & decrement operators.
@zpao
Copy link
Contributor Author

zpao commented Sep 11, 2017

Do you need anything else from me here?

@ljharb ljharb merged commit ad26580 into jsx-eslint:master Sep 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants