Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow polymorphic linting to be restricted to specified components #984

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

khiga8
Copy link
Contributor

@khiga8 khiga8 commented May 6, 2024

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.

  • When polymorphicAllowlist is undefined, the polymorphic prop will be used for any components of the prop name specified by polymorphicPropName is present.
  • When polymorphicAllowlist is defined, then there is an additional check to make sure that components are part of the polymorphicAllowlist before we use the polymorphic prop specified by polymorphicPropName.

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 an img by default and automatically render an alt based on a username. The linter may raise a false positive if Avatar is linted as an img for the alt text lint rule. This can be avoided by not adding the Avatar to the jsx-a11y component map. IfAvatar (for whatever reason) explicitly has as="img" set, since it can also be rendered as an svg, it will end up automatically linted via the polymorphicPropName 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).

This changes allows the consumer to restrict polymorphic linting to specified components.
Linting components may raise false positives when a component handles behavior that the linter has no way to know.

This means that linting components is preferred on very basic utility components.
@khiga8 khiga8 marked this pull request as ready for review May 7, 2024 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant