doc.d.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // https://github.com/prettier/prettier/blob/next/src/document/public.js
  2. export namespace builders {
  3. type DocCommand =
  4. | Align
  5. | BreakParent
  6. | Cursor
  7. | Fill
  8. | Group
  9. | IfBreak
  10. | Indent
  11. | IndentIfBreak
  12. | Label
  13. | Line
  14. | LineSuffix
  15. | LineSuffixBoundary
  16. | Trim;
  17. type Doc = string | Doc[] | DocCommand;
  18. interface Align {
  19. type: "align";
  20. contents: Doc;
  21. n: number | string | { type: "root" };
  22. }
  23. interface BreakParent {
  24. type: "break-parent";
  25. }
  26. interface Cursor {
  27. type: "cursor";
  28. placeholder: symbol;
  29. }
  30. interface Fill {
  31. type: "fill";
  32. parts: Doc[];
  33. }
  34. interface Group {
  35. type: "group";
  36. contents: Doc;
  37. break: boolean;
  38. expandedStates: Doc[];
  39. }
  40. interface HardlineWithoutBreakParent extends Line {
  41. hard: true;
  42. }
  43. interface IfBreak {
  44. type: "if-break";
  45. breakContents: Doc;
  46. flatContents: Doc;
  47. }
  48. interface Indent {
  49. type: "indent";
  50. contents: Doc;
  51. }
  52. interface IndentIfBreak {
  53. type: "indent-if-break";
  54. }
  55. interface Label {
  56. type: "label";
  57. }
  58. interface Line {
  59. type: "line";
  60. soft?: boolean | undefined;
  61. hard?: boolean | undefined;
  62. literal?: boolean | undefined;
  63. }
  64. interface LineSuffix {
  65. type: "line-suffix";
  66. contents: Doc;
  67. }
  68. interface LineSuffixBoundary {
  69. type: "line-suffix-boundary";
  70. }
  71. interface LiterallineWithoutBreakParent extends Line {
  72. hard: true;
  73. literal: true;
  74. }
  75. type LiteralLine = [LiterallineWithoutBreakParent, BreakParent];
  76. interface Softline extends Line {
  77. soft: true;
  78. }
  79. type Hardline = [HardlineWithoutBreakParent, BreakParent];
  80. interface Trim {
  81. type: "trim";
  82. }
  83. interface GroupOptions {
  84. shouldBreak?: boolean | undefined;
  85. id?: symbol | undefined;
  86. }
  87. function addAlignmentToDoc(doc: Doc, size: number, tabWidth: number): Doc;
  88. /** @see [align](https://github.com/prettier/prettier/blob/main/commands.md#align) */
  89. function align(widthOrString: Align["n"], doc: Doc): Align;
  90. /** @see [breakParent](https://github.com/prettier/prettier/blob/main/commands.md#breakparent) */
  91. const breakParent: BreakParent;
  92. /** @see [conditionalGroup](https://github.com/prettier/prettier/blob/main/commands.md#conditionalgroup) */
  93. function conditionalGroup(alternatives: Doc[], options?: GroupOptions): Group;
  94. /** @see [dedent](https://github.com/prettier/prettier/blob/main/commands.md#dedent) */
  95. function dedent(doc: Doc): Align;
  96. /** @see [dedentToRoot](https://github.com/prettier/prettier/blob/main/commands.md#dedenttoroot) */
  97. function dedentToRoot(doc: Doc): Align;
  98. /** @see [fill](https://github.com/prettier/prettier/blob/main/commands.md#fill) */
  99. function fill(docs: Doc[]): Fill;
  100. /** @see [group](https://github.com/prettier/prettier/blob/main/commands.md#group) */
  101. function group(doc: Doc, opts?: GroupOptions): Group;
  102. /** @see [hardline](https://github.com/prettier/prettier/blob/main/commands.md#hardline) */
  103. const hardline: Hardline;
  104. /** @see [hardlineWithoutBreakParent](https://github.com/prettier/prettier/blob/main/commands.md#hardlinewithoutbreakparent-and-literallinewithoutbreakparent) */
  105. const hardlineWithoutBreakParent: HardlineWithoutBreakParent;
  106. /** @see [ifBreak](https://github.com/prettier/prettier/blob/main/commands.md#ifbreak) */
  107. function ifBreak(
  108. ifBreak: Doc,
  109. noBreak?: Doc,
  110. options?: { groupId?: symbol | undefined }
  111. ): IfBreak;
  112. /** @see [indent](https://github.com/prettier/prettier/blob/main/commands.md#indent) */
  113. function indent(doc: Doc): Indent;
  114. /** @see [indentIfBreak](https://github.com/prettier/prettier/blob/main/commands.md#indentifbreak) */
  115. function indentIfBreak(
  116. doc: Doc,
  117. opts: { groupId: symbol; negate?: boolean | undefined }
  118. ): IndentIfBreak;
  119. /** @see [join](https://github.com/prettier/prettier/blob/main/commands.md#join) */
  120. function join(sep: Doc, docs: Doc[]): Doc[];
  121. /** @see [label](https://github.com/prettier/prettier/blob/main/commands.md#label) */
  122. function label(label: any | undefined, contents: Doc): Doc;
  123. /** @see [line](https://github.com/prettier/prettier/blob/main/commands.md#line) */
  124. const line: Line;
  125. /** @see [lineSuffix](https://github.com/prettier/prettier/blob/main/commands.md#linesuffix) */
  126. function lineSuffix(suffix: Doc): LineSuffix;
  127. /** @see [lineSuffixBoundary](https://github.com/prettier/prettier/blob/main/commands.md#linesuffixboundary) */
  128. const lineSuffixBoundary: LineSuffixBoundary;
  129. /** @see [literalline](https://github.com/prettier/prettier/blob/main/commands.md#literalline) */
  130. const literalline: LiteralLine;
  131. /** @see [literallineWithoutBreakParent](https://github.com/prettier/prettier/blob/main/commands.md#hardlinewithoutbreakparent-and-literallinewithoutbreakparent) */
  132. const literallineWithoutBreakParent: LiterallineWithoutBreakParent;
  133. /** @see [markAsRoot](https://github.com/prettier/prettier/blob/main/commands.md#markasroot) */
  134. function markAsRoot(doc: Doc): Align;
  135. /** @see [softline](https://github.com/prettier/prettier/blob/main/commands.md#softline) */
  136. const softline: Softline;
  137. /** @see [trim](https://github.com/prettier/prettier/blob/main/commands.md#trim) */
  138. const trim: Trim;
  139. /** @see [cursor](https://github.com/prettier/prettier/blob/main/commands.md#cursor) */
  140. const cursor: Cursor;
  141. }
  142. export namespace printer {
  143. function printDocToString(
  144. doc: builders.Doc,
  145. options: Options
  146. ): {
  147. formatted: string;
  148. cursorNodeStart?: number | undefined;
  149. cursorNodeText?: string | undefined;
  150. };
  151. interface Options {
  152. /**
  153. * Specify the line length that the printer will wrap on.
  154. * @default 80
  155. */
  156. printWidth: number;
  157. /**
  158. * Specify the number of spaces per indentation-level.
  159. * @default 2
  160. */
  161. tabWidth: number;
  162. /**
  163. * Indent lines with tabs instead of spaces
  164. * @default false
  165. */
  166. useTabs?: boolean;
  167. parentParser?: string | undefined;
  168. __embeddedInHtml?: boolean | undefined;
  169. }
  170. }
  171. export namespace utils {
  172. function willBreak(doc: builders.Doc): boolean;
  173. function traverseDoc(
  174. doc: builders.Doc,
  175. onEnter?: (doc: builders.Doc) => void | boolean,
  176. onExit?: (doc: builders.Doc) => void,
  177. shouldTraverseConditionalGroups?: boolean
  178. ): void;
  179. function findInDoc<T = builders.Doc>(
  180. doc: builders.Doc,
  181. callback: (doc: builders.Doc) => T,
  182. defaultValue: T
  183. ): T;
  184. function mapDoc<T = builders.Doc>(
  185. doc: builders.Doc,
  186. callback: (doc: builders.Doc) => T
  187. ): T;
  188. function removeLines(doc: builders.Doc): builders.Doc;
  189. function stripTrailingHardline(doc: builders.Doc): builders.Doc;
  190. function replaceEndOfLine(
  191. doc: builders.Doc,
  192. replacement?: builders.Doc
  193. ): builders.Doc;
  194. function canBreak(doc: builders.Doc): boolean;
  195. }