mayContainChildComponent.js 1.8 KB

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