Skip to content

Fix incorrect propType warning inside .map #1304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing that this would only happen address direct map() calls. So if you have a nested map, it would still throw a false positive right? eg.

'const Outer = (props) => {',
'  let team = props.names.map(() => (',
'      ["anotherThing"].map(() => {',
'            <Inner innerOne={props.one} innerTwo={props.two} />',
'      });',
'    ));',
'  return <ul>{team}</ul>;',
'};'

const usedPropTypes = component && component.usedPropTypes || [];
let ignorePropsValidation = component && component.ignorePropsValidation || false;

Expand Down
9 changes: 6 additions & 3 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is used very heavily across many rules.

I would not recommend adding this new argument here and instead handling the findParent logic inside the no-unused-prop-types rule directly.

while (findParent && node && !this._list[this._getId(node)]) {
node = node.parent;
}
return this._list[this._getId(node)];
};

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,21 @@ ruleTester.run('no-unused-prop-types', rule, {
'};'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'const Inner = (props) => <span>{props.innerOne} {props.innerTwo}</span>;',
'const Outer = (props) => {',
' let team = props.names.map(() => (',
' <Inner innerOne={props.one} innerTwo={props.two} />',
' ));',
' return <ul>{team}</ul>;',
'};',
'Outer.propTypes = {',
' names: PropTypes.array,',
' one: PropTypes.string,',
' two: PropTypes.string',
'};'
].join('\n')
}, {
code: [
'export default {',
Expand Down