@typespec/emitter-framework 0.9.0-dev.0 → 0.9.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.
Files changed (192) hide show
  1. package/dist/src/core/components/output.d.ts +4 -4
  2. package/dist/src/core/components/output.d.ts.map +1 -1
  3. package/dist/src/core/components/output.js +3 -3
  4. package/dist/src/core/context/name-policy-context.d.ts +2 -2
  5. package/dist/src/core/context/name-policy-context.d.ts.map +1 -1
  6. package/dist/src/core/context/tsp-context.d.ts +2 -2
  7. package/dist/src/core/context/tsp-context.d.ts.map +1 -1
  8. package/dist/src/core/transport-name-policy.d.ts +1 -1
  9. package/dist/src/core/transport-name-policy.d.ts.map +1 -1
  10. package/dist/src/core/write-output.d.ts +2 -2
  11. package/dist/src/core/write-output.d.ts.map +1 -1
  12. package/dist/src/csharp/components/class/declaration.d.ts +13 -0
  13. package/dist/src/csharp/components/class/declaration.d.ts.map +1 -0
  14. package/dist/src/csharp/components/class/declaration.js +91 -0
  15. package/dist/src/csharp/components/class/declaration.test.d.ts +2 -0
  16. package/dist/src/csharp/components/class/declaration.test.d.ts.map +1 -0
  17. package/dist/src/csharp/components/class/declaration.test.js +377 -0
  18. package/dist/src/csharp/components/enum/declaration.d.ts +9 -0
  19. package/dist/src/csharp/components/enum/declaration.d.ts.map +1 -0
  20. package/dist/src/csharp/components/enum/declaration.js +55 -0
  21. package/dist/src/csharp/components/enum/declaration.test.d.ts +2 -0
  22. package/dist/src/csharp/components/enum/declaration.test.d.ts.map +1 -0
  23. package/dist/src/csharp/components/enum/declaration.test.js +309 -0
  24. package/dist/src/csharp/components/index.d.ts +5 -0
  25. package/dist/src/csharp/components/index.d.ts.map +1 -0
  26. package/dist/src/csharp/components/index.js +4 -0
  27. package/dist/src/csharp/components/property/property.d.ts +15 -0
  28. package/dist/src/csharp/components/property/property.d.ts.map +1 -0
  29. package/dist/src/csharp/components/property/property.js +85 -0
  30. package/dist/src/csharp/components/property/property.test.d.ts +2 -0
  31. package/dist/src/csharp/components/property/property.test.d.ts.map +1 -0
  32. package/dist/src/csharp/components/property/property.test.js +117 -0
  33. package/dist/src/csharp/components/type-expression.d.ts +11 -0
  34. package/dist/src/csharp/components/type-expression.d.ts.map +1 -0
  35. package/dist/src/csharp/components/type-expression.js +143 -0
  36. package/dist/src/csharp/components/type-expression.test.d.ts +2 -0
  37. package/dist/src/csharp/components/type-expression.test.d.ts.map +1 -0
  38. package/dist/src/csharp/components/type-expression.test.js +128 -0
  39. package/dist/src/csharp/components/utils/doc-comments.d.ts +14 -0
  40. package/dist/src/csharp/components/utils/doc-comments.d.ts.map +1 -0
  41. package/dist/src/csharp/components/utils/doc-comments.js +67 -0
  42. package/dist/src/csharp/components/utils/refkey.d.ts +23 -0
  43. package/dist/src/csharp/components/utils/refkey.d.ts.map +1 -0
  44. package/dist/src/csharp/components/utils/refkey.js +35 -0
  45. package/dist/src/csharp/index.d.ts +2 -0
  46. package/dist/src/csharp/index.d.ts.map +1 -0
  47. package/dist/src/csharp/index.js +1 -0
  48. package/dist/src/testing/index.d.ts +1 -1
  49. package/dist/src/testing/index.d.ts.map +1 -1
  50. package/dist/src/testing/scenario-test/code-block-expectation.d.ts +33 -0
  51. package/dist/src/testing/scenario-test/code-block-expectation.d.ts.map +1 -0
  52. package/dist/src/testing/scenario-test/code-block-expectation.js +69 -0
  53. package/dist/src/testing/scenario-test/code-block-expectation.test.d.ts +2 -0
  54. package/dist/src/testing/scenario-test/code-block-expectation.test.d.ts.map +1 -0
  55. package/dist/src/testing/scenario-test/code-block-expectation.test.js +80 -0
  56. package/dist/src/testing/scenario-test/harness.d.ts +3 -3
  57. package/dist/src/testing/scenario-test/harness.d.ts.map +1 -1
  58. package/dist/src/testing/scenario-test/harness.js +69 -158
  59. package/dist/src/testing/scenario-test/index.d.ts +0 -1
  60. package/dist/src/testing/scenario-test/index.d.ts.map +1 -1
  61. package/dist/src/testing/scenario-test/index.js +1 -2
  62. package/dist/src/testing/scenario-test/snippet-extractor.d.ts +1 -1
  63. package/dist/src/testing/scenario-test/snippet-extractor.js +1 -1
  64. package/dist/src/typescript/components/array-expression.d.ts +2 -3
  65. package/dist/src/typescript/components/array-expression.d.ts.map +1 -1
  66. package/dist/src/typescript/components/array-expression.js +2 -2
  67. package/dist/src/typescript/components/arrow-function.d.ts +2 -2
  68. package/dist/src/typescript/components/arrow-function.d.ts.map +1 -1
  69. package/dist/src/typescript/components/arrow-function.js +2 -1
  70. package/dist/src/typescript/components/class-method.d.ts +1 -1
  71. package/dist/src/typescript/components/class-method.d.ts.map +1 -1
  72. package/dist/src/typescript/components/enum-declaration.d.ts +6 -6
  73. package/dist/src/typescript/components/enum-declaration.d.ts.map +1 -1
  74. package/dist/src/typescript/components/enum-declaration.js +2 -2
  75. package/dist/src/typescript/components/function-declaration.d.ts +1 -1
  76. package/dist/src/typescript/components/function-declaration.d.ts.map +1 -1
  77. package/dist/src/typescript/components/function-expression.d.ts +2 -2
  78. package/dist/src/typescript/components/function-expression.d.ts.map +1 -1
  79. package/dist/src/typescript/components/function-expression.js +2 -1
  80. package/dist/src/typescript/components/function-type.d.ts +2 -2
  81. package/dist/src/typescript/components/function-type.d.ts.map +1 -1
  82. package/dist/src/typescript/components/function-type.js +2 -1
  83. package/dist/src/typescript/components/interface-declaration.d.ts +4 -4
  84. package/dist/src/typescript/components/interface-declaration.d.ts.map +1 -1
  85. package/dist/src/typescript/components/interface-declaration.js +3 -4
  86. package/dist/src/typescript/components/interface-member.d.ts +2 -2
  87. package/dist/src/typescript/components/interface-member.d.ts.map +1 -1
  88. package/dist/src/typescript/components/interface-method.d.ts +2 -2
  89. package/dist/src/typescript/components/interface-method.d.ts.map +1 -1
  90. package/dist/src/typescript/components/record-expression.d.ts +1 -1
  91. package/dist/src/typescript/components/record-expression.d.ts.map +1 -1
  92. package/dist/src/typescript/components/type-alias-declaration.d.ts +1 -1
  93. package/dist/src/typescript/components/type-alias-declaration.d.ts.map +1 -1
  94. package/dist/src/typescript/components/type-declaration.d.ts +1 -1
  95. package/dist/src/typescript/components/type-declaration.d.ts.map +1 -1
  96. package/dist/src/typescript/components/type-expression.d.ts +1 -1
  97. package/dist/src/typescript/components/type-expression.d.ts.map +1 -1
  98. package/dist/src/typescript/components/type-transform.d.ts +2 -2
  99. package/dist/src/typescript/components/type-transform.d.ts.map +1 -1
  100. package/dist/src/typescript/components/union-declaration.d.ts +2 -2
  101. package/dist/src/typescript/components/union-declaration.d.ts.map +1 -1
  102. package/dist/src/typescript/components/union-expression.d.ts +3 -4
  103. package/dist/src/typescript/components/union-expression.d.ts.map +1 -1
  104. package/dist/src/typescript/components/union-expression.js +3 -3
  105. package/dist/src/typescript/components/value-expression.d.ts +2 -2
  106. package/dist/src/typescript/components/value-expression.d.ts.map +1 -1
  107. package/dist/src/typescript/utils/operation.d.ts +2 -2
  108. package/dist/src/typescript/utils/operation.d.ts.map +1 -1
  109. package/dist/test/test-host.d.ts +2 -0
  110. package/dist/test/test-host.d.ts.map +1 -0
  111. package/dist/test/test-host.js +5 -0
  112. package/dist/test/testing/snippet-extractor-csharp.test.js +3 -3
  113. package/dist/test/testing/snippet-extractor-java.test.js +3 -3
  114. package/dist/test/testing/snippet-extractor-python.test.js +2 -2
  115. package/dist/test/testing/snippet-extractor-typescript.test.js +3 -3
  116. package/dist/test/typescript/components/member-expression.test.js +5 -5
  117. package/dist/test/typescript/test-host.d.ts +1 -1
  118. package/dist/test/typescript/test-host.d.ts.map +1 -1
  119. package/dist/test/utils.d.ts +2 -2
  120. package/dist/test/utils.d.ts.map +1 -1
  121. package/dist/test/vitest.setup.d.ts +2 -0
  122. package/dist/test/vitest.setup.d.ts.map +1 -0
  123. package/dist/test/vitest.setup.js +1 -0
  124. package/package.json +30 -8
  125. package/src/core/components/output.tsx +9 -5
  126. package/src/core/context/name-policy-context.ts +2 -2
  127. package/src/core/context/tsp-context.ts +2 -2
  128. package/src/core/transport-name-policy.ts +1 -1
  129. package/src/core/write-output.ts +2 -2
  130. package/src/csharp/components/class/declaration.test.tsx +339 -0
  131. package/src/csharp/components/class/declaration.tsx +86 -0
  132. package/src/csharp/components/enum/declaration.test.tsx +267 -0
  133. package/src/csharp/components/enum/declaration.tsx +56 -0
  134. package/src/csharp/components/index.ts +4 -0
  135. package/src/csharp/components/property/property.test.tsx +97 -0
  136. package/src/csharp/components/property/property.tsx +62 -0
  137. package/src/csharp/components/type-expression.test.tsx +133 -0
  138. package/src/csharp/components/type-expression.tsx +119 -0
  139. package/src/csharp/components/utils/doc-comments.tsx +58 -0
  140. package/src/csharp/components/utils/refkey.ts +36 -0
  141. package/src/csharp/index.ts +1 -0
  142. package/src/testing/index.ts +1 -1
  143. package/src/testing/scenario-test/code-block-expectation.test.ts +95 -0
  144. package/src/testing/scenario-test/code-block-expectation.ts +115 -0
  145. package/src/testing/scenario-test/harness.ts +92 -237
  146. package/src/testing/scenario-test/index.ts +0 -1
  147. package/src/testing/scenario-test/snippet-extractor.ts +1 -1
  148. package/src/typescript/components/array-expression.tsx +3 -3
  149. package/src/typescript/components/arrow-function.tsx +2 -2
  150. package/src/typescript/components/class-method.tsx +1 -1
  151. package/src/typescript/components/enum-declaration.tsx +6 -6
  152. package/src/typescript/components/function-declaration.tsx +1 -1
  153. package/src/typescript/components/function-expression.tsx +2 -2
  154. package/src/typescript/components/function-type.tsx +2 -2
  155. package/src/typescript/components/interface-declaration.tsx +11 -12
  156. package/src/typescript/components/interface-member.tsx +2 -2
  157. package/src/typescript/components/interface-method.tsx +2 -2
  158. package/src/typescript/components/record-expression.tsx +1 -1
  159. package/src/typescript/components/type-alias-declaration.tsx +1 -1
  160. package/src/typescript/components/type-declaration.tsx +1 -1
  161. package/src/typescript/components/type-expression.tsx +2 -2
  162. package/src/typescript/components/type-transform.tsx +3 -3
  163. package/src/typescript/components/union-declaration.tsx +2 -2
  164. package/src/typescript/components/union-expression.tsx +12 -7
  165. package/src/typescript/components/value-expression.tsx +2 -2
  166. package/src/typescript/utils/operation.ts +2 -2
  167. package/test/test-host.ts +4 -0
  168. package/test/testing/snippet-extractor-csharp.test.ts +4 -4
  169. package/test/testing/snippet-extractor-java.test.ts +4 -4
  170. package/test/testing/snippet-extractor-python.test.ts +3 -3
  171. package/test/testing/snippet-extractor-typescript.test.ts +4 -4
  172. package/test/typescript/components/arrow-function.test.tsx +2 -2
  173. package/test/typescript/components/enum-declaration.test.tsx +1 -1
  174. package/test/typescript/components/function-declaration.test.tsx +1 -1
  175. package/test/typescript/components/function-expression.test.tsx +2 -2
  176. package/test/typescript/components/function-type.test.tsx +2 -2
  177. package/test/typescript/components/interface-declaration.test.tsx +1 -1
  178. package/test/typescript/components/interface-method.test.tsx +2 -2
  179. package/test/typescript/components/member-expression.test.tsx +10 -10
  180. package/test/typescript/components/type-alias-declaration.test.tsx +1 -1
  181. package/test/typescript/components/type-transform.test.tsx +2 -2
  182. package/test/typescript/components/union-declaration.test.tsx +2 -2
  183. package/test/typescript/components/value-expression.test.tsx +6 -6
  184. package/test/typescript/test-host.ts +1 -1
  185. package/test/utils.ts +2 -2
  186. package/test/vitest.setup.ts +1 -0
  187. package/tsconfig.json +3 -1
  188. package/vitest.config.ts +2 -1
  189. package/dist/src/testing/scenario-test/test-host.d.ts +0 -8
  190. package/dist/src/testing/scenario-test/test-host.d.ts.map +0 -1
  191. package/dist/src/testing/scenario-test/test-host.js +0 -49
  192. package/src/testing/scenario-test/test-host.ts +0 -83
@@ -0,0 +1,309 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { Tester } from "#test/test-host.js";
3
+ import { createCSharpNamePolicy, Namespace, SourceFile } from "@alloy-js/csharp";
4
+ import { t } from "@typespec/compiler/testing";
5
+ import { beforeEach, expect, it } from "vitest";
6
+ import { Output } from "../../../core/index.js";
7
+ import { EnumDeclaration } from "../../index.js";
8
+ let runner;
9
+ beforeEach(async () => {
10
+ runner = await Tester.createInstance();
11
+ });
12
+ function Wrapper(props) {
13
+ const policy = createCSharpNamePolicy();
14
+ return _$createComponent(Output, {
15
+ get program() {
16
+ return runner.program;
17
+ },
18
+ namePolicy: policy,
19
+ get children() {
20
+ return _$createComponent(Namespace, {
21
+ name: "TestNamespace",
22
+ get children() {
23
+ return _$createComponent(SourceFile, {
24
+ path: "test.cs",
25
+ get children() {
26
+ return props.children;
27
+ }
28
+ });
29
+ }
30
+ });
31
+ }
32
+ });
33
+ }
34
+ it("renders a basic enum declaration", async () => {
35
+ const {
36
+ TestEnum
37
+ } = await runner.compile(t.code`
38
+ @test enum ${t.enum("TestEnum")} {
39
+ Value1;
40
+ Value2;
41
+ Value3;
42
+ }
43
+ `);
44
+ expect(_$createComponent(Wrapper, {
45
+ get children() {
46
+ return _$createComponent(EnumDeclaration, {
47
+ type: TestEnum
48
+ });
49
+ }
50
+ })).toRenderTo(`
51
+ namespace TestNamespace
52
+ {
53
+ enum TestEnum
54
+ {
55
+ Value1,
56
+ Value2,
57
+ Value3
58
+ }
59
+ }
60
+ `);
61
+ });
62
+ it("renders an empty enum declaration", async () => {
63
+ const {
64
+ TestEnum
65
+ } = await runner.compile(t.code`
66
+ @test enum ${t.enum("TestEnum")} {}
67
+ `);
68
+ expect(_$createComponent(Wrapper, {
69
+ get children() {
70
+ return _$createComponent(EnumDeclaration, {
71
+ type: TestEnum
72
+ });
73
+ }
74
+ })).toRenderTo(`
75
+ namespace TestNamespace
76
+ {
77
+ enum TestEnum
78
+ {
79
+
80
+ }
81
+ }
82
+ `);
83
+ });
84
+ it("can override enum name", async () => {
85
+ const {
86
+ TestEnum
87
+ } = await runner.compile(t.code`
88
+ @test enum ${t.enum("TestEnum")} {
89
+ Value1;
90
+ Value2;
91
+ }
92
+ `);
93
+ expect(_$createComponent(Wrapper, {
94
+ get children() {
95
+ return _$createComponent(EnumDeclaration, {
96
+ type: TestEnum,
97
+ name: "CustomEnumName"
98
+ });
99
+ }
100
+ })).toRenderTo(`
101
+ namespace TestNamespace
102
+ {
103
+ enum CustomEnumName
104
+ {
105
+ Value1,
106
+ Value2
107
+ }
108
+ }
109
+ `);
110
+ });
111
+ it("renders an enum with access modifiers", async () => {
112
+ const {
113
+ TestEnum
114
+ } = await runner.compile(t.code`
115
+ @test enum ${t.enum("TestEnum")} {
116
+ Value1;
117
+ Value2;
118
+ }
119
+ `);
120
+ expect(_$createComponent(Wrapper, {
121
+ get children() {
122
+ return _$createComponent(EnumDeclaration, {
123
+ type: TestEnum,
124
+ internal: true
125
+ });
126
+ }
127
+ })).toRenderTo(`
128
+ namespace TestNamespace
129
+ {
130
+ internal enum TestEnum
131
+ {
132
+ Value1,
133
+ Value2
134
+ }
135
+ }
136
+ `);
137
+ });
138
+ it("renders enum with C# naming conventions", async () => {
139
+ const {
140
+ TestEnum
141
+ } = await runner.compile(t.code`
142
+ @test enum ${t.enum("TestEnum")} {
143
+ value_one;
144
+ value_two;
145
+ value_three;
146
+ }
147
+ `);
148
+ expect(_$createComponent(Output, {
149
+ get program() {
150
+ return runner.program;
151
+ },
152
+ get namePolicy() {
153
+ return createCSharpNamePolicy();
154
+ },
155
+ get children() {
156
+ return _$createComponent(Namespace, {
157
+ name: "TestNamespace",
158
+ get children() {
159
+ return _$createComponent(SourceFile, {
160
+ path: "test.cs",
161
+ get children() {
162
+ return _$createComponent(EnumDeclaration, {
163
+ type: TestEnum
164
+ });
165
+ }
166
+ });
167
+ }
168
+ });
169
+ }
170
+ })).toRenderTo(`
171
+ namespace TestNamespace
172
+ {
173
+ enum TestEnum
174
+ {
175
+ ValueOne,
176
+ ValueTwo,
177
+ ValueThree
178
+ }
179
+ }
180
+ `);
181
+ });
182
+ it("renders enum with single value", async () => {
183
+ const {
184
+ TestEnum
185
+ } = await runner.compile(t.code`
186
+ @test enum ${t.enum("TestEnum")} {
187
+ OnlyValue;
188
+ }
189
+ `);
190
+ expect(_$createComponent(Wrapper, {
191
+ get children() {
192
+ return _$createComponent(EnumDeclaration, {
193
+ type: TestEnum
194
+ });
195
+ }
196
+ })).toRenderTo(`
197
+ namespace TestNamespace
198
+ {
199
+ enum TestEnum
200
+ {
201
+ OnlyValue
202
+ }
203
+ }
204
+ `);
205
+ });
206
+ it("renders enum with numeric-like member names", async () => {
207
+ const {
208
+ TestEnum
209
+ } = await runner.compile(t.code`
210
+ @test enum ${t.enum("TestEnum")} {
211
+ Value0;
212
+ Value1;
213
+ Value10;
214
+ Value100;
215
+ }
216
+ `);
217
+ expect(_$createComponent(Wrapper, {
218
+ get children() {
219
+ return _$createComponent(EnumDeclaration, {
220
+ type: TestEnum
221
+ });
222
+ }
223
+ })).toRenderTo(`
224
+ namespace TestNamespace
225
+ {
226
+ enum TestEnum
227
+ {
228
+ Value0,
229
+ Value1,
230
+ Value10,
231
+ Value100
232
+ }
233
+ }
234
+ `);
235
+ });
236
+ it("renders multiple enums in the same namespace", async () => {
237
+ const {
238
+ TestEnum1,
239
+ TestEnum2
240
+ } = await runner.compile(t.code`
241
+ @test enum ${t.enum("TestEnum1")} {
242
+ Value1;
243
+ Value2;
244
+ }
245
+ @test enum ${t.enum("TestEnum2")} {
246
+ OptionA;
247
+ OptionB;
248
+ OptionC;
249
+ }
250
+ `);
251
+ expect(_$createComponent(Wrapper, {
252
+ get children() {
253
+ return [_$createComponent(EnumDeclaration, {
254
+ type: TestEnum1
255
+ }), _$createIntrinsic("hbr", {}), _$createComponent(EnumDeclaration, {
256
+ type: TestEnum2
257
+ })];
258
+ }
259
+ })).toRenderTo(`
260
+ namespace TestNamespace
261
+ {
262
+ enum TestEnum1
263
+ {
264
+ Value1,
265
+ Value2
266
+ }
267
+ enum TestEnum2
268
+ {
269
+ OptionA,
270
+ OptionB,
271
+ OptionC
272
+ }
273
+ }
274
+ `);
275
+ });
276
+ it("renders an enum with doc comments", async () => {
277
+ const {
278
+ TestEnum
279
+ } = await runner.compile(t.code`
280
+ @test enum ${t.enum("TestEnum")} {
281
+ @doc("This is value one")
282
+ Value1;
283
+ /** This is value two */
284
+ Value2;
285
+ }
286
+ `);
287
+ expect(_$createComponent(Wrapper, {
288
+ get children() {
289
+ return _$createComponent(EnumDeclaration, {
290
+ type: TestEnum
291
+ });
292
+ }
293
+ })).toRenderTo(`
294
+ namespace TestNamespace
295
+ {
296
+ enum TestEnum
297
+ {
298
+ /// <summary>
299
+ /// This is value one
300
+ /// </summary>
301
+ Value1,
302
+ /// <summary>
303
+ /// This is value two
304
+ /// </summary>
305
+ Value2
306
+ }
307
+ }
308
+ `);
309
+ });
@@ -0,0 +1,5 @@
1
+ export * from "./class/declaration.js";
2
+ export * from "./enum/declaration.jsx";
3
+ export * from "./property/property.jsx";
4
+ export * from "./type-expression.jsx";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/csharp/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from "./class/declaration.js";
2
+ export * from "./enum/declaration.js";
3
+ export * from "./property/property.js";
4
+ export * from "./type-expression.js";
@@ -0,0 +1,15 @@
1
+ import { type Children } from "@alloy-js/core";
2
+ import { type ModelProperty } from "@typespec/compiler";
3
+ export interface PropertyProps {
4
+ type: ModelProperty;
5
+ /** If set the property will add the json serialization attributes(using System.Text.Json). */
6
+ jsonAttributes?: boolean;
7
+ }
8
+ /**
9
+ * Create a C# property declaration from a TypeSpec property type.
10
+ */
11
+ export declare function Property(props: PropertyProps): Children;
12
+ export interface JsonNameAttributeProps {
13
+ type: ModelProperty;
14
+ }
15
+ //# sourceMappingURL=property.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"property.d.ts","sourceRoot":"","sources":["../../../../../src/csharp/components/property/property.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAG/C,OAAO,EAAE,KAAK,aAAa,EAAiC,MAAM,oBAAoB,CAAC;AAKvF,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,aAAa,CAAC;IACpB,8FAA8F;IAC9F,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,aAAa,GAAG,QAAQ,CAiBvD;AAED,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,aAAa,CAAC;CACrB"}
@@ -0,0 +1,85 @@
1
+ import { memo as _$memo, createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import * as cs from "@alloy-js/csharp";
3
+ import { Attribute } from "@alloy-js/csharp";
4
+ import { resolveEncodedName } from "@typespec/compiler";
5
+ import { useTsp } from "../../../core/index.js";
6
+ import { TypeExpression } from "../type-expression.js";
7
+ import { getDocComments } from "../utils/doc-comments.js";
8
+ /**
9
+ * Create a C# property declaration from a TypeSpec property type.
10
+ */
11
+ export function Property(props) {
12
+ const result = preprocessPropertyType(props.type.type);
13
+ const {
14
+ $
15
+ } = useTsp();
16
+ return _$createComponent(cs.Property, {
17
+ get name() {
18
+ return props.type.name;
19
+ },
20
+ get type() {
21
+ return _$createComponent(TypeExpression, {
22
+ get type() {
23
+ return result.type;
24
+ }
25
+ });
26
+ },
27
+ "public": true,
28
+ get required() {
29
+ return !props.type.optional;
30
+ },
31
+ get nullable() {
32
+ return result.nullable;
33
+ },
34
+ get doc() {
35
+ return getDocComments($, props.type);
36
+ },
37
+ get attributes() {
38
+ return _$memo(() => !!props.jsonAttributes)() ? [_$createComponent(JsonNameAttribute, {
39
+ get type() {
40
+ return props.type;
41
+ }
42
+ })] : undefined;
43
+ },
44
+ get: true,
45
+ set: true
46
+ });
47
+ }
48
+ function JsonNameAttribute(props) {
49
+ const {
50
+ program
51
+ } = useTsp();
52
+ const jsonName = resolveEncodedName(program, props.type, "application/json");
53
+ return _$createComponent(Attribute, {
54
+ name: "System.Text.Json.JsonPropertyName",
55
+ get args() {
56
+ return [JSON.stringify(jsonName)];
57
+ }
58
+ });
59
+ }
60
+ function preprocessPropertyType(type) {
61
+ const {
62
+ $
63
+ } = useTsp();
64
+ if (type.kind === "Union") {
65
+ const variants = type.variants;
66
+ const nonNullVariant = [...variants.values()].find(v => v.type !== $.intrinsic.null);
67
+ const nullVariant = [...variants.values()].find(v => v.type !== $.intrinsic.null);
68
+ if (nonNullVariant && nullVariant && variants.size === 2) {
69
+ return {
70
+ type: nonNullVariant.type,
71
+ nullable: true
72
+ };
73
+ } else {
74
+ return {
75
+ type,
76
+ nullable: false
77
+ };
78
+ }
79
+ } else {
80
+ return {
81
+ type,
82
+ nullable: false
83
+ };
84
+ }
85
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=property.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"property.test.d.ts","sourceRoot":"","sources":["../../../../../src/csharp/components/property/property.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,117 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { Tester } from "#test/test-host.js";
3
+ import { ClassDeclaration, createCSharpNamePolicy, Namespace, SourceFile } from "@alloy-js/csharp";
4
+ import { t } from "@typespec/compiler/testing";
5
+ import { beforeEach, describe, expect, it } from "vitest";
6
+ import { Output } from "../../../core/components/output.js";
7
+ import { Property } from "./property.js";
8
+ let tester;
9
+ beforeEach(async () => {
10
+ tester = await Tester.createInstance();
11
+ });
12
+ function Wrapper(props) {
13
+ const policy = createCSharpNamePolicy();
14
+ return _$createComponent(Output, {
15
+ get program() {
16
+ return tester.program;
17
+ },
18
+ namePolicy: policy,
19
+ get children() {
20
+ return _$createComponent(Namespace, {
21
+ name: "TestNamespace",
22
+ get children() {
23
+ return _$createComponent(SourceFile, {
24
+ path: "test.cs",
25
+ get children() {
26
+ return _$createComponent(ClassDeclaration, {
27
+ name: "Test",
28
+ get children() {
29
+ return props.children;
30
+ }
31
+ });
32
+ }
33
+ });
34
+ }
35
+ });
36
+ }
37
+ });
38
+ }
39
+ it("maps prop: string | null to nullable property", async () => {
40
+ const {
41
+ prop1
42
+ } = await tester.compile(t.code`
43
+ model TestModel {
44
+ ${t.modelProperty("prop1")}: string | null;
45
+ }
46
+ `);
47
+ expect(_$createComponent(Wrapper, {
48
+ get children() {
49
+ return _$createComponent(Property, {
50
+ type: prop1
51
+ });
52
+ }
53
+ })).toRenderTo(`
54
+ namespace TestNamespace
55
+ {
56
+ class Test
57
+ {
58
+ public required string? Prop1 { get; set; }
59
+ }
60
+ }
61
+ `);
62
+ });
63
+ describe("jsonAttributes", () => {
64
+ it("adds [JsonNameAttribute]", async () => {
65
+ const {
66
+ prop1
67
+ } = await tester.compile(t.code`
68
+ model TestModel {
69
+ ${t.modelProperty("prop1")}: string;
70
+ }
71
+ `);
72
+ expect(_$createComponent(Wrapper, {
73
+ get children() {
74
+ return _$createComponent(Property, {
75
+ type: prop1,
76
+ jsonAttributes: true
77
+ });
78
+ }
79
+ })).toRenderTo(`
80
+ namespace TestNamespace
81
+ {
82
+ class Test
83
+ {
84
+ [System.Text.Json.JsonPropertyName("prop1")]
85
+ public required string Prop1 { get; set; }
86
+ }
87
+ }
88
+ `);
89
+ });
90
+ it("adds [JsonNameAttribute] respecting encodedName", async () => {
91
+ const {
92
+ prop1
93
+ } = await tester.compile(t.code`
94
+ model TestModel {
95
+ @encodedName("application/json", "prop_1")
96
+ ${t.modelProperty("prop1")}: string;
97
+ }
98
+ `);
99
+ expect(_$createComponent(Wrapper, {
100
+ get children() {
101
+ return _$createComponent(Property, {
102
+ type: prop1,
103
+ jsonAttributes: true
104
+ });
105
+ }
106
+ })).toRenderTo(`
107
+ namespace TestNamespace
108
+ {
109
+ class Test
110
+ {
111
+ [System.Text.Json.JsonPropertyName("prop_1")]
112
+ public required string Prop1 { get; set; }
113
+ }
114
+ }
115
+ `);
116
+ });
117
+ });
@@ -0,0 +1,11 @@
1
+ import { type Children } from "@alloy-js/core";
2
+ import { type IntrinsicType, type Scalar, type Type } from "@typespec/compiler";
3
+ import type { Typekit } from "@typespec/compiler/typekit";
4
+ export interface TypeExpressionProps {
5
+ type: Type;
6
+ }
7
+ export declare function TypeExpression(props: TypeExpressionProps): Children;
8
+ declare const intrinsicNameToCSharpType: Map<string, string | null>;
9
+ export declare function getScalarIntrinsicExpression($: Typekit, type: Scalar | IntrinsicType): string | null;
10
+ export { intrinsicNameToCSharpType };
11
+ //# sourceMappingURL=type-expression.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-expression.d.ts","sourceRoot":"","sources":["../../../../src/csharp/components/type-expression.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAQ,MAAM,gBAAgB,CAAC;AAErD,OAAO,EAAe,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,KAAK,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC7F,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAK1D,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,IAAI,CAAC;CACZ;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,mBAAmB,GAAG,QAAQ,CAgBnE;AAED,QAAA,MAAM,yBAAyB,4BAqC7B,CAAC;AAEH,wBAAgB,4BAA4B,CAC1C,CAAC,EAAE,OAAO,EACV,IAAI,EAAE,MAAM,GAAG,aAAa,GAC3B,MAAM,GAAG,IAAI,CAoBf;AA0BD,OAAO,EAAE,yBAAyB,EAAE,CAAC"}