@alloy-js/csharp 0.18.0-dev.8 → 0.18.0

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 (127) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/src/components/ClassDeclaration.d.ts +39 -2
  3. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  4. package/dist/src/components/ClassDeclaration.js +35 -34
  5. package/dist/src/components/ClassMethod.d.ts +33 -1
  6. package/dist/src/components/ClassMethod.d.ts.map +1 -1
  7. package/dist/src/components/ClassMethod.js +22 -7
  8. package/dist/src/components/SourceFile.d.ts.map +1 -1
  9. package/dist/src/components/SourceFile.js +1 -0
  10. package/dist/src/components/attributes/attributes.d.ts +39 -0
  11. package/dist/src/components/attributes/attributes.d.ts.map +1 -0
  12. package/dist/src/components/attributes/attributes.js +62 -0
  13. package/dist/src/components/attributes/attributes.test.d.ts +2 -0
  14. package/dist/src/components/attributes/attributes.test.d.ts.map +1 -0
  15. package/dist/src/components/attributes/attributes.test.js +75 -0
  16. package/dist/src/components/doc/comment.d.ts +12 -11
  17. package/dist/src/components/doc/comment.d.ts.map +1 -1
  18. package/dist/src/components/doc/comment.js +27 -10
  19. package/dist/src/components/doc/comment.test.js +98 -88
  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 +9 -1
  27. package/dist/src/components/index.d.ts.map +1 -1
  28. package/dist/src/components/index.js +10 -2
  29. package/dist/src/components/interface/declaration.d.ts +32 -1
  30. package/dist/src/components/interface/declaration.d.ts.map +1 -1
  31. package/dist/src/components/interface/declaration.js +18 -25
  32. package/dist/src/components/interface/declaration.test.js +102 -0
  33. package/dist/src/components/interface/method.d.ts +33 -1
  34. package/dist/src/components/interface/method.d.ts.map +1 -1
  35. package/dist/src/components/interface/method.js +22 -7
  36. package/dist/src/components/interface/method.test.js +169 -0
  37. package/dist/src/components/interface/property.d.ts +36 -1
  38. package/dist/src/components/interface/property.d.ts.map +1 -1
  39. package/dist/src/components/interface/property.js +18 -4
  40. package/dist/src/components/interface/property.test.js +24 -0
  41. package/dist/src/components/parameters/parameters.d.ts +19 -0
  42. package/dist/src/components/parameters/parameters.d.ts.map +1 -0
  43. package/dist/src/components/parameters/parameters.js +43 -0
  44. package/dist/src/components/property/property.d.ts +80 -0
  45. package/dist/src/components/property/property.d.ts.map +1 -0
  46. package/dist/src/components/property/property.js +76 -0
  47. package/dist/src/components/property/property.test.d.ts +2 -0
  48. package/dist/src/components/property/property.test.d.ts.map +1 -0
  49. package/dist/src/components/property/property.test.js +242 -0
  50. package/dist/src/components/record/declaration.d.ts +35 -0
  51. package/dist/src/components/record/declaration.d.ts.map +1 -0
  52. package/dist/src/components/record/declaration.js +90 -0
  53. package/dist/src/components/record/declaration.test.d.ts +2 -0
  54. package/dist/src/components/record/declaration.test.d.ts.map +1 -0
  55. package/dist/src/components/record/declaration.test.js +94 -0
  56. package/dist/src/components/type-parameters/type-parameter-constraints.d.ts +8 -0
  57. package/dist/src/components/type-parameters/type-parameter-constraints.d.ts.map +1 -0
  58. package/dist/src/components/type-parameters/type-parameter-constraints.js +44 -0
  59. package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts +2 -0
  60. package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts.map +1 -0
  61. package/dist/src/components/type-parameters/type-parameter-constraints.test.js +67 -0
  62. package/dist/src/components/type-parameters/type-parameter.d.ts +20 -0
  63. package/dist/src/components/type-parameters/type-parameter.d.ts.map +1 -0
  64. package/dist/src/components/type-parameters/type-parameter.js +22 -0
  65. package/dist/src/components/type-parameters/type-parameters.d.ts +17 -0
  66. package/dist/src/components/type-parameters/type-parameters.d.ts.map +1 -0
  67. package/dist/src/components/type-parameters/type-parameters.js +54 -0
  68. package/dist/src/components/type-parameters/type-parameters.test.d.ts +2 -0
  69. package/dist/src/components/type-parameters/type-parameters.test.d.ts.map +1 -0
  70. package/dist/src/components/type-parameters/type-parameters.test.js +48 -0
  71. package/dist/src/components/var/declaration.d.ts +35 -0
  72. package/dist/src/components/var/declaration.d.ts.map +1 -0
  73. package/dist/src/components/var/declaration.js +40 -0
  74. package/dist/src/components/var/declaration.test.d.ts +2 -0
  75. package/dist/src/components/var/declaration.test.d.ts.map +1 -0
  76. package/dist/src/components/var/declaration.test.js +73 -0
  77. package/dist/src/name-policy.d.ts +1 -1
  78. package/dist/src/name-policy.d.ts.map +1 -1
  79. package/dist/src/name-policy.js +1 -0
  80. package/dist/test/class-declaration.test.d.ts +2 -0
  81. package/dist/test/class-declaration.test.d.ts.map +1 -0
  82. package/dist/test/{class.test.js → class-declaration.test.js} +161 -33
  83. package/dist/test/project-directory.test.d.ts +2 -0
  84. package/dist/test/project-directory.test.d.ts.map +1 -0
  85. package/dist/tsconfig.tsbuildinfo +1 -1
  86. package/package.json +7 -6
  87. package/src/components/ClassDeclaration.tsx +65 -33
  88. package/src/components/ClassMethod.tsx +46 -4
  89. package/src/components/SourceFile.tsx +1 -0
  90. package/src/components/attributes/attributes.test.tsx +61 -0
  91. package/src/components/attributes/attributes.tsx +100 -0
  92. package/src/components/doc/comment.test.tsx +80 -79
  93. package/src/components/doc/comment.tsx +44 -14
  94. package/src/components/doc/from-markdown.test.tsx +103 -0
  95. package/src/components/doc/from-markdown.tsx +58 -0
  96. package/src/components/index.ts +9 -1
  97. package/src/components/interface/declaration.test.tsx +102 -0
  98. package/src/components/interface/declaration.tsx +43 -27
  99. package/src/components/interface/method.test.tsx +173 -0
  100. package/src/components/interface/method.tsx +45 -5
  101. package/src/components/interface/property.test.tsx +21 -0
  102. package/src/components/interface/property.tsx +43 -6
  103. package/src/components/parameters/parameters.tsx +74 -0
  104. package/src/components/property/property.test.tsx +209 -0
  105. package/src/components/property/property.tsx +172 -0
  106. package/src/components/record/declaration.test.tsx +73 -0
  107. package/src/components/record/declaration.tsx +109 -0
  108. package/src/components/type-parameters/type-parameter-constraints.test.tsx +93 -0
  109. package/src/components/type-parameters/type-parameter-constraints.tsx +46 -0
  110. package/src/components/type-parameters/type-parameter.tsx +35 -0
  111. package/src/components/type-parameters/type-parameters.test.tsx +46 -0
  112. package/src/components/type-parameters/type-parameters.tsx +63 -0
  113. package/src/components/var/declaration.test.tsx +59 -0
  114. package/src/components/var/declaration.tsx +47 -0
  115. package/src/name-policy.ts +3 -0
  116. package/temp/api.json +5838 -1626
  117. package/test/{class.test.tsx → class-declaration.test.tsx} +144 -26
  118. package/dist/src/components/Parameters.d.ts +0 -13
  119. package/dist/src/components/Parameters.d.ts.map +0 -1
  120. package/dist/src/components/Parameters.js +0 -34
  121. package/dist/test/class.test.d.ts +0 -2
  122. package/dist/test/class.test.d.ts.map +0 -1
  123. package/dist/test/projectdirectory.test.d.ts +0 -2
  124. package/dist/test/projectdirectory.test.d.ts.map +0 -1
  125. package/src/components/Parameters.tsx +0 -51
  126. /package/dist/test/{projectdirectory.test.js → project-directory.test.js} +0 -0
  127. /package/test/{projectdirectory.test.tsx → project-directory.test.tsx} +0 -0
@@ -15,8 +15,12 @@ import {
15
15
  import { useCSharpNamePolicy } from "../../name-policy.js";
16
16
  import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
17
17
  import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
18
- import { ParameterProps, Parameters } from "../Parameters.jsx";
18
+ import { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
19
19
  import { DocWhen } from "../doc/comment.jsx";
20
+ import { ParameterProps, Parameters } from "../parameters/parameters.jsx";
21
+ import { TypeParameterConstraints } from "../type-parameters/type-parameter-constraints.jsx";
22
+ import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
23
+ import { TypeParameters } from "../type-parameters/type-parameters.jsx";
20
24
 
21
25
  /** Method modifiers. Can only be one. */
22
26
  export interface InterfaceMethodModifiers {
@@ -33,10 +37,41 @@ export interface InterfaceMethodProps
33
37
  refkey?: Refkey;
34
38
  children?: Children;
35
39
  parameters?: Array<ParameterProps>;
40
+ /**
41
+ * Type parameters for the method
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * <InterfaceMethod name="Test" typeParameters={["T"]} />
46
+ * ```
47
+ * This will produce:
48
+ * ```csharp
49
+ * public void Test<T>()
50
+ * ```
51
+ */
52
+ typeParameters?: (TypeParameterProps | string)[];
36
53
  returns?: Children;
37
54
 
38
55
  /** Doc comment */
39
56
  doc?: Children;
57
+
58
+ /**
59
+ * Define attributes to attach
60
+ * @example
61
+ * ```tsx
62
+ * <InterfaceMethod name="MyMethod" attributes={[
63
+ * <Attribute name="Test" />
64
+ * <Attribute name="Test2" args={["arg1", "arg2"]} />
65
+ * ]} />
66
+ * ```
67
+ * This will produce:
68
+ * ```csharp
69
+ * [Test]
70
+ * [Test2("arg1", "arg2")]
71
+ * void MyMethod();
72
+ * ```
73
+ */
74
+ attributes?: AttributesProp;
40
75
  }
41
76
 
42
77
  // a C# interface method
@@ -59,9 +94,6 @@ export function InterfaceMethod(props: InterfaceMethodProps) {
59
94
  owner: methodSymbol,
60
95
  });
61
96
 
62
- const params =
63
- props.parameters ? <Parameters parameters={props.parameters} /> : "";
64
-
65
97
  const modifiers = computeModifiersPrefix([
66
98
  getAccessModifier(props),
67
99
  getMethodModifier(props),
@@ -71,8 +103,16 @@ export function InterfaceMethod(props: InterfaceMethodProps) {
71
103
  <MemberDeclaration symbol={methodSymbol}>
72
104
  <Scope value={methodScope}>
73
105
  <DocWhen doc={props.doc} />
106
+ <AttributeList attributes={props.attributes} endline />
74
107
  {modifiers}
75
- {props.returns ?? "void"} {name}({params})
108
+ {props.returns ?? "void"} {name}
109
+ {props.typeParameters && (
110
+ <TypeParameters parameters={props.typeParameters} />
111
+ )}
112
+ <Parameters parameters={props.parameters} />
113
+ {props.typeParameters && (
114
+ <TypeParameterConstraints parameters={props.typeParameters} />
115
+ )}
76
116
  {props.children ?
77
117
  <Block newline>{props.children}</Block>
78
118
  : ";"}
@@ -1,6 +1,7 @@
1
1
  import { Children } from "@alloy-js/core/jsx-runtime";
2
2
  import { describe, expect, it } from "vitest";
3
3
  import { TestNamespace } from "../../../test/utils.jsx";
4
+ import { Attribute } from "../attributes/attributes.jsx";
4
5
  import { InterfaceDeclaration } from "./declaration.jsx";
5
6
  import { InterfaceProperty } from "./property.jsx";
6
7
 
@@ -142,3 +143,23 @@ it("specify doc comment", () => {
142
143
  }
143
144
  `);
144
145
  });
146
+
147
+ it("specify attributes", () => {
148
+ expect(
149
+ <Wrapper>
150
+ <InterfaceProperty
151
+ name="Test"
152
+ type="int"
153
+ attributes={[<Attribute name="Test" />]}
154
+ get
155
+ set
156
+ />
157
+ </Wrapper>,
158
+ ).toRenderTo(`
159
+ public interface TestInterface
160
+ {
161
+ [Test]
162
+ int Test { get; set; }
163
+ }
164
+ `);
165
+ });
@@ -16,6 +16,7 @@ import {
16
16
  import { useCSharpNamePolicy } from "../../name-policy.js";
17
17
  import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
18
18
  import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
19
+ import { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
19
20
  import { DocWhen } from "../doc/comment.jsx";
20
21
 
21
22
  /** Method modifiers. Can only be one. */
@@ -23,9 +24,7 @@ export interface InterfacePropertyModifiers {
23
24
  readonly new?: boolean;
24
25
  }
25
26
 
26
- export const getMethodModifier = makeModifiers<InterfacePropertyModifiers>([
27
- "new",
28
- ]);
27
+ const getModifiers = makeModifiers<InterfacePropertyModifiers>(["new"]);
29
28
 
30
29
  // properties for creating a method
31
30
  export interface InterfacePropertyProps
@@ -45,9 +44,45 @@ export interface InterfacePropertyProps
45
44
 
46
45
  /** Doc comment */
47
46
  doc?: Children;
47
+
48
+ /**
49
+ * Property initializer
50
+ * @example `<ClassProperty name="My" get set nullable />`
51
+ *
52
+ * ```cs
53
+ * int? My { get; set; };
54
+ * ```
55
+ */
56
+ nullable?: boolean;
57
+
58
+ /**
59
+ * Define attributes to attach
60
+ * @example
61
+ * ```tsx
62
+ * <InterfaceProperty name="MyProp" attributes={[
63
+ * <Attribute name="Test" />
64
+ * <Attribute name="Test2" args={["arg1", "arg2"]} />
65
+ * ]} />
66
+ * ```
67
+ * This will produce:
68
+ * ```csharp
69
+ * [Test]
70
+ * [Test2("arg1", "arg2")]
71
+ * int MyProp { get; set; }
72
+ * ```
73
+ */
74
+ attributes?: AttributesProp;
48
75
  }
49
76
 
50
- // a C# interface property
77
+ /**
78
+ * Render a C# interface property.
79
+ *
80
+ * @example `<InterfaceProperty public name="My" get set />`
81
+ *
82
+ * ```cs
83
+ * public int My { get; set; };
84
+ * ```
85
+ */
51
86
  export function InterfaceProperty(props: InterfacePropertyProps) {
52
87
  const name = useCSharpNamePolicy().getName(props.name, "class-property");
53
88
  const scope = useCSharpScope();
@@ -69,15 +104,17 @@ export function InterfaceProperty(props: InterfacePropertyProps) {
69
104
 
70
105
  const modifiers = computeModifiersPrefix([
71
106
  getAccessModifier(props),
72
- getMethodModifier(props),
107
+ getModifiers(props),
73
108
  ]);
74
109
  // note that scope wraps the method decl so that the params get the correct scope
75
110
  return (
76
111
  <MemberDeclaration symbol={propertySymbol}>
77
112
  <Scope value={propertyScope}>
78
113
  <DocWhen doc={props.doc} />
114
+ <AttributeList attributes={props.attributes} endline />
79
115
  {modifiers}
80
- {props.type} {name}{" "}
116
+ {props.type}
117
+ {props.nullable && "?"} {name}{" "}
81
118
  <Block newline inline>
82
119
  <List joiner=" ">
83
120
  {props.get && "get;"}
@@ -0,0 +1,74 @@
1
+ import {
2
+ Children,
3
+ code,
4
+ Declaration,
5
+ For,
6
+ Indent,
7
+ OutputSymbol,
8
+ refkey,
9
+ Refkey,
10
+ } from "@alloy-js/core";
11
+ import { useCSharpNamePolicy } from "../../name-policy.js";
12
+ import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
13
+ import { useCSharpScope } from "../../symbols/scopes.js";
14
+ import { Name } from "../Name.jsx";
15
+
16
+ export interface ParameterProps {
17
+ name: string;
18
+ type: Children;
19
+ /** If the parmaeter is optional(without default value) */
20
+ optional?: boolean;
21
+ /** Default value for the parameter */
22
+ default?: Children;
23
+ refkey?: Refkey;
24
+ symbol?: OutputSymbol;
25
+ }
26
+
27
+ /** Define a parameter to be used in class or interface method. */
28
+ export function Parameter(props: ParameterProps) {
29
+ const name = useCSharpNamePolicy().getName(props.name, "parameter");
30
+ const scope = useCSharpScope();
31
+ if (
32
+ scope.kind !== "member" ||
33
+ (scope.name !== "constructor-decl" && scope.name !== "method-decl")
34
+ ) {
35
+ throw new Error(
36
+ "can't define a parameter outside of a constructor-decl or method-decl scope",
37
+ );
38
+ }
39
+
40
+ const memberSymbol = new CSharpOutputSymbol(name, {
41
+ scope,
42
+ refkeys: props.refkey ?? refkey(props.name),
43
+ });
44
+
45
+ return (
46
+ <Declaration symbol={memberSymbol}>
47
+ {props.type}
48
+ {props.optional ? "?" : ""} <Name />
49
+ {props.default ? code` = ${props.default}` : ""}
50
+ </Declaration>
51
+ );
52
+ }
53
+
54
+ export interface ParametersProps {
55
+ parameters: ParameterProps[] | undefined;
56
+ }
57
+
58
+ /** Render a collection of parameters */
59
+ export function Parameters(props: ParametersProps) {
60
+ return (
61
+ <group>
62
+ {"("}
63
+ {props.parameters && (
64
+ <Indent softline>
65
+ <For each={props.parameters} joiner={", "}>
66
+ {(param) => <Parameter {...param} />}
67
+ </For>
68
+ </Indent>
69
+ )}
70
+ <softline />
71
+ {")"}
72
+ </group>
73
+ );
74
+ }
@@ -0,0 +1,209 @@
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 { Attribute } from "../attributes/attributes.jsx";
5
+ import { ClassDeclaration } from "../ClassDeclaration.jsx";
6
+ import { Property } from "./property.jsx";
7
+
8
+ const Wrapper = (props: { children: Children }) => (
9
+ <TestNamespace>
10
+ <ClassDeclaration public name="TestClass">
11
+ {props.children}
12
+ </ClassDeclaration>
13
+ </TestNamespace>
14
+ );
15
+
16
+ describe("modifiers", () => {
17
+ describe("access modifiers", () => {
18
+ it.each(["public", "private", "protected", "internal"] as const)(
19
+ "%s",
20
+ (accessModifier) => {
21
+ expect(
22
+ <Wrapper>
23
+ <Property
24
+ {...{ [accessModifier]: true }}
25
+ name="TestProp"
26
+ type="string"
27
+ get
28
+ />
29
+ </Wrapper>,
30
+ ).toRenderTo(`
31
+ public class TestClass
32
+ {
33
+ ${accessModifier} string TestProp { get; }
34
+ }
35
+ `);
36
+ },
37
+ );
38
+ });
39
+
40
+ describe("property modifiers", () => {
41
+ it.each([
42
+ "new",
43
+ "static",
44
+ "virtual",
45
+ "sealed",
46
+ "override",
47
+ "abstract",
48
+ "extern",
49
+ "readonly",
50
+ "required",
51
+ ] as const)("%s", (methodModifier) => {
52
+ expect(
53
+ <Wrapper>
54
+ <Property
55
+ {...{ [methodModifier]: true }}
56
+ name="TestProp"
57
+ type="string"
58
+ get
59
+ />
60
+ </Wrapper>,
61
+ ).toRenderTo(`
62
+ public class TestClass
63
+ {
64
+ ${methodModifier} string TestProp { get; }
65
+ }
66
+ `);
67
+ });
68
+ });
69
+
70
+ it("combine modifiers", () => {
71
+ expect(
72
+ <Wrapper>
73
+ <Property public new name="TestProp" type="string" get />
74
+ </Wrapper>,
75
+ ).toRenderTo(`
76
+ public class TestClass
77
+ {
78
+ public new string TestProp { get; }
79
+ }
80
+ `);
81
+ });
82
+ });
83
+
84
+ it("applies PascalCase naming policy", () => {
85
+ expect(
86
+ <Wrapper>
87
+ <Property name="test_prop" type="string" get />
88
+ </Wrapper>,
89
+ ).toRenderTo(`
90
+ public class TestClass
91
+ {
92
+ string TestProp { get; }
93
+ }
94
+ `);
95
+ });
96
+
97
+ it("has getter only", () => {
98
+ expect(
99
+ <Wrapper>
100
+ <Property name="TestProp" type="string" get />
101
+ </Wrapper>,
102
+ ).toRenderTo(`
103
+ public class TestClass
104
+ {
105
+ string TestProp { get; }
106
+ }
107
+ `);
108
+ });
109
+
110
+ it("has setter only", () => {
111
+ expect(
112
+ <Wrapper>
113
+ <Property name="TestProp" type="string" set />
114
+ </Wrapper>,
115
+ ).toRenderTo(`
116
+ public class TestClass
117
+ {
118
+ string TestProp { set; }
119
+ }
120
+ `);
121
+ });
122
+
123
+ it("has getter and setter", () => {
124
+ expect(
125
+ <Wrapper>
126
+ <Property name="TestProp" type="string" get set />
127
+ </Wrapper>,
128
+ ).toRenderTo(`
129
+ public class TestClass
130
+ {
131
+ string TestProp { get; set; }
132
+ }
133
+ `);
134
+ });
135
+
136
+ it("has getter and init", () => {
137
+ expect(
138
+ <Wrapper>
139
+ <Property name="TestProp" type="string" get init />
140
+ </Wrapper>,
141
+ ).toRenderTo(`
142
+ public class TestClass
143
+ {
144
+ string TestProp { get; init; }
145
+ }
146
+ `);
147
+ });
148
+
149
+ it("specify doc comment", () => {
150
+ expect(
151
+ <TestNamespace>
152
+ <ClassDeclaration name="Test">
153
+ <Property name="Method" type="string" get set doc="This is a test" />
154
+ </ClassDeclaration>
155
+ </TestNamespace>,
156
+ ).toRenderTo(`
157
+ class Test
158
+ {
159
+ /// This is a test
160
+ string Method { get; set; }
161
+ }
162
+ `);
163
+ });
164
+
165
+ it("specify nullable property", () => {
166
+ expect(
167
+ <Wrapper>
168
+ <Property name="TestProp" type="string" nullable get set />
169
+ </Wrapper>,
170
+ ).toRenderTo(`
171
+ public class TestClass
172
+ {
173
+ string? TestProp { get; set; }
174
+ }
175
+ `);
176
+ });
177
+
178
+ it("specify initializer", () => {
179
+ expect(
180
+ <Wrapper>
181
+ <Property name="TestProp" type="string" get set initializer={`"abc"`} />
182
+ </Wrapper>,
183
+ ).toRenderTo(`
184
+ public class TestClass
185
+ {
186
+ string TestProp { get; set; } = "abc";
187
+ }
188
+ `);
189
+ });
190
+
191
+ it("specify attributes", () => {
192
+ expect(
193
+ <Wrapper>
194
+ <Property
195
+ name="Test"
196
+ type="int"
197
+ attributes={[<Attribute name="Test" />]}
198
+ get
199
+ set
200
+ />
201
+ </Wrapper>,
202
+ ).toRenderTo(`
203
+ public class TestClass
204
+ {
205
+ [Test]
206
+ int Test { get; set; }
207
+ }
208
+ `);
209
+ });
@@ -0,0 +1,172 @@
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 { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
21
+ import { DocWhen } from "../doc/comment.jsx";
22
+
23
+ /** Property modifiers. */
24
+ export interface PropertyModifiers {
25
+ readonly new?: boolean;
26
+ readonly static?: boolean;
27
+ readonly virtual?: boolean;
28
+ readonly sealed?: boolean;
29
+ readonly override?: boolean;
30
+ readonly abstract?: boolean;
31
+ readonly extern?: boolean;
32
+ readonly readonly?: boolean;
33
+ /**
34
+ * Set required modifier on property
35
+ * https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required
36
+ */
37
+ readonly required?: boolean;
38
+ }
39
+
40
+ const getModifiers = makeModifiers<PropertyModifiers>([
41
+ "new",
42
+ "static",
43
+ "virtual",
44
+ "sealed",
45
+ "override",
46
+ "abstract",
47
+ "extern",
48
+ "readonly",
49
+ "required",
50
+ ]);
51
+
52
+ /** Properties for {@link Property} component */
53
+ export interface PropertyProps extends AccessModifiers, PropertyModifiers {
54
+ name: string;
55
+ refkey?: Refkey;
56
+
57
+ /** Property type */
58
+ type: Children;
59
+
60
+ /** If property should have a getter */
61
+ get?: boolean;
62
+
63
+ /** If property should have a setter */
64
+ set?: boolean;
65
+
66
+ /** If property should only be set on the type creation */
67
+ init?: boolean;
68
+
69
+ /** Doc comment */
70
+ doc?: Children;
71
+
72
+ /**
73
+ * Property initializer
74
+ * @example `<ClassProperty name="My" get set nullable />`
75
+ *
76
+ * ```cs
77
+ * int? My { get; set; };
78
+ * ```
79
+ */
80
+ nullable?: boolean;
81
+
82
+ /**
83
+ * Property initializer
84
+ * @example `<ClassProperty name="My" get set init={42} />`
85
+ *
86
+ * ```cs
87
+ * int My { get; set; } = 42;
88
+ * ```
89
+ */
90
+ initializer?: Children;
91
+
92
+ /**
93
+ * Define attributes to attach
94
+ * @example
95
+ * ```tsx
96
+ * <Property name="MyProp" attributes={[
97
+ * <Attribute name="Test" />
98
+ * <Attribute name="Test2" args={["arg1", "arg2"]} />
99
+ * ]} />
100
+ * ```
101
+ * This will produce:
102
+ * ```csharp
103
+ * [Test]
104
+ * [Test2("arg1", "arg2")]
105
+ * int MyProp { get; set; }
106
+ * ```
107
+ */
108
+ attributes?: AttributesProp;
109
+ }
110
+
111
+ /**
112
+ * Render a C# class property.
113
+ *
114
+ * @example `<ClassProperty public name="My" get set />`
115
+ *
116
+ * ```cs
117
+ * public int My { get; set; };
118
+ * ```
119
+ */
120
+ export function Property(props: PropertyProps) {
121
+ const name = useCSharpNamePolicy().getName(props.name, "class-property");
122
+ const scope = useCSharpScope();
123
+ if (
124
+ scope.kind !== "member" ||
125
+ (scope.name !== "class-decl" && scope.name !== "record-decl")
126
+ ) {
127
+ throw new Error(
128
+ "can't define an interface method outside of an interface scope",
129
+ );
130
+ }
131
+
132
+ const propertySymbol = new CSharpOutputSymbol(name, {
133
+ scope,
134
+ refkeys: props.refkey ?? refkey(props.name),
135
+ });
136
+
137
+ // scope for property declaration
138
+ const propertyScope = new CSharpMemberScope("property-decl", {
139
+ owner: propertySymbol,
140
+ });
141
+
142
+ const modifiers = computeModifiersPrefix([
143
+ getAccessModifier(props),
144
+ getModifiers(props),
145
+ ]);
146
+
147
+ if (props.init && props.set) {
148
+ throw new Error(
149
+ `Cannot use 'init' and 'set' together on property '${name}'`,
150
+ );
151
+ }
152
+ // note that scope wraps the method decl so that the params get the correct scope
153
+ return (
154
+ <MemberDeclaration symbol={propertySymbol}>
155
+ <Scope value={propertyScope}>
156
+ <DocWhen doc={props.doc} />
157
+ <AttributeList attributes={props.attributes} endline />
158
+ {modifiers}
159
+ {props.type}
160
+ {props.nullable && "?"} {name}{" "}
161
+ <Block newline inline>
162
+ <List joiner=" ">
163
+ {props.get && "get;"}
164
+ {props.set && "set;"}
165
+ {props.init && "init;"}
166
+ </List>
167
+ </Block>
168
+ {props.initializer && code` = ${props.initializer};`}
169
+ </Scope>
170
+ </MemberDeclaration>
171
+ );
172
+ }