Skip to content

Commit 3e2421e

Browse files
authored
Merge pull request #946 from BarryThePenguin/no-unused-prop-types-in-jsx
detect used props in jsx
2 parents 3b25a3b + 9af8d25 commit 3e2421e

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/util/Components.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,14 @@ function componentRule(rule, context) {
362362
var isFunction = /Function/.test(node.type); // Functions
363363
var isMethod = node.parent && node.parent.type === 'MethodDefinition'; // Classes methods
364364
var isArgument = node.parent && node.parent.type === 'CallExpression'; // Arguments (callback, etc.)
365+
// Attribute Expressions inside JSX Elements (<button onClick={() => props.handleClick()}></button>)
366+
var isJSXExpressionContainer = node.parent && node.parent.type === 'JSXExpressionContainer';
365367
// Stop moving up if we reach a class or an argument (like a callback)
366368
if (isClass || isArgument) {
367369
return null;
368370
}
369-
// Return the node if it is a function that is not a class method
370-
if (isFunction && !isMethod) {
371+
// Return the node if it is a function that is not a class method and is not inside a JSX Element
372+
if (isFunction && !isMethod && !isJSXExpressionContainer) {
371373
return node;
372374
}
373375
scope = scope.upper;

tests/lib/rules/no-unused-prop-types.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,39 @@ ruleTester.run('no-unused-prop-types', rule, {
14281428
'};'
14291429
].join('\n'),
14301430
parserOptions: parserOptions
1431+
}, {
1432+
// `no-unused-prop-types` in jsx expressions - [Issue #885]
1433+
code: [
1434+
'const PagingBlock = function(props) {',
1435+
' return (',
1436+
' <span>',
1437+
' <a onClick={() => props.previousPage()}/>',
1438+
' <a onClick={() => props.nextPage()}/>',
1439+
' </span>',
1440+
' );',
1441+
'};',
1442+
1443+
'PagingBlock.propTypes = {',
1444+
' nextPage: React.PropTypes.func.isRequired,',
1445+
' previousPage: React.PropTypes.func.isRequired,',
1446+
'};'
1447+
].join('\n'),
1448+
parserOptions: parserOptions
1449+
}, {
1450+
// `no-unused-prop-types` rest param props in jsx expressions - [Issue #885]
1451+
code: [
1452+
'const PagingBlock = function(props) {',
1453+
' return (',
1454+
' <SomeChild {...props} />',
1455+
' );',
1456+
'};',
1457+
1458+
'PagingBlock.propTypes = {',
1459+
' nextPage: React.PropTypes.func.isRequired,',
1460+
' previousPage: React.PropTypes.func.isRequired,',
1461+
'};'
1462+
].join('\n'),
1463+
parserOptions: parserOptions
14311464
}
14321465
],
14331466

0 commit comments

Comments
 (0)