Skip to content

Commit

Permalink
[Fix] prop-types: fix false positives on renames in object destruct…
Browse files Browse the repository at this point in the history
…uring

Fixes #2944
  • Loading branch information
golopot authored and ljharb committed Nov 17, 2021
1 parent 0132c47 commit 8f00023
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,10 +8,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Fixed
* [`no-invalid-html-attribute`]: allow `link` `rel` to have `apple-touch-icon`, `mask-icon` ([#3132][] @ljharb)
* [`no-unused-class-component-methods`]: add `getChildContext` lifecycle method ([#3136][] @yoyo837)
* [`prop-types`]: fix false positives on renames in object destructuring ([#3142][] @golopot)

### Changed
* [readme] fix syntax typo ([#3141][] @moselhy)

[#3142]: https://github.com/yannickcr/eslint-plugin-react/pull/3142
[#3141]: https://github.com/yannickcr/eslint-plugin-react/pull/3141
[#3136]: https://github.com/yannickcr/eslint-plugin-react/pull/3136
[#3132]: https://github.com/yannickcr/eslint-plugin-react/issue/3132
Expand Down
2 changes: 1 addition & 1 deletion lib/util/usedPropTypes.js
Expand Up @@ -395,7 +395,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
if (properties[k].value.type === 'ObjectPattern') {
markPropTypesAsUsed(properties[k].value, parentNames.concat([propName]));
} else if (properties[k].value.type === 'Identifier') {
propVariables.set(propName, parentNames.concat(propName));
propVariables.set(properties[k].value.name, parentNames.concat(propName));
}
}
break;
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/rules/no-unused-prop-types.js
Expand Up @@ -4921,7 +4921,7 @@ ruleTester.run('no-unused-prop-types', rule, {
foo: PropTypes.string,
bar: PropTypes.string,
};
componentWillUpdate (nextProps) {
if (nextProps.foo) {
return true;
Expand Down Expand Up @@ -4994,7 +4994,7 @@ ruleTester.run('no-unused-prop-types', rule, {
foo: PropTypes.string,
bar: PropTypes.string,
};
shouldComponentUpdate (nextProps) {
if (nextProps.foo) {
return true;
Expand Down Expand Up @@ -5086,7 +5086,7 @@ ruleTester.run('no-unused-prop-types', rule, {
foo: PropTypes.string,
bar: PropTypes.string,
};
componentDidUpdate (nextProps) {
if (nextProps.foo) {
return true;
Expand Down Expand Up @@ -5319,7 +5319,7 @@ ruleTester.run('no-unused-prop-types', rule, {
{
// None of the props are used issue #1162
code: `
import React from "react";
import React from "react";
var Hello = React.createReactClass({
propTypes: {
name: React.PropTypes.string
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -3542,6 +3542,28 @@ ruleTester.run('prop-types', rule, {
}
`,
features: ['class fields'],
},

// #2944
{
code: `
const styles = { width: 0 };
function Foo(props) {
const { styles: x } = props;
return (
<p>
{styles.width} {x._}
</p>
);
}
Foo.propTypes = {
styles: PropTypes.shape({
_: PropTypes.number,
}),
};
`,
}
)),

Expand Down

0 comments on commit 8f00023

Please sign in to comment.