@typespec/emitter-framework 0.8.0-dev.1 → 0.8.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/CHANGELOG.md +11 -0
- package/dist/src/typescript/components/enum-declaration.d.ts +1 -0
- package/dist/src/typescript/components/enum-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/enum-declaration.js +7 -0
- package/dist/src/typescript/components/function-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/function-declaration.js +8 -3
- package/dist/src/typescript/components/interface-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/interface-declaration.js +3 -0
- package/dist/src/typescript/components/interface-member.d.ts +3 -1
- package/dist/src/typescript/components/interface-member.d.ts.map +1 -1
- package/dist/src/typescript/components/interface-member.js +2 -0
- package/dist/src/typescript/components/interface-method.d.ts +3 -1
- package/dist/src/typescript/components/interface-method.d.ts.map +1 -1
- package/dist/src/typescript/components/interface-method.js +9 -2
- package/dist/src/typescript/components/type-alias-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/type-alias-declaration.js +4 -1
- package/dist/src/typescript/components/type-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/type-declaration.js +10 -0
- package/dist/src/typescript/components/union-declaration.d.ts +3 -1
- package/dist/src/typescript/components/union-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/union-declaration.js +4 -1
- package/dist/src/typescript/components/union-expression.d.ts.map +1 -1
- package/dist/src/typescript/components/union-expression.js +103 -10
- package/dist/src/typescript/utils/operation.d.ts.map +1 -1
- package/dist/src/typescript/utils/operation.js +5 -0
- package/dist/test/typescript/components/enum-declaration.test.js +79 -0
- package/dist/test/typescript/components/function-declaration.test.js +81 -0
- package/dist/test/typescript/components/interface-declaration.test.js +232 -9
- package/dist/test/typescript/components/type-alias-declaration.test.js +75 -0
- package/dist/test/typescript/components/union-declaration.test.js +358 -106
- package/package.json +6 -6
- package/src/typescript/components/enum-declaration.tsx +12 -1
- package/src/typescript/components/function-declaration.tsx +6 -1
- package/src/typescript/components/interface-declaration.tsx +3 -1
- package/src/typescript/components/interface-member.tsx +4 -0
- package/src/typescript/components/interface-method.tsx +7 -1
- package/src/typescript/components/type-alias-declaration.tsx +2 -1
- package/src/typescript/components/type-declaration.tsx +8 -6
- package/src/typescript/components/union-declaration.tsx +4 -1
- package/src/typescript/components/union-expression.tsx +100 -7
- package/src/typescript/utils/operation.ts +3 -0
- package/test/typescript/components/enum-declaration.test.tsx +72 -0
- package/test/typescript/components/function-declaration.test.tsx +78 -0
- package/test/typescript/components/interface-declaration.test.tsx +223 -9
- package/test/typescript/components/type-alias-declaration.test.tsx +72 -0
- package/test/typescript/components/union-declaration.test.tsx +330 -102
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
1
|
+
import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
|
|
2
2
|
import { render } from "@alloy-js/core";
|
|
3
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
4
|
import { SourceFile } from "@alloy-js/typescript";
|
|
4
|
-
import {
|
|
5
|
-
import { assert, describe, expect, it } from "vitest";
|
|
5
|
+
import { beforeEach, describe, it } from "vitest";
|
|
6
6
|
import { Output } from "../../../src/core/components/output.js";
|
|
7
7
|
import { UnionDeclaration } from "../../../src/typescript/components/union-declaration.js";
|
|
8
8
|
import { UnionExpression } from "../../../src/typescript/components/union-expression.js";
|
|
9
|
-
import {
|
|
9
|
+
import { InterfaceDeclaration } from "../../../src/typescript/index.js";
|
|
10
|
+
import { assertFileContents } from "../../utils.js";
|
|
11
|
+
import { createEmitterFrameworkTestRunner } from "../test-host.js";
|
|
10
12
|
describe("Typescript Union Declaration", () => {
|
|
13
|
+
let runner;
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
runner = await createEmitterFrameworkTestRunner();
|
|
16
|
+
});
|
|
11
17
|
describe("Union not bound to Typespec Types", () => {
|
|
12
18
|
it("creates a union declaration", async () => {
|
|
13
|
-
|
|
19
|
+
await runner.compile(``);
|
|
14
20
|
const res = render(_$createComponent(Output, {
|
|
15
|
-
program
|
|
21
|
+
get program() {
|
|
22
|
+
return runner.program;
|
|
23
|
+
},
|
|
16
24
|
get children() {
|
|
17
25
|
return _$createComponent(SourceFile, {
|
|
18
26
|
path: "test.ts",
|
|
@@ -25,188 +33,432 @@ describe("Typescript Union Declaration", () => {
|
|
|
25
33
|
});
|
|
26
34
|
}
|
|
27
35
|
}));
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
parser: "typescript"
|
|
32
|
-
});
|
|
33
|
-
const expectedContent = await format(`type MyUnion = "red" | "blue"`, {
|
|
34
|
-
parser: "typescript"
|
|
35
|
-
});
|
|
36
|
-
expect(actualContent).toBe(expectedContent);
|
|
36
|
+
assertFileContents(res, d`
|
|
37
|
+
type MyUnion = "red" | "blue";
|
|
38
|
+
`);
|
|
37
39
|
});
|
|
38
40
|
});
|
|
39
41
|
describe("Union bound to Typespec Types", () => {
|
|
40
42
|
describe("Bound to Union", () => {
|
|
41
43
|
it("creates a union declaration", async () => {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
const {
|
|
45
|
+
TestUnion
|
|
46
|
+
} = await runner.compile(`
|
|
47
|
+
namespace DemoService;
|
|
48
|
+
@test union TestUnion {
|
|
49
|
+
one: "one",
|
|
50
|
+
two: "two"
|
|
51
|
+
}
|
|
48
52
|
`);
|
|
49
|
-
const [namespace] = program.resolveTypeReference("DemoService");
|
|
50
|
-
const union = Array.from(namespace.unions.values())[0];
|
|
51
53
|
const res = render(_$createComponent(Output, {
|
|
52
|
-
program
|
|
54
|
+
get program() {
|
|
55
|
+
return runner.program;
|
|
56
|
+
},
|
|
53
57
|
get children() {
|
|
54
58
|
return _$createComponent(SourceFile, {
|
|
55
59
|
path: "test.ts",
|
|
56
60
|
get children() {
|
|
57
61
|
return _$createComponent(UnionDeclaration, {
|
|
58
|
-
type:
|
|
62
|
+
type: TestUnion
|
|
59
63
|
});
|
|
60
64
|
}
|
|
61
65
|
});
|
|
62
66
|
}
|
|
63
67
|
}));
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
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
|
+
`);
|
|
73
106
|
});
|
|
74
107
|
it("creates a union declaration with name override", async () => {
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
108
|
+
const {
|
|
109
|
+
TestUnion
|
|
110
|
+
} = await runner.compile(`
|
|
111
|
+
namespace DemoService;
|
|
112
|
+
@test union TestUnion {
|
|
113
|
+
one: "one",
|
|
114
|
+
two: "two"
|
|
115
|
+
}
|
|
81
116
|
`);
|
|
82
|
-
const [namespace] = program.resolveTypeReference("DemoService");
|
|
83
|
-
const union = Array.from(namespace.unions.values())[0];
|
|
84
117
|
const res = render(_$createComponent(Output, {
|
|
85
|
-
program
|
|
118
|
+
get program() {
|
|
119
|
+
return runner.program;
|
|
120
|
+
},
|
|
86
121
|
get children() {
|
|
87
122
|
return _$createComponent(SourceFile, {
|
|
88
123
|
path: "test.ts",
|
|
89
124
|
get children() {
|
|
90
125
|
return _$createComponent(UnionDeclaration, {
|
|
91
126
|
"export": true,
|
|
92
|
-
type:
|
|
127
|
+
type: TestUnion,
|
|
93
128
|
name: "MyUnion"
|
|
94
129
|
});
|
|
95
130
|
}
|
|
96
131
|
});
|
|
97
132
|
}
|
|
98
133
|
}));
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
parser: "typescript"
|
|
103
|
-
});
|
|
104
|
-
const expectedContent = await format(`export type MyUnion = "one" | "two"`, {
|
|
105
|
-
parser: "typescript"
|
|
106
|
-
});
|
|
107
|
-
expect(actualContent).toBe(expectedContent);
|
|
134
|
+
assertFileContents(res, d`
|
|
135
|
+
export type MyUnion = "one" | "two";
|
|
136
|
+
`);
|
|
108
137
|
});
|
|
109
138
|
it("creates a union declaration with extra children", async () => {
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
139
|
+
const {
|
|
140
|
+
TestUnion
|
|
141
|
+
} = await runner.compile(`
|
|
142
|
+
namespace DemoService;
|
|
143
|
+
@test union TestUnion {
|
|
144
|
+
one: "one",
|
|
145
|
+
two: "two"
|
|
146
|
+
}
|
|
116
147
|
`);
|
|
117
|
-
const [namespace] = program.resolveTypeReference("DemoService");
|
|
118
|
-
const union = Array.from(namespace.unions.values())[0];
|
|
119
148
|
const res = render(_$createComponent(Output, {
|
|
120
|
-
program
|
|
149
|
+
get program() {
|
|
150
|
+
return runner.program;
|
|
151
|
+
},
|
|
121
152
|
get children() {
|
|
122
153
|
return _$createComponent(SourceFile, {
|
|
123
154
|
path: "test.ts",
|
|
124
155
|
get children() {
|
|
125
156
|
return _$createComponent(UnionDeclaration, {
|
|
126
|
-
type:
|
|
157
|
+
type: TestUnion,
|
|
127
158
|
children: "\"three\""
|
|
128
159
|
});
|
|
129
160
|
}
|
|
130
161
|
});
|
|
131
162
|
}
|
|
132
163
|
}));
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
parser: "typescript"
|
|
137
|
-
});
|
|
138
|
-
const expectedContent = await format(`type TestUnion = "one" | "two" | "three"`, {
|
|
139
|
-
parser: "typescript"
|
|
140
|
-
});
|
|
141
|
-
expect(actualContent).toBe(expectedContent);
|
|
164
|
+
assertFileContents(res, d`
|
|
165
|
+
type TestUnion = "one" | "two" | "three";
|
|
166
|
+
`);
|
|
142
167
|
});
|
|
143
|
-
it("renders
|
|
144
|
-
const
|
|
168
|
+
it("renders a union expression", async () => {
|
|
169
|
+
const {
|
|
170
|
+
TestUnion
|
|
171
|
+
} = await runner.compile(`
|
|
145
172
|
namespace DemoService;
|
|
146
|
-
union TestUnion {
|
|
173
|
+
@test union TestUnion {
|
|
147
174
|
one: "one",
|
|
148
175
|
two: "two"
|
|
149
176
|
}
|
|
150
|
-
|
|
151
|
-
const [namespace] = program.resolveTypeReference("DemoService");
|
|
152
|
-
const union = Array.from(namespace.unions.values())[0];
|
|
177
|
+
`);
|
|
153
178
|
const res = render(_$createComponent(Output, {
|
|
154
|
-
program
|
|
179
|
+
get program() {
|
|
180
|
+
return runner.program;
|
|
181
|
+
},
|
|
155
182
|
get children() {
|
|
156
183
|
return _$createComponent(SourceFile, {
|
|
157
184
|
path: "test.ts",
|
|
158
185
|
get children() {
|
|
159
186
|
return ["let x: ", _$createComponent(UnionExpression, {
|
|
160
|
-
type:
|
|
187
|
+
type: TestUnion
|
|
161
188
|
}), " = \"one\";"];
|
|
162
189
|
}
|
|
163
190
|
});
|
|
164
191
|
}
|
|
165
192
|
}));
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
+
`);
|
|
170
235
|
});
|
|
171
|
-
|
|
172
|
-
|
|
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
|
+
`);
|
|
173
430
|
});
|
|
174
|
-
expect(actualContent).toBe(expectedContent);
|
|
175
431
|
});
|
|
176
432
|
});
|
|
177
433
|
describe("Bound to Enum", () => {
|
|
178
434
|
it("creates a union declaration", async () => {
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
435
|
+
const {
|
|
436
|
+
TestEnum
|
|
437
|
+
} = await runner.compile(`
|
|
438
|
+
namespace DemoService;
|
|
439
|
+
@test enum TestEnum {
|
|
440
|
+
one: "one",
|
|
441
|
+
two: "two"
|
|
442
|
+
}
|
|
185
443
|
`);
|
|
186
|
-
const [namespace] = program.resolveTypeReference("DemoService");
|
|
187
|
-
const union = Array.from(namespace.enums.values())[0];
|
|
188
444
|
const res = render(_$createComponent(Output, {
|
|
189
|
-
program
|
|
445
|
+
get program() {
|
|
446
|
+
return runner.program;
|
|
447
|
+
},
|
|
190
448
|
get children() {
|
|
191
449
|
return _$createComponent(SourceFile, {
|
|
192
450
|
path: "test.ts",
|
|
193
451
|
get children() {
|
|
194
452
|
return _$createComponent(UnionDeclaration, {
|
|
195
|
-
type:
|
|
453
|
+
type: TestEnum
|
|
196
454
|
});
|
|
197
455
|
}
|
|
198
456
|
});
|
|
199
457
|
}
|
|
200
458
|
}));
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
parser: "typescript"
|
|
205
|
-
});
|
|
206
|
-
const expectedContent = await format(`type TestEnum = "one" | "two"`, {
|
|
207
|
-
parser: "typescript"
|
|
208
|
-
});
|
|
209
|
-
expect(actualContent).toBe(expectedContent);
|
|
459
|
+
assertFileContents(res, d`
|
|
460
|
+
type TestEnum = "one" | "two";
|
|
461
|
+
`);
|
|
210
462
|
});
|
|
211
463
|
});
|
|
212
464
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typespec/emitter-framework",
|
|
3
|
-
"version": "0.8.0-dev.
|
|
3
|
+
"version": "0.8.0-dev.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -19,17 +19,17 @@
|
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"description": "",
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@alloy-js/core": "^0.
|
|
23
|
-
"@alloy-js/typescript": "^0.
|
|
22
|
+
"@alloy-js/core": "^0.16.0",
|
|
23
|
+
"@alloy-js/typescript": "^0.16.0",
|
|
24
24
|
"@typespec/compiler": "^1.0.0",
|
|
25
25
|
"@typespec/http": "^1.0.1",
|
|
26
26
|
"@typespec/rest": "^0.70.0 || >=0.71.0-dev <0.71.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@alloy-js/cli": "^0.
|
|
29
|
+
"@alloy-js/cli": "^0.16.0",
|
|
30
30
|
"@alloy-js/rollup-plugin": "^0.1.0",
|
|
31
|
-
"@alloy-js/core": "^0.
|
|
32
|
-
"@alloy-js/typescript": "^0.
|
|
31
|
+
"@alloy-js/core": "^0.16.0",
|
|
32
|
+
"@alloy-js/typescript": "^0.16.0",
|
|
33
33
|
"@types/minimist": "^1.2.5",
|
|
34
34
|
"@typespec/compiler": "^1.0.0",
|
|
35
35
|
"@typespec/http": "^1.0.1",
|
|
@@ -28,13 +28,22 @@ export function EnumDeclaration(props: EnumDeclarationProps) {
|
|
|
28
28
|
const refkeys = declarationRefkeys(props.refkey, props.type);
|
|
29
29
|
const name = props.name ?? ts.useTSNamePolicy().getName(props.type.name!, "enum");
|
|
30
30
|
const members = Array.from(type.members.entries());
|
|
31
|
+
const doc = props.doc ?? $.type.getDoc(type);
|
|
31
32
|
|
|
32
33
|
return (
|
|
33
|
-
<ts.EnumDeclaration
|
|
34
|
+
<ts.EnumDeclaration
|
|
35
|
+
doc={doc}
|
|
36
|
+
name={name}
|
|
37
|
+
refkey={refkeys}
|
|
38
|
+
default={props.default}
|
|
39
|
+
export={props.export}
|
|
40
|
+
>
|
|
34
41
|
<ay.For each={members} joiner={",\n"}>
|
|
35
42
|
{([key, value]) => {
|
|
43
|
+
const memberDoc = $.type.getDoc(value);
|
|
36
44
|
return (
|
|
37
45
|
<EnumMember
|
|
46
|
+
doc={memberDoc}
|
|
38
47
|
type={value}
|
|
39
48
|
refkey={
|
|
40
49
|
$.union.is(props.type) ? efRefkey(props.type.variants.get(key)) : efRefkey(value)
|
|
@@ -49,12 +58,14 @@ export function EnumDeclaration(props: EnumDeclarationProps) {
|
|
|
49
58
|
|
|
50
59
|
export interface EnumMemberProps {
|
|
51
60
|
type: TspEnumMember;
|
|
61
|
+
doc?: ay.Children;
|
|
52
62
|
refkey?: ay.Refkey;
|
|
53
63
|
}
|
|
54
64
|
|
|
55
65
|
export function EnumMember(props: EnumMemberProps) {
|
|
56
66
|
return (
|
|
57
67
|
<ts.EnumMember
|
|
68
|
+
doc={props.doc}
|
|
58
69
|
name={props.type.name}
|
|
59
70
|
jsValue={props.type.value ?? props.type.name}
|
|
60
71
|
refkey={props.refkey}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as ts from "@alloy-js/typescript";
|
|
2
2
|
import { Model, Operation } from "@typespec/compiler";
|
|
3
|
+
import { useTsp } from "../../core/index.js";
|
|
3
4
|
import { buildParameterDescriptors, getReturnType } from "../utils/operation.js";
|
|
4
5
|
import { declarationRefkeys } from "../utils/refkey.js";
|
|
5
6
|
import { TypeExpression } from "./type-expression.js";
|
|
@@ -21,6 +22,8 @@ export type FunctionDeclarationProps =
|
|
|
21
22
|
* provided will take precedence.
|
|
22
23
|
*/
|
|
23
24
|
export function FunctionDeclaration(props: FunctionDeclarationProps) {
|
|
25
|
+
const { $ } = useTsp();
|
|
26
|
+
|
|
24
27
|
if (!isTypedFunctionDeclarationProps(props)) {
|
|
25
28
|
return <ts.FunctionDeclaration {...props} />;
|
|
26
29
|
}
|
|
@@ -40,8 +43,10 @@ export function FunctionDeclaration(props: FunctionDeclarationProps) {
|
|
|
40
43
|
params: props.parameters,
|
|
41
44
|
mode: props.parametersMode,
|
|
42
45
|
});
|
|
46
|
+
const doc = props.doc ?? $.type.getDoc(props.type);
|
|
43
47
|
return (
|
|
44
48
|
<ts.FunctionDeclaration
|
|
49
|
+
doc={doc}
|
|
45
50
|
refkey={refkeys}
|
|
46
51
|
name={name}
|
|
47
52
|
async={props.async}
|
|
@@ -49,8 +54,8 @@ export function FunctionDeclaration(props: FunctionDeclarationProps) {
|
|
|
49
54
|
export={props.export}
|
|
50
55
|
kind={props.kind}
|
|
51
56
|
returnType={returnType}
|
|
57
|
+
parameters={allParameters}
|
|
52
58
|
>
|
|
53
|
-
<ts.FunctionDeclaration.Parameters parameters={allParameters} />
|
|
54
59
|
{props.children}
|
|
55
60
|
</ts.FunctionDeclaration>
|
|
56
61
|
);
|