-
Notifications
You must be signed in to change notification settings - Fork 657
Add noDirectMutationState rule and tests #582
Conversation
ccfc975
to
cf5ece1
Compare
Currently unable to continue as this error is coming up when I run
|
Can you rebase and try again? #583 was merged that should resolve that error however it will uncover another that might be more actionable. |
I tried that, unfortunately no change. Its something to do with the noDirectMutationState.ts file. If I remove most of the file, it stops giving me that error. By most of the file, I mean leave just the barebones. So something in the file is causing the issue. |
@sebmck I think it has something to do with |
Did some looking, had some thoughts. fwiw if I just pull your branch I don't get the error. |
b35805f
to
3c8d797
Compare
The new commit (b2206e0) makes sure the node being considered is a react component. React components are defined by a class extending |
3c8d797
to
b2206e0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pr. I left a comment on removing the createReactClass test cases. That might simplify some checks in packages/@romejs/compiler/lint/rules/react/noDirectMutationState.ts
{ | ||
type: "log", | ||
category: "info", | ||
text: "Calling <emphasis>setState()</emphasis> after mutating <emphasis>this.state</emphasis> directly may replace the mutation you made.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think adding an additional "The only place to set this.setState directly is in the constructor of a react class component." is helpful.
{ | ||
invalid: [ | ||
` | ||
var Hello = createReactClass({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome on the extensive test cases! from a decision in previous discussions (#341 (comment) as one place), you don't need to account for the createReactClass
case. Sorry for the noise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops I missed that. Ill take those checks out. Ive changed the createReactClass tests as well and stripped a couple other repetitive ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WOW! Amazing PR with amazing comments!! Top notch!!!
f9e199a
to
5bc25e4
Compare
5bc25e4
to
24def7e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One small change requested.
When you get a chance to also squash your commits, that would be great
5 │ return <div>Hello {this.props.name}</div>; | ||
6 │ } | ||
|
||
ℹ Calling setState() after mutating this.state directly may replace the mutation you made. Theonly |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't from your changes, but I'm just noting how The only
turned in Theonly
. I'll make an issue after when I'm more clear on what is happening.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried something out and made this the new message
Calling <emphasis>setState()</emphasis> The only after mutating <emphasis>this.state</emphasis> directly may replace the mutation you made. The only place you may set <emphasis>this.state</emphasis> directly is in a constructor of a react class component.
I added The only
after the first emphasis tag. This is the result of that change.
ℹ Calling setState() The only after mutating this.state directly may replace the mutation youmade.
The only place you may set this.state directly is in a constructor of a react class component.
Its related to the last 2 words in that line being combined together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It tries to merge the last 2 words if the line is 97 characters long. If its 98 or more, it doesn't try it, but if it is exactly 97, it removes 1 space to shrink it to 96 characters. Is that length significant?
Here is another example showing that.
lint.ts
aCalling <emphasis>setState()</emphasis> after mutating <emphasis>this.state</emphasis> directly may replace the mutation you made. abc abc ab The only place you may set <emphasis>this.state</emphasis> directly is in a constructor of a react class component.
test.md
ℹ aCalling setState() after mutating this.state directly may replace the mutation you made. abcabc
ab The only place you may set this.state directly is in a constructor of a react classcomponent.
Notice the padding I added at the beginning of the lines and notice the abc abc
being merged in beginning of the second sentence, and the class component
being merged at the end of it. If I took out the letter a
at the beginning of each line, the abc abc
and class component
would not be merged.
// Check if it extends React.Component or Component | ||
node.type === "JSClassHead" && | ||
node.superClass !== undefined && | ||
(doesNodeMatchPattern(node.superClass, "React.Component") || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another component that is good to check is React.PureComponent
Formatting changes Included check for react components only createReactClass is not being tested anymore
24def7e
to
995bfd4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into the error message and also this pr! this lgtm 👍
@VictorHom Awesome, thanks for your help! |
#341
Implemented react/no-direct-mutation-state.
If anyone has any ideas for scenarios that should be tested, please let me know. This was my first contribution so any advice/suggestions about the code are welcome.