doc.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.doc = factory());
  5. }(this, (function () { 'use strict';
  6. function concat(parts) {
  7. return {
  8. type: "concat",
  9. parts: parts
  10. };
  11. }
  12. function indent(contents) {
  13. return {
  14. type: "indent",
  15. contents: contents
  16. };
  17. }
  18. function align(n, contents) {
  19. return {
  20. type: "align",
  21. contents: contents,
  22. n: n
  23. };
  24. }
  25. function group(contents, opts) {
  26. opts = opts || {};
  27. return {
  28. type: "group",
  29. id: opts.id,
  30. contents: contents,
  31. break: !!opts.shouldBreak,
  32. expandedStates: opts.expandedStates
  33. };
  34. }
  35. function dedentToRoot(contents) {
  36. return align(-Infinity, contents);
  37. }
  38. function markAsRoot(contents) {
  39. return align({
  40. type: "root"
  41. }, contents);
  42. }
  43. function dedent(contents) {
  44. return align(-1, contents);
  45. }
  46. function conditionalGroup(states, opts) {
  47. return group(states[0], Object.assign(opts || {}, {
  48. expandedStates: states
  49. }));
  50. }
  51. function fill(parts) {
  52. return {
  53. type: "fill",
  54. parts: parts
  55. };
  56. }
  57. function ifBreak(breakContents, flatContents, opts) {
  58. opts = opts || {};
  59. return {
  60. type: "if-break",
  61. breakContents: breakContents,
  62. flatContents: flatContents,
  63. groupId: opts.groupId
  64. };
  65. }
  66. function lineSuffix(contents) {
  67. return {
  68. type: "line-suffix",
  69. contents: contents
  70. };
  71. }
  72. var lineSuffixBoundary = {
  73. type: "line-suffix-boundary"
  74. };
  75. var breakParent = {
  76. type: "break-parent"
  77. };
  78. var trim = {
  79. type: "trim"
  80. };
  81. var line = {
  82. type: "line"
  83. };
  84. var softline = {
  85. type: "line",
  86. soft: true
  87. };
  88. var hardline = concat([{
  89. type: "line",
  90. hard: true
  91. }, breakParent]);
  92. var literalline = concat([{
  93. type: "line",
  94. hard: true,
  95. literal: true
  96. }, breakParent]);
  97. var cursor = {
  98. type: "cursor",
  99. placeholder: Symbol("cursor")
  100. };
  101. function join(sep, arr) {
  102. var res = [];
  103. for (var i = 0; i < arr.length; i++) {
  104. if (i !== 0) {
  105. res.push(sep);
  106. }
  107. res.push(arr[i]);
  108. }
  109. return concat(res);
  110. }
  111. function addAlignmentToDoc(doc, size, tabWidth) {
  112. var aligned = doc;
  113. if (size > 0) {
  114. // Use indent to add tabs for all the levels of tabs we need
  115. for (var i = 0; i < Math.floor(size / tabWidth); ++i) {
  116. aligned = indent(aligned);
  117. } // Use align for all the spaces that are needed
  118. aligned = align(size % tabWidth, aligned); // size is absolute from 0 and not relative to the current
  119. // indentation, so we use -Infinity to reset the indentation to 0
  120. aligned = align(-Infinity, aligned);
  121. }
  122. return aligned;
  123. }
  124. var docBuilders = {
  125. concat: concat,
  126. join: join,
  127. line: line,
  128. softline: softline,
  129. hardline: hardline,
  130. literalline: literalline,
  131. group: group,
  132. conditionalGroup: conditionalGroup,
  133. fill: fill,
  134. lineSuffix: lineSuffix,
  135. lineSuffixBoundary: lineSuffixBoundary,
  136. cursor: cursor,
  137. breakParent: breakParent,
  138. ifBreak: ifBreak,
  139. trim: trim,
  140. indent: indent,
  141. align: align,
  142. addAlignmentToDoc: addAlignmentToDoc,
  143. markAsRoot: markAsRoot,
  144. dedentToRoot: dedentToRoot,
  145. dedent: dedent
  146. };
  147. function createCommonjsModule(fn, module) {
  148. return module = { exports: {} }, fn(module, module.exports), module.exports;
  149. }
  150. var ansiRegex = createCommonjsModule(function (module) {
  151. 'use strict';
  152. module.exports = function (options) {
  153. options = Object.assign({
  154. onlyFirst: false
  155. }, options);
  156. var pattern = ["[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)", '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'].join('|');
  157. return new RegExp(pattern, options.onlyFirst ? undefined : 'g');
  158. };
  159. });
  160. var stripAnsi = function stripAnsi(input) {
  161. return typeof input === 'string' ? input.replace(ansiRegex(), '') : input;
  162. };
  163. var isFullwidthCodePoint = createCommonjsModule(function (module) {
  164. 'use strict';
  165. /* eslint-disable yoda */
  166. module.exports = function (x) {
  167. if (Number.isNaN(x)) {
  168. return false;
  169. } // code points are derived from:
  170. // http://www.unix.org/Public/UNIDATA/EastAsianWidth.txt
  171. if (x >= 0x1100 && (x <= 0x115f || // Hangul Jamo
  172. x === 0x2329 || // LEFT-POINTING ANGLE BRACKET
  173. x === 0x232a || // RIGHT-POINTING ANGLE BRACKET
  174. // CJK Radicals Supplement .. Enclosed CJK Letters and Months
  175. 0x2e80 <= x && x <= 0x3247 && x !== 0x303f || // Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A
  176. 0x3250 <= x && x <= 0x4dbf || // CJK Unified Ideographs .. Yi Radicals
  177. 0x4e00 <= x && x <= 0xa4c6 || // Hangul Jamo Extended-A
  178. 0xa960 <= x && x <= 0xa97c || // Hangul Syllables
  179. 0xac00 <= x && x <= 0xd7a3 || // CJK Compatibility Ideographs
  180. 0xf900 <= x && x <= 0xfaff || // Vertical Forms
  181. 0xfe10 <= x && x <= 0xfe19 || // CJK Compatibility Forms .. Small Form Variants
  182. 0xfe30 <= x && x <= 0xfe6b || // Halfwidth and Fullwidth Forms
  183. 0xff01 <= x && x <= 0xff60 || 0xffe0 <= x && x <= 0xffe6 || // Kana Supplement
  184. 0x1b000 <= x && x <= 0x1b001 || // Enclosed Ideographic Supplement
  185. 0x1f200 <= x && x <= 0x1f251 || // CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane
  186. 0x20000 <= x && x <= 0x3fffd)) {
  187. return true;
  188. }
  189. return false;
  190. };
  191. });
  192. var emojiRegex = function emojiRegex() {
  193. // https://mths.be/emoji
  194. return /\uD83C\uDFF4(?:\uDB40\uDC67\uDB40\uDC62(?:\uDB40\uDC65\uDB40\uDC6E\uDB40\uDC67|\uDB40\uDC77\uDB40\uDC6C\uDB40\uDC73|\uDB40\uDC73\uDB40\uDC63\uDB40\uDC74)\uDB40\uDC7F|\u200D\u2620\uFE0F)|\uD83D\uDC69\u200D\uD83D\uDC69\u200D(?:\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67]))|\uD83D\uDC68(?:\u200D(?:\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D)?\uD83D\uDC68|(?:\uD83D[\uDC68\uDC69])\u200D(?:\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67]))|\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67])|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDB0-\uDDB3])|(?:\uD83C[\uDFFB-\uDFFF])\u200D(?:\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDB0-\uDDB3]))|\uD83D\uDC69\u200D(?:\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D(?:\uD83D[\uDC68\uDC69])|\uD83D[\uDC68\uDC69])|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDB0-\uDDB3])|\uD83D\uDC69\u200D\uD83D\uDC66\u200D\uD83D\uDC66|(?:\uD83D\uDC41\uFE0F\u200D\uD83D\uDDE8|\uD83D\uDC69(?:\uD83C[\uDFFB-\uDFFF])\u200D[\u2695\u2696\u2708]|\uD83D\uDC68(?:(?:\uD83C[\uDFFB-\uDFFF])\u200D[\u2695\u2696\u2708]|\u200D[\u2695\u2696\u2708])|(?:(?:\u26F9|\uD83C[\uDFCB\uDFCC]|\uD83D\uDD75)\uFE0F|\uD83D\uDC6F|\uD83E[\uDD3C\uDDDE\uDDDF])\u200D[\u2640\u2642]|(?:\u26F9|\uD83C[\uDFCB\uDFCC]|\uD83D\uDD75)(?:\uD83C[\uDFFB-\uDFFF])\u200D[\u2640\u2642]|(?:\uD83C[\uDFC3\uDFC4\uDFCA]|\uD83D[\uDC6E\uDC71\uDC73\uDC77\uDC81\uDC82\uDC86\uDC87\uDE45-\uDE47\uDE4B\uDE4D\uDE4E\uDEA3\uDEB4-\uDEB6]|\uD83E[\uDD26\uDD37-\uDD39\uDD3D\uDD3E\uDDB8\uDDB9\uDDD6-\uDDDD])(?:(?:\uD83C[\uDFFB-\uDFFF])\u200D[\u2640\u2642]|\u200D[\u2640\u2642])|\uD83D\uDC69\u200D[\u2695\u2696\u2708])\uFE0F|\uD83D\uDC69\u200D\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67])|\uD83D\uDC69\u200D\uD83D\uDC69\u200D(?:\uD83D[\uDC66\uDC67])|\uD83D\uDC68(?:\u200D(?:(?:\uD83D[\uDC68\uDC69])\u200D(?:\uD83D[\uDC66\uDC67])|\uD83D[\uDC66\uDC67])|\uD83C[\uDFFB-\uDFFF])|\uD83C\uDFF3\uFE0F\u200D\uD83C\uDF08|\uD83D\uDC69\u200D\uD83D\uDC67|\uD83D\uDC69(?:\uD83C[\uDFFB-\uDFFF])\u200D(?:\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDB0-\uDDB3])|\uD83D\uDC69\u200D\uD83D\uDC66|\uD83C\uDDF6\uD83C\uDDE6|\uD83C\uDDFD\uD83C\uDDF0|\uD83C\uDDF4\uD83C\uDDF2|\uD83D\uDC69(?:\uD83C[\uDFFB-\uDFFF])|\uD83C\uDDED(?:\uD83C[\uDDF0\uDDF2\uDDF3\uDDF7\uDDF9\uDDFA])|\uD83C\uDDEC(?:\uD83C[\uDDE6\uDDE7\uDDE9-\uDDEE\uDDF1-\uDDF3\uDDF5-\uDDFA\uDDFC\uDDFE])|\uD83C\uDDEA(?:\uD83C[\uDDE6\uDDE8\uDDEA\uDDEC\uDDED\uDDF7-\uDDFA])|\uD83C\uDDE8(?:\uD83C[\uDDE6\uDDE8\uDDE9\uDDEB-\uDDEE\uDDF0-\uDDF5\uDDF7\uDDFA-\uDDFF])|\uD83C\uDDF2(?:\uD83C[\uDDE6\uDDE8-\uDDED\uDDF0-\uDDFF])|\uD83C\uDDF3(?:\uD83C[\uDDE6\uDDE8\uDDEA-\uDDEC\uDDEE\uDDF1\uDDF4\uDDF5\uDDF7\uDDFA\uDDFF])|\uD83C\uDDFC(?:\uD83C[\uDDEB\uDDF8])|\uD83C\uDDFA(?:\uD83C[\uDDE6\uDDEC\uDDF2\uDDF3\uDDF8\uDDFE\uDDFF])|\uD83C\uDDF0(?:\uD83C[\uDDEA\uDDEC-\uDDEE\uDDF2\uDDF3\uDDF5\uDDF7\uDDFC\uDDFE\uDDFF])|\uD83C\uDDEF(?:\uD83C[\uDDEA\uDDF2\uDDF4\uDDF5])|\uD83C\uDDF8(?:\uD83C[\uDDE6-\uDDEA\uDDEC-\uDDF4\uDDF7-\uDDF9\uDDFB\uDDFD-\uDDFF])|\uD83C\uDDEE(?:\uD83C[\uDDE8-\uDDEA\uDDF1-\uDDF4\uDDF6-\uDDF9])|\uD83C\uDDFF(?:\uD83C[\uDDE6\uDDF2\uDDFC])|\uD83C\uDDEB(?:\uD83C[\uDDEE-\uDDF0\uDDF2\uDDF4\uDDF7])|\uD83C\uDDF5(?:\uD83C[\uDDE6\uDDEA-\uDDED\uDDF0-\uDDF3\uDDF7-\uDDF9\uDDFC\uDDFE])|\uD83C\uDDE9(?:\uD83C[\uDDEA\uDDEC\uDDEF\uDDF0\uDDF2\uDDF4\uDDFF])|\uD83C\uDDF9(?:\uD83C[\uDDE6\uDDE8\uDDE9\uDDEB-\uDDED\uDDEF-\uDDF4\uDDF7\uDDF9\uDDFB\uDDFC\uDDFF])|\uD83C\uDDE7(?:\uD83C[\uDDE6\uDDE7\uDDE9-\uDDEF\uDDF1-\uDDF4\uDDF6-\uDDF9\uDDFB\uDDFC\uDDFE\uDDFF])|[#\*0-9]\uFE0F\u20E3|\uD83C\uDDF1(?:\uD83C[\uDDE6-\uDDE8\uDDEE\uDDF0\uDDF7-\uDDFB\uDDFE])|\uD83C\uDDE6(?:\uD83C[\uDDE8-\uDDEC\uDDEE\uDDF1\uDDF2\uDDF4\uDDF6-\uDDFA\uDDFC\uDDFD\uDDFF])|\uD83C\uDDF7(?:\uD83C[\uDDEA\uDDF4\uDDF8\uDDFA\uDDFC])|\uD83C\uDDFB(?:\uD83C[\uDDE6\uDDE8\uDDEA\uDDEC\uDDEE\uDDF3\uDDFA])|\uD83C\uDDFE(?:\uD83C[\uDDEA\uDDF9])|(?:\uD83C[\uDFC3\uDFC4\uDFCA]|\uD83D[\uDC6E\uDC71\uDC73\uDC77\uDC81\uDC82\uDC86\uDC87\uDE45-\uDE47\uDE4B\uDE4D\uDE4E\uDEA3\uDEB4-\uDEB6]|\uD83E[\uDD26\uDD37-\uDD39\uDD3D\uDD3E\uDDB8\uDDB9\uDDD6-\uDDDD])(?:\uD83C[\uDFFB-\uDFFF])|(?:\u26F9|\uD83C[\uDFCB\uDFCC]|\uD83D\uDD75)(?:\uD83C[\uDFFB-\uDFFF])|(?:[\u261D\u270A-\u270D]|\uD83C[\uDF85\uDFC2\uDFC7]|\uD83D[\uDC42\uDC43\uDC46-\uDC50\uDC66\uDC67\uDC70\uDC72\uDC74-\uDC76\uDC78\uDC7C\uDC83\uDC85\uDCAA\uDD74\uDD7A\uDD90\uDD95\uDD96\uDE4C\uDE4F\uDEC0\uDECC]|\uD83E[\uDD18-\uDD1C\uDD1E\uDD1F\uDD30-\uDD36\uDDB5\uDDB6\uDDD1-\uDDD5])(?:\uD83C[\uDFFB-\uDFFF])|(?:[\u231A\u231B\u23E9-\u23EC\u23F0\u23F3\u25FD\u25FE\u2614\u2615\u2648-\u2653\u267F\u2693\u26A1\u26AA\u26AB\u26BD\u26BE\u26C4\u26C5\u26CE\u26D4\u26EA\u26F2\u26F3\u26F5\u26FA\u26FD\u2705\u270A\u270B\u2728\u274C\u274E\u2753-\u2755\u2757\u2795-\u2797\u27B0\u27BF\u2B1B\u2B1C\u2B50\u2B55]|\uD83C[\uDC04\uDCCF\uDD8E\uDD91-\uDD9A\uDDE6-\uDDFF\uDE01\uDE1A\uDE2F\uDE32-\uDE36\uDE38-\uDE3A\uDE50\uDE51\uDF00-\uDF20\uDF2D-\uDF35\uDF37-\uDF7C\uDF7E-\uDF93\uDFA0-\uDFCA\uDFCF-\uDFD3\uDFE0-\uDFF0\uDFF4\uDFF8-\uDFFF]|\uD83D[\uDC00-\uDC3E\uDC40\uDC42-\uDCFC\uDCFF-\uDD3D\uDD4B-\uDD4E\uDD50-\uDD67\uDD7A\uDD95\uDD96\uDDA4\uDDFB-\uDE4F\uDE80-\uDEC5\uDECC\uDED0-\uDED2\uDEEB\uDEEC\uDEF4-\uDEF9]|\uD83E[\uDD10-\uDD3A\uDD3C-\uDD3E\uDD40-\uDD45\uDD47-\uDD70\uDD73-\uDD76\uDD7A\uDD7C-\uDDA2\uDDB0-\uDDB9\uDDC0-\uDDC2\uDDD0-\uDDFF])|(?:[#\*0-9\xA9\xAE\u203C\u2049\u2122\u2139\u2194-\u2199\u21A9\u21AA\u231A\u231B\u2328\u23CF\u23E9-\u23F3\u23F8-\u23FA\u24C2\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE\u2600-\u2604\u260E\u2611\u2614\u2615\u2618\u261D\u2620\u2622\u2623\u2626\u262A\u262E\u262F\u2638-\u263A\u2640\u2642\u2648-\u2653\u265F\u2660\u2663\u2665\u2666\u2668\u267B\u267E\u267F\u2692-\u2697\u2699\u269B\u269C\u26A0\u26A1\u26AA\u26AB\u26B0\u26B1\u26BD\u26BE\u26C4\u26C5\u26C8\u26CE\u26CF\u26D1\u26D3\u26D4\u26E9\u26EA\u26F0-\u26F5\u26F7-\u26FA\u26FD\u2702\u2705\u2708-\u270D\u270F\u2712\u2714\u2716\u271D\u2721\u2728\u2733\u2734\u2744\u2747\u274C\u274E\u2753-\u2755\u2757\u2763\u2764\u2795-\u2797\u27A1\u27B0\u27BF\u2934\u2935\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55\u3030\u303D\u3297\u3299]|\uD83C[\uDC04\uDCCF\uDD70\uDD71\uDD7E\uDD7F\uDD8E\uDD91-\uDD9A\uDDE6-\uDDFF\uDE01\uDE02\uDE1A\uDE2F\uDE32-\uDE3A\uDE50\uDE51\uDF00-\uDF21\uDF24-\uDF93\uDF96\uDF97\uDF99-\uDF9B\uDF9E-\uDFF0\uDFF3-\uDFF5\uDFF7-\uDFFF]|\uD83D[\uDC00-\uDCFD\uDCFF-\uDD3D\uDD49-\uDD4E\uDD50-\uDD67\uDD6F\uDD70\uDD73-\uDD7A\uDD87\uDD8A-\uDD8D\uDD90\uDD95\uDD96\uDDA4\uDDA5\uDDA8\uDDB1\uDDB2\uDDBC\uDDC2-\uDDC4\uDDD1-\uDDD3\uDDDC-\uDDDE\uDDE1\uDDE3\uDDE8\uDDEF\uDDF3\uDDFA-\uDE4F\uDE80-\uDEC5\uDECB-\uDED2\uDEE0-\uDEE5\uDEE9\uDEEB\uDEEC\uDEF0\uDEF3-\uDEF9]|\uD83E[\uDD10-\uDD3A\uDD3C-\uDD3E\uDD40-\uDD45\uDD47-\uDD70\uDD73-\uDD76\uDD7A\uDD7C-\uDDA2\uDDB0-\uDDB9\uDDC0-\uDDC2\uDDD0-\uDDFF])\uFE0F|(?:[\u261D\u26F9\u270A-\u270D]|\uD83C[\uDF85\uDFC2-\uDFC4\uDFC7\uDFCA-\uDFCC]|\uD83D[\uDC42\uDC43\uDC46-\uDC50\uDC66-\uDC69\uDC6E\uDC70-\uDC78\uDC7C\uDC81-\uDC83\uDC85-\uDC87\uDCAA\uDD74\uDD75\uDD7A\uDD90\uDD95\uDD96\uDE45-\uDE47\uDE4B-\uDE4F\uDEA3\uDEB4-\uDEB6\uDEC0\uDECC]|\uD83E[\uDD18-\uDD1C\uDD1E\uDD1F\uDD26\uDD30-\uDD39\uDD3D\uDD3E\uDDB5\uDDB6\uDDB8\uDDB9\uDDD1-\uDDDD])/g;
  195. };
  196. var stringWidth = createCommonjsModule(function (module) {
  197. 'use strict';
  198. var emojiRegex$$1 = emojiRegex();
  199. module.exports = function (input) {
  200. input = input.replace(emojiRegex$$1, ' ');
  201. if (typeof input !== 'string' || input.length === 0) {
  202. return 0;
  203. }
  204. input = stripAnsi(input);
  205. var width = 0;
  206. for (var i = 0; i < input.length; i++) {
  207. var code = input.codePointAt(i); // Ignore control characters
  208. if (code <= 0x1F || code >= 0x7F && code <= 0x9F) {
  209. continue;
  210. } // Ignore combining characters
  211. if (code >= 0x300 && code <= 0x36F) {
  212. continue;
  213. } // Surrogates
  214. if (code > 0xFFFF) {
  215. i++;
  216. }
  217. width += isFullwidthCodePoint(code) ? 2 : 1;
  218. }
  219. return width;
  220. };
  221. });
  222. var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
  223. var escapeStringRegexp = function escapeStringRegexp(str) {
  224. if (typeof str !== 'string') {
  225. throw new TypeError('Expected a string');
  226. }
  227. return str.replace(matchOperatorsRe, '\\$&');
  228. };
  229. var getLast = function getLast(arr) {
  230. return arr.length > 0 ? arr[arr.length - 1] : null;
  231. };
  232. var notAsciiRegex = /[^\x20-\x7F]/;
  233. function isExportDeclaration(node) {
  234. if (node) {
  235. switch (node.type) {
  236. case "ExportDefaultDeclaration":
  237. case "ExportDefaultSpecifier":
  238. case "DeclareExportDeclaration":
  239. case "ExportNamedDeclaration":
  240. case "ExportAllDeclaration":
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. function getParentExportDeclaration(path) {
  247. var parentNode = path.getParentNode();
  248. if (path.getName() === "declaration" && isExportDeclaration(parentNode)) {
  249. return parentNode;
  250. }
  251. return null;
  252. }
  253. function getPenultimate(arr) {
  254. if (arr.length > 1) {
  255. return arr[arr.length - 2];
  256. }
  257. return null;
  258. }
  259. function skip(chars) {
  260. return function (text, index, opts) {
  261. var backwards = opts && opts.backwards; // Allow `skip` functions to be threaded together without having
  262. // to check for failures (did someone say monads?).
  263. if (index === false) {
  264. return false;
  265. }
  266. var length = text.length;
  267. var cursor = index;
  268. while (cursor >= 0 && cursor < length) {
  269. var c = text.charAt(cursor);
  270. if (chars instanceof RegExp) {
  271. if (!chars.test(c)) {
  272. return cursor;
  273. }
  274. } else if (chars.indexOf(c) === -1) {
  275. return cursor;
  276. }
  277. backwards ? cursor-- : cursor++;
  278. }
  279. if (cursor === -1 || cursor === length) {
  280. // If we reached the beginning or end of the file, return the
  281. // out-of-bounds cursor. It's up to the caller to handle this
  282. // correctly. We don't want to indicate `false` though if it
  283. // actually skipped valid characters.
  284. return cursor;
  285. }
  286. return false;
  287. };
  288. }
  289. var skipWhitespace = skip(/\s/);
  290. var skipSpaces = skip(" \t");
  291. var skipToLineEnd = skip(",; \t");
  292. var skipEverythingButNewLine = skip(/[^\r\n]/);
  293. function skipInlineComment(text, index) {
  294. if (index === false) {
  295. return false;
  296. }
  297. if (text.charAt(index) === "/" && text.charAt(index + 1) === "*") {
  298. for (var i = index + 2; i < text.length; ++i) {
  299. if (text.charAt(i) === "*" && text.charAt(i + 1) === "/") {
  300. return i + 2;
  301. }
  302. }
  303. }
  304. return index;
  305. }
  306. function skipTrailingComment(text, index) {
  307. if (index === false) {
  308. return false;
  309. }
  310. if (text.charAt(index) === "/" && text.charAt(index + 1) === "/") {
  311. return skipEverythingButNewLine(text, index);
  312. }
  313. return index;
  314. } // This one doesn't use the above helper function because it wants to
  315. // test \r\n in order and `skip` doesn't support ordering and we only
  316. // want to skip one newline. It's simple to implement.
  317. function skipNewline(text, index, opts) {
  318. var backwards = opts && opts.backwards;
  319. if (index === false) {
  320. return false;
  321. }
  322. var atIndex = text.charAt(index);
  323. if (backwards) {
  324. if (text.charAt(index - 1) === "\r" && atIndex === "\n") {
  325. return index - 2;
  326. }
  327. if (atIndex === "\n" || atIndex === "\r" || atIndex === "\u2028" || atIndex === "\u2029") {
  328. return index - 1;
  329. }
  330. } else {
  331. if (atIndex === "\r" && text.charAt(index + 1) === "\n") {
  332. return index + 2;
  333. }
  334. if (atIndex === "\n" || atIndex === "\r" || atIndex === "\u2028" || atIndex === "\u2029") {
  335. return index + 1;
  336. }
  337. }
  338. return index;
  339. }
  340. function hasNewline(text, index, opts) {
  341. opts = opts || {};
  342. var idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
  343. var idx2 = skipNewline(text, idx, opts);
  344. return idx !== idx2;
  345. }
  346. function hasNewlineInRange(text, start, end) {
  347. for (var i = start; i < end; ++i) {
  348. if (text.charAt(i) === "\n") {
  349. return true;
  350. }
  351. }
  352. return false;
  353. } // Note: this function doesn't ignore leading comments unlike isNextLineEmpty
  354. function isPreviousLineEmpty(text, node, locStart) {
  355. var idx = locStart(node) - 1;
  356. idx = skipSpaces(text, idx, {
  357. backwards: true
  358. });
  359. idx = skipNewline(text, idx, {
  360. backwards: true
  361. });
  362. idx = skipSpaces(text, idx, {
  363. backwards: true
  364. });
  365. var idx2 = skipNewline(text, idx, {
  366. backwards: true
  367. });
  368. return idx !== idx2;
  369. }
  370. function isNextLineEmptyAfterIndex(text, index) {
  371. var oldIdx = null;
  372. var idx = index;
  373. while (idx !== oldIdx) {
  374. // We need to skip all the potential trailing inline comments
  375. oldIdx = idx;
  376. idx = skipToLineEnd(text, idx);
  377. idx = skipInlineComment(text, idx);
  378. idx = skipSpaces(text, idx);
  379. }
  380. idx = skipTrailingComment(text, idx);
  381. idx = skipNewline(text, idx);
  382. return hasNewline(text, idx);
  383. }
  384. function isNextLineEmpty(text, node, locEnd) {
  385. return isNextLineEmptyAfterIndex(text, locEnd(node));
  386. }
  387. function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, idx) {
  388. var oldIdx = null;
  389. while (idx !== oldIdx) {
  390. oldIdx = idx;
  391. idx = skipSpaces(text, idx);
  392. idx = skipInlineComment(text, idx);
  393. idx = skipTrailingComment(text, idx);
  394. idx = skipNewline(text, idx);
  395. }
  396. return idx;
  397. }
  398. function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
  399. return getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, locEnd(node));
  400. }
  401. function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
  402. return text.charAt(getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd));
  403. }
  404. function hasSpaces(text, index, opts) {
  405. opts = opts || {};
  406. var idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
  407. return idx !== index;
  408. }
  409. function setLocStart(node, index) {
  410. if (node.range) {
  411. node.range[0] = index;
  412. } else {
  413. node.start = index;
  414. }
  415. }
  416. function setLocEnd(node, index) {
  417. if (node.range) {
  418. node.range[1] = index;
  419. } else {
  420. node.end = index;
  421. }
  422. }
  423. var PRECEDENCE = {};
  424. [["|>"], ["||", "??"], ["&&"], ["|"], ["^"], ["&"], ["==", "===", "!=", "!=="], ["<", ">", "<=", ">=", "in", "instanceof"], [">>", "<<", ">>>"], ["+", "-"], ["*", "/", "%"], ["**"]].forEach(function (tier, i) {
  425. tier.forEach(function (op) {
  426. PRECEDENCE[op] = i;
  427. });
  428. });
  429. function getPrecedence(op) {
  430. return PRECEDENCE[op];
  431. }
  432. var equalityOperators = {
  433. "==": true,
  434. "!=": true,
  435. "===": true,
  436. "!==": true
  437. };
  438. var multiplicativeOperators = {
  439. "*": true,
  440. "/": true,
  441. "%": true
  442. };
  443. var bitshiftOperators = {
  444. ">>": true,
  445. ">>>": true,
  446. "<<": true
  447. };
  448. function shouldFlatten(parentOp, nodeOp) {
  449. if (getPrecedence(nodeOp) !== getPrecedence(parentOp)) {
  450. return false;
  451. } // ** is right-associative
  452. // x ** y ** z --> x ** (y ** z)
  453. if (parentOp === "**") {
  454. return false;
  455. } // x == y == z --> (x == y) == z
  456. if (equalityOperators[parentOp] && equalityOperators[nodeOp]) {
  457. return false;
  458. } // x * y % z --> (x * y) % z
  459. if (nodeOp === "%" && multiplicativeOperators[parentOp] || parentOp === "%" && multiplicativeOperators[nodeOp]) {
  460. return false;
  461. } // x * y / z --> (x * y) / z
  462. // x / y * z --> (x / y) * z
  463. if (nodeOp !== parentOp && multiplicativeOperators[nodeOp] && multiplicativeOperators[parentOp]) {
  464. return false;
  465. } // x << y << z --> (x << y) << z
  466. if (bitshiftOperators[parentOp] && bitshiftOperators[nodeOp]) {
  467. return false;
  468. }
  469. return true;
  470. }
  471. function isBitwiseOperator(operator) {
  472. return !!bitshiftOperators[operator] || operator === "|" || operator === "^" || operator === "&";
  473. } // Tests if an expression starts with `{`, or (if forbidFunctionClassAndDoExpr
  474. // holds) `function`, `class`, or `do {}`. Will be overzealous if there's
  475. // already necessary grouping parentheses.
  476. function startsWithNoLookaheadToken(node, forbidFunctionClassAndDoExpr) {
  477. node = getLeftMost(node);
  478. switch (node.type) {
  479. case "FunctionExpression":
  480. case "ClassExpression":
  481. case "DoExpression":
  482. return forbidFunctionClassAndDoExpr;
  483. case "ObjectExpression":
  484. return true;
  485. case "MemberExpression":
  486. return startsWithNoLookaheadToken(node.object, forbidFunctionClassAndDoExpr);
  487. case "TaggedTemplateExpression":
  488. if (node.tag.type === "FunctionExpression") {
  489. // IIFEs are always already parenthesized
  490. return false;
  491. }
  492. return startsWithNoLookaheadToken(node.tag, forbidFunctionClassAndDoExpr);
  493. case "CallExpression":
  494. if (node.callee.type === "FunctionExpression") {
  495. // IIFEs are always already parenthesized
  496. return false;
  497. }
  498. return startsWithNoLookaheadToken(node.callee, forbidFunctionClassAndDoExpr);
  499. case "ConditionalExpression":
  500. return startsWithNoLookaheadToken(node.test, forbidFunctionClassAndDoExpr);
  501. case "UpdateExpression":
  502. return !node.prefix && startsWithNoLookaheadToken(node.argument, forbidFunctionClassAndDoExpr);
  503. case "BindExpression":
  504. return node.object && startsWithNoLookaheadToken(node.object, forbidFunctionClassAndDoExpr);
  505. case "SequenceExpression":
  506. return startsWithNoLookaheadToken(node.expressions[0], forbidFunctionClassAndDoExpr);
  507. case "TSAsExpression":
  508. return startsWithNoLookaheadToken(node.expression, forbidFunctionClassAndDoExpr);
  509. default:
  510. return false;
  511. }
  512. }
  513. function getLeftMost(node) {
  514. if (node.left) {
  515. return getLeftMost(node.left);
  516. }
  517. return node;
  518. }
  519. function getAlignmentSize(value, tabWidth, startIndex) {
  520. startIndex = startIndex || 0;
  521. var size = 0;
  522. for (var i = startIndex; i < value.length; ++i) {
  523. if (value[i] === "\t") {
  524. // Tabs behave in a way that they are aligned to the nearest
  525. // multiple of tabWidth:
  526. // 0 -> 4, 1 -> 4, 2 -> 4, 3 -> 4
  527. // 4 -> 8, 5 -> 8, 6 -> 8, 7 -> 8 ...
  528. size = size + tabWidth - size % tabWidth;
  529. } else {
  530. size++;
  531. }
  532. }
  533. return size;
  534. }
  535. function getIndentSize(value, tabWidth) {
  536. var lastNewlineIndex = value.lastIndexOf("\n");
  537. if (lastNewlineIndex === -1) {
  538. return 0;
  539. }
  540. return getAlignmentSize( // All the leading whitespaces
  541. value.slice(lastNewlineIndex + 1).match(/^[ \t]*/)[0], tabWidth);
  542. }
  543. function getPreferredQuote(raw, preferredQuote) {
  544. // `rawContent` is the string exactly like it appeared in the input source
  545. // code, without its enclosing quotes.
  546. var rawContent = raw.slice(1, -1);
  547. var double = {
  548. quote: '"',
  549. regex: /"/g
  550. };
  551. var single = {
  552. quote: "'",
  553. regex: /'/g
  554. };
  555. var preferred = preferredQuote === "'" ? single : double;
  556. var alternate = preferred === single ? double : single;
  557. var result = preferred.quote; // If `rawContent` contains at least one of the quote preferred for enclosing
  558. // the string, we might want to enclose with the alternate quote instead, to
  559. // minimize the number of escaped quotes.
  560. if (rawContent.includes(preferred.quote) || rawContent.includes(alternate.quote)) {
  561. var numPreferredQuotes = (rawContent.match(preferred.regex) || []).length;
  562. var numAlternateQuotes = (rawContent.match(alternate.regex) || []).length;
  563. result = numPreferredQuotes > numAlternateQuotes ? alternate.quote : preferred.quote;
  564. }
  565. return result;
  566. }
  567. function printString(raw, options, isDirectiveLiteral) {
  568. // `rawContent` is the string exactly like it appeared in the input source
  569. // code, without its enclosing quotes.
  570. var rawContent = raw.slice(1, -1); // Check for the alternate quote, to determine if we're allowed to swap
  571. // the quotes on a DirectiveLiteral.
  572. var canChangeDirectiveQuotes = !rawContent.includes('"') && !rawContent.includes("'");
  573. var enclosingQuote = options.parser === "json" ? '"' : options.__isInHtmlAttribute ? "'" : getPreferredQuote(raw, options.singleQuote ? "'" : '"'); // Directives are exact code unit sequences, which means that you can't
  574. // change the escape sequences they use.
  575. // See https://github.com/prettier/prettier/issues/1555
  576. // and https://tc39.github.io/ecma262/#directive-prologue
  577. if (isDirectiveLiteral) {
  578. if (canChangeDirectiveQuotes) {
  579. return enclosingQuote + rawContent + enclosingQuote;
  580. }
  581. return raw;
  582. } // It might sound unnecessary to use `makeString` even if the string already
  583. // is enclosed with `enclosingQuote`, but it isn't. The string could contain
  584. // unnecessary escapes (such as in `"\'"`). Always using `makeString` makes
  585. // sure that we consistently output the minimum amount of escaped quotes.
  586. return makeString(rawContent, enclosingQuote, !(options.parser === "css" || options.parser === "less" || options.parser === "scss" || options.embeddedInHtml));
  587. }
  588. function makeString(rawContent, enclosingQuote, unescapeUnnecessaryEscapes) {
  589. var otherQuote = enclosingQuote === '"' ? "'" : '"'; // Matches _any_ escape and unescaped quotes (both single and double).
  590. var regex = /\\([\s\S])|(['"])/g; // Escape and unescape single and double quotes as needed to be able to
  591. // enclose `rawContent` with `enclosingQuote`.
  592. var newContent = rawContent.replace(regex, function (match, escaped, quote) {
  593. // If we matched an escape, and the escaped character is a quote of the
  594. // other type than we intend to enclose the string with, there's no need for
  595. // it to be escaped, so return it _without_ the backslash.
  596. if (escaped === otherQuote) {
  597. return escaped;
  598. } // If we matched an unescaped quote and it is of the _same_ type as we
  599. // intend to enclose the string with, it must be escaped, so return it with
  600. // a backslash.
  601. if (quote === enclosingQuote) {
  602. return "\\" + quote;
  603. }
  604. if (quote) {
  605. return quote;
  606. } // Unescape any unnecessarily escaped character.
  607. // Adapted from https://github.com/eslint/eslint/blob/de0b4ad7bd820ade41b1f606008bea68683dc11a/lib/rules/no-useless-escape.js#L27
  608. return unescapeUnnecessaryEscapes && /^[^\\nrvtbfux\r\n\u2028\u2029"'0-7]$/.test(escaped) ? escaped : "\\" + escaped;
  609. });
  610. return enclosingQuote + newContent + enclosingQuote;
  611. }
  612. function printNumber(rawNumber) {
  613. return rawNumber.toLowerCase() // Remove unnecessary plus and zeroes from scientific notation.
  614. .replace(/^([+-]?[\d.]+e)(?:\+|(-))?0*(\d)/, "$1$2$3") // Remove unnecessary scientific notation (1e0).
  615. .replace(/^([+-]?[\d.]+)e[+-]?0+$/, "$1") // Make sure numbers always start with a digit.
  616. .replace(/^([+-])?\./, "$10.") // Remove extraneous trailing decimal zeroes.
  617. .replace(/(\.\d+?)0+(?=e|$)/, "$1") // Remove trailing dot.
  618. .replace(/\.(?=e|$)/, "");
  619. }
  620. function getMaxContinuousCount(str, target) {
  621. var results = str.match(new RegExp("(".concat(escapeStringRegexp(target), ")+"), "g"));
  622. if (results === null) {
  623. return 0;
  624. }
  625. return results.reduce(function (maxCount, result) {
  626. return Math.max(maxCount, result.length / target.length);
  627. }, 0);
  628. }
  629. function getMinNotPresentContinuousCount(str, target) {
  630. var matches = str.match(new RegExp("(".concat(escapeStringRegexp(target), ")+"), "g"));
  631. if (matches === null) {
  632. return 0;
  633. }
  634. var countPresent = new Map();
  635. var max = 0;
  636. var _iteratorNormalCompletion = true;
  637. var _didIteratorError = false;
  638. var _iteratorError = undefined;
  639. try {
  640. for (var _iterator = matches[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  641. var match = _step.value;
  642. var count = match.length / target.length;
  643. countPresent.set(count, true);
  644. if (count > max) {
  645. max = count;
  646. }
  647. }
  648. } catch (err) {
  649. _didIteratorError = true;
  650. _iteratorError = err;
  651. } finally {
  652. try {
  653. if (!_iteratorNormalCompletion && _iterator.return != null) {
  654. _iterator.return();
  655. }
  656. } finally {
  657. if (_didIteratorError) {
  658. throw _iteratorError;
  659. }
  660. }
  661. }
  662. for (var i = 1; i < max; i++) {
  663. if (!countPresent.get(i)) {
  664. return i;
  665. }
  666. }
  667. return max + 1;
  668. }
  669. function getStringWidth$1(text) {
  670. if (!text) {
  671. return 0;
  672. } // shortcut to avoid needless string `RegExp`s, replacements, and allocations within `string-width`
  673. if (!notAsciiRegex.test(text)) {
  674. return text.length;
  675. }
  676. return stringWidth(text);
  677. }
  678. function hasIgnoreComment(path) {
  679. var node = path.getValue();
  680. return hasNodeIgnoreComment(node);
  681. }
  682. function hasNodeIgnoreComment(node) {
  683. return node && node.comments && node.comments.length > 0 && node.comments.some(function (comment) {
  684. return comment.value.trim() === "prettier-ignore";
  685. });
  686. }
  687. function matchAncestorTypes(path, types, index) {
  688. index = index || 0;
  689. types = types.slice();
  690. while (types.length) {
  691. var parent = path.getParentNode(index);
  692. var type = types.shift();
  693. if (!parent || parent.type !== type) {
  694. return false;
  695. }
  696. index++;
  697. }
  698. return true;
  699. }
  700. function addCommentHelper(node, comment) {
  701. var comments = node.comments || (node.comments = []);
  702. comments.push(comment);
  703. comment.printed = false; // For some reason, TypeScript parses `// x` inside of JSXText as a comment
  704. // We already "print" it via the raw text, we don't need to re-print it as a
  705. // comment
  706. if (node.type === "JSXText") {
  707. comment.printed = true;
  708. }
  709. }
  710. function addLeadingComment(node, comment) {
  711. comment.leading = true;
  712. comment.trailing = false;
  713. addCommentHelper(node, comment);
  714. }
  715. function addDanglingComment(node, comment) {
  716. comment.leading = false;
  717. comment.trailing = false;
  718. addCommentHelper(node, comment);
  719. }
  720. function addTrailingComment(node, comment) {
  721. comment.leading = false;
  722. comment.trailing = true;
  723. addCommentHelper(node, comment);
  724. }
  725. function isWithinParentArrayProperty(path, propertyName) {
  726. var node = path.getValue();
  727. var parent = path.getParentNode();
  728. if (parent == null) {
  729. return false;
  730. }
  731. if (!Array.isArray(parent[propertyName])) {
  732. return false;
  733. }
  734. var key = path.getName();
  735. return parent[propertyName][key] === node;
  736. }
  737. function replaceEndOfLineWith(text, replacement) {
  738. var parts = [];
  739. var _iteratorNormalCompletion2 = true;
  740. var _didIteratorError2 = false;
  741. var _iteratorError2 = undefined;
  742. try {
  743. for (var _iterator2 = text.split("\n")[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  744. var part = _step2.value;
  745. if (parts.length !== 0) {
  746. parts.push(replacement);
  747. }
  748. parts.push(part);
  749. }
  750. } catch (err) {
  751. _didIteratorError2 = true;
  752. _iteratorError2 = err;
  753. } finally {
  754. try {
  755. if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
  756. _iterator2.return();
  757. }
  758. } finally {
  759. if (_didIteratorError2) {
  760. throw _iteratorError2;
  761. }
  762. }
  763. }
  764. return parts;
  765. }
  766. var util = {
  767. replaceEndOfLineWith: replaceEndOfLineWith,
  768. getStringWidth: getStringWidth$1,
  769. getMaxContinuousCount: getMaxContinuousCount,
  770. getMinNotPresentContinuousCount: getMinNotPresentContinuousCount,
  771. getPrecedence: getPrecedence,
  772. shouldFlatten: shouldFlatten,
  773. isBitwiseOperator: isBitwiseOperator,
  774. isExportDeclaration: isExportDeclaration,
  775. getParentExportDeclaration: getParentExportDeclaration,
  776. getPenultimate: getPenultimate,
  777. getLast: getLast,
  778. getNextNonSpaceNonCommentCharacterIndexWithStartIndex: getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
  779. getNextNonSpaceNonCommentCharacterIndex: getNextNonSpaceNonCommentCharacterIndex,
  780. getNextNonSpaceNonCommentCharacter: getNextNonSpaceNonCommentCharacter,
  781. skip: skip,
  782. skipWhitespace: skipWhitespace,
  783. skipSpaces: skipSpaces,
  784. skipToLineEnd: skipToLineEnd,
  785. skipEverythingButNewLine: skipEverythingButNewLine,
  786. skipInlineComment: skipInlineComment,
  787. skipTrailingComment: skipTrailingComment,
  788. skipNewline: skipNewline,
  789. isNextLineEmptyAfterIndex: isNextLineEmptyAfterIndex,
  790. isNextLineEmpty: isNextLineEmpty,
  791. isPreviousLineEmpty: isPreviousLineEmpty,
  792. hasNewline: hasNewline,
  793. hasNewlineInRange: hasNewlineInRange,
  794. hasSpaces: hasSpaces,
  795. setLocStart: setLocStart,
  796. setLocEnd: setLocEnd,
  797. startsWithNoLookaheadToken: startsWithNoLookaheadToken,
  798. getAlignmentSize: getAlignmentSize,
  799. getIndentSize: getIndentSize,
  800. getPreferredQuote: getPreferredQuote,
  801. printString: printString,
  802. printNumber: printNumber,
  803. hasIgnoreComment: hasIgnoreComment,
  804. hasNodeIgnoreComment: hasNodeIgnoreComment,
  805. makeString: makeString,
  806. matchAncestorTypes: matchAncestorTypes,
  807. addLeadingComment: addLeadingComment,
  808. addDanglingComment: addDanglingComment,
  809. addTrailingComment: addTrailingComment,
  810. isWithinParentArrayProperty: isWithinParentArrayProperty
  811. };
  812. function guessEndOfLine(text) {
  813. var index = text.indexOf("\r");
  814. if (index >= 0) {
  815. return text.charAt(index + 1) === "\n" ? "crlf" : "cr";
  816. }
  817. return "lf";
  818. }
  819. function convertEndOfLineToChars$1(value) {
  820. switch (value) {
  821. case "cr":
  822. return "\r";
  823. case "crlf":
  824. return "\r\n";
  825. default:
  826. return "\n";
  827. }
  828. }
  829. var endOfLine = {
  830. guessEndOfLine: guessEndOfLine,
  831. convertEndOfLineToChars: convertEndOfLineToChars$1
  832. };
  833. var getStringWidth = util.getStringWidth;
  834. var convertEndOfLineToChars = endOfLine.convertEndOfLineToChars;
  835. var concat$1 = docBuilders.concat;
  836. var fill$1 = docBuilders.fill;
  837. var cursor$1 = docBuilders.cursor;
  838. /** @type {{[groupId: PropertyKey]: MODE}} */
  839. var groupModeMap;
  840. var MODE_BREAK = 1;
  841. var MODE_FLAT = 2;
  842. function rootIndent() {
  843. return {
  844. value: "",
  845. length: 0,
  846. queue: []
  847. };
  848. }
  849. function makeIndent(ind, options) {
  850. return generateInd(ind, {
  851. type: "indent"
  852. }, options);
  853. }
  854. function makeAlign(ind, n, options) {
  855. return n === -Infinity ? ind.root || rootIndent() : n < 0 ? generateInd(ind, {
  856. type: "dedent"
  857. }, options) : !n ? ind : n.type === "root" ? Object.assign({}, ind, {
  858. root: ind
  859. }) : typeof n === "string" ? generateInd(ind, {
  860. type: "stringAlign",
  861. n: n
  862. }, options) : generateInd(ind, {
  863. type: "numberAlign",
  864. n: n
  865. }, options);
  866. }
  867. function generateInd(ind, newPart, options) {
  868. var queue = newPart.type === "dedent" ? ind.queue.slice(0, -1) : ind.queue.concat(newPart);
  869. var value = "";
  870. var length = 0;
  871. var lastTabs = 0;
  872. var lastSpaces = 0;
  873. var _iteratorNormalCompletion = true;
  874. var _didIteratorError = false;
  875. var _iteratorError = undefined;
  876. try {
  877. for (var _iterator = queue[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  878. var part = _step.value;
  879. switch (part.type) {
  880. case "indent":
  881. flush();
  882. if (options.useTabs) {
  883. addTabs(1);
  884. } else {
  885. addSpaces(options.tabWidth);
  886. }
  887. break;
  888. case "stringAlign":
  889. flush();
  890. value += part.n;
  891. length += part.n.length;
  892. break;
  893. case "numberAlign":
  894. lastTabs += 1;
  895. lastSpaces += part.n;
  896. break;
  897. /* istanbul ignore next */
  898. default:
  899. throw new Error("Unexpected type '".concat(part.type, "'"));
  900. }
  901. }
  902. } catch (err) {
  903. _didIteratorError = true;
  904. _iteratorError = err;
  905. } finally {
  906. try {
  907. if (!_iteratorNormalCompletion && _iterator.return != null) {
  908. _iterator.return();
  909. }
  910. } finally {
  911. if (_didIteratorError) {
  912. throw _iteratorError;
  913. }
  914. }
  915. }
  916. flushSpaces();
  917. return Object.assign({}, ind, {
  918. value: value,
  919. length: length,
  920. queue: queue
  921. });
  922. function addTabs(count) {
  923. value += "\t".repeat(count);
  924. length += options.tabWidth * count;
  925. }
  926. function addSpaces(count) {
  927. value += " ".repeat(count);
  928. length += count;
  929. }
  930. function flush() {
  931. if (options.useTabs) {
  932. flushTabs();
  933. } else {
  934. flushSpaces();
  935. }
  936. }
  937. function flushTabs() {
  938. if (lastTabs > 0) {
  939. addTabs(lastTabs);
  940. }
  941. resetLast();
  942. }
  943. function flushSpaces() {
  944. if (lastSpaces > 0) {
  945. addSpaces(lastSpaces);
  946. }
  947. resetLast();
  948. }
  949. function resetLast() {
  950. lastTabs = 0;
  951. lastSpaces = 0;
  952. }
  953. }
  954. function trim$1(out) {
  955. if (out.length === 0) {
  956. return 0;
  957. }
  958. var trimCount = 0; // Trim whitespace at the end of line
  959. while (out.length > 0 && typeof out[out.length - 1] === "string" && out[out.length - 1].match(/^[ \t]*$/)) {
  960. trimCount += out.pop().length;
  961. }
  962. if (out.length && typeof out[out.length - 1] === "string") {
  963. var trimmed = out[out.length - 1].replace(/[ \t]*$/, "");
  964. trimCount += out[out.length - 1].length - trimmed.length;
  965. out[out.length - 1] = trimmed;
  966. }
  967. return trimCount;
  968. }
  969. function fits(next, restCommands, width, options, mustBeFlat) {
  970. var restIdx = restCommands.length;
  971. var cmds = [next]; // `out` is only used for width counting because `trim` requires to look
  972. // backwards for space characters.
  973. var out = [];
  974. while (width >= 0) {
  975. if (cmds.length === 0) {
  976. if (restIdx === 0) {
  977. return true;
  978. }
  979. cmds.push(restCommands[restIdx - 1]);
  980. restIdx--;
  981. continue;
  982. }
  983. var x = cmds.pop();
  984. var ind = x[0];
  985. var mode = x[1];
  986. var doc = x[2];
  987. if (typeof doc === "string") {
  988. out.push(doc);
  989. width -= getStringWidth(doc);
  990. } else {
  991. switch (doc.type) {
  992. case "concat":
  993. for (var i = doc.parts.length - 1; i >= 0; i--) {
  994. cmds.push([ind, mode, doc.parts[i]]);
  995. }
  996. break;
  997. case "indent":
  998. cmds.push([makeIndent(ind, options), mode, doc.contents]);
  999. break;
  1000. case "align":
  1001. cmds.push([makeAlign(ind, doc.n, options), mode, doc.contents]);
  1002. break;
  1003. case "trim":
  1004. width += trim$1(out);
  1005. break;
  1006. case "group":
  1007. if (mustBeFlat && doc.break) {
  1008. return false;
  1009. }
  1010. cmds.push([ind, doc.break ? MODE_BREAK : mode, doc.contents]);
  1011. if (doc.id) {
  1012. groupModeMap[doc.id] = cmds[cmds.length - 1][1];
  1013. }
  1014. break;
  1015. case "fill":
  1016. for (var _i = doc.parts.length - 1; _i >= 0; _i--) {
  1017. cmds.push([ind, mode, doc.parts[_i]]);
  1018. }
  1019. break;
  1020. case "if-break":
  1021. {
  1022. var groupMode = doc.groupId ? groupModeMap[doc.groupId] : mode;
  1023. if (groupMode === MODE_BREAK) {
  1024. if (doc.breakContents) {
  1025. cmds.push([ind, mode, doc.breakContents]);
  1026. }
  1027. }
  1028. if (groupMode === MODE_FLAT) {
  1029. if (doc.flatContents) {
  1030. cmds.push([ind, mode, doc.flatContents]);
  1031. }
  1032. }
  1033. break;
  1034. }
  1035. case "line":
  1036. switch (mode) {
  1037. // fallthrough
  1038. case MODE_FLAT:
  1039. if (!doc.hard) {
  1040. if (!doc.soft) {
  1041. out.push(" ");
  1042. width -= 1;
  1043. }
  1044. break;
  1045. }
  1046. return true;
  1047. case MODE_BREAK:
  1048. return true;
  1049. }
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. return false;
  1055. }
  1056. function printDocToString(doc, options) {
  1057. groupModeMap = {};
  1058. var width = options.printWidth;
  1059. var newLine = convertEndOfLineToChars(options.endOfLine);
  1060. var pos = 0; // cmds is basically a stack. We've turned a recursive call into a
  1061. // while loop which is much faster. The while loop below adds new
  1062. // cmds to the array instead of recursively calling `print`.
  1063. var cmds = [[rootIndent(), MODE_BREAK, doc]];
  1064. var out = [];
  1065. var shouldRemeasure = false;
  1066. var lineSuffix = [];
  1067. while (cmds.length !== 0) {
  1068. var x = cmds.pop();
  1069. var ind = x[0];
  1070. var mode = x[1];
  1071. var _doc = x[2];
  1072. if (typeof _doc === "string") {
  1073. out.push(_doc);
  1074. pos += getStringWidth(_doc);
  1075. } else {
  1076. switch (_doc.type) {
  1077. case "cursor":
  1078. out.push(cursor$1.placeholder);
  1079. break;
  1080. case "concat":
  1081. for (var i = _doc.parts.length - 1; i >= 0; i--) {
  1082. cmds.push([ind, mode, _doc.parts[i]]);
  1083. }
  1084. break;
  1085. case "indent":
  1086. cmds.push([makeIndent(ind, options), mode, _doc.contents]);
  1087. break;
  1088. case "align":
  1089. cmds.push([makeAlign(ind, _doc.n, options), mode, _doc.contents]);
  1090. break;
  1091. case "trim":
  1092. pos -= trim$1(out);
  1093. break;
  1094. case "group":
  1095. switch (mode) {
  1096. case MODE_FLAT:
  1097. if (!shouldRemeasure) {
  1098. cmds.push([ind, _doc.break ? MODE_BREAK : MODE_FLAT, _doc.contents]);
  1099. break;
  1100. }
  1101. // fallthrough
  1102. case MODE_BREAK:
  1103. {
  1104. shouldRemeasure = false;
  1105. var next = [ind, MODE_FLAT, _doc.contents];
  1106. var rem = width - pos;
  1107. if (!_doc.break && fits(next, cmds, rem, options)) {
  1108. cmds.push(next);
  1109. } else {
  1110. // Expanded states are a rare case where a document
  1111. // can manually provide multiple representations of
  1112. // itself. It provides an array of documents
  1113. // going from the least expanded (most flattened)
  1114. // representation first to the most expanded. If a
  1115. // group has these, we need to manually go through
  1116. // these states and find the first one that fits.
  1117. if (_doc.expandedStates) {
  1118. var mostExpanded = _doc.expandedStates[_doc.expandedStates.length - 1];
  1119. if (_doc.break) {
  1120. cmds.push([ind, MODE_BREAK, mostExpanded]);
  1121. break;
  1122. } else {
  1123. for (var _i2 = 1; _i2 < _doc.expandedStates.length + 1; _i2++) {
  1124. if (_i2 >= _doc.expandedStates.length) {
  1125. cmds.push([ind, MODE_BREAK, mostExpanded]);
  1126. break;
  1127. } else {
  1128. var state = _doc.expandedStates[_i2];
  1129. var cmd = [ind, MODE_FLAT, state];
  1130. if (fits(cmd, cmds, rem, options)) {
  1131. cmds.push(cmd);
  1132. break;
  1133. }
  1134. }
  1135. }
  1136. }
  1137. } else {
  1138. cmds.push([ind, MODE_BREAK, _doc.contents]);
  1139. }
  1140. }
  1141. break;
  1142. }
  1143. }
  1144. if (_doc.id) {
  1145. groupModeMap[_doc.id] = cmds[cmds.length - 1][1];
  1146. }
  1147. break;
  1148. // Fills each line with as much code as possible before moving to a new
  1149. // line with the same indentation.
  1150. //
  1151. // Expects doc.parts to be an array of alternating content and
  1152. // whitespace. The whitespace contains the linebreaks.
  1153. //
  1154. // For example:
  1155. // ["I", line, "love", line, "monkeys"]
  1156. // or
  1157. // [{ type: group, ... }, softline, { type: group, ... }]
  1158. //
  1159. // It uses this parts structure to handle three main layout cases:
  1160. // * The first two content items fit on the same line without
  1161. // breaking
  1162. // -> output the first content item and the whitespace "flat".
  1163. // * Only the first content item fits on the line without breaking
  1164. // -> output the first content item "flat" and the whitespace with
  1165. // "break".
  1166. // * Neither content item fits on the line without breaking
  1167. // -> output the first content item and the whitespace with "break".
  1168. case "fill":
  1169. {
  1170. var _rem = width - pos;
  1171. var parts = _doc.parts;
  1172. if (parts.length === 0) {
  1173. break;
  1174. }
  1175. var content = parts[0];
  1176. var contentFlatCmd = [ind, MODE_FLAT, content];
  1177. var contentBreakCmd = [ind, MODE_BREAK, content];
  1178. var contentFits = fits(contentFlatCmd, [], _rem, options, true);
  1179. if (parts.length === 1) {
  1180. if (contentFits) {
  1181. cmds.push(contentFlatCmd);
  1182. } else {
  1183. cmds.push(contentBreakCmd);
  1184. }
  1185. break;
  1186. }
  1187. var whitespace = parts[1];
  1188. var whitespaceFlatCmd = [ind, MODE_FLAT, whitespace];
  1189. var whitespaceBreakCmd = [ind, MODE_BREAK, whitespace];
  1190. if (parts.length === 2) {
  1191. if (contentFits) {
  1192. cmds.push(whitespaceFlatCmd);
  1193. cmds.push(contentFlatCmd);
  1194. } else {
  1195. cmds.push(whitespaceBreakCmd);
  1196. cmds.push(contentBreakCmd);
  1197. }
  1198. break;
  1199. } // At this point we've handled the first pair (context, separator)
  1200. // and will create a new fill doc for the rest of the content.
  1201. // Ideally we wouldn't mutate the array here but coping all the
  1202. // elements to a new array would make this algorithm quadratic,
  1203. // which is unusable for large arrays (e.g. large texts in JSX).
  1204. parts.splice(0, 2);
  1205. var remainingCmd = [ind, mode, fill$1(parts)];
  1206. var secondContent = parts[0];
  1207. var firstAndSecondContentFlatCmd = [ind, MODE_FLAT, concat$1([content, whitespace, secondContent])];
  1208. var firstAndSecondContentFits = fits(firstAndSecondContentFlatCmd, [], _rem, options, true);
  1209. if (firstAndSecondContentFits) {
  1210. cmds.push(remainingCmd);
  1211. cmds.push(whitespaceFlatCmd);
  1212. cmds.push(contentFlatCmd);
  1213. } else if (contentFits) {
  1214. cmds.push(remainingCmd);
  1215. cmds.push(whitespaceBreakCmd);
  1216. cmds.push(contentFlatCmd);
  1217. } else {
  1218. cmds.push(remainingCmd);
  1219. cmds.push(whitespaceBreakCmd);
  1220. cmds.push(contentBreakCmd);
  1221. }
  1222. break;
  1223. }
  1224. case "if-break":
  1225. {
  1226. var groupMode = _doc.groupId ? groupModeMap[_doc.groupId] : mode;
  1227. if (groupMode === MODE_BREAK) {
  1228. if (_doc.breakContents) {
  1229. cmds.push([ind, mode, _doc.breakContents]);
  1230. }
  1231. }
  1232. if (groupMode === MODE_FLAT) {
  1233. if (_doc.flatContents) {
  1234. cmds.push([ind, mode, _doc.flatContents]);
  1235. }
  1236. }
  1237. break;
  1238. }
  1239. case "line-suffix":
  1240. lineSuffix.push([ind, mode, _doc.contents]);
  1241. break;
  1242. case "line-suffix-boundary":
  1243. if (lineSuffix.length > 0) {
  1244. cmds.push([ind, mode, {
  1245. type: "line",
  1246. hard: true
  1247. }]);
  1248. }
  1249. break;
  1250. case "line":
  1251. switch (mode) {
  1252. case MODE_FLAT:
  1253. if (!_doc.hard) {
  1254. if (!_doc.soft) {
  1255. out.push(" ");
  1256. pos += 1;
  1257. }
  1258. break;
  1259. } else {
  1260. // This line was forced into the output even if we
  1261. // were in flattened mode, so we need to tell the next
  1262. // group that no matter what, it needs to remeasure
  1263. // because the previous measurement didn't accurately
  1264. // capture the entire expression (this is necessary
  1265. // for nested groups)
  1266. shouldRemeasure = true;
  1267. }
  1268. // fallthrough
  1269. case MODE_BREAK:
  1270. if (lineSuffix.length) {
  1271. cmds.push([ind, mode, _doc]);
  1272. [].push.apply(cmds, lineSuffix.reverse());
  1273. lineSuffix = [];
  1274. break;
  1275. }
  1276. if (_doc.literal) {
  1277. if (ind.root) {
  1278. out.push(newLine, ind.root.value);
  1279. pos = ind.root.length;
  1280. } else {
  1281. out.push(newLine);
  1282. pos = 0;
  1283. }
  1284. } else {
  1285. pos -= trim$1(out);
  1286. out.push(newLine + ind.value);
  1287. pos = ind.length;
  1288. }
  1289. break;
  1290. }
  1291. break;
  1292. default:
  1293. }
  1294. }
  1295. }
  1296. var cursorPlaceholderIndex = out.indexOf(cursor$1.placeholder);
  1297. if (cursorPlaceholderIndex !== -1) {
  1298. var otherCursorPlaceholderIndex = out.indexOf(cursor$1.placeholder, cursorPlaceholderIndex + 1);
  1299. var beforeCursor = out.slice(0, cursorPlaceholderIndex).join("");
  1300. var aroundCursor = out.slice(cursorPlaceholderIndex + 1, otherCursorPlaceholderIndex).join("");
  1301. var afterCursor = out.slice(otherCursorPlaceholderIndex + 1).join("");
  1302. return {
  1303. formatted: beforeCursor + aroundCursor + afterCursor,
  1304. cursorNodeStart: beforeCursor.length,
  1305. cursorNodeText: aroundCursor
  1306. };
  1307. }
  1308. return {
  1309. formatted: out.join("")
  1310. };
  1311. }
  1312. var docPrinter = {
  1313. printDocToString: printDocToString
  1314. };
  1315. var traverseDocOnExitStackMarker = {};
  1316. function traverseDoc(doc, onEnter, onExit, shouldTraverseConditionalGroups) {
  1317. var docsStack = [doc];
  1318. while (docsStack.length !== 0) {
  1319. var _doc = docsStack.pop();
  1320. if (_doc === traverseDocOnExitStackMarker) {
  1321. onExit(docsStack.pop());
  1322. continue;
  1323. }
  1324. var shouldRecurse = true;
  1325. if (onEnter) {
  1326. if (onEnter(_doc) === false) {
  1327. shouldRecurse = false;
  1328. }
  1329. }
  1330. if (onExit) {
  1331. docsStack.push(_doc);
  1332. docsStack.push(traverseDocOnExitStackMarker);
  1333. }
  1334. if (shouldRecurse) {
  1335. // When there are multiple parts to process,
  1336. // the parts need to be pushed onto the stack in reverse order,
  1337. // so that they are processed in the original order
  1338. // when the stack is popped.
  1339. if (_doc.type === "concat" || _doc.type === "fill") {
  1340. for (var ic = _doc.parts.length, i = ic - 1; i >= 0; --i) {
  1341. docsStack.push(_doc.parts[i]);
  1342. }
  1343. } else if (_doc.type === "if-break") {
  1344. if (_doc.flatContents) {
  1345. docsStack.push(_doc.flatContents);
  1346. }
  1347. if (_doc.breakContents) {
  1348. docsStack.push(_doc.breakContents);
  1349. }
  1350. } else if (_doc.type === "group" && _doc.expandedStates) {
  1351. if (shouldTraverseConditionalGroups) {
  1352. for (var _ic = _doc.expandedStates.length, _i = _ic - 1; _i >= 0; --_i) {
  1353. docsStack.push(_doc.expandedStates[_i]);
  1354. }
  1355. } else {
  1356. docsStack.push(_doc.contents);
  1357. }
  1358. } else if (_doc.contents) {
  1359. docsStack.push(_doc.contents);
  1360. }
  1361. }
  1362. }
  1363. }
  1364. function mapDoc(doc, cb) {
  1365. if (doc.type === "concat" || doc.type === "fill") {
  1366. var parts = doc.parts.map(function (part) {
  1367. return mapDoc(part, cb);
  1368. });
  1369. return cb(Object.assign({}, doc, {
  1370. parts: parts
  1371. }));
  1372. } else if (doc.type === "if-break") {
  1373. var breakContents = doc.breakContents && mapDoc(doc.breakContents, cb);
  1374. var flatContents = doc.flatContents && mapDoc(doc.flatContents, cb);
  1375. return cb(Object.assign({}, doc, {
  1376. breakContents: breakContents,
  1377. flatContents: flatContents
  1378. }));
  1379. } else if (doc.contents) {
  1380. var contents = mapDoc(doc.contents, cb);
  1381. return cb(Object.assign({}, doc, {
  1382. contents: contents
  1383. }));
  1384. }
  1385. return cb(doc);
  1386. }
  1387. function findInDoc(doc, fn, defaultValue) {
  1388. var result = defaultValue;
  1389. var hasStopped = false;
  1390. function findInDocOnEnterFn(doc) {
  1391. var maybeResult = fn(doc);
  1392. if (maybeResult !== undefined) {
  1393. hasStopped = true;
  1394. result = maybeResult;
  1395. }
  1396. if (hasStopped) {
  1397. return false;
  1398. }
  1399. }
  1400. traverseDoc(doc, findInDocOnEnterFn);
  1401. return result;
  1402. }
  1403. function isEmpty(n) {
  1404. return typeof n === "string" && n.length === 0;
  1405. }
  1406. function isLineNextFn(doc) {
  1407. if (typeof doc === "string") {
  1408. return false;
  1409. }
  1410. if (doc.type === "line") {
  1411. return true;
  1412. }
  1413. }
  1414. function isLineNext(doc) {
  1415. return findInDoc(doc, isLineNextFn, false);
  1416. }
  1417. function willBreakFn(doc) {
  1418. if (doc.type === "group" && doc.break) {
  1419. return true;
  1420. }
  1421. if (doc.type === "line" && doc.hard) {
  1422. return true;
  1423. }
  1424. if (doc.type === "break-parent") {
  1425. return true;
  1426. }
  1427. }
  1428. function willBreak(doc) {
  1429. return findInDoc(doc, willBreakFn, false);
  1430. }
  1431. function breakParentGroup(groupStack) {
  1432. if (groupStack.length > 0) {
  1433. var parentGroup = groupStack[groupStack.length - 1]; // Breaks are not propagated through conditional groups because
  1434. // the user is expected to manually handle what breaks.
  1435. if (!parentGroup.expandedStates) {
  1436. parentGroup.break = true;
  1437. }
  1438. }
  1439. return null;
  1440. }
  1441. function propagateBreaks(doc) {
  1442. var alreadyVisitedSet = new Set();
  1443. var groupStack = [];
  1444. function propagateBreaksOnEnterFn(doc) {
  1445. if (doc.type === "break-parent") {
  1446. breakParentGroup(groupStack);
  1447. }
  1448. if (doc.type === "group") {
  1449. groupStack.push(doc);
  1450. if (alreadyVisitedSet.has(doc)) {
  1451. return false;
  1452. }
  1453. alreadyVisitedSet.add(doc);
  1454. }
  1455. }
  1456. function propagateBreaksOnExitFn(doc) {
  1457. if (doc.type === "group") {
  1458. var group = groupStack.pop();
  1459. if (group.break) {
  1460. breakParentGroup(groupStack);
  1461. }
  1462. }
  1463. }
  1464. traverseDoc(doc, propagateBreaksOnEnterFn, propagateBreaksOnExitFn,
  1465. /* shouldTraverseConditionalGroups */
  1466. true);
  1467. }
  1468. function removeLinesFn(doc) {
  1469. // Force this doc into flat mode by statically converting all
  1470. // lines into spaces (or soft lines into nothing). Hard lines
  1471. // should still output because there's too great of a chance
  1472. // of breaking existing assumptions otherwise.
  1473. if (doc.type === "line" && !doc.hard) {
  1474. return doc.soft ? "" : " ";
  1475. } else if (doc.type === "if-break") {
  1476. return doc.flatContents || "";
  1477. }
  1478. return doc;
  1479. }
  1480. function removeLines(doc) {
  1481. return mapDoc(doc, removeLinesFn);
  1482. }
  1483. function stripTrailingHardline(doc) {
  1484. // HACK remove ending hardline, original PR: #1984
  1485. if (doc.type === "concat" && doc.parts.length !== 0) {
  1486. var lastPart = doc.parts[doc.parts.length - 1];
  1487. if (lastPart.type === "concat") {
  1488. if (lastPart.parts.length === 2 && lastPart.parts[0].hard && lastPart.parts[1].type === "break-parent") {
  1489. return {
  1490. type: "concat",
  1491. parts: doc.parts.slice(0, -1)
  1492. };
  1493. }
  1494. return {
  1495. type: "concat",
  1496. parts: doc.parts.slice(0, -1).concat(stripTrailingHardline(lastPart))
  1497. };
  1498. }
  1499. }
  1500. return doc;
  1501. }
  1502. var docUtils = {
  1503. isEmpty: isEmpty,
  1504. willBreak: willBreak,
  1505. isLineNext: isLineNext,
  1506. traverseDoc: traverseDoc,
  1507. findInDoc: findInDoc,
  1508. mapDoc: mapDoc,
  1509. propagateBreaks: propagateBreaks,
  1510. removeLines: removeLines,
  1511. stripTrailingHardline: stripTrailingHardline
  1512. };
  1513. function flattenDoc(doc) {
  1514. if (doc.type === "concat") {
  1515. var res = [];
  1516. for (var i = 0; i < doc.parts.length; ++i) {
  1517. var doc2 = doc.parts[i];
  1518. if (typeof doc2 !== "string" && doc2.type === "concat") {
  1519. [].push.apply(res, flattenDoc(doc2).parts);
  1520. } else {
  1521. var flattened = flattenDoc(doc2);
  1522. if (flattened !== "") {
  1523. res.push(flattened);
  1524. }
  1525. }
  1526. }
  1527. return Object.assign({}, doc, {
  1528. parts: res
  1529. });
  1530. } else if (doc.type === "if-break") {
  1531. return Object.assign({}, doc, {
  1532. breakContents: doc.breakContents != null ? flattenDoc(doc.breakContents) : null,
  1533. flatContents: doc.flatContents != null ? flattenDoc(doc.flatContents) : null
  1534. });
  1535. } else if (doc.type === "group") {
  1536. return Object.assign({}, doc, {
  1537. contents: flattenDoc(doc.contents),
  1538. expandedStates: doc.expandedStates ? doc.expandedStates.map(flattenDoc) : doc.expandedStates
  1539. });
  1540. } else if (doc.contents) {
  1541. return Object.assign({}, doc, {
  1542. contents: flattenDoc(doc.contents)
  1543. });
  1544. }
  1545. return doc;
  1546. }
  1547. function printDoc(doc) {
  1548. if (typeof doc === "string") {
  1549. return JSON.stringify(doc);
  1550. }
  1551. if (doc.type === "line") {
  1552. if (doc.literal) {
  1553. return "literalline";
  1554. }
  1555. if (doc.hard) {
  1556. return "hardline";
  1557. }
  1558. if (doc.soft) {
  1559. return "softline";
  1560. }
  1561. return "line";
  1562. }
  1563. if (doc.type === "break-parent") {
  1564. return "breakParent";
  1565. }
  1566. if (doc.type === "trim") {
  1567. return "trim";
  1568. }
  1569. if (doc.type === "concat") {
  1570. return "[" + doc.parts.map(printDoc).join(", ") + "]";
  1571. }
  1572. if (doc.type === "indent") {
  1573. return "indent(" + printDoc(doc.contents) + ")";
  1574. }
  1575. if (doc.type === "align") {
  1576. return doc.n === -Infinity ? "dedentToRoot(" + printDoc(doc.contents) + ")" : doc.n < 0 ? "dedent(" + printDoc(doc.contents) + ")" : doc.n.type === "root" ? "markAsRoot(" + printDoc(doc.contents) + ")" : "align(" + JSON.stringify(doc.n) + ", " + printDoc(doc.contents) + ")";
  1577. }
  1578. if (doc.type === "if-break") {
  1579. return "ifBreak(" + printDoc(doc.breakContents) + (doc.flatContents ? ", " + printDoc(doc.flatContents) : "") + ")";
  1580. }
  1581. if (doc.type === "group") {
  1582. if (doc.expandedStates) {
  1583. return "conditionalGroup(" + "[" + doc.expandedStates.map(printDoc).join(",") + "])";
  1584. }
  1585. return (doc.break ? "wrappedGroup" : "group") + "(" + printDoc(doc.contents) + ")";
  1586. }
  1587. if (doc.type === "fill") {
  1588. return "fill" + "(" + doc.parts.map(printDoc).join(", ") + ")";
  1589. }
  1590. if (doc.type === "line-suffix") {
  1591. return "lineSuffix(" + printDoc(doc.contents) + ")";
  1592. }
  1593. if (doc.type === "line-suffix-boundary") {
  1594. return "lineSuffixBoundary";
  1595. }
  1596. throw new Error("Unknown doc type " + doc.type);
  1597. }
  1598. var docDebug = {
  1599. printDocToDebug: function printDocToDebug(doc) {
  1600. return printDoc(flattenDoc(doc));
  1601. }
  1602. };
  1603. var doc = {
  1604. builders: docBuilders,
  1605. printer: docPrinter,
  1606. utils: docUtils,
  1607. debug: docDebug
  1608. };
  1609. return doc;
  1610. })));