-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S6851 (
jsx-a11y/img-redundant-alt
): Images should have …
…a non-redundant alternate description (#4394)
- Loading branch information
1 parent
131cd48
commit 16a9b9e
Showing
9 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
5 changes: 5 additions & 0 deletions
5
its/ruling/src/test/expected/jsts/file-for-rules/javascript-S6851.json
This file contains 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,5 @@ | ||
{ | ||
"file-for-rules:S6851.js": [ | ||
2 | ||
] | ||
} |
This file contains 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,3 @@ | ||
function MyImage() { | ||
return <img src="sunrise.jpg" alt="image of a sunrise" />; | ||
} |
This file contains 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
36 changes: 36 additions & 0 deletions
36
...gin/javascript-checks/src/main/java/org/sonar/javascript/checks/ImgRedundantAltCheck.java
This file contains 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,36 @@ | ||
/** | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.javascript.checks; | ||
|
||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.javascript.api.EslintBasedCheck; | ||
import org.sonar.plugins.javascript.api.JavaScriptRule; | ||
import org.sonar.plugins.javascript.api.TypeScriptRule; | ||
|
||
@JavaScriptRule | ||
@TypeScriptRule | ||
@Rule(key = "S6851") | ||
public class ImgRedundantAltCheck implements EslintBasedCheck { | ||
|
||
@Override | ||
public String eslintKey() { | ||
return "img-redundant-alt"; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6851.html
This file contains 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,35 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p><code>alt</code> attributes, also known as "alt tags" or "alt descriptions," are used to specify alternative text that is rendered when an image | ||
cannot be displayed. They are crucial for improving web accessibility, as they provide a text description of images for users who rely on screen | ||
readers.</p> | ||
<p>Screen readers announce the presence of an <code>img</code> element and read its <code>alt</code> attribute aloud to describe the image. If the | ||
<code>alt</code> attribute includes words like "image", "picture", or "photo", it leads to redundancy as the screen reader would repeat "image". For | ||
instance, an <code>alt</code> attribute like "image of a sunrise" would be read as "Image, image of a sunrise", unnecessarily repeating "image".</p> | ||
<p>Instead, the <code>alt</code> attribute should focus on describing the content of the image, not the fact that it is an image. This makes the | ||
browsing experience more efficient and enjoyable for users of screen readers, as they receive a concise and meaningful description of the image | ||
without unnecessary repetition.</p> | ||
<h2>How to fix it</h2> | ||
<p>To fix this issue, you should revise the <code>alt</code> attribute of your <code>img</code> elements to remove any instances of the words "image", | ||
"picture", or "photo". Instead, provide a concise and accurate description of the image content that adds value for users who cannot see the | ||
image.</p> | ||
<h3>Code examples</h3> | ||
<h4>Noncompliant code example</h4> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
function MyImage() { | ||
return <img src="sunrise.jpg" alt="image of a sunrise" />; // Noncompliant: "Image, image of a sunrise" | ||
} | ||
</pre> | ||
<h4>Compliant solution</h4> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
function MyImage() { | ||
return <img src="sunrise.jpg" alt="a sunrise over a mountain range" />; | ||
} | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> MDN web docs - <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img">img element</a> </li> | ||
<li> MDN web docs - <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt">alt property</a> </li> | ||
<li> WebAIM - <a href="https://webaim.org/techniques/alttext/">Alternative Text</a> </li> | ||
</ul> | ||
|
29 changes: 29 additions & 0 deletions
29
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6851.json
This file contains 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,29 @@ | ||
{ | ||
"title": "Images should have a non-redundant alternate description", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"accessibility", | ||
"react" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6851", | ||
"sqKey": "S6851", | ||
"scope": "All", | ||
"quickfix": "infeasible", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "LOW", | ||
"RELIABILITY": "LOW" | ||
}, | ||
"attribute": "CONVENTIONAL" | ||
}, | ||
"compatibleLanguages": [ | ||
"JAVASCRIPT", | ||
"TYPESCRIPT" | ||
] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -317,6 +317,7 @@ | |
"S6846", | ||
"S6848", | ||
"S6849", | ||
"S6850" | ||
"S6850", | ||
"S6851" | ||
] | ||
} |