From 94b439bd46a1ed659dd4fe20a6924e05c7707781 Mon Sep 17 00:00:00 2001 From: Yannick Croissant Date: Mon, 29 Jun 2015 23:46:51 +0000 Subject: [PATCH] Fix nested props destructuring (fixes #136) --- lib/rules/prop-types.js | 11 ++++++++++- tests/lib/rules/prop-types.js | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/rules/prop-types.js b/lib/rules/prop-types.js index 75f4441fcd..658f50fae4 100644 --- a/lib/rules/prop-types.js +++ b/lib/rules/prop-types.js @@ -423,10 +423,19 @@ module.exports = function(context) { continue; } var propName = getKeyValue(properties[k]); + + var currentNode = node; + allNames = []; + while (currentNode.property && currentNode.property.name !== 'props') { + allNames.unshift(currentNode.property.name); + currentNode = currentNode.object; + } + allNames.push(propName); + if (propName) { usedPropTypes.push({ name: propName, - allNames: [propName], + allNames: allNames, node: properties[k] }); } diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index 4b59e6b656..bdf907359d 100644 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -522,6 +522,22 @@ eslintTester.addRuleTest('lib/rules/prop-types', { ].join('\n'), parser: 'babel-eslint', args: [1, {ignore: ['name']}] + }, { + code: [ + 'class Hello extends React.Component {', + ' render() {', + ' const {firstname, lastname} = this.props.name;', + ' return
{firstname} {lastname}
;', + ' }', + '}', + 'Hello.propTypes = {', + ' name: PropTypes.shape({', + ' firstname: PropTypes.string,', + ' lastname: PropTypes.string', + ' })', + '};' + ].join('\n'), + parser: 'babel-eslint' } ],