Skip to content

Commit

Permalink
Fix destructured props detection in prop-types (fixes #443)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Feb 14, 2016
1 parent de538cb commit 6643f87
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,12 +713,8 @@ module.exports = Components.detect(function(context, components, utils) {
* FunctionDeclaration, or FunctionExpression
*/
function markDestructuredFunctionArgumentsAsUsed(node) {
var destructuring = node.params &&
node.params.length === 1 &&
node.params[0] &&
node.params[0].type === 'ObjectPattern';

if (destructuring) {
var destructuring = node.params && node.params[0] && node.params[0].type === 'ObjectPattern';
if (destructuring && components.get(node)) {
markPropTypesAsUsed(node);
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,16 @@ ruleTester.run('prop-types', rule, {
'};'
].join('\n'),
parserOptions: parserOptions
}, {
// Ignore destructured function arguments
code: [
'class Hello extends React.Component {',
' render () {',
' return ["string"].map(({length}) => <div>{length}</div>);',
' }',
'}'
].join('\n'),
parserOptions: parserOptions
}
],

Expand Down Expand Up @@ -1916,6 +1926,17 @@ ruleTester.run('prop-types', rule, {
errors: [{
message: '\'name\' is missing in props validation'
}]
}, {
code: [
'function Greetings({names}) {',
' names = names.map(({firstname, lastname}) => <div>{firstname} {lastname}</div>);',
' return <Hello>{names}</Hello>;',
'}'
].join('\n'),
parserOptions: parserOptions,
errors: [{
message: '\'names\' is missing in props validation'
}]
}
]
});

0 comments on commit 6643f87

Please sign in to comment.