UNPKG

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