UNPKG

7.42 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 /**
196 * This property is a misnomer, and has been since the changes in
197 * https://github.com/prettier/prettier/pull/15709.
198 * The region of the document indicated by `cursorNodeStart` and `cursorNodeText` will
199 * sometimes actually be what lies BETWEEN a pair of leaf nodes in the AST, rather than a node.
200 */
201 cursorNodeStart?: number | undefined;
202
203 /**
204 * Note that, like cursorNodeStart, this is a misnomer and may actually be the text between two
205 * leaf nodes in the AST instead of the text of a node.
206 */
207 cursorNodeText?: string | undefined;
208 };
209 interface Options {
210 /**
211 * Specify the line length that the printer will wrap on.
212 * @default 80
213 */
214 printWidth: number;
215 /**
216 * Specify the number of spaces per indentation-level.
217 * @default 2
218 */
219 tabWidth: number;
220 /**
221 * Indent lines with tabs instead of spaces
222 * @default false
223 */
224 useTabs?: boolean;
225 parentParser?: string | undefined;
226 __embeddedInHtml?: boolean | undefined;
227 }
228}
229
230export namespace utils {
231 function willBreak(doc: builders.Doc): boolean;
232 function traverseDoc(
233 doc: builders.Doc,
234 onEnter?: (doc: builders.Doc) => void | boolean,
235 onExit?: (doc: builders.Doc) => void,
236 shouldTraverseConditionalGroups?: boolean,
237 ): void;
238 function findInDoc<T = builders.Doc>(
239 doc: builders.Doc,
240 callback: (doc: builders.Doc) => T,
241 defaultValue: T,
242 ): T;
243 function mapDoc<T = builders.Doc>(
244 doc: builders.Doc,
245 callback: (doc: builders.Doc) => T,
246 ): T;
247 function removeLines(doc: builders.Doc): builders.Doc;
248 function stripTrailingHardline(doc: builders.Doc): builders.Doc;
249 function replaceEndOfLine(
250 doc: builders.Doc,
251 replacement?: builders.Doc,
252 ): builders.Doc;
253 function canBreak(doc: builders.Doc): boolean;
254}