@alloy-js/csharp 0.18.0-dev.14 → 0.18.0-dev.16

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.
@@ -2,7 +2,7 @@ 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
4
  import { ClassDeclaration } from "../ClassDeclaration.jsx";
5
- import { ClassProperty } from "./property.jsx";
5
+ import { Property } from "./property.jsx";
6
6
 
7
7
  const Wrapper = (props: { children: Children }) => (
8
8
  <TestNamespace>
@@ -19,7 +19,7 @@ describe("modifiers", () => {
19
19
  (accessModifier) => {
20
20
  expect(
21
21
  <Wrapper>
22
- <ClassProperty
22
+ <Property
23
23
  {...{ [accessModifier]: true }}
24
24
  name="TestProp"
25
25
  type="string"
@@ -49,7 +49,7 @@ describe("modifiers", () => {
49
49
  ] as const)("%s", (methodModifier) => {
50
50
  expect(
51
51
  <Wrapper>
52
- <ClassProperty
52
+ <Property
53
53
  {...{ [methodModifier]: true }}
54
54
  name="TestProp"
55
55
  type="string"
@@ -68,7 +68,7 @@ describe("modifiers", () => {
68
68
  it("combine modifiers", () => {
69
69
  expect(
70
70
  <Wrapper>
71
- <ClassProperty public new name="TestProp" type="string" get />
71
+ <Property public new name="TestProp" type="string" get />
72
72
  </Wrapper>,
73
73
  ).toRenderTo(`
74
74
  public class TestClass
@@ -82,7 +82,7 @@ describe("modifiers", () => {
82
82
  it("applies PascalCase naming policy", () => {
83
83
  expect(
84
84
  <Wrapper>
85
- <ClassProperty name="test_prop" type="string" get />
85
+ <Property name="test_prop" type="string" get />
86
86
  </Wrapper>,
87
87
  ).toRenderTo(`
88
88
  public class TestClass
@@ -95,7 +95,7 @@ it("applies PascalCase naming policy", () => {
95
95
  it("has getter only", () => {
96
96
  expect(
97
97
  <Wrapper>
98
- <ClassProperty name="TestProp" type="string" get />
98
+ <Property name="TestProp" type="string" get />
99
99
  </Wrapper>,
100
100
  ).toRenderTo(`
101
101
  public class TestClass
@@ -108,7 +108,7 @@ it("has getter only", () => {
108
108
  it("has setter only", () => {
109
109
  expect(
110
110
  <Wrapper>
111
- <ClassProperty name="TestProp" type="string" set />
111
+ <Property name="TestProp" type="string" set />
112
112
  </Wrapper>,
113
113
  ).toRenderTo(`
114
114
  public class TestClass
@@ -121,7 +121,7 @@ it("has setter only", () => {
121
121
  it("has getter and setter", () => {
122
122
  expect(
123
123
  <Wrapper>
124
- <ClassProperty name="TestProp" type="string" get set />
124
+ <Property name="TestProp" type="string" get set />
125
125
  </Wrapper>,
126
126
  ).toRenderTo(`
127
127
  public class TestClass
@@ -131,17 +131,24 @@ it("has getter and setter", () => {
131
131
  `);
132
132
  });
133
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
+
134
147
  it("specify doc comment", () => {
135
148
  expect(
136
149
  <TestNamespace>
137
150
  <ClassDeclaration name="Test">
138
- <ClassProperty
139
- name="Method"
140
- type="string"
141
- get
142
- set
143
- doc="This is a test"
144
- />
151
+ <Property name="Method" type="string" get set doc="This is a test" />
145
152
  </ClassDeclaration>
146
153
  </TestNamespace>,
147
154
  ).toRenderTo(`
@@ -156,7 +163,7 @@ it("specify doc comment", () => {
156
163
  it("specify nullable property", () => {
157
164
  expect(
158
165
  <Wrapper>
159
- <ClassProperty name="TestProp" type="string" nullable get set />
166
+ <Property name="TestProp" type="string" nullable get set />
160
167
  </Wrapper>,
161
168
  ).toRenderTo(`
162
169
  public class TestClass
@@ -169,7 +176,7 @@ it("specify nullable property", () => {
169
176
  it("specify initializer", () => {
170
177
  expect(
171
178
  <Wrapper>
172
- <ClassProperty name="TestProp" type="string" get set init={`"abc"`} />
179
+ <Property name="TestProp" type="string" get set initializer={`"abc"`} />
173
180
  </Wrapper>,
174
181
  ).toRenderTo(`
175
182
  public class TestClass
@@ -19,8 +19,8 @@ import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
19
19
  import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
20
20
  import { DocWhen } from "../doc/comment.jsx";
21
21
 
22
- /** Method modifiers. Can only be one. */
23
- export interface ClassPropertyModifiers {
22
+ /** Property modifiers. */
23
+ export interface PropertyModifiers {
24
24
  readonly new?: boolean;
25
25
  readonly static?: boolean;
26
26
  readonly virtual?: boolean;
@@ -31,7 +31,7 @@ export interface ClassPropertyModifiers {
31
31
  readonly readonly?: boolean;
32
32
  }
33
33
 
34
- const getModifiers = makeModifiers<ClassPropertyModifiers>([
34
+ const getModifiers = makeModifiers<PropertyModifiers>([
35
35
  "new",
36
36
  "static",
37
37
  "virtual",
@@ -42,10 +42,8 @@ const getModifiers = makeModifiers<ClassPropertyModifiers>([
42
42
  "readonly",
43
43
  ]);
44
44
 
45
- /** Properties for {@link ClassProperty} component */
46
- export interface ClassPropertyProps
47
- extends AccessModifiers,
48
- ClassPropertyModifiers {
45
+ /** Properties for {@link Property} component */
46
+ export interface PropertyProps extends AccessModifiers, PropertyModifiers {
49
47
  name: string;
50
48
  refkey?: Refkey;
51
49
 
@@ -58,6 +56,9 @@ export interface ClassPropertyProps
58
56
  /** If property should have a setter */
59
57
  set?: boolean;
60
58
 
59
+ /** If property should only be set on the type creation */
60
+ init?: boolean;
61
+
61
62
  /** Doc comment */
62
63
  doc?: Children;
63
64
 
@@ -79,7 +80,7 @@ export interface ClassPropertyProps
79
80
  * int My { get; set; } = 42;
80
81
  * ```
81
82
  */
82
- init?: Children;
83
+ initializer?: Children;
83
84
  }
84
85
 
85
86
  /**
@@ -91,7 +92,7 @@ export interface ClassPropertyProps
91
92
  * public int My { get; set; };
92
93
  * ```
93
94
  */
94
- export function ClassProperty(props: ClassPropertyProps) {
95
+ export function Property(props: PropertyProps) {
95
96
  const name = useCSharpNamePolicy().getName(props.name, "class-property");
96
97
  const scope = useCSharpScope();
97
98
  if (
@@ -117,6 +118,12 @@ export function ClassProperty(props: ClassPropertyProps) {
117
118
  getAccessModifier(props),
118
119
  getModifiers(props),
119
120
  ]);
121
+
122
+ if (props.init && props.set) {
123
+ throw new Error(
124
+ `Cannot use 'init' and 'set' together on property '${name}'`,
125
+ );
126
+ }
120
127
  // note that scope wraps the method decl so that the params get the correct scope
121
128
  return (
122
129
  <MemberDeclaration symbol={propertySymbol}>
@@ -129,9 +136,10 @@ export function ClassProperty(props: ClassPropertyProps) {
129
136
  <List joiner=" ">
130
137
  {props.get && "get;"}
131
138
  {props.set && "set;"}
139
+ {props.init && "init;"}
132
140
  </List>
133
141
  </Block>
134
- {props.init && code` = ${props.init};`}
142
+ {props.initializer && code` = ${props.initializer};`}
135
143
  </Scope>
136
144
  </MemberDeclaration>
137
145
  );
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import { TestNamespace } from "../../../test/utils.jsx";
3
- import { ClassProperty } from "../class/property.jsx";
3
+ import { Property } from "../property/property.jsx";
4
4
  import { RecordDeclaration } from "./declaration.jsx";
5
5
 
6
6
  it("declares class with no members", () => {
@@ -60,7 +60,7 @@ it("specify class property inside", () => {
60
60
  expect(
61
61
  <TestNamespace>
62
62
  <RecordDeclaration name="TestRecord" doc="This is a test">
63
- <ClassProperty name="Prop" get set type="string" />
63
+ <Property name="Prop" get set type="string" />
64
64
  </RecordDeclaration>
65
65
  </TestNamespace>,
66
66
  ).toRenderTo(`
@@ -17,7 +17,7 @@ export interface RecordModifiers {
17
17
 
18
18
  const getRecordModifiers = makeModifiers<RecordModifiers>(["partial"]);
19
19
 
20
- // properties for creating a class
20
+ /** Props to use the {@link RecordDeclaration} component */
21
21
  export interface RecordDeclarationProps
22
22
  extends Omit<core.DeclarationProps, "nameKind">,
23
23
  AccessModifiers,