Eslint-plugin-react: no-unused-prop-types does not recognize use of prop inside Array.map()

Created on 11 Sep 2016  路  9Comments  路  Source: yannickcr/eslint-plugin-react

In the following example, no-unused-prop-types will flag a non-use of arrayOfObjWithFoo.*.foo:

    static PropTypes = {
      arrayOfObjWithFoo: PropTypes.arrayOf(
        PropTypes.shape({ foo: PropTypes.string.isRequired })
      ).isRequired
    };

    render() {
      const fooItems = this.props.arrayOfObjWithFoo.map(objWithFoo => {
        <Item foo={objWithFoo.foo} />
      });
    }
bug

Most helpful comment

I'm saying that if that option is required for the rule to be usable when using shapes, then the rule should never check shapes.

All 9 comments

Duplicate of #871.

The no-unused-prop-types rule does not support shape props at the moment as such detection is very difficult. If you use shape props, I recommend setting the skipShapeProps option to true on the rule.

Again, this is still a bug. If it doesn't support shape props, it should not warn on them at all.

If you set the skipShapeProps option to true, it should stop warning on all shape props. Is that not working?

I'm saying that if that option is required for the rule to be usable when using shapes, then the rule should never check shapes.

@ljharb That's a valid point. I'll look into whether or not it makes sense to set skipShapeProps to true as a default.

I agree that skipShapeProps should default to true if it can't properly detect usage of props in shape.

The current behavior contradicts the forbid-prop-types rule since that encourages the use of shape instead of object.

@EvNaverniouk any progress here?

@ljharb Just took a few minutes now to look into this but wasn't able to get far. This issue is talking about 2 things:

  1. The code snippet @smsenesac originally posted is supposedly throwing an error when it shouldn't.
    I cannot reproduce the failure. I added a test for this here: https://github.com/EvNaverniouk/eslint-plugin-react/commit/15e2fd8448e415b1a3961bcbbe62e3e9cdb93580 but the test isn't failing for me. So I wonder if maybe this has already been fixed in another commit.
  2. Setting skipShapeProps to true by default.
    This is already done. See latest master code here: https://github.com/yannickcr/eslint-plugin-react/blob/master/lib/rules/no-unused-prop-types.js#L51

Don't think there are any further action items on this issue. Can we close?

Seems reasonable.

Was this page helpful?
0 / 5 - 0 ratings