From 8f0002302ce42f8822be0d174a5f5ca37f325d1f 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 --- CHANGELOG.md | 2 ++ lib/util/usedPropTypes.js | 2 +- tests/lib/rules/no-unused-prop-types.js | 8 ++++---- tests/lib/rules/prop-types.js | 22 ++++++++++++++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c486dd78f4..2a83a6ecb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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, + }), + }; + `, } )),