prettier 3.0.0-alpha.0 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +51 -67
  2. package/{bin-prettier.js → bin/prettier.cjs} +2 -2
  3. package/doc.js +438 -221
  4. package/doc.mjs +1490 -0
  5. package/index.cjs +532 -0
  6. package/{index.js → index.mjs} +4411 -5763
  7. package/{cli.mjs → internal/cli.mjs} +150 -94
  8. package/{third-party.js → internal/third-party.mjs} +129 -100
  9. package/package.json +139 -6
  10. package/plugins/acorn-and-espree.js +13 -0
  11. package/plugins/acorn-and-espree.mjs +13 -0
  12. package/plugins/angular.js +2 -0
  13. package/plugins/angular.mjs +2 -0
  14. package/plugins/babel.js +16 -0
  15. package/plugins/babel.mjs +16 -0
  16. package/plugins/flow.js +21 -0
  17. package/plugins/flow.mjs +21 -0
  18. package/{parser-glimmer.js → plugins/glimmer.js} +9 -9
  19. package/{esm/parser-glimmer.mjs → plugins/glimmer.mjs} +10 -10
  20. package/plugins/graphql.js +9 -0
  21. package/plugins/graphql.mjs +9 -0
  22. package/plugins/html.js +29 -0
  23. package/plugins/html.mjs +29 -0
  24. package/plugins/markdown.js +39 -0
  25. package/plugins/markdown.mjs +39 -0
  26. package/plugins/meriyah.js +6 -0
  27. package/plugins/meriyah.mjs +6 -0
  28. package/plugins/postcss.js +50 -0
  29. package/plugins/postcss.mjs +50 -0
  30. package/plugins/typescript.js +242 -0
  31. package/plugins/typescript.mjs +242 -0
  32. package/{parser-yaml.js → plugins/yaml.js} +1 -1
  33. package/{esm/parser-yaml.mjs → plugins/yaml.mjs} +1 -1
  34. package/standalone.js +70 -87
  35. package/standalone.mjs +107 -0
  36. package/esm/parser-angular.mjs +0 -2
  37. package/esm/parser-babel.mjs +0 -18
  38. package/esm/parser-espree.mjs +0 -15
  39. package/esm/parser-flow.mjs +0 -23
  40. package/esm/parser-graphql.mjs +0 -9
  41. package/esm/parser-html.mjs +0 -28
  42. package/esm/parser-markdown.mjs +0 -60
  43. package/esm/parser-meriyah.mjs +0 -8
  44. package/esm/parser-postcss.mjs +0 -53
  45. package/esm/parser-typescript.mjs +0 -269
  46. package/esm/standalone.mjs +0 -124
  47. package/parser-angular.js +0 -2
  48. package/parser-babel.js +0 -18
  49. package/parser-espree.js +0 -15
  50. package/parser-flow.js +0 -23
  51. package/parser-graphql.js +0 -9
  52. package/parser-html.js +0 -28
  53. package/parser-markdown.js +0 -60
  54. package/parser-meriyah.js +0 -8
  55. package/parser-postcss.js +0 -53
  56. package/parser-typescript.js +0 -269
package/doc.js CHANGED
@@ -133,6 +133,21 @@
133
133
  var DOC_TYPE_LINE = "line";
134
134
  var DOC_TYPE_LABEL = "label";
135
135
  var DOC_TYPE_BREAK_PARENT = "break-parent";
136
+ var VALID_OBJECT_DOC_TYPES = /* @__PURE__ */ new Set([
137
+ DOC_TYPE_CURSOR,
138
+ DOC_TYPE_INDENT,
139
+ DOC_TYPE_ALIGN,
140
+ DOC_TYPE_TRIM,
141
+ DOC_TYPE_GROUP,
142
+ DOC_TYPE_FILL,
143
+ DOC_TYPE_IF_BREAK,
144
+ DOC_TYPE_INDENT_IF_BREAK,
145
+ DOC_TYPE_LINE_SUFFIX,
146
+ DOC_TYPE_LINE_SUFFIX_BOUNDARY,
147
+ DOC_TYPE_LINE,
148
+ DOC_TYPE_LABEL,
149
+ DOC_TYPE_BREAK_PARENT
150
+ ]);
136
151
 
137
152
  // src/document/builders.js
138
153
  function indent(contents) {
@@ -222,7 +237,7 @@
222
237
  var softline = { type: DOC_TYPE_LINE, soft: true };
223
238
  var hardline = [hardlineWithoutBreakParent, breakParent];
224
239
  var literalline = [literallineWithoutBreakParent, breakParent];
225
- var cursor = { type: DOC_TYPE_CURSOR, placeholder: Symbol("cursor") };
240
+ var cursor = { type: DOC_TYPE_CURSOR };
226
241
  function join(sep, arr) {
227
242
  const res = [];
228
243
  for (let i = 0; i < arr.length; i++) {
@@ -245,7 +260,7 @@
245
260
  return aligned;
246
261
  }
247
262
  function label(label2, contents) {
248
- return { type: DOC_TYPE_LABEL, label: label2, contents };
263
+ return label2 ? { type: DOC_TYPE_LABEL, label: label2, contents } : contents;
249
264
  }
250
265
  var concat = (parts) => parts;
251
266
 
@@ -256,6 +271,16 @@
256
271
  });
257
272
  init_define_process();
258
273
 
274
+ // scripts/build/shims/at.js
275
+ init_define_process();
276
+ var at = (isOptionalObject, object, index, ...args) => {
277
+ if (isOptionalObject && (object === void 0 || object === null)) {
278
+ return;
279
+ }
280
+ return object.at ? object.at(index, ...args) : object[index < 0 ? object.length + index : index];
281
+ };
282
+ var at_default = at;
283
+
259
284
  // src/common/end-of-line.js
260
285
  init_define_process();
261
286
  function convertEndOfLineToChars(value) {
@@ -269,11 +294,6 @@
269
294
  }
270
295
  }
271
296
 
272
- // src/utils/get-last.js
273
- init_define_process();
274
- var getLast = (arr) => arr[arr.length - 1];
275
- var get_last_default = getLast;
276
-
277
297
  // src/utils/get-string-width.js
278
298
  init_define_process();
279
299
 
@@ -358,28 +378,71 @@
358
378
  cleanDoc: () => cleanDoc,
359
379
  findInDoc: () => findInDoc,
360
380
  getDocParts: () => getDocParts,
361
- getDocType: () => getDocType,
381
+ getDocType: () => get_doc_type_default,
362
382
  mapDoc: () => mapDoc,
363
383
  normalizeDoc: () => normalizeDoc,
364
384
  normalizeParts: () => normalizeParts,
365
385
  propagateBreaks: () => propagateBreaks,
366
386
  removeLines: () => removeLines,
367
387
  replaceEndOfLine: () => replaceEndOfLine,
368
- replaceTextEndOfLine: () => replaceTextEndOfLine,
369
388
  stripTrailingHardline: () => stripTrailingHardline,
370
- traverseDoc: () => traverseDoc,
389
+ traverseDoc: () => traverse_doc_default,
371
390
  willBreak: () => willBreak
372
391
  });
373
392
  init_define_process();
374
- var getDocParts = (doc) => {
393
+
394
+ // src/document/utils/get-doc-type.js
395
+ init_define_process();
396
+ function getDocType(doc) {
397
+ if (typeof doc === "string") {
398
+ return DOC_TYPE_STRING;
399
+ }
375
400
  if (Array.isArray(doc)) {
376
- return doc;
401
+ return DOC_TYPE_ARRAY;
377
402
  }
378
- if (doc.type !== DOC_TYPE_FILL) {
379
- throw new Error(`Expect doc to be 'array' or '${DOC_TYPE_FILL}'.`);
403
+ if (!doc) {
404
+ return;
405
+ }
406
+ const { type } = doc;
407
+ if (VALID_OBJECT_DOC_TYPES.has(type)) {
408
+ return type;
409
+ }
410
+ }
411
+ var get_doc_type_default = getDocType;
412
+
413
+ // src/document/utils/traverse-doc.js
414
+ init_define_process();
415
+
416
+ // src/document/invalid-doc-error.js
417
+ init_define_process();
418
+ var disjunctionListFormat = (list) => [...list.slice(0, -1), `or ${at_default(false, list, -1)}`].join(", ");
419
+ function getDocErrorMessage(doc) {
420
+ const type = doc === null ? null : typeof doc;
421
+ if (type !== "string" && type !== "object") {
422
+ return `Unexpected doc '${type}',
423
+ Expected it to be 'string' or 'object'.`;
424
+ }
425
+ if (get_doc_type_default(doc)) {
426
+ throw new Error("doc is valid.");
427
+ }
428
+ const objectType = Object.prototype.toString.call(doc);
429
+ if (objectType !== "[object Object]") {
430
+ return `Unexpected doc '${objectType}'.`;
431
+ }
432
+ const EXPECTED_TYPE_VALUES = disjunctionListFormat([...VALID_OBJECT_DOC_TYPES].map((type2) => `'${type2}'`));
433
+ return `Unexpected doc.type '${doc.type}'.
434
+ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
435
+ }
436
+ var InvalidDocError = class extends Error {
437
+ constructor(doc) {
438
+ super(getDocErrorMessage(doc));
439
+ __publicField(this, "name", "InvalidDocError");
440
+ this.doc = doc;
380
441
  }
381
- return doc.parts;
382
442
  };
443
+ var invalid_doc_error_default = InvalidDocError;
444
+
445
+ // src/document/utils/traverse-doc.js
383
446
  var traverseDocOnExitStackMarker = {};
384
447
  function traverseDoc(doc, onEnter, onExit, shouldTraverseConditionalGroups) {
385
448
  const docsStack = [doc];
@@ -392,34 +455,71 @@
392
455
  if (onExit) {
393
456
  docsStack.push(doc2, traverseDocOnExitStackMarker);
394
457
  }
395
- if (!onEnter || onEnter(doc2) !== false) {
396
- if (Array.isArray(doc2) || doc2.type === DOC_TYPE_FILL) {
397
- const parts = getDocParts(doc2);
458
+ if ((onEnter == null ? void 0 : onEnter(doc2)) === false) {
459
+ continue;
460
+ }
461
+ const docType = get_doc_type_default(doc2);
462
+ switch (docType) {
463
+ case DOC_TYPE_ARRAY:
464
+ case DOC_TYPE_FILL: {
465
+ const parts = docType === DOC_TYPE_ARRAY ? doc2 : doc2.parts;
398
466
  for (let ic = parts.length, i = ic - 1; i >= 0; --i) {
399
467
  docsStack.push(parts[i]);
400
468
  }
401
- } else if (doc2.type === DOC_TYPE_IF_BREAK) {
469
+ break;
470
+ }
471
+ case DOC_TYPE_IF_BREAK:
402
472
  if (doc2.flatContents) {
403
473
  docsStack.push(doc2.flatContents);
404
474
  }
405
475
  if (doc2.breakContents) {
406
476
  docsStack.push(doc2.breakContents);
407
477
  }
408
- } else if (doc2.type === DOC_TYPE_GROUP && doc2.expandedStates) {
409
- if (shouldTraverseConditionalGroups) {
478
+ break;
479
+ case DOC_TYPE_GROUP:
480
+ if (shouldTraverseConditionalGroups && doc2.expandedStates) {
410
481
  for (let ic = doc2.expandedStates.length, i = ic - 1; i >= 0; --i) {
411
482
  docsStack.push(doc2.expandedStates[i]);
412
483
  }
413
484
  } else {
414
485
  docsStack.push(doc2.contents);
415
486
  }
416
- } else if (doc2.contents) {
487
+ break;
488
+ case DOC_TYPE_ALIGN:
489
+ case DOC_TYPE_INDENT:
490
+ case DOC_TYPE_INDENT_IF_BREAK:
491
+ case DOC_TYPE_LABEL:
492
+ case DOC_TYPE_LINE_SUFFIX:
417
493
  docsStack.push(doc2.contents);
418
- }
494
+ break;
495
+ case DOC_TYPE_STRING:
496
+ case DOC_TYPE_CURSOR:
497
+ case DOC_TYPE_TRIM:
498
+ case DOC_TYPE_LINE_SUFFIX_BOUNDARY:
499
+ case DOC_TYPE_LINE:
500
+ case DOC_TYPE_BREAK_PARENT:
501
+ break;
502
+ default:
503
+ throw new invalid_doc_error_default(doc2);
419
504
  }
420
505
  }
421
506
  }
507
+ var traverse_doc_default = traverseDoc;
508
+
509
+ // src/document/utils.js
510
+ var getDocParts = (doc) => {
511
+ if (Array.isArray(doc)) {
512
+ return doc;
513
+ }
514
+ if (doc.type !== DOC_TYPE_FILL) {
515
+ throw new Error(`Expect doc to be 'array' or '${DOC_TYPE_FILL}'.`);
516
+ }
517
+ return doc.parts;
518
+ };
422
519
  function mapDoc(doc, cb) {
520
+ if (typeof doc === "string") {
521
+ return cb(doc);
522
+ }
423
523
  const mapped = /* @__PURE__ */ new Map();
424
524
  return rec(doc);
425
525
  function rec(doc2) {
@@ -431,44 +531,79 @@
431
531
  return result;
432
532
  }
433
533
  function process2(doc2) {
434
- if (Array.isArray(doc2)) {
435
- return cb(doc2.map(rec));
436
- }
437
- if (doc2.type === DOC_TYPE_ARRAY || doc2.type === DOC_TYPE_FILL) {
438
- const parts = doc2.parts.map(rec);
439
- return cb({ ...doc2, parts });
440
- }
441
- if (doc2.type === DOC_TYPE_IF_BREAK) {
442
- const breakContents = doc2.breakContents && rec(doc2.breakContents);
443
- const flatContents = doc2.flatContents && rec(doc2.flatContents);
444
- return cb({ ...doc2, breakContents, flatContents });
445
- }
446
- if (doc2.type === DOC_TYPE_GROUP && doc2.expandedStates) {
447
- const expandedStates = doc2.expandedStates.map(rec);
448
- const contents = expandedStates[0];
449
- return cb({ ...doc2, contents, expandedStates });
450
- }
451
- if (doc2.contents) {
452
- const contents = rec(doc2.contents);
453
- return cb({ ...doc2, contents });
534
+ switch (get_doc_type_default(doc2)) {
535
+ case DOC_TYPE_ARRAY:
536
+ return cb(doc2.map(rec));
537
+ case DOC_TYPE_FILL:
538
+ return cb({
539
+ ...doc2,
540
+ parts: doc2.parts.map(rec)
541
+ });
542
+ case DOC_TYPE_IF_BREAK: {
543
+ let {
544
+ breakContents,
545
+ flatContents
546
+ } = doc2;
547
+ breakContents && (breakContents = rec(breakContents));
548
+ flatContents && (flatContents = rec(flatContents));
549
+ return cb({
550
+ ...doc2,
551
+ breakContents,
552
+ flatContents
553
+ });
554
+ }
555
+ case DOC_TYPE_GROUP: {
556
+ let {
557
+ expandedStates,
558
+ contents
559
+ } = doc2;
560
+ if (expandedStates) {
561
+ expandedStates = expandedStates.map(rec);
562
+ contents = expandedStates[0];
563
+ } else {
564
+ contents = rec(contents);
565
+ }
566
+ return cb({
567
+ ...doc2,
568
+ contents,
569
+ expandedStates
570
+ });
571
+ }
572
+ case DOC_TYPE_ALIGN:
573
+ case DOC_TYPE_INDENT:
574
+ case DOC_TYPE_INDENT_IF_BREAK:
575
+ case DOC_TYPE_LABEL:
576
+ case DOC_TYPE_LINE_SUFFIX:
577
+ return cb({
578
+ ...doc2,
579
+ contents: rec(doc2.contents)
580
+ });
581
+ case DOC_TYPE_STRING:
582
+ case DOC_TYPE_CURSOR:
583
+ case DOC_TYPE_TRIM:
584
+ case DOC_TYPE_LINE_SUFFIX_BOUNDARY:
585
+ case DOC_TYPE_LINE:
586
+ case DOC_TYPE_BREAK_PARENT:
587
+ return cb(doc2);
588
+ default:
589
+ throw new invalid_doc_error_default(doc2);
454
590
  }
455
- return cb(doc2);
456
591
  }
457
592
  }
458
593
  function findInDoc(doc, fn, defaultValue) {
459
594
  let result = defaultValue;
460
- let hasStopped = false;
595
+ let shouldSkipFurtherProcessing = false;
461
596
  function findInDocOnEnterFn(doc2) {
597
+ if (shouldSkipFurtherProcessing) {
598
+ return false;
599
+ }
462
600
  const maybeResult = fn(doc2);
463
601
  if (maybeResult !== void 0) {
464
- hasStopped = true;
602
+ shouldSkipFurtherProcessing = true;
465
603
  result = maybeResult;
466
604
  }
467
- if (hasStopped) {
468
- return false;
469
- }
470
605
  }
471
- traverseDoc(doc, findInDocOnEnterFn);
606
+ traverse_doc_default(doc, findInDocOnEnterFn);
472
607
  return result;
473
608
  }
474
609
  function willBreakFn(doc) {
@@ -487,7 +622,7 @@
487
622
  }
488
623
  function breakParentGroup(groupStack) {
489
624
  if (groupStack.length > 0) {
490
- const parentGroup = get_last_default(groupStack);
625
+ const parentGroup = at_default(false, groupStack, -1);
491
626
  if (!parentGroup.expandedStates && !parentGroup.break) {
492
627
  parentGroup.break = "propagated";
493
628
  }
@@ -517,7 +652,7 @@
517
652
  }
518
653
  }
519
654
  }
520
- traverseDoc(
655
+ traverse_doc_default(
521
656
  doc,
522
657
  propagateBreaksOnEnterFn,
523
658
  propagateBreaksOnExitFn,
@@ -547,10 +682,13 @@
547
682
  parts.length -= 2;
548
683
  }
549
684
  if (parts.length > 0) {
550
- const lastPart = stripDocTrailingHardlineFromDoc(get_last_default(parts));
685
+ const lastPart = stripDocTrailingHardlineFromDoc(at_default(false, parts, -1));
551
686
  parts[parts.length - 1] = lastPart;
552
687
  }
553
- return Array.isArray(doc) ? parts : { ...doc, parts };
688
+ return Array.isArray(doc) ? parts : {
689
+ ...doc,
690
+ parts
691
+ };
554
692
  }
555
693
  switch (doc.type) {
556
694
  case DOC_TYPE_ALIGN:
@@ -560,12 +698,19 @@
560
698
  case DOC_TYPE_LINE_SUFFIX:
561
699
  case DOC_TYPE_LABEL: {
562
700
  const contents = stripDocTrailingHardlineFromDoc(doc.contents);
563
- return { ...doc, contents };
701
+ return {
702
+ ...doc,
703
+ contents
704
+ };
564
705
  }
565
706
  case DOC_TYPE_IF_BREAK: {
566
707
  const breakContents = stripDocTrailingHardlineFromDoc(doc.breakContents);
567
708
  const flatContents = stripDocTrailingHardlineFromDoc(doc.flatContents);
568
- return { ...doc, breakContents, flatContents };
709
+ return {
710
+ ...doc,
711
+ breakContents,
712
+ flatContents
713
+ };
569
714
  }
570
715
  }
571
716
  return doc;
@@ -577,7 +722,7 @@
577
722
  return stripDocTrailingHardlineFromDoc(cleanDoc(doc));
578
723
  }
579
724
  function cleanDocFn(doc) {
580
- switch (doc.type) {
725
+ switch (get_doc_type_default(doc)) {
581
726
  case DOC_TYPE_FILL:
582
727
  if (doc.parts.every((part) => part === "")) {
583
728
  return "";
@@ -604,30 +749,40 @@
604
749
  return "";
605
750
  }
606
751
  break;
607
- }
608
- if (!Array.isArray(doc)) {
609
- return doc;
610
- }
611
- const parts = [];
612
- for (const part of doc) {
613
- if (!part) {
614
- continue;
615
- }
616
- const [currentPart, ...restParts] = Array.isArray(part) ? part : [part];
617
- if (typeof currentPart === "string" && typeof get_last_default(parts) === "string") {
618
- parts[parts.length - 1] += currentPart;
619
- } else {
620
- parts.push(currentPart);
752
+ case DOC_TYPE_ARRAY: {
753
+ const parts = [];
754
+ for (const part of doc) {
755
+ if (!part) {
756
+ continue;
757
+ }
758
+ const [currentPart, ...restParts] = Array.isArray(part) ? part : [part];
759
+ if (typeof currentPart === "string" && typeof at_default(false, parts, -1) === "string") {
760
+ parts[parts.length - 1] += currentPart;
761
+ } else {
762
+ parts.push(currentPart);
763
+ }
764
+ parts.push(...restParts);
765
+ }
766
+ if (parts.length === 0) {
767
+ return "";
768
+ }
769
+ if (parts.length === 1) {
770
+ return parts[0];
771
+ }
772
+ return parts;
621
773
  }
622
- parts.push(...restParts);
623
- }
624
- if (parts.length === 0) {
625
- return "";
626
- }
627
- if (parts.length === 1) {
628
- return parts[0];
774
+ case DOC_TYPE_STRING:
775
+ case DOC_TYPE_CURSOR:
776
+ case DOC_TYPE_TRIM:
777
+ case DOC_TYPE_LINE_SUFFIX_BOUNDARY:
778
+ case DOC_TYPE_LINE:
779
+ case DOC_TYPE_LABEL:
780
+ case DOC_TYPE_BREAK_PARENT:
781
+ break;
782
+ default:
783
+ throw new invalid_doc_error_default(doc);
629
784
  }
630
- return parts;
785
+ return doc;
631
786
  }
632
787
  function cleanDoc(doc) {
633
788
  return mapDoc(doc, (currentDoc) => cleanDocFn(currentDoc));
@@ -644,7 +799,7 @@
644
799
  restParts.unshift(...part);
645
800
  continue;
646
801
  }
647
- if (newParts.length > 0 && typeof get_last_default(newParts) === "string" && typeof part === "string") {
802
+ if (newParts.length > 0 && typeof at_default(false, newParts, -1) === "string" && typeof part === "string") {
648
803
  newParts[newParts.length - 1] += part;
649
804
  continue;
650
805
  }
@@ -666,14 +821,8 @@
666
821
  };
667
822
  });
668
823
  }
669
- function replaceEndOfLine(doc) {
670
- return mapDoc(
671
- doc,
672
- (currentDoc) => typeof currentDoc === "string" && currentDoc.includes("\n") ? replaceTextEndOfLine(currentDoc) : currentDoc
673
- );
674
- }
675
- function replaceTextEndOfLine(text, replacement = literalline) {
676
- return join(replacement, text.split("\n"));
824
+ function replaceEndOfLine(doc, replacement = literalline) {
825
+ return mapDoc(doc, (currentDoc) => typeof currentDoc === "string" ? join(replacement, currentDoc.split("\n")) : currentDoc);
677
826
  }
678
827
  function canBreakFn(doc) {
679
828
  if (doc.type === DOC_TYPE_LINE) {
@@ -683,88 +832,46 @@
683
832
  function canBreak(doc) {
684
833
  return findInDoc(doc, canBreakFn, false);
685
834
  }
686
- function getDocType(doc) {
687
- if (typeof doc === "string") {
688
- return DOC_TYPE_STRING;
689
- }
690
- if (Array.isArray(doc)) {
691
- return DOC_TYPE_ARRAY;
692
- }
693
- return doc == null ? void 0 : doc.type;
694
- }
695
-
696
- // src/document/invalid-doc-error.js
697
- init_define_process();
698
- var VALID_OBJECT_DOC_TYPE_VALUES = [
699
- DOC_TYPE_CURSOR,
700
- DOC_TYPE_INDENT,
701
- DOC_TYPE_ALIGN,
702
- DOC_TYPE_TRIM,
703
- DOC_TYPE_GROUP,
704
- DOC_TYPE_FILL,
705
- DOC_TYPE_IF_BREAK,
706
- DOC_TYPE_INDENT_IF_BREAK,
707
- DOC_TYPE_LINE_SUFFIX,
708
- DOC_TYPE_LINE_SUFFIX_BOUNDARY,
709
- DOC_TYPE_LINE,
710
- DOC_TYPE_LABEL,
711
- DOC_TYPE_BREAK_PARENT
712
- ];
713
- var disjunctionListFormat = (list) => [...list.slice(0, -1), `or ${get_last_default(list)}`].join(", ");
714
- function getDocErrorMessage(doc) {
715
- const type = doc === null ? null : typeof doc;
716
- if (type !== "string" && type !== "object") {
717
- return `Unexpected doc '${type}',
718
- Expected it to be 'string' or 'object'.`;
719
- }
720
- const docType = getDocType(doc);
721
- if (docType === DOC_TYPE_STRING || docType === DOC_TYPE_ARRAY || VALID_OBJECT_DOC_TYPE_VALUES.includes(docType)) {
722
- throw new Error("doc is valid.");
723
- }
724
- const objectType = Object.prototype.toString.call(doc);
725
- if (objectType !== "[object Object]") {
726
- return `Unexpected doc '${objectType}'.`;
727
- }
728
- const EXPECTED_TYPE_VALUES = disjunctionListFormat(
729
- VALID_OBJECT_DOC_TYPE_VALUES.map((type2) => `'${type2}'`)
730
- );
731
- return `Unexpected doc.type '${docType}'.
732
- Expected it to be ${EXPECTED_TYPE_VALUES}.`;
733
- }
734
- var InvalidDocError = class extends Error {
735
- constructor(doc) {
736
- super(getDocErrorMessage(doc));
737
- __publicField(this, "name", "InvalidDocError");
738
- this.doc = doc;
739
- }
740
- };
741
- var invalid_doc_error_default = InvalidDocError;
742
835
 
743
836
  // src/document/printer.js
744
- var groupModeMap;
745
- var MODE_BREAK = 1;
746
- var MODE_FLAT = 2;
837
+ var MODE_BREAK = Symbol("MODE_BREAK");
838
+ var MODE_FLAT = Symbol("MODE_FLAT");
839
+ var CURSOR_PLACEHOLDER = Symbol("cursor");
747
840
  function rootIndent() {
748
- return { value: "", length: 0, queue: [] };
841
+ return {
842
+ value: "",
843
+ length: 0,
844
+ queue: []
845
+ };
749
846
  }
750
847
  function makeIndent(ind, options) {
751
- return generateInd(ind, { type: "indent" }, options);
848
+ return generateInd(ind, {
849
+ type: "indent"
850
+ }, options);
752
851
  }
753
852
  function makeAlign(indent2, widthOrDoc, options) {
754
853
  if (widthOrDoc === Number.NEGATIVE_INFINITY) {
755
854
  return indent2.root || rootIndent();
756
855
  }
757
856
  if (widthOrDoc < 0) {
758
- return generateInd(indent2, { type: "dedent" }, options);
857
+ return generateInd(indent2, {
858
+ type: "dedent"
859
+ }, options);
759
860
  }
760
861
  if (!widthOrDoc) {
761
862
  return indent2;
762
863
  }
763
864
  if (widthOrDoc.type === "root") {
764
- return { ...indent2, root: indent2 };
865
+ return {
866
+ ...indent2,
867
+ root: indent2
868
+ };
765
869
  }
766
870
  const alignType = typeof widthOrDoc === "string" ? "stringAlign" : "numberAlign";
767
- return generateInd(indent2, { type: alignType, n: widthOrDoc }, options);
871
+ return generateInd(indent2, {
872
+ type: alignType,
873
+ n: widthOrDoc
874
+ }, options);
768
875
  }
769
876
  function generateInd(ind, newPart, options) {
770
877
  const queue = newPart.type === "dedent" ? ind.queue.slice(0, -1) : [...ind.queue, newPart];
@@ -796,7 +903,12 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
796
903
  }
797
904
  }
798
905
  flushSpaces();
799
- return { ...ind, value, length, queue };
906
+ return {
907
+ ...ind,
908
+ value,
909
+ length,
910
+ queue
911
+ };
800
912
  function addTabs(count) {
801
913
  value += " ".repeat(count);
802
914
  length += options.tabWidth * count;
@@ -830,21 +942,38 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
830
942
  }
831
943
  }
832
944
  function trim2(out) {
833
- if (out.length === 0) {
834
- return 0;
835
- }
836
945
  let trimCount = 0;
837
- while (out.length > 0 && typeof get_last_default(out) === "string" && /^[\t ]*$/.test(get_last_default(out))) {
838
- trimCount += out.pop().length;
839
- }
840
- if (out.length > 0 && typeof get_last_default(out) === "string") {
841
- const trimmed = get_last_default(out).replace(/[\t ]*$/, "");
842
- trimCount += get_last_default(out).length - trimmed.length;
843
- out[out.length - 1] = trimmed;
946
+ let cursorCount = 0;
947
+ let outIndex = out.length;
948
+ outer:
949
+ while (outIndex--) {
950
+ const last = out[outIndex];
951
+ if (last === CURSOR_PLACEHOLDER) {
952
+ cursorCount++;
953
+ continue;
954
+ }
955
+ if (false) {
956
+ throw new Error(`Unexpected value in trim: '${typeof last}'`);
957
+ }
958
+ for (let charIndex = last.length - 1; charIndex >= 0; charIndex--) {
959
+ const char = last[charIndex];
960
+ if (char === " " || char === " ") {
961
+ trimCount++;
962
+ } else {
963
+ out[outIndex] = last.slice(0, charIndex + 1);
964
+ break outer;
965
+ }
966
+ }
967
+ }
968
+ if (trimCount > 0 || cursorCount > 0) {
969
+ out.length = outIndex + 1;
970
+ while (cursorCount-- > 0) {
971
+ out.push(CURSOR_PLACEHOLDER);
972
+ }
844
973
  }
845
974
  return trimCount;
846
975
  }
847
- function fits(next, restCommands, width, hasLineSuffix, mustBeFlat) {
976
+ function fits(next, restCommands, width, hasLineSuffix, groupModeMap, mustBeFlat) {
848
977
  let restIdx = restCommands.length;
849
978
  const cmds = [next];
850
979
  const out = [];
@@ -856,8 +985,11 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
856
985
  cmds.push(restCommands[--restIdx]);
857
986
  continue;
858
987
  }
859
- const { mode, doc } = cmds.pop();
860
- switch (getDocType(doc)) {
988
+ const {
989
+ mode,
990
+ doc
991
+ } = cmds.pop();
992
+ switch (get_doc_type_default(doc)) {
861
993
  case DOC_TYPE_STRING:
862
994
  out.push(doc);
863
995
  width -= get_string_width_default(doc);
@@ -866,7 +998,10 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
866
998
  case DOC_TYPE_FILL: {
867
999
  const parts = getDocParts(doc);
868
1000
  for (let i = parts.length - 1; i >= 0; i--) {
869
- cmds.push({ mode, doc: parts[i] });
1001
+ cmds.push({
1002
+ mode,
1003
+ doc: parts[i]
1004
+ });
870
1005
  }
871
1006
  break;
872
1007
  }
@@ -874,7 +1009,10 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
874
1009
  case DOC_TYPE_ALIGN:
875
1010
  case DOC_TYPE_INDENT_IF_BREAK:
876
1011
  case DOC_TYPE_LABEL:
877
- cmds.push({ mode, doc: doc.contents });
1012
+ cmds.push({
1013
+ mode,
1014
+ doc: doc.contents
1015
+ });
878
1016
  break;
879
1017
  case DOC_TYPE_TRIM:
880
1018
  width += trim2(out);
@@ -884,15 +1022,21 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
884
1022
  return false;
885
1023
  }
886
1024
  const groupMode = doc.break ? MODE_BREAK : mode;
887
- const contents = doc.expandedStates && groupMode === MODE_BREAK ? get_last_default(doc.expandedStates) : doc.contents;
888
- cmds.push({ mode: groupMode, doc: contents });
1025
+ const contents = doc.expandedStates && groupMode === MODE_BREAK ? at_default(false, doc.expandedStates, -1) : doc.contents;
1026
+ cmds.push({
1027
+ mode: groupMode,
1028
+ doc: contents
1029
+ });
889
1030
  break;
890
1031
  }
891
1032
  case DOC_TYPE_IF_BREAK: {
892
1033
  const groupMode = doc.groupId ? groupModeMap[doc.groupId] || MODE_FLAT : mode;
893
1034
  const contents = groupMode === MODE_BREAK ? doc.breakContents : doc.flatContents;
894
1035
  if (contents) {
895
- cmds.push({ mode, doc: contents });
1036
+ cmds.push({
1037
+ mode,
1038
+ doc: contents
1039
+ });
896
1040
  }
897
1041
  break;
898
1042
  }
@@ -918,34 +1062,54 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
918
1062
  return false;
919
1063
  }
920
1064
  function printDocToString(doc, options) {
921
- groupModeMap = {};
1065
+ const groupModeMap = {};
922
1066
  const width = options.printWidth;
923
1067
  const newLine = convertEndOfLineToChars(options.endOfLine);
924
1068
  let pos = 0;
925
- const cmds = [{ ind: rootIndent(), mode: MODE_BREAK, doc }];
1069
+ const cmds = [{
1070
+ ind: rootIndent(),
1071
+ mode: MODE_BREAK,
1072
+ doc
1073
+ }];
926
1074
  const out = [];
927
1075
  let shouldRemeasure = false;
928
1076
  const lineSuffix2 = [];
1077
+ let printedCursorCount = 0;
929
1078
  while (cmds.length > 0) {
930
- const { ind, mode, doc: doc2 } = cmds.pop();
931
- switch (getDocType(doc2)) {
1079
+ const {
1080
+ ind,
1081
+ mode,
1082
+ doc: doc2
1083
+ } = cmds.pop();
1084
+ switch (get_doc_type_default(doc2)) {
932
1085
  case DOC_TYPE_STRING: {
933
1086
  const formatted = newLine !== "\n" ? doc2.replace(/\n/g, newLine) : doc2;
934
1087
  out.push(formatted);
935
1088
  pos += get_string_width_default(formatted);
936
1089
  break;
937
1090
  }
938
- case DOC_TYPE_ARRAY: {
1091
+ case DOC_TYPE_ARRAY:
939
1092
  for (let i = doc2.length - 1; i >= 0; i--) {
940
- cmds.push({ ind, mode, doc: doc2[i] });
1093
+ cmds.push({
1094
+ ind,
1095
+ mode,
1096
+ doc: doc2[i]
1097
+ });
941
1098
  }
942
1099
  break;
943
- }
944
1100
  case DOC_TYPE_CURSOR:
945
- out.push(cursor.placeholder);
1101
+ if (printedCursorCount >= 2) {
1102
+ throw new Error("There are too many 'cursor' in doc.");
1103
+ }
1104
+ out.push(CURSOR_PLACEHOLDER);
1105
+ printedCursorCount++;
946
1106
  break;
947
1107
  case DOC_TYPE_INDENT:
948
- cmds.push({ ind: makeIndent(ind, options), mode, doc: doc2.contents });
1108
+ cmds.push({
1109
+ ind: makeIndent(ind, options),
1110
+ mode,
1111
+ doc: doc2.contents
1112
+ });
949
1113
  break;
950
1114
  case DOC_TYPE_ALIGN:
951
1115
  cmds.push({
@@ -970,26 +1134,42 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
970
1134
  }
971
1135
  case MODE_BREAK: {
972
1136
  shouldRemeasure = false;
973
- const next = { ind, mode: MODE_FLAT, doc: doc2.contents };
1137
+ const next = {
1138
+ ind,
1139
+ mode: MODE_FLAT,
1140
+ doc: doc2.contents
1141
+ };
974
1142
  const rem = width - pos;
975
1143
  const hasLineSuffix = lineSuffix2.length > 0;
976
- if (!doc2.break && fits(next, cmds, rem, hasLineSuffix)) {
1144
+ if (!doc2.break && fits(next, cmds, rem, hasLineSuffix, groupModeMap)) {
977
1145
  cmds.push(next);
978
1146
  } else {
979
1147
  if (doc2.expandedStates) {
980
- const mostExpanded = get_last_default(doc2.expandedStates);
1148
+ const mostExpanded = at_default(false, doc2.expandedStates, -1);
981
1149
  if (doc2.break) {
982
- cmds.push({ ind, mode: MODE_BREAK, doc: mostExpanded });
1150
+ cmds.push({
1151
+ ind,
1152
+ mode: MODE_BREAK,
1153
+ doc: mostExpanded
1154
+ });
983
1155
  break;
984
1156
  } else {
985
1157
  for (let i = 1; i < doc2.expandedStates.length + 1; i++) {
986
1158
  if (i >= doc2.expandedStates.length) {
987
- cmds.push({ ind, mode: MODE_BREAK, doc: mostExpanded });
1159
+ cmds.push({
1160
+ ind,
1161
+ mode: MODE_BREAK,
1162
+ doc: mostExpanded
1163
+ });
988
1164
  break;
989
1165
  } else {
990
1166
  const state = doc2.expandedStates[i];
991
- const cmd = { ind, mode: MODE_FLAT, doc: state };
992
- if (fits(cmd, cmds, rem, hasLineSuffix)) {
1167
+ const cmd = {
1168
+ ind,
1169
+ mode: MODE_FLAT,
1170
+ doc: state
1171
+ };
1172
+ if (fits(cmd, cmds, rem, hasLineSuffix, groupModeMap)) {
993
1173
  cmds.push(cmd);
994
1174
  break;
995
1175
  }
@@ -997,32 +1177,40 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
997
1177
  }
998
1178
  }
999
1179
  } else {
1000
- cmds.push({ ind, mode: MODE_BREAK, doc: doc2.contents });
1180
+ cmds.push({
1181
+ ind,
1182
+ mode: MODE_BREAK,
1183
+ doc: doc2.contents
1184
+ });
1001
1185
  }
1002
1186
  }
1003
1187
  break;
1004
1188
  }
1005
1189
  }
1006
1190
  if (doc2.id) {
1007
- groupModeMap[doc2.id] = get_last_default(cmds).mode;
1191
+ groupModeMap[doc2.id] = at_default(false, cmds, -1).mode;
1008
1192
  }
1009
1193
  break;
1010
1194
  case DOC_TYPE_FILL: {
1011
1195
  const rem = width - pos;
1012
- const { parts } = doc2;
1196
+ const {
1197
+ parts
1198
+ } = doc2;
1013
1199
  if (parts.length === 0) {
1014
1200
  break;
1015
1201
  }
1016
1202
  const [content, whitespace] = parts;
1017
- const contentFlatCmd = { ind, mode: MODE_FLAT, doc: content };
1018
- const contentBreakCmd = { ind, mode: MODE_BREAK, doc: content };
1019
- const contentFits = fits(
1020
- contentFlatCmd,
1021
- [],
1022
- rem,
1023
- lineSuffix2.length > 0,
1024
- true
1025
- );
1203
+ const contentFlatCmd = {
1204
+ ind,
1205
+ mode: MODE_FLAT,
1206
+ doc: content
1207
+ };
1208
+ const contentBreakCmd = {
1209
+ ind,
1210
+ mode: MODE_BREAK,
1211
+ doc: content
1212
+ };
1213
+ const contentFits = fits(contentFlatCmd, [], rem, lineSuffix2.length > 0, groupModeMap, true);
1026
1214
  if (parts.length === 1) {
1027
1215
  if (contentFits) {
1028
1216
  cmds.push(contentFlatCmd);
@@ -1031,8 +1219,16 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1031
1219
  }
1032
1220
  break;
1033
1221
  }
1034
- const whitespaceFlatCmd = { ind, mode: MODE_FLAT, doc: whitespace };
1035
- const whitespaceBreakCmd = { ind, mode: MODE_BREAK, doc: whitespace };
1222
+ const whitespaceFlatCmd = {
1223
+ ind,
1224
+ mode: MODE_FLAT,
1225
+ doc: whitespace
1226
+ };
1227
+ const whitespaceBreakCmd = {
1228
+ ind,
1229
+ mode: MODE_BREAK,
1230
+ doc: whitespace
1231
+ };
1036
1232
  if (parts.length === 2) {
1037
1233
  if (contentFits) {
1038
1234
  cmds.push(whitespaceFlatCmd, contentFlatCmd);
@@ -1042,20 +1238,18 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1042
1238
  break;
1043
1239
  }
1044
1240
  parts.splice(0, 2);
1045
- const remainingCmd = { ind, mode, doc: fill(parts) };
1241
+ const remainingCmd = {
1242
+ ind,
1243
+ mode,
1244
+ doc: fill(parts)
1245
+ };
1046
1246
  const secondContent = parts[0];
1047
1247
  const firstAndSecondContentFlatCmd = {
1048
1248
  ind,
1049
1249
  mode: MODE_FLAT,
1050
1250
  doc: [content, whitespace, secondContent]
1051
1251
  };
1052
- const firstAndSecondContentFits = fits(
1053
- firstAndSecondContentFlatCmd,
1054
- [],
1055
- rem,
1056
- lineSuffix2.length > 0,
1057
- true
1058
- );
1252
+ const firstAndSecondContentFits = fits(firstAndSecondContentFlatCmd, [], rem, lineSuffix2.length > 0, groupModeMap, true);
1059
1253
  if (firstAndSecondContentFits) {
1060
1254
  cmds.push(remainingCmd, whitespaceFlatCmd, contentFlatCmd);
1061
1255
  } else if (contentFits) {
@@ -1071,23 +1265,39 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1071
1265
  if (groupMode === MODE_BREAK) {
1072
1266
  const breakContents = doc2.type === DOC_TYPE_IF_BREAK ? doc2.breakContents : doc2.negate ? doc2.contents : indent(doc2.contents);
1073
1267
  if (breakContents) {
1074
- cmds.push({ ind, mode, doc: breakContents });
1268
+ cmds.push({
1269
+ ind,
1270
+ mode,
1271
+ doc: breakContents
1272
+ });
1075
1273
  }
1076
1274
  }
1077
1275
  if (groupMode === MODE_FLAT) {
1078
1276
  const flatContents = doc2.type === DOC_TYPE_IF_BREAK ? doc2.flatContents : doc2.negate ? indent(doc2.contents) : doc2.contents;
1079
1277
  if (flatContents) {
1080
- cmds.push({ ind, mode, doc: flatContents });
1278
+ cmds.push({
1279
+ ind,
1280
+ mode,
1281
+ doc: flatContents
1282
+ });
1081
1283
  }
1082
1284
  }
1083
1285
  break;
1084
1286
  }
1085
1287
  case DOC_TYPE_LINE_SUFFIX:
1086
- lineSuffix2.push({ ind, mode, doc: doc2.contents });
1288
+ lineSuffix2.push({
1289
+ ind,
1290
+ mode,
1291
+ doc: doc2.contents
1292
+ });
1087
1293
  break;
1088
1294
  case DOC_TYPE_LINE_SUFFIX_BOUNDARY:
1089
1295
  if (lineSuffix2.length > 0) {
1090
- cmds.push({ ind, mode, doc: hardlineWithoutBreakParent });
1296
+ cmds.push({
1297
+ ind,
1298
+ mode,
1299
+ doc: hardlineWithoutBreakParent
1300
+ });
1091
1301
  }
1092
1302
  break;
1093
1303
  case DOC_TYPE_LINE:
@@ -1104,7 +1314,11 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1104
1314
  }
1105
1315
  case MODE_BREAK:
1106
1316
  if (lineSuffix2.length > 0) {
1107
- cmds.push({ ind, mode, doc: doc2 }, ...lineSuffix2.reverse());
1317
+ cmds.push({
1318
+ ind,
1319
+ mode,
1320
+ doc: doc2
1321
+ }, ...lineSuffix2.reverse());
1108
1322
  lineSuffix2.length = 0;
1109
1323
  break;
1110
1324
  }
@@ -1125,7 +1339,11 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1125
1339
  }
1126
1340
  break;
1127
1341
  case DOC_TYPE_LABEL:
1128
- cmds.push({ ind, mode, doc: doc2.contents });
1342
+ cmds.push({
1343
+ ind,
1344
+ mode,
1345
+ doc: doc2.contents
1346
+ });
1129
1347
  break;
1130
1348
  case DOC_TYPE_BREAK_PARENT:
1131
1349
  break;
@@ -1137,12 +1355,9 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1137
1355
  lineSuffix2.length = 0;
1138
1356
  }
1139
1357
  }
1140
- const cursorPlaceholderIndex = out.indexOf(cursor.placeholder);
1358
+ const cursorPlaceholderIndex = out.indexOf(CURSOR_PLACEHOLDER);
1141
1359
  if (cursorPlaceholderIndex !== -1) {
1142
- const otherCursorPlaceholderIndex = out.indexOf(
1143
- cursor.placeholder,
1144
- cursorPlaceholderIndex + 1
1145
- );
1360
+ const otherCursorPlaceholderIndex = out.indexOf(CURSOR_PLACEHOLDER, cursorPlaceholderIndex + 1);
1146
1361
  const beforeCursor = out.slice(0, cursorPlaceholderIndex).join("");
1147
1362
  const aroundCursor = out.slice(cursorPlaceholderIndex + 1, otherCursorPlaceholderIndex).join("");
1148
1363
  const afterCursor = out.slice(otherCursorPlaceholderIndex + 1).join("");
@@ -1152,7 +1367,9 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1152
1367
  cursorNodeText: aroundCursor
1153
1368
  };
1154
1369
  }
1155
- return { formatted: out.join("") };
1370
+ return {
1371
+ formatted: out.join("")
1372
+ };
1156
1373
  }
1157
1374
 
1158
1375
  // src/document/debug.js