@alloy-js/csharp 0.18.0-dev.2 → 0.18.0-dev.20

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.
Files changed (153) hide show
  1. package/LICENSE +7 -0
  2. package/dist/src/components/ClassDeclaration.d.ts +74 -0
  3. package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
  4. package/dist/src/components/{Class.js → ClassDeclaration.js} +59 -32
  5. package/dist/src/components/ClassMethod.d.ts +25 -4
  6. package/dist/src/components/ClassMethod.d.ts.map +1 -1
  7. package/dist/src/components/ClassMethod.js +22 -3
  8. package/dist/src/components/EnumDeclaration.d.ts +34 -0
  9. package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
  10. package/dist/src/components/{Enum.js → EnumDeclaration.js} +25 -5
  11. package/dist/src/components/Parameters.d.ts +6 -1
  12. package/dist/src/components/Parameters.d.ts.map +1 -1
  13. package/dist/src/components/Parameters.js +3 -2
  14. package/dist/src/components/doc/comment.d.ts +70 -0
  15. package/dist/src/components/doc/comment.d.ts.map +1 -0
  16. package/dist/src/components/doc/comment.js +88 -0
  17. package/dist/src/components/doc/comment.test.d.ts +2 -0
  18. package/dist/src/components/doc/comment.test.d.ts.map +1 -0
  19. package/dist/src/components/doc/comment.test.js +348 -0
  20. package/dist/src/components/doc/from-markdown.d.ts +6 -0
  21. package/dist/src/components/doc/from-markdown.d.ts.map +1 -0
  22. package/dist/src/components/doc/from-markdown.js +58 -0
  23. package/dist/src/components/doc/from-markdown.test.d.ts +2 -0
  24. package/dist/src/components/doc/from-markdown.test.d.ts.map +1 -0
  25. package/dist/src/components/doc/from-markdown.test.js +83 -0
  26. package/dist/src/components/index.d.ts +12 -3
  27. package/dist/src/components/index.d.ts.map +1 -1
  28. package/dist/src/components/index.js +13 -4
  29. package/dist/src/components/interface/declaration.d.ts +47 -0
  30. package/dist/src/components/interface/declaration.d.ts.map +1 -0
  31. package/dist/src/components/interface/declaration.js +77 -0
  32. package/dist/src/components/interface/declaration.test.d.ts +2 -0
  33. package/dist/src/components/interface/declaration.test.d.ts.map +1 -0
  34. package/dist/src/components/interface/declaration.test.js +153 -0
  35. package/dist/src/components/interface/method.d.ts +32 -0
  36. package/dist/src/components/interface/method.d.ts.map +1 -0
  37. package/dist/src/components/interface/method.js +69 -0
  38. package/dist/src/components/interface/method.test.d.ts +2 -0
  39. package/dist/src/components/interface/method.test.d.ts.map +1 -0
  40. package/dist/src/components/interface/method.test.js +254 -0
  41. package/dist/src/components/interface/property.d.ts +38 -0
  42. package/dist/src/components/interface/property.d.ts.map +1 -0
  43. package/dist/src/components/interface/property.js +67 -0
  44. package/dist/src/components/interface/property.test.d.ts +2 -0
  45. package/dist/src/components/interface/property.test.d.ts.map +1 -0
  46. package/dist/src/components/interface/property.test.js +165 -0
  47. package/dist/src/components/property/property.d.ts +57 -0
  48. package/dist/src/components/property/property.d.ts.map +1 -0
  49. package/dist/src/components/property/property.js +70 -0
  50. package/dist/src/components/property/property.test.d.ts +2 -0
  51. package/dist/src/components/property/property.test.d.ts.map +1 -0
  52. package/dist/src/components/property/property.test.js +218 -0
  53. package/dist/src/components/record/declaration.d.ts +35 -0
  54. package/dist/src/components/record/declaration.d.ts.map +1 -0
  55. package/dist/src/components/record/declaration.js +90 -0
  56. package/dist/src/components/record/declaration.test.d.ts +2 -0
  57. package/dist/src/components/record/declaration.test.d.ts.map +1 -0
  58. package/dist/src/components/record/declaration.test.js +94 -0
  59. package/dist/src/components/stc/index.d.ts +2 -2
  60. package/dist/src/components/stc/index.d.ts.map +1 -1
  61. package/dist/src/components/stc/index.js +2 -2
  62. package/dist/src/components/type-parameters/type-parameter-constraints.d.ts +8 -0
  63. package/dist/src/components/type-parameters/type-parameter-constraints.d.ts.map +1 -0
  64. package/dist/src/components/type-parameters/type-parameter-constraints.js +44 -0
  65. package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts +2 -0
  66. package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts.map +1 -0
  67. package/dist/src/components/type-parameters/type-parameter-constraints.test.js +67 -0
  68. package/dist/src/components/type-parameters/type-parameter.d.ts +20 -0
  69. package/dist/src/components/type-parameters/type-parameter.d.ts.map +1 -0
  70. package/dist/src/components/type-parameters/type-parameter.js +22 -0
  71. package/dist/src/components/type-parameters/type-parameters.d.ts +17 -0
  72. package/dist/src/components/type-parameters/type-parameters.d.ts.map +1 -0
  73. package/dist/src/components/type-parameters/type-parameters.js +54 -0
  74. package/dist/src/components/type-parameters/type-parameters.test.d.ts +2 -0
  75. package/dist/src/components/type-parameters/type-parameters.test.d.ts.map +1 -0
  76. package/dist/src/components/type-parameters/type-parameters.test.js +48 -0
  77. package/dist/src/components/var/declaration.d.ts +35 -0
  78. package/dist/src/components/var/declaration.d.ts.map +1 -0
  79. package/dist/src/components/var/declaration.js +40 -0
  80. package/dist/src/components/var/declaration.test.d.ts +2 -0
  81. package/dist/src/components/var/declaration.test.d.ts.map +1 -0
  82. package/dist/src/components/var/declaration.test.js +73 -0
  83. package/dist/src/modifiers.d.ts +10 -4
  84. package/dist/src/modifiers.d.ts.map +1 -1
  85. package/dist/src/modifiers.js +9 -32
  86. package/dist/src/name-policy.d.ts +1 -1
  87. package/dist/src/name-policy.d.ts.map +1 -1
  88. package/dist/src/name-policy.js +2 -0
  89. package/dist/test/class-declaration.test.d.ts +2 -0
  90. package/dist/test/class-declaration.test.d.ts.map +1 -0
  91. package/dist/test/class-declaration.test.js +463 -0
  92. package/dist/test/class-method.test.js +37 -14
  93. package/dist/test/enum.test.js +12 -12
  94. package/dist/test/namespace.test.js +8 -8
  95. package/dist/test/project-directory.test.d.ts +2 -0
  96. package/dist/test/project-directory.test.d.ts.map +1 -0
  97. package/dist/test/{projectdirectory.test.js → project-directory.test.js} +8 -8
  98. package/dist/test/sourcefile.test.js +4 -4
  99. package/dist/test/using.test.js +9 -9
  100. package/dist/test/vitest.setup.d.ts +2 -0
  101. package/dist/test/vitest.setup.d.ts.map +1 -0
  102. package/dist/test/vitest.setup.js +1 -0
  103. package/dist/tsconfig.tsbuildinfo +1 -1
  104. package/package.json +21 -21
  105. package/src/components/{Class.tsx → ClassDeclaration.tsx} +108 -43
  106. package/src/components/ClassMethod.tsx +53 -12
  107. package/src/components/{Enum.tsx → EnumDeclaration.tsx} +30 -6
  108. package/src/components/Parameters.tsx +10 -3
  109. package/src/components/doc/comment.test.tsx +337 -0
  110. package/src/components/doc/comment.tsx +152 -0
  111. package/src/components/doc/from-markdown.test.tsx +103 -0
  112. package/src/components/doc/from-markdown.tsx +58 -0
  113. package/src/components/index.ts +12 -3
  114. package/src/components/interface/declaration.test.tsx +143 -0
  115. package/src/components/interface/declaration.tsx +105 -0
  116. package/src/components/interface/method.test.tsx +250 -0
  117. package/src/components/interface/method.tsx +105 -0
  118. package/src/components/interface/property.test.tsx +144 -0
  119. package/src/components/interface/property.tsx +107 -0
  120. package/src/components/property/property.test.tsx +187 -0
  121. package/src/components/property/property.tsx +146 -0
  122. package/src/components/record/declaration.test.tsx +73 -0
  123. package/src/components/record/declaration.tsx +109 -0
  124. package/src/components/stc/index.ts +2 -2
  125. package/src/components/type-parameters/type-parameter-constraints.test.tsx +93 -0
  126. package/src/components/type-parameters/type-parameter-constraints.tsx +46 -0
  127. package/src/components/type-parameters/type-parameter.tsx +35 -0
  128. package/src/components/type-parameters/type-parameters.test.tsx +46 -0
  129. package/src/components/type-parameters/type-parameters.tsx +63 -0
  130. package/src/components/var/declaration.test.tsx +59 -0
  131. package/src/components/var/declaration.tsx +47 -0
  132. package/src/modifiers.ts +25 -42
  133. package/src/name-policy.ts +5 -0
  134. package/temp/api.json +5227 -685
  135. package/test/class-declaration.test.tsx +418 -0
  136. package/test/class-method.test.tsx +24 -14
  137. package/test/enum.test.tsx +11 -11
  138. package/test/namespace.test.tsx +4 -4
  139. package/test/{projectdirectory.test.tsx → project-directory.test.tsx} +4 -4
  140. package/test/sourcefile.test.tsx +2 -2
  141. package/test/using.test.tsx +9 -9
  142. package/test/vitest.setup.ts +1 -0
  143. package/vitest.config.ts +3 -0
  144. package/dist/src/components/Class.d.ts +0 -26
  145. package/dist/src/components/Class.d.ts.map +0 -1
  146. package/dist/src/components/Enum.d.ts +0 -15
  147. package/dist/src/components/Enum.d.ts.map +0 -1
  148. package/dist/test/class.test.d.ts +0 -2
  149. package/dist/test/class.test.d.ts.map +0 -1
  150. package/dist/test/class.test.js +0 -298
  151. package/dist/test/projectdirectory.test.d.ts +0 -2
  152. package/dist/test/projectdirectory.test.d.ts.map +0 -1
  153. package/test/class.test.tsx +0 -292
@@ -0,0 +1,144 @@
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
+ });
@@ -0,0 +1,107 @@
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
+ }
@@ -0,0 +1,187 @@
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
+ });
@@ -0,0 +1,146 @@
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
+ }
@@ -0,0 +1,73 @@
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
+ });