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

Conversation

dustinsoftware
Copy link
Contributor

Opening this PR for discussion. This PR fixes this false error:

'one' PropType is defined but prop is never used

Caused by this test case:

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
};

However, looks like my fix caused a few other tests to fail. I intend to push an update once I do some more investigation.

@@ -234,8 +234,7 @@ module.exports = {
* @returns {Boolean} True if the prop is used, false if not.
*/
function isPropUsed(node, prop) {
for (let i = 0, l = node.usedPropTypes.length; i < l; i++) {
const usedProp = node.usedPropTypes[i];
for (const usedProp of node.usedPropTypes) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer to avoid for..of in favor of .forEach or .map

' two: PropTypes.string',
'};'
].join('\n'),
parser: 'babel-eslint'
Copy link
Member

Choose a reason for hiding this comment

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

nothing about this test case should require babel-eslint; can it use the default parser instead?

@@ -52,8 +52,10 @@ Components.prototype.add = function(node, confidence) {
* @returns {Object} Component object, undefined if the component is not found
*/
Components.prototype.get = function(node) {
Copy link
Member

Choose a reason for hiding this comment

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

Are there any tests for this change, btw?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't see a good place to put a test for this specific change, and the logic behind get changes, the test I did add will fail.

@dustinsoftware
Copy link
Contributor Author

Updated

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Your fix LGTM. I'm not stoked on the findParent arg, but I'll let other collabs weigh in.

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.

@@ -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>;',
'};'

@dustinsoftware
Copy link
Contributor Author

@DianaSuvorova had a better fix in #1322 😄 This PR isn't needed anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants