@typespec/emitter-framework 0.10.0-dev.1 → 0.10.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/src/core/write-output.js +10 -6
- package/dist/src/csharp/components/class/declaration.d.ts.map +1 -1
- package/dist/src/csharp/components/class/declaration.js +2 -1
- package/dist/src/typescript/components/index.d.ts +2 -2
- package/dist/src/typescript/components/index.js +2 -2
- package/dist/src/typescript/components/interface-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/interface-declaration.js +2 -2
- package/dist/src/typescript/components/type-declaration.js +1 -1
- package/dist/src/typescript/components/type-expression.js +1 -1
- package/dist/src/typescript/components/{union-declaration.d.ts → union/declaration.d.ts} +1 -1
- package/dist/src/typescript/components/union/declaration.d.ts.map +1 -0
- package/dist/src/typescript/components/{union-declaration.js → union/declaration.js} +4 -4
- package/dist/src/typescript/components/union/declaration.test.d.ts +2 -0
- package/dist/src/typescript/components/union/declaration.test.d.ts.map +1 -0
- package/dist/src/typescript/components/union/declaration.test.js +308 -0
- package/dist/src/typescript/components/{union-expression.d.ts → union/expression.d.ts} +1 -1
- package/dist/src/typescript/components/union/expression.d.ts.map +1 -0
- package/dist/src/typescript/components/{union-expression.js → union/expression.js} +3 -3
- package/dist/src/typescript/components/union/expression.test.d.ts +2 -0
- package/dist/src/typescript/components/union/expression.test.d.ts.map +1 -0
- package/dist/src/typescript/components/union/expression.test.js +51 -0
- package/dist/test/typescript/components/function-declaration.test.js +31 -112
- package/dist/test/typescript/components/interface-declaration.test.js +146 -308
- package/dist/test/typescript/components/type-alias-declaration.test.js +17 -75
- package/dist/test/typescript/components/type-transform.test.js +32 -83
- package/dist/test/typescript/components/value-expression.test.js +3 -6
- package/dist/test/typescript/utils.d.ts +7 -0
- package/dist/test/typescript/utils.d.ts.map +1 -0
- package/dist/test/typescript/utils.js +18 -0
- package/dist/test/utils.d.ts.map +1 -1
- package/dist/test/utils.js +2 -0
- package/package.json +7 -7
- package/src/core/write-output.ts +10 -6
- package/src/csharp/components/class/declaration.tsx +2 -1
- package/src/typescript/components/index.ts +2 -2
- package/src/typescript/components/interface-declaration.tsx +3 -3
- package/src/typescript/components/type-declaration.tsx +1 -1
- package/src/typescript/components/type-expression.tsx +1 -1
- package/src/typescript/components/union/declaration.test.tsx +264 -0
- package/src/typescript/components/{union-declaration.tsx → union/declaration.tsx} +4 -4
- package/src/typescript/components/union/expression.test.tsx +42 -0
- package/src/typescript/components/{union-expression.tsx → union/expression.tsx} +3 -3
- package/test/typescript/components/function-declaration.test.tsx +31 -121
- package/test/typescript/components/interface-declaration.test.tsx +123 -342
- package/test/typescript/components/type-alias-declaration.test.tsx +17 -76
- package/test/typescript/components/type-transform.test.tsx +32 -93
- package/test/typescript/components/value-expression.test.tsx +4 -12
- package/test/typescript/utils.tsx +12 -0
- package/test/utils.ts +2 -1
- package/dist/src/typescript/components/union-declaration.d.ts.map +0 -1
- package/dist/src/typescript/components/union-expression.d.ts.map +0 -1
- package/dist/test/typescript/components/union-declaration.test.d.ts +0 -2
- package/dist/test/typescript/components/union-declaration.test.d.ts.map +0 -1
- package/dist/test/typescript/components/union-declaration.test.js +0 -465
- package/test/typescript/components/union-declaration.test.tsx +0 -411
|
@@ -6,13 +6,17 @@ export async function writeOutput(program, rootComponent, emitterOutputDir) {
|
|
|
6
6
|
}
|
|
7
7
|
async function writeOutputDirectory(program, dir, emitterOutputDir) {
|
|
8
8
|
for (const sub of dir.contents) {
|
|
9
|
-
if (
|
|
10
|
-
|
|
9
|
+
if ("contents" in sub) {
|
|
10
|
+
if (Array.isArray(sub.contents)) {
|
|
11
|
+
await writeOutputDirectory(program, sub, emitterOutputDir);
|
|
12
|
+
} else {
|
|
13
|
+
await emitFile(program, {
|
|
14
|
+
content: sub.contents,
|
|
15
|
+
path: joinPaths(emitterOutputDir, sub.path)
|
|
16
|
+
});
|
|
17
|
+
}
|
|
11
18
|
} else {
|
|
12
|
-
|
|
13
|
-
content: sub.contents,
|
|
14
|
-
path: joinPaths(emitterOutputDir, sub.path)
|
|
15
|
-
});
|
|
19
|
+
// TODO: support copy file
|
|
16
20
|
}
|
|
17
21
|
}
|
|
18
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"declaration.d.ts","sourceRoot":"","sources":["../../../../../src/csharp/components/class/declaration.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAO,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"declaration.d.ts","sourceRoot":"","sources":["../../../../../src/csharp/components/class/declaration.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAO,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEvC,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAO3D,MAAM,WAAW,qBAAsB,SAAQ,IAAI,CAAC,EAAE,CAAC,qBAAqB,EAAE,MAAM,CAAC;IACnF,iFAAiF;IACjF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wCAAwC;IACxC,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC;IACxB,8FAA8F;IAC9F,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAYD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,QAAQ,CAuBvE"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { memo as _$memo, mergeProps as _$mergeProps, createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
2
|
import { For } from "@alloy-js/core";
|
|
3
3
|
import * as cs from "@alloy-js/csharp";
|
|
4
|
+
import { Method } from "@alloy-js/csharp";
|
|
4
5
|
import { useTsp } from "../../../core/index.js";
|
|
5
6
|
import { Property } from "../property/property.js";
|
|
6
7
|
import { TypeExpression } from "../type-expression.js";
|
|
@@ -57,7 +58,7 @@ function ClassMethods(props) {
|
|
|
57
58
|
const namePolicy = cs.useCSharpNamePolicy();
|
|
58
59
|
const abstractMethods = [];
|
|
59
60
|
for (const [name, method] of props.type.operations) {
|
|
60
|
-
abstractMethods.push(_$createComponent(
|
|
61
|
+
abstractMethods.push(_$createComponent(Method, {
|
|
61
62
|
get name() {
|
|
62
63
|
return namePolicy.getName(name, "class-method");
|
|
63
64
|
},
|
|
@@ -11,7 +11,7 @@ export * from "./static-serializers.js";
|
|
|
11
11
|
export * from "./type-declaration.js";
|
|
12
12
|
export * from "./type-expression.js";
|
|
13
13
|
export * from "./type-transform.js";
|
|
14
|
-
export * from "./union
|
|
15
|
-
export * from "./union
|
|
14
|
+
export * from "./union/declaration.js";
|
|
15
|
+
export * from "./union/expression.js";
|
|
16
16
|
export * from "./value-expression.js";
|
|
17
17
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -11,6 +11,6 @@ export * from "./static-serializers.js";
|
|
|
11
11
|
export * from "./type-declaration.js";
|
|
12
12
|
export * from "./type-expression.js";
|
|
13
13
|
export * from "./type-transform.js";
|
|
14
|
-
export * from "./union
|
|
15
|
-
export * from "./union
|
|
14
|
+
export * from "./union/declaration.js";
|
|
15
|
+
export * from "./union/expression.js";
|
|
16
16
|
export * from "./value-expression.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interface-declaration.d.ts","sourceRoot":"","sources":["../../../../src/typescript/components/interface-declaration.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"interface-declaration.d.ts","sourceRoot":"","sources":["../../../../src/typescript/components/interface-declaration.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAgB,MAAM,gBAAgB,CAAC;AAC7D,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC3C,OAAO,EACL,KAAK,SAAS,EAEd,KAAK,KAAK,EAIX,MAAM,oBAAoB,CAAC;AAQ5B,MAAM,WAAW,8BAA+B,SAAQ,IAAI,CAAC,EAAE,CAAC,yBAAyB,EAAE,MAAM,CAAC;IAChG,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,MAAM,yBAAyB,GACjC,8BAA8B,GAC9B,EAAE,CAAC,yBAAyB,CAAC;AAEjC,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,yBAAyB,YAmCpE;AAQD,MAAM,WAAW,wBAAyB,SAAQ,EAAE,CAAC,wBAAwB;IAC3E,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC;CACzB;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,wBAAwB,YAMlE"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createComponent as _$createComponent, mergeProps as _$mergeProps, memo as _$memo } from "@alloy-js/core/jsx-runtime";
|
|
2
|
-
import {
|
|
2
|
+
import { For, mapJoin } from "@alloy-js/core";
|
|
3
3
|
import * as ts from "@alloy-js/typescript";
|
|
4
4
|
import { isNeverType } from "@typespec/compiler";
|
|
5
5
|
import { createRekeyableMap } from "@typespec/compiler/utils";
|
|
@@ -50,7 +50,7 @@ function isTypedInterfaceDeclarationProps(props) {
|
|
|
50
50
|
return "type" in props;
|
|
51
51
|
}
|
|
52
52
|
export function InterfaceExpression(props) {
|
|
53
|
-
return _$createComponent(
|
|
53
|
+
return _$createComponent(ts.InterfaceExpression, {
|
|
54
54
|
get children() {
|
|
55
55
|
return _$createComponent(InterfaceBody, props);
|
|
56
56
|
}
|
|
@@ -5,7 +5,7 @@ import { declarationRefkeys } from "../utils/refkey.js";
|
|
|
5
5
|
import { EnumDeclaration } from "./enum-declaration.js";
|
|
6
6
|
import { InterfaceDeclaration } from "./interface-declaration.js";
|
|
7
7
|
import { TypeAliasDeclaration } from "./type-alias-declaration.js";
|
|
8
|
-
import { UnionDeclaration } from "./union
|
|
8
|
+
import { UnionDeclaration } from "./union/declaration.js";
|
|
9
9
|
export function TypeDeclaration(props) {
|
|
10
10
|
const {
|
|
11
11
|
$
|
|
@@ -9,7 +9,7 @@ import { ArrayExpression } from "./array-expression.js";
|
|
|
9
9
|
import { FunctionType } from "./function-type.js";
|
|
10
10
|
import { InterfaceExpression } from "./interface-declaration.js";
|
|
11
11
|
import { RecordExpression } from "./record-expression.js";
|
|
12
|
-
import { UnionExpression } from "./union
|
|
12
|
+
import { UnionExpression } from "./union/expression.js";
|
|
13
13
|
export function TypeExpression(props) {
|
|
14
14
|
const {
|
|
15
15
|
$
|
|
@@ -8,4 +8,4 @@ export interface TypedUnionDeclarationProps extends Omit<ts.TypeDeclarationProps
|
|
|
8
8
|
}
|
|
9
9
|
export type UnionDeclarationProps = TypedUnionDeclarationProps | ts.TypeDeclarationProps;
|
|
10
10
|
export declare function UnionDeclaration(props: UnionDeclarationProps): Children;
|
|
11
|
-
//# sourceMappingURL=
|
|
11
|
+
//# sourceMappingURL=declaration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"declaration.d.ts","sourceRoot":"","sources":["../../../../../src/typescript/components/union/declaration.tsx"],"names":[],"mappings":"AACA,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;AAKtD,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,9 +1,9 @@
|
|
|
1
1
|
import { mergeProps as _$mergeProps, createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { declarationRefkeys } from "#typescript/utils/refkey.js";
|
|
2
3
|
import * as ts from "@alloy-js/typescript";
|
|
3
|
-
import { useTsp } from "
|
|
4
|
-
import { reportDiagnostic } from "
|
|
5
|
-
import {
|
|
6
|
-
import { UnionExpression } from "./union-expression.js";
|
|
4
|
+
import { useTsp } from "../../../core/context/tsp-context.js";
|
|
5
|
+
import { reportDiagnostic } from "../../../lib.js";
|
|
6
|
+
import { UnionExpression } from "./expression.js";
|
|
7
7
|
export function UnionDeclaration(props) {
|
|
8
8
|
const {
|
|
9
9
|
$
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"declaration.test.d.ts","sourceRoot":"","sources":["../../../../../src/typescript/components/union/declaration.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { Output } from "#core/index.js";
|
|
3
|
+
import { Tester } from "#test/test-host.js";
|
|
4
|
+
import { SourceFile } from "@alloy-js/typescript";
|
|
5
|
+
import { t } from "@typespec/compiler/testing";
|
|
6
|
+
import { beforeEach, expect, it } from "vitest";
|
|
7
|
+
import { InterfaceDeclaration } from "../../index.js";
|
|
8
|
+
import { UnionDeclaration } from "./declaration.js";
|
|
9
|
+
let tester;
|
|
10
|
+
function Wrapper(props) {
|
|
11
|
+
return _$createComponent(Output, {
|
|
12
|
+
get program() {
|
|
13
|
+
return tester.program;
|
|
14
|
+
},
|
|
15
|
+
get children() {
|
|
16
|
+
return _$createComponent(SourceFile, {
|
|
17
|
+
path: "test.ts",
|
|
18
|
+
get children() {
|
|
19
|
+
return props.children;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
beforeEach(async () => {
|
|
26
|
+
tester = await Tester.createInstance();
|
|
27
|
+
});
|
|
28
|
+
it("creates a union declaration (not bound to Typespec Types)", async () => {
|
|
29
|
+
await tester.compile("");
|
|
30
|
+
expect(_$createComponent(Wrapper, {
|
|
31
|
+
get children() {
|
|
32
|
+
return _$createComponent(UnionDeclaration, {
|
|
33
|
+
name: "MyUnion",
|
|
34
|
+
children: "\"red\" | \"blue\""
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
})).toRenderTo(`type MyUnion = "red" | "blue";`);
|
|
38
|
+
});
|
|
39
|
+
it("creates a union declaration (bound to union)", async () => {
|
|
40
|
+
const {
|
|
41
|
+
TestUnion
|
|
42
|
+
} = await tester.compile(t.code`
|
|
43
|
+
namespace DemoService;
|
|
44
|
+
union ${t.union("TestUnion")} {
|
|
45
|
+
one: "one",
|
|
46
|
+
two: "two"
|
|
47
|
+
}
|
|
48
|
+
`);
|
|
49
|
+
expect(_$createComponent(Wrapper, {
|
|
50
|
+
get children() {
|
|
51
|
+
return _$createComponent(UnionDeclaration, {
|
|
52
|
+
type: TestUnion
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
})).toRenderTo(`type TestUnion = "one" | "two";`);
|
|
56
|
+
});
|
|
57
|
+
it("creates a union declaration with JSDoc", async () => {
|
|
58
|
+
const {
|
|
59
|
+
TestUnion
|
|
60
|
+
} = await tester.compile(t.code`
|
|
61
|
+
namespace DemoService;
|
|
62
|
+
/**
|
|
63
|
+
* Test Union
|
|
64
|
+
*/
|
|
65
|
+
union ${t.union("TestUnion")} {
|
|
66
|
+
one: "one",
|
|
67
|
+
two: "two"
|
|
68
|
+
}
|
|
69
|
+
`);
|
|
70
|
+
expect(_$createComponent(Wrapper, {
|
|
71
|
+
get children() {
|
|
72
|
+
return _$createComponent(UnionDeclaration, {
|
|
73
|
+
type: TestUnion
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
})).toRenderTo(`/**
|
|
77
|
+
* Test Union
|
|
78
|
+
*/
|
|
79
|
+
type TestUnion = "one" | "two";`);
|
|
80
|
+
});
|
|
81
|
+
it("creates a union declaration with name override", async () => {
|
|
82
|
+
const {
|
|
83
|
+
TestUnion
|
|
84
|
+
} = await tester.compile(t.code`
|
|
85
|
+
namespace DemoService;
|
|
86
|
+
union ${t.union("TestUnion")} {
|
|
87
|
+
one: "one",
|
|
88
|
+
two: "two"
|
|
89
|
+
}
|
|
90
|
+
`);
|
|
91
|
+
expect(_$createComponent(Wrapper, {
|
|
92
|
+
get children() {
|
|
93
|
+
return _$createComponent(UnionDeclaration, {
|
|
94
|
+
"export": true,
|
|
95
|
+
type: TestUnion,
|
|
96
|
+
name: "MyUnion"
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
})).toRenderTo(`export type MyUnion = "one" | "two";`);
|
|
100
|
+
});
|
|
101
|
+
it("creates a union declaration with extra children", async () => {
|
|
102
|
+
const {
|
|
103
|
+
TestUnion
|
|
104
|
+
} = await tester.compile(t.code`
|
|
105
|
+
namespace DemoService;
|
|
106
|
+
union ${t.union("TestUnion")} {
|
|
107
|
+
one: "one",
|
|
108
|
+
two: "two"
|
|
109
|
+
}
|
|
110
|
+
`);
|
|
111
|
+
expect(_$createComponent(Wrapper, {
|
|
112
|
+
get children() {
|
|
113
|
+
return _$createComponent(UnionDeclaration, {
|
|
114
|
+
type: TestUnion,
|
|
115
|
+
children: "\"three\""
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
})).toRenderTo(`type TestUnion = "one" | "two" | "three";`);
|
|
119
|
+
});
|
|
120
|
+
it("renders a discriminated union", async () => {
|
|
121
|
+
const {
|
|
122
|
+
TestUnion
|
|
123
|
+
} = await tester.compile(t.code`
|
|
124
|
+
namespace DemoService;
|
|
125
|
+
@discriminated
|
|
126
|
+
union ${t.union("TestUnion")} {
|
|
127
|
+
one: { oneItem: true },
|
|
128
|
+
two: true
|
|
129
|
+
}
|
|
130
|
+
`);
|
|
131
|
+
expect(_$createComponent(Wrapper, {
|
|
132
|
+
get children() {
|
|
133
|
+
return _$createComponent(UnionDeclaration, {
|
|
134
|
+
type: TestUnion
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
})).toRenderTo(`type TestUnion = {
|
|
138
|
+
kind: "one";
|
|
139
|
+
value: {
|
|
140
|
+
oneItem: true;
|
|
141
|
+
};
|
|
142
|
+
} | {
|
|
143
|
+
kind: "two";
|
|
144
|
+
value: true;
|
|
145
|
+
};`);
|
|
146
|
+
});
|
|
147
|
+
it("renders a discriminated union with custom properties", async () => {
|
|
148
|
+
const {
|
|
149
|
+
TestUnion
|
|
150
|
+
} = await tester.compile(t.code`
|
|
151
|
+
namespace DemoService;
|
|
152
|
+
@discriminated(#{ discriminatorPropertyName: "dataKind", envelopePropertyName: "data" })
|
|
153
|
+
union ${t.union("TestUnion")} {
|
|
154
|
+
one: { oneItem: true },
|
|
155
|
+
two: true
|
|
156
|
+
}
|
|
157
|
+
`);
|
|
158
|
+
expect(_$createComponent(Wrapper, {
|
|
159
|
+
get children() {
|
|
160
|
+
return _$createComponent(UnionDeclaration, {
|
|
161
|
+
type: TestUnion
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
})).toRenderTo(`type TestUnion = {
|
|
165
|
+
dataKind: "one";
|
|
166
|
+
data: {
|
|
167
|
+
oneItem: true;
|
|
168
|
+
};
|
|
169
|
+
} | {
|
|
170
|
+
dataKind: "two";
|
|
171
|
+
data: true;
|
|
172
|
+
};`);
|
|
173
|
+
});
|
|
174
|
+
it("renders a discriminated union with named models", async () => {
|
|
175
|
+
const {
|
|
176
|
+
Pet,
|
|
177
|
+
Cat,
|
|
178
|
+
Dog
|
|
179
|
+
} = await tester.compile(t.code`
|
|
180
|
+
namespace DemoService;
|
|
181
|
+
model ${t.model("Cat")} {
|
|
182
|
+
name: string;
|
|
183
|
+
meow: boolean;
|
|
184
|
+
}
|
|
185
|
+
model ${t.model("Dog")} {
|
|
186
|
+
name: string;
|
|
187
|
+
bark: boolean;
|
|
188
|
+
}
|
|
189
|
+
@discriminated
|
|
190
|
+
union ${t.union("Pet")} {
|
|
191
|
+
cat: Cat,
|
|
192
|
+
dog: Dog,
|
|
193
|
+
}
|
|
194
|
+
`);
|
|
195
|
+
expect(_$createComponent(Wrapper, {
|
|
196
|
+
get children() {
|
|
197
|
+
return [_$createComponent(InterfaceDeclaration, {
|
|
198
|
+
type: Cat
|
|
199
|
+
}), _$createIntrinsic("hbr", {}), _$createComponent(InterfaceDeclaration, {
|
|
200
|
+
type: Dog
|
|
201
|
+
}), _$createIntrinsic("hbr", {}), _$createComponent(UnionDeclaration, {
|
|
202
|
+
type: Pet
|
|
203
|
+
})];
|
|
204
|
+
}
|
|
205
|
+
})).toRenderTo(`interface Cat {
|
|
206
|
+
name: string;
|
|
207
|
+
meow: boolean;
|
|
208
|
+
}
|
|
209
|
+
interface Dog {
|
|
210
|
+
name: string;
|
|
211
|
+
bark: boolean;
|
|
212
|
+
}
|
|
213
|
+
type Pet = {
|
|
214
|
+
kind: "cat";
|
|
215
|
+
value: Cat;
|
|
216
|
+
} | {
|
|
217
|
+
kind: "dog";
|
|
218
|
+
value: Dog;
|
|
219
|
+
};`);
|
|
220
|
+
});
|
|
221
|
+
it("renders named discriminated union (no envelope)", async () => {
|
|
222
|
+
const {
|
|
223
|
+
Pet,
|
|
224
|
+
Cat,
|
|
225
|
+
Dog
|
|
226
|
+
} = await tester.compile(t.code`
|
|
227
|
+
namespace DemoService;
|
|
228
|
+
model ${t.model("Cat")} {
|
|
229
|
+
name: string;
|
|
230
|
+
meow: boolean;
|
|
231
|
+
}
|
|
232
|
+
model ${t.model("Dog")} {
|
|
233
|
+
name: string;
|
|
234
|
+
bark: boolean;
|
|
235
|
+
}
|
|
236
|
+
@discriminated(#{ envelope: "none", discriminatorPropertyName: "dataKind" })
|
|
237
|
+
union ${t.union("Pet")} {
|
|
238
|
+
cat: Cat,
|
|
239
|
+
dog: Dog,
|
|
240
|
+
}
|
|
241
|
+
`);
|
|
242
|
+
expect(_$createComponent(Wrapper, {
|
|
243
|
+
get children() {
|
|
244
|
+
return [_$createComponent(InterfaceDeclaration, {
|
|
245
|
+
type: Cat
|
|
246
|
+
}), _$createIntrinsic("hbr", {}), _$createComponent(InterfaceDeclaration, {
|
|
247
|
+
type: Dog
|
|
248
|
+
}), _$createIntrinsic("hbr", {}), _$createComponent(UnionDeclaration, {
|
|
249
|
+
type: Pet
|
|
250
|
+
})];
|
|
251
|
+
}
|
|
252
|
+
})).toRenderTo(`interface Cat {
|
|
253
|
+
name: string;
|
|
254
|
+
meow: boolean;
|
|
255
|
+
}
|
|
256
|
+
interface Dog {
|
|
257
|
+
name: string;
|
|
258
|
+
bark: boolean;
|
|
259
|
+
}
|
|
260
|
+
type Pet = {
|
|
261
|
+
dataKind: "cat"
|
|
262
|
+
} & Cat | {
|
|
263
|
+
dataKind: "dog"
|
|
264
|
+
} & Dog;`);
|
|
265
|
+
});
|
|
266
|
+
it("renders anonymous discriminated union (no envelope)", async () => {
|
|
267
|
+
const {
|
|
268
|
+
TestUnion
|
|
269
|
+
} = await tester.compile(t.code`
|
|
270
|
+
namespace DemoService;
|
|
271
|
+
@discriminated(#{ envelope: "none", discriminatorPropertyName: "dataKind" })
|
|
272
|
+
union ${t.union("TestUnion")} {
|
|
273
|
+
one: { oneItem: true },
|
|
274
|
+
two: { secondItem: false }
|
|
275
|
+
}
|
|
276
|
+
`);
|
|
277
|
+
expect(_$createComponent(Wrapper, {
|
|
278
|
+
get children() {
|
|
279
|
+
return _$createComponent(UnionDeclaration, {
|
|
280
|
+
type: TestUnion
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
})).toRenderTo(`type TestUnion = {
|
|
284
|
+
dataKind: "one";
|
|
285
|
+
oneItem: true;
|
|
286
|
+
} | {
|
|
287
|
+
dataKind: "two";
|
|
288
|
+
secondItem: false;
|
|
289
|
+
};`);
|
|
290
|
+
});
|
|
291
|
+
it("creates a union declaration (bound to enum)", async () => {
|
|
292
|
+
const {
|
|
293
|
+
TestEnum
|
|
294
|
+
} = await tester.compile(t.code`
|
|
295
|
+
namespace DemoService;
|
|
296
|
+
enum ${t.enum("TestEnum")} {
|
|
297
|
+
one: "one",
|
|
298
|
+
two: "two"
|
|
299
|
+
}
|
|
300
|
+
`);
|
|
301
|
+
expect(_$createComponent(Wrapper, {
|
|
302
|
+
get children() {
|
|
303
|
+
return _$createComponent(UnionDeclaration, {
|
|
304
|
+
type: TestEnum
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
})).toRenderTo(`type TestEnum = "one" | "two";`);
|
|
308
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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"}
|
|
@@ -2,9 +2,9 @@ import { createComponent as _$createComponent, memo as _$memo } from "@alloy-js/
|
|
|
2
2
|
import { For, List } from "@alloy-js/core";
|
|
3
3
|
import * as ts from "@alloy-js/typescript";
|
|
4
4
|
import { compilerAssert } from "@typespec/compiler";
|
|
5
|
-
import { useTsp } from "
|
|
6
|
-
import { efRefkey } from "
|
|
7
|
-
import { TypeExpression } from "
|
|
5
|
+
import { useTsp } from "../../../core/context/tsp-context.js";
|
|
6
|
+
import { efRefkey } from "../../utils/refkey.js";
|
|
7
|
+
import { TypeExpression } from "../type-expression.js";
|
|
8
8
|
export function UnionExpression({
|
|
9
9
|
type,
|
|
10
10
|
children
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expression.test.d.ts","sourceRoot":"","sources":["../../../../../src/typescript/components/union/expression.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { Tester } from "#test/test-host.js";
|
|
3
|
+
import { TestFile } from "#test/typescript/utils.js";
|
|
4
|
+
import { t } from "@typespec/compiler/testing";
|
|
5
|
+
import { expect, it } from "vitest";
|
|
6
|
+
import { UnionExpression } from "./expression.js";
|
|
7
|
+
it("renders a union expression", async () => {
|
|
8
|
+
const {
|
|
9
|
+
program,
|
|
10
|
+
TestUnion
|
|
11
|
+
} = await Tester.compile(t.code`
|
|
12
|
+
union ${t.union("TestUnion")} {
|
|
13
|
+
one: "one",
|
|
14
|
+
two: "two"
|
|
15
|
+
}
|
|
16
|
+
`);
|
|
17
|
+
expect(_$createComponent(TestFile, {
|
|
18
|
+
program: program,
|
|
19
|
+
get children() {
|
|
20
|
+
return _$createComponent(UnionExpression, {
|
|
21
|
+
type: TestUnion
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
})).toRenderTo(`"one" | "two"`);
|
|
25
|
+
});
|
|
26
|
+
it("renders a union expression without conflicting names", async () => {
|
|
27
|
+
const {
|
|
28
|
+
program,
|
|
29
|
+
TestUnion
|
|
30
|
+
} = await Tester.compile(t.code`
|
|
31
|
+
union ${t.union("TestUnion")} {
|
|
32
|
+
{common: "one"},
|
|
33
|
+
{common: "one", two: "two"}
|
|
34
|
+
}
|
|
35
|
+
`);
|
|
36
|
+
expect(_$createComponent(TestFile, {
|
|
37
|
+
program: program,
|
|
38
|
+
get children() {
|
|
39
|
+
return _$createComponent(UnionExpression, {
|
|
40
|
+
type: TestUnion
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
})).toRenderTo(`
|
|
44
|
+
{
|
|
45
|
+
common: "one";
|
|
46
|
+
} | {
|
|
47
|
+
common: "one";
|
|
48
|
+
two: "two";
|
|
49
|
+
}
|
|
50
|
+
`);
|
|
51
|
+
});
|