-
-
Notifications
You must be signed in to change notification settings - Fork 639
/
Copy pathscope.js
56 lines (47 loc) · 1.48 KB
/
scope.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
/**
* @fileoverview Enforce scope prop is only used on <th> elements.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import { dom } from 'aria-query';
import { propName } from 'jsx-ast-utils';
import { generateObjSchema } from '../util/schemas';
import getElementType from '../util/getElementType';
const errorMessage = 'The scope prop can only be used on <th> elements.';
const schema = generateObjSchema();
export default {
meta: {
docs: {
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/scope.md',
description: 'Enforce `scope` prop is only used on `<th>` elements.',
},
schema: [schema],
},
create: (context) => {
const elementType = getElementType(context);
return {
JSXAttribute: (node) => {
const name = propName(node);
if (name && name.toUpperCase() !== 'SCOPE') {
return;
}
const { parent } = node;
const tagName = elementType(parent);
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
if (!dom.has(tagName)) {
return;
}
if (tagName && tagName.toUpperCase() === 'TH') {
return;
}
context.report({
node,
message: errorMessage,
});
},
};
},
};