@alloy-js/csharp 0.18.0-dev.23 → 0.18.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/components/Class.d.ts +26 -0
- package/dist/src/components/Class.d.ts.map +1 -0
- package/dist/src/components/{ClassDeclaration.js → Class.js} +38 -64
- package/dist/src/components/ClassMethod.d.ts +5 -26
- package/dist/src/components/ClassMethod.d.ts.map +1 -1
- package/dist/src/components/ClassMethod.js +9 -27
- package/dist/src/components/Enum.d.ts +15 -0
- package/dist/src/components/Enum.d.ts.map +1 -0
- package/dist/src/components/{EnumDeclaration.js → Enum.js} +5 -25
- package/dist/src/components/Parameters.d.ts +13 -0
- package/dist/src/components/Parameters.d.ts.map +1 -0
- package/dist/src/components/Parameters.js +34 -0
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +0 -1
- package/dist/src/components/index.d.ts +4 -13
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +5 -14
- package/dist/src/components/stc/index.d.ts +2 -2
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +2 -2
- package/dist/src/modifiers.d.ts +4 -10
- package/dist/src/modifiers.d.ts.map +1 -1
- package/dist/src/modifiers.js +32 -9
- 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 +0 -2
- package/dist/test/class-method.test.js +14 -37
- package/dist/test/class.test.d.ts +2 -0
- package/dist/test/class.test.d.ts.map +1 -0
- package/dist/test/class.test.js +298 -0
- package/dist/test/enum.test.js +12 -12
- package/dist/test/namespace.test.js +8 -8
- package/dist/test/projectdirectory.test.d.ts +2 -0
- package/dist/test/projectdirectory.test.d.ts.map +1 -0
- package/dist/test/{project-directory.test.js → projectdirectory.test.js} +8 -8
- package/dist/test/sourcefile.test.js +4 -4
- package/dist/test/using.test.js +9 -9
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -4
- package/src/components/Class.tsx +149 -0
- package/src/components/ClassMethod.tsx +15 -54
- package/src/components/{EnumDeclaration.tsx → Enum.tsx} +6 -30
- package/src/components/Parameters.tsx +51 -0
- package/src/components/SourceFile.tsx +0 -1
- package/src/components/index.ts +4 -13
- package/src/components/stc/index.ts +2 -2
- package/src/modifiers.ts +42 -25
- package/src/name-policy.ts +0 -5
- package/temp/api.json +715 -5284
- package/test/class-method.test.tsx +14 -24
- package/test/class.test.tsx +292 -0
- package/test/enum.test.tsx +11 -11
- package/test/namespace.test.tsx +4 -4
- package/test/{project-directory.test.tsx → projectdirectory.test.tsx} +4 -4
- package/test/sourcefile.test.tsx +2 -2
- package/test/using.test.tsx +9 -9
- package/vitest.config.ts +0 -3
- package/dist/src/components/ClassDeclaration.d.ts +0 -74
- package/dist/src/components/ClassDeclaration.d.ts.map +0 -1
- package/dist/src/components/EnumDeclaration.d.ts +0 -34
- package/dist/src/components/EnumDeclaration.d.ts.map +0 -1
- package/dist/src/components/doc/comment.d.ts +0 -70
- package/dist/src/components/doc/comment.d.ts.map +0 -1
- package/dist/src/components/doc/comment.js +0 -88
- package/dist/src/components/doc/comment.test.d.ts +0 -2
- package/dist/src/components/doc/comment.test.d.ts.map +0 -1
- package/dist/src/components/doc/comment.test.js +0 -348
- package/dist/src/components/doc/from-markdown.d.ts +0 -6
- package/dist/src/components/doc/from-markdown.d.ts.map +0 -1
- package/dist/src/components/doc/from-markdown.js +0 -58
- package/dist/src/components/doc/from-markdown.test.d.ts +0 -2
- package/dist/src/components/doc/from-markdown.test.d.ts.map +0 -1
- package/dist/src/components/doc/from-markdown.test.js +0 -83
- package/dist/src/components/interface/declaration.d.ts +0 -47
- package/dist/src/components/interface/declaration.d.ts.map +0 -1
- package/dist/src/components/interface/declaration.js +0 -77
- package/dist/src/components/interface/declaration.test.d.ts +0 -2
- package/dist/src/components/interface/declaration.test.d.ts.map +0 -1
- package/dist/src/components/interface/declaration.test.js +0 -153
- package/dist/src/components/interface/method.d.ts +0 -32
- package/dist/src/components/interface/method.d.ts.map +0 -1
- package/dist/src/components/interface/method.js +0 -68
- package/dist/src/components/interface/method.test.d.ts +0 -2
- package/dist/src/components/interface/method.test.d.ts.map +0 -1
- package/dist/src/components/interface/method.test.js +0 -279
- package/dist/src/components/interface/property.d.ts +0 -38
- package/dist/src/components/interface/property.d.ts.map +0 -1
- package/dist/src/components/interface/property.js +0 -67
- package/dist/src/components/interface/property.test.d.ts +0 -2
- package/dist/src/components/interface/property.test.d.ts.map +0 -1
- package/dist/src/components/interface/property.test.js +0 -165
- package/dist/src/components/parameters/parameters.d.ts +0 -19
- package/dist/src/components/parameters/parameters.d.ts.map +0 -1
- package/dist/src/components/parameters/parameters.js +0 -43
- package/dist/src/components/property/property.d.ts +0 -62
- package/dist/src/components/property/property.d.ts.map +0 -1
- package/dist/src/components/property/property.js +0 -70
- package/dist/src/components/property/property.test.d.ts +0 -2
- package/dist/src/components/property/property.test.d.ts.map +0 -1
- package/dist/src/components/property/property.test.js +0 -218
- package/dist/src/components/record/declaration.d.ts +0 -35
- package/dist/src/components/record/declaration.d.ts.map +0 -1
- package/dist/src/components/record/declaration.js +0 -90
- package/dist/src/components/record/declaration.test.d.ts +0 -2
- package/dist/src/components/record/declaration.test.d.ts.map +0 -1
- package/dist/src/components/record/declaration.test.js +0 -94
- package/dist/src/components/type-parameters/type-parameter-constraints.d.ts +0 -8
- package/dist/src/components/type-parameters/type-parameter-constraints.d.ts.map +0 -1
- package/dist/src/components/type-parameters/type-parameter-constraints.js +0 -44
- package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts +0 -2
- package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts.map +0 -1
- package/dist/src/components/type-parameters/type-parameter-constraints.test.js +0 -67
- package/dist/src/components/type-parameters/type-parameter.d.ts +0 -20
- package/dist/src/components/type-parameters/type-parameter.d.ts.map +0 -1
- package/dist/src/components/type-parameters/type-parameter.js +0 -22
- package/dist/src/components/type-parameters/type-parameters.d.ts +0 -17
- package/dist/src/components/type-parameters/type-parameters.d.ts.map +0 -1
- package/dist/src/components/type-parameters/type-parameters.js +0 -54
- package/dist/src/components/type-parameters/type-parameters.test.d.ts +0 -2
- package/dist/src/components/type-parameters/type-parameters.test.d.ts.map +0 -1
- package/dist/src/components/type-parameters/type-parameters.test.js +0 -48
- package/dist/src/components/var/declaration.d.ts +0 -35
- package/dist/src/components/var/declaration.d.ts.map +0 -1
- package/dist/src/components/var/declaration.js +0 -40
- package/dist/src/components/var/declaration.test.d.ts +0 -2
- package/dist/src/components/var/declaration.test.d.ts.map +0 -1
- package/dist/src/components/var/declaration.test.js +0 -73
- package/dist/test/class-declaration.test.d.ts +0 -2
- package/dist/test/class-declaration.test.d.ts.map +0 -1
- package/dist/test/class-declaration.test.js +0 -463
- package/dist/test/project-directory.test.d.ts +0 -2
- package/dist/test/project-directory.test.d.ts.map +0 -1
- package/dist/test/vitest.setup.d.ts +0 -2
- package/dist/test/vitest.setup.d.ts.map +0 -1
- package/dist/test/vitest.setup.js +0 -1
- package/src/components/ClassDeclaration.tsx +0 -213
- package/src/components/doc/comment.test.tsx +0 -337
- package/src/components/doc/comment.tsx +0 -152
- package/src/components/doc/from-markdown.test.tsx +0 -103
- package/src/components/doc/from-markdown.tsx +0 -58
- package/src/components/interface/declaration.test.tsx +0 -143
- package/src/components/interface/declaration.tsx +0 -105
- package/src/components/interface/method.test.tsx +0 -278
- package/src/components/interface/method.tsx +0 -102
- package/src/components/interface/property.test.tsx +0 -144
- package/src/components/interface/property.tsx +0 -107
- package/src/components/parameters/parameters.tsx +0 -74
- package/src/components/property/property.test.tsx +0 -188
- package/src/components/property/property.tsx +0 -152
- package/src/components/record/declaration.test.tsx +0 -73
- package/src/components/record/declaration.tsx +0 -109
- package/src/components/type-parameters/type-parameter-constraints.test.tsx +0 -93
- package/src/components/type-parameters/type-parameter-constraints.tsx +0 -46
- package/src/components/type-parameters/type-parameter.tsx +0 -35
- package/src/components/type-parameters/type-parameters.test.tsx +0 -46
- package/src/components/type-parameters/type-parameters.tsx +0 -63
- package/src/components/var/declaration.test.tsx +0 -59
- package/src/components/var/declaration.tsx +0 -47
- package/test/class-declaration.test.tsx +0 -418
- package/test/vitest.setup.ts +0 -1
|
@@ -1,463 +0,0 @@
|
|
|
1
|
-
import { createComponent as _$createComponent, mergeProps as _$mergeProps, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
|
|
2
|
-
import * as core from "@alloy-js/core";
|
|
3
|
-
import { List, refkey } from "@alloy-js/core";
|
|
4
|
-
import * as coretest from "@alloy-js/core/testing";
|
|
5
|
-
import { describe, expect, it } from "vitest";
|
|
6
|
-
import * as csharp from "../src/index.js";
|
|
7
|
-
import { ClassDeclaration, ClassMember, Property, SourceFile } from "../src/index.js";
|
|
8
|
-
import * as utils from "./utils.js";
|
|
9
|
-
it("declares class with no members", () => {
|
|
10
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
11
|
-
get children() {
|
|
12
|
-
return _$createComponent(ClassDeclaration, {
|
|
13
|
-
name: "TestClass"
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
})).toRenderTo(`
|
|
17
|
-
class TestClass;
|
|
18
|
-
`);
|
|
19
|
-
});
|
|
20
|
-
describe("modifiers", () => {
|
|
21
|
-
it.each(["public", "private"])("%s", mod => {
|
|
22
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
23
|
-
get children() {
|
|
24
|
-
return _$createComponent(ClassDeclaration, _$mergeProps({
|
|
25
|
-
[mod]: true
|
|
26
|
-
}, {
|
|
27
|
-
name: "TestClass"
|
|
28
|
-
}));
|
|
29
|
-
}
|
|
30
|
-
})).toRenderTo(`
|
|
31
|
-
${mod} class TestClass;
|
|
32
|
-
`);
|
|
33
|
-
});
|
|
34
|
-
it.each(["partial", "abstract", "static", "sealed"])("%s", mod => {
|
|
35
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
36
|
-
get children() {
|
|
37
|
-
return _$createComponent(ClassDeclaration, _$mergeProps({
|
|
38
|
-
[mod]: true
|
|
39
|
-
}, {
|
|
40
|
-
name: "TestClass"
|
|
41
|
-
}));
|
|
42
|
-
}
|
|
43
|
-
})).toRenderTo(`
|
|
44
|
-
${mod} class TestClass;
|
|
45
|
-
`);
|
|
46
|
-
});
|
|
47
|
-
it("combines modifiers", () => {
|
|
48
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
49
|
-
get children() {
|
|
50
|
-
return _$createComponent(ClassDeclaration, {
|
|
51
|
-
"public": true,
|
|
52
|
-
abstract: true,
|
|
53
|
-
partial: true,
|
|
54
|
-
name: "TestClass"
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
})).toRenderTo(`
|
|
58
|
-
public abstract partial class TestClass;
|
|
59
|
-
`);
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
describe("base", () => {
|
|
63
|
-
it("define base class", () => {
|
|
64
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
65
|
-
get children() {
|
|
66
|
-
return _$createComponent(csharp.ClassDeclaration, {
|
|
67
|
-
name: "TestClass",
|
|
68
|
-
baseType: "Foo"
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
})).toRenderTo(`
|
|
72
|
-
class TestClass : Foo;
|
|
73
|
-
`);
|
|
74
|
-
});
|
|
75
|
-
it("define multiple interface types", () => {
|
|
76
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
77
|
-
get children() {
|
|
78
|
-
return _$createComponent(csharp.ClassDeclaration, {
|
|
79
|
-
name: "TestClass",
|
|
80
|
-
interfaceTypes: ["Foo", "Bar"]
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
})).toRenderTo(`
|
|
84
|
-
class TestClass : Foo, Bar;
|
|
85
|
-
`);
|
|
86
|
-
});
|
|
87
|
-
it("define base class and multiple interface types", () => {
|
|
88
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
89
|
-
get children() {
|
|
90
|
-
return _$createComponent(csharp.ClassDeclaration, {
|
|
91
|
-
name: "TestClass",
|
|
92
|
-
baseType: "BaseClass",
|
|
93
|
-
interfaceTypes: ["Foo", "Bar"]
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
})).toRenderTo(`
|
|
97
|
-
class TestClass : BaseClass, Foo, Bar;
|
|
98
|
-
`);
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
it("declares class with some members", () => {
|
|
102
|
-
const res = utils.toSourceText(_$createComponent(csharp.ClassDeclaration, {
|
|
103
|
-
"public": true,
|
|
104
|
-
name: "TestClass",
|
|
105
|
-
get children() {
|
|
106
|
-
return _$createComponent(core.StatementList, {
|
|
107
|
-
get children() {
|
|
108
|
-
return [_$createComponent(csharp.ClassMember, {
|
|
109
|
-
"public": true,
|
|
110
|
-
name: "MemberOne",
|
|
111
|
-
type: "string"
|
|
112
|
-
}), _$createComponent(csharp.ClassMember, {
|
|
113
|
-
"private": true,
|
|
114
|
-
name: "MemberTwo",
|
|
115
|
-
type: "int"
|
|
116
|
-
})];
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
}));
|
|
121
|
-
expect(res).toBe(coretest.d`
|
|
122
|
-
namespace TestCode
|
|
123
|
-
{
|
|
124
|
-
public class TestClass
|
|
125
|
-
{
|
|
126
|
-
public string MemberOne;
|
|
127
|
-
private int memberTwo;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
`);
|
|
131
|
-
});
|
|
132
|
-
it("declares class with some methods", () => {
|
|
133
|
-
const res = utils.toSourceText(_$createComponent(csharp.ClassDeclaration, {
|
|
134
|
-
"public": true,
|
|
135
|
-
name: "TestClass",
|
|
136
|
-
get children() {
|
|
137
|
-
return _$createComponent(core.List, {
|
|
138
|
-
get children() {
|
|
139
|
-
return [_$createComponent(csharp.ClassMethod, {
|
|
140
|
-
"public": true,
|
|
141
|
-
name: "MethodOne"
|
|
142
|
-
}), _$createComponent(csharp.ClassMethod, {
|
|
143
|
-
"private": true,
|
|
144
|
-
virtual: true,
|
|
145
|
-
name: "MethodTwo"
|
|
146
|
-
})];
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
}));
|
|
151
|
-
expect(res).toBe(coretest.d`
|
|
152
|
-
namespace TestCode
|
|
153
|
-
{
|
|
154
|
-
public class TestClass
|
|
155
|
-
{
|
|
156
|
-
public void MethodOne() {}
|
|
157
|
-
private virtual void MethodTwo() {}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
`);
|
|
161
|
-
});
|
|
162
|
-
it("uses refkeys for members, params, and return type", () => {
|
|
163
|
-
const inputTypeRefkey = core.refkey();
|
|
164
|
-
const testResultTypeRefkey = core.refkey();
|
|
165
|
-
const enumTypeRefkey = core.refkey();
|
|
166
|
-
const params = [{
|
|
167
|
-
name: "IntParam",
|
|
168
|
-
type: "int"
|
|
169
|
-
}, {
|
|
170
|
-
name: "BodyParam",
|
|
171
|
-
type: inputTypeRefkey
|
|
172
|
-
}];
|
|
173
|
-
const res = core.render(_$createComponent(core.Output, {
|
|
174
|
-
get namePolicy() {
|
|
175
|
-
return csharp.createCSharpNamePolicy();
|
|
176
|
-
},
|
|
177
|
-
get children() {
|
|
178
|
-
return _$createComponent(csharp.Namespace, {
|
|
179
|
-
name: "TestCode",
|
|
180
|
-
get children() {
|
|
181
|
-
return _$createComponent(csharp.SourceFile, {
|
|
182
|
-
path: "Test.cs",
|
|
183
|
-
get children() {
|
|
184
|
-
return [_$createComponent(csharp.EnumDeclaration, {
|
|
185
|
-
"public": true,
|
|
186
|
-
name: "TestEnum",
|
|
187
|
-
refkey: enumTypeRefkey,
|
|
188
|
-
get children() {
|
|
189
|
-
return _$createComponent(core.List, {
|
|
190
|
-
comma: true,
|
|
191
|
-
hardline: true,
|
|
192
|
-
get children() {
|
|
193
|
-
return [_$createComponent(csharp.EnumMember, {
|
|
194
|
-
name: "One"
|
|
195
|
-
}), _$createComponent(csharp.EnumMember, {
|
|
196
|
-
name: "Two"
|
|
197
|
-
})];
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
}), _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassDeclaration, {
|
|
202
|
-
"public": true,
|
|
203
|
-
name: "TestInput",
|
|
204
|
-
refkey: inputTypeRefkey
|
|
205
|
-
}), _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassDeclaration, {
|
|
206
|
-
"public": true,
|
|
207
|
-
name: "TestResult",
|
|
208
|
-
refkey: testResultTypeRefkey
|
|
209
|
-
}), _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassDeclaration, {
|
|
210
|
-
"public": true,
|
|
211
|
-
name: "TestClass",
|
|
212
|
-
get children() {
|
|
213
|
-
return [_$createComponent(csharp.ClassMember, {
|
|
214
|
-
"private": true,
|
|
215
|
-
name: "MemberOne",
|
|
216
|
-
type: enumTypeRefkey
|
|
217
|
-
}), ";", _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassMethod, {
|
|
218
|
-
"public": true,
|
|
219
|
-
name: "MethodOne",
|
|
220
|
-
parameters: params,
|
|
221
|
-
returns: testResultTypeRefkey,
|
|
222
|
-
get children() {
|
|
223
|
-
return ["return new ", testResultTypeRefkey, "();"];
|
|
224
|
-
}
|
|
225
|
-
})];
|
|
226
|
-
}
|
|
227
|
-
})];
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
}));
|
|
234
|
-
expect(res.contents[0].contents).toBe(coretest.d`
|
|
235
|
-
namespace TestCode
|
|
236
|
-
{
|
|
237
|
-
public enum TestEnum
|
|
238
|
-
{
|
|
239
|
-
One,
|
|
240
|
-
Two
|
|
241
|
-
}
|
|
242
|
-
public class TestInput;
|
|
243
|
-
public class TestResult;
|
|
244
|
-
public class TestClass
|
|
245
|
-
{
|
|
246
|
-
private TestEnum memberOne;
|
|
247
|
-
public TestResult MethodOne(int intParam, TestInput bodyParam)
|
|
248
|
-
{
|
|
249
|
-
return new TestResult();
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
`);
|
|
254
|
-
});
|
|
255
|
-
describe("with type parameters", () => {
|
|
256
|
-
it("reference parameters", () => {
|
|
257
|
-
const typeParameters = [{
|
|
258
|
-
name: "T",
|
|
259
|
-
refkey: refkey()
|
|
260
|
-
}, {
|
|
261
|
-
name: "U",
|
|
262
|
-
refkey: refkey()
|
|
263
|
-
}];
|
|
264
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
265
|
-
get children() {
|
|
266
|
-
return _$createComponent(SourceFile, {
|
|
267
|
-
path: "Test.cs",
|
|
268
|
-
get children() {
|
|
269
|
-
return _$createComponent(ClassDeclaration, {
|
|
270
|
-
"public": true,
|
|
271
|
-
name: "TestClass",
|
|
272
|
-
typeParameters: typeParameters,
|
|
273
|
-
get children() {
|
|
274
|
-
return _$createComponent(List, {
|
|
275
|
-
get children() {
|
|
276
|
-
return [_$createComponent(Property, {
|
|
277
|
-
name: "PropA",
|
|
278
|
-
get type() {
|
|
279
|
-
return typeParameters[0].refkey;
|
|
280
|
-
},
|
|
281
|
-
get: true,
|
|
282
|
-
set: true
|
|
283
|
-
}), _$createComponent(Property, {
|
|
284
|
-
name: "PropB",
|
|
285
|
-
get type() {
|
|
286
|
-
return typeParameters[1].refkey;
|
|
287
|
-
},
|
|
288
|
-
get: true,
|
|
289
|
-
set: true
|
|
290
|
-
})];
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
})).toRenderTo(`
|
|
299
|
-
namespace TestCode
|
|
300
|
-
{
|
|
301
|
-
public class TestClass<T, U>
|
|
302
|
-
{
|
|
303
|
-
T PropA { get; set; }
|
|
304
|
-
U PropB { get; set; }
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
`);
|
|
308
|
-
});
|
|
309
|
-
it("defines with constraints", () => {
|
|
310
|
-
const typeParameters = [{
|
|
311
|
-
name: "T",
|
|
312
|
-
constraints: "IFoo"
|
|
313
|
-
}, {
|
|
314
|
-
name: "U",
|
|
315
|
-
constraints: "IBar"
|
|
316
|
-
}];
|
|
317
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
318
|
-
get children() {
|
|
319
|
-
return _$createComponent(ClassDeclaration, {
|
|
320
|
-
"public": true,
|
|
321
|
-
name: "TestClass",
|
|
322
|
-
typeParameters: typeParameters,
|
|
323
|
-
children: "// Body"
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
})).toRenderTo(`
|
|
327
|
-
public class TestClass<T, U>
|
|
328
|
-
where T : IFoo
|
|
329
|
-
where U : IBar
|
|
330
|
-
{
|
|
331
|
-
// Body
|
|
332
|
-
}
|
|
333
|
-
`);
|
|
334
|
-
});
|
|
335
|
-
});
|
|
336
|
-
it("declares class with invalid members", () => {
|
|
337
|
-
const decl = _$createComponent(csharp.ClassDeclaration, {
|
|
338
|
-
"public": true,
|
|
339
|
-
name: "TestClass",
|
|
340
|
-
get children() {
|
|
341
|
-
return [_$createComponent(csharp.EnumMember, {
|
|
342
|
-
name: "One"
|
|
343
|
-
}), ",", _$createIntrinsic("hbr", {}), _$createComponent(csharp.EnumMember, {
|
|
344
|
-
name: "Two"
|
|
345
|
-
})];
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
expect(() => utils.toSourceText(decl)).toThrow("can't define an enum member outside of an enum-decl scope");
|
|
349
|
-
});
|
|
350
|
-
it("declares class with constructor", () => {
|
|
351
|
-
const res = utils.toSourceText(_$createComponent(csharp.ClassDeclaration, {
|
|
352
|
-
"public": true,
|
|
353
|
-
name: "TestClass",
|
|
354
|
-
get children() {
|
|
355
|
-
return _$createComponent(csharp.ClassConstructor, {
|
|
356
|
-
"public": true
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
}));
|
|
360
|
-
expect(res).toBe(coretest.d`
|
|
361
|
-
namespace TestCode
|
|
362
|
-
{
|
|
363
|
-
public class TestClass
|
|
364
|
-
{
|
|
365
|
-
public TestClass() {}
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
`);
|
|
369
|
-
});
|
|
370
|
-
it("declares class with constructor params and assigns values to fields", () => {
|
|
371
|
-
const thisNameRefkey = core.refkey();
|
|
372
|
-
const thisSizeRefkey = core.refkey();
|
|
373
|
-
const paramNameRefkey = core.refkey();
|
|
374
|
-
const paramSizeRefkey = core.refkey();
|
|
375
|
-
const ctorParams = [{
|
|
376
|
-
name: "name",
|
|
377
|
-
type: "string",
|
|
378
|
-
refkey: paramNameRefkey
|
|
379
|
-
}, {
|
|
380
|
-
name: "size",
|
|
381
|
-
type: "int",
|
|
382
|
-
refkey: paramSizeRefkey
|
|
383
|
-
}];
|
|
384
|
-
const res = utils.toSourceText(_$createComponent(csharp.ClassDeclaration, {
|
|
385
|
-
"public": true,
|
|
386
|
-
name: "TestClass",
|
|
387
|
-
get children() {
|
|
388
|
-
return [_$createComponent(csharp.ClassMember, {
|
|
389
|
-
"private": true,
|
|
390
|
-
name: "name",
|
|
391
|
-
type: "string",
|
|
392
|
-
refkey: thisNameRefkey
|
|
393
|
-
}), ";", _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassMember, {
|
|
394
|
-
"private": true,
|
|
395
|
-
name: "size",
|
|
396
|
-
type: "int",
|
|
397
|
-
refkey: thisSizeRefkey
|
|
398
|
-
}), ";", _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassConstructor, {
|
|
399
|
-
"public": true,
|
|
400
|
-
parameters: ctorParams,
|
|
401
|
-
get children() {
|
|
402
|
-
return [thisNameRefkey, " = ", paramNameRefkey, ";", _$createIntrinsic("hbr", {}), thisSizeRefkey, " = ", paramSizeRefkey, ";"];
|
|
403
|
-
}
|
|
404
|
-
})];
|
|
405
|
-
}
|
|
406
|
-
}));
|
|
407
|
-
|
|
408
|
-
// TODO: assignments to members should have this. prefix
|
|
409
|
-
// e.g. this.name = name;
|
|
410
|
-
expect(res).toBe(coretest.d`
|
|
411
|
-
namespace TestCode
|
|
412
|
-
{
|
|
413
|
-
public class TestClass
|
|
414
|
-
{
|
|
415
|
-
private string name;
|
|
416
|
-
private int size;
|
|
417
|
-
public TestClass(string name, int size)
|
|
418
|
-
{
|
|
419
|
-
name = name;
|
|
420
|
-
size = size;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
`);
|
|
425
|
-
});
|
|
426
|
-
it("specify doc comment", () => {
|
|
427
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
428
|
-
get children() {
|
|
429
|
-
return _$createComponent(ClassDeclaration, {
|
|
430
|
-
name: "Test",
|
|
431
|
-
doc: "This is a test"
|
|
432
|
-
});
|
|
433
|
-
}
|
|
434
|
-
})).toRenderTo(`
|
|
435
|
-
/// This is a test
|
|
436
|
-
class Test;
|
|
437
|
-
`);
|
|
438
|
-
});
|
|
439
|
-
it("supports class member doc comments", () => {
|
|
440
|
-
expect(_$createComponent(utils.TestNamespace, {
|
|
441
|
-
get children() {
|
|
442
|
-
return _$createComponent(ClassDeclaration, {
|
|
443
|
-
name: "Test",
|
|
444
|
-
doc: "This is a test",
|
|
445
|
-
get children() {
|
|
446
|
-
return _$createComponent(ClassMember, {
|
|
447
|
-
name: "Member",
|
|
448
|
-
"public": true,
|
|
449
|
-
type: "int",
|
|
450
|
-
doc: "This is a member"
|
|
451
|
-
});
|
|
452
|
-
}
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
})).toRenderTo(`
|
|
456
|
-
/// This is a test
|
|
457
|
-
class Test
|
|
458
|
-
{
|
|
459
|
-
/// This is a member
|
|
460
|
-
public int Member
|
|
461
|
-
}
|
|
462
|
-
`);
|
|
463
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"project-directory.test.d.ts","sourceRoot":"","sources":["../../test/project-directory.test.tsx"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"vitest.setup.d.ts","sourceRoot":"","sources":["../../test/vitest.setup.ts"],"names":[],"mappings":"AAAA,OAAO,wBAAwB,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import "@alloy-js/core/testing";
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
import * as core from "@alloy-js/core";
|
|
2
|
-
import { join } from "@alloy-js/core";
|
|
3
|
-
import {
|
|
4
|
-
AccessModifiers,
|
|
5
|
-
computeModifiersPrefix,
|
|
6
|
-
getAccessModifier,
|
|
7
|
-
makeModifiers,
|
|
8
|
-
} from "../modifiers.js";
|
|
9
|
-
import { CSharpElements, useCSharpNamePolicy } from "../name-policy.js";
|
|
10
|
-
import { CSharpOutputSymbol } from "../symbols/csharp-output-symbol.js";
|
|
11
|
-
import { CSharpMemberScope, useCSharpScope } from "../symbols/scopes.js";
|
|
12
|
-
import { DocWhen } from "./doc/comment.jsx";
|
|
13
|
-
import { Name } from "./Name.jsx";
|
|
14
|
-
import { ParameterProps, Parameters } from "./parameters/parameters.jsx";
|
|
15
|
-
import { TypeParameterConstraints } from "./type-parameters/type-parameter-constraints.jsx";
|
|
16
|
-
import { TypeParameterProps } from "./type-parameters/type-parameter.jsx";
|
|
17
|
-
import { TypeParameters } from "./type-parameters/type-parameters.jsx";
|
|
18
|
-
|
|
19
|
-
export interface ClassModifiers {
|
|
20
|
-
readonly abstract?: boolean;
|
|
21
|
-
readonly partial?: boolean;
|
|
22
|
-
readonly sealed?: boolean;
|
|
23
|
-
readonly static?: boolean;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const getClassModifiers = makeModifiers<ClassModifiers>([
|
|
27
|
-
"abstract",
|
|
28
|
-
"partial",
|
|
29
|
-
"sealed",
|
|
30
|
-
"static",
|
|
31
|
-
]);
|
|
32
|
-
|
|
33
|
-
// properties for creating a class
|
|
34
|
-
export interface ClassDeclarationProps
|
|
35
|
-
extends Omit<core.DeclarationProps, "nameKind">,
|
|
36
|
-
AccessModifiers,
|
|
37
|
-
ClassModifiers {
|
|
38
|
-
name: string;
|
|
39
|
-
/** Doc comment */
|
|
40
|
-
doc?: core.Children;
|
|
41
|
-
refkey?: core.Refkey;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Type parameters for the class
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```tsx
|
|
48
|
-
* <ClassDeclaration name="MyClass" typeParameters={["T"]} />
|
|
49
|
-
* ```
|
|
50
|
-
* This will produce:
|
|
51
|
-
* ```csharp
|
|
52
|
-
* public class MyClass<T>
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
typeParameters?: (string | TypeParameterProps)[];
|
|
56
|
-
|
|
57
|
-
/** Base class that this class extends */
|
|
58
|
-
baseType?: core.Children;
|
|
59
|
-
|
|
60
|
-
/** Interfaces this class implements */
|
|
61
|
-
interfaceTypes?: core.Children[];
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* CSharp class declaration.
|
|
66
|
-
* @example
|
|
67
|
-
* ```tsx
|
|
68
|
-
* <ClassDeclaration public name="MyClass">
|
|
69
|
-
* <ClassMember public name="MyField" type="int" />
|
|
70
|
-
* <ClassConstructor>
|
|
71
|
-
* <Parameter name="value" type="int" />
|
|
72
|
-
* this.MyField = value;
|
|
73
|
-
* </ClassConstructor>
|
|
74
|
-
* </ClassDeclaration>
|
|
75
|
-
* ```
|
|
76
|
-
* This will produce:
|
|
77
|
-
* ```csharp
|
|
78
|
-
* public class MyClass
|
|
79
|
-
* {
|
|
80
|
-
* public int MyField;
|
|
81
|
-
* public MyClass(int value)
|
|
82
|
-
* {
|
|
83
|
-
* this.MyField = value;
|
|
84
|
-
* }
|
|
85
|
-
* }
|
|
86
|
-
* ```
|
|
87
|
-
*/
|
|
88
|
-
export function ClassDeclaration(props: ClassDeclarationProps) {
|
|
89
|
-
const name = useCSharpNamePolicy().getName(props.name!, "class");
|
|
90
|
-
|
|
91
|
-
const thisClassSymbol = new CSharpOutputSymbol(name, {
|
|
92
|
-
refkeys: props.refkey,
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// this creates a new scope for the class definition.
|
|
96
|
-
// members will automatically "inherit" this scope so
|
|
97
|
-
// that refkeys to them will produce the fully-qualified
|
|
98
|
-
// name e.g. Foo.Bar.
|
|
99
|
-
const thisClassScope = new CSharpMemberScope("class-decl", {
|
|
100
|
-
owner: thisClassSymbol,
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
const bases = [
|
|
104
|
-
...(props.baseType ? [props.baseType] : []),
|
|
105
|
-
...(props.interfaceTypes || []),
|
|
106
|
-
];
|
|
107
|
-
const base =
|
|
108
|
-
bases.length > 0 ? <> : {join(bases, { joiner: ", " })}</> : null;
|
|
109
|
-
const modifiers = computeModifiersPrefix([
|
|
110
|
-
getAccessModifier(props),
|
|
111
|
-
getClassModifiers(props),
|
|
112
|
-
]);
|
|
113
|
-
return (
|
|
114
|
-
<core.Declaration symbol={thisClassSymbol}>
|
|
115
|
-
<DocWhen doc={props.doc} />
|
|
116
|
-
{modifiers}class <Name />
|
|
117
|
-
{props.typeParameters && (
|
|
118
|
-
<TypeParameters parameters={props.typeParameters} />
|
|
119
|
-
)}
|
|
120
|
-
{base}
|
|
121
|
-
{props.typeParameters && (
|
|
122
|
-
<TypeParameterConstraints parameters={props.typeParameters} />
|
|
123
|
-
)}
|
|
124
|
-
{!props.children && ";"}
|
|
125
|
-
{props.children && (
|
|
126
|
-
<core.Block newline>
|
|
127
|
-
<core.Scope value={thisClassScope}>{props.children}</core.Scope>
|
|
128
|
-
</core.Block>
|
|
129
|
-
)}
|
|
130
|
-
</core.Declaration>
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export interface ClassConstructorProps extends AccessModifiers {
|
|
135
|
-
parameters?: Array<ParameterProps>;
|
|
136
|
-
refkey?: core.Refkey;
|
|
137
|
-
symbol?: core.OutputSymbol;
|
|
138
|
-
children?: core.Children;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// a C# class constructor
|
|
142
|
-
export function ClassConstructor(props: ClassConstructorProps) {
|
|
143
|
-
const scope = useCSharpScope();
|
|
144
|
-
if (scope.kind !== "member" || scope.name !== "class-decl") {
|
|
145
|
-
throw new Error(
|
|
146
|
-
"can't define a class constructor outside of a class-decl scope",
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// fetch the class name from the scope
|
|
151
|
-
const name = useCSharpNamePolicy().getName(scope.owner!.name, "class-method");
|
|
152
|
-
const ctorSymbol = new CSharpOutputSymbol(name, {
|
|
153
|
-
scope,
|
|
154
|
-
refkeys: props.refkey ?? core.refkey(name),
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
// scope for ctor declaration
|
|
158
|
-
const ctorDeclScope = new CSharpMemberScope("constructor-decl", {
|
|
159
|
-
owner: ctorSymbol,
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
const modifiers = computeModifiersPrefix([getAccessModifier(props)]);
|
|
163
|
-
|
|
164
|
-
// note that scope wraps the ctor decl so that the params get the correct scope
|
|
165
|
-
return (
|
|
166
|
-
<core.Declaration symbol={ctorSymbol}>
|
|
167
|
-
<core.Scope value={ctorDeclScope}>
|
|
168
|
-
{modifiers}
|
|
169
|
-
<Name />
|
|
170
|
-
<Parameters parameters={props.parameters} />
|
|
171
|
-
<core.Block newline>{props.children}</core.Block>
|
|
172
|
-
</core.Scope>
|
|
173
|
-
</core.Declaration>
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// properties for creating a class member
|
|
178
|
-
export interface ClassMemberProps extends AccessModifiers {
|
|
179
|
-
name: string;
|
|
180
|
-
type: core.Children;
|
|
181
|
-
refkey?: core.Refkey;
|
|
182
|
-
/** Doc comment */
|
|
183
|
-
doc?: core.Children;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// a C# class member (i.e. a field within a class like "private int count")
|
|
187
|
-
export function ClassMember(props: ClassMemberProps) {
|
|
188
|
-
let nameElement: CSharpElements = "class-member-private";
|
|
189
|
-
if (props.public) {
|
|
190
|
-
nameElement = "class-member-public";
|
|
191
|
-
}
|
|
192
|
-
const name = useCSharpNamePolicy().getName(props.name, nameElement);
|
|
193
|
-
const scope = useCSharpScope();
|
|
194
|
-
if (scope.kind !== "member" || scope.name !== "class-decl") {
|
|
195
|
-
throw new Error(
|
|
196
|
-
"can't define a class member outside of a class-decl scope",
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const memberSymbol = new CSharpOutputSymbol(name, {
|
|
201
|
-
scope,
|
|
202
|
-
refkeys: props.refkey ?? core.refkey(props.name),
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
const modifiers = computeModifiersPrefix([getAccessModifier(props)]);
|
|
206
|
-
return (
|
|
207
|
-
<core.Declaration symbol={memberSymbol}>
|
|
208
|
-
<DocWhen doc={props.doc} />
|
|
209
|
-
{modifiers}
|
|
210
|
-
{props.type} <Name />
|
|
211
|
-
</core.Declaration>
|
|
212
|
-
);
|
|
213
|
-
}
|