/
mayContainChildComponent.js
53 lines (51 loc) · 1.5 KB
/
mayContainChildComponent.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
/**
* Returns true if it can positively determine that the element lacks an
* accessible label. If no determination is possible, it returns false. Treat
* false as an unknown value. The element might still have an accessible label,
* but this module cannot determine it positively.
*
* @flow
*/
import { elementType } from 'jsx-ast-utils';
import type { Node } from 'ast-types-flow';
import minimatch from 'minimatch';
export default function mayContainChildComponent(
root: Node,
componentName: string,
maxDepth: number = 1,
): boolean {
function traverseChildren(
node: Node,
depth: number,
): boolean {
// Bail when maxDepth is exceeded.
if (depth > maxDepth) {
return false;
}
if (node.children) {
/* $FlowFixMe */
for (let i = 0; i < node.children.length; i += 1) {
/* $FlowFixMe */
const childNode = node.children[i];
// Assume an expression container renders a label. It is the best we can
// do in this case.
if (childNode.type === 'JSXExpressionContainer') {
return true;
}
// Check for comonents with the provided name.
if (
childNode.type === 'JSXElement'
&& childNode.openingElement
&& minimatch(elementType(childNode.openingElement), componentName)
) {
return true;
}
if (traverseChildren(childNode, depth + 1)) {
return true;
}
}
}
return false;
}
return traverseChildren(root, 1);
}