Skip to content

Commit 94b439b

Browse files
committed
Fix nested props destructuring (fixes #136)
1 parent 999e7c4 commit 94b439b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/rules/prop-types.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,19 @@ module.exports = function(context) {
423423
continue;
424424
}
425425
var propName = getKeyValue(properties[k]);
426+
427+
var currentNode = node;
428+
allNames = [];
429+
while (currentNode.property && currentNode.property.name !== 'props') {
430+
allNames.unshift(currentNode.property.name);
431+
currentNode = currentNode.object;
432+
}
433+
allNames.push(propName);
434+
426435
if (propName) {
427436
usedPropTypes.push({
428437
name: propName,
429-
allNames: [propName],
438+
allNames: allNames,
430439
node: properties[k]
431440
});
432441
}

tests/lib/rules/prop-types.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,22 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
522522
].join('\n'),
523523
parser: 'babel-eslint',
524524
args: [1, {ignore: ['name']}]
525+
}, {
526+
code: [
527+
'class Hello extends React.Component {',
528+
' render() {',
529+
' const {firstname, lastname} = this.props.name;',
530+
' return <div>{firstname} {lastname}</div>;',
531+
' }',
532+
'}',
533+
'Hello.propTypes = {',
534+
' name: PropTypes.shape({',
535+
' firstname: PropTypes.string,',
536+
' lastname: PropTypes.string',
537+
' })',
538+
'};'
539+
].join('\n'),
540+
parser: 'babel-eslint'
525541
}
526542
],
527543

0 commit comments

Comments
 (0)