Skip to content

Commit

Permalink
detect used props in jsx
Browse files Browse the repository at this point in the history
fixes #885
  • Loading branch information
BarryThePenguin committed Jan 30, 2017
1 parent 30e0206 commit d5c3e91
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/util/Components.js
Expand Up @@ -362,12 +362,13 @@ function componentRule(rule, context) {
var isFunction = /Function/.test(node.type); // Functions
var isMethod = node.parent && node.parent.type === 'MethodDefinition'; // Classes methods
var isArgument = node.parent && node.parent.type === 'CallExpression'; // Arguments (callback, etc.)
var isJSX = node.parent && node.parent.type === 'JSXExpressionContainer';
// Stop moving up if we reach a class or an argument (like a callback)
if (isClass || isArgument) {
return null;
}
// Return the node if it is a function that is not a class method
if (isFunction && !isMethod) {
if (isFunction && !isMethod && !isJSX) {
return node;
}
scope = scope.upper;
Expand Down
33 changes: 33 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Expand Up @@ -1428,6 +1428,39 @@ ruleTester.run('no-unused-prop-types', rule, {
'};'
].join('\n'),
parserOptions: parserOptions
}, {
// `no-unused-prop-types` in jsx expressions - [Issue #885]
code: [
'const PagingBlock = function(props) {',
' return (',
' <span>',
' <a onClick={() => props.previousPage()}/>',
' <a onClick={() => props.nextPage()}/>',
' </span>',
' );',
'};',

'PagingBlock.propTypes = {',
' nextPage: React.PropTypes.func.isRequired,',
' previousPage: React.PropTypes.func.isRequired,',
'};'
].join('\n'),
parserOptions: parserOptions
}, {
// `no-unused-prop-types` rest param props in jsx expressions - [Issue #885]
code: [
'const PagingBlock = function(props) {',
' return (',
' <SomeChild {...props} />',
' );',
'};',

'PagingBlock.propTypes = {',
' nextPage: React.PropTypes.func.isRequired,',
' previousPage: React.PropTypes.func.isRequired,',
'};'
].join('\n'),
parserOptions: parserOptions
}
],

Expand Down

0 comments on commit d5c3e91

Please sign in to comment.