fix(img-redundant-alt): only flag alts that are purely redundant or use 'word of' prefix#1085
Open
rhuyck wants to merge 3 commits intojsx-eslint:mainfrom
Open
fix(img-redundant-alt): only flag alts that are purely redundant or use 'word of' prefix#1085rhuyck wants to merge 3 commits intojsx-eslint:mainfrom
rhuyck wants to merge 3 commits intojsx-eslint:mainfrom
Conversation
…g redundant words as part of a compound description
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The current rule flags any
alttext that contains the wordsimage,photo, orpictureanywhere — which produces false positives for legitimate, descriptive alt text like"disk image","profile photo", or"satellite image overlay". These compound nouns carry real meaning and should not be flagged.This PR tightens the rule to two specific cases that are genuinely unhelpful to screen-reader users:
alt="image",alt="photo"). These are flagged as before."of"(e.g.alt="image of a cat"). Screen-readers already announce the element as an image; the"image of"prefix adds nothing — only the description after it matters. These are flagged with a separate, softer message so they can be configured at a different severity in consumer ESLint configs. I am very open to other words, like "showing," but even that one I feel could mean showing in the figurative sense..Everything else — compound nouns, descriptive labels that happen to include one of the words, template literals with meaningful context — is left alone.
Changes
src/rules/img-redundant-alt.jsstringIncludesimportcontainsRedundantWordcheck with two named functions (isOnlyRedundantWords,startsWithRedundantWordOf) that encode the intent explicitlymessages+messageIdinmetaso each case can be configured at different severity levels"imagery")__tests__/src/rules/img-redundant-alt-test.jsmessageIdinstead of hardcoded message stringspicture doing ${things}etc.) tovalid"disk image","profile photo","cover picture","satellite image overlay", etc."word of"prefix check with explanatory commentsSee here for some examples of alt texts that I believe should be entirely valid: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/pull/1085/changes#diff-c42207e7115fb61778d9d614ea8a38b2af9bcaefd070f14014c73716fcb05bb5R100-R110
All 38,607 existing tests pass.
Motivation
The rule as written is commonly perceived as overly aggressive. A rule that fires on
alt="disk image"oralt="passport photo ID"trains developers to ignore or disable it (or just use a workaround like replacing "photo" with "photograph" rather than write better alt text — the opposite of the intended effect. Tightening the signal improves compliance with the cases that actually matter.Additionally, after searching the Issues on this repo, it seems others years ago had similar thoughts: #417