From 2476d42527f201f31b9e95c6559b3cab93cdbf04 Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Wed, 17 Nov 2021 21:40:15 +0800 Subject: [PATCH] [Fix] `prop-types`: fix false positives on renames in object destructuring Fixes #2944 --- lib/util/usedPropTypes.js | 2 +- tests/lib/rules/no-unused-prop-types.js | 8 ++++---- tests/lib/rules/prop-types.js | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/util/usedPropTypes.js b/lib/util/usedPropTypes.js index 7b11745105..66e6759e72 100644 --- a/lib/util/usedPropTypes.js +++ b/lib/util/usedPropTypes.js @@ -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; diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index 99c5956989..5080ec8a63 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -4921,7 +4921,7 @@ ruleTester.run('no-unused-prop-types', rule, { foo: PropTypes.string, bar: PropTypes.string, }; - + componentWillUpdate (nextProps) { if (nextProps.foo) { return true; @@ -4994,7 +4994,7 @@ ruleTester.run('no-unused-prop-types', rule, { foo: PropTypes.string, bar: PropTypes.string, }; - + shouldComponentUpdate (nextProps) { if (nextProps.foo) { return true; @@ -5086,7 +5086,7 @@ ruleTester.run('no-unused-prop-types', rule, { foo: PropTypes.string, bar: PropTypes.string, }; - + componentDidUpdate (nextProps) { if (nextProps.foo) { return true; @@ -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 diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index ed49884926..9e39620178 100644 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -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 ( +

+ {styles.width} {x._} +

+ ); + } + + Foo.propTypes = { + styles: PropTypes.shape({ + _: PropTypes.number, + }), + }; + `, } )),