Skip to content

Commit

Permalink
Add rule S6811 (jsx-a11y/role-supports-aria-props): DOM elements wi…
Browse files Browse the repository at this point in the history
…th ARIA role should only have supported properties (#4264)
  • Loading branch information
alex-sonar committed Oct 12, 2023
1 parent 33763d9 commit a89a217
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"desktop:app/src/ui/changes/changes-list.tsx": [
851
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
ReturnInSetterCheck.class,
ReturnOfBooleanExpressionCheck.class,
RoleHasRequiredAriaPropsCheck.class,
RoleSupportsAriaPropsCheck.class,
RulesOfHooksCheck.class,
SameLineConditionalCheck.class,
SelfAssignmentCheck.class,
Expand Down
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;

@TypeScriptRule
@JavaScriptRule
@Rule(key = "S6811")
public class RoleSupportsAriaPropsCheck implements EslintBasedCheck {

@Override
public String eslintKey() {
return "role-supports-aria-props";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h2>Why is this an issue?</h2>
<p>ARIA properties, also known as "aria-* properties", are special attributes used in HTML to enhance the accessibility of web elements. They provide
additional semantics to help assistive technologies, like screen readers, interpret the element.</p>
<p>Roles, on the other hand, define what an element is or does in the context of a web page. Some elements have explicit roles, which are directly
defined by the developer. For example, a div element might be given a role of "button". Other elements have implicit roles, which are inferred based
on the type of the element. For example, an anchor tag &lt;a href="#" /&gt; has an implicit role of "link".</p>
<p>This rule ensures that the ARIA properties used on an element are ones that are supported by the role of that element. For instance, the ARIA
property <code>aria-required</code> is not supported by the role <code>link</code>. Therefore, using <code>aria-required</code> on an anchor tag would
violate this rule.</p>
<h2>How to fix it in JSX</h2>
<p>Check the spelling of the aria-* attributes and verify that they are actually supported by the element role.</p>
<pre data-diff-id="1" data-diff-type="noncompliant">
&lt;div role="checkbox" aria-chekd={isChecked}&gt;Unchecked&lt;/div&gt; {/* Noncompliant: aria-chekd is not supported */}
</pre>
<p>To fix the code remove non-compatible attributes or replace them with the correct ones.</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/Attributes">MDN - ARIA states and properties (Reference)</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles">MDN - ARIA roles (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,29 @@
{
"title": "DOM elements with ARIA role should only have supported properties",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"a11y",
"react"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6811",
"sqKey": "S6811",
"scope": "All",
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW",
"RELIABILITY": "LOW"
},
"attribute": "CONVENTIONAL"
},
"compatibleLanguages": [
"JAVASCRIPT",
"TYPESCRIPT"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@
"S6772",
"S6774",
"S6775",
"S6807"
"S6807",
"S6811"
]
}

0 comments on commit a89a217

Please sign in to comment.