espolar 0.1.0

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.
package/dist/index.js ADDED
@@ -0,0 +1,1483 @@
1
+ //#region src/printers.ts
2
+ const EXPRESSIONS_PRECEDENCE = {
3
+ ArrayPattern: 20,
4
+ ObjectPattern: 20,
5
+ ArrayExpression: 20,
6
+ TaggedTemplateExpression: 20,
7
+ ThisExpression: 20,
8
+ Identifier: 20,
9
+ TemplateLiteral: 20,
10
+ Super: 20,
11
+ SequenceExpression: 20,
12
+ MemberExpression: 19,
13
+ MetaProperty: 19,
14
+ CallExpression: 19,
15
+ ChainExpression: 19,
16
+ ImportExpression: 19,
17
+ NewExpression: 19,
18
+ Literal: 18,
19
+ TSSatisfiesExpression: 18,
20
+ TSInstantiationExpression: 18,
21
+ TSNonNullExpression: 18,
22
+ TSTypeAssertion: 18,
23
+ AwaitExpression: 17,
24
+ ClassExpression: 17,
25
+ FunctionExpression: 17,
26
+ ObjectExpression: 17,
27
+ UnaryExpression: 16,
28
+ UpdateExpression: 16,
29
+ TSAsExpression: 15,
30
+ BinaryExpression: 14,
31
+ LogicalExpression: 13,
32
+ ConditionalExpression: 4,
33
+ ArrowFunctionExpression: 3,
34
+ AssignmentExpression: 3,
35
+ YieldExpression: 2,
36
+ RestElement: 1
37
+ };
38
+ const OPERATOR_PRECEDENCE = {
39
+ "||": 2,
40
+ "&&": 3,
41
+ "??": 4,
42
+ "|": 5,
43
+ "^": 6,
44
+ "&": 7,
45
+ "==": 8,
46
+ "!=": 8,
47
+ "===": 8,
48
+ "!==": 8,
49
+ "<": 9,
50
+ ">": 9,
51
+ "<=": 9,
52
+ ">=": 9,
53
+ in: 9,
54
+ instanceof: 9,
55
+ "<<": 10,
56
+ ">>": 10,
57
+ ">>>": 10,
58
+ "+": 11,
59
+ "-": 11,
60
+ "*": 12,
61
+ "%": 12,
62
+ "/": 12,
63
+ "**": 13
64
+ };
65
+ function commentNeedsNewline(comment) {
66
+ if (comment.type === "Line") return true;
67
+ return comment.value.includes("\n");
68
+ }
69
+ function needsParens(node, parent, isRight) {
70
+ if (node.type === "PrivateIdentifier" || node.type === "Identifier" || node.type === "Super") return false;
71
+ if (node.type === "LogicalExpression" && parent.type === "LogicalExpression" && parent.operator === "??" !== (node.operator === "??")) return true;
72
+ const precedence = EXPRESSIONS_PRECEDENCE[node.type] ?? 20;
73
+ const parentPrecedence = EXPRESSIONS_PRECEDENCE[parent.type] ?? 20;
74
+ if (precedence !== parentPrecedence) {
75
+ if (!isRight && precedence === 15 && parentPrecedence === 14 && parent.operator === "**") return false;
76
+ return precedence < parentPrecedence;
77
+ }
78
+ if (precedence !== 13 && precedence !== 14) return false;
79
+ const nodeOp = node.operator;
80
+ if (nodeOp === "**" && parent.operator === "**") return !isRight;
81
+ if (isRight) return OPERATOR_PRECEDENCE[nodeOp] <= OPERATOR_PRECEDENCE[parent.operator];
82
+ return OPERATOR_PRECEDENCE[nodeOp] < OPERATOR_PRECEDENCE[parent.operator];
83
+ }
84
+ function arrowConciseBodyNeedsWrap(body) {
85
+ if (body.type === "BlockStatement") return false;
86
+ switch (body.type) {
87
+ case "ObjectExpression": return true;
88
+ case "AssignmentExpression": return body.left.type === "ObjectPattern";
89
+ case "LogicalExpression": return body.left.type === "ObjectExpression";
90
+ case "ConditionalExpression": return body.test.type === "ObjectExpression";
91
+ case "TSAsExpression":
92
+ case "TSSatisfiesExpression":
93
+ case "TSNonNullExpression": return body.expression ? arrowConciseBodyNeedsWrap(body.expression) : false;
94
+ default: return false;
95
+ }
96
+ }
97
+ const defaultPrinters = {
98
+ Program: printProgram,
99
+ Identifier: printIdentifier,
100
+ PrivateIdentifier: printPrivateIdentifier,
101
+ Literal: printLiteral,
102
+ ExpressionStatement: printExpressionStatement,
103
+ EmptyStatement: printEmptyStatement,
104
+ VariableDeclaration: printVariableDeclaration,
105
+ VariableDeclarator: printVariableDeclarator,
106
+ BlockStatement: printBlockStatement,
107
+ ReturnStatement: printReturnStatement,
108
+ ThrowStatement: printThrowStatement,
109
+ DebuggerStatement: printDebuggerStatement,
110
+ BreakStatement: printBreakStatement,
111
+ ContinueStatement: printContinueStatement,
112
+ LabeledStatement: printLabeledStatement,
113
+ WhileStatement: printWhileStatement,
114
+ DoWhileStatement: printDoWhileStatement,
115
+ IfStatement: printIfStatement,
116
+ ForStatement: printForStatement,
117
+ ForInStatement: printForInStatement,
118
+ ForOfStatement: printForOfStatement,
119
+ SwitchStatement: printSwitchStatement,
120
+ SwitchCase: printSwitchCase,
121
+ TryStatement: printTryStatement,
122
+ WithStatement: printWithStatement,
123
+ FunctionDeclaration: printFunctionDeclaration,
124
+ FunctionExpression: printFunctionExpression,
125
+ ArrowFunctionExpression: printArrowFunctionExpression,
126
+ UnaryExpression: printUnaryExpression,
127
+ UpdateExpression: printUpdateExpression,
128
+ BinaryExpression: printBinaryExpression,
129
+ LogicalExpression: printBinaryExpression,
130
+ AssignmentExpression: printBinaryExpression,
131
+ ConditionalExpression: printConditionalExpression,
132
+ YieldExpression: printYieldExpression,
133
+ AwaitExpression: printAwaitExpression,
134
+ SequenceExpression: printSequenceExpression,
135
+ CallExpression: printCallExpression,
136
+ NewExpression: printNewExpression,
137
+ ChainExpression: printChainExpression,
138
+ MemberExpression: printMemberExpression,
139
+ ObjectExpression: printObjectExpression,
140
+ ObjectPattern: printObjectPattern,
141
+ ArrayExpression: printArrayExpression,
142
+ ArrayPattern: printArrayPattern,
143
+ Property: printProperty,
144
+ SpreadElement: printSpreadElement,
145
+ RestElement: printRestElement,
146
+ AssignmentPattern: printAssignmentPattern,
147
+ TemplateLiteral: printTemplateLiteral,
148
+ TaggedTemplateExpression: printTaggedTemplateExpression,
149
+ ThisExpression: printThisExpression,
150
+ Super: printSuper,
151
+ MetaProperty: printMetaProperty,
152
+ ParenthesizedExpression: printParenthesizedExpression,
153
+ ClassDeclaration: printClassDeclaration,
154
+ ClassExpression: printClassExpression,
155
+ ClassBody: printClassBody,
156
+ StaticBlock: printStaticBlock,
157
+ PropertyDefinition: printPropertyDefinition,
158
+ AccessorProperty: printPropertyDefinition,
159
+ MethodDefinition: printMethodDefinition,
160
+ Decorator: printDecorator,
161
+ ImportDeclaration: printImportDeclaration,
162
+ ImportExpression: printImportExpression,
163
+ ImportSpecifier: printImportSpecifier,
164
+ ExportNamedDeclaration: printExportNamedDeclaration,
165
+ ExportDefaultDeclaration: printExportDefaultDeclaration,
166
+ ExportAllDeclaration: printExportAllDeclaration,
167
+ ExportSpecifier: printExportSpecifier,
168
+ TSAsExpression: printTSAsExpression,
169
+ TSSatisfiesExpression: printTSSatisfiesExpression,
170
+ TSTypeAssertion: printTSTypeAssertion,
171
+ TSNonNullExpression: printTSNonNullExpression,
172
+ TSTypeAnnotation: printTSTypeAnnotation,
173
+ TSTypeAliasDeclaration: printTSTypeAliasDeclaration,
174
+ TSInterfaceDeclaration: printTSInterfaceDeclaration,
175
+ TSExpressionWithTypeArguments: printTSExpressionWithTypeArguments,
176
+ TSClassImplements: printTSExpressionWithTypeArguments,
177
+ TSInterfaceHeritage: printTSExpressionWithTypeArguments,
178
+ TSFunctionType: printTSFunctionType,
179
+ TSConstructorType: printTSConstructorType,
180
+ TSMethodSignature: printTSMethodSignature,
181
+ TSCallSignatureDeclaration: printTSCallSignatureDeclaration,
182
+ TSConstructSignatureDeclaration: printTSConstructSignatureDeclaration,
183
+ TSIndexSignature: printTSIndexSignature,
184
+ TSPropertySignature: printTSPropertySignature,
185
+ TSTypeParameterDeclaration: printTypeParameterDeclaration,
186
+ TSTypeParameterInstantiation: printTypeParameterInstantiation,
187
+ TSTypeParameter: printTSTypeParameter,
188
+ TSTypeReference: printTSTypeReference,
189
+ TSQualifiedName: printTSQualifiedName,
190
+ TSUnionType: printJoinedTypes,
191
+ TSIntersectionType: printJoinedTypes,
192
+ TSArrayType: printTSArrayType,
193
+ TSTupleType: printTSTupleType,
194
+ TSNamedTupleMember: printTSNamedTupleMember,
195
+ TSTypeLiteral: printTSTypeLiteral,
196
+ TSTypeOperator: printTSTypeOperator,
197
+ TSTypePredicate: printTSTypePredicate,
198
+ TSTypeQuery: printTSTypeQuery,
199
+ TSMappedType: printTSMappedType,
200
+ TSConditionalType: printTSConditionalType,
201
+ TSInferType: printTSInferType,
202
+ TSIndexedAccessType: printTSIndexedAccessType,
203
+ TSOptionalType: printTSOptionalType,
204
+ TSRestType: printTSRestType,
205
+ TSThisType: printTSThisType,
206
+ TSLiteralType: printTSLiteralType,
207
+ TSTemplateLiteralType: printTSTemplateLiteralType,
208
+ TSImportType: printTSImportType,
209
+ TSImportEqualsDeclaration: printTSImportEqualsDeclaration,
210
+ TSExternalModuleReference: printTSExternalModuleReference,
211
+ TSEnumDeclaration: printTSEnumDeclaration,
212
+ TSEnumMember: printTSEnumMember,
213
+ TSModuleDeclaration: printTSModuleDeclaration,
214
+ TSModuleBlock: printTSModuleBlock,
215
+ TSDeclareFunction: printTSDeclareFunction,
216
+ TSParameterProperty: printTSParameterProperty,
217
+ TSAbstractMethodDefinition: printMethodDefinition,
218
+ TSAbstractPropertyDefinition: printPropertyDefinition,
219
+ TSAbstractAccessorProperty: printPropertyDefinition,
220
+ TSExportAssignment: printTSExportAssignment,
221
+ TSNamespaceExportDeclaration: printTSNamespaceExportDeclaration,
222
+ TSInstantiationExpression: printTSInstantiationExpression,
223
+ TSParenthesizedType: printTSParenthesizedType,
224
+ TSInterfaceBody: printTSInterfaceBody,
225
+ TSStringKeyword: printKeywordType,
226
+ TSNumberKeyword: printKeywordType,
227
+ TSBooleanKeyword: printKeywordType,
228
+ TSVoidKeyword: printKeywordType,
229
+ TSUnknownKeyword: printKeywordType,
230
+ TSAnyKeyword: printKeywordType,
231
+ TSNeverKeyword: printKeywordType,
232
+ TSNullKeyword: printKeywordType,
233
+ TSUndefinedKeyword: printKeywordType,
234
+ TSObjectKeyword: printKeywordType,
235
+ TSSymbolKeyword: printKeywordType,
236
+ TSBigIntKeyword: printKeywordType,
237
+ TSIntrinsicKeyword: printKeywordType
238
+ };
239
+ function printProgram(program, context) {
240
+ context.writeNodeListWithSourceGaps(program.body, "\n");
241
+ }
242
+ function printExpressionStatement(statement, context) {
243
+ const expr = statement.expression;
244
+ if (expr.type === "ObjectExpression" || expr.type === "FunctionExpression" || expr.type === "AssignmentExpression" && expr.left.type === "ObjectPattern") {
245
+ context.write("(");
246
+ context.writeNode(expr);
247
+ context.write(");");
248
+ } else {
249
+ context.writeNode(expr);
250
+ context.write(";");
251
+ }
252
+ }
253
+ function printEmptyStatement(_statement, context) {
254
+ context.write(";");
255
+ }
256
+ function printVariableDeclaration(declaration, context) {
257
+ if (declaration.declare === true) context.write("declare ");
258
+ context.write(String(declaration.kind));
259
+ context.write(" ");
260
+ context.writeNodeList(declaration.declarations, ", ");
261
+ context.write(";");
262
+ }
263
+ function printVariableDeclarator(declarator, context) {
264
+ context.writeNode(declarator.id);
265
+ if (declarator.definite === true) context.write("!");
266
+ if (declarator.init) {
267
+ context.write(" = ");
268
+ context.writeNode(declarator.init);
269
+ }
270
+ }
271
+ function printBlockStatement(block, context) {
272
+ const body = block.body;
273
+ context.write("{");
274
+ if (body.length > 0) {
275
+ context.write("\n");
276
+ context.writeNodeListWithSourceGaps(body, "\n");
277
+ context.write("\n");
278
+ }
279
+ context.write("}");
280
+ }
281
+ function printReturnStatement(statement, context) {
282
+ context.write("return");
283
+ if (statement.argument) {
284
+ context.write(" ");
285
+ const needsParensASi = (context.getLeadingComments?.(statement.argument))?.some((c) => commentNeedsNewline(c)) ?? false;
286
+ if (needsParensASi) context.write("(");
287
+ context.writeNode(statement.argument);
288
+ if (needsParensASi) context.write(")");
289
+ }
290
+ context.write(";");
291
+ }
292
+ function printThrowStatement(statement, context) {
293
+ context.write("throw ");
294
+ if (statement.argument) {
295
+ const needsParensASi = (context.getLeadingComments?.(statement.argument))?.some((c) => commentNeedsNewline(c)) ?? false;
296
+ if (needsParensASi) context.write("(");
297
+ context.writeNode(statement.argument);
298
+ if (needsParensASi) context.write(")");
299
+ }
300
+ context.write(";");
301
+ }
302
+ function printDebuggerStatement(_statement, context) {
303
+ context.write("debugger;");
304
+ }
305
+ function printBreakStatement(statement, context) {
306
+ context.write("break");
307
+ if (statement.label) {
308
+ context.write(" ");
309
+ context.writeNode(statement.label);
310
+ }
311
+ context.write(";");
312
+ }
313
+ function printContinueStatement(statement, context) {
314
+ context.write("continue");
315
+ if (statement.label) {
316
+ context.write(" ");
317
+ context.writeNode(statement.label);
318
+ }
319
+ context.write(";");
320
+ }
321
+ function printLabeledStatement(statement, context) {
322
+ context.writeNode(statement.label);
323
+ context.write(": ");
324
+ context.writeNode(statement.body);
325
+ }
326
+ function printWhileStatement(statement, context) {
327
+ context.write("while (");
328
+ context.writeNode(statement.test);
329
+ context.write(") ");
330
+ context.writeNode(statement.body);
331
+ }
332
+ function printDoWhileStatement(statement, context) {
333
+ context.write("do ");
334
+ context.writeNode(statement.body);
335
+ context.write(" while (");
336
+ context.writeNode(statement.test);
337
+ context.write(");");
338
+ }
339
+ function printIfStatement(statement, context) {
340
+ context.write("if (");
341
+ context.writeNode(statement.test);
342
+ context.write(") ");
343
+ context.writeNode(statement.consequent);
344
+ if (statement.alternate) {
345
+ context.write(" else ");
346
+ context.writeNode(statement.alternate);
347
+ }
348
+ }
349
+ function printForStatement(statement, context) {
350
+ context.write("for (");
351
+ if (statement.init) if (statement.init.type === "VariableDeclaration") printVariableDeclarationFor(statement.init, context);
352
+ else context.writeNode(statement.init);
353
+ context.write("; ");
354
+ if (statement.test) context.writeNode(statement.test);
355
+ context.write("; ");
356
+ if (statement.update) context.writeNode(statement.update);
357
+ context.write(") ");
358
+ context.writeNode(statement.body);
359
+ }
360
+ function printForInStatement(statement, context) {
361
+ context.write("for (");
362
+ if (statement.left.type === "VariableDeclaration") printVariableDeclarationFor(statement.left, context);
363
+ else context.writeNode(statement.left);
364
+ context.write(" in ");
365
+ context.writeNode(statement.right);
366
+ context.write(") ");
367
+ context.writeNode(statement.body);
368
+ }
369
+ function printForOfStatement(statement, context) {
370
+ context.write("for ");
371
+ if (statement.await === true) context.write("await ");
372
+ context.write("(");
373
+ if (statement.left.type === "VariableDeclaration") printVariableDeclarationFor(statement.left, context);
374
+ else context.writeNode(statement.left);
375
+ context.write(" of ");
376
+ context.writeNode(statement.right);
377
+ context.write(") ");
378
+ context.writeNode(statement.body);
379
+ }
380
+ function printVariableDeclarationFor(declaration, context) {
381
+ context.write(String(declaration.kind));
382
+ context.write(" ");
383
+ context.writeNodeList(declaration.declarations, ", ");
384
+ }
385
+ function printSwitchStatement(statement, context) {
386
+ context.write("switch (");
387
+ context.writeNode(statement.discriminant);
388
+ context.write(") {");
389
+ for (const case_ of statement.cases) context.writeNode(case_);
390
+ context.write("}");
391
+ }
392
+ function printSwitchCase(case_, context) {
393
+ if (case_.test) {
394
+ context.write("\ncase ");
395
+ context.writeNode(case_.test);
396
+ context.write(":");
397
+ } else context.write("\ndefault:");
398
+ for (const stmt of case_.consequent) {
399
+ context.write("\n");
400
+ context.writeNode(stmt);
401
+ }
402
+ }
403
+ function printTryStatement(statement, context) {
404
+ context.write("try ");
405
+ context.writeNode(statement.block);
406
+ if (statement.handler) {
407
+ context.write(" catch");
408
+ if (statement.handler.param) {
409
+ context.write(" (");
410
+ context.writeNode(statement.handler.param);
411
+ context.write(") ");
412
+ } else context.write(" ");
413
+ context.writeNode(statement.handler.body);
414
+ }
415
+ if (statement.finalizer) {
416
+ context.write(" finally ");
417
+ context.writeNode(statement.finalizer);
418
+ }
419
+ }
420
+ function printWithStatement(statement, context) {
421
+ context.write("with (");
422
+ context.writeNode(statement.object);
423
+ context.write(") ");
424
+ context.writeNode(statement.body);
425
+ }
426
+ function printIdentifier(identifier, context) {
427
+ context.write(String(identifier.name));
428
+ writeOptionalTypeAnnotation(identifier, context);
429
+ }
430
+ function printPrivateIdentifier(identifier, context) {
431
+ context.write("#");
432
+ context.write(String(identifier.name));
433
+ }
434
+ function printLiteral(literal, context) {
435
+ if (typeof literal.raw === "string") {
436
+ context.write(literal.raw);
437
+ return;
438
+ }
439
+ const l = literal;
440
+ context.write(JSON.stringify(l.value));
441
+ }
442
+ function printUnaryExpression(expr, context) {
443
+ context.write(expr.operator);
444
+ if (expr.operator.length > 1) context.write(" ");
445
+ const argPrec = EXPRESSIONS_PRECEDENCE[expr.argument.type];
446
+ if (argPrec != null && argPrec < EXPRESSIONS_PRECEDENCE.UnaryExpression) {
447
+ context.write("(");
448
+ context.writeNode(expr.argument);
449
+ context.write(")");
450
+ } else context.writeNode(expr.argument);
451
+ }
452
+ function printUpdateExpression(expr, context) {
453
+ if (expr.prefix === true) {
454
+ context.write(expr.operator);
455
+ context.writeNode(expr.argument);
456
+ } else {
457
+ const needsParensASi = (context.getTrailingComments?.(expr.argument))?.some((c) => commentNeedsNewline(c)) ?? false;
458
+ if (needsParensASi) context.write("(");
459
+ context.writeNode(expr.argument);
460
+ if (needsParensASi) context.write(")");
461
+ context.write(expr.operator);
462
+ }
463
+ }
464
+ function printBinaryExpression(expression, context) {
465
+ const left = expression.left;
466
+ const right = expression.right;
467
+ if (needsParens(left, expression, false)) {
468
+ context.write("(");
469
+ context.writeNode(left);
470
+ context.write(")");
471
+ } else context.writeNode(left);
472
+ context.write(" ");
473
+ context.write(String(expression.operator));
474
+ context.write(" ");
475
+ if (needsParens(right, expression, true)) {
476
+ context.write("(");
477
+ context.writeNode(right);
478
+ context.write(")");
479
+ } else context.writeNode(right);
480
+ }
481
+ function printConditionalExpression(expr, context) {
482
+ if ((EXPRESSIONS_PRECEDENCE[expr.test.type] ?? 20) <= EXPRESSIONS_PRECEDENCE.ConditionalExpression) {
483
+ context.write("(");
484
+ context.writeNode(expr.test);
485
+ context.write(")");
486
+ } else context.writeNode(expr.test);
487
+ context.write(" ? ");
488
+ context.writeNode(expr.consequent);
489
+ context.write(" : ");
490
+ context.writeNode(expr.alternate);
491
+ }
492
+ function printYieldExpression(expr, context) {
493
+ context.write(expr.delegate === true ? "yield*" : "yield");
494
+ if (expr.argument) {
495
+ context.write(" ");
496
+ const needsParensASi = (context.getLeadingComments?.(expr.argument))?.some((c) => commentNeedsNewline(c)) ?? false;
497
+ if (needsParensASi) context.write("(");
498
+ context.writeNode(expr.argument);
499
+ if (needsParensASi) context.write(")");
500
+ }
501
+ }
502
+ function printAwaitExpression(expr, context) {
503
+ context.write("await");
504
+ if (expr.argument) {
505
+ const argPrec = EXPRESSIONS_PRECEDENCE[expr.argument.type];
506
+ if (argPrec != null && argPrec < EXPRESSIONS_PRECEDENCE.AwaitExpression) {
507
+ context.write(" (");
508
+ context.writeNode(expr.argument);
509
+ context.write(")");
510
+ } else {
511
+ context.write(" ");
512
+ context.writeNode(expr.argument);
513
+ }
514
+ }
515
+ }
516
+ function printSequenceExpression(expr, context) {
517
+ context.write("(");
518
+ context.writeNodeList(expr.expressions, ", ");
519
+ context.write(")");
520
+ }
521
+ function printCallExpression(expression, context) {
522
+ if ((EXPRESSIONS_PRECEDENCE[expression.callee.type] ?? 20) < EXPRESSIONS_PRECEDENCE.CallExpression) {
523
+ context.write("(");
524
+ context.writeNode(expression.callee);
525
+ context.write(")");
526
+ } else context.writeNode(expression.callee);
527
+ if (expression.typeArguments) context.writeNode(expression.typeArguments);
528
+ context.write(expression.optional === true ? "?.(" : "(");
529
+ context.writeNodeList(expression.arguments, ", ");
530
+ context.write(")");
531
+ }
532
+ function printNewExpression(expression, context) {
533
+ context.write("new ");
534
+ if ((EXPRESSIONS_PRECEDENCE[expression.callee.type] ?? 20) < EXPRESSIONS_PRECEDENCE.NewExpression || hasCallExpression(expression.callee)) {
535
+ context.write("(");
536
+ context.writeNode(expression.callee);
537
+ context.write(")");
538
+ } else context.writeNode(expression.callee);
539
+ if (expression.typeArguments) context.writeNode(expression.typeArguments);
540
+ context.write("(");
541
+ context.writeNodeList(expression.arguments, ", ");
542
+ context.write(")");
543
+ }
544
+ function hasCallExpression(node) {
545
+ let cur = node;
546
+ while (cur) {
547
+ if (cur.type === "CallExpression") return true;
548
+ if (cur.type === "MemberExpression") cur = cur.object;
549
+ else return false;
550
+ }
551
+ return false;
552
+ }
553
+ function printChainExpression(expression, context) {
554
+ context.writeNode(expression.expression);
555
+ }
556
+ function printMemberExpression(expression, context) {
557
+ if ((EXPRESSIONS_PRECEDENCE[expression.object.type] ?? 20) < EXPRESSIONS_PRECEDENCE.MemberExpression) {
558
+ context.write("(");
559
+ context.writeNode(expression.object);
560
+ context.write(")");
561
+ } else context.writeNode(expression.object);
562
+ if (expression.computed === true) {
563
+ context.write(expression.optional === true ? "?.[" : "[");
564
+ context.writeNode(expression.property);
565
+ context.write("]");
566
+ } else {
567
+ context.write(expression.optional === true ? "?." : ".");
568
+ context.writeNode(expression.property);
569
+ }
570
+ }
571
+ function printObjectExpression(object, context) {
572
+ context.write("{ ");
573
+ context.writeNodeList(object.properties, ", ");
574
+ context.write(" }");
575
+ }
576
+ function printObjectPattern(pattern, context) {
577
+ context.write("{ ");
578
+ context.writeNodeList(pattern.properties, ", ");
579
+ context.write(" }");
580
+ writeOptionalTypeAnnotation(pattern, context);
581
+ }
582
+ function printArrayExpression(array, context) {
583
+ context.write("[");
584
+ context.writeNodeList(array.elements, ", ");
585
+ context.write("]");
586
+ }
587
+ function printArrayPattern(pattern, context) {
588
+ context.write("[");
589
+ context.writeNodeList(pattern.elements, ", ");
590
+ context.write("]");
591
+ writeOptionalTypeAnnotation(pattern, context);
592
+ }
593
+ function printProperty(property, context) {
594
+ if (property.type === "Property") {
595
+ const value = property.value;
596
+ const valNode = value.type === "AssignmentPattern" ? value.left : value;
597
+ if (!property.computed && property.kind === "init" && property.key.type === "Identifier" && valNode.type === "Identifier" && property.key.name === valNode.name) {
598
+ context.writeNode(value);
599
+ return;
600
+ }
601
+ if (value.type === "FunctionExpression") {
602
+ if (property.kind !== "init") context.write(property.kind + " ");
603
+ if (value.async === true) context.write("async ");
604
+ if (value.generator === true) context.write("*");
605
+ if (property.computed === true) {
606
+ context.write("[");
607
+ context.writeNode(property.key);
608
+ context.write("]");
609
+ } else context.writeNode(property.key);
610
+ context.write("(");
611
+ context.writeNodeList(value.params, ", ");
612
+ context.write(")");
613
+ writeReturnType(value, context);
614
+ context.write(" ");
615
+ context.writeNode(value.body);
616
+ return;
617
+ }
618
+ if (property.computed === true) {
619
+ context.write("[");
620
+ context.writeNode(property.key);
621
+ context.write("]: ");
622
+ } else {
623
+ if (property.kind === "get" || property.kind === "set") context.write(property.kind + " ");
624
+ context.writeNode(property.key);
625
+ context.write(": ");
626
+ }
627
+ context.writeNode(property.value);
628
+ }
629
+ }
630
+ function printSpreadElement(spread, context) {
631
+ context.write("...");
632
+ context.writeNode(spread.argument);
633
+ }
634
+ function printRestElement(rest, context) {
635
+ context.write("...");
636
+ context.writeNode(rest.argument);
637
+ writeOptionalTypeAnnotation(rest, context);
638
+ }
639
+ function printAssignmentPattern(pattern, context) {
640
+ context.writeNode(pattern.left);
641
+ context.write(" = ");
642
+ context.writeNode(pattern.right);
643
+ }
644
+ function printTemplateLiteral(node, context) {
645
+ context.write("`");
646
+ const { quasis, expressions } = node;
647
+ for (let i = 0; i < expressions.length; i++) {
648
+ context.write(quasis[i].value.raw);
649
+ context.write("${");
650
+ context.writeNode(expressions[i]);
651
+ context.write("}");
652
+ }
653
+ context.write(quasis[quasis.length - 1].value.raw);
654
+ context.write("`");
655
+ }
656
+ function printTaggedTemplateExpression(node, context) {
657
+ context.writeNode(node.tag);
658
+ context.writeNode(node.quasi);
659
+ }
660
+ function printThisExpression(_node, context) {
661
+ context.write("this");
662
+ }
663
+ function printSuper(_node, context) {
664
+ context.write("super");
665
+ }
666
+ function printMetaProperty(node, context) {
667
+ context.writeNode(node.meta);
668
+ context.write(".");
669
+ context.writeNode(node.property);
670
+ }
671
+ function printParenthesizedExpression(node, context) {
672
+ context.write("(");
673
+ context.writeNode(node.expression);
674
+ context.write(")");
675
+ }
676
+ function printFunctionDeclaration(node, context) {
677
+ printFunction(node, context);
678
+ }
679
+ function printFunctionExpression(node, context) {
680
+ printFunction(node, context);
681
+ }
682
+ function printFunction(fn, context) {
683
+ if (fn.async === true) context.write("async ");
684
+ context.write("function");
685
+ if (fn.generator === true) context.write("*");
686
+ if (fn.id) {
687
+ context.write(" ");
688
+ context.writeNode(fn.id);
689
+ }
690
+ if (fn.typeParameters) context.writeNode(fn.typeParameters);
691
+ context.write("(");
692
+ context.writeNodeList(fn.params, ", ");
693
+ context.write(")");
694
+ writeReturnType(fn, context);
695
+ if (fn.body) {
696
+ context.write(" ");
697
+ context.writeNode(fn.body);
698
+ }
699
+ }
700
+ function printArrowFunctionExpression(fn, context) {
701
+ if (fn.async === true) context.write("async ");
702
+ if (fn.typeParameters) context.writeNode(fn.typeParameters);
703
+ context.write("(");
704
+ context.writeNodeList(fn.params, ", ");
705
+ context.write(")");
706
+ writeReturnType(fn, context);
707
+ context.write(" => ");
708
+ const body = fn.body;
709
+ if (arrowConciseBodyNeedsWrap(body)) {
710
+ context.write("(");
711
+ context.writeNode(body);
712
+ context.write(")");
713
+ } else context.writeNode(body);
714
+ }
715
+ function printClassDeclaration(node, context) {
716
+ printClass(node, context);
717
+ }
718
+ function printClassExpression(node, context) {
719
+ printClass(node, context);
720
+ }
721
+ function printClass(node, context) {
722
+ if (node.decorators) for (const d of node.decorators) context.writeNode(d);
723
+ if (node.declare === true) context.write("declare ");
724
+ if (node.abstract === true) context.write("abstract ");
725
+ context.write("class ");
726
+ if (node.id) {
727
+ context.writeNode(node.id);
728
+ context.write(" ");
729
+ }
730
+ if (node.superClass) {
731
+ context.write("extends ");
732
+ context.writeNode(node.superClass);
733
+ if (node.superTypeArguments) context.writeNode(node.superTypeArguments);
734
+ else if (node.superTypeParameters) context.writeNode(node.superTypeParameters);
735
+ }
736
+ if (node.implements && node.implements.length > 0) {
737
+ context.write(" implements ");
738
+ context.writeNodeList(node.implements, ", ");
739
+ }
740
+ if (node.superClass || node.implements && node.implements.length > 0) context.write(" ");
741
+ context.writeNode(node.body);
742
+ }
743
+ function printClassBody(node, context) {
744
+ context.write("{");
745
+ const body = node.body;
746
+ if (body.length > 0) {
747
+ context.write("\n");
748
+ context.writeNodeListWithSourceGaps(body, "\n");
749
+ context.write("\n");
750
+ }
751
+ context.write("}");
752
+ }
753
+ function printStaticBlock(node, context) {
754
+ context.write("static {");
755
+ const body = node.body;
756
+ if (body.length > 0) {
757
+ context.write("\n");
758
+ context.writeNodeListWithSourceGaps(body, "\n");
759
+ context.write("\n");
760
+ }
761
+ context.write("}");
762
+ }
763
+ function printDecorator(node, context) {
764
+ context.write("@");
765
+ context.writeNode(node.expression);
766
+ context.write("\n");
767
+ }
768
+ function printPropertyDefinition(node, context) {
769
+ if (node.decorators) for (const d of node.decorators) context.writeNode(d);
770
+ if (node.type === "TSAbstractPropertyDefinition" || "abstract" in node && node.abstract === true) context.write("abstract ");
771
+ if (node.accessibility) context.write(node.accessibility + " ");
772
+ if (node.static === true) context.write("static ");
773
+ if (node.override === true) context.write("override ");
774
+ if (node.readonly === true) context.write("readonly ");
775
+ if (node.type === "AccessorProperty" || node.type === "TSAbstractAccessorProperty" || "accessor" in node && node.accessor === true) context.write("accessor ");
776
+ if (node.computed === true) {
777
+ context.write("[");
778
+ context.writeNode(node.key);
779
+ context.write("]");
780
+ } else context.writeNode(node.key);
781
+ if (node.typeAnnotation) if ("accessor" in node && node.accessor === true) context.writeNode(node.typeAnnotation);
782
+ else {
783
+ context.write(": ");
784
+ context.writeNode(node.typeAnnotation.typeAnnotation);
785
+ }
786
+ if (node.value) {
787
+ context.write(" = ");
788
+ context.writeNode(node.value);
789
+ }
790
+ context.write(";");
791
+ }
792
+ function printMethodDefinition(node, context) {
793
+ const def = node;
794
+ if (def.decorators) for (const d of def.decorators) context.writeNode(d);
795
+ if (def.accessibility) context.write(def.accessibility + " ");
796
+ if (def.static === true) context.write("static ");
797
+ if (def.override === true) context.write("override ");
798
+ if (node.type === "TSAbstractMethodDefinition" || "abstract" in def && def.abstract === true) context.write("abstract ");
799
+ if (def.kind === "get" || def.kind === "set") context.write(def.kind + " ");
800
+ if (def.value.async === true) context.write("async ");
801
+ if (def.value.generator === true) context.write("*");
802
+ if (def.computed === true) {
803
+ context.write("[");
804
+ context.writeNode(def.key);
805
+ context.write("]");
806
+ } else context.writeNode(def.key);
807
+ context.write("(");
808
+ context.writeNodeList(def.value.params, ", ");
809
+ context.write(")");
810
+ writeReturnType(def.value, context);
811
+ if (def.value.body) {
812
+ context.write(" ");
813
+ context.writeNode(def.value.body);
814
+ } else context.write(";");
815
+ }
816
+ function printImportDeclaration(node, context) {
817
+ context.write("import ");
818
+ if (node.importKind === "type") context.write("type ");
819
+ const specifiers = node.specifiers;
820
+ if (specifiers.length === 0) {
821
+ context.writeNode(node.source);
822
+ context.write(";");
823
+ return;
824
+ }
825
+ let wroteDefault = false;
826
+ for (const s of specifiers) if (s.type === "ImportDefaultSpecifier") {
827
+ context.writeNode(s.local);
828
+ wroteDefault = true;
829
+ }
830
+ let namespaceSpec;
831
+ const namedSpecs = [];
832
+ for (const s of specifiers) if (s.type === "ImportNamespaceSpecifier") namespaceSpec = s;
833
+ else if (s.type === "ImportSpecifier") namedSpecs.push(s);
834
+ if (namespaceSpec) {
835
+ if (wroteDefault) context.write(", ");
836
+ context.write("* as ");
837
+ context.writeNode(namespaceSpec.local);
838
+ }
839
+ if (namedSpecs.length > 0) {
840
+ if (wroteDefault || namespaceSpec) context.write(", ");
841
+ context.write("{ ");
842
+ context.writeNodeList(namedSpecs, ", ");
843
+ context.write(" }");
844
+ }
845
+ context.write(" from ");
846
+ context.writeNode(node.source);
847
+ if (node.attributes && node.attributes.length > 0) {
848
+ context.write(" with { ");
849
+ context.writeNodeList(node.attributes, ", ");
850
+ context.write(" }");
851
+ }
852
+ context.write(";");
853
+ }
854
+ function printImportExpression(node, context) {
855
+ context.write("import(");
856
+ context.writeNode(node.source);
857
+ if (node.options) {
858
+ context.write(", ");
859
+ context.writeNode(node.options);
860
+ }
861
+ context.write(")");
862
+ }
863
+ function printImportSpecifier(node, context) {
864
+ if (node.importKind === "type") context.write("type ");
865
+ if (node.local.type === "Identifier" && node.imported.type === "Identifier" && node.local.name !== node.imported.name) {
866
+ context.writeNode(node.imported);
867
+ context.write(" as ");
868
+ }
869
+ context.writeNode(node.local);
870
+ }
871
+ function printExportNamedDeclaration(node, context) {
872
+ if (node.declaration) {
873
+ let decl = node.declaration;
874
+ if ("decorators" in decl && decl.decorators && decl.decorators.length > 0) {
875
+ const { decorators, ...rest } = decl;
876
+ for (const d of decorators) context.writeNode(d);
877
+ decl = rest;
878
+ }
879
+ context.write("export ");
880
+ if (node.exportKind === "type") context.write("type ");
881
+ context.writeNode(decl);
882
+ if (decl.type !== "FunctionDeclaration" && decl.type !== "ClassDeclaration" && decl.type !== "VariableDeclaration" && decl.type !== "TSModuleDeclaration" && decl.type !== "TSEnumDeclaration" && decl.type !== "TSTypeAliasDeclaration" && decl.type !== "TSInterfaceDeclaration" && decl.type !== "TSDeclareFunction") context.write(";");
883
+ return;
884
+ }
885
+ context.write("export ");
886
+ if (node.exportKind === "type") context.write("type ");
887
+ context.write("{ ");
888
+ context.writeNodeList(node.specifiers, ", ");
889
+ context.write(" }");
890
+ if (node.source) {
891
+ context.write(" from ");
892
+ context.writeNode(node.source);
893
+ }
894
+ context.write(";");
895
+ }
896
+ function printExportDefaultDeclaration(node, context) {
897
+ let decl = node.declaration;
898
+ if ("decorators" in decl && decl.decorators && decl.decorators.length > 0) {
899
+ for (const d of decl.decorators) context.writeNode(d);
900
+ const { decorators: _, ...rest } = decl;
901
+ decl = rest;
902
+ }
903
+ context.write("export default ");
904
+ context.writeNode(decl);
905
+ if (decl.type !== "FunctionDeclaration" && decl.type !== "ClassDeclaration" && decl.type !== "ClassExpression") context.write(";");
906
+ }
907
+ function printExportAllDeclaration(node, context) {
908
+ context.write(node.exportKind === "type" ? "export type * " : "export * ");
909
+ if (node.exported) {
910
+ context.write("as ");
911
+ context.writeNode(node.exported);
912
+ context.write(" ");
913
+ }
914
+ context.write("from ");
915
+ context.writeNode(node.source);
916
+ context.write(";");
917
+ }
918
+ function printExportSpecifier(node, context) {
919
+ if (node.exportKind === "type") context.write("type ");
920
+ context.writeNode(node.local);
921
+ if (node.local.type === "Identifier" && node.exported.type === "Identifier" && node.local.name !== node.exported.name) {
922
+ context.write(" as ");
923
+ context.writeNode(node.exported);
924
+ }
925
+ }
926
+ function printTSAsExpression(expression, context) {
927
+ if ((EXPRESSIONS_PRECEDENCE[expression.expression.type] ?? 20) < EXPRESSIONS_PRECEDENCE.TSAsExpression) {
928
+ context.write("(");
929
+ context.writeNode(expression.expression);
930
+ context.write(")");
931
+ } else context.writeNode(expression.expression);
932
+ context.write(" as ");
933
+ context.writeNode(expression.typeAnnotation);
934
+ }
935
+ function printTSSatisfiesExpression(expression, context) {
936
+ if ((EXPRESSIONS_PRECEDENCE[expression.expression.type] ?? 20) < EXPRESSIONS_PRECEDENCE.TSSatisfiesExpression) {
937
+ context.write("(");
938
+ context.writeNode(expression.expression);
939
+ context.write(")");
940
+ } else context.writeNode(expression.expression);
941
+ context.write(" satisfies ");
942
+ context.writeNode(expression.typeAnnotation);
943
+ }
944
+ function printTSTypeAssertion(expression, context) {
945
+ context.write("<");
946
+ context.writeNode(expression.typeAnnotation);
947
+ context.write(">");
948
+ if ((EXPRESSIONS_PRECEDENCE[expression.expression.type] ?? 20) < EXPRESSIONS_PRECEDENCE.TSTypeAssertion) {
949
+ context.write("(");
950
+ context.writeNode(expression.expression);
951
+ context.write(")");
952
+ } else context.writeNode(expression.expression);
953
+ }
954
+ function printTSNonNullExpression(expression, context) {
955
+ context.writeNode(expression.expression);
956
+ context.write("!");
957
+ }
958
+ function printTSTypeAnnotation(annotation, context) {
959
+ context.write(": ");
960
+ context.writeNode(annotation.typeAnnotation);
961
+ }
962
+ function printTSTypeAliasDeclaration(alias, context) {
963
+ if (alias.declare === true) context.write("declare ");
964
+ context.write("type ");
965
+ context.writeNode(alias.id);
966
+ if (alias.typeParameters) context.writeNode(alias.typeParameters);
967
+ context.write(" = ");
968
+ context.writeNode(alias.typeAnnotation);
969
+ context.write(";");
970
+ }
971
+ function printTSInterfaceDeclaration(node, context) {
972
+ const declaration = node;
973
+ if (declaration.declare === true) context.write("declare ");
974
+ context.write("interface ");
975
+ context.writeNode(declaration.id);
976
+ if (declaration.typeParameters) context.writeNode(declaration.typeParameters);
977
+ const heritage = declaration.extends ?? [];
978
+ if (heritage.length > 0) {
979
+ context.write(" extends ");
980
+ context.writeNodeList(heritage, ", ");
981
+ }
982
+ context.write(" ");
983
+ context.writeNode(declaration.body);
984
+ }
985
+ function printTSExpressionWithTypeArguments(node, context) {
986
+ context.writeNode(node.expression);
987
+ if (node.typeArguments) context.writeNode(node.typeArguments);
988
+ if (node.typeParameters) context.writeNode(node.typeParameters);
989
+ }
990
+ function printTSInterfaceBody(body, context) {
991
+ context.write("{");
992
+ const members = body.body;
993
+ if (members.length > 0) {
994
+ context.write(" ");
995
+ context.writeNodeList(members, " ");
996
+ context.write(" ");
997
+ }
998
+ context.write("}");
999
+ }
1000
+ function printTSPropertySignature(signature, context) {
1001
+ if (signature.readonly === true) context.write("readonly ");
1002
+ if (signature.computed === true) {
1003
+ context.write("[");
1004
+ context.writeNode(signature.key);
1005
+ context.write("]");
1006
+ } else context.writeNode(signature.key);
1007
+ if (signature.optional === true) context.write("?");
1008
+ if (signature.typeAnnotation) context.writeNode(signature.typeAnnotation);
1009
+ context.write(";");
1010
+ }
1011
+ function printTypeParameterDeclaration(declaration, context) {
1012
+ context.write("<");
1013
+ context.writeNodeList(declaration.params, ", ");
1014
+ context.write(">");
1015
+ }
1016
+ function printTypeParameterInstantiation(instantiation, context) {
1017
+ context.write("<");
1018
+ context.writeNodeList(instantiation.params, ", ");
1019
+ context.write(">");
1020
+ }
1021
+ function printTSTypeParameter(parameter, context) {
1022
+ if (parameter.const === true) context.write("const ");
1023
+ if (parameter.in === true) context.write("in ");
1024
+ if (parameter.out === true) context.write("out ");
1025
+ if (typeof parameter.name === "string") context.write(parameter.name);
1026
+ else context.writeNode(parameter.name);
1027
+ if (parameter.constraint) {
1028
+ context.write(" extends ");
1029
+ context.writeNode(parameter.constraint);
1030
+ }
1031
+ if (parameter.default) {
1032
+ context.write(" = ");
1033
+ context.writeNode(parameter.default);
1034
+ }
1035
+ }
1036
+ function printTSFunctionType(type, context) {
1037
+ if (type.typeParameters) context.writeNode(type.typeParameters);
1038
+ context.write("(");
1039
+ if (type.params) context.writeNodeList(type.params, ", ");
1040
+ else if (type.parameters) context.writeNodeList(type.parameters, ", ");
1041
+ context.write(")");
1042
+ context.write(" => ");
1043
+ writeReturnType(type, context, { tsArrowType: true });
1044
+ }
1045
+ function printTSConstructorType(type, context) {
1046
+ context.write("new ");
1047
+ if (type.typeParameters) context.writeNode(type.typeParameters);
1048
+ context.write("(");
1049
+ if (type.params) context.writeNodeList(type.params, ", ");
1050
+ else if (type.parameters) context.writeNodeList(type.parameters, ", ");
1051
+ context.write(")");
1052
+ context.write(" => ");
1053
+ writeReturnType(type, context, { tsArrowType: true });
1054
+ }
1055
+ function printTSMethodSignature(signature, context) {
1056
+ if (signature.readonly === true) context.write("readonly ");
1057
+ if (signature.computed === true) {
1058
+ context.write("[");
1059
+ context.writeNode(signature.key);
1060
+ context.write("]");
1061
+ } else context.writeNode(signature.key);
1062
+ if (signature.optional === true) context.write("?");
1063
+ if (signature.typeParameters) context.writeNode(signature.typeParameters);
1064
+ context.write("(");
1065
+ if (signature.params) context.writeNodeList(signature.params, ", ");
1066
+ else if (signature.parameters) context.writeNodeList(signature.parameters, ", ");
1067
+ context.write(")");
1068
+ writeReturnType(signature, context);
1069
+ context.write(";");
1070
+ }
1071
+ function printTSCallSignatureDeclaration(signature, context) {
1072
+ if (signature.typeParameters) context.writeNode(signature.typeParameters);
1073
+ context.write("(");
1074
+ if (signature.params) context.writeNodeList(signature.params, ", ");
1075
+ else if (signature.parameters) context.writeNodeList(signature.parameters, ", ");
1076
+ context.write(")");
1077
+ writeReturnType(signature, context);
1078
+ context.write(";");
1079
+ }
1080
+ function printTSConstructSignatureDeclaration(signature, context) {
1081
+ context.write("new ");
1082
+ if (signature.typeParameters) context.writeNode(signature.typeParameters);
1083
+ context.write("(");
1084
+ if (signature.params) context.writeNodeList(signature.params, ", ");
1085
+ else if (signature.parameters) context.writeNodeList(signature.parameters, ", ");
1086
+ context.write(")");
1087
+ writeReturnType(signature, context);
1088
+ context.write(";");
1089
+ }
1090
+ function printTSIndexSignature(signature, context) {
1091
+ context.write("[");
1092
+ if (signature.parameters) context.writeNodeList(signature.parameters, ", ");
1093
+ context.write("]");
1094
+ writeReturnType(signature, context);
1095
+ context.write(";");
1096
+ }
1097
+ function writeReturnType(node, context, options) {
1098
+ let ret = node.returnType ?? node.typeAnnotation;
1099
+ if (options?.tsArrowType) ret = ret?.typeAnnotation;
1100
+ if (ret) context.writeNode(ret);
1101
+ }
1102
+ function printTSTypeReference(reference, context) {
1103
+ context.writeNode(reference.typeName);
1104
+ if (reference.typeArguments) context.writeNode(reference.typeArguments);
1105
+ }
1106
+ function printTSQualifiedName(name, context) {
1107
+ context.writeNode(name.left);
1108
+ context.write(".");
1109
+ context.writeNode(name.right);
1110
+ }
1111
+ function printJoinedTypes(joined, context) {
1112
+ context.writeNodeList(joined.types, joined.type === "TSUnionType" ? " | " : " & ");
1113
+ }
1114
+ function printTSArrayType(array, context) {
1115
+ context.writeNode(array.elementType);
1116
+ context.write("[]");
1117
+ }
1118
+ function printTSTupleType(node, context) {
1119
+ context.write("[");
1120
+ context.writeNodeList(node.elementTypes, ", ");
1121
+ context.write("]");
1122
+ }
1123
+ function printTSNamedTupleMember(node, context) {
1124
+ context.writeNode(node.label);
1125
+ context.write(": ");
1126
+ context.writeNode(node.elementType);
1127
+ }
1128
+ function printTSTypeLiteral(literal, context) {
1129
+ context.write("{ ");
1130
+ context.writeNodeList(literal.members, " ");
1131
+ context.write(" }");
1132
+ }
1133
+ function printTSTypeOperator(node, context) {
1134
+ context.write(node.operator + " ");
1135
+ if (node.typeAnnotation) context.writeNode(node.typeAnnotation);
1136
+ }
1137
+ function printTSTypePredicate(node, context) {
1138
+ if (node.asserts === true) context.write("asserts ");
1139
+ if (node.parameterName) context.writeNode(node.parameterName);
1140
+ else if (node.typeAnnotation) context.writeNode(node.typeAnnotation);
1141
+ if (node.asserts !== true || node.typeAnnotation) context.write(" is ");
1142
+ if (node.typeAnnotation) context.writeNode(node.typeAnnotation.typeAnnotation);
1143
+ }
1144
+ function printTSTypeQuery(node, context) {
1145
+ context.write("typeof ");
1146
+ context.writeNode(node.exprName);
1147
+ }
1148
+ function printTSMappedType(node, context) {
1149
+ context.write("{ [");
1150
+ const legacyTp = node.typeParameter;
1151
+ const key = node.key ?? legacyTp?.name;
1152
+ const constraint = node.constraint ?? legacyTp?.constraint;
1153
+ if (key) if (typeof key === "string") context.write(key);
1154
+ else context.writeNode(key);
1155
+ if (constraint) {
1156
+ context.write(" in ");
1157
+ context.writeNode(constraint);
1158
+ }
1159
+ context.write("]");
1160
+ if (node.typeAnnotation) {
1161
+ context.write(": ");
1162
+ context.writeNode(node.typeAnnotation);
1163
+ }
1164
+ context.write(" }");
1165
+ }
1166
+ function printTSConditionalType(node, context) {
1167
+ context.writeNode(node.checkType);
1168
+ context.write(" extends ");
1169
+ context.writeNode(node.extendsType);
1170
+ context.write(" ? ");
1171
+ context.writeNode(node.trueType);
1172
+ context.write(" : ");
1173
+ context.writeNode(node.falseType);
1174
+ }
1175
+ function printTSInferType(node, context) {
1176
+ context.write("infer ");
1177
+ context.writeNode(node.typeParameter);
1178
+ }
1179
+ function printTSIndexedAccessType(node, context) {
1180
+ context.writeNode(node.objectType);
1181
+ context.write("[");
1182
+ context.writeNode(node.indexType);
1183
+ context.write("]");
1184
+ }
1185
+ function printTSOptionalType(node, context) {
1186
+ context.writeNode(node.typeAnnotation);
1187
+ context.write("?");
1188
+ }
1189
+ function printTSRestType(node, context) {
1190
+ context.write("...");
1191
+ context.writeNode(node.typeAnnotation);
1192
+ }
1193
+ function printTSThisType(_node, context) {
1194
+ context.write("this");
1195
+ }
1196
+ function printTSLiteralType(literal, context) {
1197
+ context.writeNode(literal.literal);
1198
+ }
1199
+ function printTSTemplateLiteralType(node, context) {
1200
+ context.write("`");
1201
+ const { quasis, types } = node;
1202
+ for (let i = 0; i < types.length; i++) {
1203
+ context.write(quasis[i].value.raw);
1204
+ context.write("${");
1205
+ context.writeNode(types[i]);
1206
+ context.write("}");
1207
+ }
1208
+ context.write(quasis[quasis.length - 1].value.raw);
1209
+ context.write("`");
1210
+ }
1211
+ function printTSImportType(node, context) {
1212
+ context.write("import(");
1213
+ context.writeNode(node.argument);
1214
+ context.write(")");
1215
+ if (node.qualifier) {
1216
+ context.write(".");
1217
+ context.writeNode(node.qualifier);
1218
+ }
1219
+ }
1220
+ function printTSImportEqualsDeclaration(node, context) {
1221
+ context.write("import ");
1222
+ context.writeNode(node.id);
1223
+ context.write(" = ");
1224
+ context.writeNode(node.moduleReference);
1225
+ context.write(";");
1226
+ }
1227
+ function printTSExternalModuleReference(node, context) {
1228
+ context.write("require(");
1229
+ context.writeNode(node.expression);
1230
+ context.write(")");
1231
+ }
1232
+ function printTSEnumDeclaration(node, context) {
1233
+ if (node.declare === true) context.write("declare ");
1234
+ if (node.const === true) context.write("const ");
1235
+ context.write("enum ");
1236
+ context.writeNode(node.id);
1237
+ context.write(" { ");
1238
+ context.writeNodeList(node.members, ", ");
1239
+ context.write(" }");
1240
+ }
1241
+ function printTSEnumMember(node, context) {
1242
+ context.writeNode(node.id);
1243
+ if (node.initializer) {
1244
+ context.write(" = ");
1245
+ context.writeNode(node.initializer);
1246
+ }
1247
+ }
1248
+ function printTSModuleDeclaration(node, context) {
1249
+ if (node.declare === true) context.write("declare ");
1250
+ if (node.global === true) context.write("global");
1251
+ else {
1252
+ const kind = node.kind ?? (node.id && node.id.type === "Literal" ? "module" : "namespace");
1253
+ context.write(String(kind) + " ");
1254
+ context.writeNode(node.id);
1255
+ }
1256
+ if (node.body) {
1257
+ if (node.body.type === "TSModuleBlock") context.write(" ");
1258
+ context.writeNode(node.body);
1259
+ }
1260
+ }
1261
+ function printTSModuleBlock(node, context) {
1262
+ context.write("{\n");
1263
+ context.writeNodeListWithSourceGaps(node.body, "\n");
1264
+ context.write("\n}");
1265
+ }
1266
+ function printTSDeclareFunction(node, context) {
1267
+ context.write("declare ");
1268
+ if (node.async === true) context.write("async ");
1269
+ context.write("function");
1270
+ if (node.generator === true) context.write("*");
1271
+ if (node.id) {
1272
+ context.write(" ");
1273
+ context.writeNode(node.id);
1274
+ }
1275
+ if (node.typeParameters) context.writeNode(node.typeParameters);
1276
+ context.write("(");
1277
+ context.writeNodeList(node.params, ", ");
1278
+ context.write(")");
1279
+ writeReturnType(node, context);
1280
+ context.write(";");
1281
+ }
1282
+ function printTSParameterProperty(node, context) {
1283
+ if (node.accessibility) context.write(node.accessibility + " ");
1284
+ if (node.readonly === true) context.write("readonly ");
1285
+ context.writeNode(node.parameter);
1286
+ }
1287
+ function printTSExportAssignment(node, context) {
1288
+ context.write("export = ");
1289
+ context.writeNode(node.expression);
1290
+ context.write(";");
1291
+ }
1292
+ function printTSNamespaceExportDeclaration(node, context) {
1293
+ context.write("export as namespace ");
1294
+ context.writeNode(node.id);
1295
+ context.write(";");
1296
+ }
1297
+ function printTSInstantiationExpression(node, context) {
1298
+ context.writeNode(node.expression);
1299
+ context.writeNode(node.typeArguments);
1300
+ }
1301
+ function printTSParenthesizedType(node, context) {
1302
+ context.write("(");
1303
+ context.writeNode(node.typeAnnotation);
1304
+ context.write(")");
1305
+ }
1306
+ function printKeywordType(node, context) {
1307
+ const keyword = node.type.slice(2, -7).toLowerCase();
1308
+ context.write(keyword);
1309
+ }
1310
+ function writeOptionalTypeAnnotation(node, context) {
1311
+ if (node.optional === true) context.write("?");
1312
+ if (node.typeAnnotation) context.writeNode(node.typeAnnotation);
1313
+ }
1314
+ //#endregion
1315
+ //#region src/mappings.ts
1316
+ function getNodeRange(node) {
1317
+ if (Array.isArray(node.range) && node.range.length === 2) {
1318
+ const [start, end] = node.range;
1319
+ if (Number.isInteger(start) && Number.isInteger(end) && start <= end) return {
1320
+ start,
1321
+ end
1322
+ };
1323
+ }
1324
+ const { start, end } = node;
1325
+ if (typeof start === "number" && typeof end === "number" && Number.isInteger(start) && Number.isInteger(end) && start <= end) return {
1326
+ start,
1327
+ end
1328
+ };
1329
+ }
1330
+ function pushMapping(mappings, mapping, combineMappingData) {
1331
+ const previous = mappings.at(-1);
1332
+ if (previous && canMerge(previous, mapping)) {
1333
+ previous.sourceEnd = mapping.sourceEnd;
1334
+ previous.generatedEnd = mapping.generatedEnd;
1335
+ previous.data = combineMappingData(previous.data, mapping.data);
1336
+ return;
1337
+ }
1338
+ mappings.push(mapping);
1339
+ }
1340
+ function canMerge(left, right) {
1341
+ return left.sourceStart + left.sourceEnd === right.sourceStart && left.generatedStart + left.generatedEnd === right.generatedStart;
1342
+ }
1343
+ function toVolarMapping(mapping) {
1344
+ const sourceLength = mapping.sourceEnd - mapping.sourceStart;
1345
+ const generatedLength = mapping.generatedEnd - mapping.generatedStart;
1346
+ return {
1347
+ sourceOffsets: [mapping.sourceStart],
1348
+ generatedOffsets: [mapping.generatedStart],
1349
+ lengths: [sourceLength],
1350
+ generatedLengths: generatedLength !== sourceLength ? [generatedLength] : void 0,
1351
+ data: mapping.data
1352
+ };
1353
+ }
1354
+ //#endregion
1355
+ //#region src/printer.ts
1356
+ function writeComment(context, comment) {
1357
+ if (comment.type === "Line") context.write("//" + comment.value + "\n");
1358
+ else {
1359
+ context.write("/*" + comment.value + "*/");
1360
+ if (comment.value.includes("\n")) context.write("\n");
1361
+ }
1362
+ }
1363
+ function print(node, options) {
1364
+ const context = createPrinterContext(options);
1365
+ context.writeNode(node);
1366
+ return context.result();
1367
+ }
1368
+ function defaultIsUntouched(node) {
1369
+ return getNodeRange(node) || false;
1370
+ }
1371
+ function defaultGetMappingData(node) {
1372
+ return {};
1373
+ }
1374
+ function defaultCombineMappingData(left, right) {
1375
+ return right;
1376
+ }
1377
+ function createPrinterContext(options) {
1378
+ const chunks = [];
1379
+ const mappings = [];
1380
+ let generatedOffset = 0;
1381
+ const isUntouched = options.isUntouched ?? defaultIsUntouched;
1382
+ const getMappingData = options.getMappingData ?? defaultGetMappingData;
1383
+ const combineMappingData = options.combineMappingData ?? (options.getMappingData ? (left) => left : defaultCombineMappingData);
1384
+ const printers = {
1385
+ ...defaultPrinters,
1386
+ ...options.printers
1387
+ };
1388
+ const getLeadingComments = options.getLeadingComments ?? (() => void 0);
1389
+ const getTrailingComments = options.getTrailingComments ?? (() => void 0);
1390
+ const appendMapping = (sourceRange, generatedStart, generatedEnd, data) => {
1391
+ pushMapping(mappings, {
1392
+ sourceStart: sourceRange.start,
1393
+ sourceEnd: sourceRange.end,
1394
+ generatedStart,
1395
+ generatedEnd,
1396
+ data
1397
+ }, combineMappingData);
1398
+ };
1399
+ const context = {
1400
+ source: options.source,
1401
+ get generatedOffset() {
1402
+ return generatedOffset;
1403
+ },
1404
+ write(text) {
1405
+ if (text.length === 0) return;
1406
+ chunks.push(text);
1407
+ generatedOffset += text.length;
1408
+ },
1409
+ writeNode(node) {
1410
+ if (!node) return;
1411
+ const range = getNodeRange(node);
1412
+ const untouchedRet = isUntouched(node);
1413
+ if (untouchedRet) {
1414
+ const sourceRange = untouchedRet === true ? range : untouchedRet;
1415
+ if (!sourceRange) throw new Error(`Node of type ${node.type} is marked as untouched but does not have valid source offsets`);
1416
+ context.writeSource(sourceRange.start, sourceRange.end, getMappingData(node));
1417
+ return;
1418
+ }
1419
+ const printer = printers[node.type];
1420
+ if (!printer) throw new Error(`No printer registered for node type ${node.type}`);
1421
+ const leadingComments = getLeadingComments(node);
1422
+ if (leadingComments) for (const c of leadingComments) writeComment(context, c);
1423
+ const generatedStart = generatedOffset;
1424
+ printer(node, context);
1425
+ const generatedEnd = generatedOffset;
1426
+ const trailingComments = getTrailingComments(node);
1427
+ if (trailingComments) for (const c of trailingComments) writeComment(context, c);
1428
+ const lastMappingGeneratedEnd = mappings.at(-1)?.generatedEnd ?? 0;
1429
+ if (range && lastMappingGeneratedEnd <= generatedStart) appendMapping(range, generatedStart, generatedEnd, getMappingData(node));
1430
+ },
1431
+ writeNodeList(nodes, separator) {
1432
+ let needsSeparator = false;
1433
+ for (const node of nodes) {
1434
+ if (!node) {
1435
+ if (needsSeparator) context.write(separator);
1436
+ continue;
1437
+ }
1438
+ if (needsSeparator) context.write(separator);
1439
+ context.writeNode(node);
1440
+ needsSeparator = true;
1441
+ }
1442
+ },
1443
+ writeNodeListWithSourceGaps(nodes, fallbackSeparator) {
1444
+ let lastRangeEnd;
1445
+ let wroteNode = false;
1446
+ for (const node of nodes) {
1447
+ if (!node) continue;
1448
+ const range = getNodeRange(node);
1449
+ if (wroteNode) if (lastRangeEnd !== void 0 && range && range.start >= lastRangeEnd) context.writeSource(lastRangeEnd, range.start, getMappingData(null));
1450
+ else context.write(fallbackSeparator);
1451
+ context.writeNode(node);
1452
+ wroteNode = true;
1453
+ if (range) lastRangeEnd = range.end;
1454
+ else lastRangeEnd = void 0;
1455
+ }
1456
+ },
1457
+ writePreservedNode(node) {
1458
+ const range = getNodeRange(node);
1459
+ if (!range) throw new Error(`Cannot preserve node ${node.type} without source offsets`);
1460
+ context.writeSource(range.start, range.end, getMappingData(node));
1461
+ },
1462
+ getLeadingComments,
1463
+ getTrailingComments,
1464
+ writeSource(start, end, data) {
1465
+ if (end <= start) return;
1466
+ const generatedStart = generatedOffset;
1467
+ context.write(options.source.slice(start, end));
1468
+ appendMapping({
1469
+ start,
1470
+ end
1471
+ }, generatedStart, generatedOffset, data);
1472
+ },
1473
+ result() {
1474
+ return {
1475
+ code: chunks.join(""),
1476
+ mappings: mappings.map(toVolarMapping)
1477
+ };
1478
+ }
1479
+ };
1480
+ return context;
1481
+ }
1482
+ //#endregion
1483
+ export { defaultCombineMappingData, defaultGetMappingData, defaultIsUntouched, print };