@typespec/emitter-framework 0.11.0-dev.1 → 0.11.0-dev.10
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/components/index.js +2 -1
- package/dist/src/core/components/index.js.map +1 -0
- package/dist/src/core/components/output.js +2 -1
- package/dist/src/core/components/output.js.map +1 -0
- package/dist/src/core/components/overrides/component-overrides.js +2 -1
- package/dist/src/core/components/overrides/component-overrides.js.map +1 -0
- package/dist/src/core/components/overrides/config.js +2 -1
- package/dist/src/core/components/overrides/config.js.map +1 -0
- package/dist/src/core/components/overrides/context.js +2 -1
- package/dist/src/core/components/overrides/context.js.map +1 -0
- package/dist/src/core/context/index.js +2 -1
- package/dist/src/core/context/index.js.map +1 -0
- package/dist/src/core/context/name-policy-context.js +2 -1
- package/dist/src/core/context/name-policy-context.js.map +1 -0
- package/dist/src/core/context/tsp-context.js +2 -1
- package/dist/src/core/context/tsp-context.js.map +1 -0
- package/dist/src/core/index.js +2 -1
- package/dist/src/core/index.js.map +1 -0
- package/dist/src/core/transport-name-policy.js +2 -1
- package/dist/src/core/transport-name-policy.js.map +1 -0
- package/dist/src/core/write-output.d.ts.map +1 -1
- package/dist/src/core/write-output.js +4 -3
- package/dist/src/core/write-output.js.map +1 -0
- package/dist/src/csharp/components/class/declaration.d.ts +1 -1
- package/dist/src/csharp/components/class/declaration.d.ts.map +1 -1
- package/dist/src/csharp/components/class/declaration.js +14 -5
- package/dist/src/csharp/components/class/declaration.js.map +1 -0
- package/dist/src/csharp/components/class/declaration.test.js +65 -105
- package/dist/src/csharp/components/class/declaration.test.js.map +1 -0
- package/dist/src/csharp/components/enum/declaration.js +2 -1
- package/dist/src/csharp/components/enum/declaration.js.map +1 -0
- package/dist/src/csharp/components/enum/declaration.test.js +52 -88
- package/dist/src/csharp/components/enum/declaration.test.js.map +1 -0
- package/dist/src/csharp/components/index.js +2 -1
- package/dist/src/csharp/components/index.js.map +1 -0
- package/dist/src/csharp/components/property/property.d.ts.map +1 -1
- package/dist/src/csharp/components/property/property.js +40 -14
- package/dist/src/csharp/components/property/property.js.map +1 -0
- package/dist/src/csharp/components/property/property.test.js +129 -29
- package/dist/src/csharp/components/property/property.test.js.map +1 -0
- package/dist/src/csharp/components/type-expression.d.ts.map +1 -1
- package/dist/src/csharp/components/type-expression.js +22 -3
- package/dist/src/csharp/components/type-expression.js.map +1 -0
- package/dist/src/csharp/components/type-expression.test.js +87 -54
- package/dist/src/csharp/components/type-expression.test.js.map +1 -0
- package/dist/src/csharp/components/utils/doc-comments.js +2 -1
- package/dist/src/csharp/components/utils/doc-comments.js.map +1 -0
- package/dist/src/csharp/components/utils/nullable-util.d.ts +4 -0
- package/dist/src/csharp/components/utils/nullable-util.d.ts.map +1 -0
- package/dist/src/csharp/components/utils/nullable-util.js +27 -0
- package/dist/src/csharp/components/utils/nullable-util.js.map +1 -0
- package/dist/src/csharp/components/utils/refkey.js +2 -1
- package/dist/src/csharp/components/utils/refkey.js.map +1 -0
- package/dist/src/csharp/index.js +2 -1
- package/dist/src/csharp/index.js.map +1 -0
- package/dist/src/lib.js +2 -1
- package/dist/src/lib.js.map +1 -0
- package/dist/src/testing/index.js +2 -1
- package/dist/src/testing/index.js.map +1 -0
- package/dist/src/testing/scenario-test/code-block-expectation.js +2 -1
- package/dist/src/testing/scenario-test/code-block-expectation.js.map +1 -0
- package/dist/src/testing/scenario-test/code-block-expectation.test.js +2 -1
- package/dist/src/testing/scenario-test/code-block-expectation.test.js.map +1 -0
- package/dist/src/testing/scenario-test/harness.js +2 -1
- package/dist/src/testing/scenario-test/harness.js.map +1 -0
- package/dist/src/testing/scenario-test/index.js +2 -1
- package/dist/src/testing/scenario-test/index.js.map +1 -0
- package/dist/src/testing/scenario-test/snippet-extractor.js +2 -1
- package/dist/src/testing/scenario-test/snippet-extractor.js.map +1 -0
- package/dist/src/typescript/components/array-expression.js +2 -1
- package/dist/src/typescript/components/array-expression.js.map +1 -0
- package/dist/src/typescript/components/arrow-function.js +2 -1
- package/dist/src/typescript/components/arrow-function.js.map +1 -0
- package/dist/src/typescript/components/class-method.js +2 -1
- package/dist/src/typescript/components/class-method.js.map +1 -0
- package/dist/src/typescript/components/enum-declaration.js +2 -1
- package/dist/src/typescript/components/enum-declaration.js.map +1 -0
- package/dist/src/typescript/components/function-declaration.js +2 -1
- package/dist/src/typescript/components/function-declaration.js.map +1 -0
- package/dist/src/typescript/components/function-expression.js +2 -1
- package/dist/src/typescript/components/function-expression.js.map +1 -0
- package/dist/src/typescript/components/function-type.js +2 -1
- package/dist/src/typescript/components/function-type.js.map +1 -0
- package/dist/src/typescript/components/index.js +2 -1
- package/dist/src/typescript/components/index.js.map +1 -0
- package/dist/src/typescript/components/interface-declaration.js +2 -1
- package/dist/src/typescript/components/interface-declaration.js.map +1 -0
- package/dist/src/typescript/components/interface-member.js +2 -1
- package/dist/src/typescript/components/interface-member.js.map +1 -0
- package/dist/src/typescript/components/interface-method.js +2 -1
- package/dist/src/typescript/components/interface-method.js.map +1 -0
- package/dist/src/typescript/components/record-expression.js +2 -1
- package/dist/src/typescript/components/record-expression.js.map +1 -0
- package/dist/src/typescript/components/static-serializers.js +2 -1
- package/dist/src/typescript/components/static-serializers.js.map +1 -0
- package/dist/src/typescript/components/type-alias-declaration.js +2 -1
- package/dist/src/typescript/components/type-alias-declaration.js.map +1 -0
- package/dist/src/typescript/components/type-declaration.js +2 -1
- package/dist/src/typescript/components/type-declaration.js.map +1 -0
- package/dist/src/typescript/components/type-expression.js +2 -1
- package/dist/src/typescript/components/type-expression.js.map +1 -0
- package/dist/src/typescript/components/type-transform.js +2 -1
- package/dist/src/typescript/components/type-transform.js.map +1 -0
- package/dist/src/typescript/components/union/declaration.js +2 -1
- package/dist/src/typescript/components/union/declaration.js.map +1 -0
- package/dist/src/typescript/components/union/declaration.test.js +2 -1
- package/dist/src/typescript/components/union/declaration.test.js.map +1 -0
- package/dist/src/typescript/components/union/expression.js +2 -1
- package/dist/src/typescript/components/union/expression.js.map +1 -0
- package/dist/src/typescript/components/union/expression.test.js +2 -1
- package/dist/src/typescript/components/union/expression.test.js.map +1 -0
- package/dist/src/typescript/components/value-expression.js +2 -1
- package/dist/src/typescript/components/value-expression.js.map +1 -0
- package/dist/src/typescript/index.js +2 -1
- package/dist/src/typescript/index.js.map +1 -0
- package/dist/src/typescript/lib.js +2 -1
- package/dist/src/typescript/lib.js.map +1 -0
- package/dist/src/typescript/utils/index.js +2 -1
- package/dist/src/typescript/utils/index.js.map +1 -0
- package/dist/src/typescript/utils/operation.js +2 -1
- package/dist/src/typescript/utils/operation.js.map +1 -0
- package/dist/src/typescript/utils/refkey.js +2 -1
- package/dist/src/typescript/utils/refkey.js.map +1 -0
- package/dist/test/test-host.js +2 -1
- package/dist/test/test-host.js.map +1 -0
- package/dist/test/testing/snippet-extractor-csharp.test.js +2 -1
- package/dist/test/testing/snippet-extractor-csharp.test.js.map +1 -0
- package/dist/test/testing/snippet-extractor-java.test.js +2 -1
- package/dist/test/testing/snippet-extractor-java.test.js.map +1 -0
- package/dist/test/testing/snippet-extractor-python.test.js +2 -1
- package/dist/test/testing/snippet-extractor-python.test.js.map +1 -0
- package/dist/test/testing/snippet-extractor-typescript.test.js +2 -1
- package/dist/test/testing/snippet-extractor-typescript.test.js.map +1 -0
- package/dist/test/typescript/components/arrow-function.test.js +18 -22
- package/dist/test/typescript/components/arrow-function.test.js.map +1 -0
- package/dist/test/typescript/components/component-override.test.js +3 -2
- package/dist/test/typescript/components/component-override.test.js.map +1 -0
- package/dist/test/typescript/components/enum-declaration.test.js +2 -1
- package/dist/test/typescript/components/enum-declaration.test.js.map +1 -0
- package/dist/test/typescript/components/function-declaration.test.js +3 -2
- package/dist/test/typescript/components/function-declaration.test.js.map +1 -0
- package/dist/test/typescript/components/function-expression.test.js +18 -22
- package/dist/test/typescript/components/function-expression.test.js.map +1 -0
- package/dist/test/typescript/components/function-type.test.js +18 -22
- package/dist/test/typescript/components/function-type.test.js.map +1 -0
- package/dist/test/typescript/components/interface-declaration.test.js +3 -2
- package/dist/test/typescript/components/interface-declaration.test.js.map +1 -0
- package/dist/test/typescript/components/interface-method.test.js +37 -45
- package/dist/test/typescript/components/interface-method.test.js.map +1 -0
- package/dist/test/typescript/components/member-expression.test.js +2 -1
- package/dist/test/typescript/components/member-expression.test.js.map +1 -0
- package/dist/test/typescript/components/type-alias-declaration.test.js +8 -5
- package/dist/test/typescript/components/type-alias-declaration.test.js.map +1 -0
- package/dist/test/typescript/components/type-transform.test.js +29 -27
- package/dist/test/typescript/components/type-transform.test.js.map +1 -0
- package/dist/test/typescript/components/value-expression.test.js +3 -2
- package/dist/test/typescript/components/value-expression.test.js.map +1 -0
- package/dist/test/typescript/utils.js +2 -1
- package/dist/test/typescript/utils.js.map +1 -0
- package/dist/test/utils.d.ts +1 -0
- package/dist/test/utils.d.ts.map +1 -1
- package/dist/test/utils.js +13 -3
- package/dist/test/utils.js.map +1 -0
- package/dist/test/vitest.setup.js +2 -1
- package/dist/test/vitest.setup.js.map +1 -0
- package/package.json +8 -8
- package/src/core/write-output.ts +2 -2
- package/src/csharp/components/class/declaration.test.tsx +64 -102
- package/src/csharp/components/class/declaration.tsx +13 -3
- package/src/csharp/components/enum/declaration.test.tsx +47 -78
- package/src/csharp/components/property/property.test.tsx +108 -25
- package/src/csharp/components/property/property.tsx +43 -11
- package/src/csharp/components/type-expression.test.tsx +84 -69
- package/src/csharp/components/type-expression.tsx +25 -4
- package/src/csharp/components/utils/nullable-util.ts +25 -0
- package/test/typescript/components/arrow-function.test.tsx +23 -42
- package/test/typescript/components/component-override.test.tsx +1 -1
- package/test/typescript/components/function-declaration.test.tsx +2 -1
- package/test/typescript/components/function-expression.test.tsx +23 -42
- package/test/typescript/components/function-type.test.tsx +22 -40
- package/test/typescript/components/interface-declaration.test.tsx +1 -1
- package/test/typescript/components/interface-method.test.tsx +50 -92
- package/test/typescript/components/type-alias-declaration.test.tsx +8 -6
- package/test/typescript/components/type-transform.test.tsx +37 -37
- package/test/typescript/components/value-expression.test.tsx +1 -1
- package/test/utils.ts +10 -3
- package/dist/test/typescript/test-host.d.ts +0 -12
- package/dist/test/typescript/test-host.d.ts.map +0 -1
- package/dist/test/typescript/test-host.js +0 -29
- package/test/typescript/test-host.ts +0 -37
|
@@ -1,27 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
5
|
-
import type
|
|
6
|
-
import { beforeEach, describe, it } from "vitest";
|
|
7
|
-
import { createEmitterFrameworkTestRunner } from "../../../test/typescript/test-host.js";
|
|
8
|
-
import { assertFileContents } from "../../../test/utils.js";
|
|
1
|
+
import { Tester } from "#test/test-host.js";
|
|
2
|
+
import { type Children } from "@alloy-js/core";
|
|
3
|
+
import { SourceFile } from "@alloy-js/csharp";
|
|
4
|
+
import type { ModelProperty } from "@typespec/compiler";
|
|
5
|
+
import { t, type TesterInstance } from "@typespec/compiler/testing";
|
|
6
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
9
7
|
import { Output } from "../../core/index.js";
|
|
10
8
|
import { ClassDeclaration } from "./class/declaration.js";
|
|
11
9
|
import { TypeExpression } from "./type-expression.jsx";
|
|
12
10
|
|
|
13
|
-
let runner:
|
|
11
|
+
let runner: TesterInstance;
|
|
14
12
|
|
|
15
13
|
beforeEach(async () => {
|
|
16
|
-
runner = await
|
|
14
|
+
runner = await Tester.createInstance();
|
|
17
15
|
});
|
|
18
16
|
|
|
19
17
|
function Wrapper(props: { children: Children }) {
|
|
20
18
|
return (
|
|
21
19
|
<Output program={runner.program}>
|
|
22
|
-
<
|
|
23
|
-
<SourceFile path="test.ts">{props.children}</SourceFile>
|
|
24
|
-
</Namespace>
|
|
20
|
+
<SourceFile path="test.ts">{props.children}</SourceFile>
|
|
25
21
|
</Output>
|
|
26
22
|
);
|
|
27
23
|
}
|
|
@@ -43,91 +39,110 @@ describe("map scalar to c# built-in types", () => {
|
|
|
43
39
|
["int64", "long"],
|
|
44
40
|
])("%s => %s", async (tspType, csType) => {
|
|
45
41
|
const type = await compileType(tspType);
|
|
46
|
-
|
|
42
|
+
expect(
|
|
47
43
|
<Wrapper>
|
|
48
44
|
<TypeExpression type={type} />
|
|
49
45
|
</Wrapper>,
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
res,
|
|
54
|
-
d`
|
|
55
|
-
namespace TestNamespace
|
|
56
|
-
{
|
|
57
|
-
${csType}
|
|
58
|
-
}
|
|
59
|
-
`,
|
|
60
|
-
);
|
|
46
|
+
).toRenderTo(`
|
|
47
|
+
${csType}
|
|
48
|
+
`);
|
|
61
49
|
});
|
|
62
50
|
});
|
|
63
51
|
|
|
64
52
|
it("maps array to c# array", async () => {
|
|
65
53
|
const type = await compileType("int32[]");
|
|
66
|
-
|
|
54
|
+
expect(
|
|
67
55
|
<Wrapper>
|
|
68
56
|
<TypeExpression type={type} />
|
|
69
57
|
</Wrapper>,
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
res,
|
|
74
|
-
d`
|
|
75
|
-
namespace TestNamespace
|
|
76
|
-
{
|
|
77
|
-
int[]
|
|
78
|
-
}
|
|
79
|
-
`,
|
|
80
|
-
);
|
|
58
|
+
).toRenderTo(`
|
|
59
|
+
int[]
|
|
60
|
+
`);
|
|
81
61
|
});
|
|
82
62
|
|
|
83
63
|
describe("Record map to IDictionary", () => {
|
|
84
64
|
it("for primitive types", async () => {
|
|
85
65
|
const type = await compileType("Record<int32>");
|
|
86
|
-
|
|
66
|
+
expect(
|
|
87
67
|
<Wrapper>
|
|
88
68
|
<TypeExpression type={type} />
|
|
89
69
|
</Wrapper>,
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
res,
|
|
94
|
-
d`
|
|
95
|
-
namespace TestNamespace
|
|
96
|
-
{
|
|
97
|
-
IDictionary<string, int>
|
|
98
|
-
}
|
|
99
|
-
`,
|
|
100
|
-
);
|
|
70
|
+
).toRenderTo(`
|
|
71
|
+
IDictionary<string, int>
|
|
72
|
+
`);
|
|
101
73
|
});
|
|
102
74
|
|
|
103
75
|
it("for models", async () => {
|
|
104
|
-
const { test, Pet } =
|
|
76
|
+
const { test, Pet } = await runner.compile(t.code`
|
|
105
77
|
model Test {
|
|
106
|
-
|
|
78
|
+
${t.modelProperty("test")}: Record<Pet>;
|
|
107
79
|
}
|
|
108
|
-
@test model Pet {}
|
|
109
|
-
`)
|
|
80
|
+
@test model ${t.model("Pet")} {}
|
|
81
|
+
`);
|
|
110
82
|
|
|
111
|
-
|
|
83
|
+
expect(
|
|
112
84
|
<Wrapper>
|
|
113
85
|
<ClassDeclaration type={Pet} />
|
|
114
86
|
<hbr />
|
|
115
87
|
<TypeExpression type={test.type} />
|
|
116
88
|
</Wrapper>,
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
89
|
+
).toRenderTo(`
|
|
90
|
+
class Pet
|
|
91
|
+
{
|
|
92
|
+
|
|
93
|
+
}
|
|
94
|
+
IDictionary<string, Pet>
|
|
95
|
+
`);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("Nullable union", () => {
|
|
100
|
+
it("nullable boolean", async () => {
|
|
101
|
+
const { Pet } = await runner.compile(t.code`
|
|
102
|
+
@test model ${t.model("Pet")} {
|
|
103
|
+
@test name: boolean | null;
|
|
104
|
+
}
|
|
105
|
+
`);
|
|
106
|
+
|
|
107
|
+
expect(
|
|
108
|
+
<Wrapper>
|
|
109
|
+
<ClassDeclaration type={Pet} />
|
|
110
|
+
</Wrapper>,
|
|
111
|
+
).toRenderTo(`
|
|
112
|
+
class Pet
|
|
113
|
+
{
|
|
114
|
+
public required bool? name { get; set; }
|
|
115
|
+
}
|
|
116
|
+
`);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("Literal types", () => {
|
|
121
|
+
it("literal types (string, int, double, bool)", async () => {
|
|
122
|
+
const { Pet } = await runner.compile(t.code`
|
|
123
|
+
@test model ${t.model("Pet")} {
|
|
124
|
+
@test boolName: true;
|
|
125
|
+
@test intName: 42;
|
|
126
|
+
@test doubleName: 3.14;
|
|
127
|
+
@test stringName: "Hello";
|
|
128
|
+
}
|
|
129
|
+
`);
|
|
130
|
+
|
|
131
|
+
expect(
|
|
132
|
+
<Wrapper>
|
|
133
|
+
<ClassDeclaration type={Pet} />
|
|
134
|
+
</Wrapper>,
|
|
135
|
+
).toRenderTo(`
|
|
136
|
+
class Pet
|
|
137
|
+
{
|
|
138
|
+
public required bool boolName { get; set; }
|
|
139
|
+
|
|
140
|
+
public required int intName { get; set; }
|
|
141
|
+
|
|
142
|
+
public required double doubleName { get; set; }
|
|
143
|
+
|
|
144
|
+
public required string stringName { get; set; }
|
|
145
|
+
}
|
|
146
|
+
`);
|
|
132
147
|
});
|
|
133
148
|
});
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { Experimental_OverridableComponent } from "#core/index.js";
|
|
2
|
-
import { type Children
|
|
2
|
+
import { code, type Children } from "@alloy-js/core";
|
|
3
3
|
import { Reference } from "@alloy-js/csharp";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
getTypeName,
|
|
6
|
+
isVoidType,
|
|
7
|
+
type IntrinsicType,
|
|
8
|
+
type Scalar,
|
|
9
|
+
type Type,
|
|
10
|
+
} from "@typespec/compiler";
|
|
5
11
|
import type { Typekit } from "@typespec/compiler/typekit";
|
|
6
12
|
import { useTsp } from "../../core/index.js";
|
|
7
13
|
import { reportTypescriptDiagnostic } from "../../typescript/lib.js";
|
|
14
|
+
import { getNullableUnionInnerType } from "./utils/nullable-util.js";
|
|
8
15
|
import { efRefkey } from "./utils/refkey.js";
|
|
9
16
|
|
|
10
17
|
export interface TypeExpressionProps {
|
|
@@ -12,6 +19,12 @@ export interface TypeExpressionProps {
|
|
|
12
19
|
}
|
|
13
20
|
|
|
14
21
|
export function TypeExpression(props: TypeExpressionProps): Children {
|
|
22
|
+
if (props.type.kind === "Union") {
|
|
23
|
+
const nullabletype = getNullableUnionInnerType(props.type);
|
|
24
|
+
if (nullabletype) {
|
|
25
|
+
return code`${(<TypeExpression type={nullabletype} />)}?`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
15
28
|
const { $ } = useTsp();
|
|
16
29
|
if (isDeclaration($, props.type)) {
|
|
17
30
|
return (
|
|
@@ -26,6 +39,15 @@ export function TypeExpression(props: TypeExpressionProps): Children {
|
|
|
26
39
|
return code`${(<TypeExpression type={props.type.indexer.value} />)}[]`;
|
|
27
40
|
} else if ($.record.is(props.type)) {
|
|
28
41
|
return code`IDictionary<string, ${(<TypeExpression type={props.type.indexer.value} />)}>`;
|
|
42
|
+
} else if ($.literal.isString(props.type)) {
|
|
43
|
+
// c# doesn't have literal types, so we map them to their corresponding C# types in general
|
|
44
|
+
return code`string`;
|
|
45
|
+
} else if ($.literal.isNumeric(props.type)) {
|
|
46
|
+
return Number.isInteger(props.type.value) ? code`int` : code`double`;
|
|
47
|
+
} else if ($.literal.isBoolean(props.type)) {
|
|
48
|
+
return code`bool`;
|
|
49
|
+
} else if (isVoidType(props.type)) {
|
|
50
|
+
return code`void`;
|
|
29
51
|
}
|
|
30
52
|
|
|
31
53
|
throw new Error(
|
|
@@ -112,8 +134,7 @@ function isDeclaration($: Typekit, type: Type): boolean {
|
|
|
112
134
|
if ($.array.is(type) || $.record.is(type)) {
|
|
113
135
|
return false;
|
|
114
136
|
}
|
|
115
|
-
|
|
116
|
-
return Boolean(type.name);
|
|
137
|
+
return true;
|
|
117
138
|
case "Union":
|
|
118
139
|
return Boolean(type.name);
|
|
119
140
|
default:
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useTsp } from "#core/index.js";
|
|
2
|
+
import type { Type, Union } from "@typespec/compiler";
|
|
3
|
+
|
|
4
|
+
/** Get the inner type if the union is a nullable, otherwise return undefined */
|
|
5
|
+
export function getNullableUnionInnerType(u: Union): Type | undefined {
|
|
6
|
+
const { $ } = useTsp();
|
|
7
|
+
const isNull = (type: Type) => type === $.intrinsic.null || type === $.intrinsic.void;
|
|
8
|
+
|
|
9
|
+
if (Array.from(u.variants.values()).some((v) => isNull(v.type))) {
|
|
10
|
+
const { $ } = useTsp();
|
|
11
|
+
const left = Array.from(u.variants.values()).filter((v) => !isNull(v.type));
|
|
12
|
+
if (left.length === 0) {
|
|
13
|
+
// a union only has null or void?
|
|
14
|
+
return $.intrinsic.void;
|
|
15
|
+
} else if (left.length === 1) {
|
|
16
|
+
return left[0].type;
|
|
17
|
+
} else {
|
|
18
|
+
return $.union.create({
|
|
19
|
+
name: u.name,
|
|
20
|
+
variants: left,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
@@ -1,71 +1,57 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { d } from "@alloy-js/core/testing";
|
|
1
|
+
import { Tester } from "#test/test-host.js";
|
|
3
2
|
import { SourceFile } from "@alloy-js/typescript";
|
|
4
|
-
import type
|
|
5
|
-
import
|
|
6
|
-
import { beforeEach, describe, it } from "vitest";
|
|
3
|
+
import { type TesterInstance, t } from "@typespec/compiler/testing";
|
|
4
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
7
5
|
import { Output } from "../../../src/core/components/output.jsx";
|
|
8
6
|
import { ArrowFunction } from "../../../src/typescript/components/arrow-function.jsx";
|
|
9
|
-
import { assertFileContents } from "../../utils.js";
|
|
10
|
-
import { createEmitterFrameworkTestRunner } from "../test-host.js";
|
|
11
7
|
|
|
12
8
|
describe("arrow functions with a `type` prop", () => {
|
|
13
|
-
let runner:
|
|
9
|
+
let runner: TesterInstance;
|
|
14
10
|
|
|
15
11
|
beforeEach(async () => {
|
|
16
|
-
runner = await
|
|
12
|
+
runner = await Tester.createInstance();
|
|
17
13
|
});
|
|
18
14
|
|
|
19
15
|
it("creates a function", async () => {
|
|
20
|
-
const { getName } =
|
|
21
|
-
@test op getName(id: string): string;
|
|
22
|
-
`)
|
|
16
|
+
const { getName } = await runner.compile(t.code`
|
|
17
|
+
@test op ${t.op("getName")}(id: string): string;
|
|
18
|
+
`);
|
|
23
19
|
|
|
24
|
-
|
|
20
|
+
expect(
|
|
25
21
|
<Output program={runner.program}>
|
|
26
22
|
<SourceFile path="test.ts">
|
|
27
23
|
<ArrowFunction type={getName}>console.log("Hello!");</ArrowFunction>
|
|
28
24
|
</SourceFile>
|
|
29
25
|
</Output>,
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
assertFileContents(
|
|
33
|
-
res,
|
|
34
|
-
d`
|
|
26
|
+
).toRenderTo(`
|
|
35
27
|
(id: string): string => {
|
|
36
28
|
console.log("Hello!");
|
|
37
29
|
}
|
|
38
|
-
|
|
39
|
-
);
|
|
30
|
+
`);
|
|
40
31
|
});
|
|
41
32
|
|
|
42
33
|
it("creates an async function", async () => {
|
|
43
|
-
const { getName } =
|
|
44
|
-
@test op getName(id: string): string;
|
|
45
|
-
`)
|
|
34
|
+
const { getName } = await runner.compile(t.code`
|
|
35
|
+
@test op ${t.op("getName")}(id: string): string;
|
|
36
|
+
`);
|
|
46
37
|
|
|
47
|
-
|
|
38
|
+
expect(
|
|
48
39
|
<Output program={runner.program}>
|
|
49
40
|
<SourceFile path="test.ts">
|
|
50
41
|
<ArrowFunction async type={getName} />
|
|
51
42
|
</SourceFile>
|
|
52
43
|
</Output>,
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
assertFileContents(
|
|
56
|
-
res,
|
|
57
|
-
d`
|
|
44
|
+
).toRenderTo(`
|
|
58
45
|
async (id: string): Promise<string> => {}
|
|
59
|
-
|
|
60
|
-
);
|
|
46
|
+
`);
|
|
61
47
|
});
|
|
62
48
|
|
|
63
49
|
it("can append extra parameters with raw params provided", async () => {
|
|
64
|
-
const { getName } =
|
|
65
|
-
@test op getName(id: string): string;
|
|
66
|
-
`)
|
|
50
|
+
const { getName } = await runner.compile(t.code`
|
|
51
|
+
@test op ${t.op("getName")}(id: string): string;
|
|
52
|
+
`);
|
|
67
53
|
|
|
68
|
-
|
|
54
|
+
expect(
|
|
69
55
|
<Output program={runner.program}>
|
|
70
56
|
<SourceFile path="test.ts">
|
|
71
57
|
<ArrowFunction
|
|
@@ -74,13 +60,8 @@ describe("arrow functions with a `type` prop", () => {
|
|
|
74
60
|
/>
|
|
75
61
|
</SourceFile>
|
|
76
62
|
</Output>,
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
assertFileContents(
|
|
80
|
-
res,
|
|
81
|
-
d`
|
|
63
|
+
).toRenderTo(`
|
|
82
64
|
(additionalParam: number, id: string): string => {}
|
|
83
|
-
|
|
84
|
-
);
|
|
65
|
+
`);
|
|
85
66
|
});
|
|
86
67
|
});
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
Experimental_ComponentOverrides,
|
|
3
3
|
Experimental_ComponentOverridesConfig,
|
|
4
4
|
} from "#core/index.js";
|
|
5
|
+
import { Tester } from "#test/test-host.js";
|
|
5
6
|
import { FunctionDeclaration } from "#typescript/index.js";
|
|
6
7
|
import { For, List, type Children } from "@alloy-js/core";
|
|
7
8
|
import { d } from "@alloy-js/core/testing";
|
|
@@ -10,7 +11,6 @@ import type { Namespace } from "@typespec/compiler";
|
|
|
10
11
|
import { expect, it } from "vitest";
|
|
11
12
|
import { Output } from "../../../src/core/components/output.jsx";
|
|
12
13
|
import { InterfaceDeclaration } from "../../../src/typescript/components/interface-declaration.jsx";
|
|
13
|
-
import { Tester } from "../../test-host.js";
|
|
14
14
|
|
|
15
15
|
it("uses overridden component", async () => {
|
|
16
16
|
const { program } = await Tester.compile(`
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getProgram } from "#test/utils.js";
|
|
1
2
|
import { code } from "@alloy-js/core";
|
|
2
3
|
import { d } from "@alloy-js/core/testing";
|
|
3
4
|
import { SourceFile } from "@alloy-js/typescript";
|
|
@@ -5,7 +6,7 @@ import type { Namespace } from "@typespec/compiler";
|
|
|
5
6
|
import { describe, expect, it } from "vitest";
|
|
6
7
|
import { Output } from "../../../src/core/components/output.jsx";
|
|
7
8
|
import { FunctionDeclaration } from "../../../src/typescript/components/function-declaration.js";
|
|
8
|
-
|
|
9
|
+
|
|
9
10
|
describe("Typescript Function Declaration", () => {
|
|
10
11
|
describe("Function bound to Typespec Types", () => {
|
|
11
12
|
describe("Bound to Operation", () => {
|
|
@@ -1,71 +1,57 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { d } from "@alloy-js/core/testing";
|
|
1
|
+
import { Tester } from "#test/test-host.js";
|
|
3
2
|
import { SourceFile } from "@alloy-js/typescript";
|
|
4
|
-
import type
|
|
5
|
-
import
|
|
6
|
-
import { beforeEach, describe, it } from "vitest";
|
|
3
|
+
import { t, type TesterInstance } from "@typespec/compiler/testing";
|
|
4
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
7
5
|
import { Output } from "../../../src/core/components/output.jsx";
|
|
8
6
|
import { FunctionExpression } from "../../../src/typescript/components/function-expression.jsx";
|
|
9
|
-
import { assertFileContents } from "../../utils.js";
|
|
10
|
-
import { createEmitterFrameworkTestRunner } from "../test-host.js";
|
|
11
7
|
|
|
12
8
|
describe("function expressions with a `type` prop", () => {
|
|
13
|
-
let runner:
|
|
9
|
+
let runner: TesterInstance;
|
|
14
10
|
|
|
15
11
|
beforeEach(async () => {
|
|
16
|
-
runner = await
|
|
12
|
+
runner = await Tester.createInstance();
|
|
17
13
|
});
|
|
18
14
|
|
|
19
15
|
it("creates a function", async () => {
|
|
20
|
-
const { getName } =
|
|
21
|
-
@test op getName(id: string): string;
|
|
22
|
-
`)
|
|
16
|
+
const { getName } = await runner.compile(t.code`
|
|
17
|
+
@test op ${t.op("getName")}(id: string): string;
|
|
18
|
+
`);
|
|
23
19
|
|
|
24
|
-
|
|
20
|
+
expect(
|
|
25
21
|
<Output program={runner.program}>
|
|
26
22
|
<SourceFile path="test.ts">
|
|
27
23
|
<FunctionExpression type={getName}>console.log("Hello!");</FunctionExpression>
|
|
28
24
|
</SourceFile>
|
|
29
25
|
</Output>,
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
assertFileContents(
|
|
33
|
-
res,
|
|
34
|
-
d`
|
|
26
|
+
).toRenderTo(`
|
|
35
27
|
function (id: string): string {
|
|
36
28
|
console.log("Hello!");
|
|
37
29
|
}
|
|
38
|
-
|
|
39
|
-
);
|
|
30
|
+
`);
|
|
40
31
|
});
|
|
41
32
|
|
|
42
33
|
it("creates an async function", async () => {
|
|
43
|
-
const { getName } =
|
|
44
|
-
@test op getName(id: string): string;
|
|
45
|
-
`)
|
|
34
|
+
const { getName } = await runner.compile(t.code`
|
|
35
|
+
@test op ${t.op("getName")}(id: string): string;
|
|
36
|
+
`);
|
|
46
37
|
|
|
47
|
-
|
|
38
|
+
expect(
|
|
48
39
|
<Output program={runner.program}>
|
|
49
40
|
<SourceFile path="test.ts">
|
|
50
41
|
<FunctionExpression async type={getName} />
|
|
51
42
|
</SourceFile>
|
|
52
43
|
</Output>,
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
assertFileContents(
|
|
56
|
-
res,
|
|
57
|
-
d`
|
|
44
|
+
).toRenderTo(`
|
|
58
45
|
async function (id: string): Promise<string> {}
|
|
59
|
-
|
|
60
|
-
);
|
|
46
|
+
`);
|
|
61
47
|
});
|
|
62
48
|
|
|
63
49
|
it("can append extra parameters with raw params provided", async () => {
|
|
64
|
-
const { getName } =
|
|
65
|
-
@test op getName(id: string): string;
|
|
66
|
-
`)
|
|
50
|
+
const { getName } = await runner.compile(t.code`
|
|
51
|
+
@test op ${t.op("getName")}(id: string): string;
|
|
52
|
+
`);
|
|
67
53
|
|
|
68
|
-
|
|
54
|
+
expect(
|
|
69
55
|
<Output program={runner.program}>
|
|
70
56
|
<SourceFile path="test.ts">
|
|
71
57
|
<FunctionExpression
|
|
@@ -74,13 +60,8 @@ describe("function expressions with a `type` prop", () => {
|
|
|
74
60
|
/>
|
|
75
61
|
</SourceFile>
|
|
76
62
|
</Output>,
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
assertFileContents(
|
|
80
|
-
res,
|
|
81
|
-
d`
|
|
63
|
+
).toRenderTo(`
|
|
82
64
|
function (additionalParam: number, id: string): string {}
|
|
83
|
-
|
|
84
|
-
);
|
|
65
|
+
`);
|
|
85
66
|
});
|
|
86
67
|
});
|
|
@@ -1,81 +1,63 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { d } from "@alloy-js/core/testing";
|
|
1
|
+
import { Tester } from "#test/test-host.js";
|
|
3
2
|
import { SourceFile } from "@alloy-js/typescript";
|
|
4
3
|
import type { Operation } from "@typespec/compiler";
|
|
5
|
-
import type
|
|
6
|
-
import { beforeEach, describe, it } from "vitest";
|
|
4
|
+
import { t, type TesterInstance } from "@typespec/compiler/testing";
|
|
5
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
7
6
|
import { Output } from "../../../src/core/components/output.jsx";
|
|
8
7
|
import { FunctionType } from "../../../src/typescript/index.js";
|
|
9
|
-
import { assertFileContents } from "../../utils.js";
|
|
10
|
-
import { createEmitterFrameworkTestRunner } from "../test-host.js";
|
|
11
8
|
|
|
12
9
|
describe("function types with a `type` prop", () => {
|
|
13
|
-
let runner:
|
|
10
|
+
let runner: TesterInstance;
|
|
14
11
|
|
|
15
12
|
beforeEach(async () => {
|
|
16
|
-
runner = await
|
|
13
|
+
runner = await Tester.createInstance();
|
|
17
14
|
});
|
|
18
15
|
|
|
19
16
|
it("creates a function type", async () => {
|
|
20
|
-
const { getName } =
|
|
21
|
-
@test op getName(id: string): string;
|
|
22
|
-
`)
|
|
17
|
+
const { getName } = await runner.compile(t.code`
|
|
18
|
+
@test op ${t.op("getName")}(id: string): string;
|
|
19
|
+
`);
|
|
23
20
|
|
|
24
|
-
|
|
21
|
+
expect(
|
|
25
22
|
<Output program={runner.program}>
|
|
26
23
|
<SourceFile path="test.ts">
|
|
27
24
|
<FunctionType type={getName} />
|
|
28
25
|
</SourceFile>
|
|
29
26
|
</Output>,
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
assertFileContents(
|
|
33
|
-
res,
|
|
34
|
-
d`
|
|
27
|
+
).toRenderTo(`
|
|
35
28
|
(id: string) => string
|
|
36
|
-
|
|
37
|
-
);
|
|
29
|
+
`);
|
|
38
30
|
});
|
|
39
31
|
|
|
40
32
|
it("creates an async function type", async () => {
|
|
41
|
-
const { getName } =
|
|
42
|
-
@test op getName(id: string): string;
|
|
43
|
-
`)
|
|
33
|
+
const { getName } = await runner.compile(t.code`
|
|
34
|
+
@test op ${t.op("getName")}(id: string): string;
|
|
35
|
+
`);
|
|
44
36
|
|
|
45
|
-
|
|
37
|
+
expect(
|
|
46
38
|
<Output program={runner.program}>
|
|
47
39
|
<SourceFile path="test.ts">
|
|
48
40
|
<FunctionType async type={getName} />
|
|
49
41
|
</SourceFile>
|
|
50
42
|
</Output>,
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
assertFileContents(
|
|
54
|
-
res,
|
|
55
|
-
d`
|
|
43
|
+
).toRenderTo(`
|
|
56
44
|
(id: string) => Promise<string>
|
|
57
|
-
|
|
58
|
-
);
|
|
45
|
+
`);
|
|
59
46
|
});
|
|
60
47
|
|
|
61
48
|
it("can append extra parameters with raw params provided", async () => {
|
|
62
|
-
const { getName } = (await runner.compile(`
|
|
63
|
-
@test op getName(id: string): string;
|
|
49
|
+
const { getName } = (await runner.compile(t.code`
|
|
50
|
+
@test op ${t.op("getName")}(id: string): string;
|
|
64
51
|
`)) as { getName: Operation };
|
|
65
52
|
|
|
66
|
-
|
|
53
|
+
expect(
|
|
67
54
|
<Output program={runner.program}>
|
|
68
55
|
<SourceFile path="test.ts">
|
|
69
56
|
<FunctionType type={getName} parameters={[{ name: "additionalParam", type: "number" }]} />
|
|
70
57
|
</SourceFile>
|
|
71
58
|
</Output>,
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
assertFileContents(
|
|
75
|
-
res,
|
|
76
|
-
d`
|
|
59
|
+
).toRenderTo(`
|
|
77
60
|
(additionalParam: number, id: string) => string
|
|
78
|
-
|
|
79
|
-
);
|
|
61
|
+
`);
|
|
80
62
|
});
|
|
81
63
|
});
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import { getProgram } from "#test/utils.js";
|
|
1
2
|
import { For, List } from "@alloy-js/core";
|
|
2
3
|
import { SourceFile } from "@alloy-js/typescript";
|
|
3
4
|
import type { Namespace } from "@typespec/compiler";
|
|
4
5
|
import { describe, expect, it } from "vitest";
|
|
5
6
|
import { Output } from "../../../src/core/components/output.jsx";
|
|
6
7
|
import { InterfaceDeclaration } from "../../../src/typescript/components/interface-declaration.js";
|
|
7
|
-
import { getProgram } from "../test-host.js";
|
|
8
8
|
|
|
9
9
|
describe("Typescript Interface", () => {
|
|
10
10
|
describe("Interface bound to Typespec Types", () => {
|