jsx-eslint / eslint-plugin-react

React-specific linting rules for ESLint
MIT License
8.94k stars 2.77k forks source link

Forbid propTypes/defaultProps entirely #2658

Closed thany closed 2 years ago

thany commented 4 years ago

I have a use-case where I need to forbid propTypes/defaultProps entirely, on certain files.

Forbidding proptypes entirely can be done in a bit of a hacky way:

"react/forbid-prop-types": [ "error", { "forbid": [
  "any", "array", "bool", "func", "number",
  "object", "string", "node", "element", "symbol",
  "elementType", "instanceOf", "oneOf", "oneOfType",
  "arrayOf", "objectOf", "shape", "exact"
] } ]

Basically forbidding every known propType type. However, this doesn't forbid the presence of propTypes alltogether. It also gives a misleading message when it is encountered as an linting error.

There also doesn't seem to be a way to forbid defaultProps entirely. Am I missing something?

ljharb commented 4 years ago

What is this use case?

thany commented 4 years ago

We have several components that are named after a certain pattern, in our case their names end in Section. These are meant to be used by Sitecore JSS. These "section" components are nothing but proxies of another component. The props to these components are passed in by JSS in a single prop called fields.

We don't want any propTypes there, because it would mean doubling them up. The proxied component are already enriched with propTypes, so any checking is done there. It would also mean we'd have to do some trickery to avoid having to redefine all those props in a fields shape.

In short, these "section" components do not need propTypes. They are completely useless.

And the same would go for the corresponding defaultProps.

ljharb commented 4 years ago

In my experience with these kind of components, it's very valuable to hoist the propTypes from the wrapped component onto the wrapper component, so that they do trigger propType warnings, even when shallow rendered.

There's even a package for doing it easily: https://www.npmjs.com/package/hoist-non-react-statics

thany commented 4 years ago

I would guess that even with that package, it would become even less useful to not write explicit propTypes in those proxy components. But eslint would still warn about them not being there.

ljharb commented 4 years ago

Sure, but in the rare cases where you're doing this, you can use an eslint override comment.

thany commented 4 years ago

They are not rare. It's one kind of component, but there are many of them.

ljharb commented 4 years ago

You could also name them with a convention, and use eslint's "overrides" with a glob to impose the no-restricted-syntax rule and configure it to block any assignment to .propTypes.

thany commented 4 years ago

Didn't know about that one. Thanks. I'll check it out.