Skip to content

Commit bc455b8

Browse files
author
Dustin Masters
committed
Fix incorrect propType warning inside .map
1 parent 739ece1 commit bc455b8

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

lib/rules/no-unused-prop-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ module.exports = {
617617
throw new Error(`${node.type} ASTNodes are not handled by markPropTypesAsUsed`);
618618
}
619619

620-
const component = components.get(utils.getParentComponent());
620+
const component = components.get(utils.getParentComponent() || node, true);
621621
const usedPropTypes = component && component.usedPropTypes || [];
622622
let ignorePropsValidation = component && component.ignorePropsValidation || false;
623623

lib/util/Components.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ Components.prototype.add = function(node, confidence) {
4949
* Find a component in the list using its node
5050
*
5151
* @param {ASTNode} node The AST node being searched.
52+
* @param {Boolean} findParent True if the node's parent can be returned if the node isn't found, false if not
5253
* @returns {Object} Component object, undefined if the component is not found
5354
*/
54-
Components.prototype.get = function(node) {
55-
const id = this._getId(node);
56-
return this._list[id];
55+
Components.prototype.get = function(node, findParent) {
56+
while (findParent && node && !this._list[this._getId(node)]) {
57+
node = node.parent;
58+
}
59+
return this._list[this._getId(node)];
5760
};
5861

5962
/**

tests/lib/rules/no-unused-prop-types.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,21 @@ ruleTester.run('no-unused-prop-types', rule, {
737737
'};'
738738
].join('\n'),
739739
parser: 'babel-eslint'
740+
}, {
741+
code: [
742+
'const Inner = (props) => <span>{props.innerOne} {props.innerTwo}</span>;',
743+
'const Outer = (props) => {',
744+
' let team = props.names.map(() => (',
745+
' <Inner innerOne={props.one} innerTwo={props.two} />',
746+
' ));',
747+
' return <ul>{team}</ul>;',
748+
'};',
749+
'Outer.propTypes = {',
750+
' names: PropTypes.array,',
751+
' one: PropTypes.string,',
752+
' two: PropTypes.string',
753+
'};'
754+
].join('\n')
740755
}, {
741756
code: [
742757
'export default {',

0 commit comments

Comments
 (0)