mayContainChildComponent.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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];
  30. // Assume an expression container renders a label. It is the best we can
  31. // do in this case.
  32. if (childNode.type === 'JSXExpressionContainer') {
  33. return true;
  34. }
  35. // Check for comonents with the provided name.
  36. if (childNode.type === 'JSXElement' && childNode.openingElement && (0, _minimatch["default"])(elementType(childNode.openingElement), componentName)) {
  37. return true;
  38. }
  39. if (traverseChildren(childNode, depth + 1)) {
  40. return true;
  41. }
  42. }
  43. }
  44. return false;
  45. }
  46. return traverseChildren(root, 1);
  47. }
  48. module.exports = exports.default;