UNPKG

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