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

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 (64) hide show
  1. package/dist/src/components/ClassDeclaration.d.ts +2 -0
  2. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  3. package/dist/src/components/ClassDeclaration.js +6 -1
  4. package/dist/src/components/ClassMethod.d.ts +11 -2
  5. package/dist/src/components/ClassMethod.d.ts.map +1 -1
  6. package/dist/src/components/ClassMethod.js +11 -2
  7. package/dist/src/components/doc/comment.d.ts +69 -0
  8. package/dist/src/components/doc/comment.d.ts.map +1 -0
  9. package/dist/src/components/doc/comment.js +71 -0
  10. package/dist/src/components/doc/comment.test.d.ts +2 -0
  11. package/dist/src/components/doc/comment.test.d.ts.map +1 -0
  12. package/dist/src/components/doc/comment.test.js +338 -0
  13. package/dist/src/components/index.d.ts +3 -1
  14. package/dist/src/components/index.d.ts.map +1 -1
  15. package/dist/src/components/index.js +3 -1
  16. package/dist/src/components/interface/declaration.d.ts +34 -0
  17. package/dist/src/components/interface/declaration.d.ts.map +1 -0
  18. package/dist/src/components/interface/declaration.js +90 -0
  19. package/dist/src/components/interface/declaration.test.d.ts +2 -0
  20. package/dist/src/components/interface/declaration.test.d.ts.map +1 -0
  21. package/dist/src/components/interface/declaration.test.js +69 -0
  22. package/dist/src/components/interface/method.d.ts +18 -0
  23. package/dist/src/components/interface/method.d.ts.map +1 -0
  24. package/dist/src/components/interface/method.js +59 -0
  25. package/dist/src/components/interface/method.test.d.ts +2 -0
  26. package/dist/src/components/interface/method.test.d.ts.map +1 -0
  27. package/dist/src/components/interface/method.test.js +131 -0
  28. package/dist/src/components/interface/property.d.ts +21 -0
  29. package/dist/src/components/interface/property.d.ts.map +1 -0
  30. package/dist/src/components/interface/property.js +59 -0
  31. package/dist/src/components/interface/property.test.d.ts +2 -0
  32. package/dist/src/components/interface/property.test.d.ts.map +1 -0
  33. package/dist/src/components/interface/property.test.js +165 -0
  34. package/dist/src/modifiers.d.ts +0 -8
  35. package/dist/src/modifiers.d.ts.map +1 -1
  36. package/dist/src/modifiers.js +0 -4
  37. package/dist/src/name-policy.d.ts +1 -1
  38. package/dist/src/name-policy.d.ts.map +1 -1
  39. package/dist/src/name-policy.js +1 -0
  40. package/dist/test/class-method.test.js +21 -0
  41. package/dist/test/class.test.js +13 -0
  42. package/dist/test/vitest.setup.d.ts +2 -0
  43. package/dist/test/vitest.setup.d.ts.map +1 -0
  44. package/dist/test/vitest.setup.js +1 -0
  45. package/dist/tsconfig.tsbuildinfo +1 -1
  46. package/package.json +2 -2
  47. package/src/components/ClassDeclaration.tsx +4 -0
  48. package/src/components/ClassMethod.tsx +24 -3
  49. package/src/components/doc/comment.test.tsx +336 -0
  50. package/src/components/doc/comment.tsx +122 -0
  51. package/src/components/index.ts +3 -1
  52. package/src/components/interface/declaration.test.tsx +56 -0
  53. package/src/components/interface/declaration.tsx +109 -0
  54. package/src/components/interface/method.test.tsx +120 -0
  55. package/src/components/interface/method.tsx +82 -0
  56. package/src/components/interface/property.test.tsx +144 -0
  57. package/src/components/interface/property.tsx +90 -0
  58. package/src/modifiers.ts +0 -15
  59. package/src/name-policy.ts +2 -0
  60. package/temp/api.json +699 -81
  61. package/test/class-method.test.tsx +16 -0
  62. package/test/class.test.tsx +11 -0
  63. package/test/vitest.setup.ts +1 -0
  64. package/vitest.config.ts +3 -0
@@ -0,0 +1,120 @@
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 { InterfaceMethod } from "./method.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
+ <InterfaceMethod {...{ [accessModifier]: true }} name="MethodOne" />
23
+ </Wrapper>,
24
+ ).toRenderTo(`
25
+ public interface TestInterface
26
+ {
27
+ ${accessModifier} void MethodOne();
28
+ }
29
+ `);
30
+ },
31
+ );
32
+ });
33
+
34
+ describe("method modifiers", () => {
35
+ it.each(["new"] as const)("%s", (methodModifier) => {
36
+ expect(
37
+ <Wrapper>
38
+ <InterfaceMethod {...{ [methodModifier]: true }} name="MethodOne" />
39
+ </Wrapper>,
40
+ ).toRenderTo(`
41
+ public interface TestInterface
42
+ {
43
+ ${methodModifier} void MethodOne();
44
+ }
45
+ `);
46
+ });
47
+ });
48
+
49
+ it("combine modifiers", () => {
50
+ expect(
51
+ <Wrapper>
52
+ <InterfaceMethod returns="Task" public new name="MethodOne" />
53
+ </Wrapper>,
54
+ ).toRenderTo(`
55
+ public interface TestInterface
56
+ {
57
+ public new Task MethodOne();
58
+ }
59
+ `);
60
+ });
61
+ });
62
+
63
+ it("applies PascalCase naming policy", () => {
64
+ expect(
65
+ <Wrapper>
66
+ <InterfaceMethod name="method_one" />
67
+ </Wrapper>,
68
+ ).toRenderTo(`
69
+ public interface TestInterface
70
+ {
71
+ void MethodOne();
72
+ }
73
+ `);
74
+ });
75
+
76
+ it("defines params and return type", () => {
77
+ const params = [
78
+ {
79
+ name: "intParam",
80
+ type: "int",
81
+ },
82
+ {
83
+ name: "stringParam",
84
+ type: "string",
85
+ },
86
+ ];
87
+ const res = (
88
+ <Wrapper>
89
+ <InterfaceMethod
90
+ public
91
+ name="MethodOne"
92
+ parameters={params}
93
+ returns="string"
94
+ />
95
+ </Wrapper>
96
+ );
97
+
98
+ expect(res).toRenderTo(`
99
+ public interface TestInterface
100
+ {
101
+ public string MethodOne(int intParam, string stringParam);
102
+ }
103
+ `);
104
+ });
105
+
106
+ it("specify doc comment", () => {
107
+ expect(
108
+ <TestNamespace>
109
+ <InterfaceDeclaration name="Test">
110
+ <InterfaceMethod name="Method" doc="This is a test" />
111
+ </InterfaceDeclaration>
112
+ </TestNamespace>,
113
+ ).toRenderTo(`
114
+ interface Test
115
+ {
116
+ /// This is a test
117
+ void Method();
118
+ }
119
+ `);
120
+ });
@@ -0,0 +1,82 @@
1
+ import {
2
+ Block,
3
+ Children,
4
+ MemberDeclaration,
5
+ refkey,
6
+ Refkey,
7
+ Scope,
8
+ } from "@alloy-js/core";
9
+ import {
10
+ AccessModifiers,
11
+ computeModifiersPrefix,
12
+ getAccessModifier,
13
+ makeModifiers,
14
+ } from "../../modifiers.js";
15
+ import { useCSharpNamePolicy } from "../../name-policy.js";
16
+ import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
17
+ import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
18
+ import { ParameterProps, Parameters } from "../Parameters.jsx";
19
+ import { DocWhen } from "../doc/comment.jsx";
20
+
21
+ /** Method modifiers. Can only be one. */
22
+ export interface InterfaceMethodModifiers {
23
+ readonly new?: boolean;
24
+ }
25
+
26
+ const getMethodModifier = makeModifiers<InterfaceMethodModifiers>(["new"]);
27
+
28
+ // properties for creating a method
29
+ export interface InterfaceMethodProps
30
+ extends AccessModifiers,
31
+ InterfaceMethodModifiers {
32
+ name: string;
33
+ refkey?: Refkey;
34
+ children?: Children;
35
+ parameters?: Array<ParameterProps>;
36
+ returns?: Children;
37
+
38
+ /** Doc comment */
39
+ doc?: Children;
40
+ }
41
+
42
+ // a C# interface method
43
+ export function InterfaceMethod(props: InterfaceMethodProps) {
44
+ const name = useCSharpNamePolicy().getName(props.name, "class-method");
45
+ const scope = useCSharpScope();
46
+ if (scope.kind !== "member" || scope.name !== "interface-decl") {
47
+ throw new Error(
48
+ "can't define an interface method outside of an interface scope",
49
+ );
50
+ }
51
+
52
+ const methodSymbol = new CSharpOutputSymbol(name, {
53
+ scope,
54
+ refkeys: props.refkey ?? refkey(props.name),
55
+ });
56
+
57
+ // scope for method declaration
58
+ const methodScope = new CSharpMemberScope("method-decl", {
59
+ owner: methodSymbol,
60
+ });
61
+
62
+ const params =
63
+ props.parameters ? <Parameters parameters={props.parameters} /> : "";
64
+
65
+ const modifiers = computeModifiersPrefix([
66
+ getAccessModifier(props),
67
+ getMethodModifier(props),
68
+ ]);
69
+ // note that scope wraps the method decl so that the params get the correct scope
70
+ return (
71
+ <MemberDeclaration symbol={methodSymbol}>
72
+ <Scope value={methodScope}>
73
+ <DocWhen doc={props.doc} />
74
+ {modifiers}
75
+ {props.returns ?? "void"} {name}({params})
76
+ {props.children ?
77
+ <Block newline>{props.children}</Block>
78
+ : ";"}
79
+ </Scope>
80
+ </MemberDeclaration>
81
+ );
82
+ }
@@ -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,90 @@
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
+ export const getMethodModifier = makeModifiers<InterfacePropertyModifiers>([
27
+ "new",
28
+ ]);
29
+
30
+ // properties for creating a method
31
+ export interface InterfacePropertyProps
32
+ extends AccessModifiers,
33
+ InterfacePropertyModifiers {
34
+ name: string;
35
+ refkey?: Refkey;
36
+
37
+ /** Property type */
38
+ type: Children;
39
+
40
+ /** If property should have a getter */
41
+ get?: boolean;
42
+
43
+ /** If property should have a setter */
44
+ set?: boolean;
45
+
46
+ /** Doc comment */
47
+ doc?: Children;
48
+ }
49
+
50
+ // a C# interface property
51
+ export function InterfaceProperty(props: InterfacePropertyProps) {
52
+ const name = useCSharpNamePolicy().getName(props.name, "class-property");
53
+ const scope = useCSharpScope();
54
+ if (scope.kind !== "member" || scope.name !== "interface-decl") {
55
+ throw new Error(
56
+ "can't define an interface method outside of an interface scope",
57
+ );
58
+ }
59
+
60
+ const propertySymbol = new CSharpOutputSymbol(name, {
61
+ scope,
62
+ refkeys: props.refkey ?? refkey(props.name),
63
+ });
64
+
65
+ // scope for property declaration
66
+ const propertyScope = new CSharpMemberScope("property-decl", {
67
+ owner: propertySymbol,
68
+ });
69
+
70
+ const modifiers = computeModifiersPrefix([
71
+ getAccessModifier(props),
72
+ getMethodModifier(props),
73
+ ]);
74
+ // note that scope wraps the method decl so that the params get the correct scope
75
+ return (
76
+ <MemberDeclaration symbol={propertySymbol}>
77
+ <Scope value={propertyScope}>
78
+ <DocWhen doc={props.doc} />
79
+ {modifiers}
80
+ {props.type} {name}{" "}
81
+ <Block newline inline>
82
+ <List joiner=" ">
83
+ {props.get && "get;"}
84
+ {props.set && "set;"}
85
+ </List>
86
+ </Block>
87
+ </Scope>
88
+ </MemberDeclaration>
89
+ );
90
+ }
package/src/modifiers.ts CHANGED
@@ -18,21 +18,6 @@ export const getAccessModifier = makeModifiers<AccessModifiers>([
18
18
  "file",
19
19
  ]);
20
20
 
21
- /** Method modifiers. Can only be one. */
22
- export interface MethodModifiers {
23
- readonly abstract?: boolean;
24
- readonly sealed?: boolean;
25
- readonly static?: boolean;
26
- readonly virtual?: boolean;
27
- }
28
-
29
- export const getMethodModifier = makeModifiers<MethodModifiers>([
30
- "abstract",
31
- "sealed",
32
- "static",
33
- "virtual",
34
- ]);
35
-
36
21
  export function getAsyncModifier(async?: boolean): string {
37
22
  return async ? "async" : "";
38
23
  }
@@ -12,6 +12,7 @@ export type CSharpElements =
12
12
  | "class-member-private"
13
13
  | "class-member-public"
14
14
  | "class-method"
15
+ | "class-property"
15
16
  | "parameter"
16
17
  | "type-parameter";
17
18
 
@@ -26,6 +27,7 @@ export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
26
27
  case "class-member-public":
27
28
  case "class-method":
28
29
  case "type-parameter":
30
+ case "class-property":
29
31
  return changecase.pascalCase(name);
30
32
  case "constant":
31
33
  return changecase.constantCase(name);