Skip to content

Commit

Permalink
Add support for propTypes assigned via a variable (fixes #355)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Dec 20, 2015
1 parent e9d191c commit 712244f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// https://github.com/yannickcr/eslint-plugin-react/issues/7

var Components = require('../util/Components');
var variable = require('../util/variable');

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -518,6 +519,18 @@ module.exports = Components.detect(function(context, components, utils) {
buildReactDeclarationTypes(propTypes.parent.right);
}
break;
case 'Identifier':
var variablesInScope = variable.variablesInScope(context);
for (var i = 0, j = variablesInScope.length; i < j; i++) {
if (variablesInScope[i].name !== propTypes.name) {
continue;
}
var defInScope = variablesInScope[i].defs[variablesInScope[i].defs.length - 1];
markPropTypesAsDeclared(node, defInScope.node && defInScope.node.init);
return;
}
ignorePropsValidation = true;
break;
case null:
break;
default:
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,24 @@ ruleTester.run('prop-types', rule, {
errors: [
{message: '\'name\' is missing in props validation'}
]
}, {
code: [
'var propTypes = {',
' firstname: React.PropTypes.string',
'};',
'class Test extends React.Component {',
' render() {',
' return (',
' <div>{this.props.firstname} {this.props.lastname}</div>',
' );',
' }',
'}',
'Test.propTypes = propTypes;'
].join('\n'),
parser: 'babel-eslint',
errors: [
{message: '\'lastname\' is missing in props validation'}
]
}
]
});

0 comments on commit 712244f

Please sign in to comment.