feat: Allow polymorphic linting to be restricted to specified components #984
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Change
This builds on the setting introduced in #945.
This change allows the consumer to restrict the polymorphic setting to a specified list of components via a new optional setting,
polymorphicAllowlist
.polymorphicAllowlist
is undefined, the polymorphic prop will be used for any components of the prop name specified bypolymorphicPropName
is present.polymorphicAllowlist
is defined, then there is an additional check to make sure that components are part of thepolymorphicAllowlist
before we use the polymorphic prop specified bypolymorphicPropName
.Motivation
Linting components can raise false positives when a component handles behavior that the linter has no way to know.
For example, an
Avatar
component may render as animg
by default and automatically render analt
based on a username. The linter may raise a false positive ifAvatar
is linted as animg
for the alt text lint rule. This can be avoided by not adding theAvatar
to thejsx-a11y
component map. IfAvatar
(for whatever reason) explicitly hasas="img"
set, since it can also be rendered as ansvg
, it will end up automatically linted via thepolymorphicPropName
setting.In some projects, polymorphic linting may be useful but it may be safer on utility/basic components that don't do much (e.g. a generic
Box
element), rather than opening it up to any component that allows the polymorphic prop to be set.I acknowledge that polymorphism is not an ideal pattern that can add complexity (as warned in the README).