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 committed Nov 17, 2021
1 parent a2f8a35 commit 2476d42
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
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 2476d42

Please sign in to comment.