1 (function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3 typeof define === 'function' && define.amd ? define(['exports'], factory) :
4 (global = global || self, factory(global.doc = {}));
5 }(this, (function (exports) { 'use strict';
13 function concat(parts) {
14 // access the internals of a document directly.
15 // if(parts.length === 1) {
16 // // If it's a single document, no need to concat it.
27 * @param {Doc} contents
32 function indent(contents) {
41 * @param {Doc} contents
46 function align(n, contents) {
55 * @param {Doc} contents
56 * @param {object} [opts] - TBD ???
61 function group(contents, opts) {
68 break: !!opts.shouldBreak,
69 expandedStates: opts.expandedStates
73 * @param {Doc} contents
78 function dedentToRoot(contents) {
79 return align(-Infinity, contents);
82 * @param {Doc} contents
87 function markAsRoot(contents) {
88 // @ts-ignore - TBD ???:
94 * @param {Doc} contents
99 function dedent(contents) {
100 return align(-1, contents);
103 * @param {Doc[]} states
104 * @param {object} [opts] - TBD ???
109 function conditionalGroup(states, opts) {
110 return group(states[0], Object.assign(opts || {}, {
111 expandedStates: states
115 * @param {Doc[]} parts
120 function fill(parts) {
128 * @param {Doc} [breakContents]
129 * @param {Doc} [flatContents]
130 * @param {object} [opts] - TBD ???
135 function ifBreak(breakContents, flatContents, opts) {
140 breakContents: breakContents,
141 flatContents: flatContents,
142 groupId: opts.groupId
146 * @param {Doc} contents
151 function lineSuffix(contents) {
159 var lineSuffixBoundary = {
160 type: "line-suffix-boundary"
175 var hardline = concat([{
179 var literalline = concat([{
186 placeholder: Symbol("cursor")
194 function join(sep, arr) {
197 for (var i = 0; i < arr.length; i++) {
209 * @param {number} size
210 * @param {number} tabWidth
214 function addAlignmentToDoc(doc, size, tabWidth) {
218 // Use indent to add tabs for all the levels of tabs we need
219 for (var i = 0; i < Math.floor(size / tabWidth); ++i) {
220 aligned = indent(aligned);
221 } // Use align for all the spaces that are needed
224 aligned = align(size % tabWidth, aligned); // size is absolute from 0 and not relative to the current
225 // indentation, so we use -Infinity to reset the indentation to 0
227 aligned = align(-Infinity, aligned);
239 literalline: literalline,
241 conditionalGroup: conditionalGroup,
243 lineSuffix: lineSuffix,
244 lineSuffixBoundary: lineSuffixBoundary,
246 breakParent: breakParent,
251 addAlignmentToDoc: addAlignmentToDoc,
252 markAsRoot: markAsRoot,
253 dedentToRoot: dedentToRoot,
257 var ansiRegex = function ansiRegex(options) {
258 options = Object.assign({
261 var pattern = ["[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)", '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'].join('|');
262 return new RegExp(pattern, options.onlyFirst ? undefined : 'g');
265 var stripAnsi = function stripAnsi(string) {
266 return typeof string === 'string' ? string.replace(ansiRegex(), '') : string;
269 var stripAnsi_1 = stripAnsi;
270 var default_1 = stripAnsi;
271 stripAnsi_1.default = default_1;
273 /* eslint-disable yoda */
275 var isFullwidthCodePoint = function isFullwidthCodePoint(codePoint) {
276 if (Number.isNaN(codePoint)) {
278 } // Code points are derived from:
279 // http://www.unix.org/Public/UNIDATA/EastAsianWidth.txt
282 if (codePoint >= 0x1100 && (codePoint <= 0x115F || // Hangul Jamo
283 codePoint === 0x2329 || // LEFT-POINTING ANGLE BRACKET
284 codePoint === 0x232A || // RIGHT-POINTING ANGLE BRACKET
285 // CJK Radicals Supplement .. Enclosed CJK Letters and Months
286 0x2E80 <= codePoint && codePoint <= 0x3247 && codePoint !== 0x303F || // Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A
287 0x3250 <= codePoint && codePoint <= 0x4DBF || // CJK Unified Ideographs .. Yi Radicals
288 0x4E00 <= codePoint && codePoint <= 0xA4C6 || // Hangul Jamo Extended-A
289 0xA960 <= codePoint && codePoint <= 0xA97C || // Hangul Syllables
290 0xAC00 <= codePoint && codePoint <= 0xD7A3 || // CJK Compatibility Ideographs
291 0xF900 <= codePoint && codePoint <= 0xFAFF || // Vertical Forms
292 0xFE10 <= codePoint && codePoint <= 0xFE19 || // CJK Compatibility Forms .. Small Form Variants
293 0xFE30 <= codePoint && codePoint <= 0xFE6B || // Halfwidth and Fullwidth Forms
294 0xFF01 <= codePoint && codePoint <= 0xFF60 || 0xFFE0 <= codePoint && codePoint <= 0xFFE6 || // Kana Supplement
295 0x1B000 <= codePoint && codePoint <= 0x1B001 || // Enclosed Ideographic Supplement
296 0x1F200 <= codePoint && codePoint <= 0x1F251 || // CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane
297 0x20000 <= codePoint && codePoint <= 0x3FFFD)) {
304 var isFullwidthCodePoint_1 = isFullwidthCodePoint;
305 var default_1$1 = isFullwidthCodePoint;
306 isFullwidthCodePoint_1.default = default_1$1;
308 var emojiRegex = function emojiRegex() {
309 // https://mths.be/emoji
310 return /\uD83C\uDFF4\uDB40\uDC67\uDB40\uDC62(?:\uDB40\uDC65\uDB40\uDC6E\uDB40\uDC67|\uDB40\uDC73\uDB40\uDC63\uDB40\uDC74|\uDB40\uDC77\uDB40\uDC6C\uDB40\uDC73)\uDB40\uDC7F|\uD83D\uDC68(?:\uD83C\uDFFC\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68\uD83C\uDFFB|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFF\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFB-\uDFFE])|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFE\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFB-\uDFFD])|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFD\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFB\uDFFC])|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\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])|(?:\uD83D[\uDC68\uDC69])\u200D(?:\uD83D[\uDC66\uDC67])|[\u2695\u2696\u2708]\uFE0F|\uD83D[\uDC66\uDC67]|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|(?:\uD83C\uDFFB\u200D[\u2695\u2696\u2708]|\uD83C\uDFFF\u200D[\u2695\u2696\u2708]|\uD83C\uDFFE\u200D[\u2695\u2696\u2708]|\uD83C\uDFFD\u200D[\u2695\u2696\u2708]|\uD83C\uDFFC\u200D[\u2695\u2696\u2708])\uFE0F|\uD83C\uDFFB\u200D(?:\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C[\uDFFB-\uDFFF])|(?:\uD83E\uDDD1\uD83C\uDFFB\u200D\uD83E\uDD1D\u200D\uD83E\uDDD1|\uD83D\uDC69\uD83C\uDFFC\u200D\uD83E\uDD1D\u200D\uD83D\uDC69)\uD83C\uDFFB|\uD83E\uDDD1(?:\uD83C\uDFFF\u200D\uD83E\uDD1D\u200D\uD83E\uDDD1(?:\uD83C[\uDFFB-\uDFFF])|\u200D\uD83E\uDD1D\u200D\uD83E\uDDD1)|(?:\uD83E\uDDD1\uD83C\uDFFE\u200D\uD83E\uDD1D\u200D\uD83E\uDDD1|\uD83D\uDC69\uD83C\uDFFF\u200D\uD83E\uDD1D\u200D(?:\uD83D[\uDC68\uDC69]))(?:\uD83C[\uDFFB-\uDFFE])|(?:\uD83E\uDDD1\uD83C\uDFFC\u200D\uD83E\uDD1D\u200D\uD83E\uDDD1|\uD83D\uDC69\uD83C\uDFFD\u200D\uD83E\uDD1D\u200D\uD83D\uDC69)(?:\uD83C[\uDFFB\uDFFC])|\uD83D\uDC69(?:\uD83C\uDFFE\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFB-\uDFFD\uDFFF])|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFC\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFB\uDFFD-\uDFFF])|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFB\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFC-\uDFFF])|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFD\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF])|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\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[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFF\u200D(?:\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD]))|\uD83D\uDC69\u200D\uD83D\uDC69\u200D(?:\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67]))|(?:\uD83E\uDDD1\uD83C\uDFFD\u200D\uD83E\uDD1D\u200D\uD83E\uDDD1|\uD83D\uDC69\uD83C\uDFFE\u200D\uD83E\uDD1D\u200D\uD83D\uDC69)(?:\uD83C[\uDFFB-\uDFFD])|\uD83D\uDC69\u200D\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC69\u200D\uD83D\uDC69\u200D(?:\uD83D[\uDC66\uDC67])|(?:\uD83D\uDC41\uFE0F\u200D\uD83D\uDDE8|\uD83D\uDC69(?:\uD83C\uDFFF\u200D[\u2695\u2696\u2708]|\uD83C\uDFFE\u200D[\u2695\u2696\u2708]|\uD83C\uDFFC\u200D[\u2695\u2696\u2708]|\uD83C\uDFFB\u200D[\u2695\u2696\u2708]|\uD83C\uDFFD\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\uDDCD-\uDDCF\uDDD6-\uDDDD])(?:(?:\uD83C[\uDFFB-\uDFFF])\u200D[\u2640\u2642]|\u200D[\u2640\u2642])|\uD83C\uDFF4\u200D\u2620)\uFE0F|\uD83D\uDC69\u200D\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67])|\uD83C\uDFF3\uFE0F\u200D\uD83C\uDF08|\uD83D\uDC15\u200D\uD83E\uDDBA|\uD83D\uDC69\u200D\uD83D\uDC66|\uD83D\uDC69\u200D\uD83D\uDC67|\uD83C\uDDFD\uD83C\uDDF0|\uD83C\uDDF4\uD83C\uDDF2|\uD83C\uDDF6\uD83C\uDDE6|[#\*0-9]\uFE0F\u20E3|\uD83C\uDDE7(?:\uD83C[\uDDE6\uDDE7\uDDE9-\uDDEF\uDDF1-\uDDF4\uDDF6-\uDDF9\uDDFB\uDDFC\uDDFE\uDDFF])|\uD83C\uDDF9(?:\uD83C[\uDDE6\uDDE8\uDDE9\uDDEB-\uDDED\uDDEF-\uDDF4\uDDF7\uDDF9\uDDFB\uDDFC\uDDFF])|\uD83C\uDDEA(?:\uD83C[\uDDE6\uDDE8\uDDEA\uDDEC\uDDED\uDDF7-\uDDFA])|\uD83E\uDDD1(?:\uD83C[\uDFFB-\uDFFF])|\uD83C\uDDF7(?:\uD83C[\uDDEA\uDDF4\uDDF8\uDDFA\uDDFC])|\uD83D\uDC69(?:\uD83C[\uDFFB-\uDFFF])|\uD83C\uDDF2(?:\uD83C[\uDDE6\uDDE8-\uDDED\uDDF0-\uDDFF])|\uD83C\uDDE6(?:\uD83C[\uDDE8-\uDDEC\uDDEE\uDDF1\uDDF2\uDDF4\uDDF6-\uDDFA\uDDFC\uDDFD\uDDFF])|\uD83C\uDDF0(?:\uD83C[\uDDEA\uDDEC-\uDDEE\uDDF2\uDDF3\uDDF5\uDDF7\uDDFC\uDDFE\uDDFF])|\uD83C\uDDED(?:\uD83C[\uDDF0\uDDF2\uDDF3\uDDF7\uDDF9\uDDFA])|\uD83C\uDDE9(?:\uD83C[\uDDEA\uDDEC\uDDEF\uDDF0\uDDF2\uDDF4\uDDFF])|\uD83C\uDDFE(?:\uD83C[\uDDEA\uDDF9])|\uD83C\uDDEC(?:\uD83C[\uDDE6\uDDE7\uDDE9-\uDDEE\uDDF1-\uDDF3\uDDF5-\uDDFA\uDDFC\uDDFE])|\uD83C\uDDF8(?:\uD83C[\uDDE6-\uDDEA\uDDEC-\uDDF4\uDDF7-\uDDF9\uDDFB\uDDFD-\uDDFF])|\uD83C\uDDEB(?:\uD83C[\uDDEE-\uDDF0\uDDF2\uDDF4\uDDF7])|\uD83C\uDDF5(?:\uD83C[\uDDE6\uDDEA-\uDDED\uDDF0-\uDDF3\uDDF7-\uDDF9\uDDFC\uDDFE])|\uD83C\uDDFB(?:\uD83C[\uDDE6\uDDE8\uDDEA\uDDEC\uDDEE\uDDF3\uDDFA])|\uD83C\uDDF3(?:\uD83C[\uDDE6\uDDE8\uDDEA-\uDDEC\uDDEE\uDDF1\uDDF4\uDDF5\uDDF7\uDDFA\uDDFF])|\uD83C\uDDE8(?:\uD83C[\uDDE6\uDDE8\uDDE9\uDDEB-\uDDEE\uDDF0-\uDDF5\uDDF7\uDDFA-\uDDFF])|\uD83C\uDDF1(?:\uD83C[\uDDE6-\uDDE8\uDDEE\uDDF0\uDDF7-\uDDFB\uDDFE])|\uD83C\uDDFF(?:\uD83C[\uDDE6\uDDF2\uDDFC])|\uD83C\uDDFC(?:\uD83C[\uDDEB\uDDF8])|\uD83C\uDDFA(?:\uD83C[\uDDE6\uDDEC\uDDF2\uDDF3\uDDF8\uDDFE\uDDFF])|\uD83C\uDDEE(?:\uD83C[\uDDE8-\uDDEA\uDDF1-\uDDF4\uDDF6-\uDDF9])|\uD83C\uDDEF(?:\uD83C[\uDDEA\uDDF2\uDDF4\uDDF5])|(?:\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\uDDCD-\uDDCF\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\uDC6B-\uDC6D\uDC70\uDC72\uDC74-\uDC76\uDC78\uDC7C\uDC83\uDC85\uDCAA\uDD74\uDD7A\uDD90\uDD95\uDD96\uDE4C\uDE4F\uDEC0\uDECC]|\uD83E[\uDD0F\uDD18-\uDD1C\uDD1E\uDD1F\uDD30-\uDD36\uDDB5\uDDB6\uDDBB\uDDD2-\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\uDED5\uDEEB\uDEEC\uDEF4-\uDEFA\uDFE0-\uDFEB]|\uD83E[\uDD0D-\uDD3A\uDD3C-\uDD45\uDD47-\uDD71\uDD73-\uDD76\uDD7A-\uDDA2\uDDA5-\uDDAA\uDDAE-\uDDCA\uDDCD-\uDDFF\uDE70-\uDE73\uDE78-\uDE7A\uDE80-\uDE82\uDE90-\uDE95])|(?:[#\*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\uDED5\uDEE0-\uDEE5\uDEE9\uDEEB\uDEEC\uDEF0\uDEF3-\uDEFA\uDFE0-\uDFEB]|\uD83E[\uDD0D-\uDD3A\uDD3C-\uDD45\uDD47-\uDD71\uDD73-\uDD76\uDD7A-\uDDA2\uDDA5-\uDDAA\uDDAE-\uDDCA\uDDCD-\uDDFF\uDE70-\uDE73\uDE78-\uDE7A\uDE80-\uDE82\uDE90-\uDE95])\uFE0F|(?:[\u261D\u26F9\u270A-\u270D]|\uD83C[\uDF85\uDFC2-\uDFC4\uDFC7\uDFCA-\uDFCC]|\uD83D[\uDC42\uDC43\uDC46-\uDC50\uDC66-\uDC78\uDC7C\uDC81-\uDC83\uDC85-\uDC87\uDC8F\uDC91\uDCAA\uDD74\uDD75\uDD7A\uDD90\uDD95\uDD96\uDE45-\uDE47\uDE4B-\uDE4F\uDEA3\uDEB4-\uDEB6\uDEC0\uDECC]|\uD83E[\uDD0F\uDD18-\uDD1F\uDD26\uDD30-\uDD39\uDD3C-\uDD3E\uDDB5\uDDB6\uDDB8\uDDB9\uDDBB\uDDCD-\uDDCF\uDDD1-\uDDDD])/g;
313 var stringWidth = function stringWidth(string) {
314 string = string.replace(emojiRegex(), ' ');
316 if (typeof string !== 'string' || string.length === 0) {
320 string = stripAnsi_1(string);
323 for (var i = 0; i < string.length; i++) {
324 var code = string.codePointAt(i); // Ignore control characters
326 if (code <= 0x1F || code >= 0x7F && code <= 0x9F) {
328 } // Ignore combining characters
331 if (code >= 0x300 && code <= 0x36F) {
340 width += isFullwidthCodePoint_1(code) ? 2 : 1;
346 var stringWidth_1 = stringWidth; // TODO: remove this in the next major version
348 var default_1$2 = stringWidth;
349 stringWidth_1.default = default_1$2;
351 var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
353 var escapeStringRegexp = function escapeStringRegexp(str) {
354 if (typeof str !== 'string') {
355 throw new TypeError('Expected a string');
358 return str.replace(matchOperatorsRe, '\\$&');
361 var getLast = function getLast(arr) {
362 return arr.length > 0 ? arr[arr.length - 1] : null;
365 var notAsciiRegex = /[^\x20-\x7F]/;
367 function isExportDeclaration(node) {
370 case "ExportDefaultDeclaration":
371 case "ExportDefaultSpecifier":
372 case "DeclareExportDeclaration":
373 case "ExportNamedDeclaration":
374 case "ExportAllDeclaration":
382 function getParentExportDeclaration(path) {
383 var parentNode = path.getParentNode();
385 if (path.getName() === "declaration" && isExportDeclaration(parentNode)) {
392 function getPenultimate(arr) {
393 if (arr.length > 1) {
394 return arr[arr.length - 2];
400 * @typedef {{backwards?: boolean}} SkipOptions
404 * @param {string | RegExp} chars
405 * @returns {(text: string, index: number | false, opts?: SkipOptions) => number | false}
409 function skip(chars) {
410 return function (text, index, opts) {
411 var backwards = opts && opts.backwards; // Allow `skip` functions to be threaded together without having
412 // to check for failures (did someone say monads?).
414 if (index === false) {
418 var length = text.length;
421 while (cursor >= 0 && cursor < length) {
422 var c = text.charAt(cursor);
424 if (chars instanceof RegExp) {
425 if (!chars.test(c)) {
428 } else if (chars.indexOf(c) === -1) {
432 backwards ? cursor-- : cursor++;
435 if (cursor === -1 || cursor === length) {
436 // If we reached the beginning or end of the file, return the
437 // out-of-bounds cursor. It's up to the caller to handle this
438 // correctly. We don't want to indicate `false` though if it
439 // actually skipped valid characters.
447 * @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
451 var skipWhitespace = skip(/\s/);
453 * @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
456 var skipSpaces = skip(" \t");
458 * @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
461 var skipToLineEnd = skip(",; \t");
463 * @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
466 var skipEverythingButNewLine = skip(/[^\r\n]/);
468 * @param {string} text
469 * @param {number | false} index
470 * @returns {number | false}
473 function skipInlineComment(text, index) {
474 if (index === false) {
478 if (text.charAt(index) === "/" && text.charAt(index + 1) === "*") {
479 for (var i = index + 2; i < text.length; ++i) {
480 if (text.charAt(i) === "*" && text.charAt(i + 1) === "/") {
489 * @param {string} text
490 * @param {number | false} index
491 * @returns {number | false}
495 function skipTrailingComment(text, index) {
496 if (index === false) {
500 if (text.charAt(index) === "/" && text.charAt(index + 1) === "/") {
501 return skipEverythingButNewLine(text, index);
505 } // This one doesn't use the above helper function because it wants to
506 // test \r\n in order and `skip` doesn't support ordering and we only
507 // want to skip one newline. It's simple to implement.
510 * @param {string} text
511 * @param {number | false} index
512 * @param {SkipOptions=} opts
513 * @returns {number | false}
517 function skipNewline(text, index, opts) {
518 var backwards = opts && opts.backwards;
520 if (index === false) {
524 var atIndex = text.charAt(index);
527 if (text.charAt(index - 1) === "\r" && atIndex === "\n") {
531 if (atIndex === "\n" || atIndex === "\r" || atIndex === "\u2028" || atIndex === "\u2029") {
535 if (atIndex === "\r" && text.charAt(index + 1) === "\n") {
539 if (atIndex === "\n" || atIndex === "\r" || atIndex === "\u2028" || atIndex === "\u2029") {
547 * @param {string} text
548 * @param {number} index
549 * @param {SkipOptions=} opts
554 function hasNewline(text, index, opts) {
556 var idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
557 var idx2 = skipNewline(text, idx, opts);
561 * @param {string} text
562 * @param {number} start
563 * @param {number} end
568 function hasNewlineInRange(text, start, end) {
569 for (var i = start; i < end; ++i) {
570 if (text.charAt(i) === "\n") {
576 } // Note: this function doesn't ignore leading comments unlike isNextLineEmpty
580 * @param {string} text
582 * @param {(node: N) => number} locStart
586 function isPreviousLineEmpty(text, node, locStart) {
587 /** @type {number | false} */
588 var idx = locStart(node) - 1;
589 idx = skipSpaces(text, idx, {
592 idx = skipNewline(text, idx, {
595 idx = skipSpaces(text, idx, {
598 var idx2 = skipNewline(text, idx, {
604 * @param {string} text
605 * @param {number} index
610 function isNextLineEmptyAfterIndex(text, index) {
611 /** @type {number | false} */
613 /** @type {number | false} */
617 while (idx !== oldIdx) {
618 // We need to skip all the potential trailing inline comments
620 idx = skipToLineEnd(text, idx);
621 idx = skipInlineComment(text, idx);
622 idx = skipSpaces(text, idx);
625 idx = skipTrailingComment(text, idx);
626 idx = skipNewline(text, idx);
627 return idx !== false && hasNewline(text, idx);
631 * @param {string} text
633 * @param {(node: N) => number} locEnd
638 function isNextLineEmpty(text, node, locEnd) {
639 return isNextLineEmptyAfterIndex(text, locEnd(node));
642 * @param {string} text
643 * @param {number} idx
644 * @returns {number | false}
648 function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, idx) {
649 /** @type {number | false} */
651 /** @type {number | false} */
655 while (nextIdx !== oldIdx) {
657 nextIdx = skipSpaces(text, nextIdx);
658 nextIdx = skipInlineComment(text, nextIdx);
659 nextIdx = skipTrailingComment(text, nextIdx);
660 nextIdx = skipNewline(text, nextIdx);
667 * @param {string} text
669 * @param {(node: N) => number} locEnd
670 * @returns {number | false}
674 function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
675 return getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, locEnd(node));
679 * @param {string} text
681 * @param {(node: N) => number} locEnd
686 function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
687 return text.charAt( // @ts-ignore => TBD: can return false, should we define a fallback?
688 getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd));
691 * @param {string} text
692 * @param {number} index
693 * @param {SkipOptions=} opts
698 function hasSpaces(text, index, opts) {
700 var idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
701 return idx !== index;
704 * @param {{range?: [number, number], start?: number}} node
705 * @param {number} index
709 function setLocStart(node, index) {
711 node.range[0] = index;
717 * @param {{range?: [number, number], end?: number}} node
718 * @param {number} index
722 function setLocEnd(node, index) {
724 node.range[1] = index;
731 [["|>"], ["??"], ["||"], ["&&"], ["|"], ["^"], ["&"], ["==", "===", "!=", "!=="], ["<", ">", "<=", ">=", "in", "instanceof"], [">>", "<<", ">>>"], ["+", "-"], ["*", "/", "%"], ["**"]].forEach(function (tier, i) {
732 tier.forEach(function (op) {
737 function getPrecedence(op) {
738 return PRECEDENCE[op];
741 var equalityOperators = {
747 var multiplicativeOperators = {
752 var bitshiftOperators = {
758 function shouldFlatten(parentOp, nodeOp) {
759 if (getPrecedence(nodeOp) !== getPrecedence(parentOp)) {
761 } // ** is right-associative
762 // x ** y ** z --> x ** (y ** z)
765 if (parentOp === "**") {
767 } // x == y == z --> (x == y) == z
770 if (equalityOperators[parentOp] && equalityOperators[nodeOp]) {
772 } // x * y % z --> (x * y) % z
775 if (nodeOp === "%" && multiplicativeOperators[parentOp] || parentOp === "%" && multiplicativeOperators[nodeOp]) {
777 } // x * y / z --> (x * y) / z
778 // x / y * z --> (x / y) * z
781 if (nodeOp !== parentOp && multiplicativeOperators[nodeOp] && multiplicativeOperators[parentOp]) {
783 } // x << y << z --> (x << y) << z
786 if (bitshiftOperators[parentOp] && bitshiftOperators[nodeOp]) {
793 function isBitwiseOperator(operator) {
794 return !!bitshiftOperators[operator] || operator === "|" || operator === "^" || operator === "&";
795 } // Tests if an expression starts with `{`, or (if forbidFunctionClassAndDoExpr
796 // holds) `function`, `class`, or `do {}`. Will be overzealous if there's
797 // already necessary grouping parentheses.
800 function startsWithNoLookaheadToken(node, forbidFunctionClassAndDoExpr) {
801 node = getLeftMost(node);
804 case "FunctionExpression":
805 case "ClassExpression":
807 return forbidFunctionClassAndDoExpr;
809 case "ObjectExpression":
812 case "MemberExpression":
813 case "OptionalMemberExpression":
814 return startsWithNoLookaheadToken(node.object, forbidFunctionClassAndDoExpr);
816 case "TaggedTemplateExpression":
817 if (node.tag.type === "FunctionExpression") {
818 // IIFEs are always already parenthesized
822 return startsWithNoLookaheadToken(node.tag, forbidFunctionClassAndDoExpr);
824 case "CallExpression":
825 case "OptionalCallExpression":
826 if (node.callee.type === "FunctionExpression") {
827 // IIFEs are always already parenthesized
831 return startsWithNoLookaheadToken(node.callee, forbidFunctionClassAndDoExpr);
833 case "ConditionalExpression":
834 return startsWithNoLookaheadToken(node.test, forbidFunctionClassAndDoExpr);
836 case "UpdateExpression":
837 return !node.prefix && startsWithNoLookaheadToken(node.argument, forbidFunctionClassAndDoExpr);
839 case "BindExpression":
840 return node.object && startsWithNoLookaheadToken(node.object, forbidFunctionClassAndDoExpr);
842 case "SequenceExpression":
843 return startsWithNoLookaheadToken(node.expressions[0], forbidFunctionClassAndDoExpr);
845 case "TSAsExpression":
846 return startsWithNoLookaheadToken(node.expression, forbidFunctionClassAndDoExpr);
853 function getLeftMost(node) {
855 return getLeftMost(node.left);
861 * @param {string} value
862 * @param {number} tabWidth
863 * @param {number=} startIndex
868 function getAlignmentSize(value, tabWidth, startIndex) {
869 startIndex = startIndex || 0;
872 for (var i = startIndex; i < value.length; ++i) {
873 if (value[i] === "\t") {
874 // Tabs behave in a way that they are aligned to the nearest
875 // multiple of tabWidth:
876 // 0 -> 4, 1 -> 4, 2 -> 4, 3 -> 4
877 // 4 -> 8, 5 -> 8, 6 -> 8, 7 -> 8 ...
878 size = size + tabWidth - size % tabWidth;
887 * @param {string} value
888 * @param {number} tabWidth
893 function getIndentSize(value, tabWidth) {
894 var lastNewlineIndex = value.lastIndexOf("\n");
896 if (lastNewlineIndex === -1) {
900 return getAlignmentSize( // All the leading whitespaces
901 value.slice(lastNewlineIndex + 1).match(/^[ \t]*/)[0], tabWidth);
904 * @typedef {'"' | "'"} Quote
909 * @param {string} raw
910 * @param {Quote} preferredQuote
915 function getPreferredQuote(raw, preferredQuote) {
916 // `rawContent` is the string exactly like it appeared in the input source
917 // code, without its enclosing quotes.
918 var rawContent = raw.slice(1, -1);
919 /** @type {{ quote: '"', regex: RegExp }} */
925 /** @type {{ quote: "'", regex: RegExp }} */
931 var preferred = preferredQuote === "'" ? single : double;
932 var alternate = preferred === single ? double : single;
933 var result = preferred.quote; // If `rawContent` contains at least one of the quote preferred for enclosing
934 // the string, we might want to enclose with the alternate quote instead, to
935 // minimize the number of escaped quotes.
937 if (rawContent.includes(preferred.quote) || rawContent.includes(alternate.quote)) {
938 var numPreferredQuotes = (rawContent.match(preferred.regex) || []).length;
939 var numAlternateQuotes = (rawContent.match(alternate.regex) || []).length;
940 result = numPreferredQuotes > numAlternateQuotes ? alternate.quote : preferred.quote;
946 function printString(raw, options, isDirectiveLiteral) {
947 // `rawContent` is the string exactly like it appeared in the input source
948 // code, without its enclosing quotes.
949 var rawContent = raw.slice(1, -1); // Check for the alternate quote, to determine if we're allowed to swap
950 // the quotes on a DirectiveLiteral.
952 var canChangeDirectiveQuotes = !rawContent.includes('"') && !rawContent.includes("'");
955 var enclosingQuote = options.parser === "json" ? '"' : options.__isInHtmlAttribute ? "'" : getPreferredQuote(raw, options.singleQuote ? "'" : '"'); // Directives are exact code unit sequences, which means that you can't
956 // change the escape sequences they use.
957 // See https://github.com/prettier/prettier/issues/1555
958 // and https://tc39.github.io/ecma262/#directive-prologue
960 if (isDirectiveLiteral) {
961 if (canChangeDirectiveQuotes) {
962 return enclosingQuote + rawContent + enclosingQuote;
966 } // It might sound unnecessary to use `makeString` even if the string already
967 // is enclosed with `enclosingQuote`, but it isn't. The string could contain
968 // unnecessary escapes (such as in `"\'"`). Always using `makeString` makes
969 // sure that we consistently output the minimum amount of escaped quotes.
972 return makeString(rawContent, enclosingQuote, !(options.parser === "css" || options.parser === "less" || options.parser === "scss" || options.embeddedInHtml));
975 * @param {string} rawContent
976 * @param {Quote} enclosingQuote
977 * @param {boolean=} unescapeUnnecessaryEscapes
982 function makeString(rawContent, enclosingQuote, unescapeUnnecessaryEscapes) {
983 var otherQuote = enclosingQuote === '"' ? "'" : '"'; // Matches _any_ escape and unescaped quotes (both single and double).
985 var regex = /\\([\s\S])|(['"])/g; // Escape and unescape single and double quotes as needed to be able to
986 // enclose `rawContent` with `enclosingQuote`.
988 var newContent = rawContent.replace(regex, function (match, escaped, quote) {
989 // If we matched an escape, and the escaped character is a quote of the
990 // other type than we intend to enclose the string with, there's no need for
991 // it to be escaped, so return it _without_ the backslash.
992 if (escaped === otherQuote) {
994 } // If we matched an unescaped quote and it is of the _same_ type as we
995 // intend to enclose the string with, it must be escaped, so return it with
999 if (quote === enclosingQuote) {
1000 return "\\" + quote;
1005 } // Unescape any unnecessarily escaped character.
1006 // Adapted from https://github.com/eslint/eslint/blob/de0b4ad7bd820ade41b1f606008bea68683dc11a/lib/rules/no-useless-escape.js#L27
1009 return unescapeUnnecessaryEscapes && /^[^\\nrvtbfux\r\n\u2028\u2029"'0-7]$/.test(escaped) ? escaped : "\\" + escaped;
1011 return enclosingQuote + newContent + enclosingQuote;
1014 function printNumber(rawNumber) {
1015 return rawNumber.toLowerCase() // Remove unnecessary plus and zeroes from scientific notation.
1016 .replace(/^([+-]?[\d.]+e)(?:\+|(-))?0*(\d)/, "$1$2$3") // Remove unnecessary scientific notation (1e0).
1017 .replace(/^([+-]?[\d.]+)e[+-]?0+$/, "$1") // Make sure numbers always start with a digit.
1018 .replace(/^([+-])?\./, "$10.") // Remove extraneous trailing decimal zeroes.
1019 .replace(/(\.\d+?)0+(?=e|$)/, "$1") // Remove trailing dot.
1020 .replace(/\.(?=e|$)/, "");
1023 * @param {string} str
1024 * @param {string} target
1029 function getMaxContinuousCount(str, target) {
1030 var results = str.match(new RegExp("(".concat(escapeStringRegexp(target), ")+"), "g"));
1032 if (results === null) {
1036 return results.reduce(function (maxCount, result) {
1037 return Math.max(maxCount, result.length / target.length);
1041 function getMinNotPresentContinuousCount(str, target) {
1042 var matches = str.match(new RegExp("(".concat(escapeStringRegexp(target), ")+"), "g"));
1044 if (matches === null) {
1048 var countPresent = new Map();
1050 var _iteratorNormalCompletion = true;
1051 var _didIteratorError = false;
1052 var _iteratorError = undefined;
1055 for (var _iterator = matches[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
1056 var match = _step.value;
1057 var count = match.length / target.length;
1058 countPresent.set(count, true);
1065 _didIteratorError = true;
1066 _iteratorError = err;
1069 if (!_iteratorNormalCompletion && _iterator.return != null) {
1073 if (_didIteratorError) {
1074 throw _iteratorError;
1079 for (var i = 1; i < max; i++) {
1080 if (!countPresent.get(i)) {
1088 * @param {string} text
1093 function getStringWidth(text) {
1096 } // shortcut to avoid needless string `RegExp`s, replacements, and allocations within `string-width`
1099 if (!notAsciiRegex.test(text)) {
1103 return stringWidth_1(text);
1106 function hasIgnoreComment(path) {
1107 var node = path.getValue();
1108 return hasNodeIgnoreComment(node);
1111 function hasNodeIgnoreComment(node) {
1112 return node && node.comments && node.comments.length > 0 && node.comments.some(function (comment) {
1113 return comment.value.trim() === "prettier-ignore";
1117 function matchAncestorTypes(path, types, index) {
1119 types = types.slice();
1121 while (types.length) {
1122 var parent = path.getParentNode(index);
1123 var type = types.shift();
1125 if (!parent || parent.type !== type) {
1135 function addCommentHelper(node, comment) {
1136 var comments = node.comments || (node.comments = []);
1137 comments.push(comment);
1138 comment.printed = false; // For some reason, TypeScript parses `// x` inside of JSXText as a comment
1139 // We already "print" it via the raw text, we don't need to re-print it as a
1142 if (node.type === "JSXText") {
1143 comment.printed = true;
1147 function addLeadingComment(node, comment) {
1148 comment.leading = true;
1149 comment.trailing = false;
1150 addCommentHelper(node, comment);
1153 function addDanglingComment(node, comment) {
1154 comment.leading = false;
1155 comment.trailing = false;
1156 addCommentHelper(node, comment);
1159 function addTrailingComment(node, comment) {
1160 comment.leading = false;
1161 comment.trailing = true;
1162 addCommentHelper(node, comment);
1165 function isWithinParentArrayProperty(path, propertyName) {
1166 var node = path.getValue();
1167 var parent = path.getParentNode();
1169 if (parent == null) {
1173 if (!Array.isArray(parent[propertyName])) {
1177 var key = path.getName();
1178 return parent[propertyName][key] === node;
1181 function replaceEndOfLineWith(text, replacement) {
1183 var _iteratorNormalCompletion2 = true;
1184 var _didIteratorError2 = false;
1185 var _iteratorError2 = undefined;
1188 for (var _iterator2 = text.split("\n")[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
1189 var part = _step2.value;
1191 if (parts.length !== 0) {
1192 parts.push(replacement);
1198 _didIteratorError2 = true;
1199 _iteratorError2 = err;
1202 if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
1203 _iterator2.return();
1206 if (_didIteratorError2) {
1207 throw _iteratorError2;
1216 replaceEndOfLineWith: replaceEndOfLineWith,
1217 getStringWidth: getStringWidth,
1218 getMaxContinuousCount: getMaxContinuousCount,
1219 getMinNotPresentContinuousCount: getMinNotPresentContinuousCount,
1220 getPrecedence: getPrecedence,
1221 shouldFlatten: shouldFlatten,
1222 isBitwiseOperator: isBitwiseOperator,
1223 isExportDeclaration: isExportDeclaration,
1224 getParentExportDeclaration: getParentExportDeclaration,
1225 getPenultimate: getPenultimate,
1227 getNextNonSpaceNonCommentCharacterIndexWithStartIndex: getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
1228 getNextNonSpaceNonCommentCharacterIndex: getNextNonSpaceNonCommentCharacterIndex,
1229 getNextNonSpaceNonCommentCharacter: getNextNonSpaceNonCommentCharacter,
1231 skipWhitespace: skipWhitespace,
1232 skipSpaces: skipSpaces,
1233 skipToLineEnd: skipToLineEnd,
1234 skipEverythingButNewLine: skipEverythingButNewLine,
1235 skipInlineComment: skipInlineComment,
1236 skipTrailingComment: skipTrailingComment,
1237 skipNewline: skipNewline,
1238 isNextLineEmptyAfterIndex: isNextLineEmptyAfterIndex,
1239 isNextLineEmpty: isNextLineEmpty,
1240 isPreviousLineEmpty: isPreviousLineEmpty,
1241 hasNewline: hasNewline,
1242 hasNewlineInRange: hasNewlineInRange,
1243 hasSpaces: hasSpaces,
1244 setLocStart: setLocStart,
1245 setLocEnd: setLocEnd,
1246 startsWithNoLookaheadToken: startsWithNoLookaheadToken,
1247 getAlignmentSize: getAlignmentSize,
1248 getIndentSize: getIndentSize,
1249 getPreferredQuote: getPreferredQuote,
1250 printString: printString,
1251 printNumber: printNumber,
1252 hasIgnoreComment: hasIgnoreComment,
1253 hasNodeIgnoreComment: hasNodeIgnoreComment,
1254 makeString: makeString,
1255 matchAncestorTypes: matchAncestorTypes,
1256 addLeadingComment: addLeadingComment,
1257 addDanglingComment: addDanglingComment,
1258 addTrailingComment: addTrailingComment,
1259 isWithinParentArrayProperty: isWithinParentArrayProperty
1262 function guessEndOfLine(text) {
1263 var index = text.indexOf("\r");
1266 return text.charAt(index + 1) === "\n" ? "crlf" : "cr";
1272 function convertEndOfLineToChars(value) {
1286 guessEndOfLine: guessEndOfLine,
1287 convertEndOfLineToChars: convertEndOfLineToChars
1290 var getStringWidth$1 = util.getStringWidth;
1291 var convertEndOfLineToChars$1 = endOfLine.convertEndOfLineToChars;
1292 var concat$1 = docBuilders.concat,
1293 fill$1 = docBuilders.fill,
1294 cursor$1 = docBuilders.cursor;
1295 /** @type {Record<symbol, typeof MODE_BREAK | typeof MODE_FLAT>} */
1301 function rootIndent() {
1309 function makeIndent(ind, options) {
1310 return generateInd(ind, {
1315 function makeAlign(ind, n, options) {
1316 return n === -Infinity ? ind.root || rootIndent() : n < 0 ? generateInd(ind, {
1318 }, options) : !n ? ind : n.type === "root" ? Object.assign({}, ind, {
1320 }) : typeof n === "string" ? generateInd(ind, {
1321 type: "stringAlign",
1323 }, options) : generateInd(ind, {
1324 type: "numberAlign",
1329 function generateInd(ind, newPart, options) {
1330 var queue = newPart.type === "dedent" ? ind.queue.slice(0, -1) : ind.queue.concat(newPart);
1335 var _iteratorNormalCompletion = true;
1336 var _didIteratorError = false;
1337 var _iteratorError = undefined;
1340 for (var _iterator = queue[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
1341 var part = _step.value;
1343 switch (part.type) {
1347 if (options.useTabs) {
1350 addSpaces(options.tabWidth);
1358 length += part.n.length;
1363 lastSpaces += part.n;
1366 /* istanbul ignore next */
1369 throw new Error("Unexpected type '".concat(part.type, "'"));
1373 _didIteratorError = true;
1374 _iteratorError = err;
1377 if (!_iteratorNormalCompletion && _iterator.return != null) {
1381 if (_didIteratorError) {
1382 throw _iteratorError;
1388 return Object.assign({}, ind, {
1394 function addTabs(count) {
1395 value += "\t".repeat(count);
1396 length += options.tabWidth * count;
1399 function addSpaces(count) {
1400 value += " ".repeat(count);
1405 if (options.useTabs) {
1412 function flushTabs() {
1420 function flushSpaces() {
1421 if (lastSpaces > 0) {
1422 addSpaces(lastSpaces);
1428 function resetLast() {
1434 function trim$1(out) {
1435 if (out.length === 0) {
1439 var trimCount = 0; // Trim whitespace at the end of line
1441 while (out.length > 0 && typeof out[out.length - 1] === "string" && out[out.length - 1].match(/^[ \t]*$/)) {
1442 trimCount += out.pop().length;
1445 if (out.length && typeof out[out.length - 1] === "string") {
1446 var trimmed = out[out.length - 1].replace(/[ \t]*$/, "");
1447 trimCount += out[out.length - 1].length - trimmed.length;
1448 out[out.length - 1] = trimmed;
1454 function fits(next, restCommands, width, options, mustBeFlat) {
1455 var restIdx = restCommands.length;
1456 var cmds = [next]; // `out` is only used for width counting because `trim` requires to look
1457 // backwards for space characters.
1461 while (width >= 0) {
1462 if (cmds.length === 0) {
1463 if (restIdx === 0) {
1467 cmds.push(restCommands[restIdx - 1]);
1477 if (typeof doc === "string") {
1479 width -= getStringWidth$1(doc);
1483 for (var i = doc.parts.length - 1; i >= 0; i--) {
1484 cmds.push([ind, mode, doc.parts[i]]);
1490 cmds.push([makeIndent(ind, options), mode, doc.contents]);
1494 cmds.push([makeAlign(ind, doc.n, options), mode, doc.contents]);
1498 width += trim$1(out);
1502 if (mustBeFlat && doc.break) {
1506 cmds.push([ind, doc.break ? MODE_BREAK : mode, doc.contents]);
1509 groupModeMap[doc.id] = cmds[cmds.length - 1][1];
1515 for (var _i = doc.parts.length - 1; _i >= 0; _i--) {
1516 cmds.push([ind, mode, doc.parts[_i]]);
1523 var groupMode = doc.groupId ? groupModeMap[doc.groupId] : mode;
1525 if (groupMode === MODE_BREAK) {
1526 if (doc.breakContents) {
1527 cmds.push([ind, mode, doc.breakContents]);
1531 if (groupMode === MODE_FLAT) {
1532 if (doc.flatContents) {
1533 cmds.push([ind, mode, doc.flatContents]);
1567 function printDocToString(doc, options) {
1569 var width = options.printWidth;
1570 var newLine = convertEndOfLineToChars$1(options.endOfLine);
1571 var pos = 0; // cmds is basically a stack. We've turned a recursive call into a
1572 // while loop which is much faster. The while loop below adds new
1573 // cmds to the array instead of recursively calling `print`.
1575 var cmds = [[rootIndent(), MODE_BREAK, doc]];
1577 var shouldRemeasure = false;
1578 var lineSuffix = [];
1580 while (cmds.length !== 0) {
1586 if (typeof _doc === "string") {
1588 pos += getStringWidth$1(_doc);
1590 switch (_doc.type) {
1592 out.push(cursor$1.placeholder);
1596 for (var i = _doc.parts.length - 1; i >= 0; i--) {
1597 cmds.push([ind, mode, _doc.parts[i]]);
1603 cmds.push([makeIndent(ind, options), mode, _doc.contents]);
1607 cmds.push([makeAlign(ind, _doc.n, options), mode, _doc.contents]);
1617 if (!shouldRemeasure) {
1618 cmds.push([ind, _doc.break ? MODE_BREAK : MODE_FLAT, _doc.contents]);
1626 shouldRemeasure = false;
1627 var next = [ind, MODE_FLAT, _doc.contents];
1628 var rem = width - pos;
1630 if (!_doc.break && fits(next, cmds, rem, options)) {
1633 // Expanded states are a rare case where a document
1634 // can manually provide multiple representations of
1635 // itself. It provides an array of documents
1636 // going from the least expanded (most flattened)
1637 // representation first to the most expanded. If a
1638 // group has these, we need to manually go through
1639 // these states and find the first one that fits.
1640 if (_doc.expandedStates) {
1641 var mostExpanded = _doc.expandedStates[_doc.expandedStates.length - 1];
1644 cmds.push([ind, MODE_BREAK, mostExpanded]);
1647 for (var _i2 = 1; _i2 < _doc.expandedStates.length + 1; _i2++) {
1648 if (_i2 >= _doc.expandedStates.length) {
1649 cmds.push([ind, MODE_BREAK, mostExpanded]);
1652 var state = _doc.expandedStates[_i2];
1653 var cmd = [ind, MODE_FLAT, state];
1655 if (fits(cmd, cmds, rem, options)) {
1663 cmds.push([ind, MODE_BREAK, _doc.contents]);
1672 groupModeMap[_doc.id] = cmds[cmds.length - 1][1];
1676 // Fills each line with as much code as possible before moving to a new
1677 // line with the same indentation.
1679 // Expects doc.parts to be an array of alternating content and
1680 // whitespace. The whitespace contains the linebreaks.
1683 // ["I", line, "love", line, "monkeys"]
1685 // [{ type: group, ... }, softline, { type: group, ... }]
1687 // It uses this parts structure to handle three main layout cases:
1688 // * The first two content items fit on the same line without
1690 // -> output the first content item and the whitespace "flat".
1691 // * Only the first content item fits on the line without breaking
1692 // -> output the first content item "flat" and the whitespace with
1694 // * Neither content item fits on the line without breaking
1695 // -> output the first content item and the whitespace with "break".
1699 var _rem = width - pos;
1701 var parts = _doc.parts;
1703 if (parts.length === 0) {
1707 var content = parts[0];
1708 var contentFlatCmd = [ind, MODE_FLAT, content];
1709 var contentBreakCmd = [ind, MODE_BREAK, content];
1710 var contentFits = fits(contentFlatCmd, [], _rem, options, true);
1712 if (parts.length === 1) {
1714 cmds.push(contentFlatCmd);
1716 cmds.push(contentBreakCmd);
1722 var whitespace = parts[1];
1723 var whitespaceFlatCmd = [ind, MODE_FLAT, whitespace];
1724 var whitespaceBreakCmd = [ind, MODE_BREAK, whitespace];
1726 if (parts.length === 2) {
1728 cmds.push(whitespaceFlatCmd);
1729 cmds.push(contentFlatCmd);
1731 cmds.push(whitespaceBreakCmd);
1732 cmds.push(contentBreakCmd);
1736 } // At this point we've handled the first pair (context, separator)
1737 // and will create a new fill doc for the rest of the content.
1738 // Ideally we wouldn't mutate the array here but coping all the
1739 // elements to a new array would make this algorithm quadratic,
1740 // which is unusable for large arrays (e.g. large texts in JSX).
1744 var remainingCmd = [ind, mode, fill$1(parts)];
1745 var secondContent = parts[0];
1746 var firstAndSecondContentFlatCmd = [ind, MODE_FLAT, concat$1([content, whitespace, secondContent])];
1747 var firstAndSecondContentFits = fits(firstAndSecondContentFlatCmd, [], _rem, options, true);
1749 if (firstAndSecondContentFits) {
1750 cmds.push(remainingCmd);
1751 cmds.push(whitespaceFlatCmd);
1752 cmds.push(contentFlatCmd);
1753 } else if (contentFits) {
1754 cmds.push(remainingCmd);
1755 cmds.push(whitespaceBreakCmd);
1756 cmds.push(contentFlatCmd);
1758 cmds.push(remainingCmd);
1759 cmds.push(whitespaceBreakCmd);
1760 cmds.push(contentBreakCmd);
1768 var groupMode = _doc.groupId ? groupModeMap[_doc.groupId] : mode;
1770 if (groupMode === MODE_BREAK) {
1771 if (_doc.breakContents) {
1772 cmds.push([ind, mode, _doc.breakContents]);
1776 if (groupMode === MODE_FLAT) {
1777 if (_doc.flatContents) {
1778 cmds.push([ind, mode, _doc.flatContents]);
1786 lineSuffix.push([ind, mode, _doc.contents]);
1789 case "line-suffix-boundary":
1790 if (lineSuffix.length > 0) {
1791 cmds.push([ind, mode, {
1810 // This line was forced into the output even if we
1811 // were in flattened mode, so we need to tell the next
1812 // group that no matter what, it needs to remeasure
1813 // because the previous measurement didn't accurately
1814 // capture the entire expression (this is necessary
1815 // for nested groups)
1816 shouldRemeasure = true;
1822 if (lineSuffix.length) {
1823 cmds.push([ind, mode, _doc]);
1824 [].push.apply(cmds, lineSuffix.reverse());
1831 out.push(newLine, ind.root.value);
1832 pos = ind.root.length;
1839 out.push(newLine + ind.value);
1851 var cursorPlaceholderIndex = out.indexOf(cursor$1.placeholder);
1853 if (cursorPlaceholderIndex !== -1) {
1854 var otherCursorPlaceholderIndex = out.indexOf(cursor$1.placeholder, cursorPlaceholderIndex + 1);
1855 var beforeCursor = out.slice(0, cursorPlaceholderIndex).join("");
1856 var aroundCursor = out.slice(cursorPlaceholderIndex + 1, otherCursorPlaceholderIndex).join("");
1857 var afterCursor = out.slice(otherCursorPlaceholderIndex + 1).join("");
1859 formatted: beforeCursor + aroundCursor + afterCursor,
1860 cursorNodeStart: beforeCursor.length,
1861 cursorNodeText: aroundCursor
1866 formatted: out.join("")
1871 printDocToString: printDocToString
1874 var traverseDocOnExitStackMarker = {};
1876 function traverseDoc(doc, onEnter, onExit, shouldTraverseConditionalGroups) {
1877 var docsStack = [doc];
1879 while (docsStack.length !== 0) {
1880 var _doc = docsStack.pop();
1882 if (_doc === traverseDocOnExitStackMarker) {
1883 onExit(docsStack.pop());
1887 var shouldRecurse = true;
1890 if (onEnter(_doc) === false) {
1891 shouldRecurse = false;
1896 docsStack.push(_doc);
1897 docsStack.push(traverseDocOnExitStackMarker);
1900 if (shouldRecurse) {
1901 // When there are multiple parts to process,
1902 // the parts need to be pushed onto the stack in reverse order,
1903 // so that they are processed in the original order
1904 // when the stack is popped.
1905 if (_doc.type === "concat" || _doc.type === "fill") {
1906 for (var ic = _doc.parts.length, i = ic - 1; i >= 0; --i) {
1907 docsStack.push(_doc.parts[i]);
1909 } else if (_doc.type === "if-break") {
1910 if (_doc.flatContents) {
1911 docsStack.push(_doc.flatContents);
1914 if (_doc.breakContents) {
1915 docsStack.push(_doc.breakContents);
1917 } else if (_doc.type === "group" && _doc.expandedStates) {
1918 if (shouldTraverseConditionalGroups) {
1919 for (var _ic = _doc.expandedStates.length, _i = _ic - 1; _i >= 0; --_i) {
1920 docsStack.push(_doc.expandedStates[_i]);
1923 docsStack.push(_doc.contents);
1925 } else if (_doc.contents) {
1926 docsStack.push(_doc.contents);
1932 function mapDoc(doc, cb) {
1933 if (doc.type === "concat" || doc.type === "fill") {
1934 var parts = doc.parts.map(function (part) {
1935 return mapDoc(part, cb);
1937 return cb(Object.assign({}, doc, {
1940 } else if (doc.type === "if-break") {
1941 var breakContents = doc.breakContents && mapDoc(doc.breakContents, cb);
1942 var flatContents = doc.flatContents && mapDoc(doc.flatContents, cb);
1943 return cb(Object.assign({}, doc, {
1944 breakContents: breakContents,
1945 flatContents: flatContents
1947 } else if (doc.contents) {
1948 var contents = mapDoc(doc.contents, cb);
1949 return cb(Object.assign({}, doc, {
1957 function findInDoc(doc, fn, defaultValue) {
1958 var result = defaultValue;
1959 var hasStopped = false;
1961 function findInDocOnEnterFn(doc) {
1962 var maybeResult = fn(doc);
1964 if (maybeResult !== undefined) {
1966 result = maybeResult;
1974 traverseDoc(doc, findInDocOnEnterFn);
1978 function isEmpty(n) {
1979 return typeof n === "string" && n.length === 0;
1982 function isLineNextFn(doc) {
1983 if (typeof doc === "string") {
1987 if (doc.type === "line") {
1992 function isLineNext(doc) {
1993 return findInDoc(doc, isLineNextFn, false);
1996 function willBreakFn(doc) {
1997 if (doc.type === "group" && doc.break) {
2001 if (doc.type === "line" && doc.hard) {
2005 if (doc.type === "break-parent") {
2010 function willBreak(doc) {
2011 return findInDoc(doc, willBreakFn, false);
2014 function breakParentGroup(groupStack) {
2015 if (groupStack.length > 0) {
2016 var parentGroup = groupStack[groupStack.length - 1]; // Breaks are not propagated through conditional groups because
2017 // the user is expected to manually handle what breaks.
2019 if (!parentGroup.expandedStates) {
2020 parentGroup.break = true;
2027 function propagateBreaks(doc) {
2028 var alreadyVisitedSet = new Set();
2029 var groupStack = [];
2031 function propagateBreaksOnEnterFn(doc) {
2032 if (doc.type === "break-parent") {
2033 breakParentGroup(groupStack);
2036 if (doc.type === "group") {
2037 groupStack.push(doc);
2039 if (alreadyVisitedSet.has(doc)) {
2043 alreadyVisitedSet.add(doc);
2047 function propagateBreaksOnExitFn(doc) {
2048 if (doc.type === "group") {
2049 var group = groupStack.pop();
2052 breakParentGroup(groupStack);
2057 traverseDoc(doc, propagateBreaksOnEnterFn, propagateBreaksOnExitFn,
2058 /* shouldTraverseConditionalGroups */
2062 function removeLinesFn(doc) {
2063 // Force this doc into flat mode by statically converting all
2064 // lines into spaces (or soft lines into nothing). Hard lines
2065 // should still output because there's too great of a chance
2066 // of breaking existing assumptions otherwise.
2067 if (doc.type === "line" && !doc.hard) {
2068 return doc.soft ? "" : " ";
2069 } else if (doc.type === "if-break") {
2070 return doc.flatContents || "";
2076 function removeLines(doc) {
2077 return mapDoc(doc, removeLinesFn);
2080 function stripTrailingHardline(doc) {
2081 // HACK remove ending hardline, original PR: #1984
2082 if (doc.type === "concat" && doc.parts.length !== 0) {
2083 var lastPart = doc.parts[doc.parts.length - 1];
2085 if (lastPart.type === "concat") {
2086 if (lastPart.parts.length === 2 && lastPart.parts[0].hard && lastPart.parts[1].type === "break-parent") {
2089 parts: doc.parts.slice(0, -1)
2095 parts: doc.parts.slice(0, -1).concat(stripTrailingHardline(lastPart))
2105 willBreak: willBreak,
2106 isLineNext: isLineNext,
2107 traverseDoc: traverseDoc,
2108 findInDoc: findInDoc,
2110 propagateBreaks: propagateBreaks,
2111 removeLines: removeLines,
2112 stripTrailingHardline: stripTrailingHardline
2115 function flattenDoc(doc) {
2116 if (doc.type === "concat") {
2119 for (var i = 0; i < doc.parts.length; ++i) {
2120 var doc2 = doc.parts[i];
2122 if (typeof doc2 !== "string" && doc2.type === "concat") {
2123 [].push.apply(res, flattenDoc(doc2).parts);
2125 var flattened = flattenDoc(doc2);
2127 if (flattened !== "") {
2128 res.push(flattened);
2133 return Object.assign({}, doc, {
2136 } else if (doc.type === "if-break") {
2137 return Object.assign({}, doc, {
2138 breakContents: doc.breakContents != null ? flattenDoc(doc.breakContents) : null,
2139 flatContents: doc.flatContents != null ? flattenDoc(doc.flatContents) : null
2141 } else if (doc.type === "group") {
2142 return Object.assign({}, doc, {
2143 contents: flattenDoc(doc.contents),
2144 expandedStates: doc.expandedStates ? doc.expandedStates.map(flattenDoc) : doc.expandedStates
2146 } else if (doc.contents) {
2147 return Object.assign({}, doc, {
2148 contents: flattenDoc(doc.contents)
2155 function printDoc(doc) {
2156 if (typeof doc === "string") {
2157 return JSON.stringify(doc);
2160 if (doc.type === "line") {
2162 return "literalline";
2176 if (doc.type === "break-parent") {
2177 return "breakParent";
2180 if (doc.type === "trim") {
2184 if (doc.type === "concat") {
2185 return "[" + doc.parts.map(printDoc).join(", ") + "]";
2188 if (doc.type === "indent") {
2189 return "indent(" + printDoc(doc.contents) + ")";
2192 if (doc.type === "align") {
2193 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) + ")";
2196 if (doc.type === "if-break") {
2197 return "ifBreak(" + printDoc(doc.breakContents) + (doc.flatContents ? ", " + printDoc(doc.flatContents) : "") + ")";
2200 if (doc.type === "group") {
2201 if (doc.expandedStates) {
2202 return "conditionalGroup(" + "[" + doc.expandedStates.map(printDoc).join(",") + "])";
2205 return (doc.break ? "wrappedGroup" : "group") + "(" + printDoc(doc.contents) + ")";
2208 if (doc.type === "fill") {
2209 return "fill" + "(" + doc.parts.map(printDoc).join(", ") + ")";
2212 if (doc.type === "line-suffix") {
2213 return "lineSuffix(" + printDoc(doc.contents) + ")";
2216 if (doc.type === "line-suffix-boundary") {
2217 return "lineSuffixBoundary";
2220 throw new Error("Unknown doc type " + doc.type);
2224 printDocToDebug: function printDocToDebug(doc) {
2225 return printDoc(flattenDoc(doc));
2230 builders: docBuilders,
2231 printer: docPrinter,
2235 var doc_1 = doc.builders;
2236 var doc_2 = doc.printer;
2237 var doc_3 = doc.utils;
2238 var doc_4 = doc.debug;
2240 exports.builders = doc_1;
2241 exports.debug = doc_4;
2242 exports.default = doc;
2243 exports.printer = doc_2;
2244 exports.utils = doc_3;
2246 Object.defineProperty(exports, '__esModule', { value: true });