Skip to content

Commit 712244f

Browse files
committed
Add support for propTypes assigned via a variable (fixes #355)
1 parent e9d191c commit 712244f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/rules/prop-types.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// https://github.com/yannickcr/eslint-plugin-react/issues/7
99

1010
var Components = require('../util/Components');
11+
var variable = require('../util/variable');
1112

1213
// ------------------------------------------------------------------------------
1314
// Rule Definition
@@ -518,6 +519,18 @@ module.exports = Components.detect(function(context, components, utils) {
518519
buildReactDeclarationTypes(propTypes.parent.right);
519520
}
520521
break;
522+
case 'Identifier':
523+
var variablesInScope = variable.variablesInScope(context);
524+
for (var i = 0, j = variablesInScope.length; i < j; i++) {
525+
if (variablesInScope[i].name !== propTypes.name) {
526+
continue;
527+
}
528+
var defInScope = variablesInScope[i].defs[variablesInScope[i].defs.length - 1];
529+
markPropTypesAsDeclared(node, defInScope.node && defInScope.node.init);
530+
return;
531+
}
532+
ignorePropsValidation = true;
533+
break;
521534
case null:
522535
break;
523536
default:

tests/lib/rules/prop-types.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,24 @@ ruleTester.run('prop-types', rule, {
14081408
errors: [
14091409
{message: '\'name\' is missing in props validation'}
14101410
]
1411+
}, {
1412+
code: [
1413+
'var propTypes = {',
1414+
' firstname: React.PropTypes.string',
1415+
'};',
1416+
'class Test extends React.Component {',
1417+
' render() {',
1418+
' return (',
1419+
' <div>{this.props.firstname} {this.props.lastname}</div>',
1420+
' );',
1421+
' }',
1422+
'}',
1423+
'Test.propTypes = propTypes;'
1424+
].join('\n'),
1425+
parser: 'babel-eslint',
1426+
errors: [
1427+
{message: '\'lastname\' is missing in props validation'}
1428+
]
14111429
}
14121430
]
14131431
});

0 commit comments

Comments
 (0)