@alloy-js/csharp 0.18.0-dev.19 → 0.18.0-dev.2
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} +32 -59
- package/dist/src/components/ClassMethod.d.ts +4 -25
- package/dist/src/components/ClassMethod.d.ts.map +1 -1
- package/dist/src/components/ClassMethod.js +3 -22
- 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 +1 -6
- package/dist/src/components/Parameters.d.ts.map +1 -1
- package/dist/src/components/Parameters.js +2 -3
- package/dist/src/components/index.d.ts +3 -12
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +4 -13
- 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 +21 -21
- package/src/components/{ClassDeclaration.tsx → Class.tsx} +43 -108
- package/src/components/ClassMethod.tsx +12 -53
- package/src/components/{EnumDeclaration.tsx → Enum.tsx} +6 -30
- package/src/components/Parameters.tsx +3 -10
- package/src/components/index.ts +3 -12
- 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 +714 -5256
- 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/LICENSE +0 -7
- 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 -69
- 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 -254
- 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/property/property.d.ts +0 -57
- 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 -65
- 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 -26
- 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/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 -250
- package/src/components/interface/method.tsx +0 -105
- package/src/components/interface/property.test.tsx +0 -144
- package/src/components/interface/property.tsx +0 -107
- package/src/components/property/property.test.tsx +0 -187
- package/src/components/property/property.tsx +0 -146
- 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 -19
- package/src/components/type-parameters/type-parameters.tsx +0 -72
- 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,144 +0,0 @@
|
|
|
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 { InterfaceDeclaration } from "./declaration.jsx";
|
|
5
|
-
import { InterfaceProperty } from "./property.jsx";
|
|
6
|
-
|
|
7
|
-
const Wrapper = (props: { children: Children }) => (
|
|
8
|
-
<TestNamespace>
|
|
9
|
-
<InterfaceDeclaration public name="TestInterface">
|
|
10
|
-
{props.children}
|
|
11
|
-
</InterfaceDeclaration>
|
|
12
|
-
</TestNamespace>
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
describe("modifiers", () => {
|
|
16
|
-
describe("access modifiers", () => {
|
|
17
|
-
it.each(["public", "private", "protected", "internal"] as const)(
|
|
18
|
-
"%s",
|
|
19
|
-
(accessModifier) => {
|
|
20
|
-
expect(
|
|
21
|
-
<Wrapper>
|
|
22
|
-
<InterfaceProperty
|
|
23
|
-
{...{ [accessModifier]: true }}
|
|
24
|
-
name="TestProp"
|
|
25
|
-
type="string"
|
|
26
|
-
get
|
|
27
|
-
/>
|
|
28
|
-
</Wrapper>,
|
|
29
|
-
).toRenderTo(`
|
|
30
|
-
public interface TestInterface
|
|
31
|
-
{
|
|
32
|
-
${accessModifier} string TestProp { get; }
|
|
33
|
-
}
|
|
34
|
-
`);
|
|
35
|
-
},
|
|
36
|
-
);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
describe("method modifiers", () => {
|
|
40
|
-
it.each(["new"] as const)("%s", (methodModifier) => {
|
|
41
|
-
expect(
|
|
42
|
-
<Wrapper>
|
|
43
|
-
<InterfaceProperty
|
|
44
|
-
{...{ [methodModifier]: true }}
|
|
45
|
-
name="TestProp"
|
|
46
|
-
type="string"
|
|
47
|
-
get
|
|
48
|
-
/>
|
|
49
|
-
</Wrapper>,
|
|
50
|
-
).toRenderTo(`
|
|
51
|
-
public interface TestInterface
|
|
52
|
-
{
|
|
53
|
-
${methodModifier} string TestProp { get; }
|
|
54
|
-
}
|
|
55
|
-
`);
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("combine modifiers", () => {
|
|
60
|
-
expect(
|
|
61
|
-
<Wrapper>
|
|
62
|
-
<InterfaceProperty public new name="TestProp" type="string" get />
|
|
63
|
-
</Wrapper>,
|
|
64
|
-
).toRenderTo(`
|
|
65
|
-
public interface TestInterface
|
|
66
|
-
{
|
|
67
|
-
public new string TestProp { get; }
|
|
68
|
-
}
|
|
69
|
-
`);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("applies PascalCase naming policy", () => {
|
|
74
|
-
expect(
|
|
75
|
-
<Wrapper>
|
|
76
|
-
<InterfaceProperty name="test_prop" type="string" get />
|
|
77
|
-
</Wrapper>,
|
|
78
|
-
).toRenderTo(`
|
|
79
|
-
public interface TestInterface
|
|
80
|
-
{
|
|
81
|
-
string TestProp { get; }
|
|
82
|
-
}
|
|
83
|
-
`);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it("has getter only", () => {
|
|
87
|
-
expect(
|
|
88
|
-
<Wrapper>
|
|
89
|
-
<InterfaceProperty name="TestProp" type="string" get />
|
|
90
|
-
</Wrapper>,
|
|
91
|
-
).toRenderTo(`
|
|
92
|
-
public interface TestInterface
|
|
93
|
-
{
|
|
94
|
-
string TestProp { get; }
|
|
95
|
-
}
|
|
96
|
-
`);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it("has setter only", () => {
|
|
100
|
-
expect(
|
|
101
|
-
<Wrapper>
|
|
102
|
-
<InterfaceProperty name="TestProp" type="string" set />
|
|
103
|
-
</Wrapper>,
|
|
104
|
-
).toRenderTo(`
|
|
105
|
-
public interface TestInterface
|
|
106
|
-
{
|
|
107
|
-
string TestProp { set; }
|
|
108
|
-
}
|
|
109
|
-
`);
|
|
110
|
-
});
|
|
111
|
-
it("has getter and setter", () => {
|
|
112
|
-
expect(
|
|
113
|
-
<Wrapper>
|
|
114
|
-
<InterfaceProperty name="TestProp" type="string" get set />
|
|
115
|
-
</Wrapper>,
|
|
116
|
-
).toRenderTo(`
|
|
117
|
-
public interface TestInterface
|
|
118
|
-
{
|
|
119
|
-
string TestProp { get; set; }
|
|
120
|
-
}
|
|
121
|
-
`);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it("specify doc comment", () => {
|
|
125
|
-
expect(
|
|
126
|
-
<TestNamespace>
|
|
127
|
-
<InterfaceDeclaration name="Test">
|
|
128
|
-
<InterfaceProperty
|
|
129
|
-
name="Method"
|
|
130
|
-
type="string"
|
|
131
|
-
get
|
|
132
|
-
set
|
|
133
|
-
doc="This is a test"
|
|
134
|
-
/>
|
|
135
|
-
</InterfaceDeclaration>
|
|
136
|
-
</TestNamespace>,
|
|
137
|
-
).toRenderTo(`
|
|
138
|
-
interface Test
|
|
139
|
-
{
|
|
140
|
-
/// This is a test
|
|
141
|
-
string Method { get; set; }
|
|
142
|
-
}
|
|
143
|
-
`);
|
|
144
|
-
});
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Block,
|
|
3
|
-
Children,
|
|
4
|
-
List,
|
|
5
|
-
MemberDeclaration,
|
|
6
|
-
refkey,
|
|
7
|
-
Refkey,
|
|
8
|
-
Scope,
|
|
9
|
-
} from "@alloy-js/core";
|
|
10
|
-
import {
|
|
11
|
-
AccessModifiers,
|
|
12
|
-
computeModifiersPrefix,
|
|
13
|
-
getAccessModifier,
|
|
14
|
-
makeModifiers,
|
|
15
|
-
} from "../../modifiers.js";
|
|
16
|
-
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
17
|
-
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
18
|
-
import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
|
|
19
|
-
import { DocWhen } from "../doc/comment.jsx";
|
|
20
|
-
|
|
21
|
-
/** Method modifiers. Can only be one. */
|
|
22
|
-
export interface InterfacePropertyModifiers {
|
|
23
|
-
readonly new?: boolean;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const getModifiers = makeModifiers<InterfacePropertyModifiers>(["new"]);
|
|
27
|
-
|
|
28
|
-
// properties for creating a method
|
|
29
|
-
export interface InterfacePropertyProps
|
|
30
|
-
extends AccessModifiers,
|
|
31
|
-
InterfacePropertyModifiers {
|
|
32
|
-
name: string;
|
|
33
|
-
refkey?: Refkey;
|
|
34
|
-
|
|
35
|
-
/** Property type */
|
|
36
|
-
type: Children;
|
|
37
|
-
|
|
38
|
-
/** If property should have a getter */
|
|
39
|
-
get?: boolean;
|
|
40
|
-
|
|
41
|
-
/** If property should have a setter */
|
|
42
|
-
set?: boolean;
|
|
43
|
-
|
|
44
|
-
/** Doc comment */
|
|
45
|
-
doc?: Children;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Property initializer
|
|
49
|
-
* @example `<ClassProperty name="My" get set nullable />`
|
|
50
|
-
*
|
|
51
|
-
* ```cs
|
|
52
|
-
* int? My { get; set; };
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
nullable?: boolean;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Render a C# interface property.
|
|
60
|
-
*
|
|
61
|
-
* @example `<InterfaceProperty public name="My" get set />`
|
|
62
|
-
*
|
|
63
|
-
* ```cs
|
|
64
|
-
* public int My { get; set; };
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export function InterfaceProperty(props: InterfacePropertyProps) {
|
|
68
|
-
const name = useCSharpNamePolicy().getName(props.name, "class-property");
|
|
69
|
-
const scope = useCSharpScope();
|
|
70
|
-
if (scope.kind !== "member" || scope.name !== "interface-decl") {
|
|
71
|
-
throw new Error(
|
|
72
|
-
"can't define an interface method outside of an interface scope",
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const propertySymbol = new CSharpOutputSymbol(name, {
|
|
77
|
-
scope,
|
|
78
|
-
refkeys: props.refkey ?? refkey(props.name),
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// scope for property declaration
|
|
82
|
-
const propertyScope = new CSharpMemberScope("property-decl", {
|
|
83
|
-
owner: propertySymbol,
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
const modifiers = computeModifiersPrefix([
|
|
87
|
-
getAccessModifier(props),
|
|
88
|
-
getModifiers(props),
|
|
89
|
-
]);
|
|
90
|
-
// note that scope wraps the method decl so that the params get the correct scope
|
|
91
|
-
return (
|
|
92
|
-
<MemberDeclaration symbol={propertySymbol}>
|
|
93
|
-
<Scope value={propertyScope}>
|
|
94
|
-
<DocWhen doc={props.doc} />
|
|
95
|
-
{modifiers}
|
|
96
|
-
{props.type}
|
|
97
|
-
{props.nullable && "?"} {name}{" "}
|
|
98
|
-
<Block newline inline>
|
|
99
|
-
<List joiner=" ">
|
|
100
|
-
{props.get && "get;"}
|
|
101
|
-
{props.set && "set;"}
|
|
102
|
-
</List>
|
|
103
|
-
</Block>
|
|
104
|
-
</Scope>
|
|
105
|
-
</MemberDeclaration>
|
|
106
|
-
);
|
|
107
|
-
}
|
|
@@ -1,187 +0,0 @@
|
|
|
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 { ClassDeclaration } from "../ClassDeclaration.jsx";
|
|
5
|
-
import { Property } from "./property.jsx";
|
|
6
|
-
|
|
7
|
-
const Wrapper = (props: { children: Children }) => (
|
|
8
|
-
<TestNamespace>
|
|
9
|
-
<ClassDeclaration public name="TestClass">
|
|
10
|
-
{props.children}
|
|
11
|
-
</ClassDeclaration>
|
|
12
|
-
</TestNamespace>
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
describe("modifiers", () => {
|
|
16
|
-
describe("access modifiers", () => {
|
|
17
|
-
it.each(["public", "private", "protected", "internal"] as const)(
|
|
18
|
-
"%s",
|
|
19
|
-
(accessModifier) => {
|
|
20
|
-
expect(
|
|
21
|
-
<Wrapper>
|
|
22
|
-
<Property
|
|
23
|
-
{...{ [accessModifier]: true }}
|
|
24
|
-
name="TestProp"
|
|
25
|
-
type="string"
|
|
26
|
-
get
|
|
27
|
-
/>
|
|
28
|
-
</Wrapper>,
|
|
29
|
-
).toRenderTo(`
|
|
30
|
-
public class TestClass
|
|
31
|
-
{
|
|
32
|
-
${accessModifier} string TestProp { get; }
|
|
33
|
-
}
|
|
34
|
-
`);
|
|
35
|
-
},
|
|
36
|
-
);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
describe("property modifiers", () => {
|
|
40
|
-
it.each([
|
|
41
|
-
"new",
|
|
42
|
-
"static",
|
|
43
|
-
"virtual",
|
|
44
|
-
"sealed",
|
|
45
|
-
"override",
|
|
46
|
-
"abstract",
|
|
47
|
-
"extern",
|
|
48
|
-
"readonly",
|
|
49
|
-
] as const)("%s", (methodModifier) => {
|
|
50
|
-
expect(
|
|
51
|
-
<Wrapper>
|
|
52
|
-
<Property
|
|
53
|
-
{...{ [methodModifier]: true }}
|
|
54
|
-
name="TestProp"
|
|
55
|
-
type="string"
|
|
56
|
-
get
|
|
57
|
-
/>
|
|
58
|
-
</Wrapper>,
|
|
59
|
-
).toRenderTo(`
|
|
60
|
-
public class TestClass
|
|
61
|
-
{
|
|
62
|
-
${methodModifier} string TestProp { get; }
|
|
63
|
-
}
|
|
64
|
-
`);
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it("combine modifiers", () => {
|
|
69
|
-
expect(
|
|
70
|
-
<Wrapper>
|
|
71
|
-
<Property public new name="TestProp" type="string" get />
|
|
72
|
-
</Wrapper>,
|
|
73
|
-
).toRenderTo(`
|
|
74
|
-
public class TestClass
|
|
75
|
-
{
|
|
76
|
-
public new string TestProp { get; }
|
|
77
|
-
}
|
|
78
|
-
`);
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it("applies PascalCase naming policy", () => {
|
|
83
|
-
expect(
|
|
84
|
-
<Wrapper>
|
|
85
|
-
<Property name="test_prop" type="string" get />
|
|
86
|
-
</Wrapper>,
|
|
87
|
-
).toRenderTo(`
|
|
88
|
-
public class TestClass
|
|
89
|
-
{
|
|
90
|
-
string TestProp { get; }
|
|
91
|
-
}
|
|
92
|
-
`);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("has getter only", () => {
|
|
96
|
-
expect(
|
|
97
|
-
<Wrapper>
|
|
98
|
-
<Property name="TestProp" type="string" get />
|
|
99
|
-
</Wrapper>,
|
|
100
|
-
).toRenderTo(`
|
|
101
|
-
public class TestClass
|
|
102
|
-
{
|
|
103
|
-
string TestProp { get; }
|
|
104
|
-
}
|
|
105
|
-
`);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it("has setter only", () => {
|
|
109
|
-
expect(
|
|
110
|
-
<Wrapper>
|
|
111
|
-
<Property name="TestProp" type="string" set />
|
|
112
|
-
</Wrapper>,
|
|
113
|
-
).toRenderTo(`
|
|
114
|
-
public class TestClass
|
|
115
|
-
{
|
|
116
|
-
string TestProp { set; }
|
|
117
|
-
}
|
|
118
|
-
`);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it("has getter and setter", () => {
|
|
122
|
-
expect(
|
|
123
|
-
<Wrapper>
|
|
124
|
-
<Property name="TestProp" type="string" get set />
|
|
125
|
-
</Wrapper>,
|
|
126
|
-
).toRenderTo(`
|
|
127
|
-
public class TestClass
|
|
128
|
-
{
|
|
129
|
-
string TestProp { get; set; }
|
|
130
|
-
}
|
|
131
|
-
`);
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it("has getter and init", () => {
|
|
135
|
-
expect(
|
|
136
|
-
<Wrapper>
|
|
137
|
-
<Property name="TestProp" type="string" get init />
|
|
138
|
-
</Wrapper>,
|
|
139
|
-
).toRenderTo(`
|
|
140
|
-
public class TestClass
|
|
141
|
-
{
|
|
142
|
-
string TestProp { get; init; }
|
|
143
|
-
}
|
|
144
|
-
`);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it("specify doc comment", () => {
|
|
148
|
-
expect(
|
|
149
|
-
<TestNamespace>
|
|
150
|
-
<ClassDeclaration name="Test">
|
|
151
|
-
<Property name="Method" type="string" get set doc="This is a test" />
|
|
152
|
-
</ClassDeclaration>
|
|
153
|
-
</TestNamespace>,
|
|
154
|
-
).toRenderTo(`
|
|
155
|
-
class Test
|
|
156
|
-
{
|
|
157
|
-
/// This is a test
|
|
158
|
-
string Method { get; set; }
|
|
159
|
-
}
|
|
160
|
-
`);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it("specify nullable property", () => {
|
|
164
|
-
expect(
|
|
165
|
-
<Wrapper>
|
|
166
|
-
<Property name="TestProp" type="string" nullable get set />
|
|
167
|
-
</Wrapper>,
|
|
168
|
-
).toRenderTo(`
|
|
169
|
-
public class TestClass
|
|
170
|
-
{
|
|
171
|
-
string? TestProp { get; set; }
|
|
172
|
-
}
|
|
173
|
-
`);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it("specify initializer", () => {
|
|
177
|
-
expect(
|
|
178
|
-
<Wrapper>
|
|
179
|
-
<Property name="TestProp" type="string" get set initializer={`"abc"`} />
|
|
180
|
-
</Wrapper>,
|
|
181
|
-
).toRenderTo(`
|
|
182
|
-
public class TestClass
|
|
183
|
-
{
|
|
184
|
-
string TestProp { get; set; } = "abc";
|
|
185
|
-
}
|
|
186
|
-
`);
|
|
187
|
-
});
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Block,
|
|
3
|
-
Children,
|
|
4
|
-
code,
|
|
5
|
-
List,
|
|
6
|
-
MemberDeclaration,
|
|
7
|
-
refkey,
|
|
8
|
-
Refkey,
|
|
9
|
-
Scope,
|
|
10
|
-
} from "@alloy-js/core";
|
|
11
|
-
import {
|
|
12
|
-
AccessModifiers,
|
|
13
|
-
computeModifiersPrefix,
|
|
14
|
-
getAccessModifier,
|
|
15
|
-
makeModifiers,
|
|
16
|
-
} from "../../modifiers.js";
|
|
17
|
-
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
18
|
-
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
19
|
-
import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
|
|
20
|
-
import { DocWhen } from "../doc/comment.jsx";
|
|
21
|
-
|
|
22
|
-
/** Property modifiers. */
|
|
23
|
-
export interface PropertyModifiers {
|
|
24
|
-
readonly new?: boolean;
|
|
25
|
-
readonly static?: boolean;
|
|
26
|
-
readonly virtual?: boolean;
|
|
27
|
-
readonly sealed?: boolean;
|
|
28
|
-
readonly override?: boolean;
|
|
29
|
-
readonly abstract?: boolean;
|
|
30
|
-
readonly extern?: boolean;
|
|
31
|
-
readonly readonly?: boolean;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const getModifiers = makeModifiers<PropertyModifiers>([
|
|
35
|
-
"new",
|
|
36
|
-
"static",
|
|
37
|
-
"virtual",
|
|
38
|
-
"sealed",
|
|
39
|
-
"override",
|
|
40
|
-
"abstract",
|
|
41
|
-
"extern",
|
|
42
|
-
"readonly",
|
|
43
|
-
]);
|
|
44
|
-
|
|
45
|
-
/** Properties for {@link Property} component */
|
|
46
|
-
export interface PropertyProps extends AccessModifiers, PropertyModifiers {
|
|
47
|
-
name: string;
|
|
48
|
-
refkey?: Refkey;
|
|
49
|
-
|
|
50
|
-
/** Property type */
|
|
51
|
-
type: Children;
|
|
52
|
-
|
|
53
|
-
/** If property should have a getter */
|
|
54
|
-
get?: boolean;
|
|
55
|
-
|
|
56
|
-
/** If property should have a setter */
|
|
57
|
-
set?: boolean;
|
|
58
|
-
|
|
59
|
-
/** If property should only be set on the type creation */
|
|
60
|
-
init?: boolean;
|
|
61
|
-
|
|
62
|
-
/** Doc comment */
|
|
63
|
-
doc?: Children;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Property initializer
|
|
67
|
-
* @example `<ClassProperty name="My" get set nullable />`
|
|
68
|
-
*
|
|
69
|
-
* ```cs
|
|
70
|
-
* int? My { get; set; };
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
|
-
nullable?: boolean;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Property initializer
|
|
77
|
-
* @example `<ClassProperty name="My" get set init={42} />`
|
|
78
|
-
*
|
|
79
|
-
* ```cs
|
|
80
|
-
* int My { get; set; } = 42;
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
initializer?: Children;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Render a C# class property.
|
|
88
|
-
*
|
|
89
|
-
* @example `<ClassProperty public name="My" get set />`
|
|
90
|
-
*
|
|
91
|
-
* ```cs
|
|
92
|
-
* public int My { get; set; };
|
|
93
|
-
* ```
|
|
94
|
-
*/
|
|
95
|
-
export function Property(props: PropertyProps) {
|
|
96
|
-
const name = useCSharpNamePolicy().getName(props.name, "class-property");
|
|
97
|
-
const scope = useCSharpScope();
|
|
98
|
-
if (
|
|
99
|
-
scope.kind !== "member" ||
|
|
100
|
-
(scope.name !== "class-decl" && scope.name !== "record-decl")
|
|
101
|
-
) {
|
|
102
|
-
throw new Error(
|
|
103
|
-
"can't define an interface method outside of an interface scope",
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const propertySymbol = new CSharpOutputSymbol(name, {
|
|
108
|
-
scope,
|
|
109
|
-
refkeys: props.refkey ?? refkey(props.name),
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
// scope for property declaration
|
|
113
|
-
const propertyScope = new CSharpMemberScope("property-decl", {
|
|
114
|
-
owner: propertySymbol,
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
const modifiers = computeModifiersPrefix([
|
|
118
|
-
getAccessModifier(props),
|
|
119
|
-
getModifiers(props),
|
|
120
|
-
]);
|
|
121
|
-
|
|
122
|
-
if (props.init && props.set) {
|
|
123
|
-
throw new Error(
|
|
124
|
-
`Cannot use 'init' and 'set' together on property '${name}'`,
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
// note that scope wraps the method decl so that the params get the correct scope
|
|
128
|
-
return (
|
|
129
|
-
<MemberDeclaration symbol={propertySymbol}>
|
|
130
|
-
<Scope value={propertyScope}>
|
|
131
|
-
<DocWhen doc={props.doc} />
|
|
132
|
-
{modifiers}
|
|
133
|
-
{props.type}
|
|
134
|
-
{props.nullable && "?"} {name}{" "}
|
|
135
|
-
<Block newline inline>
|
|
136
|
-
<List joiner=" ">
|
|
137
|
-
{props.get && "get;"}
|
|
138
|
-
{props.set && "set;"}
|
|
139
|
-
{props.init && "init;"}
|
|
140
|
-
</List>
|
|
141
|
-
</Block>
|
|
142
|
-
{props.initializer && code` = ${props.initializer};`}
|
|
143
|
-
</Scope>
|
|
144
|
-
</MemberDeclaration>
|
|
145
|
-
);
|
|
146
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { TestNamespace } from "../../../test/utils.jsx";
|
|
3
|
-
import { Property } from "../property/property.jsx";
|
|
4
|
-
import { RecordDeclaration } from "./declaration.jsx";
|
|
5
|
-
|
|
6
|
-
it("declares class with no members", () => {
|
|
7
|
-
expect(
|
|
8
|
-
<TestNamespace>
|
|
9
|
-
<RecordDeclaration name="TestRecord" />
|
|
10
|
-
</TestNamespace>,
|
|
11
|
-
).toRenderTo(`
|
|
12
|
-
record TestRecord;
|
|
13
|
-
`);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
describe("modifiers", () => {
|
|
17
|
-
it.each(["public", "private", "internal"])("%s", (mod) => {
|
|
18
|
-
expect(
|
|
19
|
-
<TestNamespace>
|
|
20
|
-
<RecordDeclaration {...{ [mod]: true }} name="TestRecord" />
|
|
21
|
-
</TestNamespace>,
|
|
22
|
-
).toRenderTo(`
|
|
23
|
-
${mod} record TestRecord;
|
|
24
|
-
`);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it.each(["partial"])("%s", (mod) => {
|
|
28
|
-
expect(
|
|
29
|
-
<TestNamespace>
|
|
30
|
-
<RecordDeclaration {...{ [mod]: true }} name="TestRecord" />
|
|
31
|
-
</TestNamespace>,
|
|
32
|
-
).toRenderTo(`
|
|
33
|
-
${mod} record TestRecord;
|
|
34
|
-
`);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("combines modifiers", () => {
|
|
38
|
-
expect(
|
|
39
|
-
<TestNamespace>
|
|
40
|
-
<RecordDeclaration public partial name="TestRecord" />
|
|
41
|
-
</TestNamespace>,
|
|
42
|
-
).toRenderTo(`
|
|
43
|
-
public partial record TestRecord;
|
|
44
|
-
`);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("specify doc comment", () => {
|
|
49
|
-
expect(
|
|
50
|
-
<TestNamespace>
|
|
51
|
-
<RecordDeclaration name="TestRecord" doc="This is a test" />
|
|
52
|
-
</TestNamespace>,
|
|
53
|
-
).toRenderTo(`
|
|
54
|
-
/// This is a test
|
|
55
|
-
record TestRecord;
|
|
56
|
-
`);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("specify class property inside", () => {
|
|
60
|
-
expect(
|
|
61
|
-
<TestNamespace>
|
|
62
|
-
<RecordDeclaration name="TestRecord" doc="This is a test">
|
|
63
|
-
<Property name="Prop" get set type="string" />
|
|
64
|
-
</RecordDeclaration>
|
|
65
|
-
</TestNamespace>,
|
|
66
|
-
).toRenderTo(`
|
|
67
|
-
/// This is a test
|
|
68
|
-
record TestRecord
|
|
69
|
-
{
|
|
70
|
-
string Prop { get; set; }
|
|
71
|
-
}
|
|
72
|
-
`);
|
|
73
|
-
});
|