jsx-eslint / eslint-plugin-jsx-a11y

Static AST checker for a11y rules on JSX elements.
MIT License
3.39k stars 637 forks source link

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

Closed khiga8 closed 1 month ago

khiga8 commented 5 months ago

Change

This builds on the setting introduced in https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/pull/945.

This change allows the consumer to restrict the polymorphic setting to a specified list of components via a new optional setting,polymorphicAllowlist.

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).

ljharb commented 5 months ago

we'll also need documentation.

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 98.84%. Comparing base (882bd4d) to head (dd807b6).

:exclamation: 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 ```diff @@ 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 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

khiga8 commented 4 months ago

This is ready for another review 🙇‍♀️