Skip to content

Commit

Permalink
Fix props validation for nested stateless components (fixes #331)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Nov 28, 2015
1 parent bccf86b commit f1ca75d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,23 @@ module.exports = Components.detect(function(context, components, utils) {

/**
* Checks if the prop is declared
* @param {Object} component The component to process
* @param {ASTNode} node The AST node being checked.
* @param {String[]} names List of names of the prop to check.
* @returns {Boolean} True if the prop is declared, false if not.
*/
function isDeclaredInComponent(component, names) {
return _isDeclaredInComponent(
component.declaredPropTypes || {},
names
);
function isDeclaredInComponent(node, names) {
while (node) {
var component = components.get(node);
var isDeclared =
component && component.confidence === 2 &&
_isDeclaredInComponent(component.declaredPropTypes || {}, names)
;
if (isDeclared) {
return true;
}
node = node.parent;
}
return false;
}

/**
Expand Down Expand Up @@ -532,7 +540,7 @@ module.exports = Components.detect(function(context, components, utils) {
allNames = component.usedPropTypes[i].allNames;
if (
isIgnored(allNames[0]) ||
isDeclaredInComponent(component, allNames)
isDeclaredInComponent(component.node, allNames)
) {
continue;
}
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 @@ -935,6 +935,24 @@ ruleTester.run('prop-types', rule, {
jsx: true,
experimentalObjectRestSpread: true
}
}, {
code: [
'const statelessComponent = (props) => {',
' const subRender = () => {',
' return <span>{props.someProp}</span>;',
' };',
' return <div>{subRender()}</div>;',
'};',
'statelessComponent.propTypes = {',
' someProp: PropTypes.string',
'};'
].join('\n'),
env: {
es6: true
},
ecmaFeatures: {
jsx: true
}
}
],

Expand Down

0 comments on commit f1ca75d

Please sign in to comment.