-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule S6821 (
jsx-a11y/aria-role
): DOM elements with ARIA roles s…
…hould have a valid non-abstract role (#4268)
- Loading branch information
1 parent
538ae84
commit ccb2988
Showing
7 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
its/ruling/src/test/expected/jsts/vuetify/typescript-S6821.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 @@ | ||
{ | ||
"vuetify:packages/vuetify/src/components/VDivider/VDivider.tsx": [ | ||
65 | ||
] | ||
} |
49 changes: 49 additions & 0 deletions
49
sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/AriaRoleCheck.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,49 @@ | ||
/** | ||
* 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 java.util.Collections; | ||
import java.util.List; | ||
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; | ||
|
||
@TypeScriptRule | ||
@JavaScriptRule | ||
@Rule(key = "S6821") | ||
public class AriaRoleCheck implements EslintBasedCheck { | ||
|
||
@Override | ||
public String eslintKey() { | ||
return "aria-role"; | ||
} | ||
|
||
@Override | ||
public List<Object> configurations() { | ||
return Collections.singletonList(new Config()); | ||
} | ||
|
||
private static class Config { | ||
|
||
boolean ignoreNonDOM = true; | ||
String[] allowedInvalidRoles = { "text" }; | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6821.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,30 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>ARIA (Accessible Rich Internet Applications) attributes are used to enhance the accessibility of web content and web applications. These attributes | ||
provide additional information about an element’s role, state, properties, and values to assistive technologies like screen readers.</p> | ||
<p>Each role in ARIA has a set of required attributes that must be included for the role to be properly understood by assistive technologies. These | ||
attributes are known as "required aria-* properties".</p> | ||
<p>For example, if an element has a role of "checkbox", it must also include the aria-checked property. This property indicates whether the checkbox | ||
is checked (true), unchecked (false), or in a mixed state (mixed).</p> | ||
<p>This rules checks that each element with a defined ARIA role also has all required attributes.</p> | ||
<h2>How to fix it in JSX</h2> | ||
<p>Check that each element with a defined ARIA role also has all required attributes.</p> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
<div role="checkbox">Unchecked</div> {/* Noncompliant: aria-checked is missing */} | ||
</pre> | ||
<p>To fix the code add missing aria-* attributes.</p> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
<div role="checkbox" aria-checked={isChecked}>Unchecked</div> | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques">MDN - Using ARIA: Roles, states, and properties</a> | ||
</li> | ||
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles">MDN - ARIA roles (Reference)</a> </li> | ||
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes">MDN - ARIA states and properties (Reference)</a> </li> | ||
</ul> | ||
<h3>Standards</h3> | ||
<ul> | ||
<li> <a href="https://www.w3.org/TR/wai-aria-1.2/">W3C - Accessible Rich Internet Applications (WAI-ARIA) 1.2</a> </li> | ||
</ul> | ||
|
30 changes: 30 additions & 0 deletions
30
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6821.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,30 @@ | ||
{ | ||
"title": "DOM elements with ARIA roles should have a valid non-abstract role", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"react", | ||
"a11y" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6821", | ||
"sqKey": "S6821", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "infeasible", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "LOW", | ||
"RELIABILITY": "MEDIUM" | ||
}, | ||
"attribute": "LOGICAL" | ||
}, | ||
"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 |
---|---|---|
|
@@ -293,6 +293,7 @@ | |
"S6775", | ||
"S6793", | ||
"S6807", | ||
"S6811" | ||
"S6811", | ||
"S6821" | ||
] | ||
} |
35 changes: 35 additions & 0 deletions
35
...plugin/javascript-checks/src/test/java/org/sonar/javascript/checks/AriaRoleCheckTest.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,35 @@ | ||
/* | ||
* 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 static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.gson.Gson; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class AriaRoleCheckTest { | ||
|
||
@Test | ||
void config() { | ||
String configAsString = new Gson().toJson(new AriaRoleCheck().configurations()); | ||
assertThat(configAsString) | ||
.isEqualTo("[{\"ignoreNonDOM\":true,\"allowedInvalidRoles\":[\"text\"]}]"); | ||
} | ||
} |