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

[New] allow polymorphic linting to be restricted to specified components #984

Merged
merged 1 commit into from
Sep 4, 2024

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
@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 (882bd4d) to head (dd807b6).

Current head dd807b6 differs from pull request most recent head 2178c5f

Please upload reports for the commit 2178c5f to get more accurate results.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #984      +/-   ##
==========================================
- Coverage   99.03%   98.84%   -0.19%     
==========================================
  Files         107      105       -2     
  Lines        1651     1640      -11     
  Branches      581      580       -1     
==========================================
- Hits         1635     1621      -14     
- Misses         16       19       +3     

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

@khiga8 khiga8 force-pushed the kh-restrict-polymorphic-linting branch from dd807b6 to e928282 Compare May 28, 2024 12:45
@khiga8 khiga8 requested a review from ljharb May 28, 2024 16:24
@khiga8
Copy link
Contributor Author

khiga8 commented Jun 19, 2024

This is ready for another review 🙇‍♀️

@ljharb ljharb force-pushed the kh-restrict-polymorphic-linting branch from 2178c5f to 6cd1a70 Compare September 4, 2024 05:48
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i still find polymorphic components horrific, and i hope you're able to fix your codebase by removing them :-) but let's land this

@ljharb ljharb changed the title feat: Allow polymorphic linting to be restricted to specified components [New] allow polymorphic linting to be restricted to specified components Sep 4, 2024
@ljharb ljharb merged commit 6cd1a70 into jsx-eslint:main Sep 4, 2024
105 checks passed
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.

2 participants