ReactElement.js 4.1 KB

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