@typespec/emitter-framework 0.10.0-dev.0 → 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 (31) 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 +12 -12
  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/vitest.config.ts +1 -1
  26. package/dist/src/typescript/components/union-declaration.d.ts.map +0 -1
  27. package/dist/src/typescript/components/union-expression.d.ts.map +0 -1
  28. package/dist/test/typescript/components/union-declaration.test.d.ts +0 -2
  29. package/dist/test/typescript/components/union-declaration.test.d.ts.map +0 -1
  30. package/dist/test/typescript/components/union-declaration.test.js +0 -465
  31. package/test/typescript/components/union-declaration.test.tsx +0 -411
@@ -0,0 +1,264 @@
1
+ import { Output } from "#core/index.js";
2
+ import { Tester } from "#test/test-host.js";
3
+ import type { Children } from "@alloy-js/core";
4
+ import { SourceFile } from "@alloy-js/typescript";
5
+ import { t, type TesterInstance } from "@typespec/compiler/testing";
6
+ import { beforeEach, expect, it } from "vitest";
7
+ import { InterfaceDeclaration } from "../../index.js";
8
+ import { UnionDeclaration } from "./declaration.jsx";
9
+
10
+ let tester: TesterInstance;
11
+
12
+ function Wrapper(props: { children: Children }) {
13
+ return (
14
+ <Output program={tester.program}>
15
+ <SourceFile path="test.ts">{props.children}</SourceFile>
16
+ </Output>
17
+ );
18
+ }
19
+
20
+ beforeEach(async () => {
21
+ tester = await Tester.createInstance();
22
+ });
23
+
24
+ it("creates a union declaration (not bound to Typespec Types)", async () => {
25
+ await tester.compile("");
26
+ expect(
27
+ <Wrapper>
28
+ <UnionDeclaration name="MyUnion">"red" | "blue"</UnionDeclaration>
29
+ </Wrapper>,
30
+ ).toRenderTo(`type MyUnion = "red" | "blue";`);
31
+ });
32
+
33
+ it("creates a union declaration (bound to union)", async () => {
34
+ const { TestUnion } = await tester.compile(t.code`
35
+ namespace DemoService;
36
+ union ${t.union("TestUnion")} {
37
+ one: "one",
38
+ two: "two"
39
+ }
40
+ `);
41
+ expect(
42
+ <Wrapper>
43
+ <UnionDeclaration type={TestUnion} />
44
+ </Wrapper>,
45
+ ).toRenderTo(`type TestUnion = "one" | "two";`);
46
+ });
47
+
48
+ it("creates a union declaration with JSDoc", async () => {
49
+ const { TestUnion } = await tester.compile(t.code`
50
+ namespace DemoService;
51
+ /**
52
+ * Test Union
53
+ */
54
+ union ${t.union("TestUnion")} {
55
+ one: "one",
56
+ two: "two"
57
+ }
58
+ `);
59
+ expect(
60
+ <Wrapper>
61
+ <UnionDeclaration type={TestUnion} />
62
+ </Wrapper>,
63
+ ).toRenderTo(`/**
64
+ * Test Union
65
+ */
66
+ type TestUnion = "one" | "two";`);
67
+ });
68
+
69
+ it("creates a union declaration with name override", async () => {
70
+ const { TestUnion } = await tester.compile(t.code`
71
+ namespace DemoService;
72
+ union ${t.union("TestUnion")} {
73
+ one: "one",
74
+ two: "two"
75
+ }
76
+ `);
77
+ expect(
78
+ <Wrapper>
79
+ <UnionDeclaration export type={TestUnion} name="MyUnion" />
80
+ </Wrapper>,
81
+ ).toRenderTo(`export type MyUnion = "one" | "two";`);
82
+ });
83
+
84
+ it("creates a union declaration with extra children", async () => {
85
+ const { TestUnion } = await tester.compile(t.code`
86
+ namespace DemoService;
87
+ union ${t.union("TestUnion")} {
88
+ one: "one",
89
+ two: "two"
90
+ }
91
+ `);
92
+ expect(
93
+ <Wrapper>
94
+ <UnionDeclaration type={TestUnion}>"three"</UnionDeclaration>
95
+ </Wrapper>,
96
+ ).toRenderTo(`type TestUnion = "one" | "two" | "three";`);
97
+ });
98
+
99
+ it("renders a discriminated union", async () => {
100
+ const { TestUnion } = await tester.compile(t.code`
101
+ namespace DemoService;
102
+ @discriminated
103
+ union ${t.union("TestUnion")} {
104
+ one: { oneItem: true },
105
+ two: true
106
+ }
107
+ `);
108
+ expect(
109
+ <Wrapper>
110
+ <UnionDeclaration type={TestUnion} />
111
+ </Wrapper>,
112
+ ).toRenderTo(`type TestUnion = {
113
+ kind: "one";
114
+ value: {
115
+ oneItem: true;
116
+ };
117
+ } | {
118
+ kind: "two";
119
+ value: true;
120
+ };`);
121
+ });
122
+
123
+ it("renders a discriminated union with custom properties", async () => {
124
+ const { TestUnion } = await tester.compile(t.code`
125
+ namespace DemoService;
126
+ @discriminated(#{ discriminatorPropertyName: "dataKind", envelopePropertyName: "data" })
127
+ union ${t.union("TestUnion")} {
128
+ one: { oneItem: true },
129
+ two: true
130
+ }
131
+ `);
132
+ expect(
133
+ <Wrapper>
134
+ <UnionDeclaration type={TestUnion} />
135
+ </Wrapper>,
136
+ ).toRenderTo(`type TestUnion = {
137
+ dataKind: "one";
138
+ data: {
139
+ oneItem: true;
140
+ };
141
+ } | {
142
+ dataKind: "two";
143
+ data: true;
144
+ };`);
145
+ });
146
+
147
+ it("renders a discriminated union with named models", async () => {
148
+ const { Pet, Cat, Dog } = await tester.compile(t.code`
149
+ namespace DemoService;
150
+ model ${t.model("Cat")} {
151
+ name: string;
152
+ meow: boolean;
153
+ }
154
+ model ${t.model("Dog")} {
155
+ name: string;
156
+ bark: boolean;
157
+ }
158
+ @discriminated
159
+ union ${t.union("Pet")} {
160
+ cat: Cat,
161
+ dog: Dog,
162
+ }
163
+ `);
164
+ expect(
165
+ <Wrapper>
166
+ <InterfaceDeclaration type={Cat} />
167
+ <hbr />
168
+ <InterfaceDeclaration type={Dog} />
169
+ <hbr />
170
+ <UnionDeclaration type={Pet} />
171
+ </Wrapper>,
172
+ ).toRenderTo(`interface Cat {
173
+ name: string;
174
+ meow: boolean;
175
+ }
176
+ interface Dog {
177
+ name: string;
178
+ bark: boolean;
179
+ }
180
+ type Pet = {
181
+ kind: "cat";
182
+ value: Cat;
183
+ } | {
184
+ kind: "dog";
185
+ value: Dog;
186
+ };`);
187
+ });
188
+
189
+ it("renders named discriminated union (no envelope)", async () => {
190
+ const { Pet, Cat, Dog } = await tester.compile(t.code`
191
+ namespace DemoService;
192
+ model ${t.model("Cat")} {
193
+ name: string;
194
+ meow: boolean;
195
+ }
196
+ model ${t.model("Dog")} {
197
+ name: string;
198
+ bark: boolean;
199
+ }
200
+ @discriminated(#{ envelope: "none", discriminatorPropertyName: "dataKind" })
201
+ union ${t.union("Pet")} {
202
+ cat: Cat,
203
+ dog: Dog,
204
+ }
205
+ `);
206
+ expect(
207
+ <Wrapper>
208
+ <InterfaceDeclaration type={Cat} />
209
+ <hbr />
210
+ <InterfaceDeclaration type={Dog} />
211
+ <hbr />
212
+ <UnionDeclaration type={Pet} />
213
+ </Wrapper>,
214
+ ).toRenderTo(`interface Cat {
215
+ name: string;
216
+ meow: boolean;
217
+ }
218
+ interface Dog {
219
+ name: string;
220
+ bark: boolean;
221
+ }
222
+ type Pet = {
223
+ dataKind: "cat"
224
+ } & Cat | {
225
+ dataKind: "dog"
226
+ } & Dog;`);
227
+ });
228
+
229
+ it("renders anonymous discriminated union (no envelope)", async () => {
230
+ const { TestUnion } = await tester.compile(t.code`
231
+ namespace DemoService;
232
+ @discriminated(#{ envelope: "none", discriminatorPropertyName: "dataKind" })
233
+ union ${t.union("TestUnion")} {
234
+ one: { oneItem: true },
235
+ two: { secondItem: false }
236
+ }
237
+ `);
238
+ expect(
239
+ <Wrapper>
240
+ <UnionDeclaration type={TestUnion} />
241
+ </Wrapper>,
242
+ ).toRenderTo(`type TestUnion = {
243
+ dataKind: "one";
244
+ oneItem: true;
245
+ } | {
246
+ dataKind: "two";
247
+ secondItem: false;
248
+ };`);
249
+ });
250
+
251
+ it("creates a union declaration (bound to enum)", async () => {
252
+ const { TestEnum } = await tester.compile(t.code`
253
+ namespace DemoService;
254
+ enum ${t.enum("TestEnum")} {
255
+ one: "one",
256
+ two: "two"
257
+ }
258
+ `);
259
+ expect(
260
+ <Wrapper>
261
+ <UnionDeclaration type={TestEnum} />
262
+ </Wrapper>,
263
+ ).toRenderTo(`type TestEnum = "one" | "two";`);
264
+ });
@@ -1,10 +1,10 @@
1
+ import { declarationRefkeys } from "#typescript/utils/refkey.js";
1
2
  import { type Children } from "@alloy-js/core";
2
3
  import * as ts from "@alloy-js/typescript";
3
4
  import type { Enum, Union } from "@typespec/compiler";
4
- import { useTsp } from "../../core/context/tsp-context.js";
5
- import { reportDiagnostic } from "../../lib.js";
6
- import { declarationRefkeys } from "../utils/refkey.js";
7
- import { UnionExpression } from "./union-expression.js";
5
+ import { useTsp } from "../../../core/context/tsp-context.js";
6
+ import { reportDiagnostic } from "../../../lib.js";
7
+ import { UnionExpression } from "./expression.jsx";
8
8
 
9
9
  export interface TypedUnionDeclarationProps extends Omit<ts.TypeDeclarationProps, "name"> {
10
10
  type: Union | Enum;
@@ -0,0 +1,42 @@
1
+ import { Output } from "#core/index.js";
2
+ import { Tester } from "#test/test-host.js";
3
+ import { t } from "@typespec/compiler/testing";
4
+ import { expect, it } from "vitest";
5
+ import { UnionExpression } from "./expression.jsx";
6
+
7
+ it("renders a union expression", async () => {
8
+ const { program, TestUnion } = await Tester.compile(t.code`
9
+ union ${t.union("TestUnion")} {
10
+ one: "one",
11
+ two: "two"
12
+ }
13
+ `);
14
+
15
+ expect(
16
+ <Output program={program}>
17
+ <UnionExpression type={TestUnion} />
18
+ </Output>,
19
+ ).toRenderTo(`"one" | "two"`);
20
+ });
21
+
22
+ it("renders a union expression without conflicting names", async () => {
23
+ const { program, TestUnion } = await Tester.compile(t.code`
24
+ union ${t.union("TestUnion")} {
25
+ {common: "one"},
26
+ {common: "one", two: "two"}
27
+ }
28
+ `);
29
+
30
+ expect(
31
+ <Output program={program}>
32
+ <UnionExpression type={TestUnion} />
33
+ </Output>,
34
+ ).toRenderTo(`
35
+ {
36
+ common: "one";
37
+ } | {
38
+ common: "one";
39
+ two: "two";
40
+ }
41
+ `);
42
+ });
@@ -7,9 +7,9 @@ import {
7
7
  type Union,
8
8
  type UnionVariant,
9
9
  } from "@typespec/compiler";
10
- import { useTsp } from "../../core/context/tsp-context.js";
11
- import { efRefkey } from "../utils/refkey.js";
12
- import { TypeExpression } from "./type-expression.jsx";
10
+ import { useTsp } from "../../../core/context/tsp-context.js";
11
+ import { efRefkey } from "../../utils/refkey.js";
12
+ import { TypeExpression } from "../type-expression.jsx";
13
13
 
14
14
  export interface UnionExpressionProps {
15
15
  type: Union | Enum;
package/vitest.config.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import alloyPlugin from "@alloy-js/rollup-plugin";
2
2
  import { defineConfig, mergeConfig } from "vitest/config";
3
- import { defaultTypeSpecVitestConfig } from "../../vitest.workspace.js";
3
+ import { defaultTypeSpecVitestConfig } from "../../vitest.config.js";
4
4
 
5
5
  export default mergeConfig(
6
6
  defaultTypeSpecVitestConfig,
@@ -1 +0,0 @@
1
- {"version":3,"file":"union-declaration.d.ts","sourceRoot":"","sources":["../../../../src/typescript/components/union-declaration.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC3C,OAAO,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAMtD,MAAM,WAAW,0BAA2B,SAAQ,IAAI,CAAC,EAAE,CAAC,oBAAoB,EAAE,MAAM,CAAC;IACvF,IAAI,EAAE,KAAK,GAAG,IAAI,CAAC;IACnB,GAAG,CAAC,EAAE,QAAQ,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,MAAM,qBAAqB,GAAG,0BAA0B,GAAG,EAAE,CAAC,oBAAoB,CAAC;AAEzF,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,YAuB5D"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"union-expression.d.ts","sourceRoot":"","sources":["../../../../src/typescript/components/union-expression.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAa,MAAM,gBAAgB,CAAC;AAE1D,OAAO,EAEL,KAAK,IAAI,EAET,KAAK,KAAK,EAEX,MAAM,oBAAoB,CAAC;AAK5B,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,KAAK,GAAG,IAAI,CAAC;IACnB,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB;AAED,wBAAgB,eAAe,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,oBAAoB,YA+CvE"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=union-declaration.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"union-declaration.test.d.ts","sourceRoot":"","sources":["../../../../test/typescript/components/union-declaration.test.tsx"],"names":[],"mappings":""}