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