@alloy-js/csharp 0.19.0-dev.4 → 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/ClassDeclaration.d.ts +0 -8
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.js +1 -31
- package/dist/src/components/field/field.d.ts +19 -0
- package/dist/src/components/field/field.d.ts.map +1 -0
- package/dist/src/components/field/field.js +38 -0
- package/dist/src/components/field/field.test.d.ts +2 -0
- package/dist/src/components/field/field.test.d.ts.map +1 -0
- package/dist/src/components/field/field.test.js +143 -0
- package/dist/src/components/index.d.ts +1 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +1 -0
- package/dist/src/components/stc/index.d.ts +1 -1
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +1 -1
- package/dist/src/components/struct/declaration.d.ts +2 -0
- package/dist/src/components/struct/declaration.d.ts.map +1 -1
- package/dist/src/components/struct/declaration.js +6 -2
- package/dist/src/components/struct/declaration.test.js +44 -0
- 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 +19 -18
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/components/ClassDeclaration.tsx +2 -40
- package/src/components/field/field.test.tsx +122 -0
- package/src/components/field/field.tsx +70 -0
- package/src/components/index.ts +1 -0
- package/src/components/stc/index.ts +1 -1
- package/src/components/struct/declaration.test.tsx +30 -0
- package/src/components/struct/declaration.tsx +11 -0
- package/src/name-policy.ts +2 -0
- package/temp/api.json +361 -190
- package/test/class-declaration.test.tsx +19 -38
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { List } from "@alloy-js/core";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { TestNamespace } from "../../../test/utils.jsx";
|
|
4
|
+
import { ClassDeclaration } from "../ClassDeclaration.jsx";
|
|
5
|
+
import { Field } from "./field.jsx";
|
|
6
|
+
|
|
7
|
+
function Wrapper(props: { children: any }) {
|
|
8
|
+
return (
|
|
9
|
+
<TestNamespace>
|
|
10
|
+
<ClassDeclaration public name="TestClass">
|
|
11
|
+
{props.children}
|
|
12
|
+
</ClassDeclaration>
|
|
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>,
|
|
24
|
+
).toRenderTo(`
|
|
25
|
+
public class TestClass
|
|
26
|
+
{
|
|
27
|
+
public string MemberOne;
|
|
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;
|
|
119
|
+
}
|
|
120
|
+
`);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Children, Declaration, Name, refkey, Refkey } from "@alloy-js/core";
|
|
2
|
+
import {
|
|
3
|
+
AccessModifiers,
|
|
4
|
+
computeModifiersPrefix,
|
|
5
|
+
getAccessModifier,
|
|
6
|
+
makeModifiers,
|
|
7
|
+
} from "../../modifiers.js";
|
|
8
|
+
import { CSharpElements, useCSharpNamePolicy } from "../../name-policy.js";
|
|
9
|
+
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
10
|
+
import { useCSharpScope } from "../../symbols/scopes.js";
|
|
11
|
+
import { DocWhen } from "../doc/comment.jsx";
|
|
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 {
|
|
29
|
+
name: string;
|
|
30
|
+
type: Children;
|
|
31
|
+
refkey?: Refkey;
|
|
32
|
+
/** Doc comment */
|
|
33
|
+
doc?: Children;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Render a c# field */
|
|
37
|
+
export function Field(props: FieldProps) {
|
|
38
|
+
let nameElement: CSharpElements = "class-member-private";
|
|
39
|
+
if (props.public || props.protected || props.internal) {
|
|
40
|
+
nameElement = "class-member-public";
|
|
41
|
+
}
|
|
42
|
+
const name = useCSharpNamePolicy().getName(props.name, nameElement);
|
|
43
|
+
const scope = useCSharpScope();
|
|
44
|
+
if (
|
|
45
|
+
scope.kind !== "member" ||
|
|
46
|
+
(scope.name !== "class-decl" && scope.name !== "struct-decl")
|
|
47
|
+
) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
"can't define a class member outside of a class or struct scope",
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const memberSymbol = new CSharpOutputSymbol(name, {
|
|
54
|
+
scope,
|
|
55
|
+
refkeys: props.refkey ?? refkey(props.name),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const modifiers = computeModifiersPrefix([
|
|
59
|
+
getAccessModifier(props),
|
|
60
|
+
getModifiers(props),
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<Declaration symbol={memberSymbol}>
|
|
65
|
+
<DocWhen doc={props.doc} />
|
|
66
|
+
{modifiers}
|
|
67
|
+
{props.type} <Name />;
|
|
68
|
+
</Declaration>
|
|
69
|
+
);
|
|
70
|
+
}
|
package/src/components/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./Declaration.js";
|
|
|
5
5
|
export * from "./doc/comment.jsx";
|
|
6
6
|
export * from "./doc/from-markdown.jsx";
|
|
7
7
|
export * from "./EnumDeclaration.jsx";
|
|
8
|
+
export * from "./field/field.jsx";
|
|
8
9
|
export * from "./interface/declaration.js";
|
|
9
10
|
export * from "./interface/method.js";
|
|
10
11
|
export * from "./interface/property.js";
|
|
@@ -3,7 +3,7 @@ import * as base from "../index.js";
|
|
|
3
3
|
|
|
4
4
|
export const ClassDeclaration = core.stc(base.ClassDeclaration);
|
|
5
5
|
export const Constructor = core.stc(base.Constructor);
|
|
6
|
-
export const
|
|
6
|
+
export const Field = core.stc(base.Field);
|
|
7
7
|
export const ClassMethod = core.stc(base.Method);
|
|
8
8
|
export const EnumDeclaration = core.stc(base.EnumDeclaration);
|
|
9
9
|
export const EnumMember = core.stc(base.EnumMember);
|
|
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vitest";
|
|
|
3
3
|
import { TestNamespace } from "../../../test/utils.jsx";
|
|
4
4
|
import { Attribute } from "../attributes/attributes.jsx";
|
|
5
5
|
import { Constructor } from "../constructor/constructor.jsx";
|
|
6
|
+
import { Field } from "../field/field.jsx";
|
|
6
7
|
import { Method } from "../method/method.jsx";
|
|
7
8
|
import { Property } from "../property/property.jsx";
|
|
8
9
|
import { SourceFile } from "../SourceFile.jsx";
|
|
@@ -167,3 +168,32 @@ it("define constructor", () => {
|
|
|
167
168
|
}
|
|
168
169
|
`);
|
|
169
170
|
});
|
|
171
|
+
|
|
172
|
+
it("defines fields", () => {
|
|
173
|
+
expect(
|
|
174
|
+
<TestNamespace>
|
|
175
|
+
<StructDeclaration public name="TestClass">
|
|
176
|
+
<List>
|
|
177
|
+
<Field public name="MemberOne" type="string" />
|
|
178
|
+
<Field public name="MemberTwo" type="int" />
|
|
179
|
+
</List>
|
|
180
|
+
</StructDeclaration>
|
|
181
|
+
</TestNamespace>,
|
|
182
|
+
).toRenderTo(`
|
|
183
|
+
public struct TestClass
|
|
184
|
+
{
|
|
185
|
+
public string MemberOne;
|
|
186
|
+
public int MemberTwo;
|
|
187
|
+
}
|
|
188
|
+
`);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("define multiple interface types", () => {
|
|
192
|
+
expect(
|
|
193
|
+
<TestNamespace>
|
|
194
|
+
<StructDeclaration name="Test" interfaceTypes={["Foo", "Bar"]} />
|
|
195
|
+
</TestNamespace>,
|
|
196
|
+
).toRenderTo(`
|
|
197
|
+
struct Test : Foo, Bar;
|
|
198
|
+
`);
|
|
199
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as core from "@alloy-js/core";
|
|
2
|
+
import { join } from "@alloy-js/core";
|
|
2
3
|
import {
|
|
3
4
|
AccessModifiers,
|
|
4
5
|
computeModifiersPrefix,
|
|
@@ -71,6 +72,9 @@ export interface StructDeclarationProps
|
|
|
71
72
|
* ```
|
|
72
73
|
*/
|
|
73
74
|
attributes?: AttributesProp;
|
|
75
|
+
|
|
76
|
+
/** Interfaces this struct implements */
|
|
77
|
+
interfaceTypes?: core.Children[];
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
/**
|
|
@@ -108,6 +112,12 @@ export function StructDeclaration(props: StructDeclarationProps) {
|
|
|
108
112
|
getAccessModifier(props),
|
|
109
113
|
getStructModifiers(props),
|
|
110
114
|
]);
|
|
115
|
+
|
|
116
|
+
const base =
|
|
117
|
+
props.interfaceTypes && props.interfaceTypes.length > 0 ?
|
|
118
|
+
<> : {join(props.interfaceTypes, { joiner: ", " })}</>
|
|
119
|
+
: null;
|
|
120
|
+
|
|
111
121
|
return (
|
|
112
122
|
<core.Declaration symbol={thisStructSymbol}>
|
|
113
123
|
<DocWhen doc={props.doc} />
|
|
@@ -116,6 +126,7 @@ export function StructDeclaration(props: StructDeclarationProps) {
|
|
|
116
126
|
{props.typeParameters && (
|
|
117
127
|
<TypeParameters parameters={props.typeParameters} />
|
|
118
128
|
)}
|
|
129
|
+
{base}
|
|
119
130
|
{props.typeParameters && (
|
|
120
131
|
<TypeParameterConstraints parameters={props.typeParameters} />
|
|
121
132
|
)}
|
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
|
}
|