-
Notifications
You must be signed in to change notification settings - Fork 21
Added rule enforcing Image alt text #155
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b80d3d9
Fixed create rule script
rbitting 653be71
Added rule to require alt text on image
rbitting e6f06e2
Increased coverage for image alt rule
rbitting dd4e747
Allow empty string as Image alt
rbitting c62d365
Doc fix
rbitting 23a0211
Improved coverage for coveralls
rbitting 9e09a31
PR feedback
rbitting 7754859
Fixed issues from rebase
rbitting File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Accessibility: Image must have alt attribute with a meaningful description of the image. If the image is decorative, use alt="" (`@microsoft/fluentui-jsx-a11y/image-needs-alt`) | ||
|
|
||
| 💼 This rule is enabled in the ✅ `recommended` config. | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| ## Rule details | ||
|
|
||
| This rule requires all `<Image>` components have non-empty alternative text. The `alt` attribute should provide a clear and concise text replacement for the image's content. It should *not* describe the presence of the image itself or the file name of the image. Purely decorative images should have empty `alt` text (`alt=""`). | ||
|
|
||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```jsx | ||
| <Image src="image.png" /> | ||
| ``` | ||
|
|
||
| ```jsx | ||
| <Image src="image.png" alt={null} /> | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```jsx | ||
| <Image src="image.png" alt="A dog playing in a park." /> | ||
| ``` | ||
|
|
||
| ```jsx | ||
| <Image src="decorative-image.png" alt="" /> | ||
| ``` | ||
|
|
||
| ## Further Reading | ||
|
|
||
| - [`<img>` Accessibility](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/img#accessibility) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { ESLintUtils } from "@typescript-eslint/utils"; | ||
| import { makeLabeledControlRule } from "../util/ruleFactory"; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Rule Definition | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const rule = ESLintUtils.RuleCreator.withoutDocs( | ||
| makeLabeledControlRule({ | ||
| component: "Image", | ||
| messageId: "imageNeedsAlt", | ||
| description: | ||
| 'Accessibility: Image must have alt attribute with a meaningful description of the image. If the image is decorative, use alt="".', | ||
| requiredProps: ["alt"], | ||
| allowFieldParent: false, | ||
| allowHtmlFor: false, | ||
| allowLabelledBy: false, | ||
| allowWrappingLabel: false, | ||
| allowTooltipParent: false, | ||
| allowDescribedBy: false, | ||
| allowLabeledChild: false | ||
| }) | ||
| ); | ||
|
|
||
| export default rule; |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { TSESTree } from "@typescript-eslint/utils"; | ||
| import { JSXOpeningElement } from "estree-jsx"; | ||
| import { hasProp, getPropValue, getProp } from "jsx-ast-utils"; | ||
|
|
||
| /** | ||
| * Determines if the property exists and has a non-nullish value. | ||
| * @param attributes The attributes on the visited node | ||
| * @param name The name of the prop to check | ||
| * @returns Whether the specified prop exists and is not null or undefined | ||
| * @example | ||
| * // <img src="image.png" /> | ||
| * hasDefinedProp(attributes, 'src') // true | ||
| * // <img src="" /> | ||
| * hasDefinedProp(attributes, 'src') // true | ||
| * // <img src={null} /> | ||
| * hasDefinedProp(attributes, 'src') // false | ||
| * // <img src={undefined} /> | ||
| * hasDefinedProp(attributes, 'src') // false | ||
| * // <img src={1} /> | ||
| * hasDefinedProp(attributes, 'src') // false | ||
| * // <img src={true} /> | ||
| * hasDefinedProp(attributes, 'src') // false | ||
| * // <img /> | ||
| * hasDefinedProp(attributes, 'src') // false | ||
| */ | ||
| const hasDefinedProp = (attributes: TSESTree.JSXOpeningElement["attributes"], name: string): boolean => { | ||
| if (!hasProp(attributes as JSXOpeningElement["attributes"], name)) { | ||
| return false; | ||
| } | ||
|
|
||
| const prop = getProp(attributes as JSXOpeningElement["attributes"], name); | ||
|
|
||
| // Safely get the value of the prop, handling potential undefined or null values | ||
| const propValue = prop ? getPropValue(prop) : undefined; | ||
|
|
||
| // Return true if the prop value is not null or undefined | ||
| return propValue !== null && propValue !== undefined; | ||
| }; | ||
|
|
||
| export { hasDefinedProp }; | ||
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /** | ||
| * Helper method to convert LF line endings to CRLF line endings | ||
| * @remarks This is needed to avoid prettier formatting issues on generation of the files | ||
| * @param text The text to convert | ||
| * @returns The converted text | ||
| */ | ||
| const withCRLF = text => text.replace(/\n/g, "\r\n"); | ||
|
|
||
| module.exports = { withCRLF }; |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { Rule } from "eslint"; | ||
| import ruleTester from "./helper/ruleTester"; | ||
| import rule from "../../../lib/rules/image-needs-alt"; | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| // Tests | ||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| ruleTester.run("image-needs-alt", rule as unknown as Rule.RuleModule, { | ||
| valid: [ | ||
| // Not an Image | ||
| "<div></div>", | ||
| // Valid string test | ||
| '<Image src="image.png" alt="Description of image" />', | ||
| // Valid expression test | ||
| '<Image src="image.png" alt={altText} />', | ||
| // Decorative image with empty alt | ||
| '<Image src="image.png" alt="" />' | ||
| ], | ||
| invalid: [ | ||
| { | ||
| // No alt attribute | ||
| code: '<Image src="image.png" />', | ||
| errors: [{ messageId: "imageNeedsAlt" }] | ||
| }, | ||
| { | ||
| // Null alt attribute | ||
| code: '<Image src="image.png" alt={null} />', | ||
| errors: [{ messageId: "imageNeedsAlt" }] | ||
| }, | ||
| { | ||
| // Undefined alt attribute | ||
| code: '<Image src="image.png" alt={undefined} />', | ||
| errors: [{ messageId: "imageNeedsAlt" }] | ||
| } | ||
| ] | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is already a function that checks for a non-empty prop? Maybe that function could be adjusted to include null and undefined checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's
hasNonEmptyPropbut adding a flag to that to specifically allow an empty value seemed counter-intuitive. Could rename it fromhasNonEmptyProptohasDefinedPropwith a flag to allow non-empty values, but that would require a lot of files to be touched for the name change and I was trying to keep this PR small-ish. If you feel strongly about this I could implement this in a follow-up PR?