index.js 23 KB

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