@alloy-js/csharp 0.18.0-dev.23 → 0.18.0-dev.3

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