-
-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathdiv-has-content.js
54 lines (46 loc) · 1.45 KB
/
div-has-content.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
/**
* @fileoverview check if div has content
* @author Felicia
* @flow
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import { elementType } from 'jsx-ast-utils';
// import type { JSXOpeningElement } from 'ast-types-flow';
import { generateObjSchema, arraySchema } from '../util/schemas';
import hasAccessibleChild from '../util/hasAccessibleChild';
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
const errorMessage = 'Div must have content and the content must be accessible by a screen reader.';
const headings = [
'div',
];
const schema = generateObjSchema({ components: arraySchema });
module.exports = {
meta: {
docs: {},
schema: [schema],
},
create: (context) => ({
JSXOpeningElement: (node) => {
const options = context.options[0] || {};
const componentOptions = options.components || [];
const typeCheck = headings.concat(componentOptions);
const nodeType = elementType(node);
// Only check 'div*' elements and custom types.
if (typeCheck.indexOf(nodeType) === -1) {
return;
}
if (hasAccessibleChild(node.parent)) {
return;
}
if (isHiddenFromScreenReader(nodeType, node.attributes)) {
return;
}
context.report({
node,
message: errorMessage,
});
},
}),
};