/
aria-unsupported-elements.js
69 lines (58 loc) · 1.92 KB
/
aria-unsupported-elements.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 that elements that do not support ARIA roles,
* states and properties do not have those attributes.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import {
aria,
dom,
} from 'aria-query';
import { propName } from 'jsx-ast-utils';
import { generateObjSchema } from '../util/schemas';
import getElementType from '../util/getElementType';
const errorMessage = (invalidProp) => (
`This element does not support ARIA roles, states and properties. \
Try removing the prop '${invalidProp}'.`
);
const schema = generateObjSchema();
export default {
meta: {
docs: {
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/aria-unsupported-elements.md',
description: 'Enforce that elements that do not support ARIA roles, states, and properties do not have those attributes.',
},
schema: [schema],
},
create: (context) => {
const elementType = getElementType(context);
return {
JSXOpeningElement: (node) => {
const nodeType = elementType(node);
const nodeAttrs = dom.get(nodeType) || {};
const {
reserved: isReservedNodeType = false,
} = nodeAttrs;
// If it's not reserved, then it can have aria-* roles, states, and properties
if (isReservedNodeType === false) {
return;
}
const invalidAttributes = new Set([...aria.keys(), 'role']);
node.attributes.forEach((prop) => {
if (prop.type === 'JSXSpreadAttribute') {
return;
}
const name = propName(prop).toLowerCase();
if (invalidAttributes.has(name)) {
context.report({
node,
message: errorMessage(name),
});
}
});
},
};
},
};