Eslint-plugin-react: no-unused-prop-types doesn't appear to work with static propTypes

Created on 27 Jul 2017  路  4Comments  路  Source: yannickcr/eslint-plugin-react

I'm not sure how many people will be affected by this but leveraging transform-class-properties will not result in errors when using react/no-unused-prop-types. Sample code below:

class MyComponent extends React.PureComponent
{
    static propTypes =
    {
        unused: PropTypes.string // Should show error
        /* ... */
    }

    static defaultProps =
    {
        /* ... */
    }

    render()
    {
        /* ... */
    }
}
bug help wanted

Most helpful comment

Second code block was a bug with the latest release. But should be fixed on master.

All 4 comments

@dkrutsko generally it does. Do you have a complete use case I can take a look at?

@DianaSuvorova I did some further testing. It turns out that the example above won't work unless this.props is used somewhere. So the following example will work:

class MyComponent extends React.PureComponent
{
    static propTypes =
    {
          used: PropTypes.string,
        unused: PropTypes.string
    }

    render()
    {
        return <div>{this.props.used}</div>;
    }
}

But this one won't:

class MyComponent extends React.PureComponent
{
    static propTypes =
    {
          used: PropTypes.string,
        unused: PropTypes.string
    }

    render()
    {
        return <div></div>;
    }
}

Moreover, in my original test, the reason it wasn't working was because I did something like:

let
{
    used,
    ...attributes
} = this.props;

Which I guess makes sense because I'm grabbing the result of the attributes but I'm not necessarily using them. I can't tell whether this is a bug or not. Hope this information helps!

Indeed, once you've spread "attributes" out, the plugin can no longer follow that it's from props.

I'd recommend explicitly destructuring all props you need out of this.props at the top of every function you need them in.

It sounds like your second code block is a bug, though.

Second code block was a bug with the latest release. But should be fixed on master.

Was this page helpful?
0 / 5 - 0 ratings