UNPKG

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