/
aria-role.js
69 lines (55 loc) · 1.97 KB
/
aria-role.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @fileoverview Enforce aria role attribute is valid.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import { dom, roles } from 'aria-query';
import { getLiteralPropValue, propName, elementType } from 'jsx-ast-utils';
import { generateObjSchema } from '../util/schemas';
const errorMessage = 'Elements with ARIA roles must use a valid, non-abstract ARIA role.';
const schema = generateObjSchema({
ignoreNonDOM: {
type: 'boolean',
default: false,
},
});
export default {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/aria-role.md',
},
schema: [schema],
},
create: (context) => ({
JSXAttribute: (attribute) => {
// Determine if ignoreNonDOM is set to true
// If true, then do not run rule.
const options = context.options[0] || {};
const ignoreNonDOM = !!options.ignoreNonDOM;
if (ignoreNonDOM) {
const type = elementType(attribute.parent);
if (!dom.get(type)) {
return;
}
}
// Get prop name
const name = propName(attribute).toUpperCase();
if (name !== 'ROLE') { return; }
const value = getLiteralPropValue(attribute);
// If value is undefined, then the role attribute will be dropped in the DOM.
// If value is null, then getLiteralAttributeValue is telling us that the
// value isn't in the form of a literal.
if (value === undefined || value === null) { return; }
const values = String(value).split(' ');
const validRoles = [...roles.keys()].filter((role) => roles.get(role).abstract === false);
const isValid = values.every((val) => validRoles.indexOf(val) > -1);
if (isValid === true) { return; }
context.report({
node: attribute,
message: errorMessage,
});
},
}),
};