Skip to content

Commit

Permalink
Create rule S6811: DOM elements with ARIA role should only have suppo…
Browse files Browse the repository at this point in the history
…rted properties (#3244)

SonarSource/SonarJS#4244
  • Loading branch information
github-actions[bot] authored Oct 12, 2023
1 parent a422950 commit b7c2c63
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rules/S6811/javascript/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"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",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW",
"RELIABILITY": "LOW"
},
"attribute": "CONVENTIONAL"
}
}
34 changes: 34 additions & 0 deletions rules/S6811/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
== Why is this an issue?

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.

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 <a href="#" /> has an implicit role of "link".

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 `aria-required` is not supported by the role `link`. Therefore, using `aria-required` on an anchor tag would violate this rule.

== How to fix it in JSX

Check the spelling of the aria-* attributes and verify that they are actually supported by the element role.

[source,javascript,diff-id=1,diff-type=noncompliant]
----
<div role="checkbox" aria-chekd={isChecked}>Unchecked</div> {/* Noncompliant: aria-chekd is not supported */}
----

To fix the code remove non-compatible attributes or replace them with the correct ones.

[source,javascript,diff-id=1,diff-type=compliant]
----
<div role="checkbox" aria-checked={isChecked}>Unchecked</div>
----

== Resources
=== Documentation

* https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques[MDN - Using ARIA: Roles, states, and properties]
* https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes[MDN - ARIA states and properties (Reference)]
* https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles[MDN - ARIA roles (Reference)]

=== Standards

* https://www.w3.org/TR/wai-aria-1.2/[W3C - Accessible Rich Internet Applications (WAI-ARIA) 1.2]
2 changes: 2 additions & 0 deletions rules/S6811/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit b7c2c63

Please sign in to comment.