-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
[no-unused-state] Add ability to handle arrow function class method #1411
[no-unused-state] Add ability to handle arrow function class method #1411
Conversation
lib/rules/no-unused-state.js
Outdated
node.value.type === 'ArrowFunctionExpression' | ||
) { | ||
// Create a new set for this.state aliases local to this function. | ||
classInfo.aliases = new Set(); |
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'm not seeing anything that adds to this set?
If it otherwise crashes because this doesn't exist; then let's initialize it in every case.
I think the set, For instance, class App extends React.Component {
state = { foo: 0 };
doSomething = () => {
const { state: { foo: bar }} = this;
return bar;
}
render() {
return <SomeComponent />;
}
} will add I also refactored how the code deals with |
lib/rules/no-unused-state.js
Outdated
classInfo.aliases = null; | ||
|
||
// Remove the current set of aliases local to the previous class method | ||
classInfo.aliases.clear(); |
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.
rather than storing across files and clearing; we may want to instead assign classInfo.aliases = new Set()
at program entry?
Based on your suggestion, I have restored the implementation back to using However, we still need to do Let me know if that's ok. If not, there are alternative but potentially more complicated ways to implement this, such as making |
This seems fine to me; I'm deferring to someone more familiar with this code.
c35c5ea
to
ddd05b1
Compare
Thanks @jackyho112 This is because of the new ESLint release. They changed some internal process and a case that we didn't handle before will now work. I can update the case to be valid, but it would only work with the latest ESLint. With older cases it will still fail. Even more: We had to write some work-around to make sure ESLint did not crash but I can't remove that code unless we update the peerDependency for ESLint. I also commented here: #1375 (comment) But I think I can start a new PR and we can start/continue a discussion there. |
Created a PR to move that test: #1437 |
393467f
to
e616d7f
Compare
@ljharb, @EvHaus, @yannickcr, @lencioni Decided to adhere to the original implementation instead because it is the safest approach, it is easier to review and if there is a new way of defining a class method in the future, the rule won't break immediately. Now, this PR just merely adds the support for a class method defined by an arrow function in a way how the original code intends to and does nothing else. I hope that is ok and should at least make this PR more mergable. 😄 |
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 does seem much simpler. Test changes LGTM; implementation feems fine.
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.
LGTM
For #1363, so that state property usage by object destructuring assignment in a class method defined with an arrow function gets recorded. Examples:
should not be warned.
Let me know if there is anything I can do to improve this PR. Also, just to confirm, this rule doesn't care about whether the class method is used or not, right?