-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[New] add no-unused-class-component-methods
#2239
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
Conversation
a537eeb
to
1aaa6c9
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, this is a great start. Let's add a lot more test cases tho :-)
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
const internalMethods = [ |
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.
what about unused static methods?
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.
Should we be concerned about static methods? They doesn't seem to be closely connected to component. I feel that static methods could be used outside of component and in that case it would be harder to determine if method is used..
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.
In a react component, yes - it's not meant to be used as a class.
(An instance method could also be used outside of the component)
Perhaps maybe an option to control checking of static methods?
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.
That might be a good idea. I'll try to tackle it after basic version.
const isMethod = node.type === 'MethodDefinition'; | ||
const isArrowFunction = ( | ||
node.type === 'ClassProperty' && | ||
node.value.type === 'ArrowFunctionExpression' |
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.
what about async functions, async arrow functions, generators, and async generators?
class Foo extends React.Component { | ||
action() {} | ||
componentDidMount() { | ||
const action = this.action; |
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.
What about use by object destructuring?
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.
ping on this one
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.
cc @meowtec
Thanks for feedback. Currently I'm taking some time off, but I'll definitely get back to it next week. I'm super excited about it! |
Would this also account for functional components? |
@ha404 It shouldn't. |
@golopot but you can add static properties to functional components too. const SomeComponent = () => ...
SomeComponent.propTypes = ...
SomeComponent.anythingYouWant = ... |
Is there anything I can do to help to get this to mergeable state? |
@Kiwka if you post a link here to a branch that addresses the review comments and test failures here (please do NOT create a new PR) then I can pull in your commits, and we can definitely get this in. |
Looks good, can we look at those tests, please? desperado for this... :) (y) |
ffff596
to
c4dd9d0
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
{ | ||
code: ` | ||
class Foo extends React.Component { | ||
property = {} |
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.
Why is it valid?
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.
Could it also check unused property?
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.
That's a good idea; the rule should check both properties and methods.
This comment has been minimized.
This comment has been minimized.
Still in progress? |
ping @pawelnvk |
case 'ClassProperty': | ||
case 'JSXAttribute': | ||
case 'MethodDefinition': | ||
getThisExpressions(subnode.value); |
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.
Consider using the Object switch methodology rather than an actual switch, in my view, it's a lot faster than the switch.
See this link for a reference.
https://stackoverflow.com/questions/37730199/switch-vs-object-lookup-performance-since-jsperf-is-down
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.
Faster or not, it's always cleaner :-)
It seams that this PR has been abandoned. I have (re)written this rule with another implementation and use it in our project which has over 100k lines of code. |
@meowtec if you'd be able to make those changes in a branch on a fork - not a PR - then I can look into pulling them into this PR. |
@ljharb My branch is at https://github.com/meowtec/eslint-plugin-react/tree/unused-component-methods-improve What are different:
@pawelnvk Please take a look because I am not sure if any important things was missing. ref: #2239 (comment) |
Yes, please add createClass support back; we'll be supporting it for the foreseeable future. |
c4dd9d0
to
e84c99e
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.
Let's add tests for state = {}
and ensure that unused state
is ignored.
} | ||
``` | ||
|
||
The following patterns are **not** considered warnings: |
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.
let's add some unused static methods here, so it's 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.
cc @meowtec
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.
cc @meowtec
should all static methods be list here?
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.
Is there more than one? Either way we'd only need at least one as an example.
class Foo extends React.Component { | ||
action() {} | ||
componentDidMount() { | ||
const action = this.action; |
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.
ping on this one
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.
Yes, please add createClass support back; we'll be supporting it for the foreseeable future.
https://github.com/meowtec/eslint-plugin-react/tree/unused-component-methods
- add
createReactClass
back - ignore unused
state
return; | ||
} | ||
|
||
switch (subnode.type) { |
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.
These codes look verbose and many cases were lost, such as BinaryExpression
arguments
ArrayExpression
, ObjectExpression
, etc.
case 'ClassProperty': | ||
case 'JSXAttribute': | ||
case 'MethodDefinition': | ||
getThisExpressions(subnode.value); |
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.
subnode.value
could be null
so it will throw with error at the next switch (subnode.type)
.
eg:
<button disabled />
class Foo extends React.Component {
a;
}
Fixed and added some test cases: meowtec@dd88dc1
getThisExpressions(subnode.init); | ||
break; | ||
case 'MemberExpression': | ||
if (subnode.object.type !== 'ThisExpression') { |
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.
Should exclude the computed property:
class Foo extends React.Component {
handleClick() {}
render() {
return <button onClick={this[handleClick]}>Text</button>;
}
}
32857ec
to
aff2f24
Compare
Bumping |
@pawelnvk are you still interested in landing this PR? if so, it could use a rebase and there's some review comments to address :-) |
@jakeleventhal that'd be great! however, please do NOT open a new PR; please comment here with a link to your branch, and I'll pull in the changes. |
Co-authored-by: meowtec <bertonzh@gmail.com> Co-authored-by: Jake Leventhal <jakeleventhal@me.com>
@jakeleventhal thanks! Pulling it in now. I do note that it's still missing support for the static |
e51f8d7
to
7114353
Compare
no-unused-class-component-methods
Codecov Report
@@ Coverage Diff @@
## master #2239 +/- ##
=======================================
Coverage 97.43% 97.44%
=======================================
Files 111 112 +1
Lines 7529 7622 +93
Branches 2769 2798 +29
=======================================
+ Hits 7336 7427 +91
- Misses 193 195 +2
Continue to review full report at Codecov.
|
@ljharb im a bit unfamiliar with this repo. do you think you could just take this over since you're pulling in the change anyway? i would appreciate it |
@ljharb also, does this cover hooks? |
@ljharb bump |
Sure, I can try to add it. No, this doesn't cover hooks; the rule only has to do with class components. SFCs don't have methods. |
@ljharb okay, thank you. I will leave it to you to finish then i suppose |
08a6f69
to
1371f71
Compare
Display errors in case class component contains unused methods.
Fixes #2166.