UNPKG

9.66 kBTypeScriptView Raw
1/**
2 * Options for the writer.
3 */
4export interface Options {
5 /**
6 * Newline character.
7 * @remarks Defaults to \n.
8 */
9 newLine: "\n" | "\r\n";
10 /**
11 * Number of spaces to indent when `useTabs` is false.
12 * @remarks Defaults to 4.
13 */
14 indentNumberOfSpaces: number;
15 /**
16 * Whether to use tabs (true) or spaces (false).
17 * @remarks Defaults to false.
18 */
19 useTabs: boolean;
20 /**
21 * Whether to use a single quote (true) or double quote (false).
22 * @remarks Defaults to false.
23 */
24 useSingleQuote: boolean;
25}
26/**
27 * Code writer that assists with formatting and visualizing blocks of JavaScript or TypeScript code.
28 */
29export default class CodeBlockWriter {
30 /**
31 * Constructor.
32 * @param opts - Options for the writer.
33 */
34 constructor(opts?: Partial<Options>);
35 /**
36 * Gets the options.
37 */
38 getOptions(): Options;
39 /**
40 * Queues the indentation level for the next lines written.
41 * @param indentationLevel - Indentation level to queue.
42 */
43 queueIndentationLevel(indentationLevel: number): this;
44 /**
45 * Queues the indentation level for the next lines written using the provided indentation text.
46 * @param whitespaceText - Gets the indentation level from the indentation text.
47 */
48 queueIndentationLevel(whitespaceText: string): this;
49 /**
50 * Writes the text within the provided action with hanging indentation.
51 * @param action - Action to perform with hanging indentation.
52 */
53 hangingIndent(action: () => void): this;
54 /**
55 * Writes the text within the provided action with hanging indentation unless writing a block.
56 * @param action - Action to perform with hanging indentation unless a block is written.
57 */
58 hangingIndentUnlessBlock(action: () => void): this;
59 /**
60 * Sets the current indentation level.
61 * @param indentationLevel - Indentation level to be at.
62 */
63 setIndentationLevel(indentationLevel: number): this;
64 /**
65 * Sets the current indentation using the provided indentation text.
66 * @param whitespaceText - Gets the indentation level from the indentation text.
67 */
68 setIndentationLevel(whitespaceText: string): this;
69 /**
70 * Sets the indentation level within the provided action and restores the writer's indentation
71 * state afterwards.
72 * @remarks Restores the writer's state after the action.
73 * @param indentationLevel - Indentation level to set.
74 * @param action - Action to perform with the indentation.
75 */
76 withIndentationLevel(indentationLevel: number, action: () => void): this;
77 /**
78 * Sets the indentation level with the provided indentation text within the provided action
79 * and restores the writer's indentation state afterwards.
80 * @param whitespaceText - Gets the indentation level from the indentation text.
81 * @param action - Action to perform with the indentation.
82 */
83 withIndentationLevel(whitespaceText: string, action: () => void): this;
84 /**
85 * Gets the current indentation level.
86 */
87 getIndentationLevel(): number;
88 /**
89 * Writes a block using braces.
90 * @param block - Write using the writer within this block.
91 */
92 block(block?: () => void): this;
93 /**
94 * Writes an inline block with braces.
95 * @param block - Write using the writer within this block.
96 */
97 inlineBlock(block?: () => void): this;
98 /**
99 * Indents the code one level for the current line.
100 */
101 indent(times?: number): this;
102 /**
103 * Indents a block of code.
104 * @param block - Block to indent.
105 */
106 indent(block: () => void): this;
107 /**
108 * Conditionally writes a line of text.
109 * @param condition - Condition to evaluate.
110 * @param textFunc - A function that returns a string to write if the condition is true.
111 */
112 conditionalWriteLine(condition: boolean | undefined, textFunc: () => string): this;
113 /**
114 * Conditionally writes a line of text.
115 * @param condition - Condition to evaluate.
116 * @param text - Text to write if the condition is true.
117 */
118 conditionalWriteLine(condition: boolean | undefined, text: string): this;
119 /**
120 * Writes a line of text.
121 * @param text - String to write.
122 */
123 writeLine(text: string): this;
124 /**
125 * Writes a newline if the last line was not a newline.
126 */
127 newLineIfLastNot(): this;
128 /**
129 * Writes a blank line if the last written text was not a blank line.
130 */
131 blankLineIfLastNot(): this;
132 /**
133 * Writes a blank line if the condition is true.
134 * @param condition - Condition to evaluate.
135 */
136 conditionalBlankLine(condition: boolean | undefined): this;
137 /**
138 * Writes a blank line.
139 */
140 blankLine(): this;
141 /**
142 * Writes a newline if the condition is true.
143 * @param condition - Condition to evaluate.
144 */
145 conditionalNewLine(condition: boolean | undefined): this;
146 /**
147 * Writes a newline.
148 */
149 newLine(): this;
150 /**
151 * Writes a quote character.
152 */
153 quote(): this;
154 /**
155 * Writes text surrounded in quotes.
156 * @param text - Text to write.
157 */
158 quote(text: string): this;
159 /**
160 * Writes a space if the last character was not a space.
161 */
162 spaceIfLastNot(): this;
163 /**
164 * Writes a space.
165 * @param times - Number of times to write a space.
166 */
167 space(times?: number): this;
168 /**
169 * Writes a tab if the last character was not a tab.
170 */
171 tabIfLastNot(): this;
172 /**
173 * Writes a tab.
174 * @param times - Number of times to write a tab.
175 */
176 tab(times?: number): this;
177 /**
178 * Conditionally writes text.
179 * @param condition - Condition to evaluate.
180 * @param textFunc - A function that returns a string to write if the condition is true.
181 */
182 conditionalWrite(condition: boolean | undefined, textFunc: () => string): this;
183 /**
184 * Conditionally writes text.
185 * @param condition - Condition to evaluate.
186 * @param text - Text to write if the condition is true.
187 */
188 conditionalWrite(condition: boolean | undefined, text: string): this;
189 /**
190 * Writes the provided text.
191 * @param text - Text to write.
192 */
193 write(text: string): this;
194 /**
195 * Writes text to exit a comment if in a comment.
196 */
197 closeComment(): this;
198 /**
199 * Inserts text at the provided position.
200 *
201 * This method is "unsafe" because it won't update the state of the writer unless
202 * inserting at the end position. It is biased towards being fast at inserting closer
203 * to the start or end, but slower to insert in the middle. Only use this if
204 * absolutely necessary.
205 * @param pos - Position to insert at.
206 * @param text - Text to insert.
207 */
208 unsafeInsert(pos: number, text: string): this;
209 /**
210 * Gets the length of the string in the writer.
211 */
212 getLength(): number;
213 /**
214 * Gets if the writer is currently in a comment.
215 */
216 isInComment(): boolean;
217 /**
218 * Gets if the writer is currently at the start of the first line of the text, block, or indentation block.
219 */
220 isAtStartOfFirstLineOfBlock(): boolean;
221 /**
222 * Gets if the writer is currently on the first line of the text, block, or indentation block.
223 */
224 isOnFirstLineOfBlock(): boolean;
225 /**
226 * Gets if the writer is currently in a string.
227 */
228 isInString(): boolean;
229 /**
230 * Gets if the last chars written were for a newline.
231 */
232 isLastNewLine(): boolean;
233 /**
234 * Gets if the last chars written were for a blank line.
235 */
236 isLastBlankLine(): boolean;
237 /**
238 * Gets if the last char written was a space.
239 */
240 isLastSpace(): boolean;
241 /**
242 * Gets if the last char written was a tab.
243 */
244 isLastTab(): boolean;
245 /**
246 * Gets the last char written.
247 */
248 getLastChar(): string | undefined;
249 /**
250 * Gets if the writer ends with the provided text.
251 * @param text - Text to check if the writer ends with the provided text.
252 */
253 endsWith(text: string): boolean;
254 /**
255 * Iterates over the writer characters in reverse order. The iteration stops when a non-null or
256 * undefined value is returned from the action. The returned value is then returned by the method.
257 *
258 * @remarks It is much more efficient to use this method rather than `#toString()` since `#toString()`
259 * will combine the internal array into a string.
260 */
261 iterateLastChars<T>(action: (char: string, index: number) => T | undefined): T | undefined;
262 /**
263 * Iterates over the writer character char codes in reverse order. The iteration stops when a non-null or
264 * undefined value is returned from the action. The returned value is then returned by the method.
265 *
266 * @remarks It is much more efficient to use this method rather than `#toString()` since `#toString()`
267 * will combine the internal array into a string. Additionally, this is slightly more efficient that
268 * `iterateLastChars` as this won't allocate a string per character.
269 */
270 iterateLastCharCodes<T>(action: (charCode: number, index: number) => T | undefined): T | undefined;
271 /**
272 * Gets the writer's text.
273 */
274 toString(): string;
275}