Skip to content

Commit 33fe0b0

Browse files
committed
Add aria-label and aria-labelledby and alt and name
Add additional dis-ambiguating attributes. When `aria-label` or `aria-labelledby` is set at all, constraints should be set.
1 parent 281fa95 commit 33fe0b0

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lib/rules/role-supports-aria-props.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ const elementRolesMap = new ObjectMap()
99
for (const [key, value] of elementRoles.entries()) {
1010
// - Remove unused `constraints` key
1111
delete key.constraints
12-
// - Remove `constraints` key within `attributes`
13-
for (const attribute of key.attributes || []) {
14-
delete attribute.constraints
15-
}
1612
// - Remove empty `attributes` key
1713
if (!key.attributes || key.attributes?.length === 0) {
1814
delete key.attributes
@@ -44,14 +40,31 @@ module.exports = {
4440
// - Get the element’s name
4541
const key = {name: getElementType(context, node)}
4642
// - Get the element’s disambiguating attributes
47-
for (const prop of ['type', 'size', 'role', 'href', 'multiple', 'scope']) {
43+
for (const prop of [
44+
'aria-label',
45+
'aria-labelledby',
46+
'alt',
47+
'type',
48+
'size',
49+
'role',
50+
'href',
51+
'multiple',
52+
'scope',
53+
'name',
54+
]) {
55+
if ((prop === 'aria-labelledby' || prop === 'aria-label') && !['section', 'aside', 'form'].includes(key.name))
56+
continue
57+
if (prop === 'name' && key.name !== 'form') continue
58+
4859
const value = getPropValue(getProp(node.attributes, prop))
4960
if (value) {
5061
if (!('attributes' in key)) {
5162
key.attributes = []
5263
}
53-
if (prop === 'href') {
54-
key.attributes.push({name: prop})
64+
if (prop === 'href' || prop === 'aria-labelledby' || prop === 'aria-label') {
65+
key.attributes.push({name: prop, constraints: ['set']})
66+
} else if (prop === 'alt' && value !== '') {
67+
key.attributes.push({name: prop, constraints: ['set']})
5568
} else {
5669
key.attributes.push({name: prop, value})
5770
}

0 commit comments

Comments
 (0)