Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect used props in jsx #946

Merged
merged 2 commits into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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