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 4 commits 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
src/util/getElementType.js Outdated Show resolved Hide resolved
khiga8 and others added 2 commits May 20, 2024 16:14
Co-authored-by: Jordan Harband <ljharb@gmail.com>
@ljharb
Copy link
Member

ljharb commented May 21, 2024

we'll also need documentation.

Copy link

codecov bot commented May 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.84%. Comparing base (0d5321a) to head (dd807b6).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #984      +/-   ##
==========================================
- Coverage   99.02%   98.84%   -0.19%     
==========================================
  Files         105      105              
  Lines        1636     1640       +4     
  Branches      579      580       +1     
==========================================
+ Hits         1620     1621       +1     
- Misses         16       19       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants