eslint-plugin-react: Destructured use of property is not recognized by no-unused-prop-types
Given a React component like:
export default class Thing extends React.Component {
static propTypes = {
i18n: PropTypes.shape({
gettext: PropTypes.func,
}).isRequired,
}
render() {
const { i18n } = this.props;
return (
<div>
<span>{i18n.gettext('Some Text')}</span>
</div>
);
}
}
And the following eslint rule:
"react/no-unused-prop-types": "error"
I see the following error:
/Users/kumar/tmp/eslint-scratch/index.js
6:16 error 'i18n.gettext' PropType is defined but prop is never used react/no-unused-prop-types
This is incorrect because the i18n property is destructured into a new constant and then the gettext() function is called. If I edit the code so that it doesn’t use destructuring then the error goes away, revealing the bug.
Here is a small app that reproduces it: eslint-scratch.zip. Run:
npm install
npm run eslint
This is similar to https://github.com/yannickcr/eslint-plugin-react/issues/782 but they seem different.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 48
- Comments: 37 (14 by maintainers)
Commits related to this issue
- test cases from #816 — committed to DianaSuvorova/eslint-plugin-react by deleted user 7 years ago
I am seeing this as well when using
PropTypes.arrayOf(PropTypes.shape({...})and then mapping the prop inrenderI have a warning with flow too. My code:
And my solution:
I can create a repro if it’s necessary.
@numbergames Warnings are valid, in this case.
Thus, all prop validation does apply to original prop names - not aliased. Same applies to
defaultPropsCorrect usage would be:
@ljharb I have created a PR with (what I think) all of the valid use cases discussed in this thread. Most of them are fixed already. I found only one reported by @robguy21, that needs to be investigated (I commented it out and will look into it). Please let me know if missed any other valid cases from this issue. Thanks!
It’s this kind of snark that I came here for. Thanks!