forked from jsx-eslint/eslint-plugin-jsx-a11y
/
hasAccessibleChild.js
29 lines (27 loc) · 1 KB
/
hasAccessibleChild.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
// @flow
import { hasAnyProp } from 'jsx-ast-utils';
import type { JSXElement, Node, JSXOpeningElement } from 'ast-types-flow';
import isHiddenFromScreenReader from './isHiddenFromScreenReader';
export default function hasAccessibleChild(node: JSXElement, elementType: (JSXOpeningElement) => string): boolean {
return node.children.some((child: Node) => {
switch (child.type) {
case 'Literal':
return !!child.value;
// $FlowFixMe JSXText is missing in ast-types-flow
case 'JSXText':
return !!child.value;
case 'JSXElement':
return !isHiddenFromScreenReader(
elementType(child.openingElement),
child.openingElement.attributes,
);
case 'JSXExpressionContainer':
if (child.expression.type === 'Identifier') {
return child.expression.name !== 'undefined';
}
return true;
default:
return false;
}
}) || hasAnyProp(node.openingElement.attributes, ['dangerouslySetInnerHTML', 'children']);
}