UNPKG

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