Eslint-plugin-react: propTypes not validating on classes that extend a subclass of React.Component

Created on 6 Feb 2018  路  9Comments  路  Source: yannickcr/eslint-plugin-react

We are using a subclass of React.Component to handle exceptions thrown in render().
PropTypes validation doesn't seem to work with anything subclassed.
Looking for a solution I came upon this issue - https://github.com/yannickcr/eslint-plugin-react/issues/632#issuecomment-363256305.

It states that adding JSDoc comment should solve the issue, meanwhile, it is not.

An example to test this behaviour is available here (run npm test):
https://github.com/seavan/eslint-proptypes-subclass-test

Expected output:

two warnings on missing props validation (for class extending React.Component and for class extending SafeComponent which is a subclass of React.Component).

Actual output:

only one warning, for the class extending React.Component.

question

Most helpful comment

I think JSDoc needs one more *. Try to use /** @extends React.Component */.

All 9 comments

(For posterity: subclassing React.Component is considered a very bad antipattern, and the community as well as the React team advises against it)

The /** @extends React.Component */ comment needs to go on the subclass - on the thing that is trying to pretend it extends React.Component. In your example code, you have it on SafeComponent but you need it on TestEslint.

@ljharb it doesn't work that way either (updated the repo)

it seems like it always returns here:

https://github.com/yannickcr/eslint-plugin-react/blob/cfd1c3461d01a14a92f665ae89498b1c407085ea/lib/util/Components.js#L235

I debugged this piece of code and comment is always null for my example.

is sourceCode.getJSDocComment(node); throwing, or returning null?

it's returning null.

It doesn't seem like we have tests for this behavior; we should add them when fixing this.

I think JSDoc needs one more *. Try to use /** @extends React.Component */.

@pauldijou it worked, thank you!

Was this page helpful?
0 / 5 - 0 ratings