Skip to content

Commit 389c10b

Browse files
Merge pull request #2569 from NullVoxPopuli/nvp/template-lint-extract-rule-template-no-redundant-role
Extract rule: template-no-redundant-role
2 parents b054aa5 + 5bebdec commit 389c10b

4 files changed

Lines changed: 510 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ rules in templates can be disabled with eslint directives with mustache or html
197197
| [template-no-nested-interactive](docs/rules/template-no-nested-interactive.md) | disallow nested interactive elements | | | |
198198
| [template-no-nested-landmark](docs/rules/template-no-nested-landmark.md) | disallow nested landmark elements | | | |
199199
| [template-no-pointer-down-event-binding](docs/rules/template-no-pointer-down-event-binding.md) | disallow pointer down event bindings | | | |
200+
| [template-no-redundant-role](docs/rules/template-no-redundant-role.md) | disallow redundant role attributes | | 🔧 | |
200201
| [template-no-unsupported-role-attributes](docs/rules/template-no-unsupported-role-attributes.md) | disallow ARIA attributes that are not supported by the element role | | 🔧 | |
201202
| [template-no-whitespace-within-word](docs/rules/template-no-whitespace-within-word.md) | disallow excess whitespace within words (e.g. "W e l c o m e") | | | |
202203
| [template-require-aria-activedescendant-tabindex](docs/rules/template-require-aria-activedescendant-tabindex.md) | require non-interactive elements with aria-activedescendant to have tabindex | | 🔧 | |
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# ember/template-no-redundant-role
2+
3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
5+
<!-- end auto-generated rule header -->
6+
7+
The rule checks for redundancy between any semantic HTML element with a default/implicit ARIA role and the role provided.
8+
9+
For example, if a landmark element is used, any role provided will either be redundant or incorrect. This rule ensures that no role attribute is placed on any of the landmark elements, with the following exceptions:
10+
11+
- a `nav` element with the `navigation` role to [make the structure of the page more accessible to user agents](https://www.w3.org/WAI/GL/wiki/Using_HTML5_nav_element#Example:The_.3Cnav.3E_element)
12+
- a `form` element with the `search` role to [identify the form's search functionality](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/search_role#examples)
13+
- a `input` element with `combobox` role to [identify the input as a combobox](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-autocomplete-both/)
14+
15+
## Examples
16+
17+
This rule **forbids** the following:
18+
19+
```hbs
20+
<header role='banner'></header>
21+
```
22+
23+
```hbs
24+
<main role='main'></main>
25+
```
26+
27+
```hbs
28+
<aside role='complementary'></aside>
29+
```
30+
31+
```hbs
32+
<footer role='contentinfo'></footer>
33+
```
34+
35+
```hbs
36+
<form role='form'></form>
37+
```
38+
39+
This rule **allows** the following:
40+
41+
```hbs
42+
<form role='search'></form>
43+
```
44+
45+
```hbs
46+
<nav role='navigation'></nav>
47+
```
48+
49+
```hbs
50+
<input role='combobox' />
51+
```
52+
53+
## Configuration
54+
55+
- boolean -- if `true`, default configuration is applied
56+
57+
- object -- containing the following property:
58+
- boolean -- `checkAllHTMLElements` -- if `true`, the rule checks for redundancy between any semantic HTML element with a default/implicit ARIA role and the role provided, instead of just landmark roles (default: `true`)
59+
60+
## References
61+
62+
- [Landmark Roles (WAI-ARIA spec)](https://www.w3.org/WAI/PF/aria/roles#landmark_roles)
63+
- [Using ARIA landmarks to identify regions of a page](https://www.w3.org/WAI/WCAG21/Techniques/aria/ARIA11)
64+
- [Document conformance requirements for use of ARIA attributes in HTML](https://www.w3.org/TR/html-aria/#docconformance)
65+
- [ARIA Spec, ARIA Adds Nothing to Default Semantics of Most HTML Elements](https://www.w3.org/TR/using-aria/#aria-does-nothing)
66+
- [Disabling a link](https://www.scottohara.me/blog/2021/05/28/disabled-links.html)
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
const DEFAULT_CONFIG = {
2+
checkAllHTMLElements: true,
3+
};
4+
5+
function parseConfig(config) {
6+
if (config === true) {
7+
return DEFAULT_CONFIG;
8+
}
9+
return { ...DEFAULT_CONFIG, ...config };
10+
}
11+
12+
function createErrorMessageLandmarkElement(element, role) {
13+
return `Use of redundant or invalid role: ${role} on <${element}> detected. If a landmark element is used, any role provided will either be redundant or incorrect.`;
14+
}
15+
16+
function createErrorMessageAnyElement(element, role) {
17+
return `Use of redundant or invalid role: ${role} on <${element}> detected.`;
18+
}
19+
20+
// https://www.w3.org/TR/html-aria/#docconformance
21+
const LANDMARK_ROLES = new Set([
22+
'banner',
23+
'main',
24+
'complementary',
25+
'search',
26+
'form',
27+
'navigation',
28+
'contentinfo',
29+
]);
30+
31+
const ALLOWED_ELEMENT_ROLES = [
32+
{ name: 'nav', role: 'navigation' },
33+
{ name: 'form', role: 'search' },
34+
{ name: 'ol', role: 'list' },
35+
{ name: 'ul', role: 'list' },
36+
{ name: 'a', role: 'link' },
37+
{ name: 'input', role: 'combobox' },
38+
];
39+
40+
// Mapping of roles to their corresponding HTML elements
41+
// From https://www.w3.org/TR/html-aria/
42+
const ROLE_TO_ELEMENTS = {
43+
article: ['article'],
44+
banner: ['header'],
45+
button: ['button'],
46+
cell: ['td'],
47+
checkbox: ['input'],
48+
columnheader: ['th'],
49+
complementary: ['aside'],
50+
contentinfo: ['footer'],
51+
definition: ['dd'],
52+
dialog: ['dialog'],
53+
document: ['body'],
54+
figure: ['figure'],
55+
form: ['form'],
56+
grid: ['table'],
57+
gridcell: ['td'],
58+
group: ['details', 'fieldset', 'optgroup'],
59+
heading: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
60+
img: ['img'],
61+
link: ['a'],
62+
list: ['ol', 'ul'],
63+
listbox: ['select'],
64+
listitem: ['li'],
65+
main: ['main'],
66+
navigation: ['nav'],
67+
option: ['option'],
68+
radio: ['input'],
69+
region: ['section'],
70+
row: ['tr'],
71+
rowgroup: ['tbody', 'tfoot', 'thead'],
72+
rowheader: ['th'],
73+
search: ['search'],
74+
searchbox: ['input'],
75+
separator: ['hr'],
76+
slider: ['input'],
77+
spinbutton: ['input'],
78+
status: ['output'],
79+
table: ['table'],
80+
term: ['dfn', 'dt'],
81+
textbox: ['input', 'textarea'],
82+
};
83+
84+
/** @type {import('eslint').Rule.RuleModule} */
85+
module.exports = {
86+
meta: {
87+
type: 'suggestion',
88+
docs: {
89+
description: 'disallow redundant role attributes',
90+
category: 'Accessibility',
91+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-redundant-role.md',
92+
templateMode: 'both',
93+
},
94+
fixable: 'code',
95+
schema: [
96+
{
97+
type: 'object',
98+
properties: {
99+
checkAllHTMLElements: {
100+
type: 'boolean',
101+
},
102+
},
103+
additionalProperties: false,
104+
},
105+
],
106+
messages: {},
107+
originallyFrom: {
108+
name: 'ember-template-lint',
109+
rule: 'lib/rules/no-redundant-role.js',
110+
docs: 'docs/rule/no-redundant-role.md',
111+
tests: 'test/unit/rules/no-redundant-role-test.js',
112+
},
113+
},
114+
115+
create(context) {
116+
const config = parseConfig(context.options[0]);
117+
118+
return {
119+
GlimmerElementNode(node) {
120+
const roleAttr = node.attributes?.find((attr) => attr.name === 'role');
121+
122+
if (!roleAttr) {
123+
return;
124+
}
125+
126+
let roleValue;
127+
if (roleAttr.value && roleAttr.value.type === 'GlimmerTextNode') {
128+
roleValue = roleAttr.value.chars || '';
129+
} else {
130+
// Skip dynamic role values
131+
return;
132+
}
133+
134+
const isLandmarkRole = LANDMARK_ROLES.has(roleValue);
135+
if (!config.checkAllHTMLElements && !isLandmarkRole) {
136+
return;
137+
}
138+
139+
const elementsWithRole = ROLE_TO_ELEMENTS[roleValue];
140+
if (!elementsWithRole) {
141+
return;
142+
}
143+
144+
const isRedundant =
145+
elementsWithRole.includes(node.tag) &&
146+
!ALLOWED_ELEMENT_ROLES.some((e) => e.name === node.tag && e.role === roleValue);
147+
148+
if (isRedundant) {
149+
const errorMessage = isLandmarkRole
150+
? createErrorMessageLandmarkElement(node.tag, roleValue)
151+
: createErrorMessageAnyElement(node.tag, roleValue);
152+
153+
context.report({
154+
node,
155+
message: errorMessage,
156+
fix(fixer) {
157+
const sourceCode = context.getSourceCode();
158+
const elementText = sourceCode.getText(node);
159+
const roleAttrText = sourceCode.getText(roleAttr);
160+
161+
// Find the role attribute in the element text and remove it along with preceding space
162+
const roleAttrPattern = new RegExp(
163+
`\\s+${roleAttrText.replaceAll(/[$()*+.?[\\\]^{|}]/g, '\\$&')}`
164+
);
165+
const fixedText = elementText.replace(roleAttrPattern, '');
166+
167+
return fixer.replaceText(node, fixedText);
168+
},
169+
});
170+
}
171+
},
172+
};
173+
},
174+
};

0 commit comments

Comments
 (0)