UNPKG

6.66 kBTypeScriptView Raw
1interface CodeBlockWriterOptions {
2 newLine: string;
3 indentNumberOfSpaces: number;
4 useTabs: boolean;
5 useSingleQuote: boolean;
6}
7declare class CodeBlockWriter {
8 private readonly _indentationText;
9 private readonly _newLine;
10 private readonly _useTabs;
11 private readonly _quoteChar;
12 private readonly _indentNumberOfSpaces;
13 private _currentIndentation;
14 private _queuedIndentation;
15 private _text;
16 private _newLineOnNextWrite;
17 private _stringCharStack;
18 private _isInRegEx;
19 private _isOnFirstLineOfBlock;
20 private static readonly _newLineRegEx;
21 private static readonly _spacesOrTabsRegEx;
22 constructor(opts?: Partial<CodeBlockWriterOptions>);
23 /**
24 * Gets the options.
25 */
26 getOptions(): CodeBlockWriterOptions;
27 /**
28 * Queues the indentation level for the next lines written.
29 * @param indentationLevel - Indentation level to queue.
30 */
31 queueIndentationLevel(indentationLevel: number): this;
32 /**
33 * Queues the indentation level for the next lines written using the provided indentation text.
34 * @param indentationText - Gets the indentation level from the indentation text.
35 */
36 queueIndentationLevel(indentationText: string): this;
37 /**
38 * Sets the current indentation level.
39 * @param indentationLevel - Indentation level to be at.
40 */
41 setIndentationLevel(indentationLevel: number): this;
42 /**
43 * Sets the current indentation using the provided indentation text.
44 * @param indentationText - Gets the indentation level from the indentation text.
45 */
46 setIndentationLevel(indentationText: string): this;
47 /**
48 * Gets the current indentation level.
49 */
50 getIndentationLevel(): number;
51 /**
52 * Writes a block using braces.
53 * @param block - Write using the writer within this block.
54 */
55 block(block?: () => void): this;
56 /**
57 * Writes an inline block with braces.
58 * @param block - Write using the writer within this block.
59 */
60 inlineBlock(block?: () => void): this;
61 /**
62 * Indents a block of code.
63 * @param block - Block to indent.
64 */
65 indentBlock(block: () => void): this;
66 private _indentBlockInternal;
67 /**
68 * Conditionally writes a line of text.
69 * @param condition - Condition to evaluate.
70 * @param textFunc - A function that returns a string to write if the condition is true.
71 */
72 conditionalWriteLine(condition: boolean | undefined, textFunc: () => string): this;
73 /**
74 * Conditionally writes a line of text.
75 * @param condition - Condition to evaluate.
76 * @param text - Text to write if the condition is true.
77 */
78 conditionalWriteLine(condition: boolean | undefined, text: string): this;
79 /**
80 * Writes a line of text.
81 * @param text - String to write.
82 */
83 writeLine(text: string): this;
84 /**
85 * Writes a newline if the last line was not a newline.
86 */
87 newLineIfLastNot(): this;
88 /**
89 * Writes a blank line if the last written text was not a blank line.
90 */
91 blankLineIfLastNot(): this;
92 /**
93 * Writes a blank line if the condition is true.
94 * @param condition - Condition to evaluate.
95 */
96 conditionalBlankLine(condition: boolean | undefined): this;
97 /**
98 * Writes a blank line.
99 */
100 blankLine(): this;
101 /**
102 * Indents the code one level for the current line.
103 */
104 indent(): this;
105 /**
106 * Writes a newline if the condition is true.
107 * @param condition - Condition to evaluate.
108 */
109 conditionalNewLine(condition: boolean | undefined): this;
110 /**
111 * Writes a newline.
112 */
113 newLine(): this;
114 /**
115 * Writes a quote character.
116 */
117 quote(): this;
118 /**
119 * Writes text surrounded in quotes.
120 * @param text - Text to write.
121 */
122 quote(text: string): this;
123 /**
124 * Writes a space if the last character was not a space.
125 */
126 spaceIfLastNot(): this;
127 /**
128 * Writes a space.
129 * @param times - Number of times to write a space.
130 */
131 space(times?: number): this;
132 /**
133 * Writes a tab if the last character was not a tab.
134 */
135 tabIfLastNot(): this;
136 /**
137 * Writes a tab.
138 * @param times - Number of times to write a tab.
139 */
140 tab(times?: number): this;
141 /**
142 * Conditionally writes text.
143 * @param condition - Condition to evaluate.
144 * @param textFunc - A function that returns a string to write if the condition is true.
145 */
146 conditionalWrite(condition: boolean | undefined, textFunc: () => string): this;
147 /**
148 * Conditionally writes text.
149 * @param condition - Condition to evaluate.
150 * @param text - Text to write if the condition is true.
151 */
152 conditionalWrite(condition: boolean | undefined, text: string): this;
153 /**
154 * Writes the provided text.
155 * @param text - Text to write.
156 */
157 write(text: string): this;
158 /**
159 * Gets the length of the string in the writer.
160 */
161 getLength(): number;
162 /**
163 * Gets if the writer is currently in a comment.
164 */
165 isInComment(): boolean;
166 /**
167 * Gets if the writer is currently at the start of the first line of the text, block, or indentation block.
168 */
169 isAtStartOfFirstLineOfBlock(): boolean;
170 /**
171 * Gets if the writer is currently on the first line of the text, block, or indentation block.
172 */
173 isOnFirstLineOfBlock(): boolean;
174 /**
175 * Gets if the writer is currently in a string.
176 */
177 isInString(): boolean;
178 /**
179 * Gets if the last chars written were for a newline.
180 */
181 isLastNewLine(): boolean;
182 /**
183 * Gets if the last chars written were for a blank line.
184 */
185 isLastBlankLine(): boolean;
186 /**
187 * Gets if the last char written was a space.
188 */
189 isLastSpace(): boolean;
190 /**
191 * Gets if the last char written was a tab.
192 */
193 isLastTab(): boolean;
194 /**
195 * Gets the last char written.
196 */
197 getLastChar(): string | undefined;
198 /**
199 * Gets the writer's text.
200 */
201 toString(): string;
202 private _writeIndentingNewLines;
203 private _baseWriteNewline;
204 private dequeueQueuedIndentation;
205 private _updateInternalState;
206 private _writeIndentation;
207 private _newLineIfNewLineOnNextWrite;
208 private _getIndentationLevelFromArg;
209}
210export { CodeBlockWriter, CodeBlockWriterOptions };