ReactElement.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = exports.test = exports.serialize = void 0;
  6. var ReactIs = _interopRequireWildcard(require('react-is'));
  7. var _markup = require('./lib/markup');
  8. function _interopRequireWildcard(obj) {
  9. if (obj && obj.__esModule) {
  10. return obj;
  11. } else {
  12. var newObj = {};
  13. if (obj != null) {
  14. for (var key in obj) {
  15. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  16. var desc =
  17. Object.defineProperty && Object.getOwnPropertyDescriptor
  18. ? Object.getOwnPropertyDescriptor(obj, key)
  19. : {};
  20. if (desc.get || desc.set) {
  21. Object.defineProperty(newObj, key, desc);
  22. } else {
  23. newObj[key] = obj[key];
  24. }
  25. }
  26. }
  27. }
  28. newObj.default = obj;
  29. return newObj;
  30. }
  31. }
  32. /**
  33. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  34. *
  35. * This source code is licensed under the MIT license found in the
  36. * LICENSE file in the root directory of this source tree.
  37. */
  38. // Given element.props.children, or subtree during recursive traversal,
  39. // return flattened array of children.
  40. const getChildren = (arg, children = []) => {
  41. if (Array.isArray(arg)) {
  42. arg.forEach(item => {
  43. getChildren(item, children);
  44. });
  45. } else if (arg != null && arg !== false) {
  46. children.push(arg);
  47. }
  48. return children;
  49. };
  50. const getType = element => {
  51. const type = element.type;
  52. if (typeof type === 'string') {
  53. return type;
  54. }
  55. if (typeof type === 'function') {
  56. return type.displayName || type.name || 'Unknown';
  57. }
  58. if (ReactIs.isFragment(element)) {
  59. return 'React.Fragment';
  60. }
  61. if (ReactIs.isSuspense(element)) {
  62. return 'React.Suspense';
  63. }
  64. if (typeof type === 'object' && type !== null) {
  65. if (ReactIs.isContextProvider(element)) {
  66. return 'Context.Provider';
  67. }
  68. if (ReactIs.isContextConsumer(element)) {
  69. return 'Context.Consumer';
  70. }
  71. if (ReactIs.isForwardRef(element)) {
  72. const functionName = type.render.displayName || type.render.name || '';
  73. return functionName !== ''
  74. ? 'ForwardRef(' + functionName + ')'
  75. : 'ForwardRef';
  76. }
  77. if (ReactIs.isMemo(type)) {
  78. const functionName =
  79. type.displayName || type.type.displayName || type.type.name || '';
  80. return functionName !== '' ? 'Memo(' + functionName + ')' : 'Memo';
  81. }
  82. }
  83. return 'UNDEFINED';
  84. };
  85. const getPropKeys = element => {
  86. const props = element.props;
  87. return Object.keys(props)
  88. .filter(key => key !== 'children' && props[key] !== undefined)
  89. .sort();
  90. };
  91. const serialize = (element, config, indentation, depth, refs, printer) =>
  92. ++depth > config.maxDepth
  93. ? (0, _markup.printElementAsLeaf)(getType(element), config)
  94. : (0, _markup.printElement)(
  95. getType(element),
  96. (0, _markup.printProps)(
  97. getPropKeys(element),
  98. element.props,
  99. config,
  100. indentation + config.indent,
  101. depth,
  102. refs,
  103. printer
  104. ),
  105. (0, _markup.printChildren)(
  106. getChildren(element.props.children),
  107. config,
  108. indentation + config.indent,
  109. depth,
  110. refs,
  111. printer
  112. ),
  113. config,
  114. indentation
  115. );
  116. exports.serialize = serialize;
  117. const test = val => val && ReactIs.isElement(val);
  118. exports.test = test;
  119. const plugin = {
  120. serialize,
  121. test
  122. };
  123. var _default = plugin;
  124. exports.default = _default;