Skip to content
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

Create rule S6842 (jsx-a11y/no-noninteractive-element-to-interactive-role): Non-interactive DOM elements should not have interactive ARIA roles #4366

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"desktop:app/src/ui/lib/vertical-segmented-control/segmented-item.tsx": [
71
],
"desktop:app/src/ui/lib/vertical-segmented-control/vertical-segmented-control.tsx": [
215
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
NoNestedTemplateLiteralsCheck.class,
NoNewNativeNonconstructorCheck.class,
NoNonNullAssertionCheck.class,
NoNoninteractiveElementToInteractiveRoleCheck.class,
NoOctalEscapeCheck.class,
NoOneIterationLoopCheck.class,
NoOsCommandsFromPathCheck.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;

@JavaScriptRule
@TypeScriptRule
@Rule(key = "S6842")
public class NoNoninteractiveElementToInteractiveRoleCheck implements EslintBasedCheck {

@Override
public String eslintKey() {
return "no-noninteractive-element-to-interactive-role";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<h2>Why is this an issue?</h2>
<p>Non-interactive DOM elements are those that do not have built-in interactivity or do not require user interaction. Examples include
<code>&lt;div&gt;</code>, <code>&lt;p&gt;</code>, <code>&lt;img&gt;</code>, <code>&lt;h1&gt;</code> to <code>&lt;h6&gt;</code>, and
<code>&lt;ul&gt;</code>, among others. These elements are typically used to structure content and layout but do not have any inherent interactive
behavior.</p>
<p>Interactive ARIA roles, on the other hand, are used to make elements interactive and accessible. These roles include <code>button</code>,
<code>link</code>, <code>checkbox</code>, <code>menuitem</code>, <code>tab</code>, and others. They are used to communicate the type of interaction
that users can expect from an element.</p>
<p>Non-interactive DOM elements should not use interactive ARIA roles because it can confuse assistive technologies and their users. For example, if a
<code>&lt;div&gt;</code> (a non-interactive element) is given an interactive role like "button", assistive technologies like screen readers will
announce it as a button. However, since <code>&lt;div&gt;</code> doesn’t have the inherent behavior of a button, it can confuse users who expect it to
behave like a button when interacted with.</p>
<p>This can lead to a poor user experience and can make the website less accessible for users relying on assistive technologies. Therefore, it’s
important to ensure that non-interactive DOM elements are not given interactive ARIA roles.</p>
<h2>How to fix it</h2>
<p>Ensure that non-interactive DOM elements do not use interactive ARIA roles.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
function myListElement() {
return &lt;li role="button"&gt;Foo&lt;/li&gt;; // Noncompliant; "li" isn't interactive, but "button" is
}
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
function myListElement() {
return &lt;li role="listitem"&gt;Foo&lt;/li&gt;;
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> WCAG - <a href="https://www.w3.org/WAI/WCAG21/Understanding/name-role-value">Name, Role, Value</a> </li>
<li> WCAG - <a href="https://www.w3.org/TR/wai-aria-1.1/#usage_intro">WAI-ARIA Roles</a> </li>
<li> MDN web docs - <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles">WAI-ARIA Roles</a> </li>
</ul>

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"title": "Non-interactive DOM elements should not have interactive ARIA roles",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"accessibility",
"react"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6842",
"sqKey": "S6842",
"scope": "All",
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW",
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
},
"compatibleLanguages": [
"JAVASCRIPT",
"TYPESCRIPT"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@
"S6825",
"S6827",
"S6836",
"S6841"
"S6841",
"S6842"
]
}