-
-
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
Consider deprecating no-unused-prop-types
rule
#976
Comments
It seems like a very useful rule that I'm loath to remove, but I do agree that it could have very many false positives. |
Like @ljharb said, it's very useful and it does have probems of current implementation. |
I face the false positives like your sample code. Currently I add comment for temporary disable the rule on the prop. |
Well it would help if this rule also checks on
I think that would already cover a big number of false positives. Overall I think it is a very useful rule which should be kept. |
@johanneserber agreed. In my project, there are some cases that some props are used only in |
@EvNaverniouk like @ljharb I find this rule useful and I think it would be a shame to remove it (despite the fact that I do not use it myself, because of the false positives 😢). Imho to make it really useful we should:
|
The complex case of the This rule would be very helpful for the top-level props tracking, especially in the stateless functions. It would better skip some "maybe used" props and reliably report "definitely unused", i.e. if there's no props object passing (except constructor super), but there's still destructuring in render. |
Closing this for now. Consensus seems to be that it's useful. Hopefully the community and I can help fill in the missing gaps to reduce the false positives. |
Thanks @EvNaverniouk ! |
Ha, seems like you'd have to solve the halting problem for this one. I think this rule should be relaxed quite a bit, so that it just looks for the prop name string anywhere in the same file. I think that would solve 99% of these false positives, and it would still be quite useful. Even if that's not the default behavior, it would be really useful as an option. |
Actually I think johanneserber's comment suggestion would cover 99% of cases, and I would also like to add a destructuring check. e.g.:
That would solve everything for me. Even if there are some edge-cases, that would give me enough options to rewrite the code and pass the linting. |
I have another case which raises an error as a false positive. type CouponsListContainerProps = {
errorMessage?: string, // this props will only be used in `shouldComponentUpdate`
...
};
class CouponsListContainer extends React.Component {
props: CouponsListContainerProps
static defaultProps = {
errorMessage: "",
};
shouldComponentUpdate(props: CouponsListContainerProps) {
// we will use errorMessage without `this` keyword
if (props.errorMessage.length > 0) {
this.toast.show(props.errorMessage);
}
return true;
}
render = () => {...}
} The |
Using |
export default class Users extends Component {
static propTypes = {
...
onLoad: PropTypes.func.isRequired,
};
async componentWillMount() {
await Promise.all([
this.props.onLoad(),
this.onSearch(),
]);
...
}
...
}
|
I know there are a lot of false positives for this rule in our codebase and unfortunately that is keeping us from turning it on. I think I'd actually prefer false negatives than false positives for this lint rule. False negatives would make me think it would be great if it was smarter and could be improved whereas false positives are annoying for teams and make it more likely it will get turned back off. I'm not sure how feasible a shift in that direction would be, I haven't taken a close look at the code. Do others have the same perspective on false positives vs false negatives? |
I wrote the
no-unused-prop-types
rule which was merged intoeslint-plugin-react
inv6.2.0
. Thanks for accepting it and letting me contribute to this project. I've been using this rule for many different projects with some success. However, after some more careful analysis, I am now fairly convinced that we should remove it.Although it's very useful and can definitely help detect dead code, it's highly prone to false positives as seen via: #628, #811, #816, #819, #833, #879, #880, #884, #885, #944, #974 and many more.
I've taken a stab last week to improve the rule to fix the false positives, but it ultimately led me to the realization that in order to truly fix them, we would need to update the rule to fully track the use of props across all functions in the file. This is an incredibly difficult task given the plethora of ways that arguments can be passed into functions. Consider this worst case scenario:
In order to properly mark
myProp
as used in this case, I need to track the movement ofprops
all the way from theconstructor
, intodoSomething
(viabind
), then intodoAnotherThing
while being destructured (and outside the original component), and finally then I can check if a specific prop is used.Now you have to do this for every prop in every component of your code. The ESLint rule that would support this tracking is hugely complicated and super slow. I don't think it's worth the benefits of us going down this path.
But unless we venture down this road and do it right, we will always have false positives reported.
@yannickcr @lencioni @ljharb Thoughts?
The text was updated successfully, but these errors were encountered: