@alloy-js/csharp 0.18.0-dev.24 → 0.18.0-dev.4

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 (166) hide show
  1. package/dist/src/components/Class.d.ts +23 -0
  2. package/dist/src/components/Class.d.ts.map +1 -0
  3. package/dist/src/components/{ClassDeclaration.js → Class.js} +37 -67
  4. package/dist/src/components/ClassMethod.d.ts +3 -44
  5. package/dist/src/components/ClassMethod.d.ts.map +1 -1
  6. package/dist/src/components/ClassMethod.js +8 -32
  7. package/dist/src/components/Enum.d.ts +14 -0
  8. package/dist/src/components/Enum.d.ts.map +1 -0
  9. package/dist/src/components/{EnumDeclaration.js → Enum.js} +2 -21
  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 -14
  16. package/dist/src/components/index.d.ts.map +1 -1
  17. package/dist/src/components/index.js +5 -15
  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 +9 -2
  22. package/dist/src/modifiers.d.ts.map +1 -1
  23. package/dist/src/modifiers.js +9 -6
  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 +2 -23
  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-declaration.test.js → class.test.js} +51 -234
  31. package/dist/test/enum.test.js +6 -6
  32. package/dist/test/namespace.test.js +4 -4
  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} +4 -4
  36. package/dist/test/sourcefile.test.js +2 -2
  37. package/dist/test/using.test.js +4 -4
  38. package/dist/tsconfig.tsbuildinfo +1 -1
  39. package/package.json +3 -4
  40. package/src/components/{ClassDeclaration.tsx → Class.tsx} +38 -116
  41. package/src/components/ClassMethod.tsx +7 -70
  42. package/src/components/{EnumDeclaration.tsx → Enum.tsx} +3 -22
  43. package/src/components/Parameters.tsx +51 -0
  44. package/src/components/SourceFile.tsx +0 -1
  45. package/src/components/index.ts +4 -14
  46. package/src/components/stc/index.ts +2 -2
  47. package/src/modifiers.ts +30 -16
  48. package/src/name-policy.ts +0 -5
  49. package/temp/api.json +1194 -6218
  50. package/test/class-method.test.tsx +3 -19
  51. package/test/class.test.tsx +257 -0
  52. package/test/enum.test.tsx +11 -13
  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 +6 -14
  57. package/vitest.config.ts +0 -3
  58. package/dist/src/components/ClassDeclaration.d.ts +0 -92
  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/attributes/attributes.d.ts +0 -39
  63. package/dist/src/components/attributes/attributes.d.ts.map +0 -1
  64. package/dist/src/components/attributes/attributes.js +0 -62
  65. package/dist/src/components/attributes/attributes.test.d.ts +0 -2
  66. package/dist/src/components/attributes/attributes.test.d.ts.map +0 -1
  67. package/dist/src/components/attributes/attributes.test.js +0 -75
  68. package/dist/src/components/doc/comment.d.ts +0 -70
  69. package/dist/src/components/doc/comment.d.ts.map +0 -1
  70. package/dist/src/components/doc/comment.js +0 -88
  71. package/dist/src/components/doc/comment.test.d.ts +0 -2
  72. package/dist/src/components/doc/comment.test.d.ts.map +0 -1
  73. package/dist/src/components/doc/comment.test.js +0 -348
  74. package/dist/src/components/doc/from-markdown.d.ts +0 -6
  75. package/dist/src/components/doc/from-markdown.d.ts.map +0 -1
  76. package/dist/src/components/doc/from-markdown.js +0 -58
  77. package/dist/src/components/doc/from-markdown.test.d.ts +0 -2
  78. package/dist/src/components/doc/from-markdown.test.d.ts.map +0 -1
  79. package/dist/src/components/doc/from-markdown.test.js +0 -83
  80. package/dist/src/components/interface/declaration.d.ts +0 -65
  81. package/dist/src/components/interface/declaration.d.ts.map +0 -1
  82. package/dist/src/components/interface/declaration.js +0 -83
  83. package/dist/src/components/interface/declaration.test.d.ts +0 -2
  84. package/dist/src/components/interface/declaration.test.d.ts.map +0 -1
  85. package/dist/src/components/interface/declaration.test.js +0 -171
  86. package/dist/src/components/interface/method.d.ts +0 -50
  87. package/dist/src/components/interface/method.d.ts.map +0 -1
  88. package/dist/src/components/interface/method.js +0 -74
  89. package/dist/src/components/interface/method.test.d.ts +0 -2
  90. package/dist/src/components/interface/method.test.d.ts.map +0 -1
  91. package/dist/src/components/interface/method.test.js +0 -300
  92. package/dist/src/components/interface/property.d.ts +0 -56
  93. package/dist/src/components/interface/property.d.ts.map +0 -1
  94. package/dist/src/components/interface/property.js +0 -73
  95. package/dist/src/components/interface/property.test.d.ts +0 -2
  96. package/dist/src/components/interface/property.test.d.ts.map +0 -1
  97. package/dist/src/components/interface/property.test.js +0 -189
  98. package/dist/src/components/parameters/parameters.d.ts +0 -19
  99. package/dist/src/components/parameters/parameters.d.ts.map +0 -1
  100. package/dist/src/components/parameters/parameters.js +0 -43
  101. package/dist/src/components/property/property.d.ts +0 -80
  102. package/dist/src/components/property/property.d.ts.map +0 -1
  103. package/dist/src/components/property/property.js +0 -76
  104. package/dist/src/components/property/property.test.d.ts +0 -2
  105. package/dist/src/components/property/property.test.d.ts.map +0 -1
  106. package/dist/src/components/property/property.test.js +0 -242
  107. package/dist/src/components/record/declaration.d.ts +0 -35
  108. package/dist/src/components/record/declaration.d.ts.map +0 -1
  109. package/dist/src/components/record/declaration.js +0 -90
  110. package/dist/src/components/record/declaration.test.d.ts +0 -2
  111. package/dist/src/components/record/declaration.test.d.ts.map +0 -1
  112. package/dist/src/components/record/declaration.test.js +0 -94
  113. package/dist/src/components/type-parameters/type-parameter-constraints.d.ts +0 -8
  114. package/dist/src/components/type-parameters/type-parameter-constraints.d.ts.map +0 -1
  115. package/dist/src/components/type-parameters/type-parameter-constraints.js +0 -44
  116. package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts +0 -2
  117. package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts.map +0 -1
  118. package/dist/src/components/type-parameters/type-parameter-constraints.test.js +0 -67
  119. package/dist/src/components/type-parameters/type-parameter.d.ts +0 -20
  120. package/dist/src/components/type-parameters/type-parameter.d.ts.map +0 -1
  121. package/dist/src/components/type-parameters/type-parameter.js +0 -22
  122. package/dist/src/components/type-parameters/type-parameters.d.ts +0 -17
  123. package/dist/src/components/type-parameters/type-parameters.d.ts.map +0 -1
  124. package/dist/src/components/type-parameters/type-parameters.js +0 -54
  125. package/dist/src/components/type-parameters/type-parameters.test.d.ts +0 -2
  126. package/dist/src/components/type-parameters/type-parameters.test.d.ts.map +0 -1
  127. package/dist/src/components/type-parameters/type-parameters.test.js +0 -48
  128. package/dist/src/components/var/declaration.d.ts +0 -35
  129. package/dist/src/components/var/declaration.d.ts.map +0 -1
  130. package/dist/src/components/var/declaration.js +0 -40
  131. package/dist/src/components/var/declaration.test.d.ts +0 -2
  132. package/dist/src/components/var/declaration.test.d.ts.map +0 -1
  133. package/dist/src/components/var/declaration.test.js +0 -73
  134. package/dist/test/class-declaration.test.d.ts +0 -2
  135. package/dist/test/class-declaration.test.d.ts.map +0 -1
  136. package/dist/test/project-directory.test.d.ts +0 -2
  137. package/dist/test/project-directory.test.d.ts.map +0 -1
  138. package/dist/test/vitest.setup.d.ts +0 -2
  139. package/dist/test/vitest.setup.d.ts.map +0 -1
  140. package/dist/test/vitest.setup.js +0 -1
  141. package/src/components/attributes/attributes.test.tsx +0 -61
  142. package/src/components/attributes/attributes.tsx +0 -100
  143. package/src/components/doc/comment.test.tsx +0 -337
  144. package/src/components/doc/comment.tsx +0 -152
  145. package/src/components/doc/from-markdown.test.tsx +0 -103
  146. package/src/components/doc/from-markdown.tsx +0 -58
  147. package/src/components/interface/declaration.test.tsx +0 -158
  148. package/src/components/interface/declaration.tsx +0 -125
  149. package/src/components/interface/method.test.tsx +0 -293
  150. package/src/components/interface/method.tsx +0 -122
  151. package/src/components/interface/property.test.tsx +0 -165
  152. package/src/components/interface/property.tsx +0 -127
  153. package/src/components/parameters/parameters.tsx +0 -74
  154. package/src/components/property/property.test.tsx +0 -209
  155. package/src/components/property/property.tsx +0 -172
  156. package/src/components/record/declaration.test.tsx +0 -73
  157. package/src/components/record/declaration.tsx +0 -109
  158. package/src/components/type-parameters/type-parameter-constraints.test.tsx +0 -93
  159. package/src/components/type-parameters/type-parameter-constraints.tsx +0 -46
  160. package/src/components/type-parameters/type-parameter.tsx +0 -35
  161. package/src/components/type-parameters/type-parameters.test.tsx +0 -46
  162. package/src/components/type-parameters/type-parameters.tsx +0 -63
  163. package/src/components/var/declaration.test.tsx +0 -59
  164. package/src/components/var/declaration.tsx +0 -47
  165. package/test/class-declaration.test.tsx +0 -430
  166. package/test/vitest.setup.ts +0 -1
@@ -1,158 +0,0 @@
1
- import { List, refkey } from "@alloy-js/core";
2
- import { describe, expect, it } from "vitest";
3
- import { TestNamespace } from "../../../test/utils.jsx";
4
- import { Attribute } from "../attributes/attributes.jsx";
5
- import { SourceFile } from "../SourceFile.jsx";
6
- import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
7
- import { InterfaceDeclaration } from "./declaration.jsx";
8
- import { InterfaceProperty } from "./property.jsx";
9
-
10
- it("declares class with no members", () => {
11
- expect(
12
- <TestNamespace>
13
- <InterfaceDeclaration name="TestInterface" />
14
- </TestNamespace>,
15
- ).toRenderTo(`
16
- interface TestInterface;
17
- `);
18
- });
19
-
20
- describe("modifiers", () => {
21
- it.each(["public", "private", "internal"])("%s", (mod) => {
22
- expect(
23
- <TestNamespace>
24
- <InterfaceDeclaration {...{ [mod]: true }} name="TestInterface" />
25
- </TestNamespace>,
26
- ).toRenderTo(`
27
- ${mod} interface TestInterface;
28
- `);
29
- });
30
-
31
- it.each(["partial"])("%s", (mod) => {
32
- expect(
33
- <TestNamespace>
34
- <InterfaceDeclaration {...{ [mod]: true }} name="TestInterface" />
35
- </TestNamespace>,
36
- ).toRenderTo(`
37
- ${mod} interface TestInterface;
38
- `);
39
- });
40
-
41
- it("combines modifiers", () => {
42
- expect(
43
- <TestNamespace>
44
- <InterfaceDeclaration public partial name="TestInterface" />
45
- </TestNamespace>,
46
- ).toRenderTo(`
47
- public partial interface TestInterface;
48
- `);
49
- });
50
- });
51
-
52
- it("specify doc comment", () => {
53
- expect(
54
- <TestNamespace>
55
- <InterfaceDeclaration name="TestInterface" doc="This is a test" />
56
- </TestNamespace>,
57
- ).toRenderTo(`
58
- /// This is a test
59
- interface TestInterface;
60
- `);
61
- });
62
-
63
- describe("with type parameters", () => {
64
- it("reference parameters", () => {
65
- const typeParameters: TypeParameterProps[] = [
66
- {
67
- name: "T",
68
- refkey: refkey(),
69
- },
70
- {
71
- name: "U",
72
- refkey: refkey(),
73
- },
74
- ];
75
-
76
- expect(
77
- <TestNamespace>
78
- <SourceFile path="Test.cs">
79
- <InterfaceDeclaration
80
- public
81
- name="Test"
82
- typeParameters={typeParameters}
83
- >
84
- <List>
85
- <InterfaceProperty
86
- name="PropA"
87
- type={typeParameters[0].refkey}
88
- get
89
- set
90
- />
91
- <InterfaceProperty
92
- name="PropB"
93
- type={typeParameters[1].refkey}
94
- get
95
- set
96
- />
97
- </List>
98
- </InterfaceDeclaration>
99
- </SourceFile>
100
- </TestNamespace>,
101
- ).toRenderTo(`
102
- namespace TestCode
103
- {
104
- public interface Test<T, U>
105
- {
106
- T PropA { get; set; }
107
- U PropB { get; set; }
108
- }
109
- }
110
- `);
111
- });
112
-
113
- it("defines with constraints", () => {
114
- const typeParameters: TypeParameterProps[] = [
115
- {
116
- name: "T",
117
- constraints: "IFoo",
118
- },
119
- {
120
- name: "U",
121
- constraints: "IBar",
122
- },
123
- ];
124
-
125
- expect(
126
- <TestNamespace>
127
- <InterfaceDeclaration
128
- public
129
- name="Test"
130
- typeParameters={typeParameters}
131
- >
132
- // Body
133
- </InterfaceDeclaration>
134
- </TestNamespace>,
135
- ).toRenderTo(`
136
- public interface Test<T, U>
137
- where T : IFoo
138
- where U : IBar
139
- {
140
- // Body
141
- }
142
- `);
143
- });
144
- });
145
-
146
- it("specify attributes", () => {
147
- expect(
148
- <TestNamespace>
149
- <InterfaceDeclaration
150
- name="Test"
151
- attributes={[<Attribute name="Test" />]}
152
- />
153
- </TestNamespace>,
154
- ).toRenderTo(`
155
- [Test]
156
- interface Test;
157
- `);
158
- });
@@ -1,125 +0,0 @@
1
- import * as core from "@alloy-js/core";
2
- import {
3
- AccessModifiers,
4
- computeModifiersPrefix,
5
- getAccessModifier,
6
- makeModifiers,
7
- } from "../../modifiers.js";
8
- import { useCSharpNamePolicy } from "../../name-policy.js";
9
- import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
10
- import { CSharpMemberScope } from "../../symbols/scopes.js";
11
- import { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
12
- import { DocWhen } from "../doc/comment.jsx";
13
- import { Name } from "../Name.jsx";
14
- import { TypeParameterConstraints } from "../type-parameters/type-parameter-constraints.jsx";
15
- import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
16
- import { TypeParameters } from "../type-parameters/type-parameters.jsx";
17
-
18
- export interface InterfaceModifiers {
19
- readonly partial?: boolean;
20
- }
21
-
22
- const getInterfaceModifiers = makeModifiers<InterfaceModifiers>(["partial"]);
23
-
24
- // properties for creating a class
25
- export interface InterfaceDeclarationProps
26
- extends Omit<core.DeclarationProps, "nameKind">,
27
- AccessModifiers,
28
- InterfaceModifiers {
29
- name: string;
30
-
31
- /** Doc comment */
32
- doc?: core.Children;
33
- refkey?: core.Refkey;
34
-
35
- /**
36
- * Type parameters for the interface
37
- *
38
- * @example
39
- * ```tsx
40
- * <InterfaceDeclaration name="IList" typeParameters={["T"]} />
41
- * ```
42
- * This will produce:
43
- * ```csharp
44
- * public interface IList<T>
45
- * ```
46
- */
47
- typeParameters?: (TypeParameterProps | string)[];
48
-
49
- /**
50
- * Define attributes to attach
51
- * @example
52
- * ```tsx
53
- * <InterfaceDeclaration name="MyInterface" attributes={[
54
- * <Attribute name="Test" />
55
- * <Attribute name="Test2" args={["arg1", "arg2"]} />
56
- * ]} />
57
- * ```
58
- * This will produce:
59
- * ```csharp
60
- * [Test]
61
- * [Test2("arg1", "arg2")]
62
- * public interface MyInterface
63
- * ```
64
- */
65
- attributes?: AttributesProp;
66
- }
67
-
68
- /**
69
- * CSharp interface declaration.
70
- * @example
71
- * ```tsx
72
- * <InterfaceDeclaration public name="IMyInterface">
73
- * <InterfaceMember public name="MyProperty" type="int" />
74
- * <InterfaceMethod public name="MyMethod" returnType="void">
75
- * <Parameter name="value" type="int" />
76
- * </InterfaceMethod>
77
- * </InterfaceDeclaration>
78
- * ```
79
- * This will produce:
80
- * ```csharp
81
- * public interface MyIface
82
- * {
83
- * public int MyProperty { get; set; }
84
- * public void MyMethod(int value);
85
- * }
86
- * ```
87
- */
88
- export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
89
- const name = useCSharpNamePolicy().getName(props.name!, "interface");
90
-
91
- const thisInterfaceSymbol = new CSharpOutputSymbol(name, {
92
- refkeys: props.refkey,
93
- });
94
-
95
- // this creates a new scope for the interface definition.
96
- // members will automatically "inherit" this scope so
97
- // that refkeys to them will produce the fully-qualified
98
- // name e.g. Foo.Bar.
99
- const thisInterfaceScope = new CSharpMemberScope("interface-decl", {
100
- owner: thisInterfaceSymbol,
101
- });
102
-
103
- const modifiers = computeModifiersPrefix([
104
- getAccessModifier(props),
105
- getInterfaceModifiers(props),
106
- ]);
107
- return (
108
- <core.Declaration symbol={thisInterfaceSymbol}>
109
- <DocWhen doc={props.doc} />
110
- <AttributeList attributes={props.attributes} endline />
111
- {modifiers}interface <Name />
112
- {props.typeParameters && (
113
- <TypeParameters parameters={props.typeParameters} />
114
- )}
115
- {props.typeParameters && (
116
- <TypeParameterConstraints parameters={props.typeParameters} />
117
- )}
118
- {props.children ?
119
- <core.Block newline>
120
- <core.Scope value={thisInterfaceScope}>{props.children}</core.Scope>
121
- </core.Block>
122
- : ";"}
123
- </core.Declaration>
124
- );
125
- }
@@ -1,293 +0,0 @@
1
- import { refkey } from "@alloy-js/core";
2
- import { Children } from "@alloy-js/core/jsx-runtime";
3
- import { describe, expect, it } from "vitest";
4
- import { TestNamespace } from "../../../test/utils.jsx";
5
- import { Attribute } from "../attributes/attributes.jsx";
6
- import { SourceFile } from "../SourceFile.jsx";
7
- import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
8
- import { InterfaceDeclaration } from "./declaration.jsx";
9
- import { InterfaceMethod } from "./method.jsx";
10
-
11
- const Wrapper = (props: { children: Children }) => (
12
- <TestNamespace>
13
- <InterfaceDeclaration public name="TestInterface">
14
- {props.children}
15
- </InterfaceDeclaration>
16
- </TestNamespace>
17
- );
18
-
19
- describe("modifiers", () => {
20
- describe("access modifiers", () => {
21
- it.each(["public", "private", "protected", "internal"] as const)(
22
- "%s",
23
- (accessModifier) => {
24
- expect(
25
- <Wrapper>
26
- <InterfaceMethod {...{ [accessModifier]: true }} name="MethodOne" />
27
- </Wrapper>,
28
- ).toRenderTo(`
29
- public interface TestInterface
30
- {
31
- ${accessModifier} void MethodOne();
32
- }
33
- `);
34
- },
35
- );
36
- });
37
-
38
- describe("method modifiers", () => {
39
- it.each(["new"] as const)("%s", (methodModifier) => {
40
- expect(
41
- <Wrapper>
42
- <InterfaceMethod {...{ [methodModifier]: true }} name="MethodOne" />
43
- </Wrapper>,
44
- ).toRenderTo(`
45
- public interface TestInterface
46
- {
47
- ${methodModifier} void MethodOne();
48
- }
49
- `);
50
- });
51
- });
52
-
53
- it("combine modifiers", () => {
54
- expect(
55
- <Wrapper>
56
- <InterfaceMethod returns="Task" public new name="MethodOne" />
57
- </Wrapper>,
58
- ).toRenderTo(`
59
- public interface TestInterface
60
- {
61
- public new Task MethodOne();
62
- }
63
- `);
64
- });
65
- });
66
-
67
- it("applies PascalCase naming policy", () => {
68
- expect(
69
- <Wrapper>
70
- <InterfaceMethod name="method_one" />
71
- </Wrapper>,
72
- ).toRenderTo(`
73
- public interface TestInterface
74
- {
75
- void MethodOne();
76
- }
77
- `);
78
- });
79
-
80
- it("defines params and return type", () => {
81
- const params = [
82
- {
83
- name: "intParam",
84
- type: "int",
85
- },
86
- {
87
- name: "stringParam",
88
- type: "string",
89
- },
90
- ];
91
- const res = (
92
- <Wrapper>
93
- <InterfaceMethod
94
- public
95
- name="MethodOne"
96
- parameters={params}
97
- returns="string"
98
- />
99
- </Wrapper>
100
- );
101
-
102
- expect(res).toRenderTo(`
103
- public interface TestInterface
104
- {
105
- public string MethodOne(int intParam, string stringParam);
106
- }
107
- `);
108
- });
109
-
110
- it("defines optional param", () => {
111
- const res = (
112
- <Wrapper>
113
- <InterfaceMethod
114
- public
115
- name="MethodOne"
116
- parameters={[
117
- {
118
- name: "intParam",
119
- type: "int",
120
- optional: true,
121
- },
122
- ]}
123
- returns="string"
124
- />
125
- </Wrapper>
126
- );
127
-
128
- expect(res).toRenderTo(`
129
- public interface TestInterface
130
- {
131
- public string MethodOne(int? intParam);
132
- }
133
- `);
134
- });
135
-
136
- it("defines optional param with default", () => {
137
- const res = (
138
- <Wrapper>
139
- <InterfaceMethod
140
- public
141
- name="MethodOne"
142
- parameters={[
143
- {
144
- name: "intParam",
145
- type: "int",
146
- default: 12,
147
- },
148
- ]}
149
- returns="string"
150
- />
151
- </Wrapper>
152
- );
153
-
154
- expect(res).toRenderTo(`
155
- public interface TestInterface
156
- {
157
- public string MethodOne(int intParam = 12);
158
- }
159
- `);
160
- });
161
-
162
- it("specify doc comment", () => {
163
- expect(
164
- <TestNamespace>
165
- <InterfaceDeclaration name="Test">
166
- <InterfaceMethod name="Method" doc="This is a test" />
167
- </InterfaceDeclaration>
168
- </TestNamespace>,
169
- ).toRenderTo(`
170
- interface Test
171
- {
172
- /// This is a test
173
- void Method();
174
- }
175
- `);
176
- });
177
-
178
- it("specify attributes", () => {
179
- expect(
180
- <Wrapper>
181
- <InterfaceMethod name="Test" attributes={[<Attribute name="Test" />]} />
182
- </Wrapper>,
183
- ).toRenderTo(`
184
- public interface TestInterface
185
- {
186
- [Test]
187
- void Test();
188
- }
189
- `);
190
- });
191
-
192
- describe("with type parameters", () => {
193
- it("reference parameters", () => {
194
- const typeParameters: TypeParameterProps[] = [
195
- {
196
- name: "T",
197
- refkey: refkey(),
198
- },
199
- {
200
- name: "U",
201
- refkey: refkey(),
202
- },
203
- ];
204
-
205
- expect(
206
- <TestNamespace>
207
- <SourceFile path="TestFile.cs">
208
- <InterfaceDeclaration public name="TestInterface">
209
- <InterfaceMethod
210
- name="Test"
211
- public
212
- typeParameters={typeParameters}
213
- parameters={[
214
- {
215
- name: "paramA",
216
- type: typeParameters[0].refkey,
217
- },
218
- ]}
219
- returns={typeParameters[0].refkey}
220
- />
221
- </InterfaceDeclaration>
222
- </SourceFile>
223
- </TestNamespace>,
224
- ).toRenderTo(`
225
- namespace TestCode
226
- {
227
- public interface TestInterface
228
- {
229
- public T Test<T, U>(T paramA);
230
- }
231
- }
232
- `);
233
- });
234
-
235
- it("defines with constraints", () => {
236
- const typeParameters: TypeParameterProps[] = [
237
- {
238
- name: "T",
239
- constraints: "IFoo",
240
- },
241
- {
242
- name: "U",
243
- constraints: "IBar",
244
- },
245
- ];
246
-
247
- expect(
248
- <Wrapper>
249
- <InterfaceMethod public name="Test" typeParameters={typeParameters}>
250
- // Body
251
- </InterfaceMethod>
252
- </Wrapper>,
253
- ).toRenderTo(`
254
- public interface TestInterface
255
- {
256
- public void Test<T, U>()
257
- where T : IFoo
258
- where U : IBar
259
- {
260
- // Body
261
- }
262
- }
263
- `);
264
- });
265
- });
266
-
267
- describe("formatting", () => {
268
- it("Split parameters before type parameters", () => {
269
- expect(
270
- <Wrapper>
271
- <InterfaceMethod
272
- public
273
- name="Handle"
274
- parameters={[
275
- {
276
- name: "message",
277
- type: "Some.Quite.Long.Type.That.Will.Split",
278
- },
279
- ]}
280
- typeParameters={["T"]}
281
- returns="Some.Quite.Long.Type.That.Will.Split"
282
- />
283
- </Wrapper>,
284
- ).toRenderTo(`
285
- public interface TestInterface
286
- {
287
- public Some.Quite.Long.Type.That.Will.Split Handle<T>(
288
- Some.Quite.Long.Type.That.Will.Split message
289
- );
290
- }
291
- `);
292
- });
293
- });
@@ -1,122 +0,0 @@
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 { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
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";
24
-
25
- /** Method modifiers. Can only be one. */
26
- export interface InterfaceMethodModifiers {
27
- readonly new?: boolean;
28
- }
29
-
30
- const getMethodModifier = makeModifiers<InterfaceMethodModifiers>(["new"]);
31
-
32
- // properties for creating a method
33
- export interface InterfaceMethodProps
34
- extends AccessModifiers,
35
- InterfaceMethodModifiers {
36
- name: string;
37
- refkey?: Refkey;
38
- children?: Children;
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)[];
53
- returns?: Children;
54
-
55
- /** Doc comment */
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;
75
- }
76
-
77
- // a C# interface method
78
- export function InterfaceMethod(props: InterfaceMethodProps) {
79
- const name = useCSharpNamePolicy().getName(props.name, "class-method");
80
- const scope = useCSharpScope();
81
- if (scope.kind !== "member" || scope.name !== "interface-decl") {
82
- throw new Error(
83
- "can't define an interface method outside of an interface scope",
84
- );
85
- }
86
-
87
- const methodSymbol = new CSharpOutputSymbol(name, {
88
- scope,
89
- refkeys: props.refkey ?? refkey(props.name),
90
- });
91
-
92
- // scope for method declaration
93
- const methodScope = new CSharpMemberScope("method-decl", {
94
- owner: methodSymbol,
95
- });
96
-
97
- const modifiers = computeModifiersPrefix([
98
- getAccessModifier(props),
99
- getMethodModifier(props),
100
- ]);
101
- // note that scope wraps the method decl so that the params get the correct scope
102
- return (
103
- <MemberDeclaration symbol={methodSymbol}>
104
- <Scope value={methodScope}>
105
- <DocWhen doc={props.doc} />
106
- <AttributeList attributes={props.attributes} endline />
107
- {modifiers}
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
- )}
116
- {props.children ?
117
- <Block newline>{props.children}</Block>
118
- : ";"}
119
- </Scope>
120
- </MemberDeclaration>
121
- );
122
- }