@alloy-js/csharp 0.18.0-dev.9 → 0.19.0-dev.0
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 +31 -0
- package/dist/src/components/ClassDeclaration.d.ts +39 -2
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.js +35 -34
- package/dist/src/components/ClassMethod.d.ts +33 -1
- package/dist/src/components/ClassMethod.d.ts.map +1 -1
- package/dist/src/components/ClassMethod.js +22 -7
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +1 -0
- package/dist/src/components/attributes/attributes.d.ts +39 -0
- package/dist/src/components/attributes/attributes.d.ts.map +1 -0
- package/dist/src/components/attributes/attributes.js +62 -0
- package/dist/src/components/attributes/attributes.test.d.ts +2 -0
- package/dist/src/components/attributes/attributes.test.d.ts.map +1 -0
- package/dist/src/components/attributes/attributes.test.js +75 -0
- package/dist/src/components/index.d.ts +7 -1
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +8 -2
- package/dist/src/components/interface/declaration.d.ts +32 -1
- package/dist/src/components/interface/declaration.d.ts.map +1 -1
- package/dist/src/components/interface/declaration.js +18 -25
- package/dist/src/components/interface/declaration.test.js +102 -0
- package/dist/src/components/interface/method.d.ts +33 -1
- package/dist/src/components/interface/method.d.ts.map +1 -1
- package/dist/src/components/interface/method.js +22 -7
- package/dist/src/components/interface/method.test.js +169 -0
- package/dist/src/components/interface/property.d.ts +36 -1
- package/dist/src/components/interface/property.d.ts.map +1 -1
- package/dist/src/components/interface/property.js +18 -4
- package/dist/src/components/interface/property.test.js +24 -0
- package/dist/src/components/parameters/parameters.d.ts +19 -0
- package/dist/src/components/parameters/parameters.d.ts.map +1 -0
- package/dist/src/components/parameters/parameters.js +43 -0
- package/dist/src/components/property/property.d.ts +80 -0
- package/dist/src/components/property/property.d.ts.map +1 -0
- package/dist/src/components/property/property.js +76 -0
- package/dist/src/components/property/property.test.d.ts +2 -0
- package/dist/src/components/property/property.test.d.ts.map +1 -0
- package/dist/src/components/property/property.test.js +242 -0
- package/dist/src/components/record/declaration.d.ts +35 -0
- package/dist/src/components/record/declaration.d.ts.map +1 -0
- package/dist/src/components/record/declaration.js +90 -0
- package/dist/src/components/record/declaration.test.d.ts +2 -0
- package/dist/src/components/record/declaration.test.d.ts.map +1 -0
- package/dist/src/components/record/declaration.test.js +94 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.d.ts +8 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.d.ts.map +1 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.js +44 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts +2 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts.map +1 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.test.js +67 -0
- package/dist/src/components/type-parameters/type-parameter.d.ts +20 -0
- package/dist/src/components/type-parameters/type-parameter.d.ts.map +1 -0
- package/dist/src/components/type-parameters/type-parameter.js +22 -0
- package/dist/src/components/type-parameters/type-parameters.d.ts +17 -0
- package/dist/src/components/type-parameters/type-parameters.d.ts.map +1 -0
- package/dist/src/components/type-parameters/type-parameters.js +54 -0
- package/dist/src/components/type-parameters/type-parameters.test.d.ts +2 -0
- package/dist/src/components/type-parameters/type-parameters.test.d.ts.map +1 -0
- package/dist/src/components/type-parameters/type-parameters.test.js +48 -0
- package/dist/src/components/var/declaration.d.ts +35 -0
- package/dist/src/components/var/declaration.d.ts.map +1 -0
- package/dist/src/components/var/declaration.js +40 -0
- package/dist/src/components/var/declaration.test.d.ts +2 -0
- package/dist/src/components/var/declaration.test.d.ts.map +1 -0
- package/dist/src/components/var/declaration.test.js +73 -0
- package/dist/src/name-policy.d.ts +1 -1
- package/dist/src/name-policy.d.ts.map +1 -1
- package/dist/src/name-policy.js +1 -0
- package/dist/test/class-declaration.test.d.ts +2 -0
- package/dist/test/class-declaration.test.d.ts.map +1 -0
- package/dist/test/{class.test.js → class-declaration.test.js} +161 -33
- package/dist/test/project-directory.test.d.ts +2 -0
- package/dist/test/project-directory.test.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/components/ClassDeclaration.tsx +65 -33
- package/src/components/ClassMethod.tsx +46 -4
- package/src/components/SourceFile.tsx +1 -0
- package/src/components/attributes/attributes.test.tsx +61 -0
- package/src/components/attributes/attributes.tsx +100 -0
- package/src/components/index.ts +7 -1
- package/src/components/interface/declaration.test.tsx +102 -0
- package/src/components/interface/declaration.tsx +43 -27
- package/src/components/interface/method.test.tsx +173 -0
- package/src/components/interface/method.tsx +45 -5
- package/src/components/interface/property.test.tsx +21 -0
- package/src/components/interface/property.tsx +43 -6
- package/src/components/parameters/parameters.tsx +74 -0
- package/src/components/property/property.test.tsx +209 -0
- package/src/components/property/property.tsx +172 -0
- package/src/components/record/declaration.test.tsx +73 -0
- package/src/components/record/declaration.tsx +109 -0
- package/src/components/type-parameters/type-parameter-constraints.test.tsx +93 -0
- package/src/components/type-parameters/type-parameter-constraints.tsx +46 -0
- package/src/components/type-parameters/type-parameter.tsx +35 -0
- package/src/components/type-parameters/type-parameters.test.tsx +46 -0
- package/src/components/type-parameters/type-parameters.tsx +63 -0
- package/src/components/var/declaration.test.tsx +59 -0
- package/src/components/var/declaration.tsx +47 -0
- package/src/name-policy.ts +3 -0
- package/temp/api.json +4722 -2384
- package/test/{class.test.tsx → class-declaration.test.tsx} +144 -26
- package/dist/src/components/Parameters.d.ts +0 -13
- package/dist/src/components/Parameters.d.ts.map +0 -1
- package/dist/src/components/Parameters.js +0 -34
- package/dist/test/class.test.d.ts +0 -2
- package/dist/test/class.test.d.ts.map +0 -1
- package/dist/test/projectdirectory.test.d.ts +0 -2
- package/dist/test/projectdirectory.test.d.ts.map +0 -1
- package/src/components/Parameters.tsx +0 -51
- /package/dist/test/{projectdirectory.test.js → project-directory.test.js} +0 -0
- /package/test/{projectdirectory.test.tsx → project-directory.test.tsx} +0 -0
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import { refkey } from "@alloy-js/core";
|
|
1
2
|
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
2
3
|
import { describe, expect, it } from "vitest";
|
|
3
4
|
import { TestNamespace } from "../../../test/utils.jsx";
|
|
5
|
+
import { Attribute } from "../attributes/attributes.jsx";
|
|
6
|
+
import { SourceFile } from "../SourceFile.jsx";
|
|
7
|
+
import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
|
|
4
8
|
import { InterfaceDeclaration } from "./declaration.jsx";
|
|
5
9
|
import { InterfaceMethod } from "./method.jsx";
|
|
6
10
|
|
|
@@ -103,6 +107,58 @@ it("defines params and return type", () => {
|
|
|
103
107
|
`);
|
|
104
108
|
});
|
|
105
109
|
|
|
110
|
+
it("defines optional param", () => {
|
|
111
|
+
const res = (
|
|
112
|
+
<Wrapper>
|
|
113
|
+
<InterfaceMethod
|
|
114
|
+
public
|
|
115
|
+
name="MethodOne"
|
|
116
|
+
parameters={[
|
|
117
|
+
{
|
|
118
|
+
name: "intParam",
|
|
119
|
+
type: "int",
|
|
120
|
+
optional: true,
|
|
121
|
+
},
|
|
122
|
+
]}
|
|
123
|
+
returns="string"
|
|
124
|
+
/>
|
|
125
|
+
</Wrapper>
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
expect(res).toRenderTo(`
|
|
129
|
+
public interface TestInterface
|
|
130
|
+
{
|
|
131
|
+
public string MethodOne(int? intParam);
|
|
132
|
+
}
|
|
133
|
+
`);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("defines optional param with default", () => {
|
|
137
|
+
const res = (
|
|
138
|
+
<Wrapper>
|
|
139
|
+
<InterfaceMethod
|
|
140
|
+
public
|
|
141
|
+
name="MethodOne"
|
|
142
|
+
parameters={[
|
|
143
|
+
{
|
|
144
|
+
name: "intParam",
|
|
145
|
+
type: "int",
|
|
146
|
+
default: 12,
|
|
147
|
+
},
|
|
148
|
+
]}
|
|
149
|
+
returns="string"
|
|
150
|
+
/>
|
|
151
|
+
</Wrapper>
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
expect(res).toRenderTo(`
|
|
155
|
+
public interface TestInterface
|
|
156
|
+
{
|
|
157
|
+
public string MethodOne(int intParam = 12);
|
|
158
|
+
}
|
|
159
|
+
`);
|
|
160
|
+
});
|
|
161
|
+
|
|
106
162
|
it("specify doc comment", () => {
|
|
107
163
|
expect(
|
|
108
164
|
<TestNamespace>
|
|
@@ -118,3 +174,120 @@ it("specify doc comment", () => {
|
|
|
118
174
|
}
|
|
119
175
|
`);
|
|
120
176
|
});
|
|
177
|
+
|
|
178
|
+
it("specify attributes", () => {
|
|
179
|
+
expect(
|
|
180
|
+
<Wrapper>
|
|
181
|
+
<InterfaceMethod name="Test" attributes={[<Attribute name="Test" />]} />
|
|
182
|
+
</Wrapper>,
|
|
183
|
+
).toRenderTo(`
|
|
184
|
+
public interface TestInterface
|
|
185
|
+
{
|
|
186
|
+
[Test]
|
|
187
|
+
void Test();
|
|
188
|
+
}
|
|
189
|
+
`);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe("with type parameters", () => {
|
|
193
|
+
it("reference parameters", () => {
|
|
194
|
+
const typeParameters: TypeParameterProps[] = [
|
|
195
|
+
{
|
|
196
|
+
name: "T",
|
|
197
|
+
refkey: refkey(),
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: "U",
|
|
201
|
+
refkey: refkey(),
|
|
202
|
+
},
|
|
203
|
+
];
|
|
204
|
+
|
|
205
|
+
expect(
|
|
206
|
+
<TestNamespace>
|
|
207
|
+
<SourceFile path="TestFile.cs">
|
|
208
|
+
<InterfaceDeclaration public name="TestInterface">
|
|
209
|
+
<InterfaceMethod
|
|
210
|
+
name="Test"
|
|
211
|
+
public
|
|
212
|
+
typeParameters={typeParameters}
|
|
213
|
+
parameters={[
|
|
214
|
+
{
|
|
215
|
+
name: "paramA",
|
|
216
|
+
type: typeParameters[0].refkey,
|
|
217
|
+
},
|
|
218
|
+
]}
|
|
219
|
+
returns={typeParameters[0].refkey}
|
|
220
|
+
/>
|
|
221
|
+
</InterfaceDeclaration>
|
|
222
|
+
</SourceFile>
|
|
223
|
+
</TestNamespace>,
|
|
224
|
+
).toRenderTo(`
|
|
225
|
+
namespace TestCode
|
|
226
|
+
{
|
|
227
|
+
public interface TestInterface
|
|
228
|
+
{
|
|
229
|
+
public T Test<T, U>(T paramA);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
`);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("defines with constraints", () => {
|
|
236
|
+
const typeParameters: TypeParameterProps[] = [
|
|
237
|
+
{
|
|
238
|
+
name: "T",
|
|
239
|
+
constraints: "IFoo",
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
name: "U",
|
|
243
|
+
constraints: "IBar",
|
|
244
|
+
},
|
|
245
|
+
];
|
|
246
|
+
|
|
247
|
+
expect(
|
|
248
|
+
<Wrapper>
|
|
249
|
+
<InterfaceMethod public name="Test" typeParameters={typeParameters}>
|
|
250
|
+
// Body
|
|
251
|
+
</InterfaceMethod>
|
|
252
|
+
</Wrapper>,
|
|
253
|
+
).toRenderTo(`
|
|
254
|
+
public interface TestInterface
|
|
255
|
+
{
|
|
256
|
+
public void Test<T, U>()
|
|
257
|
+
where T : IFoo
|
|
258
|
+
where U : IBar
|
|
259
|
+
{
|
|
260
|
+
// Body
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
`);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
describe("formatting", () => {
|
|
268
|
+
it("Split parameters before type parameters", () => {
|
|
269
|
+
expect(
|
|
270
|
+
<Wrapper>
|
|
271
|
+
<InterfaceMethod
|
|
272
|
+
public
|
|
273
|
+
name="Handle"
|
|
274
|
+
parameters={[
|
|
275
|
+
{
|
|
276
|
+
name: "message",
|
|
277
|
+
type: "Some.Quite.Long.Type.That.Will.Split",
|
|
278
|
+
},
|
|
279
|
+
]}
|
|
280
|
+
typeParameters={["T"]}
|
|
281
|
+
returns="Some.Quite.Long.Type.That.Will.Split"
|
|
282
|
+
/>
|
|
283
|
+
</Wrapper>,
|
|
284
|
+
).toRenderTo(`
|
|
285
|
+
public interface TestInterface
|
|
286
|
+
{
|
|
287
|
+
public Some.Quite.Long.Type.That.Will.Split Handle<T>(
|
|
288
|
+
Some.Quite.Long.Type.That.Will.Split message
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
`);
|
|
292
|
+
});
|
|
293
|
+
});
|
|
@@ -15,8 +15,12 @@ import {
|
|
|
15
15
|
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
16
16
|
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
17
17
|
import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
|
|
18
|
-
import {
|
|
18
|
+
import { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
|
|
19
19
|
import { DocWhen } from "../doc/comment.jsx";
|
|
20
|
+
import { ParameterProps, Parameters } from "../parameters/parameters.jsx";
|
|
21
|
+
import { TypeParameterConstraints } from "../type-parameters/type-parameter-constraints.jsx";
|
|
22
|
+
import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
|
|
23
|
+
import { TypeParameters } from "../type-parameters/type-parameters.jsx";
|
|
20
24
|
|
|
21
25
|
/** Method modifiers. Can only be one. */
|
|
22
26
|
export interface InterfaceMethodModifiers {
|
|
@@ -33,10 +37,41 @@ export interface InterfaceMethodProps
|
|
|
33
37
|
refkey?: Refkey;
|
|
34
38
|
children?: Children;
|
|
35
39
|
parameters?: Array<ParameterProps>;
|
|
40
|
+
/**
|
|
41
|
+
* Type parameters for the method
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```tsx
|
|
45
|
+
* <InterfaceMethod name="Test" typeParameters={["T"]} />
|
|
46
|
+
* ```
|
|
47
|
+
* This will produce:
|
|
48
|
+
* ```csharp
|
|
49
|
+
* public void Test<T>()
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
typeParameters?: (TypeParameterProps | string)[];
|
|
36
53
|
returns?: Children;
|
|
37
54
|
|
|
38
55
|
/** Doc comment */
|
|
39
56
|
doc?: Children;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Define attributes to attach
|
|
60
|
+
* @example
|
|
61
|
+
* ```tsx
|
|
62
|
+
* <InterfaceMethod name="MyMethod" attributes={[
|
|
63
|
+
* <Attribute name="Test" />
|
|
64
|
+
* <Attribute name="Test2" args={["arg1", "arg2"]} />
|
|
65
|
+
* ]} />
|
|
66
|
+
* ```
|
|
67
|
+
* This will produce:
|
|
68
|
+
* ```csharp
|
|
69
|
+
* [Test]
|
|
70
|
+
* [Test2("arg1", "arg2")]
|
|
71
|
+
* void MyMethod();
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
attributes?: AttributesProp;
|
|
40
75
|
}
|
|
41
76
|
|
|
42
77
|
// a C# interface method
|
|
@@ -59,9 +94,6 @@ export function InterfaceMethod(props: InterfaceMethodProps) {
|
|
|
59
94
|
owner: methodSymbol,
|
|
60
95
|
});
|
|
61
96
|
|
|
62
|
-
const params =
|
|
63
|
-
props.parameters ? <Parameters parameters={props.parameters} /> : "";
|
|
64
|
-
|
|
65
97
|
const modifiers = computeModifiersPrefix([
|
|
66
98
|
getAccessModifier(props),
|
|
67
99
|
getMethodModifier(props),
|
|
@@ -71,8 +103,16 @@ export function InterfaceMethod(props: InterfaceMethodProps) {
|
|
|
71
103
|
<MemberDeclaration symbol={methodSymbol}>
|
|
72
104
|
<Scope value={methodScope}>
|
|
73
105
|
<DocWhen doc={props.doc} />
|
|
106
|
+
<AttributeList attributes={props.attributes} endline />
|
|
74
107
|
{modifiers}
|
|
75
|
-
{props.returns ?? "void"} {name}
|
|
108
|
+
{props.returns ?? "void"} {name}
|
|
109
|
+
{props.typeParameters && (
|
|
110
|
+
<TypeParameters parameters={props.typeParameters} />
|
|
111
|
+
)}
|
|
112
|
+
<Parameters parameters={props.parameters} />
|
|
113
|
+
{props.typeParameters && (
|
|
114
|
+
<TypeParameterConstraints parameters={props.typeParameters} />
|
|
115
|
+
)}
|
|
76
116
|
{props.children ?
|
|
77
117
|
<Block newline>{props.children}</Block>
|
|
78
118
|
: ";"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
2
2
|
import { describe, expect, it } from "vitest";
|
|
3
3
|
import { TestNamespace } from "../../../test/utils.jsx";
|
|
4
|
+
import { Attribute } from "../attributes/attributes.jsx";
|
|
4
5
|
import { InterfaceDeclaration } from "./declaration.jsx";
|
|
5
6
|
import { InterfaceProperty } from "./property.jsx";
|
|
6
7
|
|
|
@@ -142,3 +143,23 @@ it("specify doc comment", () => {
|
|
|
142
143
|
}
|
|
143
144
|
`);
|
|
144
145
|
});
|
|
146
|
+
|
|
147
|
+
it("specify attributes", () => {
|
|
148
|
+
expect(
|
|
149
|
+
<Wrapper>
|
|
150
|
+
<InterfaceProperty
|
|
151
|
+
name="Test"
|
|
152
|
+
type="int"
|
|
153
|
+
attributes={[<Attribute name="Test" />]}
|
|
154
|
+
get
|
|
155
|
+
set
|
|
156
|
+
/>
|
|
157
|
+
</Wrapper>,
|
|
158
|
+
).toRenderTo(`
|
|
159
|
+
public interface TestInterface
|
|
160
|
+
{
|
|
161
|
+
[Test]
|
|
162
|
+
int Test { get; set; }
|
|
163
|
+
}
|
|
164
|
+
`);
|
|
165
|
+
});
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
17
17
|
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
18
18
|
import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
|
|
19
|
+
import { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
|
|
19
20
|
import { DocWhen } from "../doc/comment.jsx";
|
|
20
21
|
|
|
21
22
|
/** Method modifiers. Can only be one. */
|
|
@@ -23,9 +24,7 @@ export interface InterfacePropertyModifiers {
|
|
|
23
24
|
readonly new?: boolean;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
"new",
|
|
28
|
-
]);
|
|
27
|
+
const getModifiers = makeModifiers<InterfacePropertyModifiers>(["new"]);
|
|
29
28
|
|
|
30
29
|
// properties for creating a method
|
|
31
30
|
export interface InterfacePropertyProps
|
|
@@ -45,9 +44,45 @@ export interface InterfacePropertyProps
|
|
|
45
44
|
|
|
46
45
|
/** Doc comment */
|
|
47
46
|
doc?: Children;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Property initializer
|
|
50
|
+
* @example `<ClassProperty name="My" get set nullable />`
|
|
51
|
+
*
|
|
52
|
+
* ```cs
|
|
53
|
+
* int? My { get; set; };
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
nullable?: boolean;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Define attributes to attach
|
|
60
|
+
* @example
|
|
61
|
+
* ```tsx
|
|
62
|
+
* <InterfaceProperty name="MyProp" attributes={[
|
|
63
|
+
* <Attribute name="Test" />
|
|
64
|
+
* <Attribute name="Test2" args={["arg1", "arg2"]} />
|
|
65
|
+
* ]} />
|
|
66
|
+
* ```
|
|
67
|
+
* This will produce:
|
|
68
|
+
* ```csharp
|
|
69
|
+
* [Test]
|
|
70
|
+
* [Test2("arg1", "arg2")]
|
|
71
|
+
* int MyProp { get; set; }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
attributes?: AttributesProp;
|
|
48
75
|
}
|
|
49
76
|
|
|
50
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Render a C# interface property.
|
|
79
|
+
*
|
|
80
|
+
* @example `<InterfaceProperty public name="My" get set />`
|
|
81
|
+
*
|
|
82
|
+
* ```cs
|
|
83
|
+
* public int My { get; set; };
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
51
86
|
export function InterfaceProperty(props: InterfacePropertyProps) {
|
|
52
87
|
const name = useCSharpNamePolicy().getName(props.name, "class-property");
|
|
53
88
|
const scope = useCSharpScope();
|
|
@@ -69,15 +104,17 @@ export function InterfaceProperty(props: InterfacePropertyProps) {
|
|
|
69
104
|
|
|
70
105
|
const modifiers = computeModifiersPrefix([
|
|
71
106
|
getAccessModifier(props),
|
|
72
|
-
|
|
107
|
+
getModifiers(props),
|
|
73
108
|
]);
|
|
74
109
|
// note that scope wraps the method decl so that the params get the correct scope
|
|
75
110
|
return (
|
|
76
111
|
<MemberDeclaration symbol={propertySymbol}>
|
|
77
112
|
<Scope value={propertyScope}>
|
|
78
113
|
<DocWhen doc={props.doc} />
|
|
114
|
+
<AttributeList attributes={props.attributes} endline />
|
|
79
115
|
{modifiers}
|
|
80
|
-
{props.type}
|
|
116
|
+
{props.type}
|
|
117
|
+
{props.nullable && "?"} {name}{" "}
|
|
81
118
|
<Block newline inline>
|
|
82
119
|
<List joiner=" ">
|
|
83
120
|
{props.get && "get;"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Children,
|
|
3
|
+
code,
|
|
4
|
+
Declaration,
|
|
5
|
+
For,
|
|
6
|
+
Indent,
|
|
7
|
+
OutputSymbol,
|
|
8
|
+
refkey,
|
|
9
|
+
Refkey,
|
|
10
|
+
} from "@alloy-js/core";
|
|
11
|
+
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
12
|
+
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
13
|
+
import { useCSharpScope } from "../../symbols/scopes.js";
|
|
14
|
+
import { Name } from "../Name.jsx";
|
|
15
|
+
|
|
16
|
+
export interface ParameterProps {
|
|
17
|
+
name: string;
|
|
18
|
+
type: Children;
|
|
19
|
+
/** If the parmaeter is optional(without default value) */
|
|
20
|
+
optional?: boolean;
|
|
21
|
+
/** Default value for the parameter */
|
|
22
|
+
default?: Children;
|
|
23
|
+
refkey?: Refkey;
|
|
24
|
+
symbol?: OutputSymbol;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Define a parameter to be used in class or interface method. */
|
|
28
|
+
export function Parameter(props: ParameterProps) {
|
|
29
|
+
const name = useCSharpNamePolicy().getName(props.name, "parameter");
|
|
30
|
+
const scope = useCSharpScope();
|
|
31
|
+
if (
|
|
32
|
+
scope.kind !== "member" ||
|
|
33
|
+
(scope.name !== "constructor-decl" && scope.name !== "method-decl")
|
|
34
|
+
) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
"can't define a parameter outside of a constructor-decl or method-decl scope",
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const memberSymbol = new CSharpOutputSymbol(name, {
|
|
41
|
+
scope,
|
|
42
|
+
refkeys: props.refkey ?? refkey(props.name),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<Declaration symbol={memberSymbol}>
|
|
47
|
+
{props.type}
|
|
48
|
+
{props.optional ? "?" : ""} <Name />
|
|
49
|
+
{props.default ? code` = ${props.default}` : ""}
|
|
50
|
+
</Declaration>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ParametersProps {
|
|
55
|
+
parameters: ParameterProps[] | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Render a collection of parameters */
|
|
59
|
+
export function Parameters(props: ParametersProps) {
|
|
60
|
+
return (
|
|
61
|
+
<group>
|
|
62
|
+
{"("}
|
|
63
|
+
{props.parameters && (
|
|
64
|
+
<Indent softline>
|
|
65
|
+
<For each={props.parameters} joiner={", "}>
|
|
66
|
+
{(param) => <Parameter {...param} />}
|
|
67
|
+
</For>
|
|
68
|
+
</Indent>
|
|
69
|
+
)}
|
|
70
|
+
<softline />
|
|
71
|
+
{")"}
|
|
72
|
+
</group>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { TestNamespace } from "../../../test/utils.jsx";
|
|
4
|
+
import { Attribute } from "../attributes/attributes.jsx";
|
|
5
|
+
import { ClassDeclaration } from "../ClassDeclaration.jsx";
|
|
6
|
+
import { Property } from "./property.jsx";
|
|
7
|
+
|
|
8
|
+
const Wrapper = (props: { children: Children }) => (
|
|
9
|
+
<TestNamespace>
|
|
10
|
+
<ClassDeclaration public name="TestClass">
|
|
11
|
+
{props.children}
|
|
12
|
+
</ClassDeclaration>
|
|
13
|
+
</TestNamespace>
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
describe("modifiers", () => {
|
|
17
|
+
describe("access modifiers", () => {
|
|
18
|
+
it.each(["public", "private", "protected", "internal"] as const)(
|
|
19
|
+
"%s",
|
|
20
|
+
(accessModifier) => {
|
|
21
|
+
expect(
|
|
22
|
+
<Wrapper>
|
|
23
|
+
<Property
|
|
24
|
+
{...{ [accessModifier]: true }}
|
|
25
|
+
name="TestProp"
|
|
26
|
+
type="string"
|
|
27
|
+
get
|
|
28
|
+
/>
|
|
29
|
+
</Wrapper>,
|
|
30
|
+
).toRenderTo(`
|
|
31
|
+
public class TestClass
|
|
32
|
+
{
|
|
33
|
+
${accessModifier} string TestProp { get; }
|
|
34
|
+
}
|
|
35
|
+
`);
|
|
36
|
+
},
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe("property modifiers", () => {
|
|
41
|
+
it.each([
|
|
42
|
+
"new",
|
|
43
|
+
"static",
|
|
44
|
+
"virtual",
|
|
45
|
+
"sealed",
|
|
46
|
+
"override",
|
|
47
|
+
"abstract",
|
|
48
|
+
"extern",
|
|
49
|
+
"readonly",
|
|
50
|
+
"required",
|
|
51
|
+
] as const)("%s", (methodModifier) => {
|
|
52
|
+
expect(
|
|
53
|
+
<Wrapper>
|
|
54
|
+
<Property
|
|
55
|
+
{...{ [methodModifier]: true }}
|
|
56
|
+
name="TestProp"
|
|
57
|
+
type="string"
|
|
58
|
+
get
|
|
59
|
+
/>
|
|
60
|
+
</Wrapper>,
|
|
61
|
+
).toRenderTo(`
|
|
62
|
+
public class TestClass
|
|
63
|
+
{
|
|
64
|
+
${methodModifier} string TestProp { get; }
|
|
65
|
+
}
|
|
66
|
+
`);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("combine modifiers", () => {
|
|
71
|
+
expect(
|
|
72
|
+
<Wrapper>
|
|
73
|
+
<Property public new name="TestProp" type="string" get />
|
|
74
|
+
</Wrapper>,
|
|
75
|
+
).toRenderTo(`
|
|
76
|
+
public class TestClass
|
|
77
|
+
{
|
|
78
|
+
public new string TestProp { get; }
|
|
79
|
+
}
|
|
80
|
+
`);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("applies PascalCase naming policy", () => {
|
|
85
|
+
expect(
|
|
86
|
+
<Wrapper>
|
|
87
|
+
<Property name="test_prop" type="string" get />
|
|
88
|
+
</Wrapper>,
|
|
89
|
+
).toRenderTo(`
|
|
90
|
+
public class TestClass
|
|
91
|
+
{
|
|
92
|
+
string TestProp { get; }
|
|
93
|
+
}
|
|
94
|
+
`);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("has getter only", () => {
|
|
98
|
+
expect(
|
|
99
|
+
<Wrapper>
|
|
100
|
+
<Property name="TestProp" type="string" get />
|
|
101
|
+
</Wrapper>,
|
|
102
|
+
).toRenderTo(`
|
|
103
|
+
public class TestClass
|
|
104
|
+
{
|
|
105
|
+
string TestProp { get; }
|
|
106
|
+
}
|
|
107
|
+
`);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("has setter only", () => {
|
|
111
|
+
expect(
|
|
112
|
+
<Wrapper>
|
|
113
|
+
<Property name="TestProp" type="string" set />
|
|
114
|
+
</Wrapper>,
|
|
115
|
+
).toRenderTo(`
|
|
116
|
+
public class TestClass
|
|
117
|
+
{
|
|
118
|
+
string TestProp { set; }
|
|
119
|
+
}
|
|
120
|
+
`);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("has getter and setter", () => {
|
|
124
|
+
expect(
|
|
125
|
+
<Wrapper>
|
|
126
|
+
<Property name="TestProp" type="string" get set />
|
|
127
|
+
</Wrapper>,
|
|
128
|
+
).toRenderTo(`
|
|
129
|
+
public class TestClass
|
|
130
|
+
{
|
|
131
|
+
string TestProp { get; set; }
|
|
132
|
+
}
|
|
133
|
+
`);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("has getter and init", () => {
|
|
137
|
+
expect(
|
|
138
|
+
<Wrapper>
|
|
139
|
+
<Property name="TestProp" type="string" get init />
|
|
140
|
+
</Wrapper>,
|
|
141
|
+
).toRenderTo(`
|
|
142
|
+
public class TestClass
|
|
143
|
+
{
|
|
144
|
+
string TestProp { get; init; }
|
|
145
|
+
}
|
|
146
|
+
`);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("specify doc comment", () => {
|
|
150
|
+
expect(
|
|
151
|
+
<TestNamespace>
|
|
152
|
+
<ClassDeclaration name="Test">
|
|
153
|
+
<Property name="Method" type="string" get set doc="This is a test" />
|
|
154
|
+
</ClassDeclaration>
|
|
155
|
+
</TestNamespace>,
|
|
156
|
+
).toRenderTo(`
|
|
157
|
+
class Test
|
|
158
|
+
{
|
|
159
|
+
/// This is a test
|
|
160
|
+
string Method { get; set; }
|
|
161
|
+
}
|
|
162
|
+
`);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("specify nullable property", () => {
|
|
166
|
+
expect(
|
|
167
|
+
<Wrapper>
|
|
168
|
+
<Property name="TestProp" type="string" nullable get set />
|
|
169
|
+
</Wrapper>,
|
|
170
|
+
).toRenderTo(`
|
|
171
|
+
public class TestClass
|
|
172
|
+
{
|
|
173
|
+
string? TestProp { get; set; }
|
|
174
|
+
}
|
|
175
|
+
`);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("specify initializer", () => {
|
|
179
|
+
expect(
|
|
180
|
+
<Wrapper>
|
|
181
|
+
<Property name="TestProp" type="string" get set initializer={`"abc"`} />
|
|
182
|
+
</Wrapper>,
|
|
183
|
+
).toRenderTo(`
|
|
184
|
+
public class TestClass
|
|
185
|
+
{
|
|
186
|
+
string TestProp { get; set; } = "abc";
|
|
187
|
+
}
|
|
188
|
+
`);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("specify attributes", () => {
|
|
192
|
+
expect(
|
|
193
|
+
<Wrapper>
|
|
194
|
+
<Property
|
|
195
|
+
name="Test"
|
|
196
|
+
type="int"
|
|
197
|
+
attributes={[<Attribute name="Test" />]}
|
|
198
|
+
get
|
|
199
|
+
set
|
|
200
|
+
/>
|
|
201
|
+
</Wrapper>,
|
|
202
|
+
).toRenderTo(`
|
|
203
|
+
public class TestClass
|
|
204
|
+
{
|
|
205
|
+
[Test]
|
|
206
|
+
int Test { get; set; }
|
|
207
|
+
}
|
|
208
|
+
`);
|
|
209
|
+
});
|