.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / pretty-format / build / plugins / react_element.js
1 'use strict';
2
3 Object.defineProperty(exports, '__esModule', {
4   value: true
5 });
6 exports.test = exports.serialize = undefined;
7
8 var _markup = require('./lib/markup');
9
10 /**
11  * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
12  *
13  * This source code is licensed under the MIT license found in the
14  * LICENSE file in the root directory of this source tree.
15  *
16  *
17  */
18
19 const elementSymbol = Symbol.for('react.element');
20 const fragmentSymbol = Symbol.for('react.fragment');
21 const forwardRefSymbol = Symbol.for('react.forward_ref');
22 const providerSymbol = Symbol.for('react.provider');
23 const contextSymbol = Symbol.for('react.context');
24
25 // Given element.props.children, or subtree during recursive traversal,
26 // return flattened array of children.
27 const getChildren = function(arg) {
28   let children =
29     arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
30
31   if (Array.isArray(arg)) {
32     arg.forEach(item => {
33       getChildren(item, children);
34     });
35   } else if (arg != null && arg !== false) {
36     children.push(arg);
37   }
38   return children;
39 };
40
41 const getType = element => {
42   const type = element.type;
43   if (typeof type === 'string') {
44     return type;
45   }
46   if (typeof type === 'function') {
47     return type.displayName || type.name || 'Unknown';
48   }
49   if (type === fragmentSymbol) {
50     return 'React.Fragment';
51   }
52   if (typeof type === 'object' && type !== null) {
53     if (type.$$typeof === providerSymbol) {
54       return 'Context.Provider';
55     }
56
57     if (type.$$typeof === contextSymbol) {
58       return 'Context.Consumer';
59     }
60
61     if (type.$$typeof === forwardRefSymbol) {
62       const functionName = type.render.displayName || type.render.name || '';
63
64       return functionName !== ''
65         ? 'ForwardRef(' + functionName + ')'
66         : 'ForwardRef';
67     }
68   }
69   return 'UNDEFINED';
70 };
71
72 const getPropKeys = element => {
73   const props = element.props;
74
75   return Object.keys(props)
76     .filter(key => key !== 'children' && props[key] !== undefined)
77     .sort();
78 };
79
80 const serialize = (exports.serialize = (
81   element,
82   config,
83   indentation,
84   depth,
85   refs,
86   printer
87 ) =>
88   ++depth > config.maxDepth
89     ? (0, _markup.printElementAsLeaf)(getType(element), config)
90     : (0, _markup.printElement)(
91         getType(element),
92         (0, _markup.printProps)(
93           getPropKeys(element),
94           element.props,
95           config,
96           indentation + config.indent,
97           depth,
98           refs,
99           printer
100         ),
101         (0, _markup.printChildren)(
102           getChildren(element.props.children),
103           config,
104           indentation + config.indent,
105           depth,
106           refs,
107           printer
108         ),
109         config,
110         indentation
111       ));
112
113 const test = (exports.test = val => val && val.$$typeof === elementSymbol);
114
115 exports.default = {serialize: serialize, test: test};