Skip to content

Commit d5c3e91

Browse files
detect used props in jsx
fixes #885
1 parent 30e0206 commit d5c3e91

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/util/Components.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,13 @@ 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+
var isJSX = node.parent && node.parent.type === 'JSXExpressionContainer';
365366
// Stop moving up if we reach a class or an argument (like a callback)
366367
if (isClass || isArgument) {
367368
return null;
368369
}
369370
// Return the node if it is a function that is not a class method
370-
if (isFunction && !isMethod) {
371+
if (isFunction && !isMethod && !isJSX) {
371372
return node;
372373
}
373374
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)