@typespec/emitter-framework 0.10.0-dev.1 → 0.10.0-dev.2

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 (30) hide show
  1. package/dist/src/typescript/components/index.d.ts +2 -2
  2. package/dist/src/typescript/components/index.js +2 -2
  3. package/dist/src/typescript/components/type-declaration.js +1 -1
  4. package/dist/src/typescript/components/type-expression.js +1 -1
  5. package/dist/src/typescript/components/{union-declaration.d.ts → union/declaration.d.ts} +1 -1
  6. package/dist/src/typescript/components/union/declaration.d.ts.map +1 -0
  7. package/dist/src/typescript/components/{union-declaration.js → union/declaration.js} +4 -4
  8. package/dist/src/typescript/components/union/declaration.test.d.ts +2 -0
  9. package/dist/src/typescript/components/union/declaration.test.d.ts.map +1 -0
  10. package/dist/src/typescript/components/union/declaration.test.js +308 -0
  11. package/dist/src/typescript/components/{union-expression.d.ts → union/expression.d.ts} +1 -1
  12. package/dist/src/typescript/components/union/expression.d.ts.map +1 -0
  13. package/dist/src/typescript/components/{union-expression.js → union/expression.js} +3 -3
  14. package/dist/src/typescript/components/union/expression.test.d.ts +2 -0
  15. package/dist/src/typescript/components/union/expression.test.d.ts.map +1 -0
  16. package/dist/src/typescript/components/union/expression.test.js +51 -0
  17. package/package.json +1 -1
  18. package/src/typescript/components/index.ts +2 -2
  19. package/src/typescript/components/type-declaration.tsx +1 -1
  20. package/src/typescript/components/type-expression.tsx +1 -1
  21. package/src/typescript/components/union/declaration.test.tsx +264 -0
  22. package/src/typescript/components/{union-declaration.tsx → union/declaration.tsx} +4 -4
  23. package/src/typescript/components/union/expression.test.tsx +42 -0
  24. package/src/typescript/components/{union-expression.tsx → union/expression.tsx} +3 -3
  25. package/dist/src/typescript/components/union-declaration.d.ts.map +0 -1
  26. package/dist/src/typescript/components/union-expression.d.ts.map +0 -1
  27. package/dist/test/typescript/components/union-declaration.test.d.ts +0 -2
  28. package/dist/test/typescript/components/union-declaration.test.d.ts.map +0 -1
  29. package/dist/test/typescript/components/union-declaration.test.js +0 -465
  30. package/test/typescript/components/union-declaration.test.tsx +0 -411
@@ -1,465 +0,0 @@
1
- import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
- import { render } from "@alloy-js/core";
3
- import { d } from "@alloy-js/core/testing";
4
- import { SourceFile } from "@alloy-js/typescript";
5
- import { beforeEach, describe, it } from "vitest";
6
- import { Output } from "../../../src/core/components/output.js";
7
- import { UnionDeclaration } from "../../../src/typescript/components/union-declaration.js";
8
- import { UnionExpression } from "../../../src/typescript/components/union-expression.js";
9
- import { InterfaceDeclaration } from "../../../src/typescript/index.js";
10
- import { assertFileContents } from "../../utils.js";
11
- import { createEmitterFrameworkTestRunner } from "../test-host.js";
12
- describe("Typescript Union Declaration", () => {
13
- let runner;
14
- beforeEach(async () => {
15
- runner = await createEmitterFrameworkTestRunner();
16
- });
17
- describe("Union not bound to Typespec Types", () => {
18
- it("creates a union declaration", async () => {
19
- await runner.compile(``);
20
- const res = render(_$createComponent(Output, {
21
- get program() {
22
- return runner.program;
23
- },
24
- get children() {
25
- return _$createComponent(SourceFile, {
26
- path: "test.ts",
27
- get children() {
28
- return _$createComponent(UnionDeclaration, {
29
- name: "MyUnion",
30
- children: "\"red\" | \"blue\""
31
- });
32
- }
33
- });
34
- }
35
- }));
36
- assertFileContents(res, d`
37
- type MyUnion = "red" | "blue";
38
- `);
39
- });
40
- });
41
- describe("Union bound to Typespec Types", () => {
42
- describe("Bound to Union", () => {
43
- it("creates a union declaration", async () => {
44
- const {
45
- TestUnion
46
- } = await runner.compile(`
47
- namespace DemoService;
48
- @test union TestUnion {
49
- one: "one",
50
- two: "two"
51
- }
52
- `);
53
- const res = render(_$createComponent(Output, {
54
- get program() {
55
- return runner.program;
56
- },
57
- get children() {
58
- return _$createComponent(SourceFile, {
59
- path: "test.ts",
60
- get children() {
61
- return _$createComponent(UnionDeclaration, {
62
- type: TestUnion
63
- });
64
- }
65
- });
66
- }
67
- }));
68
- assertFileContents(res, d`
69
- type TestUnion = "one" | "two";
70
- `);
71
- });
72
- it("creates a union declaration with JSDoc", async () => {
73
- const {
74
- TestUnion
75
- } = await runner.compile(`
76
- namespace DemoService;
77
- /**
78
- * Test Union
79
- */
80
- @test union TestUnion {
81
- one: "one",
82
- two: "two"
83
- }
84
- `);
85
- const res = render(_$createComponent(Output, {
86
- get program() {
87
- return runner.program;
88
- },
89
- get children() {
90
- return _$createComponent(SourceFile, {
91
- path: "test.ts",
92
- get children() {
93
- return _$createComponent(UnionDeclaration, {
94
- type: TestUnion
95
- });
96
- }
97
- });
98
- }
99
- }));
100
- assertFileContents(res, d`
101
- /**
102
- * Test Union
103
- */
104
- type TestUnion = "one" | "two";
105
- `);
106
- });
107
- it("creates a union declaration with name override", async () => {
108
- const {
109
- TestUnion
110
- } = await runner.compile(`
111
- namespace DemoService;
112
- @test union TestUnion {
113
- one: "one",
114
- two: "two"
115
- }
116
- `);
117
- const res = render(_$createComponent(Output, {
118
- get program() {
119
- return runner.program;
120
- },
121
- get children() {
122
- return _$createComponent(SourceFile, {
123
- path: "test.ts",
124
- get children() {
125
- return _$createComponent(UnionDeclaration, {
126
- "export": true,
127
- type: TestUnion,
128
- name: "MyUnion"
129
- });
130
- }
131
- });
132
- }
133
- }));
134
- assertFileContents(res, d`
135
- export type MyUnion = "one" | "two";
136
- `);
137
- });
138
- it("creates a union declaration with extra children", async () => {
139
- const {
140
- TestUnion
141
- } = await runner.compile(`
142
- namespace DemoService;
143
- @test union TestUnion {
144
- one: "one",
145
- two: "two"
146
- }
147
- `);
148
- const res = render(_$createComponent(Output, {
149
- get program() {
150
- return runner.program;
151
- },
152
- get children() {
153
- return _$createComponent(SourceFile, {
154
- path: "test.ts",
155
- get children() {
156
- return _$createComponent(UnionDeclaration, {
157
- type: TestUnion,
158
- children: "\"three\""
159
- });
160
- }
161
- });
162
- }
163
- }));
164
- assertFileContents(res, d`
165
- type TestUnion = "one" | "two" | "three";
166
- `);
167
- });
168
- it("renders a union expression", async () => {
169
- const {
170
- TestUnion
171
- } = await runner.compile(`
172
- namespace DemoService;
173
- @test union TestUnion {
174
- one: "one",
175
- two: "two"
176
- }
177
- `);
178
- const res = render(_$createComponent(Output, {
179
- get program() {
180
- return runner.program;
181
- },
182
- get children() {
183
- return _$createComponent(SourceFile, {
184
- path: "test.ts",
185
- get children() {
186
- return ["let x: ", _$createComponent(UnionExpression, {
187
- type: TestUnion
188
- }), " = \"one\";"];
189
- }
190
- });
191
- }
192
- }));
193
- assertFileContents(res, d`
194
- let x: "one" | "two" = "one";
195
- `);
196
- });
197
- describe("Discriminated Union", () => {
198
- it("renders a discriminated union", async () => {
199
- const {
200
- TestUnion
201
- } = await runner.compile(`
202
- namespace DemoService;
203
- @discriminated
204
- @test union TestUnion {
205
- one: { oneItem: true },
206
- two: true
207
- }
208
- `);
209
- const res = render(_$createComponent(Output, {
210
- get program() {
211
- return runner.program;
212
- },
213
- get children() {
214
- return _$createComponent(SourceFile, {
215
- path: "test.ts",
216
- get children() {
217
- return _$createComponent(UnionDeclaration, {
218
- type: TestUnion
219
- });
220
- }
221
- });
222
- }
223
- }));
224
- assertFileContents(res, d`
225
- type TestUnion = {
226
- kind: "one";
227
- value: {
228
- oneItem: true;
229
- };
230
- } | {
231
- kind: "two";
232
- value: true;
233
- };
234
- `);
235
- });
236
- });
237
- it("renders a discriminated union with custom properties", async () => {
238
- const {
239
- TestUnion
240
- } = await runner.compile(`
241
- namespace DemoService;
242
- @discriminated(#{ discriminatorPropertyName: "dataKind", envelopePropertyName: "data" })
243
- @test union TestUnion {
244
- one: { oneItem: true },
245
- two: true
246
- }
247
- `);
248
- const res = render(_$createComponent(Output, {
249
- get program() {
250
- return runner.program;
251
- },
252
- get children() {
253
- return _$createComponent(SourceFile, {
254
- path: "test.ts",
255
- get children() {
256
- return _$createComponent(UnionDeclaration, {
257
- type: TestUnion
258
- });
259
- }
260
- });
261
- }
262
- }));
263
- assertFileContents(res, d`
264
- type TestUnion = {
265
- dataKind: "one";
266
- data: {
267
- oneItem: true;
268
- };
269
- } | {
270
- dataKind: "two";
271
- data: true;
272
- };
273
- `);
274
- });
275
- it("renders a discriminated union with named models", async () => {
276
- const {
277
- Pet,
278
- Cat,
279
- Dog
280
- } = await runner.compile(`
281
- namespace DemoService;
282
- @test model Cat {
283
- name: string;
284
- meow: boolean;
285
- }
286
-
287
- @test model Dog {
288
- name: string;
289
- bark: boolean;
290
- }
291
-
292
- @discriminated
293
- @test union Pet {
294
- cat: Cat,
295
- dog: Dog,
296
- }
297
- `);
298
- const res = render(_$createComponent(Output, {
299
- get program() {
300
- return runner.program;
301
- },
302
- get children() {
303
- return _$createComponent(SourceFile, {
304
- path: "test.ts",
305
- get children() {
306
- return [_$createComponent(InterfaceDeclaration, {
307
- type: Cat
308
- }), _$createIntrinsic("hbr", {}), _$createComponent(InterfaceDeclaration, {
309
- type: Dog
310
- }), _$createIntrinsic("hbr", {}), _$createComponent(UnionDeclaration, {
311
- type: Pet
312
- })];
313
- }
314
- });
315
- }
316
- }));
317
- assertFileContents(res, d`
318
- interface Cat {
319
- name: string;
320
- meow: boolean;
321
- }
322
- interface Dog {
323
- name: string;
324
- bark: boolean;
325
- }
326
- type Pet = {
327
- kind: "cat";
328
- value: Cat;
329
- } | {
330
- kind: "dog";
331
- value: Dog;
332
- };
333
- `);
334
- });
335
- describe("Discriminated Union with no envelope", () => {
336
- it("renders named discriminated union", async () => {
337
- const {
338
- Pet,
339
- Cat,
340
- Dog
341
- } = await runner.compile(`
342
- namespace DemoService;
343
-
344
- @test model Cat {
345
- name: string;
346
- meow: boolean;
347
- }
348
-
349
- @test model Dog {
350
- name: string;
351
- bark: boolean;
352
- }
353
-
354
- @discriminated(#{ envelope: "none", discriminatorPropertyName: "dataKind" })
355
- @test union Pet {
356
- cat: Cat,
357
- dog: Dog,
358
- }
359
- `);
360
- const res = render(_$createComponent(Output, {
361
- get program() {
362
- return runner.program;
363
- },
364
- get children() {
365
- return _$createComponent(SourceFile, {
366
- path: "test.ts",
367
- get children() {
368
- return [_$createComponent(InterfaceDeclaration, {
369
- type: Cat
370
- }), _$createIntrinsic("hbr", {}), _$createComponent(InterfaceDeclaration, {
371
- type: Dog
372
- }), _$createIntrinsic("hbr", {}), _$createComponent(UnionDeclaration, {
373
- type: Pet
374
- })];
375
- }
376
- });
377
- }
378
- }));
379
- assertFileContents(res, d`
380
- interface Cat {
381
- name: string;
382
- meow: boolean;
383
- }
384
- interface Dog {
385
- name: string;
386
- bark: boolean;
387
- }
388
- type Pet = {
389
- dataKind: "cat"
390
- } & Cat | {
391
- dataKind: "dog"
392
- } & Dog;
393
- `);
394
- });
395
- it("renders anonymous discriminated union", async () => {
396
- const {
397
- TestUnion
398
- } = await runner.compile(`
399
- namespace DemoService;
400
- @discriminated(#{ envelope: "none", discriminatorPropertyName: "dataKind" })
401
- @test union TestUnion {
402
- one: { oneItem: true },
403
- two: { secondItem: false }
404
- }
405
- `);
406
- const res = render(_$createComponent(Output, {
407
- get program() {
408
- return runner.program;
409
- },
410
- get children() {
411
- return _$createComponent(SourceFile, {
412
- path: "test.ts",
413
- get children() {
414
- return _$createComponent(UnionDeclaration, {
415
- type: TestUnion
416
- });
417
- }
418
- });
419
- }
420
- }));
421
- assertFileContents(res, d`
422
- type TestUnion = {
423
- dataKind: "one";
424
- oneItem: true;
425
- } | {
426
- dataKind: "two";
427
- secondItem: false;
428
- };
429
- `);
430
- });
431
- });
432
- });
433
- describe("Bound to Enum", () => {
434
- it("creates a union declaration", async () => {
435
- const {
436
- TestEnum
437
- } = await runner.compile(`
438
- namespace DemoService;
439
- @test enum TestEnum {
440
- one: "one",
441
- two: "two"
442
- }
443
- `);
444
- const res = render(_$createComponent(Output, {
445
- get program() {
446
- return runner.program;
447
- },
448
- get children() {
449
- return _$createComponent(SourceFile, {
450
- path: "test.ts",
451
- get children() {
452
- return _$createComponent(UnionDeclaration, {
453
- type: TestEnum
454
- });
455
- }
456
- });
457
- }
458
- }));
459
- assertFileContents(res, d`
460
- type TestEnum = "one" | "two";
461
- `);
462
- });
463
- });
464
- });
465
- });