Skip to content

Commit 81bde27

Browse files
author
Yannick Croissant
committed
Fix component detection (fixes #144)
1 parent a8f3a05 commit 81bde27

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

lib/rules/prop-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module.exports = function(context) {
7878
* @returns {Boolean} True if the component must be validated, false if not.
7979
*/
8080
function mustBeValidated(component) {
81-
return (
81+
return Boolean(
8282
component &&
8383
component.isReactComponent &&
8484
component.usedPropTypes &&

lib/util/component.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function isReactComponent(context, node) {
2222
}
2323

2424
var scope = context.getScope();
25+
while (scope.upper && scope.type !== 'function') {
26+
scope = scope.upper;
27+
}
28+
2529
var isComponentRender =
2630
node.argument &&
2731
node.argument.type === 'JSXElement' &&

tests/lib/rules/prop-types.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,25 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
955955
errors: [
956956
{message: '\'firstname\' is missing in props validation for Hello'}
957957
]
958+
}, {
959+
code: [
960+
'class Hello extends React.Component {',
961+
' render() {',
962+
' if (true) {',
963+
' return <span>{this.props.firstname}</span>',
964+
' } else {',
965+
' return <span>{this.props.lastname}</span>',
966+
' }',
967+
' }',
968+
'}',
969+
'Hello.propTypes = {',
970+
' lastname: React.PropTypes.string',
971+
'}'
972+
].join('\n'),
973+
parser: 'babel-eslint',
974+
errors: [
975+
{message: '\'firstname\' is missing in props validation for Hello'}
976+
]
958977
}
959978
]
960979
});

0 commit comments

Comments
 (0)