@typespec/asset-emitter 0.66.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.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/dist/src/asset-emitter.d.ts +5 -0
  4. package/dist/src/asset-emitter.d.ts.map +1 -0
  5. package/dist/src/asset-emitter.js +738 -0
  6. package/dist/src/asset-emitter.js.map +1 -0
  7. package/dist/src/builders/array-builder.d.ts +7 -0
  8. package/dist/src/builders/array-builder.d.ts.map +1 -0
  9. package/dist/src/builders/array-builder.js +35 -0
  10. package/dist/src/builders/array-builder.js.map +1 -0
  11. package/dist/src/builders/object-builder.d.ts +12 -0
  12. package/dist/src/builders/object-builder.d.ts.map +1 -0
  13. package/dist/src/builders/object-builder.js +52 -0
  14. package/dist/src/builders/object-builder.js.map +1 -0
  15. package/dist/src/builders/string-builder.d.ts +13 -0
  16. package/dist/src/builders/string-builder.d.ts.map +1 -0
  17. package/dist/src/builders/string-builder.js +90 -0
  18. package/dist/src/builders/string-builder.js.map +1 -0
  19. package/dist/src/custom-key-map.d.ts +29 -0
  20. package/dist/src/custom-key-map.d.ts.map +1 -0
  21. package/dist/src/custom-key-map.js +49 -0
  22. package/dist/src/custom-key-map.js.map +1 -0
  23. package/dist/src/index.d.ts +9 -0
  24. package/dist/src/index.d.ts.map +1 -0
  25. package/dist/src/index.js +9 -0
  26. package/dist/src/index.js.map +1 -0
  27. package/dist/src/placeholder.d.ts +12 -0
  28. package/dist/src/placeholder.d.ts.map +1 -0
  29. package/dist/src/placeholder.js +18 -0
  30. package/dist/src/placeholder.js.map +1 -0
  31. package/dist/src/ref-scope.d.ts +14 -0
  32. package/dist/src/ref-scope.d.ts.map +1 -0
  33. package/dist/src/ref-scope.js +30 -0
  34. package/dist/src/ref-scope.js.map +1 -0
  35. package/dist/src/reference-cycle.d.ts +23 -0
  36. package/dist/src/reference-cycle.d.ts.map +1 -0
  37. package/dist/src/reference-cycle.js +32 -0
  38. package/dist/src/reference-cycle.js.map +1 -0
  39. package/dist/src/type-emitter.d.ts +349 -0
  40. package/dist/src/type-emitter.d.ts.map +1 -0
  41. package/dist/src/type-emitter.js +720 -0
  42. package/dist/src/type-emitter.js.map +1 -0
  43. package/dist/src/types.d.ts +139 -0
  44. package/dist/src/types.d.ts.map +1 -0
  45. package/dist/src/types.js +42 -0
  46. package/dist/src/types.js.map +1 -0
  47. package/package.json +57 -0
@@ -0,0 +1,720 @@
1
+ import { compilerAssert, emitFile, isTemplateDeclaration, } from "@typespec/compiler";
2
+ import { StringBuilder, code } from "./builders/string-builder.js";
3
+ import { resolveDeclarationReferenceScope } from "./ref-scope.js";
4
+ import { ReferenceCycle } from "./reference-cycle.js";
5
+ /**
6
+ * Implement emitter logic by extending this class and passing it to
7
+ * `emitContext.createAssetEmitter`. This class should not be constructed
8
+ * directly.
9
+ *
10
+ * TypeEmitters serve two primary purposes:
11
+ *
12
+ * 1. Handle emitting TypeSpec types into other languages
13
+ * 2. Set emitter context
14
+ *
15
+ * The generic type parameter `T` is the type you expect to produce for each TypeSpec type.
16
+ * In the case of generating source code for a programming language, this is probably `string`
17
+ * (in which case, consider using the `CodeTypeEmitter`) but might also be an AST node. If you
18
+ * are emitting JSON or similar, `T` would likely be `object`.
19
+ *
20
+ * ## Emitting types
21
+ *
22
+ * Emitting TypeSpec types into other languages is accomplished by implementing
23
+ * the AssetEmitter method that corresponds with the TypeSpec type you are
24
+ * emitting. For example, to emit a TypeSpec model declaration, implement the
25
+ * `modelDeclaration` method.
26
+ *
27
+ * TypeSpec types that have both declaration and literal forms like models or
28
+ * unions will have separate methods. For example, models have both
29
+ * `modelDeclaration` and `modelLiteral` methods that can be implemented
30
+ * separately.
31
+ *
32
+ * Also, types which can be instantiated like models or operations have a
33
+ * separate method for the instantiated type. For example, models have a
34
+ * `modelInstantiation` method that gets called with such types. Generally these
35
+ * will be treated either as if they were declarations or literals depending on
36
+ * preference, but may also be treated specially.
37
+ *
38
+ * ## Emitter results
39
+ * There are three kinds of results your methods might return - declarations,
40
+ * raw code, or nothing.
41
+ *
42
+ * ### Declarations
43
+ *
44
+ * Create declarations by calling `this.emitter.result.declaration` passing it a
45
+ * name and the emit output for the declaration. Note that you must have scope
46
+ * in your context or you will get an error. If you want all declarations to be
47
+ * emitted to the same source file, you can create a single scope in
48
+ * `programContext` via something like:
49
+ *
50
+ * ```typescript
51
+ * programContext(program: Program): Context {
52
+ * const sourceFile = this.emitter.createSourceFile("test.txt");
53
+ * return {
54
+ * scope: sourceFile.globalScope,
55
+ * };
56
+ * }
57
+ * ```
58
+ *
59
+ * ### Raw Code
60
+ *
61
+ * Create raw code, or emitter output that doesn't contribute to a declaration,
62
+ * by calling `this.emitter.result.rawCode` passing it a value. Returning just a
63
+ * value is considered raw code and so you often don't need to call this
64
+ * directly.
65
+ *
66
+ * ### No Emit
67
+ *
68
+ * When a type doesn't contribute anything to the emitted output, return
69
+ * `this.emitter.result.none()`.
70
+ *
71
+ * ## Context
72
+ *
73
+ * The TypeEmitter will often want to keep track of what context a type is found
74
+ * in. There are two kinds of context - lexical context, and reference context.
75
+ *
76
+ * * Lexical context is context that applies to the type and every type
77
+ * contained inside of it. For example, lexical context for a model will apply
78
+ * to the model, its properties, and any nested model literals.
79
+ * * Reference context is context that applies to types contained inside of the
80
+ * type and referenced anywhere inside of it. For example, reference context
81
+ * set on a model will apply to the model, its properties, any nested model
82
+ * literals, and any type referenced inside anywhere inside the model and any
83
+ * of the referenced types' references.
84
+ *
85
+ * In both cases, context is an object. It's strongly recommended that the context
86
+ * object either contain only primitive types, or else only reference immutable
87
+ * objects.
88
+ *
89
+ * Set lexical by implementing the `*Context` methods of the TypeEmitter and
90
+ * returning the context, for example `modelDeclarationContext` sets the context
91
+ * for model declarations and the types contained inside of it.
92
+ *
93
+ * Set reference context by implementing the `*ReferenceContext` methods of the
94
+ * TypeEmitter and returning the context. Note that not all types have reference
95
+ * context methods, because not all types can actually reference anything.
96
+ *
97
+ * When a context method returns some context, it is merged with the current
98
+ * context. It is not possible to remove previous context, but it can be
99
+ * overridden with `undefined`.
100
+ *
101
+ * When emitting types with context, the same type might be emitted multiple
102
+ * times if we come across that type with different contexts. For example, if we
103
+ * have a TypeSpec program like
104
+ *
105
+ * ```typespec
106
+ * model Pet { }
107
+ * model Person {
108
+ * pet: Pet;
109
+ * }
110
+ * ```
111
+ *
112
+ * And we set reference context for the Person model, Pet will be emitted twice,
113
+ * once without context and once with the reference context.
114
+ */
115
+ export class TypeEmitter {
116
+ emitter;
117
+ /**
118
+ * @private
119
+ *
120
+ * Constructs a TypeEmitter. Do not use this constructor directly, instead
121
+ * call `createAssetEmitter` on the emitter context object.
122
+ * @param emitter The asset emitter
123
+ */
124
+ constructor(emitter) {
125
+ this.emitter = emitter;
126
+ }
127
+ /**
128
+ * Context shared by the entire program. In cases where you are emitting to a
129
+ * single file, use this method to establish your main source file and set the
130
+ * `scope` property to that source file's `globalScope`.
131
+ * @param program
132
+ * @returns Context
133
+ */
134
+ programContext(program) {
135
+ return {};
136
+ }
137
+ /**
138
+ * Emit a namespace
139
+ *
140
+ * @param namespace
141
+ * @returns Emitter output
142
+ */
143
+ namespace(namespace) {
144
+ for (const ns of namespace.namespaces.values()) {
145
+ this.emitter.emitType(ns);
146
+ }
147
+ for (const model of namespace.models.values()) {
148
+ if (!isTemplateDeclaration(model)) {
149
+ this.emitter.emitType(model);
150
+ }
151
+ }
152
+ for (const operation of namespace.operations.values()) {
153
+ if (!isTemplateDeclaration(operation)) {
154
+ this.emitter.emitType(operation);
155
+ }
156
+ }
157
+ for (const enumeration of namespace.enums.values()) {
158
+ this.emitter.emitType(enumeration);
159
+ }
160
+ for (const union of namespace.unions.values()) {
161
+ if (!isTemplateDeclaration(union)) {
162
+ this.emitter.emitType(union);
163
+ }
164
+ }
165
+ for (const iface of namespace.interfaces.values()) {
166
+ if (!isTemplateDeclaration(iface)) {
167
+ this.emitter.emitType(iface);
168
+ }
169
+ }
170
+ for (const scalar of namespace.scalars.values()) {
171
+ this.emitter.emitType(scalar);
172
+ }
173
+ return this.emitter.result.none();
174
+ }
175
+ /**
176
+ * Set lexical context for a namespace
177
+ *
178
+ * @param namespace
179
+ */
180
+ namespaceContext(namespace) {
181
+ return {};
182
+ }
183
+ /**
184
+ * Set reference context for a namespace.
185
+ *
186
+ * @param namespace
187
+ */
188
+ namespaceReferenceContext(namespace) {
189
+ return {};
190
+ }
191
+ /**
192
+ * Emit a model literal (e.g. as created by `{}` syntax in TypeSpec).
193
+ *
194
+ * @param model
195
+ */
196
+ modelLiteral(model) {
197
+ if (model.baseModel) {
198
+ this.emitter.emitType(model.baseModel);
199
+ }
200
+ this.emitter.emitModelProperties(model);
201
+ return this.emitter.result.none();
202
+ }
203
+ /**
204
+ * Set lexical context for a model literal.
205
+ * @param model
206
+ */
207
+ modelLiteralContext(model) {
208
+ return {};
209
+ }
210
+ /**
211
+ * Set reference context for a model literal.
212
+ * @param model
213
+ */
214
+ modelLiteralReferenceContext(model) {
215
+ return {};
216
+ }
217
+ /**
218
+ * Emit a model declaration (e.g. as created by `model Foo { }` syntax in
219
+ * TypeSpec).
220
+ *
221
+ * @param model
222
+ */
223
+ modelDeclaration(model, name) {
224
+ if (model.baseModel) {
225
+ this.emitter.emitType(model.baseModel);
226
+ }
227
+ this.emitter.emitModelProperties(model);
228
+ return this.emitter.result.none();
229
+ }
230
+ /**
231
+ * Set lexical context for a model declaration.
232
+ *
233
+ * @param model
234
+ * @param name the model's declaration name as retrieved from the
235
+ * `declarationName` method.
236
+ */
237
+ modelDeclarationContext(model, name) {
238
+ return {};
239
+ }
240
+ /**
241
+ * Set reference context for a model declaration.
242
+ * @param model
243
+ */
244
+ modelDeclarationReferenceContext(model, name) {
245
+ return {};
246
+ }
247
+ /**
248
+ * Emit a model instantiation (e.g. as created by `Box<string>` syntax in
249
+ * TypeSpec). In some cases, `name` is undefined because a good name could
250
+ * not be found for the instantiation. This often occurs with for instantiations
251
+ * involving type expressions like `Box<string | int32>`.
252
+ *
253
+ * @param model
254
+ * @param name The name of the instantiation as retrieved from the
255
+ * `declarationName` method.
256
+ */
257
+ modelInstantiation(model, name) {
258
+ if (model.baseModel) {
259
+ this.emitter.emitType(model.baseModel);
260
+ }
261
+ this.emitter.emitModelProperties(model);
262
+ return this.emitter.result.none();
263
+ }
264
+ /**
265
+ * Set lexical context for a model instantiation.
266
+ * @param model
267
+ */
268
+ modelInstantiationContext(model, name) {
269
+ return {};
270
+ }
271
+ /**
272
+ * Set reference context for a model declaration.
273
+ * @param model
274
+ */
275
+ modelInstantiationReferenceContext(model, name) {
276
+ return {};
277
+ }
278
+ /**
279
+ * Emit a model's properties. Unless overridden, this method will emit each of
280
+ * the model's properties and return a no emit result.
281
+ *
282
+ * @param model
283
+ */
284
+ modelProperties(model) {
285
+ for (const prop of model.properties.values()) {
286
+ this.emitter.emitModelProperty(prop);
287
+ }
288
+ return this.emitter.result.none();
289
+ }
290
+ modelPropertiesContext(model) {
291
+ return {};
292
+ }
293
+ modelPropertiesReferenceContext(model) {
294
+ return {};
295
+ }
296
+ /**
297
+ * Emit a property of a model.
298
+ *
299
+ * @param property
300
+ */
301
+ modelPropertyLiteral(property) {
302
+ this.emitter.emitTypeReference(property.type);
303
+ return this.emitter.result.none();
304
+ }
305
+ /**
306
+ * Set lexical context for a property of a model.
307
+ *
308
+ * @param property
309
+ */
310
+ modelPropertyLiteralContext(property) {
311
+ return {};
312
+ }
313
+ /**
314
+ * Set reference context for a property of a model.
315
+ *
316
+ * @param property
317
+ */
318
+ modelPropertyLiteralReferenceContext(property) {
319
+ return {};
320
+ }
321
+ /**
322
+ * Emit a model property reference (e.g. as created by the `SomeModel.prop`
323
+ * syntax in TypeSpec). By default, this will emit the type of the referenced
324
+ * property and return that result. In other words, the emit will look as if
325
+ * `SomeModel.prop` were replaced with the type of `prop`.
326
+ *
327
+ * @param property
328
+ */
329
+ modelPropertyReference(property) {
330
+ return this.emitter.emitTypeReference(property.type);
331
+ }
332
+ /**
333
+ * Emit an enum member reference (e.g. as created by the `SomeEnum.member` syntax
334
+ * in TypeSpec). By default, this will emit nothing.
335
+ *
336
+ * @param property the enum member
337
+ */
338
+ enumMemberReference(member) {
339
+ return this.emitter.result.none();
340
+ }
341
+ arrayDeclaration(array, name, elementType) {
342
+ this.emitter.emitType(array.indexer.value);
343
+ return this.emitter.result.none();
344
+ }
345
+ arrayDeclarationContext(array, name, elementType) {
346
+ return {};
347
+ }
348
+ arrayDeclarationReferenceContext(array, name, elementType) {
349
+ return {};
350
+ }
351
+ arrayLiteral(array, elementType) {
352
+ return this.emitter.result.none();
353
+ }
354
+ arrayLiteralContext(array, elementType) {
355
+ return {};
356
+ }
357
+ arrayLiteralReferenceContext(array, elementType) {
358
+ return {};
359
+ }
360
+ scalarDeclaration(scalar, name) {
361
+ if (scalar.baseScalar) {
362
+ this.emitter.emitType(scalar.baseScalar);
363
+ }
364
+ return this.emitter.result.none();
365
+ }
366
+ scalarDeclarationContext(scalar, name) {
367
+ return {};
368
+ }
369
+ scalarDeclarationReferenceContext(scalar, name) {
370
+ return {};
371
+ }
372
+ scalarInstantiation(scalar, name) {
373
+ return this.emitter.result.none();
374
+ }
375
+ scalarInstantiationContext(scalar, name) {
376
+ return {};
377
+ }
378
+ intrinsic(intrinsic, name) {
379
+ return this.emitter.result.none();
380
+ }
381
+ intrinsicContext(intrinsic, name) {
382
+ return {};
383
+ }
384
+ booleanLiteralContext(boolean) {
385
+ return {};
386
+ }
387
+ booleanLiteral(boolean) {
388
+ return this.emitter.result.none();
389
+ }
390
+ stringTemplateContext(string) {
391
+ return {};
392
+ }
393
+ stringTemplate(stringTemplate) {
394
+ return this.emitter.result.none();
395
+ }
396
+ stringLiteralContext(string) {
397
+ return {};
398
+ }
399
+ stringLiteral(string) {
400
+ return this.emitter.result.none();
401
+ }
402
+ numericLiteralContext(number) {
403
+ return {};
404
+ }
405
+ numericLiteral(number) {
406
+ return this.emitter.result.none();
407
+ }
408
+ operationDeclaration(operation, name) {
409
+ this.emitter.emitOperationParameters(operation);
410
+ this.emitter.emitOperationReturnType(operation);
411
+ return this.emitter.result.none();
412
+ }
413
+ operationDeclarationContext(operation, name) {
414
+ return {};
415
+ }
416
+ operationDeclarationReferenceContext(operation, name) {
417
+ return {};
418
+ }
419
+ interfaceDeclarationOperationsContext(iface) {
420
+ return {};
421
+ }
422
+ interfaceDeclarationOperationsReferenceContext(iface) {
423
+ return {};
424
+ }
425
+ interfaceOperationDeclarationContext(operation, name) {
426
+ return {};
427
+ }
428
+ interfaceOperationDeclarationReferenceContext(operation, name) {
429
+ return {};
430
+ }
431
+ operationParameters(operation, parameters) {
432
+ return this.emitter.result.none();
433
+ }
434
+ operationParametersContext(operation, parameters) {
435
+ return {};
436
+ }
437
+ operationParametersReferenceContext(operation, parameters) {
438
+ return {};
439
+ }
440
+ operationReturnType(operation, returnType) {
441
+ return this.emitter.result.none();
442
+ }
443
+ operationReturnTypeContext(operation, returnType) {
444
+ return {};
445
+ }
446
+ operationReturnTypeReferenceContext(operation, returnType) {
447
+ return {};
448
+ }
449
+ interfaceDeclaration(iface, name) {
450
+ this.emitter.emitInterfaceOperations(iface);
451
+ return this.emitter.result.none();
452
+ }
453
+ interfaceDeclarationContext(iface, name) {
454
+ return {};
455
+ }
456
+ interfaceDeclarationReferenceContext(iface, name) {
457
+ return {};
458
+ }
459
+ interfaceDeclarationOperations(iface) {
460
+ for (const op of iface.operations.values()) {
461
+ this.emitter.emitInterfaceOperation(op);
462
+ }
463
+ return this.emitter.result.none();
464
+ }
465
+ interfaceOperationDeclaration(operation, name) {
466
+ this.emitter.emitOperationParameters(operation);
467
+ this.emitter.emitOperationReturnType(operation);
468
+ return this.emitter.result.none();
469
+ }
470
+ enumDeclaration(en, name) {
471
+ this.emitter.emitEnumMembers(en);
472
+ return this.emitter.result.none();
473
+ }
474
+ enumDeclarationContext(en, name) {
475
+ return {};
476
+ }
477
+ enumDeclarationReferenceContext(en, name) {
478
+ return {};
479
+ }
480
+ enumMembers(en) {
481
+ for (const member of en.members.values()) {
482
+ this.emitter.emitType(member);
483
+ }
484
+ return this.emitter.result.none();
485
+ }
486
+ enumMembersContext(en) {
487
+ return {};
488
+ }
489
+ enumMember(member) {
490
+ return this.emitter.result.none();
491
+ }
492
+ enumMemberContext(member) {
493
+ return {};
494
+ }
495
+ unionDeclaration(union, name) {
496
+ this.emitter.emitUnionVariants(union);
497
+ return this.emitter.result.none();
498
+ }
499
+ unionDeclarationContext(union) {
500
+ return {};
501
+ }
502
+ unionDeclarationReferenceContext(union) {
503
+ return {};
504
+ }
505
+ unionInstantiation(union, name) {
506
+ this.emitter.emitUnionVariants(union);
507
+ return this.emitter.result.none();
508
+ }
509
+ unionInstantiationContext(union, name) {
510
+ return {};
511
+ }
512
+ unionInstantiationReferenceContext(union, name) {
513
+ return {};
514
+ }
515
+ unionLiteral(union) {
516
+ this.emitter.emitUnionVariants(union);
517
+ return this.emitter.result.none();
518
+ }
519
+ unionLiteralContext(union) {
520
+ return {};
521
+ }
522
+ unionLiteralReferenceContext(union) {
523
+ return {};
524
+ }
525
+ unionVariants(union) {
526
+ for (const variant of union.variants.values()) {
527
+ this.emitter.emitType(variant);
528
+ }
529
+ return this.emitter.result.none();
530
+ }
531
+ unionVariantsContext() {
532
+ return {};
533
+ }
534
+ unionVariantsReferenceContext() {
535
+ return {};
536
+ }
537
+ unionVariant(variant) {
538
+ this.emitter.emitTypeReference(variant.type);
539
+ return this.emitter.result.none();
540
+ }
541
+ unionVariantContext(union) {
542
+ return {};
543
+ }
544
+ unionVariantReferenceContext(union) {
545
+ return {};
546
+ }
547
+ tupleLiteral(tuple) {
548
+ this.emitter.emitTupleLiteralValues(tuple);
549
+ return this.emitter.result.none();
550
+ }
551
+ tupleLiteralContext(tuple) {
552
+ return {};
553
+ }
554
+ tupleLiteralValues(tuple) {
555
+ for (const value of tuple.values.values()) {
556
+ this.emitter.emitType(value);
557
+ }
558
+ return this.emitter.result.none();
559
+ }
560
+ tupleLiteralValuesContext(tuple) {
561
+ return {};
562
+ }
563
+ tupleLiteralValuesReferenceContext(tuple) {
564
+ return {};
565
+ }
566
+ tupleLiteralReferenceContext(tuple) {
567
+ return {};
568
+ }
569
+ sourceFile(sourceFile) {
570
+ const emittedSourceFile = {
571
+ path: sourceFile.path,
572
+ contents: "",
573
+ };
574
+ for (const decl of sourceFile.globalScope.declarations) {
575
+ emittedSourceFile.contents += decl.value + "\n";
576
+ }
577
+ return emittedSourceFile;
578
+ }
579
+ async writeOutput(sourceFiles) {
580
+ for (const file of sourceFiles) {
581
+ const outputFile = await this.emitter.emitSourceFile(file);
582
+ await emitFile(this.emitter.getProgram(), {
583
+ path: outputFile.path,
584
+ content: outputFile.contents,
585
+ });
586
+ }
587
+ }
588
+ reference(targetDeclaration, pathUp, pathDown, commonScope) {
589
+ return this.emitter.result.none();
590
+ }
591
+ /**
592
+ * Handle circular references. When this method is called it means we are resolving a circular reference.
593
+ * By default if the target is a declaration it will call to {@link reference} otherwise it means we have an inline reference
594
+ * @param target Reference target.
595
+ * @param scope Current scope.
596
+ * @returns Resolved reference entity.
597
+ */
598
+ circularReference(target, scope, cycle) {
599
+ if (!cycle.containsDeclaration) {
600
+ throw new Error(`Circular references to non-declarations are not supported by this emitter. Cycle:\n${cycle}`);
601
+ }
602
+ if (target.kind !== "declaration") {
603
+ return target;
604
+ }
605
+ compilerAssert(scope, "Emit context must have a scope set in order to create references to declarations.");
606
+ const { pathUp, pathDown, commonScope } = resolveDeclarationReferenceScope(target, scope);
607
+ return this.reference(target, pathUp, pathDown, commonScope);
608
+ }
609
+ declarationName(declarationType) {
610
+ compilerAssert(declarationType.name !== undefined, "Can't emit a declaration that doesn't have a name.");
611
+ if (declarationType.kind === "Enum" || declarationType.kind === "Intrinsic") {
612
+ return declarationType.name;
613
+ }
614
+ // for operations inside interfaces, we don't want to do the fancy thing because it will make
615
+ // operations inside instantiated interfaces get weird names
616
+ if (declarationType.kind === "Operation" && declarationType.interface) {
617
+ return declarationType.name;
618
+ }
619
+ if (!declarationType.templateMapper) {
620
+ return declarationType.name;
621
+ }
622
+ let unspeakable = false;
623
+ const parameterNames = declarationType.templateMapper.args.map((t) => {
624
+ if (t.entityKind === "Indeterminate") {
625
+ t = t.type;
626
+ }
627
+ if (!("kind" in t)) {
628
+ return undefined;
629
+ }
630
+ switch (t.kind) {
631
+ case "Model":
632
+ case "Scalar":
633
+ case "Interface":
634
+ case "Operation":
635
+ case "Enum":
636
+ case "Union":
637
+ case "Intrinsic":
638
+ if (!t.name) {
639
+ unspeakable = true;
640
+ return undefined;
641
+ }
642
+ const declName = this.emitter.emitDeclarationName(t);
643
+ if (declName === undefined) {
644
+ unspeakable = true;
645
+ return undefined;
646
+ }
647
+ return declName[0].toUpperCase() + declName.slice(1);
648
+ default:
649
+ unspeakable = true;
650
+ return undefined;
651
+ }
652
+ });
653
+ if (unspeakable) {
654
+ return undefined;
655
+ }
656
+ return declarationType.name + parameterNames.join("");
657
+ }
658
+ }
659
+ /**
660
+ * A subclass of `TypeEmitter<string>` that makes working with strings a bit easier.
661
+ * In particular, when emitting members of a type (`modelProperties`, `enumMembers`, etc.),
662
+ * instead of returning no result, it returns the value of each of the members concatenated
663
+ * by commas. It will also construct references by concatenating namespace elements together
664
+ * with `.` which should work nicely in many object oriented languages.
665
+ */
666
+ export class CodeTypeEmitter extends TypeEmitter {
667
+ modelProperties(model) {
668
+ const builder = new StringBuilder();
669
+ let i = 0;
670
+ for (const prop of model.properties.values()) {
671
+ i++;
672
+ const propVal = this.emitter.emitModelProperty(prop);
673
+ builder.push(code `${propVal}${i < model.properties.size ? "," : ""}`);
674
+ }
675
+ return this.emitter.result.rawCode(builder.reduce());
676
+ }
677
+ interfaceDeclarationOperations(iface) {
678
+ const builder = new StringBuilder();
679
+ let i = 0;
680
+ for (const op of iface.operations.values()) {
681
+ i++;
682
+ builder.push(code `${this.emitter.emitInterfaceOperation(op)}${i < iface.operations.size ? "," : ""}`);
683
+ }
684
+ return builder.reduce();
685
+ }
686
+ enumMembers(en) {
687
+ const builder = new StringBuilder();
688
+ let i = 0;
689
+ for (const enumMember of en.members.values()) {
690
+ i++;
691
+ builder.push(code `${this.emitter.emitType(enumMember)}${i < en.members.size ? "," : ""}`);
692
+ }
693
+ return builder.reduce();
694
+ }
695
+ unionVariants(union) {
696
+ const builder = new StringBuilder();
697
+ let i = 0;
698
+ for (const v of union.variants.values()) {
699
+ i++;
700
+ builder.push(code `${this.emitter.emitType(v)}${i < union.variants.size ? "," : ""}`);
701
+ }
702
+ return builder.reduce();
703
+ }
704
+ tupleLiteralValues(tuple) {
705
+ const builder = new StringBuilder();
706
+ let i = 0;
707
+ for (const v of tuple.values) {
708
+ i++;
709
+ builder.push(code `${this.emitter.emitTypeReference(v)}${i < tuple.values.length ? "," : ""}`);
710
+ }
711
+ return builder.reduce();
712
+ }
713
+ reference(targetDeclaration, pathUp, pathDown, commonScope) {
714
+ const basePath = pathDown.map((s) => s.name).join(".");
715
+ return basePath
716
+ ? this.emitter.result.rawCode(basePath + "." + targetDeclaration.name)
717
+ : this.emitter.result.rawCode(targetDeclaration.name);
718
+ }
719
+ }
720
+ //# sourceMappingURL=type-emitter.js.map