From c153a86f092c65e9fb01ee5ce544302b67f040de Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Wed, 2 Oct 2019 07:35:53 +0800 Subject: [PATCH] [Fix] `no-unused-prop-types`: false positive when nested destructuring --- lib/util/usedPropTypes.js | 23 ++++++++++++----------- tests/lib/rules/no-unused-prop-types.js | 14 +++++++++++++- tests/lib/rules/prop-types.js | 2 ++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/util/usedPropTypes.js b/lib/util/usedPropTypes.js index 02b7ed67e8..992ab58077 100755 --- a/lib/util/usedPropTypes.js +++ b/lib/util/usedPropTypes.js @@ -362,19 +362,20 @@ module.exports = function usedPropTypesInstructions(context, components, utils) } const propName = ast.getKeyValue(context, properties[k]); - if ( - propName && - properties[k].type === 'Property' && - properties[k].value.type === 'ObjectPattern' - ) { + if (!propName || properties[k].type !== 'Property') { + break; + } + + usedPropTypes.push({ + allNames: parentNames.concat([propName]), + name: propName, + node: properties[k] + }); + + if (properties[k].value.type === 'ObjectPattern') { markPropTypesAsUsed(properties[k].value, parentNames.concat([propName])); - } else if (propName) { + } else if (properties[k].value.type === 'Identifier') { propVariables.set(propName, parentNames.concat(propName)); - usedPropTypes.push({ - allNames: parentNames.concat([propName]), - name: propName, - node: properties[k] - }); } } break; diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index 0ef4b382a1..b5ba74ea25 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -1825,6 +1825,18 @@ ruleTester.run('no-unused-prop-types', rule, { ' return
;', '};' ].join('\n') + }, { + // Nested destructuring; issue 2424 + code: ` + function SomeComponent(props) { + const {aaa: {bbb}} = props; + return

{bbb}

; + } + + SomeComponent.propTypes = { + aaa: somePropType, + }; + ` }, { // `no-unused-prop-types` in jsx expressions - [Issue #885] code: [ @@ -1864,7 +1876,7 @@ ruleTester.run('no-unused-prop-types', rule, { const { a } = props; document.title = a; }); - + return

; } diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index bcdee30c03..dedf5ff727 100755 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -2498,6 +2498,7 @@ ruleTester.run('prop-types', rule, { } `, errors: [ + {message: "'foo' is missing in props validation"}, {message: "'foo.bar' is missing in props validation"} ] }, { @@ -2525,6 +2526,7 @@ ruleTester.run('prop-types', rule, { } `, errors: [ + {message: "'foo' is missing in props validation"}, {message: "'foo.bar' is missing in props validation"} ] },