diff --git a/lib/rules/no-unused-prop-types.js b/lib/rules/no-unused-prop-types.js index 5127971d1a..d5311fdb14 100644 --- a/lib/rules/no-unused-prop-types.js +++ b/lib/rules/no-unused-prop-types.js @@ -617,7 +617,7 @@ module.exports = { throw new Error(`${node.type} ASTNodes are not handled by markPropTypesAsUsed`); } - const component = components.get(utils.getParentComponent()); + const component = components.get(utils.getParentComponent() || node, true); const usedPropTypes = component && component.usedPropTypes || []; let ignorePropsValidation = component && component.ignorePropsValidation || false; diff --git a/lib/util/Components.js b/lib/util/Components.js index 1346fb9ef1..bbd8b7cd28 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -49,11 +49,14 @@ Components.prototype.add = function(node, confidence) { * Find a component in the list using its node * * @param {ASTNode} node The AST node being searched. + * @param {Boolean} findParent True if the node's parent can be returned if the node isn't found, false if not * @returns {Object} Component object, undefined if the component is not found */ -Components.prototype.get = function(node) { - const id = this._getId(node); - return this._list[id]; +Components.prototype.get = function(node, findParent) { + while (findParent && node && !this._list[this._getId(node)]) { + node = node.parent; + } + return this._list[this._getId(node)]; }; /** diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index eff0e9c962..f87d1d0741 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -737,6 +737,21 @@ ruleTester.run('no-unused-prop-types', rule, { '};' ].join('\n'), parser: 'babel-eslint' + }, { + code: [ + 'const Inner = (props) => {props.innerOne} {props.innerTwo};', + 'const Outer = (props) => {', + ' let team = props.names.map(() => (', + ' ', + ' ));', + ' return ;', + '};', + 'Outer.propTypes = {', + ' names: PropTypes.array,', + ' one: PropTypes.string,', + ' two: PropTypes.string', + '};' + ].join('\n') }, { code: [ 'export default {',