Skip to content

Commit dbdc50e

Browse files
author
Callie Callaway
committed
Update no-redundant-roles to allow nav role by default
1 parent 78b1bc4 commit dbdc50e

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

__tests__/src/rules/no-redundant-roles-test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// -----------------------------------------------------------------------------
1111

1212
import { RuleTester } from 'eslint';
13-
import { configs } from '../../../src';
1413
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
1514
import rule from '../../../src/rules/no-redundant-roles';
1615
import ruleOptionsMapperFactory from '../../__util__/ruleOptionsMapperFactory';
@@ -41,26 +40,26 @@ const neverValid = [
4140
{ code: '<button role={`${undefined}button`} />', errors: [expectedError('button', 'button')] },
4241
];
4342

44-
const recommendedOptions = (configs.recommended.rules[ruleName][1] || {});
45-
4643
ruleTester.run(`${ruleName}:recommended`, rule, {
4744
valid: [
4845
...alwaysValid,
4946
{ code: '<nav role="navigation" />' },
5047
]
51-
.map(ruleOptionsMapperFactory(recommendedOptions))
5248
.map(parserOptionsMapper),
5349
invalid: neverValid
54-
.map(ruleOptionsMapperFactory(recommendedOptions))
5550
.map(parserOptionsMapper),
5651
});
5752

58-
ruleTester.run(`${ruleName}:strict`, rule, {
53+
const noNavExceptionsOptions = { nav: [] };
54+
55+
ruleTester.run(`${ruleName}:recommended`, rule, {
5956
valid: alwaysValid
57+
.map(ruleOptionsMapperFactory(noNavExceptionsOptions))
6058
.map(parserOptionsMapper),
6159
invalid: [
6260
...neverValid,
6361
{ code: '<nav role="navigation" />', errors: [expectedError('nav', 'navigation')] },
6462
]
63+
.map(ruleOptionsMapperFactory(noNavExceptionsOptions))
6564
.map(parserOptionsMapper),
6665
});

docs/rules/no-redundant-roles.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Some HTML elements have native semantics that are implemented by the browser. Th
77

88
## Rule details
99

10-
The recommended options for this rule allow an implicit role of `navigation` to be applied to a `nav` element as is [advised by w3](https://www.w3.org/WAI/GL/wiki/Using_HTML5_nav_element#Example:The_.3Cnav.3E_element). The options are provided as an object keyed by HTML element name; the value is an array of implicit ARIA roles that are allowed on the specified element.
10+
The default options for this rule allow an implicit role of `navigation` to be applied to a `nav` element as is [advised by w3](https://www.w3.org/WAI/GL/wiki/Using_HTML5_nav_element#Example:The_.3Cnav.3E_element). The options are provided as an object keyed by HTML element name; the value is an array of implicit ARIA roles that are allowed on the specified element.
1111

1212
```
1313
{
@@ -19,12 +19,6 @@ The recommended options for this rule allow an implicit role of `navigation` to
1919
}
2020
```
2121

22-
Under the recommended options, the following code is valid. It would be invalid under the strict rules.
23-
24-
```
25-
<nav role="navigation" />
26-
```
27-
2822
### Succeed
2923
```jsx
3024
<div />

src/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,7 @@ module.exports = {
137137
},
138138
],
139139
'jsx-a11y/no-onchange': 'error',
140-
'jsx-a11y/no-redundant-roles': [
141-
'error',
142-
{
143-
nav: ['navigation'],
144-
},
145-
],
140+
'jsx-a11y/no-redundant-roles': 'error',
146141
'jsx-a11y/no-static-element-interactions': [
147142
'error',
148143
{

src/rules/no-redundant-roles.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import getImplicitRole from '../util/getImplicitRole';
2020
const errorMessage = (element, implicitRole) =>
2121
`The element ${element} has an implicit role of ${implicitRole}. Defining this explicitly is redundant and should be avoided.`;
2222

23+
const DEFAULT_ROLE_EXCEPTIONS = { nav: ['navigation'] };
24+
2325
module.exports = {
2426
meta: {
2527
docs: {
@@ -50,8 +52,16 @@ module.exports = {
5052
}
5153

5254
if (implicitRole === explicitRole) {
53-
const allowedRoles = (options[0] || {});
54-
if (has(allowedRoles, type) && includes(allowedRoles[type], implicitRole)) {
55+
const allowedRedundantRoles = (options[0] || {});
56+
let redundantRolesForElement;
57+
58+
if (has(allowedRedundantRoles, type)) {
59+
redundantRolesForElement = allowedRedundantRoles[type];
60+
} else {
61+
redundantRolesForElement = DEFAULT_ROLE_EXCEPTIONS[type] || [];
62+
}
63+
64+
if (includes(redundantRolesForElement, implicitRole)) {
5565
return;
5666
}
5767

0 commit comments

Comments
 (0)