@tslite/printer 1.15.0 → 1.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,3164 @@
1
- // O namespace `ts` do outFile da tag, estendido com o printer: a parse tree inteira, e depois o modelo do printer e dos
2
- // writers, os writers e o printer.
1
+ // Modificado de microsoft/TypeScript v3.9.10 (Apache-2.0): o printer de src/compiler da tag, convertido e cortado até a Linguagem do TSLite. Ver o NOTICE.
2
+
3
+ // src/_namespaces/ts.ts
3
4
  export * from "@tslite/parser";
4
- export * from "../types.js";
5
- export * from "../utilities.js";
6
- export * from "../emitter.js";
5
+
6
+ // src/types.ts
7
+ var ListFormat = /* @__PURE__ */ ((ListFormat2) => {
8
+ ListFormat2[ListFormat2["None"] = 0] = "None";
9
+ ListFormat2[ListFormat2["SingleLine"] = 0] = "SingleLine";
10
+ ListFormat2[ListFormat2["MultiLine"] = 1] = "MultiLine";
11
+ ListFormat2[ListFormat2["PreserveLines"] = 2] = "PreserveLines";
12
+ ListFormat2[ListFormat2["LinesMask"] = 3] = "LinesMask";
13
+ ListFormat2[ListFormat2["NotDelimited"] = 0] = "NotDelimited";
14
+ ListFormat2[ListFormat2["BarDelimited"] = 4] = "BarDelimited";
15
+ ListFormat2[ListFormat2["AmpersandDelimited"] = 8] = "AmpersandDelimited";
16
+ ListFormat2[ListFormat2["CommaDelimited"] = 16] = "CommaDelimited";
17
+ ListFormat2[ListFormat2["DelimitersMask"] = 28] = "DelimitersMask";
18
+ ListFormat2[ListFormat2["AllowTrailingComma"] = 64] = "AllowTrailingComma";
19
+ ListFormat2[ListFormat2["Indented"] = 128] = "Indented";
20
+ ListFormat2[ListFormat2["SpaceBetweenBraces"] = 256] = "SpaceBetweenBraces";
21
+ ListFormat2[ListFormat2["SpaceBetweenSiblings"] = 512] = "SpaceBetweenSiblings";
22
+ ListFormat2[ListFormat2["Braces"] = 1024] = "Braces";
23
+ ListFormat2[ListFormat2["Parenthesis"] = 2048] = "Parenthesis";
24
+ ListFormat2[ListFormat2["AngleBrackets"] = 4096] = "AngleBrackets";
25
+ ListFormat2[ListFormat2["SquareBrackets"] = 8192] = "SquareBrackets";
26
+ ListFormat2[ListFormat2["BracketsMask"] = 15360] = "BracketsMask";
27
+ ListFormat2[ListFormat2["OptionalIfUndefined"] = 16384] = "OptionalIfUndefined";
28
+ ListFormat2[ListFormat2["OptionalIfEmpty"] = 32768] = "OptionalIfEmpty";
29
+ ListFormat2[ListFormat2["Optional"] = 49152] = "Optional";
30
+ ListFormat2[ListFormat2["PreferNewLine"] = 65536] = "PreferNewLine";
31
+ ListFormat2[ListFormat2["NoTrailingNewLine"] = 131072] = "NoTrailingNewLine";
32
+ ListFormat2[ListFormat2["NoInterveningComments"] = 262144] = "NoInterveningComments";
33
+ ListFormat2[ListFormat2["NoSpaceIfEmpty"] = 524288] = "NoSpaceIfEmpty";
34
+ ListFormat2[ListFormat2["SingleElement"] = 1048576] = "SingleElement";
35
+ ListFormat2[ListFormat2["SpaceAfterList"] = 2097152] = "SpaceAfterList";
36
+ ListFormat2[ListFormat2["Modifiers"] = 262656] = "Modifiers";
37
+ ListFormat2[ListFormat2["SingleLineTypeLiteralMembers"] = 768] = "SingleLineTypeLiteralMembers";
38
+ ListFormat2[ListFormat2["MultiLineTypeLiteralMembers"] = 32897] = "MultiLineTypeLiteralMembers";
39
+ ListFormat2[ListFormat2["TupleTypeElements"] = 528] = "TupleTypeElements";
40
+ ListFormat2[ListFormat2["UnionTypeConstituents"] = 516] = "UnionTypeConstituents";
41
+ ListFormat2[ListFormat2["IntersectionTypeConstituents"] = 520] = "IntersectionTypeConstituents";
42
+ ListFormat2[ListFormat2["ObjectBindingPatternElements"] = 525136] = "ObjectBindingPatternElements";
43
+ ListFormat2[ListFormat2["ArrayBindingPatternElements"] = 524880] = "ArrayBindingPatternElements";
44
+ ListFormat2[ListFormat2["ObjectLiteralExpressionProperties"] = 526226] = "ObjectLiteralExpressionProperties";
45
+ ListFormat2[ListFormat2["ArrayLiteralExpressionElements"] = 8914] = "ArrayLiteralExpressionElements";
46
+ ListFormat2[ListFormat2["CommaListElements"] = 528] = "CommaListElements";
47
+ ListFormat2[ListFormat2["CallExpressionArguments"] = 2576] = "CallExpressionArguments";
48
+ ListFormat2[ListFormat2["TemplateExpressionSpans"] = 262144] = "TemplateExpressionSpans";
49
+ ListFormat2[ListFormat2["SingleLineBlockStatements"] = 768] = "SingleLineBlockStatements";
50
+ ListFormat2[ListFormat2["MultiLineBlockStatements"] = 129] = "MultiLineBlockStatements";
51
+ ListFormat2[ListFormat2["VariableDeclarationList"] = 528] = "VariableDeclarationList";
52
+ ListFormat2[ListFormat2["SingleLineFunctionBodyStatements"] = 768] = "SingleLineFunctionBodyStatements";
53
+ ListFormat2[ListFormat2["MultiLineFunctionBodyStatements"] = 1 /* MultiLine */] = "MultiLineFunctionBodyStatements";
54
+ ListFormat2[ListFormat2["CaseBlockClauses"] = 129] = "CaseBlockClauses";
55
+ ListFormat2[ListFormat2["NamedImportsOrExportsElements"] = 525136] = "NamedImportsOrExportsElements";
56
+ ListFormat2[ListFormat2["JsxElementOrFragmentChildren"] = 262144] = "JsxElementOrFragmentChildren";
57
+ ListFormat2[ListFormat2["JsxElementAttributes"] = 262656] = "JsxElementAttributes";
58
+ ListFormat2[ListFormat2["CaseOrDefaultClauseStatements"] = 163969] = "CaseOrDefaultClauseStatements";
59
+ ListFormat2[ListFormat2["SourceFileStatements"] = 131073] = "SourceFileStatements";
60
+ ListFormat2[ListFormat2["TypeArguments"] = 53776] = "TypeArguments";
61
+ ListFormat2[ListFormat2["TypeParameters"] = 53776] = "TypeParameters";
62
+ ListFormat2[ListFormat2["Parameters"] = 2576] = "Parameters";
63
+ ListFormat2[ListFormat2["IndexSignatureParameters"] = 8848] = "IndexSignatureParameters";
64
+ return ListFormat2;
65
+ })(ListFormat || {});
66
+
67
+ // src/utilities.ts
68
+ import {
69
+ CharacterCodes,
70
+ NewLineKind,
71
+ computeLineAndCharacterOfPosition,
72
+ computeLineStarts,
73
+ filter,
74
+ getIndentSize,
75
+ getIndentString,
76
+ getLeadingCommentRanges,
77
+ getLineOfLocalPositionFromLineMap,
78
+ isPinnedComment,
79
+ isWhiteSpaceLike,
80
+ isWhiteSpaceSingleLine,
81
+ last,
82
+ noop,
83
+ skipTrivia
84
+ } from "@tslite/parser";
85
+ var stringWriter = createSingleLineStringWriter();
86
+ function createSingleLineStringWriter() {
87
+ let str = "";
88
+ const writeText = (text) => str += text;
89
+ return {
90
+ getText: () => str,
91
+ write: writeText,
92
+ rawWrite: writeText,
93
+ writeKeyword: writeText,
94
+ writeOperator: writeText,
95
+ writePunctuation: writeText,
96
+ writeSpace: writeText,
97
+ writeStringLiteral: writeText,
98
+ writeLiteral: writeText,
99
+ writeParameter: writeText,
100
+ writeProperty: writeText,
101
+ writeSymbol: (s, _) => writeText(s),
102
+ writeTrailingSemicolon: writeText,
103
+ writeComment: writeText,
104
+ getTextPos: () => str.length,
105
+ getLine: () => 0,
106
+ getColumn: () => 0,
107
+ getIndent: () => 0,
108
+ isAtStartOfLine: () => false,
109
+ hasTrailingComment: () => false,
110
+ hasTrailingWhitespace: () => !!str.length && isWhiteSpaceLike(str.charCodeAt(str.length - 1)),
111
+ // Completely ignore indentation for string writers. And map newlines to
112
+ // a single space.
113
+ writeLine: () => str += " ",
114
+ increaseIndent: noop,
115
+ decreaseIndent: noop,
116
+ clear: () => str = "",
117
+ trackSymbol: noop,
118
+ reportInaccessibleThisError: noop,
119
+ reportInaccessibleUniqueSymbolError: noop
120
+ };
121
+ }
122
+ function usingSingleLineStringWriter(action) {
123
+ const oldString = stringWriter.getText();
124
+ try {
125
+ action(stringWriter);
126
+ return stringWriter.getText();
127
+ } finally {
128
+ stringWriter.clear();
129
+ stringWriter.writeKeyword(oldString);
130
+ }
131
+ }
132
+ function isFileLevelUniqueName(sourceFile, name, hasGlobalName) {
133
+ return !(hasGlobalName && hasGlobalName(name)) && !sourceFile.identifiers.has(name);
134
+ }
135
+ function createTextWriter(newLine) {
136
+ let output;
137
+ let indent;
138
+ let lineStart;
139
+ let lineCount;
140
+ let linePos;
141
+ let hasTrailingComment = false;
142
+ function updateLineCountAndPosFor(s) {
143
+ const lineStartsOfS = computeLineStarts(s);
144
+ if (lineStartsOfS.length > 1) {
145
+ lineCount = lineCount + lineStartsOfS.length - 1;
146
+ linePos = output.length - s.length + last(lineStartsOfS);
147
+ lineStart = linePos - output.length === 0;
148
+ } else {
149
+ lineStart = false;
150
+ }
151
+ }
152
+ function writeText(s) {
153
+ if (s && s.length) {
154
+ if (lineStart) {
155
+ s = getIndentString(indent) + s;
156
+ lineStart = false;
157
+ }
158
+ output += s;
159
+ updateLineCountAndPosFor(s);
160
+ }
161
+ }
162
+ function write(s) {
163
+ if (s) hasTrailingComment = false;
164
+ writeText(s);
165
+ }
166
+ function writeComment(s) {
167
+ if (s) hasTrailingComment = true;
168
+ writeText(s);
169
+ }
170
+ function reset() {
171
+ output = "";
172
+ indent = 0;
173
+ lineStart = true;
174
+ lineCount = 0;
175
+ linePos = 0;
176
+ hasTrailingComment = false;
177
+ }
178
+ function rawWrite(s) {
179
+ if (s !== void 0) {
180
+ output += s;
181
+ updateLineCountAndPosFor(s);
182
+ hasTrailingComment = false;
183
+ }
184
+ }
185
+ function writeLiteral(s) {
186
+ if (s && s.length) {
187
+ write(s);
188
+ }
189
+ }
190
+ function writeLine(force) {
191
+ if (!lineStart || force) {
192
+ output += newLine;
193
+ lineCount++;
194
+ linePos = output.length;
195
+ lineStart = true;
196
+ hasTrailingComment = false;
197
+ }
198
+ }
199
+ function getTextPosWithWriteLine() {
200
+ return lineStart ? output.length : output.length + newLine.length;
201
+ }
202
+ reset();
203
+ return {
204
+ write,
205
+ rawWrite,
206
+ writeLiteral,
207
+ writeLine,
208
+ increaseIndent: () => {
209
+ indent++;
210
+ },
211
+ decreaseIndent: () => {
212
+ indent--;
213
+ },
214
+ getIndent: () => indent,
215
+ getTextPos: () => output.length,
216
+ getLine: () => lineCount,
217
+ getColumn: () => lineStart ? indent * getIndentSize() : output.length - linePos,
218
+ getText: () => output,
219
+ isAtStartOfLine: () => lineStart,
220
+ hasTrailingComment: () => hasTrailingComment,
221
+ hasTrailingWhitespace: () => !!output.length && isWhiteSpaceLike(output.charCodeAt(output.length - 1)),
222
+ clear: reset,
223
+ reportInaccessibleThisError: noop,
224
+ reportInaccessibleUniqueSymbolError: noop,
225
+ trackSymbol: noop,
226
+ writeKeyword: write,
227
+ writeOperator: write,
228
+ writeParameter: write,
229
+ writeProperty: write,
230
+ writePunctuation: write,
231
+ writeSpace: write,
232
+ writeStringLiteral: write,
233
+ writeSymbol: (s, _) => write(s),
234
+ writeTrailingSemicolon: write,
235
+ writeComment,
236
+ getTextPosWithWriteLine
237
+ };
238
+ }
239
+ function getTrailingSemicolonDeferringWriter(writer) {
240
+ let pendingTrailingSemicolon = false;
241
+ function commitPendingTrailingSemicolon() {
242
+ if (pendingTrailingSemicolon) {
243
+ writer.writeTrailingSemicolon(";");
244
+ pendingTrailingSemicolon = false;
245
+ }
246
+ }
247
+ return {
248
+ ...writer,
249
+ writeTrailingSemicolon() {
250
+ pendingTrailingSemicolon = true;
251
+ },
252
+ writeLiteral(s) {
253
+ commitPendingTrailingSemicolon();
254
+ writer.writeLiteral(s);
255
+ },
256
+ writeStringLiteral(s) {
257
+ commitPendingTrailingSemicolon();
258
+ writer.writeStringLiteral(s);
259
+ },
260
+ writeSymbol(s, sym) {
261
+ commitPendingTrailingSemicolon();
262
+ writer.writeSymbol(s, sym);
263
+ },
264
+ writePunctuation(s) {
265
+ commitPendingTrailingSemicolon();
266
+ writer.writePunctuation(s);
267
+ },
268
+ writeKeyword(s) {
269
+ commitPendingTrailingSemicolon();
270
+ writer.writeKeyword(s);
271
+ },
272
+ writeOperator(s) {
273
+ commitPendingTrailingSemicolon();
274
+ writer.writeOperator(s);
275
+ },
276
+ writeParameter(s) {
277
+ commitPendingTrailingSemicolon();
278
+ writer.writeParameter(s);
279
+ },
280
+ writeSpace(s) {
281
+ commitPendingTrailingSemicolon();
282
+ writer.writeSpace(s);
283
+ },
284
+ writeProperty(s) {
285
+ commitPendingTrailingSemicolon();
286
+ writer.writeProperty(s);
287
+ },
288
+ writeComment(s) {
289
+ commitPendingTrailingSemicolon();
290
+ writer.writeComment(s);
291
+ },
292
+ writeLine() {
293
+ commitPendingTrailingSemicolon();
294
+ writer.writeLine();
295
+ },
296
+ increaseIndent() {
297
+ commitPendingTrailingSemicolon();
298
+ writer.increaseIndent();
299
+ },
300
+ decreaseIndent() {
301
+ commitPendingTrailingSemicolon();
302
+ writer.decreaseIndent();
303
+ }
304
+ };
305
+ }
306
+ function emitNewLineBeforeLeadingComments(lineMap, writer, node, leadingComments) {
307
+ emitNewLineBeforeLeadingCommentsOfPosition(lineMap, writer, node.pos, leadingComments);
308
+ }
309
+ function emitNewLineBeforeLeadingCommentsOfPosition(lineMap, writer, pos, leadingComments) {
310
+ if (leadingComments && leadingComments.length && pos !== leadingComments[0].pos && getLineOfLocalPositionFromLineMap(lineMap, pos) !== getLineOfLocalPositionFromLineMap(lineMap, leadingComments[0].pos)) {
311
+ writer.writeLine();
312
+ }
313
+ }
314
+ function emitNewLineBeforeLeadingCommentOfPosition(lineMap, writer, pos, commentPos) {
315
+ if (pos !== commentPos && getLineOfLocalPositionFromLineMap(lineMap, pos) !== getLineOfLocalPositionFromLineMap(lineMap, commentPos)) {
316
+ writer.writeLine();
317
+ }
318
+ }
319
+ function emitComments(text, lineMap, writer, comments, leadingSeparator, trailingSeparator, newLine, writeComment) {
320
+ if (comments && comments.length > 0) {
321
+ if (leadingSeparator) {
322
+ writer.writeSpace(" ");
323
+ }
324
+ let emitInterveningSeparator = false;
325
+ for (const comment of comments) {
326
+ if (emitInterveningSeparator) {
327
+ writer.writeSpace(" ");
328
+ emitInterveningSeparator = false;
329
+ }
330
+ writeComment(text, lineMap, writer, comment.pos, comment.end, newLine);
331
+ if (comment.hasTrailingNewLine) {
332
+ writer.writeLine();
333
+ } else {
334
+ emitInterveningSeparator = true;
335
+ }
336
+ }
337
+ if (emitInterveningSeparator && trailingSeparator) {
338
+ writer.writeSpace(" ");
339
+ }
340
+ }
341
+ }
342
+ function emitDetachedComments(text, lineMap, writer, writeComment, node, newLine, removeComments) {
343
+ let leadingComments;
344
+ let currentDetachedCommentInfo;
345
+ if (removeComments) {
346
+ if (node.pos === 0) {
347
+ leadingComments = filter(getLeadingCommentRanges(text, node.pos), isPinnedCommentLocal);
348
+ }
349
+ } else {
350
+ leadingComments = getLeadingCommentRanges(text, node.pos);
351
+ }
352
+ if (leadingComments) {
353
+ const detachedComments = [];
354
+ let lastComment;
355
+ for (const comment of leadingComments) {
356
+ if (lastComment) {
357
+ const lastCommentLine = getLineOfLocalPositionFromLineMap(lineMap, lastComment.end);
358
+ const commentLine = getLineOfLocalPositionFromLineMap(lineMap, comment.pos);
359
+ if (commentLine >= lastCommentLine + 2) {
360
+ break;
361
+ }
362
+ }
363
+ detachedComments.push(comment);
364
+ lastComment = comment;
365
+ }
366
+ if (detachedComments.length) {
367
+ const lastCommentLine = getLineOfLocalPositionFromLineMap(lineMap, last(detachedComments).end);
368
+ const nodeLine = getLineOfLocalPositionFromLineMap(lineMap, skipTrivia(text, node.pos));
369
+ if (nodeLine >= lastCommentLine + 2) {
370
+ emitNewLineBeforeLeadingComments(lineMap, writer, node, leadingComments);
371
+ emitComments(
372
+ text,
373
+ lineMap,
374
+ writer,
375
+ detachedComments,
376
+ /*leadingSeparator*/
377
+ false,
378
+ /*trailingSeparator*/
379
+ true,
380
+ newLine,
381
+ writeComment
382
+ );
383
+ currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: last(detachedComments).end };
384
+ }
385
+ }
386
+ }
387
+ return currentDetachedCommentInfo;
388
+ function isPinnedCommentLocal(comment) {
389
+ return isPinnedComment(text, comment.pos);
390
+ }
391
+ }
392
+ function writeCommentRange(text, lineMap, writer, commentPos, commentEnd, newLine) {
393
+ if (text.charCodeAt(commentPos + 1) === CharacterCodes.asterisk) {
394
+ const firstCommentLineAndCharacter = computeLineAndCharacterOfPosition(lineMap, commentPos);
395
+ const lineCount = lineMap.length;
396
+ let firstCommentLineIndent;
397
+ for (let pos = commentPos, currentLine = firstCommentLineAndCharacter.line; pos < commentEnd; currentLine++) {
398
+ const nextLineStart = currentLine + 1 === lineCount ? text.length + 1 : lineMap[currentLine + 1];
399
+ if (pos !== commentPos) {
400
+ if (firstCommentLineIndent === void 0) {
401
+ firstCommentLineIndent = calculateIndent(text, lineMap[firstCommentLineAndCharacter.line], commentPos);
402
+ }
403
+ const currentWriterIndentSpacing = writer.getIndent() * getIndentSize();
404
+ const spacesToEmit = currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(text, pos, nextLineStart);
405
+ if (spacesToEmit > 0) {
406
+ let numberOfSingleSpacesToEmit = spacesToEmit % getIndentSize();
407
+ const indentSizeSpaceString = getIndentString((spacesToEmit - numberOfSingleSpacesToEmit) / getIndentSize());
408
+ writer.rawWrite(indentSizeSpaceString);
409
+ while (numberOfSingleSpacesToEmit) {
410
+ writer.rawWrite(" ");
411
+ numberOfSingleSpacesToEmit--;
412
+ }
413
+ } else {
414
+ writer.rawWrite("");
415
+ }
416
+ }
417
+ writeTrimmedCurrentLine(text, commentEnd, writer, newLine, pos, nextLineStart);
418
+ pos = nextLineStart;
419
+ }
420
+ } else {
421
+ writer.writeComment(text.substring(commentPos, commentEnd));
422
+ }
423
+ }
424
+ function writeTrimmedCurrentLine(text, commentEnd, writer, newLine, pos, nextLineStart) {
425
+ const end = Math.min(commentEnd, nextLineStart - 1);
426
+ const currentLineText = text.substring(pos, end).replace(/^\s+|\s+$/g, "");
427
+ if (currentLineText) {
428
+ writer.writeComment(currentLineText);
429
+ if (end !== commentEnd) {
430
+ writer.writeLine();
431
+ }
432
+ } else {
433
+ writer.rawWrite(newLine);
434
+ }
435
+ }
436
+ function calculateIndent(text, pos, end) {
437
+ let currentLineIndent = 0;
438
+ for (; pos < end && isWhiteSpaceSingleLine(text.charCodeAt(pos)); pos++) {
439
+ if (text.charCodeAt(pos) === CharacterCodes.tab) {
440
+ currentLineIndent += getIndentSize() - currentLineIndent % getIndentSize();
441
+ } else {
442
+ currentLineIndent++;
443
+ }
444
+ }
445
+ return currentLineIndent;
446
+ }
447
+ var carriageReturnLineFeed = "\r\n";
448
+ var lineFeed = "\n";
449
+ function getNewLineCharacter(options, getNewLine) {
450
+ switch (options.newLine) {
451
+ case NewLineKind.CarriageReturnLineFeed:
452
+ return carriageReturnLineFeed;
453
+ case NewLineKind.LineFeed:
454
+ return lineFeed;
455
+ }
456
+ return getNewLine ? getNewLine() : carriageReturnLineFeed;
457
+ }
458
+ function noEmitNotification(hint, node, callback) {
459
+ callback(hint, node);
460
+ }
461
+
462
+ // src/emitter.ts
463
+ import * as ts from "@tslite/printer";
464
+ import {
465
+ CharacterCodes as CharacterCodes2,
466
+ Debug,
467
+ EmitFlags,
468
+ EmitHint,
469
+ GeneratedIdentifierFlags,
470
+ NodeFlags,
471
+ ScriptTarget,
472
+ SymbolFlags,
473
+ SyntaxKind,
474
+ cast,
475
+ computeLineStarts as computeLineStarts2,
476
+ createMap,
477
+ createNode,
478
+ escapeJsxAttributeString,
479
+ escapeLeadingUnderscores,
480
+ escapeNonAsciiString,
481
+ escapeString,
482
+ findIndex,
483
+ forEach,
484
+ forEachLeadingCommentRange,
485
+ forEachTrailingCommentRange,
486
+ getCommentRange,
487
+ getEmitFlags,
488
+ getExternalModuleName,
489
+ getLineStarts,
490
+ getLinesBetweenPositionAndNextNonWhitespaceCharacter,
491
+ getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter,
492
+ getLinesBetweenRangeEndAndRangeStart,
493
+ getLiteralText,
494
+ getNodeId,
495
+ getOriginalNode,
496
+ getParseTreeNode,
497
+ getShebang,
498
+ getSourceFileOfNode,
499
+ getSourceTextOfNodeFromSourceFile,
500
+ getStartsOnNewLine,
501
+ getSyntheticLeadingComments,
502
+ getSyntheticTrailingComments,
503
+ idText,
504
+ isArrowFunction,
505
+ isBinaryExpression,
506
+ isBindingPattern,
507
+ isBlock,
508
+ isEmptyStatement,
509
+ isExpression,
510
+ isFunctionLike,
511
+ isGeneratedIdentifier,
512
+ isIdentifier,
513
+ isJsonSourceFile,
514
+ isJsxClosingElement,
515
+ isJsxOpeningElement,
516
+ isKeyword,
517
+ isLet,
518
+ isLiteralExpression,
519
+ isNodeDescendantOf,
520
+ isNumericLiteral,
521
+ isPrologueDirective,
522
+ isSourceFile,
523
+ isStringLiteral,
524
+ isTemplateLiteralKind,
525
+ isToken,
526
+ isTypeParameterDeclaration,
527
+ isVarConst,
528
+ last as last2,
529
+ lastOrUndefined,
530
+ makeIdentifierFromModuleName,
531
+ nodeIsSynthesized,
532
+ positionIsSynthesized,
533
+ positionsAreOnSameLine,
534
+ rangeEndIsOnSameLineAsRangeStart,
535
+ rangeEndPositionsAreOnSameLine,
536
+ rangeIsOnSingleLine,
537
+ rangeStartPositionsAreOnSameLine,
538
+ singleOrUndefined,
539
+ skipPartiallyEmittedExpressions,
540
+ skipTrivia as skipTrivia2,
541
+ some,
542
+ stringContains,
543
+ tokenToString
544
+ } from "@tslite/parser";
545
+ var syntheticParent = { pos: -1, end: -1 };
546
+ var brackets = createBracketsMap();
547
+ function createPrinter(printerOptions = {}, handlers = {}) {
548
+ const {
549
+ hasGlobalName,
550
+ onEmitNode = noEmitNotification,
551
+ isEmitNotificationEnabled,
552
+ onBeforeEmitNodeArray,
553
+ onAfterEmitNodeArray,
554
+ onBeforeEmitToken,
555
+ onAfterEmitToken
556
+ } = handlers;
557
+ const newLine = getNewLineCharacter(printerOptions);
558
+ let currentSourceFile;
559
+ let nodeIdToGeneratedName;
560
+ let autoGeneratedIdToGeneratedName;
561
+ let generatedNames;
562
+ let tempFlagsStack;
563
+ let tempFlags;
564
+ let reservedNamesStack;
565
+ let reservedNames;
566
+ let preserveSourceNewlines = printerOptions.preserveSourceNewlines;
567
+ let writer;
568
+ let ownWriter;
569
+ let write = writeBase;
570
+ let containerPos = -1;
571
+ let containerEnd = -1;
572
+ let declarationListContainerEnd = -1;
573
+ let currentLineMap;
574
+ let detachedCommentsInfo;
575
+ let hasWrittenComment = false;
576
+ let commentsDisabled = !!printerOptions.removeComments;
577
+ let lastNode;
578
+ reset();
579
+ return {
580
+ // public API
581
+ printNode,
582
+ printList,
583
+ printFile,
584
+ // internal API
585
+ writeNode,
586
+ writeList,
587
+ writeFile
588
+ };
589
+ function printNode(hint, node, sourceFile) {
590
+ switch (hint) {
591
+ case EmitHint.SourceFile:
592
+ Debug.assert(isSourceFile(node), "Expected a SourceFile node.");
593
+ break;
594
+ case EmitHint.IdentifierName:
595
+ Debug.assert(isIdentifier(node), "Expected an Identifier node.");
596
+ break;
597
+ case EmitHint.Expression:
598
+ Debug.assert(isExpression(node), "Expected an Expression node.");
599
+ break;
600
+ }
601
+ switch (node.kind) {
602
+ case SyntaxKind.SourceFile:
603
+ return printFile(node);
604
+ }
605
+ writeNode(hint, node, sourceFile, beginPrint());
606
+ return endPrint();
607
+ }
608
+ function printList(format, nodes, sourceFile) {
609
+ writeList(format, nodes, sourceFile, beginPrint());
610
+ return endPrint();
611
+ }
612
+ function printFile(sourceFile) {
613
+ writeFile(sourceFile, beginPrint());
614
+ return endPrint();
615
+ }
616
+ function writeNode(hint, node, sourceFile, output) {
617
+ const previousWriter = writer;
618
+ setWriter(output);
619
+ print(hint, node, sourceFile);
620
+ reset();
621
+ writer = previousWriter;
622
+ }
623
+ function writeList(format, nodes, sourceFile, output) {
624
+ const previousWriter = writer;
625
+ setWriter(output);
626
+ if (sourceFile) {
627
+ setSourceFile(sourceFile);
628
+ }
629
+ emitList(syntheticParent, nodes, format);
630
+ reset();
631
+ writer = previousWriter;
632
+ }
633
+ function writeFile(sourceFile, output) {
634
+ const previousWriter = writer;
635
+ setWriter(output);
636
+ emitShebangIfNeeded(sourceFile);
637
+ emitPrologueDirectivesIfNeeded(sourceFile);
638
+ print(EmitHint.SourceFile, sourceFile, sourceFile);
639
+ reset();
640
+ writer = previousWriter;
641
+ }
642
+ function beginPrint() {
643
+ return ownWriter || (ownWriter = createTextWriter(newLine));
644
+ }
645
+ function endPrint() {
646
+ const text = ownWriter.getText();
647
+ ownWriter.clear();
648
+ return text;
649
+ }
650
+ function print(hint, node, sourceFile) {
651
+ if (sourceFile) {
652
+ setSourceFile(sourceFile);
653
+ }
654
+ pipelineEmit(hint, node);
655
+ }
656
+ function setSourceFile(sourceFile) {
657
+ currentSourceFile = sourceFile;
658
+ currentLineMap = void 0;
659
+ detachedCommentsInfo = void 0;
660
+ }
661
+ function setWriter(_writer) {
662
+ if (_writer && printerOptions.omitTrailingSemicolon) {
663
+ _writer = getTrailingSemicolonDeferringWriter(_writer);
664
+ }
665
+ writer = _writer;
666
+ }
667
+ function reset() {
668
+ nodeIdToGeneratedName = [];
669
+ autoGeneratedIdToGeneratedName = [];
670
+ generatedNames = createMap();
671
+ tempFlagsStack = [];
672
+ tempFlags = 0 /* Auto */;
673
+ reservedNamesStack = [];
674
+ currentSourceFile = void 0;
675
+ currentLineMap = void 0;
676
+ detachedCommentsInfo = void 0;
677
+ lastNode = void 0;
678
+ setWriter(
679
+ /*output*/
680
+ void 0
681
+ );
682
+ }
683
+ function getCurrentLineMap() {
684
+ return currentLineMap || (currentLineMap = getLineStarts(currentSourceFile));
685
+ }
686
+ function emit(node) {
687
+ if (node === void 0) return;
688
+ return pipelineEmit(EmitHint.Unspecified, node);
689
+ }
690
+ function emitExpression(node) {
691
+ if (node === void 0) return;
692
+ return pipelineEmit(EmitHint.Expression, node);
693
+ }
694
+ function emitJsxAttributeValue(node) {
695
+ return pipelineEmit(isStringLiteral(node) ? EmitHint.JsxAttributeValue : EmitHint.Unspecified, node);
696
+ }
697
+ function pipelineEmit(emitHint, node) {
698
+ const savedLastNode = lastNode;
699
+ const savedPreserveSourceNewlines = preserveSourceNewlines;
700
+ lastNode = node;
701
+ if (preserveSourceNewlines && !!(getEmitFlags(node) & EmitFlags.IgnoreSourceNewlines)) {
702
+ preserveSourceNewlines = false;
703
+ }
704
+ const pipelinePhase = getPipelinePhase(0 /* Notification */, emitHint, node);
705
+ pipelinePhase(emitHint, node);
706
+ Debug.assert(lastNode === node);
707
+ lastNode = savedLastNode;
708
+ preserveSourceNewlines = savedPreserveSourceNewlines;
709
+ return node;
710
+ }
711
+ function getPipelinePhase(phase, _emitHint, node) {
712
+ switch (phase) {
713
+ case 0 /* Notification */:
714
+ if (onEmitNode !== noEmitNotification && (!isEmitNotificationEnabled || isEmitNotificationEnabled(node))) {
715
+ return pipelineEmitWithNotification;
716
+ }
717
+ // falls through
718
+ case 1 /* Comments */:
719
+ if (!commentsDisabled && node.kind !== SyntaxKind.SourceFile) {
720
+ return pipelineEmitWithComments;
721
+ }
722
+ // falls through
723
+ case 2 /* Emit */:
724
+ return pipelineEmitWithHint;
725
+ default:
726
+ return Debug.assertNever(phase);
727
+ }
728
+ }
729
+ function getNextPipelinePhase(currentPhase, emitHint, node) {
730
+ return getPipelinePhase(currentPhase + 1, emitHint, node);
731
+ }
732
+ function pipelineEmitWithNotification(hint, node) {
733
+ Debug.assert(lastNode === node);
734
+ const pipelinePhase = getNextPipelinePhase(0 /* Notification */, hint, node);
735
+ onEmitNode(hint, node, pipelinePhase);
736
+ Debug.assert(lastNode === node);
737
+ }
738
+ function pipelineEmitWithHint(hint, node) {
739
+ Debug.assert(lastNode === node);
740
+ if (hint === EmitHint.SourceFile) return emitSourceFile(cast(node, isSourceFile));
741
+ if (hint === EmitHint.IdentifierName) return emitIdentifier(cast(node, isIdentifier));
742
+ if (hint === EmitHint.JsxAttributeValue) return emitLiteral(
743
+ cast(node, isStringLiteral),
744
+ /*jsxAttributeEscape*/
745
+ true
746
+ );
747
+ if (hint === EmitHint.MappedTypeParameter) return emitMappedTypeParameter(cast(node, isTypeParameterDeclaration));
748
+ if (hint === EmitHint.EmbeddedStatement) {
749
+ Debug.assertNode(node, isEmptyStatement);
750
+ return emitEmptyStatement(
751
+ /*isEmbeddedStatement*/
752
+ true
753
+ );
754
+ }
755
+ if (hint === EmitHint.Unspecified) {
756
+ if (isKeyword(node.kind)) return writeTokenNode(node, writeKeyword);
757
+ switch (node.kind) {
758
+ // Pseudo-literals
759
+ case SyntaxKind.TemplateHead:
760
+ case SyntaxKind.TemplateMiddle:
761
+ case SyntaxKind.TemplateTail:
762
+ return emitLiteral(
763
+ node,
764
+ /*jsxAttributeEscape*/
765
+ false
766
+ );
767
+ // Identifiers
768
+ case SyntaxKind.Identifier:
769
+ return emitIdentifier(node);
770
+ // Parse tree nodes
771
+ // Names
772
+ case SyntaxKind.QualifiedName:
773
+ return emitQualifiedName(node);
774
+ case SyntaxKind.ComputedPropertyName:
775
+ return emitComputedPropertyName(node);
776
+ // Signature elements
777
+ case SyntaxKind.TypeParameter:
778
+ return emitTypeParameter(node);
779
+ case SyntaxKind.Parameter:
780
+ return emitParameter(node);
781
+ // Type members
782
+ case SyntaxKind.PropertySignature:
783
+ return emitPropertySignature(node);
784
+ case SyntaxKind.MethodSignature:
785
+ return emitMethodSignature(node);
786
+ case SyntaxKind.CallSignature:
787
+ return emitCallSignature(node);
788
+ case SyntaxKind.IndexSignature:
789
+ return emitIndexSignature(node);
790
+ // Types
791
+ case SyntaxKind.TypePredicate:
792
+ return emitTypePredicate(node);
793
+ case SyntaxKind.TypeReference:
794
+ return emitTypeReference(node);
795
+ case SyntaxKind.FunctionType:
796
+ return emitFunctionType(node);
797
+ case SyntaxKind.TypeQuery:
798
+ return emitTypeQuery(node);
799
+ case SyntaxKind.TypeLiteral:
800
+ return emitTypeLiteral(node);
801
+ case SyntaxKind.ArrayType:
802
+ return emitArrayType(node);
803
+ case SyntaxKind.TupleType:
804
+ return emitTupleType(node);
805
+ case SyntaxKind.OptionalType:
806
+ return emitOptionalType(node);
807
+ case SyntaxKind.UnionType:
808
+ return emitUnionType(node);
809
+ case SyntaxKind.IntersectionType:
810
+ return emitIntersectionType(node);
811
+ case SyntaxKind.ConditionalType:
812
+ return emitConditionalType(node);
813
+ case SyntaxKind.InferType:
814
+ return emitInferType(node);
815
+ case SyntaxKind.ParenthesizedType:
816
+ return emitParenthesizedType(node);
817
+ case SyntaxKind.TypeOperator:
818
+ return emitTypeOperator(node);
819
+ case SyntaxKind.IndexedAccessType:
820
+ return emitIndexedAccessType(node);
821
+ case SyntaxKind.MappedType:
822
+ return emitMappedType(node);
823
+ case SyntaxKind.LiteralType:
824
+ return emitLiteralType(node);
825
+ case SyntaxKind.ImportType:
826
+ return emitImportTypeNode(node);
827
+ case SyntaxKind.RestType:
828
+ return emitRestOrJSDocVariadicType(node);
829
+ // Binding patterns
830
+ case SyntaxKind.ObjectBindingPattern:
831
+ return emitObjectBindingPattern(node);
832
+ case SyntaxKind.ArrayBindingPattern:
833
+ return emitArrayBindingPattern(node);
834
+ case SyntaxKind.BindingElement:
835
+ return emitBindingElement(node);
836
+ // Misc
837
+ case SyntaxKind.TemplateSpan:
838
+ return emitTemplateSpan(node);
839
+ // Statements
840
+ case SyntaxKind.Block:
841
+ return emitBlock(node);
842
+ case SyntaxKind.VariableStatement:
843
+ return emitVariableStatement(node);
844
+ case SyntaxKind.EmptyStatement:
845
+ return emitEmptyStatement(
846
+ /*isEmbeddedStatement*/
847
+ false
848
+ );
849
+ case SyntaxKind.ExpressionStatement:
850
+ return emitExpressionStatement(node);
851
+ case SyntaxKind.IfStatement:
852
+ return emitIfStatement(node);
853
+ case SyntaxKind.DoStatement:
854
+ return emitDoStatement(node);
855
+ case SyntaxKind.WhileStatement:
856
+ return emitWhileStatement(node);
857
+ case SyntaxKind.ForStatement:
858
+ return emitForStatement(node);
859
+ case SyntaxKind.ForInStatement:
860
+ return emitForInStatement(node);
861
+ case SyntaxKind.ForOfStatement:
862
+ return emitForOfStatement(node);
863
+ case SyntaxKind.ContinueStatement:
864
+ return emitContinueStatement(node);
865
+ case SyntaxKind.BreakStatement:
866
+ return emitBreakStatement(node);
867
+ case SyntaxKind.ReturnStatement:
868
+ return emitReturnStatement(node);
869
+ case SyntaxKind.SwitchStatement:
870
+ return emitSwitchStatement(node);
871
+ case SyntaxKind.ThrowStatement:
872
+ return emitThrowStatement(node);
873
+ case SyntaxKind.TryStatement:
874
+ return emitTryStatement(node);
875
+ // Declarations
876
+ case SyntaxKind.VariableDeclaration:
877
+ return emitVariableDeclaration(node);
878
+ case SyntaxKind.VariableDeclarationList:
879
+ return emitVariableDeclarationList(node);
880
+ case SyntaxKind.TypeAliasDeclaration:
881
+ return emitTypeAliasDeclaration(node);
882
+ case SyntaxKind.ModuleDeclaration:
883
+ return emitModuleDeclaration(node);
884
+ case SyntaxKind.ModuleBlock:
885
+ return emitModuleBlock(node);
886
+ case SyntaxKind.CaseBlock:
887
+ return emitCaseBlock(node);
888
+ case SyntaxKind.ImportDeclaration:
889
+ return emitImportDeclaration(node);
890
+ case SyntaxKind.ImportClause:
891
+ return emitImportClause(node);
892
+ case SyntaxKind.NamespaceImport:
893
+ return emitNamespaceImport(node);
894
+ case SyntaxKind.NamespaceExport:
895
+ return emitNamespaceExport(node);
896
+ case SyntaxKind.NamedImports:
897
+ return emitNamedImports(node);
898
+ case SyntaxKind.ImportSpecifier:
899
+ return emitImportSpecifier(node);
900
+ case SyntaxKind.ExportAssignment:
901
+ return emitExportAssignment(node);
902
+ case SyntaxKind.ExportDeclaration:
903
+ return emitExportDeclaration(node);
904
+ case SyntaxKind.NamedExports:
905
+ return emitNamedExports(node);
906
+ case SyntaxKind.ExportSpecifier:
907
+ return emitExportSpecifier(node);
908
+ case SyntaxKind.MissingDeclaration:
909
+ return;
910
+ // JSX (non-expression)
911
+ case SyntaxKind.JsxText:
912
+ return emitJsxText(node);
913
+ case SyntaxKind.JsxOpeningElement:
914
+ case SyntaxKind.JsxOpeningFragment:
915
+ return emitJsxOpeningElementOrFragment(node);
916
+ case SyntaxKind.JsxClosingElement:
917
+ case SyntaxKind.JsxClosingFragment:
918
+ return emitJsxClosingElementOrFragment(node);
919
+ case SyntaxKind.JsxAttribute:
920
+ return emitJsxAttribute(node);
921
+ case SyntaxKind.JsxAttributes:
922
+ return emitJsxAttributes(node);
923
+ case SyntaxKind.JsxSpreadAttribute:
924
+ return emitJsxSpreadAttribute(node);
925
+ case SyntaxKind.JsxExpression:
926
+ return emitJsxExpression(node);
927
+ // Clauses
928
+ case SyntaxKind.CaseClause:
929
+ return emitCaseClause(node);
930
+ case SyntaxKind.DefaultClause:
931
+ return emitDefaultClause(node);
932
+ case SyntaxKind.CatchClause:
933
+ return emitCatchClause(node);
934
+ // Property assignments
935
+ case SyntaxKind.PropertyAssignment:
936
+ return emitPropertyAssignment(node);
937
+ case SyntaxKind.ShorthandPropertyAssignment:
938
+ return emitShorthandPropertyAssignment(node);
939
+ case SyntaxKind.SpreadAssignment:
940
+ return emitSpreadAssignment(node);
941
+ }
942
+ if (isExpression(node)) {
943
+ hint = EmitHint.Expression;
944
+ } else if (isToken(node)) {
945
+ return writeTokenNode(node, writePunctuation);
946
+ }
947
+ }
948
+ if (hint === EmitHint.Expression) {
949
+ switch (node.kind) {
950
+ // Literals
951
+ case SyntaxKind.NumericLiteral:
952
+ case SyntaxKind.BigIntLiteral:
953
+ return emitNumericOrBigIntLiteral(node);
954
+ case SyntaxKind.StringLiteral:
955
+ case SyntaxKind.NoSubstitutionTemplateLiteral:
956
+ return emitLiteral(
957
+ node,
958
+ /*jsxAttributeEscape*/
959
+ false
960
+ );
961
+ // Identifiers
962
+ case SyntaxKind.Identifier:
963
+ return emitIdentifier(node);
964
+ // Reserved words
965
+ case SyntaxKind.FalseKeyword:
966
+ case SyntaxKind.NullKeyword:
967
+ case SyntaxKind.TrueKeyword:
968
+ case SyntaxKind.ImportKeyword:
969
+ writeTokenNode(node, writeKeyword);
970
+ return;
971
+ // Expressions
972
+ case SyntaxKind.ArrayLiteralExpression:
973
+ return emitArrayLiteralExpression(node);
974
+ case SyntaxKind.ObjectLiteralExpression:
975
+ return emitObjectLiteralExpression(node);
976
+ case SyntaxKind.PropertyAccessExpression:
977
+ return emitPropertyAccessExpression(node);
978
+ case SyntaxKind.ElementAccessExpression:
979
+ return emitElementAccessExpression(node);
980
+ case SyntaxKind.CallExpression:
981
+ return emitCallExpression(node);
982
+ case SyntaxKind.ParenthesizedExpression:
983
+ return emitParenthesizedExpression(node);
984
+ case SyntaxKind.ArrowFunction:
985
+ return emitArrowFunction(node);
986
+ case SyntaxKind.DeleteExpression:
987
+ return emitDeleteExpression(node);
988
+ case SyntaxKind.TypeOfExpression:
989
+ return emitTypeOfExpression(node);
990
+ case SyntaxKind.AwaitExpression:
991
+ return emitAwaitExpression(node);
992
+ case SyntaxKind.PrefixUnaryExpression:
993
+ return emitPrefixUnaryExpression(node);
994
+ case SyntaxKind.PostfixUnaryExpression:
995
+ return emitPostfixUnaryExpression(node);
996
+ case SyntaxKind.BinaryExpression:
997
+ return emitBinaryExpression(node);
998
+ case SyntaxKind.ConditionalExpression:
999
+ return emitConditionalExpression(node);
1000
+ case SyntaxKind.TemplateExpression:
1001
+ return emitTemplateExpression(node);
1002
+ case SyntaxKind.SpreadElement:
1003
+ return emitSpreadExpression(node);
1004
+ case SyntaxKind.OmittedExpression:
1005
+ return;
1006
+ case SyntaxKind.AsExpression:
1007
+ return emitAsExpression(node);
1008
+ case SyntaxKind.MetaProperty:
1009
+ return emitMetaProperty(node);
1010
+ // JSX
1011
+ case SyntaxKind.JsxElement:
1012
+ return emitJsxElement(node);
1013
+ case SyntaxKind.JsxSelfClosingElement:
1014
+ return emitJsxSelfClosingElement(node);
1015
+ case SyntaxKind.JsxFragment:
1016
+ return emitJsxFragment(node);
1017
+ }
1018
+ }
1019
+ }
1020
+ function emitMappedTypeParameter(node) {
1021
+ emit(node.name);
1022
+ writeSpace();
1023
+ writeKeyword("in");
1024
+ writeSpace();
1025
+ emit(node.constraint);
1026
+ }
1027
+ function emitNumericOrBigIntLiteral(node) {
1028
+ emitLiteral(
1029
+ node,
1030
+ /*jsxAttributeEscape*/
1031
+ false
1032
+ );
1033
+ }
1034
+ function emitLiteral(node, jsxAttributeEscape) {
1035
+ const text = getLiteralTextOfNode(node, printerOptions.neverAsciiEscape, jsxAttributeEscape);
1036
+ if ((printerOptions.sourceMap || printerOptions.inlineSourceMap) && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
1037
+ writeLiteral(text);
1038
+ } else {
1039
+ writeStringLiteral(text);
1040
+ }
1041
+ }
1042
+ function emitIdentifier(node) {
1043
+ const writeText = node.symbol ? writeSymbol : write;
1044
+ writeText(getTextOfNode(
1045
+ node,
1046
+ /*includeTrivia*/
1047
+ false
1048
+ ), node.symbol);
1049
+ emitList(node, node.typeArguments, 53776 /* TypeParameters */);
1050
+ }
1051
+ function emitQualifiedName(node) {
1052
+ emitEntityName(node.left);
1053
+ writePunctuation(".");
1054
+ emit(node.right);
1055
+ }
1056
+ function emitEntityName(node) {
1057
+ if (node.kind === SyntaxKind.Identifier) {
1058
+ emitExpression(node);
1059
+ } else {
1060
+ emit(node);
1061
+ }
1062
+ }
1063
+ function emitComputedPropertyName(node) {
1064
+ writePunctuation("[");
1065
+ emitExpression(node.expression);
1066
+ writePunctuation("]");
1067
+ }
1068
+ function emitTypeParameter(node) {
1069
+ emit(node.name);
1070
+ if (node.constraint) {
1071
+ writeSpace();
1072
+ writeKeyword("extends");
1073
+ writeSpace();
1074
+ emit(node.constraint);
1075
+ }
1076
+ if (node.default) {
1077
+ writeSpace();
1078
+ writeOperator("=");
1079
+ writeSpace();
1080
+ emit(node.default);
1081
+ }
1082
+ }
1083
+ function emitParameter(node) {
1084
+ emitModifiers(node, node.modifiers);
1085
+ emit(node.dotDotDotToken);
1086
+ emitNodeWithWriter(node.name, writeParameter);
1087
+ emit(node.questionToken);
1088
+ emitTypeAnnotation(node.type);
1089
+ emitInitializer(node.initializer, node.type ? node.type.end : node.questionToken ? node.questionToken.end : node.name ? node.name.end : node.modifiers ? node.modifiers.end : node.pos, node);
1090
+ }
1091
+ function emitPropertySignature(node) {
1092
+ emitModifiers(node, node.modifiers);
1093
+ emitNodeWithWriter(node.name, writeProperty);
1094
+ emit(node.questionToken);
1095
+ emitTypeAnnotation(node.type);
1096
+ writeTrailingSemicolon();
1097
+ }
1098
+ function emitMethodSignature(node) {
1099
+ pushNameGenerationScope(node);
1100
+ emitModifiers(node, node.modifiers);
1101
+ emit(node.name);
1102
+ emit(node.questionToken);
1103
+ emitTypeParameters(node, node.typeParameters);
1104
+ emitParameters(node, node.parameters);
1105
+ emitTypeAnnotation(node.type);
1106
+ writeTrailingSemicolon();
1107
+ popNameGenerationScope(node);
1108
+ }
1109
+ function emitCallSignature(node) {
1110
+ pushNameGenerationScope(node);
1111
+ emitModifiers(node, node.modifiers);
1112
+ emitTypeParameters(node, node.typeParameters);
1113
+ emitParameters(node, node.parameters);
1114
+ emitTypeAnnotation(node.type);
1115
+ writeTrailingSemicolon();
1116
+ popNameGenerationScope(node);
1117
+ }
1118
+ function emitIndexSignature(node) {
1119
+ emitModifiers(node, node.modifiers);
1120
+ emitParametersForIndexSignature(node, node.parameters);
1121
+ emitTypeAnnotation(node.type);
1122
+ writeTrailingSemicolon();
1123
+ }
1124
+ function emitTypePredicate(node) {
1125
+ if (node.assertsModifier) {
1126
+ emit(node.assertsModifier);
1127
+ writeSpace();
1128
+ }
1129
+ emit(node.parameterName);
1130
+ if (node.type) {
1131
+ writeSpace();
1132
+ writeKeyword("is");
1133
+ writeSpace();
1134
+ emit(node.type);
1135
+ }
1136
+ }
1137
+ function emitTypeReference(node) {
1138
+ emit(node.typeName);
1139
+ emitTypeArguments(node, node.typeArguments);
1140
+ }
1141
+ function emitFunctionType(node) {
1142
+ pushNameGenerationScope(node);
1143
+ emitTypeParameters(node, node.typeParameters);
1144
+ emitParametersForArrow(node, node.parameters);
1145
+ writeSpace();
1146
+ writePunctuation("=>");
1147
+ writeSpace();
1148
+ emit(node.type);
1149
+ popNameGenerationScope(node);
1150
+ }
1151
+ function emitTypeQuery(node) {
1152
+ writeKeyword("typeof");
1153
+ writeSpace();
1154
+ emit(node.exprName);
1155
+ }
1156
+ function emitTypeLiteral(node) {
1157
+ writePunctuation("{");
1158
+ const flags = getEmitFlags(node) & EmitFlags.SingleLine ? 768 /* SingleLineTypeLiteralMembers */ : 32897 /* MultiLineTypeLiteralMembers */;
1159
+ emitList(node, node.members, flags | 524288 /* NoSpaceIfEmpty */);
1160
+ writePunctuation("}");
1161
+ }
1162
+ function emitArrayType(node) {
1163
+ emit(node.elementType);
1164
+ writePunctuation("[");
1165
+ writePunctuation("]");
1166
+ }
1167
+ function emitRestOrJSDocVariadicType(node) {
1168
+ writePunctuation("...");
1169
+ emit(node.type);
1170
+ }
1171
+ function emitTupleType(node) {
1172
+ writePunctuation("[");
1173
+ emitList(node, node.elementTypes, 528 /* TupleTypeElements */);
1174
+ writePunctuation("]");
1175
+ }
1176
+ function emitOptionalType(node) {
1177
+ emit(node.type);
1178
+ writePunctuation("?");
1179
+ }
1180
+ function emitUnionType(node) {
1181
+ emitList(node, node.types, 516 /* UnionTypeConstituents */);
1182
+ }
1183
+ function emitIntersectionType(node) {
1184
+ emitList(node, node.types, 520 /* IntersectionTypeConstituents */);
1185
+ }
1186
+ function emitConditionalType(node) {
1187
+ emit(node.checkType);
1188
+ writeSpace();
1189
+ writeKeyword("extends");
1190
+ writeSpace();
1191
+ emit(node.extendsType);
1192
+ writeSpace();
1193
+ writePunctuation("?");
1194
+ writeSpace();
1195
+ emit(node.trueType);
1196
+ writeSpace();
1197
+ writePunctuation(":");
1198
+ writeSpace();
1199
+ emit(node.falseType);
1200
+ }
1201
+ function emitInferType(node) {
1202
+ writeKeyword("infer");
1203
+ writeSpace();
1204
+ emit(node.typeParameter);
1205
+ }
1206
+ function emitParenthesizedType(node) {
1207
+ writePunctuation("(");
1208
+ emit(node.type);
1209
+ writePunctuation(")");
1210
+ }
1211
+ function emitTypeOperator(node) {
1212
+ writeTokenText(node.operator, writeKeyword);
1213
+ writeSpace();
1214
+ emit(node.type);
1215
+ }
1216
+ function emitIndexedAccessType(node) {
1217
+ emit(node.objectType);
1218
+ writePunctuation("[");
1219
+ emit(node.indexType);
1220
+ writePunctuation("]");
1221
+ }
1222
+ function emitMappedType(node) {
1223
+ const emitFlags = getEmitFlags(node);
1224
+ writePunctuation("{");
1225
+ if (emitFlags & EmitFlags.SingleLine) {
1226
+ writeSpace();
1227
+ } else {
1228
+ writeLine();
1229
+ increaseIndent();
1230
+ }
1231
+ if (node.readonlyToken) {
1232
+ emit(node.readonlyToken);
1233
+ if (node.readonlyToken.kind !== SyntaxKind.ReadonlyKeyword) {
1234
+ writeKeyword("readonly");
1235
+ }
1236
+ writeSpace();
1237
+ }
1238
+ writePunctuation("[");
1239
+ pipelineEmit(EmitHint.MappedTypeParameter, node.typeParameter);
1240
+ writePunctuation("]");
1241
+ if (node.questionToken) {
1242
+ emit(node.questionToken);
1243
+ if (node.questionToken.kind !== SyntaxKind.QuestionToken) {
1244
+ writePunctuation("?");
1245
+ }
1246
+ }
1247
+ writePunctuation(":");
1248
+ writeSpace();
1249
+ emit(node.type);
1250
+ writeTrailingSemicolon();
1251
+ if (emitFlags & EmitFlags.SingleLine) {
1252
+ writeSpace();
1253
+ } else {
1254
+ writeLine();
1255
+ decreaseIndent();
1256
+ }
1257
+ writePunctuation("}");
1258
+ }
1259
+ function emitLiteralType(node) {
1260
+ emitExpression(node.literal);
1261
+ }
1262
+ function emitImportTypeNode(node) {
1263
+ if (node.isTypeOf) {
1264
+ writeKeyword("typeof");
1265
+ writeSpace();
1266
+ }
1267
+ writeKeyword("import");
1268
+ writePunctuation("(");
1269
+ emit(node.argument);
1270
+ writePunctuation(")");
1271
+ if (node.qualifier) {
1272
+ writePunctuation(".");
1273
+ emit(node.qualifier);
1274
+ }
1275
+ emitTypeArguments(node, node.typeArguments);
1276
+ }
1277
+ function emitObjectBindingPattern(node) {
1278
+ writePunctuation("{");
1279
+ emitList(node, node.elements, 525136 /* ObjectBindingPatternElements */);
1280
+ writePunctuation("}");
1281
+ }
1282
+ function emitArrayBindingPattern(node) {
1283
+ writePunctuation("[");
1284
+ emitList(node, node.elements, 524880 /* ArrayBindingPatternElements */);
1285
+ writePunctuation("]");
1286
+ }
1287
+ function emitBindingElement(node) {
1288
+ emit(node.dotDotDotToken);
1289
+ if (node.propertyName) {
1290
+ emit(node.propertyName);
1291
+ writePunctuation(":");
1292
+ writeSpace();
1293
+ }
1294
+ emit(node.name);
1295
+ emitInitializer(node.initializer, node.name.end, node);
1296
+ }
1297
+ function emitArrayLiteralExpression(node) {
1298
+ const elements = node.elements;
1299
+ const preferNewLine = node.multiLine ? 65536 /* PreferNewLine */ : 0 /* None */;
1300
+ emitExpressionList(node, elements, 8914 /* ArrayLiteralExpressionElements */ | preferNewLine);
1301
+ }
1302
+ function emitObjectLiteralExpression(node) {
1303
+ forEach(node.properties, generateMemberNames);
1304
+ const indentedFlag = getEmitFlags(node) & EmitFlags.Indented;
1305
+ if (indentedFlag) {
1306
+ increaseIndent();
1307
+ }
1308
+ const preferNewLine = node.multiLine ? 65536 /* PreferNewLine */ : 0 /* None */;
1309
+ const allowTrailingComma = currentSourceFile.languageVersion >= ScriptTarget.ES5 && !isJsonSourceFile(currentSourceFile) ? 64 /* AllowTrailingComma */ : 0 /* None */;
1310
+ emitList(node, node.properties, 526226 /* ObjectLiteralExpressionProperties */ | allowTrailingComma | preferNewLine);
1311
+ if (indentedFlag) {
1312
+ decreaseIndent();
1313
+ }
1314
+ }
1315
+ function emitPropertyAccessExpression(node) {
1316
+ const expression = cast(emitExpression(node.expression), isExpression);
1317
+ const token = node.questionDotToken || createNode(SyntaxKind.DotToken, node.expression.end, node.name.pos);
1318
+ const linesBeforeDot = getLinesBetweenNodes(node, node.expression, token);
1319
+ const linesAfterDot = getLinesBetweenNodes(node, token, node.name);
1320
+ writeLinesAndIndent(
1321
+ linesBeforeDot,
1322
+ /*writeSpaceIfNotIndenting*/
1323
+ false
1324
+ );
1325
+ const shouldEmitDotDot = token.kind !== SyntaxKind.QuestionDotToken && mayNeedDotDotForPropertyAccess(expression) && !writer.hasTrailingComment() && !writer.hasTrailingWhitespace();
1326
+ if (shouldEmitDotDot) {
1327
+ writePunctuation(".");
1328
+ }
1329
+ if (node.questionDotToken) {
1330
+ emit(token);
1331
+ } else {
1332
+ emitTokenWithComment(token.kind, node.expression.end, writePunctuation, node);
1333
+ }
1334
+ writeLinesAndIndent(
1335
+ linesAfterDot,
1336
+ /*writeSpaceIfNotIndenting*/
1337
+ false
1338
+ );
1339
+ emit(node.name);
1340
+ decreaseIndentIf(linesBeforeDot, linesAfterDot);
1341
+ }
1342
+ function mayNeedDotDotForPropertyAccess(expression) {
1343
+ expression = skipPartiallyEmittedExpressions(expression);
1344
+ if (isNumericLiteral(expression)) {
1345
+ const text = getLiteralTextOfNode(
1346
+ expression,
1347
+ /*neverAsciiEscape*/
1348
+ true,
1349
+ /*jsxAttributeEscape*/
1350
+ false
1351
+ );
1352
+ return !expression.numericLiteralFlags && !stringContains(text, tokenToString(SyntaxKind.DotToken));
1353
+ }
1354
+ }
1355
+ function emitElementAccessExpression(node) {
1356
+ emitExpression(node.expression);
1357
+ emit(node.questionDotToken);
1358
+ emitTokenWithComment(SyntaxKind.OpenBracketToken, node.expression.end, writePunctuation, node);
1359
+ emitExpression(node.argumentExpression);
1360
+ emitTokenWithComment(SyntaxKind.CloseBracketToken, node.argumentExpression.end, writePunctuation, node);
1361
+ }
1362
+ function emitCallExpression(node) {
1363
+ emitExpression(node.expression);
1364
+ emit(node.questionDotToken);
1365
+ emitTypeArguments(node, node.typeArguments);
1366
+ emitExpressionList(node, node.arguments, 2576 /* CallExpressionArguments */);
1367
+ }
1368
+ function emitParenthesizedExpression(node) {
1369
+ const openParenPos = emitTokenWithComment(SyntaxKind.OpenParenToken, node.pos, writePunctuation, node);
1370
+ const indented = writeLineSeparatorsAndIndentBefore(node.expression, node);
1371
+ emitExpression(node.expression);
1372
+ writeLineSeparatorsAfter(node.expression, node);
1373
+ decreaseIndentIf(indented);
1374
+ emitTokenWithComment(SyntaxKind.CloseParenToken, node.expression ? node.expression.end : openParenPos, writePunctuation, node);
1375
+ }
1376
+ function emitArrowFunction(node) {
1377
+ emitModifiers(node, node.modifiers);
1378
+ emitSignatureAndBody(node, emitArrowFunctionHead);
1379
+ }
1380
+ function emitArrowFunctionHead(node) {
1381
+ emitTypeParameters(node, node.typeParameters);
1382
+ emitParametersForArrow(node, node.parameters);
1383
+ emitTypeAnnotation(node.type);
1384
+ writeSpace();
1385
+ emit(node.equalsGreaterThanToken);
1386
+ }
1387
+ function emitDeleteExpression(node) {
1388
+ emitTokenWithComment(SyntaxKind.DeleteKeyword, node.pos, writeKeyword, node);
1389
+ writeSpace();
1390
+ emitExpression(node.expression);
1391
+ }
1392
+ function emitTypeOfExpression(node) {
1393
+ emitTokenWithComment(SyntaxKind.TypeOfKeyword, node.pos, writeKeyword, node);
1394
+ writeSpace();
1395
+ emitExpression(node.expression);
1396
+ }
1397
+ function emitAwaitExpression(node) {
1398
+ emitTokenWithComment(SyntaxKind.AwaitKeyword, node.pos, writeKeyword, node);
1399
+ writeSpace();
1400
+ emitExpression(node.expression);
1401
+ }
1402
+ function emitPrefixUnaryExpression(node) {
1403
+ writeTokenText(node.operator, writeOperator);
1404
+ if (shouldEmitWhitespaceBeforeOperand(node)) {
1405
+ writeSpace();
1406
+ }
1407
+ emitExpression(node.operand);
1408
+ }
1409
+ function shouldEmitWhitespaceBeforeOperand(node) {
1410
+ const operand = node.operand;
1411
+ return operand.kind === SyntaxKind.PrefixUnaryExpression && (node.operator === SyntaxKind.PlusToken && (operand.operator === SyntaxKind.PlusToken || operand.operator === SyntaxKind.PlusPlusToken) || node.operator === SyntaxKind.MinusToken && (operand.operator === SyntaxKind.MinusToken || operand.operator === SyntaxKind.MinusMinusToken));
1412
+ }
1413
+ function emitPostfixUnaryExpression(node) {
1414
+ emitExpression(node.operand);
1415
+ writeTokenText(node.operator, writeOperator);
1416
+ }
1417
+ let EmitBinaryExpressionState;
1418
+ ((EmitBinaryExpressionState2) => {
1419
+ EmitBinaryExpressionState2[EmitBinaryExpressionState2["EmitLeft"] = 0] = "EmitLeft";
1420
+ EmitBinaryExpressionState2[EmitBinaryExpressionState2["EmitRight"] = 1] = "EmitRight";
1421
+ EmitBinaryExpressionState2[EmitBinaryExpressionState2["FinishEmit"] = 2] = "FinishEmit";
1422
+ })(EmitBinaryExpressionState || (EmitBinaryExpressionState = {}));
1423
+ function emitBinaryExpression(node) {
1424
+ const nodeStack = [node];
1425
+ const stateStack = [0 /* EmitLeft */];
1426
+ let stackIndex = 0;
1427
+ while (stackIndex >= 0) {
1428
+ node = nodeStack[stackIndex];
1429
+ switch (stateStack[stackIndex]) {
1430
+ case 0 /* EmitLeft */: {
1431
+ maybePipelineEmitExpression(node.left);
1432
+ break;
1433
+ }
1434
+ case 1 /* EmitRight */: {
1435
+ const isCommaOperator = node.operatorToken.kind !== SyntaxKind.CommaToken;
1436
+ const linesBeforeOperator = getLinesBetweenNodes(node, node.left, node.operatorToken);
1437
+ const linesAfterOperator = getLinesBetweenNodes(node, node.operatorToken, node.right);
1438
+ writeLinesAndIndent(linesBeforeOperator, isCommaOperator);
1439
+ emitLeadingCommentsOfPosition(node.operatorToken.pos);
1440
+ writeTokenNode(node.operatorToken, node.operatorToken.kind === SyntaxKind.InKeyword ? writeKeyword : writeOperator);
1441
+ emitTrailingCommentsOfPosition(
1442
+ node.operatorToken.end,
1443
+ /*prefixSpace*/
1444
+ true
1445
+ );
1446
+ writeLinesAndIndent(
1447
+ linesAfterOperator,
1448
+ /*writeSpaceIfNotIndenting*/
1449
+ true
1450
+ );
1451
+ maybePipelineEmitExpression(node.right);
1452
+ break;
1453
+ }
1454
+ case 2 /* FinishEmit */: {
1455
+ const linesBeforeOperator = getLinesBetweenNodes(node, node.left, node.operatorToken);
1456
+ const linesAfterOperator = getLinesBetweenNodes(node, node.operatorToken, node.right);
1457
+ decreaseIndentIf(linesBeforeOperator, linesAfterOperator);
1458
+ stackIndex--;
1459
+ break;
1460
+ }
1461
+ default:
1462
+ return Debug.fail(`Invalid state ${stateStack[stackIndex]} for emitBinaryExpressionWorker`);
1463
+ }
1464
+ }
1465
+ function maybePipelineEmitExpression(next) {
1466
+ stateStack[stackIndex]++;
1467
+ const savedLastNode = lastNode;
1468
+ lastNode = next;
1469
+ const pipelinePhase = getPipelinePhase(0 /* Notification */, EmitHint.Expression, next);
1470
+ if (pipelinePhase === pipelineEmitWithHint && isBinaryExpression(next)) {
1471
+ stackIndex++;
1472
+ stateStack[stackIndex] = 0 /* EmitLeft */;
1473
+ nodeStack[stackIndex] = next;
1474
+ } else {
1475
+ pipelinePhase(EmitHint.Expression, next);
1476
+ }
1477
+ Debug.assert(lastNode === next);
1478
+ lastNode = savedLastNode;
1479
+ }
1480
+ }
1481
+ function emitConditionalExpression(node) {
1482
+ const linesBeforeQuestion = getLinesBetweenNodes(node, node.condition, node.questionToken);
1483
+ const linesAfterQuestion = getLinesBetweenNodes(node, node.questionToken, node.whenTrue);
1484
+ const linesBeforeColon = getLinesBetweenNodes(node, node.whenTrue, node.colonToken);
1485
+ const linesAfterColon = getLinesBetweenNodes(node, node.colonToken, node.whenFalse);
1486
+ emitExpression(node.condition);
1487
+ writeLinesAndIndent(
1488
+ linesBeforeQuestion,
1489
+ /*writeSpaceIfNotIndenting*/
1490
+ true
1491
+ );
1492
+ emit(node.questionToken);
1493
+ writeLinesAndIndent(
1494
+ linesAfterQuestion,
1495
+ /*writeSpaceIfNotIndenting*/
1496
+ true
1497
+ );
1498
+ emitExpression(node.whenTrue);
1499
+ decreaseIndentIf(linesBeforeQuestion, linesAfterQuestion);
1500
+ writeLinesAndIndent(
1501
+ linesBeforeColon,
1502
+ /*writeSpaceIfNotIndenting*/
1503
+ true
1504
+ );
1505
+ emit(node.colonToken);
1506
+ writeLinesAndIndent(
1507
+ linesAfterColon,
1508
+ /*writeSpaceIfNotIndenting*/
1509
+ true
1510
+ );
1511
+ emitExpression(node.whenFalse);
1512
+ decreaseIndentIf(linesBeforeColon, linesAfterColon);
1513
+ }
1514
+ function emitTemplateExpression(node) {
1515
+ emit(node.head);
1516
+ emitList(node, node.templateSpans, 262144 /* TemplateExpressionSpans */);
1517
+ }
1518
+ function emitSpreadExpression(node) {
1519
+ emitTokenWithComment(SyntaxKind.DotDotDotToken, node.pos, writePunctuation, node);
1520
+ emitExpression(node.expression);
1521
+ }
1522
+ function emitAsExpression(node) {
1523
+ emitExpression(node.expression);
1524
+ if (node.type) {
1525
+ writeSpace();
1526
+ writeKeyword("as");
1527
+ writeSpace();
1528
+ emit(node.type);
1529
+ }
1530
+ }
1531
+ function emitMetaProperty(node) {
1532
+ writeToken(node.keywordToken, node.pos, writePunctuation);
1533
+ writePunctuation(".");
1534
+ emit(node.name);
1535
+ }
1536
+ function emitTemplateSpan(node) {
1537
+ emitExpression(node.expression);
1538
+ emit(node.literal);
1539
+ }
1540
+ function emitBlock(node) {
1541
+ emitBlockStatements(
1542
+ node,
1543
+ /*forceSingleLine*/
1544
+ !node.multiLine && isEmptyBlock(node)
1545
+ );
1546
+ }
1547
+ function emitBlockStatements(node, forceSingleLine) {
1548
+ emitTokenWithComment(
1549
+ SyntaxKind.OpenBraceToken,
1550
+ node.pos,
1551
+ writePunctuation,
1552
+ /*contextNode*/
1553
+ node
1554
+ );
1555
+ const format = forceSingleLine || getEmitFlags(node) & EmitFlags.SingleLine ? 768 /* SingleLineBlockStatements */ : 129 /* MultiLineBlockStatements */;
1556
+ emitList(node, node.statements, format);
1557
+ emitTokenWithComment(
1558
+ SyntaxKind.CloseBraceToken,
1559
+ node.statements.end,
1560
+ writePunctuation,
1561
+ /*contextNode*/
1562
+ node,
1563
+ /*indentLeading*/
1564
+ !!(format & 1 /* MultiLine */)
1565
+ );
1566
+ }
1567
+ function emitVariableStatement(node) {
1568
+ emitModifiers(node, node.modifiers);
1569
+ emit(node.declarationList);
1570
+ writeTrailingSemicolon();
1571
+ }
1572
+ function emitEmptyStatement(isEmbeddedStatement) {
1573
+ if (isEmbeddedStatement) {
1574
+ writePunctuation(";");
1575
+ } else {
1576
+ writeTrailingSemicolon();
1577
+ }
1578
+ }
1579
+ function emitExpressionStatement(node) {
1580
+ emitExpression(node.expression);
1581
+ if (!isJsonSourceFile(currentSourceFile) || nodeIsSynthesized(node.expression)) {
1582
+ writeTrailingSemicolon();
1583
+ }
1584
+ }
1585
+ function emitIfStatement(node) {
1586
+ const openParenPos = emitTokenWithComment(SyntaxKind.IfKeyword, node.pos, writeKeyword, node);
1587
+ writeSpace();
1588
+ emitTokenWithComment(SyntaxKind.OpenParenToken, openParenPos, writePunctuation, node);
1589
+ emitExpression(node.expression);
1590
+ emitTokenWithComment(SyntaxKind.CloseParenToken, node.expression.end, writePunctuation, node);
1591
+ emitEmbeddedStatement(node, node.thenStatement);
1592
+ if (node.elseStatement) {
1593
+ writeLineOrSpace(node);
1594
+ emitTokenWithComment(SyntaxKind.ElseKeyword, node.thenStatement.end, writeKeyword, node);
1595
+ if (node.elseStatement.kind === SyntaxKind.IfStatement) {
1596
+ writeSpace();
1597
+ emit(node.elseStatement);
1598
+ } else {
1599
+ emitEmbeddedStatement(node, node.elseStatement);
1600
+ }
1601
+ }
1602
+ }
1603
+ function emitWhileClause(node, startPos) {
1604
+ const openParenPos = emitTokenWithComment(SyntaxKind.WhileKeyword, startPos, writeKeyword, node);
1605
+ writeSpace();
1606
+ emitTokenWithComment(SyntaxKind.OpenParenToken, openParenPos, writePunctuation, node);
1607
+ emitExpression(node.expression);
1608
+ emitTokenWithComment(SyntaxKind.CloseParenToken, node.expression.end, writePunctuation, node);
1609
+ }
1610
+ function emitDoStatement(node) {
1611
+ emitTokenWithComment(SyntaxKind.DoKeyword, node.pos, writeKeyword, node);
1612
+ emitEmbeddedStatement(node, node.statement);
1613
+ if (isBlock(node.statement)) {
1614
+ writeSpace();
1615
+ } else {
1616
+ writeLineOrSpace(node);
1617
+ }
1618
+ emitWhileClause(node, node.statement.end);
1619
+ writeTrailingSemicolon();
1620
+ }
1621
+ function emitWhileStatement(node) {
1622
+ emitWhileClause(node, node.pos);
1623
+ emitEmbeddedStatement(node, node.statement);
1624
+ }
1625
+ function emitForStatement(node) {
1626
+ const openParenPos = emitTokenWithComment(SyntaxKind.ForKeyword, node.pos, writeKeyword, node);
1627
+ writeSpace();
1628
+ let pos = emitTokenWithComment(
1629
+ SyntaxKind.OpenParenToken,
1630
+ openParenPos,
1631
+ writePunctuation,
1632
+ /*contextNode*/
1633
+ node
1634
+ );
1635
+ emitForBinding(node.initializer);
1636
+ pos = emitTokenWithComment(SyntaxKind.SemicolonToken, node.initializer ? node.initializer.end : pos, writePunctuation, node);
1637
+ emitExpressionWithLeadingSpace(node.condition);
1638
+ pos = emitTokenWithComment(SyntaxKind.SemicolonToken, node.condition ? node.condition.end : pos, writePunctuation, node);
1639
+ emitExpressionWithLeadingSpace(node.incrementor);
1640
+ emitTokenWithComment(SyntaxKind.CloseParenToken, node.incrementor ? node.incrementor.end : pos, writePunctuation, node);
1641
+ emitEmbeddedStatement(node, node.statement);
1642
+ }
1643
+ function emitForInStatement(node) {
1644
+ const openParenPos = emitTokenWithComment(SyntaxKind.ForKeyword, node.pos, writeKeyword, node);
1645
+ writeSpace();
1646
+ emitTokenWithComment(SyntaxKind.OpenParenToken, openParenPos, writePunctuation, node);
1647
+ emitForBinding(node.initializer);
1648
+ writeSpace();
1649
+ emitTokenWithComment(SyntaxKind.InKeyword, node.initializer.end, writeKeyword, node);
1650
+ writeSpace();
1651
+ emitExpression(node.expression);
1652
+ emitTokenWithComment(SyntaxKind.CloseParenToken, node.expression.end, writePunctuation, node);
1653
+ emitEmbeddedStatement(node, node.statement);
1654
+ }
1655
+ function emitForOfStatement(node) {
1656
+ const openParenPos = emitTokenWithComment(SyntaxKind.ForKeyword, node.pos, writeKeyword, node);
1657
+ writeSpace();
1658
+ emitWithTrailingSpace(node.awaitModifier);
1659
+ emitTokenWithComment(SyntaxKind.OpenParenToken, openParenPos, writePunctuation, node);
1660
+ emitForBinding(node.initializer);
1661
+ writeSpace();
1662
+ emitTokenWithComment(SyntaxKind.OfKeyword, node.initializer.end, writeKeyword, node);
1663
+ writeSpace();
1664
+ emitExpression(node.expression);
1665
+ emitTokenWithComment(SyntaxKind.CloseParenToken, node.expression.end, writePunctuation, node);
1666
+ emitEmbeddedStatement(node, node.statement);
1667
+ }
1668
+ function emitForBinding(node) {
1669
+ if (node !== void 0) {
1670
+ if (node.kind === SyntaxKind.VariableDeclarationList) {
1671
+ emit(node);
1672
+ } else {
1673
+ emitExpression(node);
1674
+ }
1675
+ }
1676
+ }
1677
+ function emitContinueStatement(node) {
1678
+ emitTokenWithComment(SyntaxKind.ContinueKeyword, node.pos, writeKeyword, node);
1679
+ writeTrailingSemicolon();
1680
+ }
1681
+ function emitBreakStatement(node) {
1682
+ emitTokenWithComment(SyntaxKind.BreakKeyword, node.pos, writeKeyword, node);
1683
+ writeTrailingSemicolon();
1684
+ }
1685
+ function emitTokenWithComment(token, pos, writer2, contextNode, indentLeading) {
1686
+ const node = getParseTreeNode(contextNode);
1687
+ const isSimilarNode = node && node.kind === contextNode.kind;
1688
+ const startPos = pos;
1689
+ if (isSimilarNode && currentSourceFile) {
1690
+ pos = skipTrivia2(currentSourceFile.text, pos);
1691
+ }
1692
+ if (emitLeadingCommentsOfPosition && isSimilarNode && contextNode.pos !== startPos) {
1693
+ const needsIndent = indentLeading && currentSourceFile && !positionsAreOnSameLine(startPos, pos, currentSourceFile);
1694
+ if (needsIndent) {
1695
+ increaseIndent();
1696
+ }
1697
+ emitLeadingCommentsOfPosition(startPos);
1698
+ if (needsIndent) {
1699
+ decreaseIndent();
1700
+ }
1701
+ }
1702
+ pos = writeTokenText(token, writer2, pos);
1703
+ if (emitTrailingCommentsOfPosition && isSimilarNode && contextNode.end !== pos) {
1704
+ emitTrailingCommentsOfPosition(
1705
+ pos,
1706
+ /*prefixSpace*/
1707
+ true
1708
+ );
1709
+ }
1710
+ return pos;
1711
+ }
1712
+ function emitReturnStatement(node) {
1713
+ emitTokenWithComment(
1714
+ SyntaxKind.ReturnKeyword,
1715
+ node.pos,
1716
+ writeKeyword,
1717
+ /*contextNode*/
1718
+ node
1719
+ );
1720
+ emitExpressionWithLeadingSpace(node.expression);
1721
+ writeTrailingSemicolon();
1722
+ }
1723
+ function emitSwitchStatement(node) {
1724
+ const openParenPos = emitTokenWithComment(SyntaxKind.SwitchKeyword, node.pos, writeKeyword, node);
1725
+ writeSpace();
1726
+ emitTokenWithComment(SyntaxKind.OpenParenToken, openParenPos, writePunctuation, node);
1727
+ emitExpression(node.expression);
1728
+ emitTokenWithComment(SyntaxKind.CloseParenToken, node.expression.end, writePunctuation, node);
1729
+ writeSpace();
1730
+ emit(node.caseBlock);
1731
+ }
1732
+ function emitThrowStatement(node) {
1733
+ emitTokenWithComment(SyntaxKind.ThrowKeyword, node.pos, writeKeyword, node);
1734
+ emitExpressionWithLeadingSpace(node.expression);
1735
+ writeTrailingSemicolon();
1736
+ }
1737
+ function emitTryStatement(node) {
1738
+ emitTokenWithComment(SyntaxKind.TryKeyword, node.pos, writeKeyword, node);
1739
+ writeSpace();
1740
+ emit(node.tryBlock);
1741
+ if (node.catchClause) {
1742
+ writeLineOrSpace(node);
1743
+ emit(node.catchClause);
1744
+ }
1745
+ if (node.finallyBlock) {
1746
+ writeLineOrSpace(node);
1747
+ emitTokenWithComment(SyntaxKind.FinallyKeyword, (node.catchClause || node.tryBlock).end, writeKeyword, node);
1748
+ writeSpace();
1749
+ emit(node.finallyBlock);
1750
+ }
1751
+ }
1752
+ function emitVariableDeclaration(node) {
1753
+ emit(node.name);
1754
+ emit(node.exclamationToken);
1755
+ emitTypeAnnotation(node.type);
1756
+ emitInitializer(node.initializer, node.type ? node.type.end : node.name.end, node);
1757
+ }
1758
+ function emitVariableDeclarationList(node) {
1759
+ writeKeyword(isLet(node) ? "let" : isVarConst(node) ? "const" : "var");
1760
+ writeSpace();
1761
+ emitList(node, node.declarations, 528 /* VariableDeclarationList */);
1762
+ }
1763
+ function emitBlockCallback(_hint, body) {
1764
+ emitBlockFunctionBody(body);
1765
+ }
1766
+ function emitSignatureAndBody(node, emitSignatureHead) {
1767
+ const body = node.body;
1768
+ if (body) {
1769
+ if (isBlock(body)) {
1770
+ const indentedFlag = getEmitFlags(node) & EmitFlags.Indented;
1771
+ if (indentedFlag) {
1772
+ increaseIndent();
1773
+ }
1774
+ pushNameGenerationScope(node);
1775
+ forEach(node.parameters, generateNames);
1776
+ generateNames(node.body);
1777
+ emitSignatureHead(node);
1778
+ if (onEmitNode) {
1779
+ onEmitNode(EmitHint.Unspecified, body, emitBlockCallback);
1780
+ } else {
1781
+ emitBlockFunctionBody(body);
1782
+ }
1783
+ popNameGenerationScope(node);
1784
+ if (indentedFlag) {
1785
+ decreaseIndent();
1786
+ }
1787
+ } else {
1788
+ emitSignatureHead(node);
1789
+ writeSpace();
1790
+ emitExpression(body);
1791
+ }
1792
+ } else {
1793
+ emitSignatureHead(node);
1794
+ writeTrailingSemicolon();
1795
+ }
1796
+ }
1797
+ function shouldEmitBlockFunctionBodyOnSingleLine(body) {
1798
+ if (getEmitFlags(body) & EmitFlags.SingleLine) {
1799
+ return true;
1800
+ }
1801
+ if (body.multiLine) {
1802
+ return false;
1803
+ }
1804
+ if (!nodeIsSynthesized(body) && !rangeIsOnSingleLine(body, currentSourceFile)) {
1805
+ return false;
1806
+ }
1807
+ if (getLeadingLineTerminatorCount(body, body.statements, 2 /* PreserveLines */) || getClosingLineTerminatorCount(body, body.statements, 2 /* PreserveLines */)) {
1808
+ return false;
1809
+ }
1810
+ let previousStatement;
1811
+ for (const statement of body.statements) {
1812
+ if (getSeparatingLineTerminatorCount(previousStatement, statement, 2 /* PreserveLines */) > 0) {
1813
+ return false;
1814
+ }
1815
+ previousStatement = statement;
1816
+ }
1817
+ return true;
1818
+ }
1819
+ function emitBlockFunctionBody(body) {
1820
+ writeSpace();
1821
+ writePunctuation("{");
1822
+ increaseIndent();
1823
+ const emitBlockFunctionBody2 = shouldEmitBlockFunctionBodyOnSingleLine(body) ? emitBlockFunctionBodyOnSingleLine : emitBlockFunctionBodyWorker;
1824
+ if (emitBodyWithDetachedComments) {
1825
+ emitBodyWithDetachedComments(body, body.statements, emitBlockFunctionBody2);
1826
+ } else {
1827
+ emitBlockFunctionBody2(body);
1828
+ }
1829
+ decreaseIndent();
1830
+ writeToken(SyntaxKind.CloseBraceToken, body.statements.end, writePunctuation);
1831
+ }
1832
+ function emitBlockFunctionBodyOnSingleLine(body) {
1833
+ emitBlockFunctionBodyWorker(
1834
+ body,
1835
+ /*emitBlockFunctionBodyOnSingleLine*/
1836
+ true
1837
+ );
1838
+ }
1839
+ function emitBlockFunctionBodyWorker(body, emitBlockFunctionBodyOnSingleLine2) {
1840
+ const statementOffset = emitPrologueDirectives(body.statements);
1841
+ const pos = writer.getTextPos();
1842
+ if (statementOffset === 0 && pos === writer.getTextPos() && emitBlockFunctionBodyOnSingleLine2) {
1843
+ decreaseIndent();
1844
+ emitList(body, body.statements, 768 /* SingleLineFunctionBodyStatements */);
1845
+ increaseIndent();
1846
+ } else {
1847
+ emitList(body, body.statements, 1 /* MultiLineFunctionBodyStatements */, statementOffset);
1848
+ }
1849
+ }
1850
+ function emitTypeAliasDeclaration(node) {
1851
+ emitModifiers(node, node.modifiers);
1852
+ writeKeyword("type");
1853
+ writeSpace();
1854
+ emit(node.name);
1855
+ emitTypeParameters(node, node.typeParameters);
1856
+ writeSpace();
1857
+ writePunctuation("=");
1858
+ writeSpace();
1859
+ emit(node.type);
1860
+ writeTrailingSemicolon();
1861
+ }
1862
+ function emitModuleDeclaration(node) {
1863
+ emitModifiers(node, node.modifiers);
1864
+ if (~node.flags & NodeFlags.GlobalAugmentation) {
1865
+ writeKeyword(node.flags & NodeFlags.Namespace ? "namespace" : "module");
1866
+ writeSpace();
1867
+ }
1868
+ emit(node.name);
1869
+ let body = node.body;
1870
+ if (!body) return writeTrailingSemicolon();
1871
+ while (body.kind === SyntaxKind.ModuleDeclaration) {
1872
+ writePunctuation(".");
1873
+ emit(body.name);
1874
+ body = body.body;
1875
+ }
1876
+ writeSpace();
1877
+ emit(body);
1878
+ }
1879
+ function emitModuleBlock(node) {
1880
+ pushNameGenerationScope(node);
1881
+ forEach(node.statements, generateNames);
1882
+ emitBlockStatements(
1883
+ node,
1884
+ /*forceSingleLine*/
1885
+ isEmptyBlock(node)
1886
+ );
1887
+ popNameGenerationScope(node);
1888
+ }
1889
+ function emitCaseBlock(node) {
1890
+ emitTokenWithComment(SyntaxKind.OpenBraceToken, node.pos, writePunctuation, node);
1891
+ emitList(node, node.clauses, 129 /* CaseBlockClauses */);
1892
+ emitTokenWithComment(
1893
+ SyntaxKind.CloseBraceToken,
1894
+ node.clauses.end,
1895
+ writePunctuation,
1896
+ node,
1897
+ /*indentLeading*/
1898
+ true
1899
+ );
1900
+ }
1901
+ function emitImportDeclaration(node) {
1902
+ emitModifiers(node, node.modifiers);
1903
+ emitTokenWithComment(SyntaxKind.ImportKeyword, node.modifiers ? node.modifiers.end : node.pos, writeKeyword, node);
1904
+ writeSpace();
1905
+ if (node.importClause) {
1906
+ emit(node.importClause);
1907
+ writeSpace();
1908
+ emitTokenWithComment(SyntaxKind.FromKeyword, node.importClause.end, writeKeyword, node);
1909
+ writeSpace();
1910
+ }
1911
+ emitExpression(node.moduleSpecifier);
1912
+ writeTrailingSemicolon();
1913
+ }
1914
+ function emitImportClause(node) {
1915
+ if (node.isTypeOnly) {
1916
+ emitTokenWithComment(SyntaxKind.TypeKeyword, node.pos, writeKeyword, node);
1917
+ writeSpace();
1918
+ }
1919
+ emit(node.name);
1920
+ if (node.name && node.namedBindings) {
1921
+ emitTokenWithComment(SyntaxKind.CommaToken, node.name.end, writePunctuation, node);
1922
+ writeSpace();
1923
+ }
1924
+ emit(node.namedBindings);
1925
+ }
1926
+ function emitNamespaceImport(node) {
1927
+ const asPos = emitTokenWithComment(SyntaxKind.AsteriskToken, node.pos, writePunctuation, node);
1928
+ writeSpace();
1929
+ emitTokenWithComment(SyntaxKind.AsKeyword, asPos, writeKeyword, node);
1930
+ writeSpace();
1931
+ emit(node.name);
1932
+ }
1933
+ function emitNamedImports(node) {
1934
+ emitNamedImportsOrExports(node);
1935
+ }
1936
+ function emitImportSpecifier(node) {
1937
+ emitImportOrExportSpecifier(node);
1938
+ }
1939
+ function emitExportAssignment(node) {
1940
+ const nextPos = emitTokenWithComment(SyntaxKind.ExportKeyword, node.pos, writeKeyword, node);
1941
+ writeSpace();
1942
+ emitTokenWithComment(SyntaxKind.DefaultKeyword, nextPos, writeKeyword, node);
1943
+ writeSpace();
1944
+ emitExpression(node.expression);
1945
+ writeTrailingSemicolon();
1946
+ }
1947
+ function emitExportDeclaration(node) {
1948
+ let nextPos = emitTokenWithComment(SyntaxKind.ExportKeyword, node.pos, writeKeyword, node);
1949
+ writeSpace();
1950
+ if (node.isTypeOnly) {
1951
+ nextPos = emitTokenWithComment(SyntaxKind.TypeKeyword, nextPos, writeKeyword, node);
1952
+ writeSpace();
1953
+ }
1954
+ if (node.exportClause) {
1955
+ emit(node.exportClause);
1956
+ } else {
1957
+ nextPos = emitTokenWithComment(SyntaxKind.AsteriskToken, nextPos, writePunctuation, node);
1958
+ }
1959
+ if (node.moduleSpecifier) {
1960
+ writeSpace();
1961
+ const fromPos = node.exportClause ? node.exportClause.end : nextPos;
1962
+ emitTokenWithComment(SyntaxKind.FromKeyword, fromPos, writeKeyword, node);
1963
+ writeSpace();
1964
+ emitExpression(node.moduleSpecifier);
1965
+ }
1966
+ writeTrailingSemicolon();
1967
+ }
1968
+ function emitNamespaceExport(node) {
1969
+ const asPos = emitTokenWithComment(SyntaxKind.AsteriskToken, node.pos, writePunctuation, node);
1970
+ writeSpace();
1971
+ emitTokenWithComment(SyntaxKind.AsKeyword, asPos, writeKeyword, node);
1972
+ writeSpace();
1973
+ emit(node.name);
1974
+ }
1975
+ function emitNamedExports(node) {
1976
+ emitNamedImportsOrExports(node);
1977
+ }
1978
+ function emitExportSpecifier(node) {
1979
+ emitImportOrExportSpecifier(node);
1980
+ }
1981
+ function emitNamedImportsOrExports(node) {
1982
+ writePunctuation("{");
1983
+ emitList(node, node.elements, 525136 /* NamedImportsOrExportsElements */);
1984
+ writePunctuation("}");
1985
+ }
1986
+ function emitImportOrExportSpecifier(node) {
1987
+ if (node.propertyName) {
1988
+ emit(node.propertyName);
1989
+ writeSpace();
1990
+ emitTokenWithComment(SyntaxKind.AsKeyword, node.propertyName.end, writeKeyword, node);
1991
+ writeSpace();
1992
+ }
1993
+ emit(node.name);
1994
+ }
1995
+ function emitJsxElement(node) {
1996
+ emit(node.openingElement);
1997
+ emitList(node, node.children, 262144 /* JsxElementOrFragmentChildren */);
1998
+ emit(node.closingElement);
1999
+ }
2000
+ function emitJsxSelfClosingElement(node) {
2001
+ writePunctuation("<");
2002
+ emitJsxTagName(node.tagName);
2003
+ emitTypeArguments(node, node.typeArguments);
2004
+ writeSpace();
2005
+ emit(node.attributes);
2006
+ writePunctuation("/>");
2007
+ }
2008
+ function emitJsxFragment(node) {
2009
+ emit(node.openingFragment);
2010
+ emitList(node, node.children, 262144 /* JsxElementOrFragmentChildren */);
2011
+ emit(node.closingFragment);
2012
+ }
2013
+ function emitJsxOpeningElementOrFragment(node) {
2014
+ writePunctuation("<");
2015
+ if (isJsxOpeningElement(node)) {
2016
+ const indented = writeLineSeparatorsAndIndentBefore(node.tagName, node);
2017
+ emitJsxTagName(node.tagName);
2018
+ emitTypeArguments(node, node.typeArguments);
2019
+ if (node.attributes.properties && node.attributes.properties.length > 0) {
2020
+ writeSpace();
2021
+ }
2022
+ emit(node.attributes);
2023
+ writeLineSeparatorsAfter(node.attributes, node);
2024
+ decreaseIndentIf(indented);
2025
+ }
2026
+ writePunctuation(">");
2027
+ }
2028
+ function emitJsxText(node) {
2029
+ writer.writeLiteral(node.text);
2030
+ }
2031
+ function emitJsxClosingElementOrFragment(node) {
2032
+ writePunctuation("</");
2033
+ if (isJsxClosingElement(node)) {
2034
+ emitJsxTagName(node.tagName);
2035
+ }
2036
+ writePunctuation(">");
2037
+ }
2038
+ function emitJsxAttributes(node) {
2039
+ emitList(node, node.properties, 262656 /* JsxElementAttributes */);
2040
+ }
2041
+ function emitJsxAttribute(node) {
2042
+ emit(node.name);
2043
+ emitNodeWithPrefix("=", writePunctuation, node.initializer, emitJsxAttributeValue);
2044
+ }
2045
+ function emitJsxSpreadAttribute(node) {
2046
+ writePunctuation("{...");
2047
+ emitExpression(node.expression);
2048
+ writePunctuation("}");
2049
+ }
2050
+ function emitJsxExpression(node) {
2051
+ if (node.expression) {
2052
+ writePunctuation("{");
2053
+ emit(node.dotDotDotToken);
2054
+ emitExpression(node.expression);
2055
+ writePunctuation("}");
2056
+ }
2057
+ }
2058
+ function emitJsxTagName(node) {
2059
+ if (node.kind === SyntaxKind.Identifier) {
2060
+ emitExpression(node);
2061
+ } else {
2062
+ emit(node);
2063
+ }
2064
+ }
2065
+ function emitCaseClause(node) {
2066
+ emitTokenWithComment(SyntaxKind.CaseKeyword, node.pos, writeKeyword, node);
2067
+ writeSpace();
2068
+ emitExpression(node.expression);
2069
+ emitCaseOrDefaultClauseRest(node, node.statements, node.expression.end);
2070
+ }
2071
+ function emitDefaultClause(node) {
2072
+ const pos = emitTokenWithComment(SyntaxKind.DefaultKeyword, node.pos, writeKeyword, node);
2073
+ emitCaseOrDefaultClauseRest(node, node.statements, pos);
2074
+ }
2075
+ function emitCaseOrDefaultClauseRest(parentNode, statements, colonPos) {
2076
+ const emitAsSingleStatement = statements.length === 1 && // treat synthesized nodes as located on the same line for emit purposes
2077
+ (nodeIsSynthesized(parentNode) || nodeIsSynthesized(statements[0]) || rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile));
2078
+ let format = 163969 /* CaseOrDefaultClauseStatements */;
2079
+ if (emitAsSingleStatement) {
2080
+ writeToken(SyntaxKind.ColonToken, colonPos, writePunctuation);
2081
+ writeSpace();
2082
+ format &= ~(1 /* MultiLine */ | 128 /* Indented */);
2083
+ } else {
2084
+ emitTokenWithComment(SyntaxKind.ColonToken, colonPos, writePunctuation, parentNode);
2085
+ }
2086
+ emitList(parentNode, statements, format);
2087
+ }
2088
+ function emitCatchClause(node) {
2089
+ const openParenPos = emitTokenWithComment(SyntaxKind.CatchKeyword, node.pos, writeKeyword, node);
2090
+ writeSpace();
2091
+ if (node.variableDeclaration) {
2092
+ emitTokenWithComment(SyntaxKind.OpenParenToken, openParenPos, writePunctuation, node);
2093
+ emit(node.variableDeclaration);
2094
+ emitTokenWithComment(SyntaxKind.CloseParenToken, node.variableDeclaration.end, writePunctuation, node);
2095
+ writeSpace();
2096
+ }
2097
+ emit(node.block);
2098
+ }
2099
+ function emitPropertyAssignment(node) {
2100
+ emit(node.name);
2101
+ writePunctuation(":");
2102
+ writeSpace();
2103
+ const initializer = node.initializer;
2104
+ if (emitTrailingCommentsOfPosition && (getEmitFlags(initializer) & EmitFlags.NoLeadingComments) === 0) {
2105
+ const commentRange = getCommentRange(initializer);
2106
+ emitTrailingCommentsOfPosition(commentRange.pos);
2107
+ }
2108
+ emitExpression(initializer);
2109
+ }
2110
+ function emitShorthandPropertyAssignment(node) {
2111
+ emit(node.name);
2112
+ if (node.objectAssignmentInitializer) {
2113
+ writeSpace();
2114
+ writePunctuation("=");
2115
+ writeSpace();
2116
+ emitExpression(node.objectAssignmentInitializer);
2117
+ }
2118
+ }
2119
+ function emitSpreadAssignment(node) {
2120
+ if (node.expression) {
2121
+ emitTokenWithComment(SyntaxKind.DotDotDotToken, node.pos, writePunctuation, node);
2122
+ emitExpression(node.expression);
2123
+ }
2124
+ }
2125
+ function emitSourceFile(node) {
2126
+ writeLine();
2127
+ const statements = node.statements;
2128
+ if (emitBodyWithDetachedComments) {
2129
+ const shouldEmitDetachedComment = statements.length === 0 || !isPrologueDirective(statements[0]) || nodeIsSynthesized(statements[0]);
2130
+ if (shouldEmitDetachedComment) {
2131
+ emitBodyWithDetachedComments(node, statements, emitSourceFileWorker);
2132
+ return;
2133
+ }
2134
+ }
2135
+ emitSourceFileWorker(node);
2136
+ }
2137
+ function emitSourceFileWorker(node) {
2138
+ const statements = node.statements;
2139
+ pushNameGenerationScope(node);
2140
+ forEach(node.statements, generateNames);
2141
+ const index = findIndex(statements, (statement) => !isPrologueDirective(statement));
2142
+ emitList(node, statements, 1 /* MultiLine */, index === -1 ? statements.length : index);
2143
+ popNameGenerationScope(node);
2144
+ }
2145
+ function emitPrologueDirectives(statements, sourceFile) {
2146
+ let needsToSetSourceFile = !!sourceFile;
2147
+ for (let i = 0; i < statements.length; i++) {
2148
+ const statement = statements[i];
2149
+ if (isPrologueDirective(statement)) {
2150
+ if (needsToSetSourceFile) {
2151
+ needsToSetSourceFile = false;
2152
+ setSourceFile(sourceFile);
2153
+ }
2154
+ writeLine();
2155
+ emit(statement);
2156
+ } else {
2157
+ return i;
2158
+ }
2159
+ }
2160
+ return statements.length;
2161
+ }
2162
+ function emitPrologueDirectivesIfNeeded(sourceFile) {
2163
+ emitPrologueDirectives(sourceFile.statements, sourceFile);
2164
+ }
2165
+ function emitShebangIfNeeded(sourceFile) {
2166
+ const shebang = getShebang(sourceFile.text);
2167
+ if (shebang) {
2168
+ writeComment(shebang);
2169
+ writeLine();
2170
+ return true;
2171
+ }
2172
+ }
2173
+ function emitNodeWithWriter(node, writer2) {
2174
+ if (!node) return;
2175
+ const savedWrite = write;
2176
+ write = writer2;
2177
+ emit(node);
2178
+ write = savedWrite;
2179
+ }
2180
+ function emitModifiers(node, modifiers) {
2181
+ if (modifiers && modifiers.length) {
2182
+ emitList(node, modifiers, 262656 /* Modifiers */);
2183
+ writeSpace();
2184
+ }
2185
+ }
2186
+ function emitTypeAnnotation(node) {
2187
+ if (node) {
2188
+ writePunctuation(":");
2189
+ writeSpace();
2190
+ emit(node);
2191
+ }
2192
+ }
2193
+ function emitInitializer(node, equalCommentStartPos, container) {
2194
+ if (node) {
2195
+ writeSpace();
2196
+ emitTokenWithComment(SyntaxKind.EqualsToken, equalCommentStartPos, writeOperator, container);
2197
+ writeSpace();
2198
+ emitExpression(node);
2199
+ }
2200
+ }
2201
+ function emitNodeWithPrefix(prefix, prefixWriter, node, emit2) {
2202
+ if (node) {
2203
+ prefixWriter(prefix);
2204
+ emit2(node);
2205
+ }
2206
+ }
2207
+ function emitExpressionWithLeadingSpace(node) {
2208
+ if (node) {
2209
+ writeSpace();
2210
+ emitExpression(node);
2211
+ }
2212
+ }
2213
+ function emitWithTrailingSpace(node) {
2214
+ if (node) {
2215
+ emit(node);
2216
+ writeSpace();
2217
+ }
2218
+ }
2219
+ function emitEmbeddedStatement(parent, node) {
2220
+ if (isBlock(node) || getEmitFlags(parent) & EmitFlags.SingleLine) {
2221
+ writeSpace();
2222
+ emit(node);
2223
+ } else {
2224
+ writeLine();
2225
+ increaseIndent();
2226
+ if (isEmptyStatement(node)) {
2227
+ pipelineEmit(EmitHint.EmbeddedStatement, node);
2228
+ } else {
2229
+ emit(node);
2230
+ }
2231
+ decreaseIndent();
2232
+ }
2233
+ }
2234
+ function emitTypeArguments(parentNode, typeArguments) {
2235
+ emitList(parentNode, typeArguments, 53776 /* TypeArguments */);
2236
+ }
2237
+ function emitTypeParameters(parentNode, typeParameters) {
2238
+ if (isFunctionLike(parentNode) && parentNode.typeArguments) {
2239
+ return emitTypeArguments(parentNode, parentNode.typeArguments);
2240
+ }
2241
+ emitList(parentNode, typeParameters, 53776 /* TypeParameters */);
2242
+ }
2243
+ function emitParameters(parentNode, parameters) {
2244
+ emitList(parentNode, parameters, 2576 /* Parameters */);
2245
+ }
2246
+ function canEmitSimpleArrowHead(parentNode, parameters) {
2247
+ const parameter = singleOrUndefined(parameters);
2248
+ return parameter && parameter.pos === parentNode.pos && isArrowFunction(parentNode) && !parentNode.type && !some(parentNode.modifiers) && !some(parentNode.typeParameters) && !some(parameter.modifiers) && !parameter.dotDotDotToken && !parameter.questionToken && !parameter.type && !parameter.initializer && isIdentifier(parameter.name);
2249
+ }
2250
+ function emitParametersForArrow(parentNode, parameters) {
2251
+ if (canEmitSimpleArrowHead(parentNode, parameters)) {
2252
+ emitList(parentNode, parameters, 2576 /* Parameters */ & ~2048 /* Parenthesis */);
2253
+ } else {
2254
+ emitParameters(parentNode, parameters);
2255
+ }
2256
+ }
2257
+ function emitParametersForIndexSignature(parentNode, parameters) {
2258
+ emitList(parentNode, parameters, 8848 /* IndexSignatureParameters */);
2259
+ }
2260
+ function emitList(parentNode, children, format, start, count) {
2261
+ emitNodeList(emit, parentNode, children, format, start, count);
2262
+ }
2263
+ function emitExpressionList(parentNode, children, format, start, count) {
2264
+ emitNodeList(emitExpression, parentNode, children, format, start, count);
2265
+ }
2266
+ function writeDelimiter(format) {
2267
+ switch (format & 28 /* DelimitersMask */) {
2268
+ case 0 /* None */:
2269
+ break;
2270
+ case 16 /* CommaDelimited */:
2271
+ writePunctuation(",");
2272
+ break;
2273
+ case 4 /* BarDelimited */:
2274
+ writeSpace();
2275
+ writePunctuation("|");
2276
+ break;
2277
+ case 8 /* AmpersandDelimited */:
2278
+ writeSpace();
2279
+ writePunctuation("&");
2280
+ break;
2281
+ }
2282
+ }
2283
+ function emitNodeList(emit2, parentNode, children, format, start = 0, count = children ? children.length - start : 0) {
2284
+ const isUndefined = children === void 0;
2285
+ if (isUndefined && format & 16384 /* OptionalIfUndefined */) {
2286
+ return;
2287
+ }
2288
+ const isEmpty = children === void 0 || start >= children.length || count === 0;
2289
+ if (isEmpty && format & 32768 /* OptionalIfEmpty */) {
2290
+ if (onBeforeEmitNodeArray) {
2291
+ onBeforeEmitNodeArray(children);
2292
+ }
2293
+ if (onAfterEmitNodeArray) {
2294
+ onAfterEmitNodeArray(children);
2295
+ }
2296
+ return;
2297
+ }
2298
+ if (format & 15360 /* BracketsMask */) {
2299
+ writePunctuation(getOpeningBracket(format));
2300
+ if (isEmpty && !isUndefined) {
2301
+ emitTrailingCommentsOfPosition(
2302
+ children.pos,
2303
+ /*prefixSpace*/
2304
+ true
2305
+ );
2306
+ }
2307
+ }
2308
+ if (onBeforeEmitNodeArray) {
2309
+ onBeforeEmitNodeArray(children);
2310
+ }
2311
+ if (isEmpty) {
2312
+ if (format & 1 /* MultiLine */ && !(preserveSourceNewlines && rangeIsOnSingleLine(parentNode, currentSourceFile))) {
2313
+ writeLine();
2314
+ } else if (format & 256 /* SpaceBetweenBraces */ && !(format & 524288 /* NoSpaceIfEmpty */)) {
2315
+ writeSpace();
2316
+ }
2317
+ } else {
2318
+ const mayEmitInterveningComments = (format & 262144 /* NoInterveningComments */) === 0;
2319
+ let shouldEmitInterveningComments = mayEmitInterveningComments;
2320
+ const leadingLineTerminatorCount = getLeadingLineTerminatorCount(parentNode, children, format);
2321
+ if (leadingLineTerminatorCount) {
2322
+ writeLine(leadingLineTerminatorCount);
2323
+ shouldEmitInterveningComments = false;
2324
+ } else if (format & 256 /* SpaceBetweenBraces */) {
2325
+ writeSpace();
2326
+ }
2327
+ if (format & 128 /* Indented */) {
2328
+ increaseIndent();
2329
+ }
2330
+ let previousSibling;
2331
+ let shouldDecreaseIndentAfterEmit = false;
2332
+ for (let i = 0; i < count; i++) {
2333
+ const child = children[start + i];
2334
+ if (previousSibling) {
2335
+ if (format & 28 /* DelimitersMask */ && previousSibling.end !== parentNode.end) {
2336
+ emitLeadingCommentsOfPosition(previousSibling.end);
2337
+ }
2338
+ writeDelimiter(format);
2339
+ const separatingLineTerminatorCount = getSeparatingLineTerminatorCount(previousSibling, child, format);
2340
+ if (separatingLineTerminatorCount > 0) {
2341
+ if ((format & (3 /* LinesMask */ | 128 /* Indented */)) === 0 /* SingleLine */) {
2342
+ increaseIndent();
2343
+ shouldDecreaseIndentAfterEmit = true;
2344
+ }
2345
+ writeLine(separatingLineTerminatorCount);
2346
+ shouldEmitInterveningComments = false;
2347
+ } else if (previousSibling && format & 512 /* SpaceBetweenSiblings */) {
2348
+ writeSpace();
2349
+ }
2350
+ }
2351
+ if (shouldEmitInterveningComments) {
2352
+ if (emitTrailingCommentsOfPosition) {
2353
+ const commentRange = getCommentRange(child);
2354
+ emitTrailingCommentsOfPosition(commentRange.pos);
2355
+ }
2356
+ } else {
2357
+ shouldEmitInterveningComments = mayEmitInterveningComments;
2358
+ }
2359
+ emit2(child);
2360
+ if (shouldDecreaseIndentAfterEmit) {
2361
+ decreaseIndent();
2362
+ shouldDecreaseIndentAfterEmit = false;
2363
+ }
2364
+ previousSibling = child;
2365
+ }
2366
+ const hasTrailingComma = format & 64 /* AllowTrailingComma */ && children.hasTrailingComma;
2367
+ if (format & 16 /* CommaDelimited */ && hasTrailingComma) {
2368
+ writePunctuation(",");
2369
+ }
2370
+ if (previousSibling && format & 28 /* DelimitersMask */ && previousSibling.end !== parentNode.end && !(getEmitFlags(previousSibling) & EmitFlags.NoTrailingComments)) {
2371
+ emitLeadingCommentsOfPosition(previousSibling.end);
2372
+ }
2373
+ if (format & 128 /* Indented */) {
2374
+ decreaseIndent();
2375
+ }
2376
+ const closingLineTerminatorCount = getClosingLineTerminatorCount(parentNode, children, format);
2377
+ if (closingLineTerminatorCount) {
2378
+ writeLine(closingLineTerminatorCount);
2379
+ } else if (format & (2097152 /* SpaceAfterList */ | 256 /* SpaceBetweenBraces */)) {
2380
+ writeSpace();
2381
+ }
2382
+ }
2383
+ if (onAfterEmitNodeArray) {
2384
+ onAfterEmitNodeArray(children);
2385
+ }
2386
+ if (format & 15360 /* BracketsMask */) {
2387
+ if (isEmpty && !isUndefined) {
2388
+ emitLeadingCommentsOfPosition(children.end);
2389
+ }
2390
+ writePunctuation(getClosingBracket(format));
2391
+ }
2392
+ }
2393
+ function writeLiteral(s) {
2394
+ writer.writeLiteral(s);
2395
+ }
2396
+ function writeStringLiteral(s) {
2397
+ writer.writeStringLiteral(s);
2398
+ }
2399
+ function writeBase(s) {
2400
+ writer.write(s);
2401
+ }
2402
+ function writeSymbol(s, sym) {
2403
+ writer.writeSymbol(s, sym);
2404
+ }
2405
+ function writePunctuation(s) {
2406
+ writer.writePunctuation(s);
2407
+ }
2408
+ function writeTrailingSemicolon() {
2409
+ writer.writeTrailingSemicolon(";");
2410
+ }
2411
+ function writeKeyword(s) {
2412
+ writer.writeKeyword(s);
2413
+ }
2414
+ function writeOperator(s) {
2415
+ writer.writeOperator(s);
2416
+ }
2417
+ function writeParameter(s) {
2418
+ writer.writeParameter(s);
2419
+ }
2420
+ function writeComment(s) {
2421
+ writer.writeComment(s);
2422
+ }
2423
+ function writeSpace() {
2424
+ writer.writeSpace(" ");
2425
+ }
2426
+ function writeProperty(s) {
2427
+ writer.writeProperty(s);
2428
+ }
2429
+ function writeLine(count = 1) {
2430
+ for (let i = 0; i < count; i++) {
2431
+ writer.writeLine(i > 0);
2432
+ }
2433
+ }
2434
+ function increaseIndent() {
2435
+ writer.increaseIndent();
2436
+ }
2437
+ function decreaseIndent() {
2438
+ writer.decreaseIndent();
2439
+ }
2440
+ function writeToken(token, pos, writer2) {
2441
+ return writeTokenText(token, writer2, pos);
2442
+ }
2443
+ function writeTokenNode(node, writer2) {
2444
+ if (onBeforeEmitToken) {
2445
+ onBeforeEmitToken(node);
2446
+ }
2447
+ writer2(tokenToString(node.kind));
2448
+ if (onAfterEmitToken) {
2449
+ onAfterEmitToken(node);
2450
+ }
2451
+ }
2452
+ function writeTokenText(token, writer2, pos) {
2453
+ const tokenString = tokenToString(token);
2454
+ writer2(tokenString);
2455
+ return pos < 0 ? pos : pos + tokenString.length;
2456
+ }
2457
+ function writeLineOrSpace(node) {
2458
+ if (getEmitFlags(node) & EmitFlags.SingleLine) {
2459
+ writeSpace();
2460
+ } else {
2461
+ writeLine();
2462
+ }
2463
+ }
2464
+ function writeLinesAndIndent(lineCount, writeSpaceIfNotIndenting) {
2465
+ if (lineCount) {
2466
+ increaseIndent();
2467
+ writeLine(lineCount);
2468
+ } else if (writeSpaceIfNotIndenting) {
2469
+ writeSpace();
2470
+ }
2471
+ }
2472
+ function decreaseIndentIf(value1, value2) {
2473
+ if (value1) {
2474
+ decreaseIndent();
2475
+ }
2476
+ if (value2) {
2477
+ decreaseIndent();
2478
+ }
2479
+ }
2480
+ function getLeadingLineTerminatorCount(parentNode, children, format) {
2481
+ if (format & 2 /* PreserveLines */ || preserveSourceNewlines) {
2482
+ if (format & 65536 /* PreferNewLine */) {
2483
+ return 1;
2484
+ }
2485
+ const firstChild = children[0];
2486
+ if (firstChild === void 0) {
2487
+ return rangeIsOnSingleLine(parentNode, currentSourceFile) ? 0 : 1;
2488
+ }
2489
+ if (firstChild.kind === SyntaxKind.JsxText) {
2490
+ return 0;
2491
+ }
2492
+ if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(firstChild) && (!firstChild.parent || firstChild.parent === parentNode)) {
2493
+ if (preserveSourceNewlines) {
2494
+ return getEffectiveLines(
2495
+ (includeComments) => getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(
2496
+ firstChild.pos,
2497
+ parentNode.pos,
2498
+ currentSourceFile,
2499
+ includeComments
2500
+ )
2501
+ );
2502
+ }
2503
+ return rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile) ? 0 : 1;
2504
+ }
2505
+ if (synthesizedNodeStartsOnNewLine(firstChild, format)) {
2506
+ return 1;
2507
+ }
2508
+ }
2509
+ return format & 1 /* MultiLine */ ? 1 : 0;
2510
+ }
2511
+ function getSeparatingLineTerminatorCount(previousNode, nextNode, format) {
2512
+ if (format & 2 /* PreserveLines */ || preserveSourceNewlines) {
2513
+ if (previousNode === void 0 || nextNode === void 0) {
2514
+ return 0;
2515
+ }
2516
+ if (nextNode.kind === SyntaxKind.JsxText) {
2517
+ return 0;
2518
+ } else if (!nodeIsSynthesized(previousNode) && !nodeIsSynthesized(nextNode) && previousNode.parent === nextNode.parent) {
2519
+ if (preserveSourceNewlines) {
2520
+ return getEffectiveLines(
2521
+ (includeComments) => getLinesBetweenRangeEndAndRangeStart(
2522
+ previousNode,
2523
+ nextNode,
2524
+ currentSourceFile,
2525
+ includeComments
2526
+ )
2527
+ );
2528
+ }
2529
+ return rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile) ? 0 : 1;
2530
+ } else if (synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format)) {
2531
+ return 1;
2532
+ }
2533
+ } else if (getStartsOnNewLine(nextNode)) {
2534
+ return 1;
2535
+ }
2536
+ return format & 1 /* MultiLine */ ? 1 : 0;
2537
+ }
2538
+ function getClosingLineTerminatorCount(parentNode, children, format) {
2539
+ if (format & 2 /* PreserveLines */ || preserveSourceNewlines) {
2540
+ if (format & 65536 /* PreferNewLine */) {
2541
+ return 1;
2542
+ }
2543
+ const lastChild = lastOrUndefined(children);
2544
+ if (lastChild === void 0) {
2545
+ return rangeIsOnSingleLine(parentNode, currentSourceFile) ? 0 : 1;
2546
+ }
2547
+ if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(lastChild) && (!lastChild.parent || lastChild.parent === parentNode)) {
2548
+ if (preserveSourceNewlines) {
2549
+ return getEffectiveLines(
2550
+ (includeComments) => getLinesBetweenPositionAndNextNonWhitespaceCharacter(
2551
+ lastChild.end,
2552
+ parentNode.end,
2553
+ currentSourceFile,
2554
+ includeComments
2555
+ )
2556
+ );
2557
+ }
2558
+ return rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile) ? 0 : 1;
2559
+ }
2560
+ if (synthesizedNodeStartsOnNewLine(lastChild, format)) {
2561
+ return 1;
2562
+ }
2563
+ }
2564
+ if (format & 1 /* MultiLine */ && !(format & 131072 /* NoTrailingNewLine */)) {
2565
+ return 1;
2566
+ }
2567
+ return 0;
2568
+ }
2569
+ function getEffectiveLines(getLineDifference) {
2570
+ Debug.assert(!!preserveSourceNewlines);
2571
+ const lines = getLineDifference(
2572
+ /*includeComments*/
2573
+ true
2574
+ );
2575
+ if (lines === 0) {
2576
+ return getLineDifference(
2577
+ /*includeComments*/
2578
+ false
2579
+ );
2580
+ }
2581
+ return lines;
2582
+ }
2583
+ function writeLineSeparatorsAndIndentBefore(node, parent) {
2584
+ const leadingNewlines = preserveSourceNewlines && getLeadingLineTerminatorCount(parent, [node], 0 /* None */);
2585
+ if (leadingNewlines) {
2586
+ writeLinesAndIndent(
2587
+ leadingNewlines,
2588
+ /*writeLinesIfNotIndenting*/
2589
+ false
2590
+ );
2591
+ }
2592
+ return !!leadingNewlines;
2593
+ }
2594
+ function writeLineSeparatorsAfter(node, parent) {
2595
+ const trailingNewlines = preserveSourceNewlines && getClosingLineTerminatorCount(parent, [node], 0 /* None */);
2596
+ if (trailingNewlines) {
2597
+ writeLine(trailingNewlines);
2598
+ }
2599
+ }
2600
+ function synthesizedNodeStartsOnNewLine(node, format) {
2601
+ if (nodeIsSynthesized(node)) {
2602
+ const startsOnNewLine = getStartsOnNewLine(node);
2603
+ if (startsOnNewLine === void 0) {
2604
+ return (format & 65536 /* PreferNewLine */) !== 0;
2605
+ }
2606
+ return startsOnNewLine;
2607
+ }
2608
+ return (format & 65536 /* PreferNewLine */) !== 0;
2609
+ }
2610
+ function getLinesBetweenNodes(parent, node1, node2) {
2611
+ if (getEmitFlags(parent) & EmitFlags.NoIndentation) {
2612
+ return 0;
2613
+ }
2614
+ parent = skipSynthesizedParentheses(parent);
2615
+ node1 = skipSynthesizedParentheses(node1);
2616
+ node2 = skipSynthesizedParentheses(node2);
2617
+ if (getStartsOnNewLine(node2)) {
2618
+ return 1;
2619
+ }
2620
+ if (!nodeIsSynthesized(parent) && !nodeIsSynthesized(node1) && !nodeIsSynthesized(node2)) {
2621
+ if (preserveSourceNewlines) {
2622
+ return getEffectiveLines(
2623
+ (includeComments) => getLinesBetweenRangeEndAndRangeStart(
2624
+ node1,
2625
+ node2,
2626
+ currentSourceFile,
2627
+ includeComments
2628
+ )
2629
+ );
2630
+ }
2631
+ return rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile) ? 0 : 1;
2632
+ }
2633
+ return 0;
2634
+ }
2635
+ function isEmptyBlock(block) {
2636
+ return block.statements.length === 0 && rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile);
2637
+ }
2638
+ function skipSynthesizedParentheses(node) {
2639
+ while (node.kind === SyntaxKind.ParenthesizedExpression && nodeIsSynthesized(node)) {
2640
+ node = node.expression;
2641
+ }
2642
+ return node;
2643
+ }
2644
+ function getTextOfNode(node, includeTrivia) {
2645
+ if (isGeneratedIdentifier(node)) {
2646
+ return generateName(node);
2647
+ } else if (isIdentifier(node) && (nodeIsSynthesized(node) || !node.parent || !currentSourceFile || node.parent && currentSourceFile && getSourceFileOfNode(node) !== getOriginalNode(currentSourceFile))) {
2648
+ return idText(node);
2649
+ } else if (node.kind === SyntaxKind.StringLiteral && node.textSourceNode) {
2650
+ return getTextOfNode(node.textSourceNode, includeTrivia);
2651
+ } else if (isLiteralExpression(node) && (nodeIsSynthesized(node) || !node.parent)) {
2652
+ return node.text;
2653
+ }
2654
+ return getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia);
2655
+ }
2656
+ function getLiteralTextOfNode(node, neverAsciiEscape, jsxAttributeEscape) {
2657
+ if (node.kind === SyntaxKind.StringLiteral && node.textSourceNode) {
2658
+ const textSourceNode = node.textSourceNode;
2659
+ if (isIdentifier(textSourceNode)) {
2660
+ return jsxAttributeEscape ? `"${escapeJsxAttributeString(getTextOfNode(textSourceNode))}"` : neverAsciiEscape || getEmitFlags(node) & EmitFlags.NoAsciiEscaping ? `"${escapeString(getTextOfNode(textSourceNode))}"` : `"${escapeNonAsciiString(getTextOfNode(textSourceNode))}"`;
2661
+ } else {
2662
+ return getLiteralTextOfNode(textSourceNode, neverAsciiEscape, jsxAttributeEscape);
2663
+ }
2664
+ }
2665
+ return getLiteralText(node, currentSourceFile, neverAsciiEscape, jsxAttributeEscape);
2666
+ }
2667
+ function pushNameGenerationScope(node) {
2668
+ if (node && getEmitFlags(node) & EmitFlags.ReuseTempVariableScope) {
2669
+ return;
2670
+ }
2671
+ tempFlagsStack.push(tempFlags);
2672
+ tempFlags = 0;
2673
+ reservedNamesStack.push(reservedNames);
2674
+ }
2675
+ function popNameGenerationScope(node) {
2676
+ if (node && getEmitFlags(node) & EmitFlags.ReuseTempVariableScope) {
2677
+ return;
2678
+ }
2679
+ tempFlags = tempFlagsStack.pop();
2680
+ reservedNames = reservedNamesStack.pop();
2681
+ }
2682
+ function reserveNameInNestedScopes(name) {
2683
+ if (!reservedNames || reservedNames === lastOrUndefined(reservedNamesStack)) {
2684
+ reservedNames = createMap();
2685
+ }
2686
+ reservedNames.set(name, true);
2687
+ }
2688
+ function generateNames(node) {
2689
+ if (!node) return;
2690
+ switch (node.kind) {
2691
+ case SyntaxKind.Block:
2692
+ forEach(node.statements, generateNames);
2693
+ break;
2694
+ case SyntaxKind.DoStatement:
2695
+ case SyntaxKind.WhileStatement:
2696
+ generateNames(node.statement);
2697
+ break;
2698
+ case SyntaxKind.IfStatement:
2699
+ generateNames(node.thenStatement);
2700
+ generateNames(node.elseStatement);
2701
+ break;
2702
+ case SyntaxKind.ForStatement:
2703
+ case SyntaxKind.ForOfStatement:
2704
+ case SyntaxKind.ForInStatement:
2705
+ generateNames(node.initializer);
2706
+ generateNames(node.statement);
2707
+ break;
2708
+ case SyntaxKind.SwitchStatement:
2709
+ generateNames(node.caseBlock);
2710
+ break;
2711
+ case SyntaxKind.CaseBlock:
2712
+ forEach(node.clauses, generateNames);
2713
+ break;
2714
+ case SyntaxKind.CaseClause:
2715
+ case SyntaxKind.DefaultClause:
2716
+ forEach(node.statements, generateNames);
2717
+ break;
2718
+ case SyntaxKind.TryStatement:
2719
+ generateNames(node.tryBlock);
2720
+ generateNames(node.catchClause);
2721
+ generateNames(node.finallyBlock);
2722
+ break;
2723
+ case SyntaxKind.CatchClause:
2724
+ generateNames(node.variableDeclaration);
2725
+ generateNames(node.block);
2726
+ break;
2727
+ case SyntaxKind.VariableStatement:
2728
+ generateNames(node.declarationList);
2729
+ break;
2730
+ case SyntaxKind.VariableDeclarationList:
2731
+ forEach(node.declarations, generateNames);
2732
+ break;
2733
+ case SyntaxKind.VariableDeclaration:
2734
+ case SyntaxKind.Parameter:
2735
+ case SyntaxKind.BindingElement:
2736
+ generateNameIfNeeded(node.name);
2737
+ break;
2738
+ case SyntaxKind.ObjectBindingPattern:
2739
+ case SyntaxKind.ArrayBindingPattern:
2740
+ forEach(node.elements, generateNames);
2741
+ break;
2742
+ case SyntaxKind.ImportDeclaration:
2743
+ generateNames(node.importClause);
2744
+ break;
2745
+ case SyntaxKind.ImportClause:
2746
+ generateNameIfNeeded(node.name);
2747
+ generateNames(node.namedBindings);
2748
+ break;
2749
+ case SyntaxKind.NamespaceImport:
2750
+ generateNameIfNeeded(node.name);
2751
+ break;
2752
+ case SyntaxKind.NamespaceExport:
2753
+ generateNameIfNeeded(node.name);
2754
+ break;
2755
+ case SyntaxKind.NamedImports:
2756
+ forEach(node.elements, generateNames);
2757
+ break;
2758
+ case SyntaxKind.ImportSpecifier:
2759
+ generateNameIfNeeded(node.propertyName || node.name);
2760
+ break;
2761
+ }
2762
+ }
2763
+ function generateMemberNames(node) {
2764
+ if (!node) return;
2765
+ switch (node.kind) {
2766
+ case SyntaxKind.PropertyAssignment:
2767
+ case SyntaxKind.ShorthandPropertyAssignment:
2768
+ generateNameIfNeeded(node.name);
2769
+ break;
2770
+ }
2771
+ }
2772
+ function generateNameIfNeeded(name) {
2773
+ if (name) {
2774
+ if (isGeneratedIdentifier(name)) {
2775
+ generateName(name);
2776
+ } else if (isBindingPattern(name)) {
2777
+ generateNames(name);
2778
+ }
2779
+ }
2780
+ }
2781
+ function generateName(name) {
2782
+ if ((name.autoGenerateFlags & GeneratedIdentifierFlags.KindMask) === GeneratedIdentifierFlags.Node) {
2783
+ return generateNameCached(getNodeForGeneratedName(name), name.autoGenerateFlags);
2784
+ } else {
2785
+ const autoGenerateId = name.autoGenerateId;
2786
+ return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = makeName(name));
2787
+ }
2788
+ }
2789
+ function generateNameCached(node, flags) {
2790
+ const nodeId = getNodeId(node);
2791
+ return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = generateNameForNode(node, flags));
2792
+ }
2793
+ function isUniqueName(name) {
2794
+ return isFileLevelUniqueName3(name) && !generatedNames.has(name) && !(reservedNames && reservedNames.has(name));
2795
+ }
2796
+ function isFileLevelUniqueName3(name) {
2797
+ return currentSourceFile ? ts.isFileLevelUniqueName(currentSourceFile, name, hasGlobalName) : true;
2798
+ }
2799
+ function isUniqueLocalName(name, container) {
2800
+ for (let node = container; isNodeDescendantOf(node, container); node = node.nextContainer) {
2801
+ if (node.locals) {
2802
+ const local = node.locals.get(escapeLeadingUnderscores(name));
2803
+ if (local && local.flags & (SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias)) {
2804
+ return false;
2805
+ }
2806
+ }
2807
+ }
2808
+ return true;
2809
+ }
2810
+ function makeTempVariableName(flags, reservedInNestedScopes) {
2811
+ if (flags && !(tempFlags & flags)) {
2812
+ const name = flags === 268435456 /* _i */ ? "_i" : "_n";
2813
+ if (isUniqueName(name)) {
2814
+ tempFlags |= flags;
2815
+ if (reservedInNestedScopes) {
2816
+ reserveNameInNestedScopes(name);
2817
+ }
2818
+ return name;
2819
+ }
2820
+ }
2821
+ while (true) {
2822
+ const count = tempFlags & 268435455 /* CountMask */;
2823
+ tempFlags++;
2824
+ if (count !== 8 && count !== 13) {
2825
+ const name = count < 26 ? "_" + String.fromCharCode(CharacterCodes2.a + count) : "_" + (count - 26);
2826
+ if (isUniqueName(name)) {
2827
+ if (reservedInNestedScopes) {
2828
+ reserveNameInNestedScopes(name);
2829
+ }
2830
+ return name;
2831
+ }
2832
+ }
2833
+ }
2834
+ }
2835
+ function makeUniqueName(baseName, checkFn = isUniqueName, optimistic, scoped) {
2836
+ if (optimistic) {
2837
+ if (checkFn(baseName)) {
2838
+ if (scoped) {
2839
+ reserveNameInNestedScopes(baseName);
2840
+ } else {
2841
+ generatedNames.set(baseName, true);
2842
+ }
2843
+ return baseName;
2844
+ }
2845
+ }
2846
+ if (baseName.charCodeAt(baseName.length - 1) !== CharacterCodes2._) {
2847
+ baseName += "_";
2848
+ }
2849
+ let i = 1;
2850
+ while (true) {
2851
+ const generatedName = baseName + i;
2852
+ if (checkFn(generatedName)) {
2853
+ if (scoped) {
2854
+ reserveNameInNestedScopes(generatedName);
2855
+ } else {
2856
+ generatedNames.set(generatedName, true);
2857
+ }
2858
+ return generatedName;
2859
+ }
2860
+ i++;
2861
+ }
2862
+ }
2863
+ function generateNameForModuleOrEnum(node) {
2864
+ const name = getTextOfNode(node.name);
2865
+ return isUniqueLocalName(name, node) ? name : makeUniqueName(name);
2866
+ }
2867
+ function generateNameForImportOrExportDeclaration(node) {
2868
+ const expr = getExternalModuleName(node);
2869
+ const baseName = isStringLiteral(expr) ? makeIdentifierFromModuleName(expr.text) : "module";
2870
+ return makeUniqueName(baseName);
2871
+ }
2872
+ function generateNameForExportDefault() {
2873
+ return makeUniqueName("default");
2874
+ }
2875
+ function generateNameForNode(node, flags) {
2876
+ switch (node.kind) {
2877
+ case SyntaxKind.Identifier:
2878
+ return makeUniqueName(
2879
+ getTextOfNode(node),
2880
+ isUniqueName,
2881
+ !!(flags & GeneratedIdentifierFlags.Optimistic),
2882
+ !!(flags & GeneratedIdentifierFlags.ReservedInNestedScopes)
2883
+ );
2884
+ case SyntaxKind.ModuleDeclaration:
2885
+ return generateNameForModuleOrEnum(node);
2886
+ case SyntaxKind.ImportDeclaration:
2887
+ case SyntaxKind.ExportDeclaration:
2888
+ return generateNameForImportOrExportDeclaration(node);
2889
+ case SyntaxKind.ExportAssignment:
2890
+ return generateNameForExportDefault();
2891
+ case SyntaxKind.ComputedPropertyName:
2892
+ return makeTempVariableName(
2893
+ 0 /* Auto */,
2894
+ /*reserveInNestedScopes*/
2895
+ true
2896
+ );
2897
+ default:
2898
+ return makeTempVariableName(0 /* Auto */);
2899
+ }
2900
+ }
2901
+ function makeName(name) {
2902
+ switch (name.autoGenerateFlags & GeneratedIdentifierFlags.KindMask) {
2903
+ case GeneratedIdentifierFlags.Auto:
2904
+ return makeTempVariableName(0 /* Auto */, !!(name.autoGenerateFlags & GeneratedIdentifierFlags.ReservedInNestedScopes));
2905
+ case GeneratedIdentifierFlags.Loop:
2906
+ return makeTempVariableName(268435456 /* _i */, !!(name.autoGenerateFlags & GeneratedIdentifierFlags.ReservedInNestedScopes));
2907
+ case GeneratedIdentifierFlags.Unique:
2908
+ return makeUniqueName(
2909
+ idText(name),
2910
+ name.autoGenerateFlags & GeneratedIdentifierFlags.FileLevel ? isFileLevelUniqueName3 : isUniqueName,
2911
+ !!(name.autoGenerateFlags & GeneratedIdentifierFlags.Optimistic),
2912
+ !!(name.autoGenerateFlags & GeneratedIdentifierFlags.ReservedInNestedScopes)
2913
+ );
2914
+ }
2915
+ return Debug.fail("Unsupported GeneratedIdentifierKind.");
2916
+ }
2917
+ function getNodeForGeneratedName(name) {
2918
+ const autoGenerateId = name.autoGenerateId;
2919
+ let node = name;
2920
+ let original = node.original;
2921
+ while (original) {
2922
+ node = original;
2923
+ if (isIdentifier(node) && !!(node.autoGenerateFlags & GeneratedIdentifierFlags.Node) && node.autoGenerateId !== autoGenerateId) {
2924
+ break;
2925
+ }
2926
+ original = node.original;
2927
+ }
2928
+ return node;
2929
+ }
2930
+ function pipelineEmitWithComments(hint, node) {
2931
+ Debug.assert(lastNode === node);
2932
+ hasWrittenComment = false;
2933
+ const emitFlags = getEmitFlags(node);
2934
+ const { pos, end } = getCommentRange(node);
2935
+ const isEmittedNode = node.kind !== SyntaxKind.NotEmittedStatement;
2936
+ const skipLeadingComments = pos < 0 || (emitFlags & EmitFlags.NoLeadingComments) !== 0 || node.kind === SyntaxKind.JsxText;
2937
+ const skipTrailingComments = end < 0 || (emitFlags & EmitFlags.NoTrailingComments) !== 0 || node.kind === SyntaxKind.JsxText;
2938
+ const savedContainerPos = containerPos;
2939
+ const savedContainerEnd = containerEnd;
2940
+ const savedDeclarationListContainerEnd = declarationListContainerEnd;
2941
+ if ((pos > 0 || end > 0) && pos !== end) {
2942
+ if (!skipLeadingComments) {
2943
+ emitLeadingComments(pos, isEmittedNode);
2944
+ }
2945
+ if (!skipLeadingComments || pos >= 0 && (emitFlags & EmitFlags.NoLeadingComments) !== 0) {
2946
+ containerPos = pos;
2947
+ }
2948
+ if (!skipTrailingComments || end >= 0 && (emitFlags & EmitFlags.NoTrailingComments) !== 0) {
2949
+ containerEnd = end;
2950
+ if (node.kind === SyntaxKind.VariableDeclarationList) {
2951
+ declarationListContainerEnd = end;
2952
+ }
2953
+ }
2954
+ }
2955
+ forEach(getSyntheticLeadingComments(node), emitLeadingSynthesizedComment);
2956
+ const pipelinePhase = getNextPipelinePhase(1 /* Comments */, hint, node);
2957
+ if (emitFlags & EmitFlags.NoNestedComments) {
2958
+ commentsDisabled = true;
2959
+ pipelinePhase(hint, node);
2960
+ commentsDisabled = false;
2961
+ } else {
2962
+ pipelinePhase(hint, node);
2963
+ }
2964
+ forEach(getSyntheticTrailingComments(node), emitTrailingSynthesizedComment);
2965
+ if ((pos > 0 || end > 0) && pos !== end) {
2966
+ containerPos = savedContainerPos;
2967
+ containerEnd = savedContainerEnd;
2968
+ declarationListContainerEnd = savedDeclarationListContainerEnd;
2969
+ if (!skipTrailingComments && isEmittedNode) {
2970
+ emitTrailingComments(end);
2971
+ }
2972
+ }
2973
+ Debug.assert(lastNode === node);
2974
+ }
2975
+ function emitLeadingSynthesizedComment(comment) {
2976
+ if (comment.kind === SyntaxKind.SingleLineCommentTrivia) {
2977
+ writer.writeLine();
2978
+ }
2979
+ writeSynthesizedComment(comment);
2980
+ if (comment.hasTrailingNewLine || comment.kind === SyntaxKind.SingleLineCommentTrivia) {
2981
+ writer.writeLine();
2982
+ } else {
2983
+ writer.writeSpace(" ");
2984
+ }
2985
+ }
2986
+ function emitTrailingSynthesizedComment(comment) {
2987
+ if (!writer.isAtStartOfLine()) {
2988
+ writer.writeSpace(" ");
2989
+ }
2990
+ writeSynthesizedComment(comment);
2991
+ if (comment.hasTrailingNewLine) {
2992
+ writer.writeLine();
2993
+ }
2994
+ }
2995
+ function writeSynthesizedComment(comment) {
2996
+ const text = formatSynthesizedComment(comment);
2997
+ const lineMap = comment.kind === SyntaxKind.MultiLineCommentTrivia ? computeLineStarts2(text) : void 0;
2998
+ writeCommentRange(text, lineMap, writer, 0, text.length, newLine);
2999
+ }
3000
+ function formatSynthesizedComment(comment) {
3001
+ return comment.kind === SyntaxKind.MultiLineCommentTrivia ? `/*${comment.text}*/` : `//${comment.text}`;
3002
+ }
3003
+ function emitBodyWithDetachedComments(node, detachedRange, emitCallback) {
3004
+ const { pos, end } = detachedRange;
3005
+ const emitFlags = getEmitFlags(node);
3006
+ const skipLeadingComments = pos < 0 || (emitFlags & EmitFlags.NoLeadingComments) !== 0;
3007
+ const skipTrailingComments = commentsDisabled || end < 0 || (emitFlags & EmitFlags.NoTrailingComments) !== 0;
3008
+ if (!skipLeadingComments) {
3009
+ emitDetachedCommentsAndUpdateCommentsInfo(detachedRange);
3010
+ }
3011
+ if (emitFlags & EmitFlags.NoNestedComments && !commentsDisabled) {
3012
+ commentsDisabled = true;
3013
+ emitCallback(node);
3014
+ commentsDisabled = false;
3015
+ } else {
3016
+ emitCallback(node);
3017
+ }
3018
+ if (!skipTrailingComments) {
3019
+ emitLeadingComments(
3020
+ detachedRange.end,
3021
+ /*isEmittedNode*/
3022
+ true
3023
+ );
3024
+ if (hasWrittenComment && !writer.isAtStartOfLine()) {
3025
+ writer.writeLine();
3026
+ }
3027
+ }
3028
+ }
3029
+ function emitLeadingComments(pos, isEmittedNode) {
3030
+ hasWrittenComment = false;
3031
+ if (isEmittedNode) {
3032
+ forEachLeadingCommentToEmit(pos, emitLeadingComment);
3033
+ }
3034
+ }
3035
+ function emitLeadingComment(commentPos, commentEnd, kind, hasTrailingNewLine, rangePos) {
3036
+ if (!hasWrittenComment) {
3037
+ emitNewLineBeforeLeadingCommentOfPosition(getCurrentLineMap(), writer, rangePos, commentPos);
3038
+ hasWrittenComment = true;
3039
+ }
3040
+ writeCommentRange(currentSourceFile.text, getCurrentLineMap(), writer, commentPos, commentEnd, newLine);
3041
+ if (hasTrailingNewLine) {
3042
+ writer.writeLine();
3043
+ } else if (kind === SyntaxKind.MultiLineCommentTrivia) {
3044
+ writer.writeSpace(" ");
3045
+ }
3046
+ }
3047
+ function emitLeadingCommentsOfPosition(pos) {
3048
+ if (commentsDisabled || pos === -1) {
3049
+ return;
3050
+ }
3051
+ emitLeadingComments(
3052
+ pos,
3053
+ /*isEmittedNode*/
3054
+ true
3055
+ );
3056
+ }
3057
+ function emitTrailingComments(pos) {
3058
+ forEachTrailingCommentToEmit(pos, emitTrailingComment);
3059
+ }
3060
+ function emitTrailingComment(commentPos, commentEnd, _kind, hasTrailingNewLine) {
3061
+ if (!writer.isAtStartOfLine()) {
3062
+ writer.writeSpace(" ");
3063
+ }
3064
+ writeCommentRange(currentSourceFile.text, getCurrentLineMap(), writer, commentPos, commentEnd, newLine);
3065
+ if (hasTrailingNewLine) {
3066
+ writer.writeLine();
3067
+ }
3068
+ }
3069
+ function emitTrailingCommentsOfPosition(pos, prefixSpace) {
3070
+ if (commentsDisabled) {
3071
+ return;
3072
+ }
3073
+ forEachTrailingCommentToEmit(pos, prefixSpace ? emitTrailingComment : emitTrailingCommentOfPosition);
3074
+ }
3075
+ function emitTrailingCommentOfPosition(commentPos, commentEnd, _kind, hasTrailingNewLine) {
3076
+ writeCommentRange(currentSourceFile.text, getCurrentLineMap(), writer, commentPos, commentEnd, newLine);
3077
+ if (hasTrailingNewLine) {
3078
+ writer.writeLine();
3079
+ } else {
3080
+ writer.writeSpace(" ");
3081
+ }
3082
+ }
3083
+ function forEachLeadingCommentToEmit(pos, cb) {
3084
+ if (currentSourceFile && (containerPos === -1 || pos !== containerPos)) {
3085
+ if (hasDetachedComments(pos)) {
3086
+ forEachLeadingCommentWithoutDetachedComments(cb);
3087
+ } else {
3088
+ forEachLeadingCommentRange(
3089
+ currentSourceFile.text,
3090
+ pos,
3091
+ cb,
3092
+ /*state*/
3093
+ pos
3094
+ );
3095
+ }
3096
+ }
3097
+ }
3098
+ function forEachTrailingCommentToEmit(end, cb) {
3099
+ if (currentSourceFile && (containerEnd === -1 || end !== containerEnd && end !== declarationListContainerEnd)) {
3100
+ forEachTrailingCommentRange(currentSourceFile.text, end, cb);
3101
+ }
3102
+ }
3103
+ function hasDetachedComments(pos) {
3104
+ return detachedCommentsInfo !== void 0 && last2(detachedCommentsInfo).nodePos === pos;
3105
+ }
3106
+ function forEachLeadingCommentWithoutDetachedComments(cb) {
3107
+ const pos = last2(detachedCommentsInfo).detachedCommentEndPos;
3108
+ if (detachedCommentsInfo.length - 1) {
3109
+ detachedCommentsInfo.pop();
3110
+ } else {
3111
+ detachedCommentsInfo = void 0;
3112
+ }
3113
+ forEachLeadingCommentRange(
3114
+ currentSourceFile.text,
3115
+ pos,
3116
+ cb,
3117
+ /*state*/
3118
+ pos
3119
+ );
3120
+ }
3121
+ function emitDetachedCommentsAndUpdateCommentsInfo(range) {
3122
+ const currentDetachedCommentInfo = emitDetachedComments(currentSourceFile.text, getCurrentLineMap(), writer, emitComment, range, newLine, commentsDisabled);
3123
+ if (currentDetachedCommentInfo) {
3124
+ if (detachedCommentsInfo) {
3125
+ detachedCommentsInfo.push(currentDetachedCommentInfo);
3126
+ } else {
3127
+ detachedCommentsInfo = [currentDetachedCommentInfo];
3128
+ }
3129
+ }
3130
+ }
3131
+ function emitComment(text, lineMap, writer2, commentPos, commentEnd, newLine2) {
3132
+ writeCommentRange(text, lineMap, writer2, commentPos, commentEnd, newLine2);
3133
+ }
3134
+ }
3135
+ function createBracketsMap() {
3136
+ const brackets2 = [];
3137
+ brackets2[1024 /* Braces */] = ["{", "}"];
3138
+ brackets2[2048 /* Parenthesis */] = ["(", ")"];
3139
+ brackets2[4096 /* AngleBrackets */] = ["<", ">"];
3140
+ brackets2[8192 /* SquareBrackets */] = ["[", "]"];
3141
+ return brackets2;
3142
+ }
3143
+ function getOpeningBracket(format) {
3144
+ return brackets[format & 15360 /* BracketsMask */][0];
3145
+ }
3146
+ function getClosingBracket(format) {
3147
+ return brackets[format & 15360 /* BracketsMask */][1];
3148
+ }
3149
+ export {
3150
+ ListFormat,
3151
+ createPrinter,
3152
+ createTextWriter,
3153
+ emitComments,
3154
+ emitDetachedComments,
3155
+ emitNewLineBeforeLeadingCommentOfPosition,
3156
+ emitNewLineBeforeLeadingComments,
3157
+ emitNewLineBeforeLeadingCommentsOfPosition,
3158
+ getNewLineCharacter,
3159
+ getTrailingSemicolonDeferringWriter,
3160
+ isFileLevelUniqueName,
3161
+ noEmitNotification,
3162
+ usingSingleLineStringWriter,
3163
+ writeCommentRange
3164
+ };