Skip to content

Commit

Permalink
Merge pull request #946 from BarryThePenguin/no-unused-prop-types-in-jsx
Browse files Browse the repository at this point in the history
detect used props in jsx
  • Loading branch information
lencioni committed Feb 15, 2017
2 parents 3b25a3b + 9af8d25 commit 3e2421e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,14 @@ 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.)
// Attribute Expressions inside JSX Elements (<button onClick={() => props.handleClick()}></button>)
var isJSXExpressionContainer = 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) {
// Return the node if it is a function that is not a class method and is not inside a JSX Element
if (isFunction && !isMethod && !isJSXExpressionContainer) {
return node;
}
scope = scope.upper;
Expand Down
33 changes: 33 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
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 3e2421e

Please sign in to comment.