@alloy-js/csharp 0.18.0-dev.10 → 0.18.0-dev.11

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.
@@ -0,0 +1,180 @@
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 { ClassProperty } 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
+ <ClassProperty
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
+ <ClassProperty
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
+ <ClassProperty 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
+ <ClassProperty 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
+ <ClassProperty 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
+ <ClassProperty 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
+ <ClassProperty 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("specify doc comment", () => {
135
+ expect(
136
+ <TestNamespace>
137
+ <ClassDeclaration name="Test">
138
+ <ClassProperty
139
+ name="Method"
140
+ type="string"
141
+ get
142
+ set
143
+ doc="This is a test"
144
+ />
145
+ </ClassDeclaration>
146
+ </TestNamespace>,
147
+ ).toRenderTo(`
148
+ class Test
149
+ {
150
+ /// This is a test
151
+ string Method { get; set; }
152
+ }
153
+ `);
154
+ });
155
+
156
+ it("specify nullable property", () => {
157
+ expect(
158
+ <Wrapper>
159
+ <ClassProperty name="TestProp" type="string" nullable get set />
160
+ </Wrapper>,
161
+ ).toRenderTo(`
162
+ public class TestClass
163
+ {
164
+ string? TestProp { get; set; }
165
+ }
166
+ `);
167
+ });
168
+
169
+ it("specify initializer", () => {
170
+ expect(
171
+ <Wrapper>
172
+ <ClassProperty name="TestProp" type="string" get set init={`"abc"`} />
173
+ </Wrapper>,
174
+ ).toRenderTo(`
175
+ public class TestClass
176
+ {
177
+ string TestProp { get; set; } = "abc";
178
+ }
179
+ `);
180
+ });
@@ -0,0 +1,135 @@
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
+ /** Method modifiers. Can only be one. */
23
+ export interface ClassPropertyModifiers {
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<ClassPropertyModifiers>([
35
+ "new",
36
+ "static",
37
+ "virtual",
38
+ "sealed",
39
+ "override",
40
+ "abstract",
41
+ "extern",
42
+ "readonly",
43
+ ]);
44
+
45
+ /** Properties for {@link ClassProperty} component */
46
+ export interface ClassPropertyProps
47
+ extends AccessModifiers,
48
+ ClassPropertyModifiers {
49
+ name: string;
50
+ refkey?: Refkey;
51
+
52
+ /** Property type */
53
+ type: Children;
54
+
55
+ /** If property should have a getter */
56
+ get?: boolean;
57
+
58
+ /** If property should have a setter */
59
+ set?: boolean;
60
+
61
+ /** Doc comment */
62
+ doc?: Children;
63
+
64
+ /**
65
+ * Property initializer
66
+ * @example `<ClassProperty name="My" get set nullable />`
67
+ *
68
+ * ```cs
69
+ * int? My { get; set; };
70
+ * ```
71
+ */
72
+ nullable?: boolean;
73
+
74
+ /**
75
+ * Property initializer
76
+ * @example `<ClassProperty name="My" get set init={42} />`
77
+ *
78
+ * ```cs
79
+ * int My { get; set; } = 42;
80
+ * ```
81
+ */
82
+ init?: Children;
83
+ }
84
+
85
+ /**
86
+ * Render a C# class property.
87
+ *
88
+ * @example `<ClassProperty public name="My" get set />`
89
+ *
90
+ * ```cs
91
+ * public int My { get; set; };
92
+ * ```
93
+ */
94
+ export function ClassProperty(props: ClassPropertyProps) {
95
+ const name = useCSharpNamePolicy().getName(props.name, "class-property");
96
+ const scope = useCSharpScope();
97
+ if (scope.kind !== "member" || scope.name !== "class-decl") {
98
+ throw new Error(
99
+ "can't define an interface method outside of an interface scope",
100
+ );
101
+ }
102
+
103
+ const propertySymbol = new CSharpOutputSymbol(name, {
104
+ scope,
105
+ refkeys: props.refkey ?? refkey(props.name),
106
+ });
107
+
108
+ // scope for property declaration
109
+ const propertyScope = new CSharpMemberScope("property-decl", {
110
+ owner: propertySymbol,
111
+ });
112
+
113
+ const modifiers = computeModifiersPrefix([
114
+ getAccessModifier(props),
115
+ getModifiers(props),
116
+ ]);
117
+ // note that scope wraps the method decl so that the params get the correct scope
118
+ return (
119
+ <MemberDeclaration symbol={propertySymbol}>
120
+ <Scope value={propertyScope}>
121
+ <DocWhen doc={props.doc} />
122
+ {modifiers}
123
+ {props.type}
124
+ {props.nullable && "?"} {name}{" "}
125
+ <Block newline inline>
126
+ <List joiner=" ">
127
+ {props.get && "get;"}
128
+ {props.set && "set;"}
129
+ </List>
130
+ </Block>
131
+ {props.init && code` = ${props.init};`}
132
+ </Scope>
133
+ </MemberDeclaration>
134
+ );
135
+ }
@@ -1,3 +1,4 @@
1
+ export * from "./class/property.js";
1
2
  export * from "./ClassDeclaration.jsx";
2
3
  export * from "./ClassMethod.jsx";
3
4
  export * from "./Declaration.js";
@@ -6,6 +7,7 @@ export * from "./doc/from-markdown.jsx";
6
7
  export * from "./EnumDeclaration.jsx";
7
8
  export * from "./interface/declaration.js";
8
9
  export * from "./interface/method.js";
10
+ export * from "./interface/property.js";
9
11
  export * from "./Name.js";
10
12
  export * from "./Namespace.js";
11
13
  export * from "./Parameters.js";
@@ -23,9 +23,7 @@ export interface InterfacePropertyModifiers {
23
23
  readonly new?: boolean;
24
24
  }
25
25
 
26
- export const getMethodModifier = makeModifiers<InterfacePropertyModifiers>([
27
- "new",
28
- ]);
26
+ const getModifiers = makeModifiers<InterfacePropertyModifiers>(["new"]);
29
27
 
30
28
  // properties for creating a method
31
29
  export interface InterfacePropertyProps
@@ -45,9 +43,27 @@ export interface InterfacePropertyProps
45
43
 
46
44
  /** Doc comment */
47
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;
48
56
  }
49
57
 
50
- // a C# interface property
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
+ */
51
67
  export function InterfaceProperty(props: InterfacePropertyProps) {
52
68
  const name = useCSharpNamePolicy().getName(props.name, "class-property");
53
69
  const scope = useCSharpScope();
@@ -69,7 +85,7 @@ export function InterfaceProperty(props: InterfacePropertyProps) {
69
85
 
70
86
  const modifiers = computeModifiersPrefix([
71
87
  getAccessModifier(props),
72
- getMethodModifier(props),
88
+ getModifiers(props),
73
89
  ]);
74
90
  // note that scope wraps the method decl so that the params get the correct scope
75
91
  return (
@@ -77,7 +93,8 @@ export function InterfaceProperty(props: InterfacePropertyProps) {
77
93
  <Scope value={propertyScope}>
78
94
  <DocWhen doc={props.doc} />
79
95
  {modifiers}
80
- {props.type} {name}{" "}
96
+ {props.type}
97
+ {props.nullable && "?"} {name}{" "}
81
98
  <Block newline inline>
82
99
  <List joiner=" ">
83
100
  {props.get && "get;"}