mayContainChildComponent.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = mayContainChildComponent;
  6. var _jsxAstUtils = require("jsx-ast-utils");
  7. /**
  8. * Returns true if it can positively determine that the element lacks an
  9. * accessible label. If no determination is possible, it returns false. Treat
  10. * false as an unknown value. The element might still have an accessible label,
  11. * but this module cannot determine it positively.
  12. *
  13. *
  14. */
  15. function mayContainChildComponent(root, componentName) {
  16. var maxDepth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  17. function traverseChildren(node, depth) {
  18. // Bail when maxDepth is exceeded.
  19. if (depth > maxDepth) {
  20. return false;
  21. }
  22. if (node.children) {
  23. /* $FlowFixMe */
  24. for (var i = 0; i < node.children.length; i += 1) {
  25. /* $FlowFixMe */
  26. var childNode = node.children[i]; // Assume an expression container renders a label. It is the best we can
  27. // do in this case.
  28. if (childNode.type === 'JSXExpressionContainer') {
  29. return true;
  30. } // Check for comonents with the provided name.
  31. if (childNode.type === 'JSXElement' && childNode.openingElement && (0, _jsxAstUtils.elementType)(childNode.openingElement) === componentName) {
  32. return true;
  33. }
  34. if (traverseChildren(childNode, depth + 1)) {
  35. return true;
  36. }
  37. }
  38. }
  39. return false;
  40. }
  41. return traverseChildren(root, 1);
  42. }