Skip to content

Commit

Permalink
Add rule S6821 (jsx-a11y/aria-role): DOM elements with ARIA roles s…
Browse files Browse the repository at this point in the history
…hould have a valid non-abstract role (#4268)
  • Loading branch information
ilia-kebets-sonarsource authored Oct 13, 2023
1 parent 538ae84 commit ccb2988
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"vuetify:packages/vuetify/src/components/VDivider/VDivider.tsx": [
65
]
}
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" };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
ArgumentsCallerCalleeUsageCheck.class,
ArgumentsUsageCheck.class,
AriaProptypesCheck.class,
AriaRoleCheck.class,
ArithmeticOperationReturningNanCheck.class,
ArrayCallbackWithoutReturnCheck.class,
ArrayConstructorsCheck.class,
Expand Down
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">
&lt;div role="checkbox"&gt;Unchecked&lt;/div&gt; {/* 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">
&lt;div role="checkbox" aria-checked={isChecked}&gt;Unchecked&lt;/div&gt;
</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>

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"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@
"S6775",
"S6793",
"S6807",
"S6811"
"S6811",
"S6821"
]
}
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\"]}]");
}
}

0 comments on commit ccb2988

Please sign in to comment.