Skip to content

Commit

Permalink
Create rule S6852 (jsx-a11y/interactive-supports-focus): Elements w…
Browse files Browse the repository at this point in the history
…ith interactive role should support focus
  • Loading branch information
saberduck committed Nov 16, 2023
1 parent 69a1e0f commit ee23aa0
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"desktop:app/src/ui/dialog/header.tsx": [
67
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
IndexOfCompareToPositiveNumberCheck.class,
InsecureCookieCheck.class,
InsecureJwtTokenCheck.class,
InteractiveElementsShouldBeFocusableCheck.class,
InstanceofInMisuseCheck.class,
IntrusivePermissionsCheck.class,
InvariantReturnCheck.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 = "S6852")
public class InteractiveElementsShouldBeFocusableCheck implements EslintBasedCheck {

@Override
public String eslintKey() {
return "interactive-supports-focus";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<p>Interactive elements being focusable is vital for website accessibility. It enables users, including those using assistive technologies, to
interact effectively with the website. Without this, some users may be unable to access certain features, leading to a poor user experience and
potential non-compliance with accessibility standards.</p>
<h2>Why is this an issue?</h2>
<pre>
Lack of focusability can hinder navigation and interaction with the website, resulting in an exclusionary user experience and possible violation of accessibility guidelines.
</pre>
<h2>How to fix it</h2>
<p>To fix this, ensure that all interactive elements on your website can receive focus. This can be achieved by using standard HTML interactive
elements, or by assigning a tabindex of "0" to custom interactive components.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
&lt;!-- Element with mouse/keyboard handler has no tabindex --&gt;
&lt;span onclick="submitForm();" role="button"&gt;Submit&lt;/span&gt;

&lt;!-- Anchor element without href is not focusable --&gt;
&lt;a onclick="showNextPage();" role="button"&gt;Next page&lt;/a&gt;
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
&lt;span onClick="doSomething();" tabIndex="0" role="button"&gt;Element with mouse handler has tabIndex&lt;/span&gt;

&lt;a href="javascript:void(0);" onClick="doSomething();"&gt;Focusable anchor with mouse handler&lt;/a&gt;
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://www.w3.org/TR/WCAG20-TECHS/H4.html">Creating a logical tab order through links, form controls, and objects</a> </li>
<li> <a href="https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav">Fundamental Keyboard Navigation Conventions</a> </li>
<li> MDN - <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/button_role#accessibility_concerns">ARIA: button role -
Accessibility concerns</a> </li>
</ul>

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"title": "Elements with interactive role should support focus",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"accessibility",
"react"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6852",
"sqKey": "S6852",
"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 @@ -319,6 +319,7 @@
"S6848",
"S6849",
"S6850",
"S6851"
"S6851",
"S6852"
]
}

0 comments on commit ee23aa0

Please sign in to comment.