From 712244f88adab974e8685412643ceab07cd5bfa6 Mon Sep 17 00:00:00 2001 From: Yannick Croissant Date: Sun, 20 Dec 2015 20:52:44 +0000 Subject: [PATCH] Add support for propTypes assigned via a variable (fixes #355) --- lib/rules/prop-types.js | 13 +++++++++++++ tests/lib/rules/prop-types.js | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/rules/prop-types.js b/lib/rules/prop-types.js index 7c10c71385..ed78afd07c 100644 --- a/lib/rules/prop-types.js +++ b/lib/rules/prop-types.js @@ -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 @@ -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: diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index 746f1670fc..bb20dfe4ac 100644 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -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 (', + '
{this.props.firstname} {this.props.lastname}
', + ' );', + ' }', + '}', + 'Test.propTypes = propTypes;' + ].join('\n'), + parser: 'babel-eslint', + errors: [ + {message: '\'lastname\' is missing in props validation'} + ] } ] });