@alloy-js/csharp 0.19.0-dev.5 → 0.19.0-dev.7
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/field/field.d.ts +8 -1
- package/dist/src/components/field/field.d.ts.map +1 -1
- package/dist/src/components/field/field.js +8 -4
- package/dist/src/components/field/field.test.js +125 -19
- package/dist/src/components/struct/declaration.test.js +4 -4
- package/dist/src/name-policy.d.ts.map +1 -1
- package/dist/src/name-policy.js +2 -0
- package/dist/test/class-declaration.test.js +12 -12
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/components/field/field.test.tsx +108 -10
- package/src/components/field/field.tsx +24 -4
- package/src/components/struct/declaration.test.tsx +5 -5
- package/src/name-policy.ts +2 -0
- package/temp/api.json +139 -0
- package/test/class-declaration.test.tsx +12 -13
|
@@ -1,24 +1,122 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { expect, it } from "vitest";
|
|
1
|
+
import { List } from "@alloy-js/core";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
3
|
import { TestNamespace } from "../../../test/utils.jsx";
|
|
4
4
|
import { ClassDeclaration } from "../ClassDeclaration.jsx";
|
|
5
5
|
import { Field } from "./field.jsx";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
function Wrapper(props: { children: any }) {
|
|
8
|
+
return (
|
|
9
9
|
<TestNamespace>
|
|
10
10
|
<ClassDeclaration public name="TestClass">
|
|
11
|
-
|
|
12
|
-
<Field public name="MemberOne" type="string" />
|
|
13
|
-
<Field private name="MemberTwo" type="int" />
|
|
14
|
-
</StatementList>
|
|
11
|
+
{props.children}
|
|
15
12
|
</ClassDeclaration>
|
|
16
|
-
</TestNamespace
|
|
13
|
+
</TestNamespace>
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
it("declares multiple fields", () => {
|
|
17
|
+
expect(
|
|
18
|
+
<Wrapper>
|
|
19
|
+
<List>
|
|
20
|
+
<Field public name="MemberOne" type="string" />
|
|
21
|
+
<Field public name="MemberTwo" type="int" />
|
|
22
|
+
</List>
|
|
23
|
+
</Wrapper>,
|
|
17
24
|
).toRenderTo(`
|
|
18
25
|
public class TestClass
|
|
19
26
|
{
|
|
20
27
|
public string MemberOne;
|
|
21
|
-
|
|
28
|
+
public int MemberTwo;
|
|
29
|
+
}
|
|
30
|
+
`);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("modifiers", () => {
|
|
34
|
+
describe("access modifiers", () => {
|
|
35
|
+
it.each(["public", "private", "protected", "internal"] as const)(
|
|
36
|
+
"%s",
|
|
37
|
+
(accessModifier) => {
|
|
38
|
+
expect(
|
|
39
|
+
<Wrapper>
|
|
40
|
+
<Field
|
|
41
|
+
{...{ [accessModifier]: true }}
|
|
42
|
+
name="TestProp"
|
|
43
|
+
type="string"
|
|
44
|
+
/>
|
|
45
|
+
</Wrapper>,
|
|
46
|
+
).toRenderTo(`
|
|
47
|
+
public class TestClass
|
|
48
|
+
{
|
|
49
|
+
${accessModifier} string ${accessModifier === "private" ? "_testProp" : "TestProp"};
|
|
50
|
+
}
|
|
51
|
+
`);
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe("modifiers", () => {
|
|
57
|
+
it.each(["new", "static", "readonly", "volatile"] as const)(
|
|
58
|
+
"%s",
|
|
59
|
+
(methodModifier) => {
|
|
60
|
+
expect(
|
|
61
|
+
<Wrapper>
|
|
62
|
+
<Field
|
|
63
|
+
{...{ [methodModifier]: true }}
|
|
64
|
+
name="TestField"
|
|
65
|
+
type="string"
|
|
66
|
+
/>
|
|
67
|
+
</Wrapper>,
|
|
68
|
+
).toRenderTo(`
|
|
69
|
+
public class TestClass
|
|
70
|
+
{
|
|
71
|
+
${methodModifier} string _testField;
|
|
72
|
+
}
|
|
73
|
+
`);
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("combine modifiers", () => {
|
|
79
|
+
expect(
|
|
80
|
+
<Wrapper>
|
|
81
|
+
<Field public new name="TestField" type="string" />
|
|
82
|
+
</Wrapper>,
|
|
83
|
+
).toRenderTo(`
|
|
84
|
+
public class TestClass
|
|
85
|
+
{
|
|
86
|
+
public new string TestField;
|
|
87
|
+
}
|
|
88
|
+
`);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("naming", () => {
|
|
93
|
+
it("public field are PascalCase", () => {
|
|
94
|
+
expect(
|
|
95
|
+
<Wrapper>
|
|
96
|
+
<List>
|
|
97
|
+
<Field public name="member_one" type="string" />
|
|
98
|
+
</List>
|
|
99
|
+
</Wrapper>,
|
|
100
|
+
).toRenderTo(`
|
|
101
|
+
public class TestClass
|
|
102
|
+
{
|
|
103
|
+
public string MemberOne;
|
|
104
|
+
}
|
|
105
|
+
`);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("private field are camelCase with _ prefix", () => {
|
|
109
|
+
expect(
|
|
110
|
+
<Wrapper>
|
|
111
|
+
<List>
|
|
112
|
+
<Field private name="member_one" type="string" />
|
|
113
|
+
</List>
|
|
114
|
+
</Wrapper>,
|
|
115
|
+
).toRenderTo(`
|
|
116
|
+
public class TestClass
|
|
117
|
+
{
|
|
118
|
+
private string _memberOne;
|
|
22
119
|
}
|
|
23
120
|
`);
|
|
121
|
+
});
|
|
24
122
|
});
|
|
@@ -3,13 +3,29 @@ import {
|
|
|
3
3
|
AccessModifiers,
|
|
4
4
|
computeModifiersPrefix,
|
|
5
5
|
getAccessModifier,
|
|
6
|
+
makeModifiers,
|
|
6
7
|
} from "../../modifiers.js";
|
|
7
8
|
import { CSharpElements, useCSharpNamePolicy } from "../../name-policy.js";
|
|
8
9
|
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
9
10
|
import { useCSharpScope } from "../../symbols/scopes.js";
|
|
10
11
|
import { DocWhen } from "../doc/comment.jsx";
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
/** Field modifiers. */
|
|
14
|
+
export interface FieldModifiers {
|
|
15
|
+
readonly new?: boolean;
|
|
16
|
+
readonly static?: boolean;
|
|
17
|
+
readonly readonly?: boolean;
|
|
18
|
+
readonly volatile?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const getModifiers = makeModifiers<FieldModifiers>([
|
|
22
|
+
"new",
|
|
23
|
+
"static",
|
|
24
|
+
"readonly",
|
|
25
|
+
"volatile",
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
export interface FieldProps extends AccessModifiers, FieldModifiers {
|
|
13
29
|
name: string;
|
|
14
30
|
type: Children;
|
|
15
31
|
refkey?: Refkey;
|
|
@@ -20,7 +36,7 @@ export interface FieldProps extends AccessModifiers {
|
|
|
20
36
|
/** Render a c# field */
|
|
21
37
|
export function Field(props: FieldProps) {
|
|
22
38
|
let nameElement: CSharpElements = "class-member-private";
|
|
23
|
-
if (props.public) {
|
|
39
|
+
if (props.public || props.protected || props.internal) {
|
|
24
40
|
nameElement = "class-member-public";
|
|
25
41
|
}
|
|
26
42
|
const name = useCSharpNamePolicy().getName(props.name, nameElement);
|
|
@@ -39,12 +55,16 @@ export function Field(props: FieldProps) {
|
|
|
39
55
|
refkeys: props.refkey ?? refkey(props.name),
|
|
40
56
|
});
|
|
41
57
|
|
|
42
|
-
const modifiers = computeModifiersPrefix([
|
|
58
|
+
const modifiers = computeModifiersPrefix([
|
|
59
|
+
getAccessModifier(props),
|
|
60
|
+
getModifiers(props),
|
|
61
|
+
]);
|
|
62
|
+
|
|
43
63
|
return (
|
|
44
64
|
<Declaration symbol={memberSymbol}>
|
|
45
65
|
<DocWhen doc={props.doc} />
|
|
46
66
|
{modifiers}
|
|
47
|
-
{props.type} <Name
|
|
67
|
+
{props.type} <Name />;
|
|
48
68
|
</Declaration>
|
|
49
69
|
);
|
|
50
70
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { List, refkey
|
|
1
|
+
import { List, refkey } from "@alloy-js/core";
|
|
2
2
|
import { describe, expect, it } from "vitest";
|
|
3
3
|
import { TestNamespace } from "../../../test/utils.jsx";
|
|
4
4
|
import { Attribute } from "../attributes/attributes.jsx";
|
|
@@ -173,17 +173,17 @@ it("defines fields", () => {
|
|
|
173
173
|
expect(
|
|
174
174
|
<TestNamespace>
|
|
175
175
|
<StructDeclaration public name="TestClass">
|
|
176
|
-
<
|
|
176
|
+
<List>
|
|
177
177
|
<Field public name="MemberOne" type="string" />
|
|
178
|
-
<Field
|
|
179
|
-
</
|
|
178
|
+
<Field public name="MemberTwo" type="int" />
|
|
179
|
+
</List>
|
|
180
180
|
</StructDeclaration>
|
|
181
181
|
</TestNamespace>,
|
|
182
182
|
).toRenderTo(`
|
|
183
183
|
public struct TestClass
|
|
184
184
|
{
|
|
185
185
|
public string MemberOne;
|
|
186
|
-
|
|
186
|
+
public int MemberTwo;
|
|
187
187
|
}
|
|
188
188
|
`);
|
|
189
189
|
});
|
package/src/name-policy.ts
CHANGED
|
@@ -36,6 +36,8 @@ export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
|
|
|
36
36
|
return changecase.pascalCase(name);
|
|
37
37
|
case "constant":
|
|
38
38
|
return changecase.constantCase(name);
|
|
39
|
+
case "class-member-private":
|
|
40
|
+
return `_${changecase.camelCase(name)}`;
|
|
39
41
|
default:
|
|
40
42
|
return changecase.camelCase(name);
|
|
41
43
|
}
|
package/temp/api.json
CHANGED
|
@@ -4096,6 +4096,132 @@
|
|
|
4096
4096
|
],
|
|
4097
4097
|
"name": "Field"
|
|
4098
4098
|
},
|
|
4099
|
+
{
|
|
4100
|
+
"kind": "Interface",
|
|
4101
|
+
"canonicalReference": "@alloy-js/csharp!FieldModifiers:interface",
|
|
4102
|
+
"docComment": "/**\n * Field modifiers.\n */\n",
|
|
4103
|
+
"excerptTokens": [
|
|
4104
|
+
{
|
|
4105
|
+
"kind": "Content",
|
|
4106
|
+
"text": "export interface FieldModifiers "
|
|
4107
|
+
}
|
|
4108
|
+
],
|
|
4109
|
+
"fileUrlPath": "src/components/field/field.tsx",
|
|
4110
|
+
"releaseTag": "Public",
|
|
4111
|
+
"name": "FieldModifiers",
|
|
4112
|
+
"preserveMemberOrder": false,
|
|
4113
|
+
"members": [
|
|
4114
|
+
{
|
|
4115
|
+
"kind": "PropertySignature",
|
|
4116
|
+
"canonicalReference": "@alloy-js/csharp!FieldModifiers#new:member",
|
|
4117
|
+
"docComment": "",
|
|
4118
|
+
"excerptTokens": [
|
|
4119
|
+
{
|
|
4120
|
+
"kind": "Content",
|
|
4121
|
+
"text": "readonly new?: "
|
|
4122
|
+
},
|
|
4123
|
+
{
|
|
4124
|
+
"kind": "Content",
|
|
4125
|
+
"text": "boolean"
|
|
4126
|
+
},
|
|
4127
|
+
{
|
|
4128
|
+
"kind": "Content",
|
|
4129
|
+
"text": ";"
|
|
4130
|
+
}
|
|
4131
|
+
],
|
|
4132
|
+
"isReadonly": true,
|
|
4133
|
+
"isOptional": true,
|
|
4134
|
+
"releaseTag": "Public",
|
|
4135
|
+
"name": "new",
|
|
4136
|
+
"propertyTypeTokenRange": {
|
|
4137
|
+
"startIndex": 1,
|
|
4138
|
+
"endIndex": 2
|
|
4139
|
+
}
|
|
4140
|
+
},
|
|
4141
|
+
{
|
|
4142
|
+
"kind": "PropertySignature",
|
|
4143
|
+
"canonicalReference": "@alloy-js/csharp!FieldModifiers#readonly:member",
|
|
4144
|
+
"docComment": "",
|
|
4145
|
+
"excerptTokens": [
|
|
4146
|
+
{
|
|
4147
|
+
"kind": "Content",
|
|
4148
|
+
"text": "readonly readonly?: "
|
|
4149
|
+
},
|
|
4150
|
+
{
|
|
4151
|
+
"kind": "Content",
|
|
4152
|
+
"text": "boolean"
|
|
4153
|
+
},
|
|
4154
|
+
{
|
|
4155
|
+
"kind": "Content",
|
|
4156
|
+
"text": ";"
|
|
4157
|
+
}
|
|
4158
|
+
],
|
|
4159
|
+
"isReadonly": true,
|
|
4160
|
+
"isOptional": true,
|
|
4161
|
+
"releaseTag": "Public",
|
|
4162
|
+
"name": "readonly",
|
|
4163
|
+
"propertyTypeTokenRange": {
|
|
4164
|
+
"startIndex": 1,
|
|
4165
|
+
"endIndex": 2
|
|
4166
|
+
}
|
|
4167
|
+
},
|
|
4168
|
+
{
|
|
4169
|
+
"kind": "PropertySignature",
|
|
4170
|
+
"canonicalReference": "@alloy-js/csharp!FieldModifiers#static:member",
|
|
4171
|
+
"docComment": "",
|
|
4172
|
+
"excerptTokens": [
|
|
4173
|
+
{
|
|
4174
|
+
"kind": "Content",
|
|
4175
|
+
"text": "readonly static?: "
|
|
4176
|
+
},
|
|
4177
|
+
{
|
|
4178
|
+
"kind": "Content",
|
|
4179
|
+
"text": "boolean"
|
|
4180
|
+
},
|
|
4181
|
+
{
|
|
4182
|
+
"kind": "Content",
|
|
4183
|
+
"text": ";"
|
|
4184
|
+
}
|
|
4185
|
+
],
|
|
4186
|
+
"isReadonly": true,
|
|
4187
|
+
"isOptional": true,
|
|
4188
|
+
"releaseTag": "Public",
|
|
4189
|
+
"name": "static",
|
|
4190
|
+
"propertyTypeTokenRange": {
|
|
4191
|
+
"startIndex": 1,
|
|
4192
|
+
"endIndex": 2
|
|
4193
|
+
}
|
|
4194
|
+
},
|
|
4195
|
+
{
|
|
4196
|
+
"kind": "PropertySignature",
|
|
4197
|
+
"canonicalReference": "@alloy-js/csharp!FieldModifiers#volatile:member",
|
|
4198
|
+
"docComment": "",
|
|
4199
|
+
"excerptTokens": [
|
|
4200
|
+
{
|
|
4201
|
+
"kind": "Content",
|
|
4202
|
+
"text": "readonly volatile?: "
|
|
4203
|
+
},
|
|
4204
|
+
{
|
|
4205
|
+
"kind": "Content",
|
|
4206
|
+
"text": "boolean"
|
|
4207
|
+
},
|
|
4208
|
+
{
|
|
4209
|
+
"kind": "Content",
|
|
4210
|
+
"text": ";"
|
|
4211
|
+
}
|
|
4212
|
+
],
|
|
4213
|
+
"isReadonly": true,
|
|
4214
|
+
"isOptional": true,
|
|
4215
|
+
"releaseTag": "Public",
|
|
4216
|
+
"name": "volatile",
|
|
4217
|
+
"propertyTypeTokenRange": {
|
|
4218
|
+
"startIndex": 1,
|
|
4219
|
+
"endIndex": 2
|
|
4220
|
+
}
|
|
4221
|
+
}
|
|
4222
|
+
],
|
|
4223
|
+
"extendsTokenRanges": []
|
|
4224
|
+
},
|
|
4099
4225
|
{
|
|
4100
4226
|
"kind": "Interface",
|
|
4101
4227
|
"canonicalReference": "@alloy-js/csharp!FieldProps:interface",
|
|
@@ -4110,6 +4236,15 @@
|
|
|
4110
4236
|
"text": "AccessModifiers",
|
|
4111
4237
|
"canonicalReference": "@alloy-js/csharp!AccessModifiers:interface"
|
|
4112
4238
|
},
|
|
4239
|
+
{
|
|
4240
|
+
"kind": "Content",
|
|
4241
|
+
"text": ", "
|
|
4242
|
+
},
|
|
4243
|
+
{
|
|
4244
|
+
"kind": "Reference",
|
|
4245
|
+
"text": "FieldModifiers",
|
|
4246
|
+
"canonicalReference": "@alloy-js/csharp!FieldModifiers:interface"
|
|
4247
|
+
},
|
|
4113
4248
|
{
|
|
4114
4249
|
"kind": "Content",
|
|
4115
4250
|
"text": " "
|
|
@@ -4236,6 +4371,10 @@
|
|
|
4236
4371
|
{
|
|
4237
4372
|
"startIndex": 1,
|
|
4238
4373
|
"endIndex": 2
|
|
4374
|
+
},
|
|
4375
|
+
{
|
|
4376
|
+
"startIndex": 3,
|
|
4377
|
+
"endIndex": 4
|
|
4239
4378
|
}
|
|
4240
4379
|
]
|
|
4241
4380
|
},
|
|
@@ -94,10 +94,10 @@ describe("base", () => {
|
|
|
94
94
|
it("declares class with some members", () => {
|
|
95
95
|
const res = utils.toSourceText(
|
|
96
96
|
<csharp.ClassDeclaration public name="TestClass">
|
|
97
|
-
<
|
|
97
|
+
<List>
|
|
98
98
|
<Field public name="MemberOne" type="string" />
|
|
99
|
-
<Field
|
|
100
|
-
</
|
|
99
|
+
<Field public name="MemberTwo" type="int" />
|
|
100
|
+
</List>
|
|
101
101
|
</csharp.ClassDeclaration>,
|
|
102
102
|
);
|
|
103
103
|
|
|
@@ -107,7 +107,7 @@ it("declares class with some members", () => {
|
|
|
107
107
|
public class TestClass
|
|
108
108
|
{
|
|
109
109
|
public string MemberOne;
|
|
110
|
-
|
|
110
|
+
public int MemberTwo;
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
`);
|
|
@@ -180,7 +180,6 @@ it("uses refkeys for members, params, and return type", () => {
|
|
|
180
180
|
<hbr />
|
|
181
181
|
<csharp.ClassDeclaration public name="TestClass">
|
|
182
182
|
<Field private name="MemberOne" type={enumTypeRefkey} />
|
|
183
|
-
;
|
|
184
183
|
<hbr />
|
|
185
184
|
<csharp.Method
|
|
186
185
|
public
|
|
@@ -208,7 +207,7 @@ it("uses refkeys for members, params, and return type", () => {
|
|
|
208
207
|
public class TestResult;
|
|
209
208
|
public class TestClass
|
|
210
209
|
{
|
|
211
|
-
private TestEnum
|
|
210
|
+
private TestEnum _memberOne;
|
|
212
211
|
public TestResult MethodOne(int intParam, TestInput bodyParam)
|
|
213
212
|
{
|
|
214
213
|
return new TestResult();
|
|
@@ -344,9 +343,9 @@ it("declares class with constructor params and assigns values to fields", () =>
|
|
|
344
343
|
const res = utils.toSourceText(
|
|
345
344
|
<csharp.ClassDeclaration public name="TestClass">
|
|
346
345
|
<Field private name="name" type="string" refkey={thisNameRefkey} />
|
|
347
|
-
|
|
346
|
+
<hbr />
|
|
348
347
|
<Field private name="size" type="int" refkey={thisSizeRefkey} />
|
|
349
|
-
|
|
348
|
+
<hbr />
|
|
350
349
|
<Constructor public parameters={ctorParams}>
|
|
351
350
|
{thisNameRefkey} = {paramNameRefkey};<hbr />
|
|
352
351
|
{thisSizeRefkey} = {paramSizeRefkey};
|
|
@@ -361,12 +360,12 @@ it("declares class with constructor params and assigns values to fields", () =>
|
|
|
361
360
|
{
|
|
362
361
|
public class TestClass
|
|
363
362
|
{
|
|
364
|
-
private string
|
|
365
|
-
private int
|
|
363
|
+
private string _name;
|
|
364
|
+
private int _size;
|
|
366
365
|
public TestClass(string name, int size)
|
|
367
366
|
{
|
|
368
|
-
|
|
369
|
-
|
|
367
|
+
_name = name;
|
|
368
|
+
_size = size;
|
|
370
369
|
}
|
|
371
370
|
}
|
|
372
371
|
}
|
|
@@ -396,7 +395,7 @@ it("supports class member doc comments", () => {
|
|
|
396
395
|
class Test
|
|
397
396
|
{
|
|
398
397
|
/// This is a member
|
|
399
|
-
public int Member
|
|
398
|
+
public int Member;
|
|
400
399
|
}
|
|
401
400
|
`);
|
|
402
401
|
});
|