index.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var isPlainObject = require('is-plain-object');
  4. var React = require('react');
  5. var prettyPrintObject = require('@base2/pretty-print-object');
  6. var reactIs = require('react-is');
  7. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  8. function _interopNamespace(e) {
  9. if (e && e.__esModule) return e;
  10. var n = Object.create(null);
  11. if (e) {
  12. Object.keys(e).forEach(function (k) {
  13. if (k !== 'default') {
  14. var d = Object.getOwnPropertyDescriptor(e, k);
  15. Object.defineProperty(n, k, d.get ? d : {
  16. enumerable: true,
  17. get: function () { return e[k]; }
  18. });
  19. }
  20. });
  21. }
  22. n["default"] = e;
  23. return Object.freeze(n);
  24. }
  25. var React__namespace = /*#__PURE__*/_interopNamespace(React);
  26. var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
  27. var spacer = (function (times, tabStop) {
  28. if (times === 0) {
  29. return '';
  30. }
  31. return new Array(times * tabStop).fill(' ').join('');
  32. });
  33. function _typeof(obj) {
  34. "@babel/helpers - typeof";
  35. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
  36. return typeof obj;
  37. } : function (obj) {
  38. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  39. }, _typeof(obj);
  40. }
  41. function _toConsumableArray(arr) {
  42. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
  43. }
  44. function _arrayWithoutHoles(arr) {
  45. if (Array.isArray(arr)) return _arrayLikeToArray(arr);
  46. }
  47. function _iterableToArray(iter) {
  48. if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
  49. }
  50. function _unsupportedIterableToArray(o, minLen) {
  51. if (!o) return;
  52. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  53. var n = Object.prototype.toString.call(o).slice(8, -1);
  54. if (n === "Object" && o.constructor) n = o.constructor.name;
  55. if (n === "Map" || n === "Set") return Array.from(o);
  56. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  57. }
  58. function _arrayLikeToArray(arr, len) {
  59. if (len == null || len > arr.length) len = arr.length;
  60. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  61. return arr2;
  62. }
  63. function _nonIterableSpread() {
  64. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  65. }
  66. function safeSortObject(value, seen) {
  67. // return non-object value as is
  68. if (value === null || _typeof(value) !== 'object') {
  69. return value;
  70. } // return date, regexp and react element values as is
  71. if (value instanceof Date || value instanceof RegExp || /*#__PURE__*/React__namespace.isValidElement(value)) {
  72. return value;
  73. }
  74. seen.add(value); // make a copy of array with each item passed through the sorting algorithm
  75. if (Array.isArray(value)) {
  76. return value.map(function (v) {
  77. return safeSortObject(v, seen);
  78. });
  79. } // make a copy of object with key sorted
  80. return Object.keys(value).sort().reduce(function (result, key) {
  81. if (key === '_owner') {
  82. return result;
  83. }
  84. if (key === 'current' || seen.has(value[key])) {
  85. // eslint-disable-next-line no-param-reassign
  86. result[key] = '[Circular]';
  87. } else {
  88. // eslint-disable-next-line no-param-reassign
  89. result[key] = safeSortObject(value[key], seen);
  90. }
  91. return result;
  92. }, {});
  93. }
  94. function sortObject(value) {
  95. return safeSortObject(value, new WeakSet());
  96. }
  97. /* eslint-disable no-use-before-define */
  98. var createStringTreeNode = function createStringTreeNode(value) {
  99. return {
  100. type: 'string',
  101. value: value
  102. };
  103. };
  104. var createNumberTreeNode = function createNumberTreeNode(value) {
  105. return {
  106. type: 'number',
  107. value: value
  108. };
  109. };
  110. var createReactElementTreeNode = function createReactElementTreeNode(displayName, props, defaultProps, childrens) {
  111. return {
  112. type: 'ReactElement',
  113. displayName: displayName,
  114. props: props,
  115. defaultProps: defaultProps,
  116. childrens: childrens
  117. };
  118. };
  119. var createReactFragmentTreeNode = function createReactFragmentTreeNode(key, childrens) {
  120. return {
  121. type: 'ReactFragment',
  122. key: key,
  123. childrens: childrens
  124. };
  125. };
  126. var supportFragment = Boolean(React.Fragment);
  127. var getFunctionTypeName = function getFunctionTypeName(functionType) {
  128. if (!functionType.name || functionType.name === '_default') {
  129. return 'No Display Name';
  130. }
  131. return functionType.name;
  132. };
  133. var getWrappedComponentDisplayName = function getWrappedComponentDisplayName(Component) {
  134. switch (true) {
  135. case Boolean(Component.displayName):
  136. return Component.displayName;
  137. case Component.$$typeof === reactIs.Memo:
  138. return getWrappedComponentDisplayName(Component.type);
  139. case Component.$$typeof === reactIs.ForwardRef:
  140. return getWrappedComponentDisplayName(Component.render);
  141. default:
  142. return getFunctionTypeName(Component);
  143. }
  144. }; // heavily inspired by:
  145. // https://github.com/facebook/react/blob/3746eaf985dd92f8aa5f5658941d07b6b855e9d9/packages/react-devtools-shared/src/backend/renderer.js#L399-L496
  146. var getReactElementDisplayName = function getReactElementDisplayName(element) {
  147. switch (true) {
  148. case typeof element.type === 'string':
  149. return element.type;
  150. case typeof element.type === 'function':
  151. if (element.type.displayName) {
  152. return element.type.displayName;
  153. }
  154. return getFunctionTypeName(element.type);
  155. case reactIs.isForwardRef(element):
  156. case reactIs.isMemo(element):
  157. return getWrappedComponentDisplayName(element.type);
  158. case reactIs.isContextConsumer(element):
  159. return "".concat(element.type._context.displayName || 'Context', ".Consumer");
  160. case reactIs.isContextProvider(element):
  161. return "".concat(element.type._context.displayName || 'Context', ".Provider");
  162. case reactIs.isLazy(element):
  163. return 'Lazy';
  164. case reactIs.isProfiler(element):
  165. return 'Profiler';
  166. case reactIs.isStrictMode(element):
  167. return 'StrictMode';
  168. case reactIs.isSuspense(element):
  169. return 'Suspense';
  170. default:
  171. return 'UnknownElementType';
  172. }
  173. };
  174. var noChildren = function noChildren(propsValue, propName) {
  175. return propName !== 'children';
  176. };
  177. var onlyMeaningfulChildren = function onlyMeaningfulChildren(children) {
  178. return children !== true && children !== false && children !== null && children !== '';
  179. };
  180. var filterProps = function filterProps(originalProps, cb) {
  181. var filteredProps = {};
  182. Object.keys(originalProps).filter(function (key) {
  183. return cb(originalProps[key], key);
  184. }).forEach(function (key) {
  185. return filteredProps[key] = originalProps[key];
  186. });
  187. return filteredProps;
  188. };
  189. var parseReactElement = function parseReactElement(element, options) {
  190. var _options$displayName = options.displayName,
  191. displayNameFn = _options$displayName === void 0 ? getReactElementDisplayName : _options$displayName;
  192. if (typeof element === 'string') {
  193. return createStringTreeNode(element);
  194. } else if (typeof element === 'number') {
  195. return createNumberTreeNode(element);
  196. } else if (! /*#__PURE__*/React__default["default"].isValidElement(element)) {
  197. throw new Error("react-element-to-jsx-string: Expected a React.Element, got `".concat(_typeof(element), "`"));
  198. }
  199. var displayName = displayNameFn(element);
  200. var props = filterProps(element.props, noChildren);
  201. if (element.ref !== null) {
  202. props.ref = element.ref;
  203. }
  204. var key = element.key;
  205. if (typeof key === 'string' && key.search(/^\./)) {
  206. // React automatically add key=".X" when there are some children
  207. props.key = key;
  208. }
  209. var defaultProps = filterProps(element.type.defaultProps || {}, noChildren);
  210. var childrens = React__default["default"].Children.toArray(element.props.children).filter(onlyMeaningfulChildren).map(function (child) {
  211. return parseReactElement(child, options);
  212. });
  213. if (supportFragment && element.type === React.Fragment) {
  214. return createReactFragmentTreeNode(key, childrens);
  215. }
  216. return createReactElementTreeNode(displayName, props, defaultProps, childrens);
  217. };
  218. function noRefCheck() {}
  219. var inlineFunction = function inlineFunction(fn) {
  220. return fn.toString().split('\n').map(function (line) {
  221. return line.trim();
  222. }).join('');
  223. };
  224. var preserveFunctionLineBreak = function preserveFunctionLineBreak(fn) {
  225. return fn.toString();
  226. };
  227. var defaultFunctionValue = inlineFunction;
  228. var formatFunction = (function (fn, options) {
  229. var _options$functionValu = options.functionValue,
  230. functionValue = _options$functionValu === void 0 ? defaultFunctionValue : _options$functionValu,
  231. showFunctions = options.showFunctions;
  232. if (!showFunctions && functionValue === defaultFunctionValue) {
  233. return functionValue(noRefCheck);
  234. }
  235. return functionValue(fn);
  236. });
  237. var formatComplexDataStructure = (function (value, inline, lvl, options) {
  238. var normalizedValue = sortObject(value);
  239. var stringifiedValue = prettyPrintObject.prettyPrint(normalizedValue, {
  240. transform: function transform(currentObj, prop, originalResult) {
  241. var currentValue = currentObj[prop];
  242. if (currentValue && /*#__PURE__*/React.isValidElement(currentValue)) {
  243. return formatTreeNode(parseReactElement(currentValue, options), true, lvl, options);
  244. }
  245. if (typeof currentValue === 'function') {
  246. return formatFunction(currentValue, options);
  247. }
  248. return originalResult;
  249. }
  250. });
  251. if (inline) {
  252. return stringifiedValue.replace(/\s+/g, ' ').replace(/{ /g, '{').replace(/ }/g, '}').replace(/\[ /g, '[').replace(/ ]/g, ']');
  253. } // Replace tabs with spaces, and add necessary indentation in front of each new line
  254. return stringifiedValue.replace(/\t/g, spacer(1, options.tabStop)).replace(/\n([^$])/g, "\n".concat(spacer(lvl + 1, options.tabStop), "$1"));
  255. });
  256. var escape$1 = function escape(s) {
  257. return s.replace(/"/g, '&quot;');
  258. };
  259. var formatPropValue = function formatPropValue(propValue, inline, lvl, options) {
  260. if (typeof propValue === 'number') {
  261. return "{".concat(String(propValue), "}");
  262. }
  263. if (typeof propValue === 'string') {
  264. return "\"".concat(escape$1(propValue), "\"");
  265. } // > "Symbols (new in ECMAScript 2015, not yet supported in Flow)"
  266. // @see: https://flow.org/en/docs/types/primitives/
  267. // $FlowFixMe: Flow does not support Symbol
  268. if (_typeof(propValue) === 'symbol') {
  269. var symbolDescription = propValue.valueOf().toString().replace(/Symbol\((.*)\)/, '$1');
  270. if (!symbolDescription) {
  271. return "{Symbol()}";
  272. }
  273. return "{Symbol('".concat(symbolDescription, "')}");
  274. }
  275. if (typeof propValue === 'function') {
  276. return "{".concat(formatFunction(propValue, options), "}");
  277. }
  278. if ( /*#__PURE__*/React.isValidElement(propValue)) {
  279. return "{".concat(formatTreeNode(parseReactElement(propValue, options), true, lvl, options), "}");
  280. }
  281. if (propValue instanceof Date) {
  282. if (isNaN(propValue.valueOf())) {
  283. return "{new Date(NaN)}";
  284. }
  285. return "{new Date(\"".concat(propValue.toISOString(), "\")}");
  286. }
  287. if (isPlainObject.isPlainObject(propValue) || Array.isArray(propValue)) {
  288. return "{".concat(formatComplexDataStructure(propValue, inline, lvl, options), "}");
  289. }
  290. return "{".concat(String(propValue), "}");
  291. };
  292. var formatProp = (function (name, hasValue, value, hasDefaultValue, defaultValue, inline, lvl, options) {
  293. if (!hasValue && !hasDefaultValue) {
  294. throw new Error("The prop \"".concat(name, "\" has no value and no default: could not be formatted"));
  295. }
  296. var usedValue = hasValue ? value : defaultValue;
  297. var useBooleanShorthandSyntax = options.useBooleanShorthandSyntax,
  298. tabStop = options.tabStop;
  299. var formattedPropValue = formatPropValue(usedValue, inline, lvl, options);
  300. var attributeFormattedInline = ' ';
  301. var attributeFormattedMultiline = "\n".concat(spacer(lvl + 1, tabStop));
  302. var isMultilineAttribute = formattedPropValue.includes('\n');
  303. if (useBooleanShorthandSyntax && formattedPropValue === '{false}' && !hasDefaultValue) {
  304. // If a boolean is false and not different from it's default, we do not render the attribute
  305. attributeFormattedInline = '';
  306. attributeFormattedMultiline = '';
  307. } else if (useBooleanShorthandSyntax && formattedPropValue === '{true}') {
  308. attributeFormattedInline += "".concat(name);
  309. attributeFormattedMultiline += "".concat(name);
  310. } else {
  311. attributeFormattedInline += "".concat(name, "=").concat(formattedPropValue);
  312. attributeFormattedMultiline += "".concat(name, "=").concat(formattedPropValue);
  313. }
  314. return {
  315. attributeFormattedInline: attributeFormattedInline,
  316. attributeFormattedMultiline: attributeFormattedMultiline,
  317. isMultilineAttribute: isMultilineAttribute
  318. };
  319. });
  320. var mergeSiblingPlainStringChildrenReducer = (function (previousNodes, currentNode) {
  321. var nodes = previousNodes.slice(0, previousNodes.length > 0 ? previousNodes.length - 1 : 0);
  322. var previousNode = previousNodes[previousNodes.length - 1];
  323. if (previousNode && (currentNode.type === 'string' || currentNode.type === 'number') && (previousNode.type === 'string' || previousNode.type === 'number')) {
  324. nodes.push(createStringTreeNode(String(previousNode.value) + String(currentNode.value)));
  325. } else {
  326. if (previousNode) {
  327. nodes.push(previousNode);
  328. }
  329. nodes.push(currentNode);
  330. }
  331. return nodes;
  332. });
  333. var isKeyOrRefProps = function isKeyOrRefProps(propName) {
  334. return ['key', 'ref'].includes(propName);
  335. };
  336. var sortPropsByNames = (function (shouldSortUserProps) {
  337. return function (props) {
  338. var haveKeyProp = props.includes('key');
  339. var haveRefProp = props.includes('ref');
  340. var userPropsOnly = props.filter(function (oneProp) {
  341. return !isKeyOrRefProps(oneProp);
  342. });
  343. var sortedProps = shouldSortUserProps ? _toConsumableArray(userPropsOnly.sort()) // We use basic lexical order
  344. : _toConsumableArray(userPropsOnly);
  345. if (haveRefProp) {
  346. sortedProps.unshift('ref');
  347. }
  348. if (haveKeyProp) {
  349. sortedProps.unshift('key');
  350. }
  351. return sortedProps;
  352. };
  353. });
  354. function createPropFilter(props, filter) {
  355. if (Array.isArray(filter)) {
  356. return function (key) {
  357. return filter.indexOf(key) === -1;
  358. };
  359. } else {
  360. return function (key) {
  361. return filter(props[key], key);
  362. };
  363. }
  364. }
  365. var compensateMultilineStringElementIndentation = function compensateMultilineStringElementIndentation(element, formattedElement, inline, lvl, options) {
  366. var tabStop = options.tabStop;
  367. if (element.type === 'string') {
  368. return formattedElement.split('\n').map(function (line, offset) {
  369. if (offset === 0) {
  370. return line;
  371. }
  372. return "".concat(spacer(lvl, tabStop)).concat(line);
  373. }).join('\n');
  374. }
  375. return formattedElement;
  376. };
  377. var formatOneChildren = function formatOneChildren(inline, lvl, options) {
  378. return function (element) {
  379. return compensateMultilineStringElementIndentation(element, formatTreeNode(element, inline, lvl, options), inline, lvl, options);
  380. };
  381. };
  382. var onlyPropsWithOriginalValue = function onlyPropsWithOriginalValue(defaultProps, props) {
  383. return function (propName) {
  384. var haveDefaultValue = Object.keys(defaultProps).includes(propName);
  385. return !haveDefaultValue || haveDefaultValue && defaultProps[propName] !== props[propName];
  386. };
  387. };
  388. var isInlineAttributeTooLong = function isInlineAttributeTooLong(attributes, inlineAttributeString, lvl, tabStop, maxInlineAttributesLineLength) {
  389. if (!maxInlineAttributesLineLength) {
  390. return attributes.length > 1;
  391. }
  392. return spacer(lvl, tabStop).length + inlineAttributeString.length > maxInlineAttributesLineLength;
  393. };
  394. var shouldRenderMultilineAttr = function shouldRenderMultilineAttr(attributes, inlineAttributeString, containsMultilineAttr, inline, lvl, tabStop, maxInlineAttributesLineLength) {
  395. return (isInlineAttributeTooLong(attributes, inlineAttributeString, lvl, tabStop, maxInlineAttributesLineLength) || containsMultilineAttr) && !inline;
  396. };
  397. var formatReactElementNode = (function (node, inline, lvl, options) {
  398. var type = node.type,
  399. _node$displayName = node.displayName,
  400. displayName = _node$displayName === void 0 ? '' : _node$displayName,
  401. childrens = node.childrens,
  402. _node$props = node.props,
  403. props = _node$props === void 0 ? {} : _node$props,
  404. _node$defaultProps = node.defaultProps,
  405. defaultProps = _node$defaultProps === void 0 ? {} : _node$defaultProps;
  406. if (type !== 'ReactElement') {
  407. throw new Error("The \"formatReactElementNode\" function could only format node of type \"ReactElement\". Given: ".concat(type));
  408. }
  409. var filterProps = options.filterProps,
  410. maxInlineAttributesLineLength = options.maxInlineAttributesLineLength,
  411. showDefaultProps = options.showDefaultProps,
  412. sortProps = options.sortProps,
  413. tabStop = options.tabStop;
  414. var out = "<".concat(displayName);
  415. var outInlineAttr = out;
  416. var outMultilineAttr = out;
  417. var containsMultilineAttr = false;
  418. var visibleAttributeNames = [];
  419. var propFilter = createPropFilter(props, filterProps);
  420. Object.keys(props).filter(propFilter).filter(onlyPropsWithOriginalValue(defaultProps, props)).forEach(function (propName) {
  421. return visibleAttributeNames.push(propName);
  422. });
  423. Object.keys(defaultProps).filter(propFilter).filter(function () {
  424. return showDefaultProps;
  425. }).filter(function (defaultPropName) {
  426. return !visibleAttributeNames.includes(defaultPropName);
  427. }).forEach(function (defaultPropName) {
  428. return visibleAttributeNames.push(defaultPropName);
  429. });
  430. var attributes = sortPropsByNames(sortProps)(visibleAttributeNames);
  431. attributes.forEach(function (attributeName) {
  432. var _formatProp = formatProp(attributeName, Object.keys(props).includes(attributeName), props[attributeName], Object.keys(defaultProps).includes(attributeName), defaultProps[attributeName], inline, lvl, options),
  433. attributeFormattedInline = _formatProp.attributeFormattedInline,
  434. attributeFormattedMultiline = _formatProp.attributeFormattedMultiline,
  435. isMultilineAttribute = _formatProp.isMultilineAttribute;
  436. if (isMultilineAttribute) {
  437. containsMultilineAttr = true;
  438. }
  439. outInlineAttr += attributeFormattedInline;
  440. outMultilineAttr += attributeFormattedMultiline;
  441. });
  442. outMultilineAttr += "\n".concat(spacer(lvl, tabStop));
  443. if (shouldRenderMultilineAttr(attributes, outInlineAttr, containsMultilineAttr, inline, lvl, tabStop, maxInlineAttributesLineLength)) {
  444. out = outMultilineAttr;
  445. } else {
  446. out = outInlineAttr;
  447. }
  448. if (childrens && childrens.length > 0) {
  449. var newLvl = lvl + 1;
  450. out += '>';
  451. if (!inline) {
  452. out += '\n';
  453. out += spacer(newLvl, tabStop);
  454. }
  455. out += childrens.reduce(mergeSiblingPlainStringChildrenReducer, []).map(formatOneChildren(inline, newLvl, options)).join(!inline ? "\n".concat(spacer(newLvl, tabStop)) : '');
  456. if (!inline) {
  457. out += '\n';
  458. out += spacer(newLvl - 1, tabStop);
  459. }
  460. out += "</".concat(displayName, ">");
  461. } else {
  462. if (!isInlineAttributeTooLong(attributes, outInlineAttr, lvl, tabStop, maxInlineAttributesLineLength)) {
  463. out += ' ';
  464. }
  465. out += '/>';
  466. }
  467. return out;
  468. });
  469. var REACT_FRAGMENT_TAG_NAME_SHORT_SYNTAX = '';
  470. var REACT_FRAGMENT_TAG_NAME_EXPLICIT_SYNTAX = 'React.Fragment';
  471. var toReactElementTreeNode = function toReactElementTreeNode(displayName, key, childrens) {
  472. var props = {};
  473. if (key) {
  474. props = {
  475. key: key
  476. };
  477. }
  478. return {
  479. type: 'ReactElement',
  480. displayName: displayName,
  481. props: props,
  482. defaultProps: {},
  483. childrens: childrens
  484. };
  485. };
  486. var isKeyedFragment = function isKeyedFragment(_ref) {
  487. var key = _ref.key;
  488. return Boolean(key);
  489. };
  490. var hasNoChildren = function hasNoChildren(_ref2) {
  491. var childrens = _ref2.childrens;
  492. return childrens.length === 0;
  493. };
  494. var formatReactFragmentNode = (function (node, inline, lvl, options) {
  495. var type = node.type,
  496. key = node.key,
  497. childrens = node.childrens;
  498. if (type !== 'ReactFragment') {
  499. throw new Error("The \"formatReactFragmentNode\" function could only format node of type \"ReactFragment\". Given: ".concat(type));
  500. }
  501. var useFragmentShortSyntax = options.useFragmentShortSyntax;
  502. var displayName;
  503. if (useFragmentShortSyntax) {
  504. if (hasNoChildren(node) || isKeyedFragment(node)) {
  505. displayName = REACT_FRAGMENT_TAG_NAME_EXPLICIT_SYNTAX;
  506. } else {
  507. displayName = REACT_FRAGMENT_TAG_NAME_SHORT_SYNTAX;
  508. }
  509. } else {
  510. displayName = REACT_FRAGMENT_TAG_NAME_EXPLICIT_SYNTAX;
  511. }
  512. return formatReactElementNode(toReactElementTreeNode(displayName, key, childrens), inline, lvl, options);
  513. });
  514. var jsxStopChars = ['<', '>', '{', '}'];
  515. var shouldBeEscaped = function shouldBeEscaped(s) {
  516. return jsxStopChars.some(function (jsxStopChar) {
  517. return s.includes(jsxStopChar);
  518. });
  519. };
  520. var escape = function escape(s) {
  521. if (!shouldBeEscaped(s)) {
  522. return s;
  523. }
  524. return "{`".concat(s, "`}");
  525. };
  526. var preserveTrailingSpace = function preserveTrailingSpace(s) {
  527. var result = s;
  528. if (result.endsWith(' ')) {
  529. result = result.replace(/^(.*?)(\s+)$/, "$1{'$2'}");
  530. }
  531. if (result.startsWith(' ')) {
  532. result = result.replace(/^(\s+)(.*)$/, "{'$1'}$2");
  533. }
  534. return result;
  535. };
  536. var formatTreeNode = (function (node, inline, lvl, options) {
  537. if (node.type === 'number') {
  538. return String(node.value);
  539. }
  540. if (node.type === 'string') {
  541. return node.value ? "".concat(preserveTrailingSpace(escape(String(node.value)))) : '';
  542. }
  543. if (node.type === 'ReactElement') {
  544. return formatReactElementNode(node, inline, lvl, options);
  545. }
  546. if (node.type === 'ReactFragment') {
  547. return formatReactFragmentNode(node, inline, lvl, options);
  548. }
  549. throw new TypeError("Unknow format type \"".concat(node.type, "\""));
  550. });
  551. var formatTree = (function (node, options) {
  552. return formatTreeNode(node, false, 0, options);
  553. });
  554. var reactElementToJsxString = function reactElementToJsxString(element) {
  555. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  556. _ref$filterProps = _ref.filterProps,
  557. filterProps = _ref$filterProps === void 0 ? [] : _ref$filterProps,
  558. _ref$showDefaultProps = _ref.showDefaultProps,
  559. showDefaultProps = _ref$showDefaultProps === void 0 ? true : _ref$showDefaultProps,
  560. _ref$showFunctions = _ref.showFunctions,
  561. showFunctions = _ref$showFunctions === void 0 ? false : _ref$showFunctions,
  562. functionValue = _ref.functionValue,
  563. _ref$tabStop = _ref.tabStop,
  564. tabStop = _ref$tabStop === void 0 ? 2 : _ref$tabStop,
  565. _ref$useBooleanShorth = _ref.useBooleanShorthandSyntax,
  566. useBooleanShorthandSyntax = _ref$useBooleanShorth === void 0 ? true : _ref$useBooleanShorth,
  567. _ref$useFragmentShort = _ref.useFragmentShortSyntax,
  568. useFragmentShortSyntax = _ref$useFragmentShort === void 0 ? true : _ref$useFragmentShort,
  569. _ref$sortProps = _ref.sortProps,
  570. sortProps = _ref$sortProps === void 0 ? true : _ref$sortProps,
  571. maxInlineAttributesLineLength = _ref.maxInlineAttributesLineLength,
  572. displayName = _ref.displayName;
  573. if (!element) {
  574. throw new Error('react-element-to-jsx-string: Expected a ReactElement');
  575. }
  576. var options = {
  577. filterProps: filterProps,
  578. showDefaultProps: showDefaultProps,
  579. showFunctions: showFunctions,
  580. functionValue: functionValue,
  581. tabStop: tabStop,
  582. useBooleanShorthandSyntax: useBooleanShorthandSyntax,
  583. useFragmentShortSyntax: useFragmentShortSyntax,
  584. sortProps: sortProps,
  585. maxInlineAttributesLineLength: maxInlineAttributesLineLength,
  586. displayName: displayName
  587. };
  588. return formatTree(parseReactElement(element, options), options);
  589. };
  590. exports["default"] = reactElementToJsxString;
  591. exports.inlineFunction = inlineFunction;
  592. exports.preserveFunctionLineBreak = preserveFunctionLineBreak;
  593. //# sourceMappingURL=index.js.map