@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,172 +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 { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
21
- import { DocWhen } from "../doc/comment.jsx";
22
-
23
- /** Property modifiers. */
24
- export interface PropertyModifiers {
25
- readonly new?: boolean;
26
- readonly static?: boolean;
27
- readonly virtual?: boolean;
28
- readonly sealed?: boolean;
29
- readonly override?: boolean;
30
- readonly abstract?: boolean;
31
- readonly extern?: boolean;
32
- readonly readonly?: boolean;
33
- /**
34
- * Set required modifier on property
35
- * https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required
36
- */
37
- readonly required?: boolean;
38
- }
39
-
40
- const getModifiers = makeModifiers<PropertyModifiers>([
41
- "new",
42
- "static",
43
- "virtual",
44
- "sealed",
45
- "override",
46
- "abstract",
47
- "extern",
48
- "readonly",
49
- "required",
50
- ]);
51
-
52
- /** Properties for {@link Property} component */
53
- export interface PropertyProps extends AccessModifiers, PropertyModifiers {
54
- name: string;
55
- refkey?: Refkey;
56
-
57
- /** Property type */
58
- type: Children;
59
-
60
- /** If property should have a getter */
61
- get?: boolean;
62
-
63
- /** If property should have a setter */
64
- set?: boolean;
65
-
66
- /** If property should only be set on the type creation */
67
- init?: boolean;
68
-
69
- /** Doc comment */
70
- doc?: Children;
71
-
72
- /**
73
- * Property initializer
74
- * @example `<ClassProperty name="My" get set nullable />`
75
- *
76
- * ```cs
77
- * int? My { get; set; };
78
- * ```
79
- */
80
- nullable?: boolean;
81
-
82
- /**
83
- * Property initializer
84
- * @example `<ClassProperty name="My" get set init={42} />`
85
- *
86
- * ```cs
87
- * int My { get; set; } = 42;
88
- * ```
89
- */
90
- initializer?: Children;
91
-
92
- /**
93
- * Define attributes to attach
94
- * @example
95
- * ```tsx
96
- * <Property name="MyProp" attributes={[
97
- * <Attribute name="Test" />
98
- * <Attribute name="Test2" args={["arg1", "arg2"]} />
99
- * ]} />
100
- * ```
101
- * This will produce:
102
- * ```csharp
103
- * [Test]
104
- * [Test2("arg1", "arg2")]
105
- * int MyProp { get; set; }
106
- * ```
107
- */
108
- attributes?: AttributesProp;
109
- }
110
-
111
- /**
112
- * Render a C# class property.
113
- *
114
- * @example `<ClassProperty public name="My" get set />`
115
- *
116
- * ```cs
117
- * public int My { get; set; };
118
- * ```
119
- */
120
- export function Property(props: PropertyProps) {
121
- const name = useCSharpNamePolicy().getName(props.name, "class-property");
122
- const scope = useCSharpScope();
123
- if (
124
- scope.kind !== "member" ||
125
- (scope.name !== "class-decl" && scope.name !== "record-decl")
126
- ) {
127
- throw new Error(
128
- "can't define an interface method outside of an interface scope",
129
- );
130
- }
131
-
132
- const propertySymbol = new CSharpOutputSymbol(name, {
133
- scope,
134
- refkeys: props.refkey ?? refkey(props.name),
135
- });
136
-
137
- // scope for property declaration
138
- const propertyScope = new CSharpMemberScope("property-decl", {
139
- owner: propertySymbol,
140
- });
141
-
142
- const modifiers = computeModifiersPrefix([
143
- getAccessModifier(props),
144
- getModifiers(props),
145
- ]);
146
-
147
- if (props.init && props.set) {
148
- throw new Error(
149
- `Cannot use 'init' and 'set' together on property '${name}'`,
150
- );
151
- }
152
- // note that scope wraps the method decl so that the params get the correct scope
153
- return (
154
- <MemberDeclaration symbol={propertySymbol}>
155
- <Scope value={propertyScope}>
156
- <DocWhen doc={props.doc} />
157
- <AttributeList attributes={props.attributes} endline />
158
- {modifiers}
159
- {props.type}
160
- {props.nullable && "?"} {name}{" "}
161
- <Block newline inline>
162
- <List joiner=" ">
163
- {props.get && "get;"}
164
- {props.set && "set;"}
165
- {props.init && "init;"}
166
- </List>
167
- </Block>
168
- {props.initializer && code` = ${props.initializer};`}
169
- </Scope>
170
- </MemberDeclaration>
171
- );
172
- }
@@ -1,73 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { TestNamespace } from "../../../test/utils.jsx";
3
- import { Property } from "../property/property.jsx";
4
- import { RecordDeclaration } from "./declaration.jsx";
5
-
6
- it("declares class with no members", () => {
7
- expect(
8
- <TestNamespace>
9
- <RecordDeclaration name="TestRecord" />
10
- </TestNamespace>,
11
- ).toRenderTo(`
12
- record TestRecord;
13
- `);
14
- });
15
-
16
- describe("modifiers", () => {
17
- it.each(["public", "private", "internal"])("%s", (mod) => {
18
- expect(
19
- <TestNamespace>
20
- <RecordDeclaration {...{ [mod]: true }} name="TestRecord" />
21
- </TestNamespace>,
22
- ).toRenderTo(`
23
- ${mod} record TestRecord;
24
- `);
25
- });
26
-
27
- it.each(["partial"])("%s", (mod) => {
28
- expect(
29
- <TestNamespace>
30
- <RecordDeclaration {...{ [mod]: true }} name="TestRecord" />
31
- </TestNamespace>,
32
- ).toRenderTo(`
33
- ${mod} record TestRecord;
34
- `);
35
- });
36
-
37
- it("combines modifiers", () => {
38
- expect(
39
- <TestNamespace>
40
- <RecordDeclaration public partial name="TestRecord" />
41
- </TestNamespace>,
42
- ).toRenderTo(`
43
- public partial record TestRecord;
44
- `);
45
- });
46
- });
47
-
48
- it("specify doc comment", () => {
49
- expect(
50
- <TestNamespace>
51
- <RecordDeclaration name="TestRecord" doc="This is a test" />
52
- </TestNamespace>,
53
- ).toRenderTo(`
54
- /// This is a test
55
- record TestRecord;
56
- `);
57
- });
58
-
59
- it("specify class property inside", () => {
60
- expect(
61
- <TestNamespace>
62
- <RecordDeclaration name="TestRecord" doc="This is a test">
63
- <Property name="Prop" get set type="string" />
64
- </RecordDeclaration>
65
- </TestNamespace>,
66
- ).toRenderTo(`
67
- /// This is a test
68
- record TestRecord
69
- {
70
- string Prop { get; set; }
71
- }
72
- `);
73
- });
@@ -1,109 +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 { DocWhen } from "../doc/comment.jsx";
12
- import { Name } from "../Name.jsx";
13
-
14
- export interface RecordModifiers {
15
- readonly partial?: boolean;
16
- }
17
-
18
- const getRecordModifiers = makeModifiers<RecordModifiers>(["partial"]);
19
-
20
- /** Props to use the {@link RecordDeclaration} component */
21
- export interface RecordDeclarationProps
22
- extends Omit<core.DeclarationProps, "nameKind">,
23
- AccessModifiers,
24
- RecordModifiers {
25
- name: string;
26
-
27
- /** Doc comment */
28
- doc?: core.Children;
29
- refkey?: core.Refkey;
30
- typeParameters?: Record<string, core.Refkey>;
31
- }
32
-
33
- /**
34
- * CSharp record declaration.
35
- * @example
36
- * ```tsx
37
- * <RecordDeclaration public name="IMyRecord">
38
- * <RecordMember public name="MyProperty" type="int" />
39
- * <RecordMethod public name="MyMethod" returnType="void">
40
- * <Parameter name="value" type="int" />
41
- * </RecordMethod>
42
- * </RecordDeclaration>
43
- * ```
44
- * This will produce:
45
- * ```csharp
46
- * public record MyIface
47
- * {
48
- * public int MyProperty { get; set; }
49
- * public void MyMethod(int value);
50
- * }
51
- * ```
52
- */
53
- export function RecordDeclaration(props: RecordDeclarationProps) {
54
- const name = useCSharpNamePolicy().getName(props.name!, "record");
55
-
56
- const thisRecordSymbol = new CSharpOutputSymbol(name, {
57
- refkeys: props.refkey,
58
- });
59
-
60
- // this creates a new scope for the record definition.
61
- // members will automatically "inherit" this scope so
62
- // that refkeys to them will produce the fully-qualified
63
- // name e.g. Foo.Bar.
64
- const thisRecordScope = new CSharpMemberScope("record-decl", {
65
- owner: thisRecordSymbol,
66
- });
67
-
68
- let typeParams: core.Children;
69
- if (props.typeParameters) {
70
- const typeParamNames = new Array<string>();
71
- for (const entry of Object.entries(props.typeParameters)) {
72
- typeParamNames.push(
73
- useCSharpNamePolicy().getName(entry[0], "type-parameter"),
74
- );
75
- // create a symbol for each type param so its
76
- // refkey resolves to the type param's name
77
- new CSharpOutputSymbol(entry[0], {
78
- scope: thisRecordScope,
79
- refkeys: entry[1],
80
- });
81
- }
82
- typeParams = (
83
- <group>
84
- {"<"}
85
- <core.For each={typeParamNames} comma line>
86
- {(name) => name}
87
- </core.For>
88
- {">"}
89
- </group>
90
- );
91
- }
92
-
93
- const modifiers = computeModifiersPrefix([
94
- getAccessModifier(props),
95
- getRecordModifiers(props),
96
- ]);
97
- return (
98
- <core.Declaration symbol={thisRecordSymbol}>
99
- <DocWhen doc={props.doc} />
100
- {modifiers}record <Name />
101
- {typeParams}
102
- {props.children ?
103
- <core.Block newline>
104
- <core.Scope value={thisRecordScope}>{props.children}</core.Scope>
105
- </core.Block>
106
- : ";"}
107
- </core.Declaration>
108
- );
109
- }
@@ -1,93 +0,0 @@
1
- import { expect, it } from "vitest";
2
- import { TypeParameterConstraints } from "./type-parameter-constraints.jsx";
3
-
4
- it("renders nothing if there is no constraints", () => {
5
- expect(<TypeParameterConstraints parameters={["A", "B"]} />).toRenderTo(``);
6
- });
7
-
8
- it("renders inline if there is only one", () => {
9
- expect(
10
- <>
11
- {"code()"}
12
- <TypeParameterConstraints
13
- parameters={[{ name: "A", constraints: "string" }]}
14
- />
15
- </>,
16
- ).toRenderTo(`
17
- code() where A : string
18
- `);
19
- });
20
-
21
- it("renders multiple constraints", () => {
22
- expect(
23
- <>
24
- {"code()"}
25
- <TypeParameterConstraints
26
- parameters={[{ name: "A", constraints: ["string", "int"] }]}
27
- />
28
- </>,
29
- ).toRenderTo(`
30
- code() where A : string, int
31
- `);
32
- });
33
-
34
- it("renders newline if constraint is very long", () => {
35
- expect(
36
- <>
37
- {"code()"}
38
- <TypeParameterConstraints
39
- parameters={[
40
- {
41
- name: "ThisIsQuiteALongName",
42
- constraints: "VeryLongBuilderFactorySingletonThatShouldBeSplit",
43
- },
44
- ]}
45
- />
46
- </>,
47
- ).toRenderTo(`
48
- code()
49
- where ThisIsQuiteALongName : VeryLongBuilderFactorySingletonThatShouldBeSplit
50
- `);
51
- });
52
-
53
- it("renders multiple constraints on new lines if they are very long", () => {
54
- expect(
55
- <>
56
- {"code()"}
57
- <TypeParameterConstraints
58
- parameters={[
59
- {
60
- name: "A",
61
- constraints: [
62
- "IVeryLongBuilderFactorySingletonThatShouldBeSplitA",
63
- "IVeryLongBuilderFactorySingletonThatShouldBeSplitB",
64
- ],
65
- },
66
- ]}
67
- />
68
- </>,
69
- ).toRenderTo(`
70
- code()
71
- where A :
72
- IVeryLongBuilderFactorySingletonThatShouldBeSplitA,
73
- IVeryLongBuilderFactorySingletonThatShouldBeSplitB
74
- `);
75
- });
76
-
77
- it("declare type parameters using parameters", () => {
78
- expect(
79
- <>
80
- {"code()"}
81
- <TypeParameterConstraints
82
- parameters={[
83
- { name: "A", constraints: "string" },
84
- { name: "B", constraints: "string" },
85
- ]}
86
- />
87
- </>,
88
- ).toRenderTo(`
89
- code()
90
- where A : string
91
- where B : string
92
- `);
93
- });
@@ -1,46 +0,0 @@
1
- import { Children, code, For, Indent } from "@alloy-js/core";
2
- import { TypeParameterProps } from "./type-parameter.jsx";
3
- import { normalizeParameters } from "./type-parameters.jsx";
4
-
5
- export interface TypeParameterConstraintsProps {
6
- /** Parameters */
7
- parameters: (TypeParameterProps | string)[];
8
- }
9
-
10
- export function TypeParameterConstraints(
11
- props: TypeParameterConstraintsProps,
12
- ): Children {
13
- const parameters = normalizeParameters(props.parameters);
14
- if (!parameters.some((x) => x.constraints)) {
15
- return null;
16
- }
17
- return (
18
- <group>
19
- <Indent line>
20
- <For each={parameters} hardline>
21
- {(param) => <TypeParameterConstraint {...param} />}
22
- </For>
23
- </Indent>
24
- </group>
25
- );
26
- }
27
-
28
- function TypeParameterConstraint(props: TypeParameterProps): Children {
29
- const constraints = arrayify(props.constraints);
30
- return (
31
- <>
32
- where {code`${props.name} : `}
33
- <group>
34
- <Indent softline>
35
- <For each={constraints} comma line>
36
- {(constraint) => code`${constraint}`}
37
- </For>
38
- </Indent>
39
- </group>
40
- </>
41
- );
42
- }
43
-
44
- function arrayify<T>(value: T | T[]): T[] {
45
- return Array.isArray(value) ? value : [value];
46
- }
@@ -1,35 +0,0 @@
1
- import { Children, MemberDeclaration, refkey, Refkey } from "@alloy-js/core";
2
- import { useCSharpNamePolicy } from "../../name-policy.js";
3
- import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
4
- import { useCSharpScope } from "../../symbols/scopes.js";
5
-
6
- /**
7
- * Information for a TypeScript generic type parameter.
8
- */
9
- export interface TypeParameterProps {
10
- /**
11
- * The name of the type parameter.
12
- */
13
- readonly name: string;
14
-
15
- /**
16
- * The parameter constraint
17
- */
18
- readonly constraints?: Children | Children[];
19
-
20
- /**
21
- * A refkey or array of refkeys for this type parameter.
22
- */
23
- readonly refkey?: Refkey | Refkey[];
24
- }
25
-
26
- export function TypeParameter(props: TypeParameterProps) {
27
- const name = useCSharpNamePolicy().getName(props.name, "type-parameter");
28
- const scope = useCSharpScope();
29
- const symbol = new CSharpOutputSymbol(name, {
30
- scope,
31
- refkeys: props.refkey ?? refkey(props.name),
32
- });
33
-
34
- return <MemberDeclaration symbol={symbol}>{name}</MemberDeclaration>;
35
- }
@@ -1,46 +0,0 @@
1
- import { expect, it } from "vitest";
2
- import { TestNamespace } from "../../../test/utils.jsx";
3
- import { TypeParameters } from "./type-parameters.jsx";
4
-
5
- it("render one", () => {
6
- expect(
7
- <TestNamespace>
8
- <TypeParameters parameters={["A"]} />
9
- </TestNamespace>,
10
- ).toRenderTo(`<A>`);
11
- });
12
-
13
- it("render very long", () => {
14
- expect(
15
- <TestNamespace>
16
- <TypeParameters
17
- parameters={[
18
- "SomeVeryVeryLongParamThatMightGetSplitOverMultipleLinesA",
19
- "SomeVeryVeryLongParamThatMightGetSplitOverMultipleLinesB",
20
- ]}
21
- />
22
- </TestNamespace>,
23
- ).toRenderTo(
24
- `
25
- <
26
- SomeVeryVeryLongParamThatMightGetSplitOverMultipleLinesA,
27
- SomeVeryVeryLongParamThatMightGetSplitOverMultipleLinesB>
28
- `,
29
- );
30
- });
31
-
32
- it("declare type parameters using parameters names", () => {
33
- expect(
34
- <TestNamespace>
35
- <TypeParameters parameters={["A", "B"]} />
36
- </TestNamespace>,
37
- ).toRenderTo(`<A, B>`);
38
- });
39
-
40
- it("declare type parameters using parameters", () => {
41
- expect(
42
- <TestNamespace>
43
- <TypeParameters parameters={[{ name: "A" }, { name: "B" }]} />
44
- </TestNamespace>,
45
- ).toRenderTo(`<A, B>`);
46
- });
@@ -1,63 +0,0 @@
1
- import { For, Indent, taggedComponent } from "@alloy-js/core";
2
- import { TypeParameter, TypeParameterProps } from "./type-parameter.jsx";
3
-
4
- export const typeParametersTag = Symbol.for("csharp.type-parameters");
5
-
6
- export interface TypeParametersProps {
7
- /** Parameters */
8
- parameters: (TypeParameterProps | string)[];
9
- }
10
-
11
- /**
12
- * Represent type parameters
13
- *
14
- * @example
15
- * ```ts
16
- * <A, B extends string>
17
- * ```
18
- */
19
- export const TypeParameters = taggedComponent(
20
- typeParametersTag,
21
- function TypeParameters(props: TypeParametersProps) {
22
- const typeParameters = normalizeParameters(props.parameters);
23
-
24
- return (
25
- <>
26
- {"<"}
27
- <group>
28
- <Indent softline>
29
- <For each={typeParameters} comma line>
30
- {(param) => <TypeParameter {...param} />}
31
- </For>
32
- </Indent>
33
- </group>
34
- {">"}
35
- </>
36
- );
37
- },
38
- );
39
-
40
- export function normalizeParameters(
41
- parameters: (TypeParameterProps | string)[],
42
- ): TypeParameterProps[] {
43
- return parameters.map((param) => {
44
- if (typeof param === "string") {
45
- return { name: param };
46
- }
47
- return param;
48
- });
49
- }
50
-
51
- // export function declareParameter(
52
- // parameters: TypeParameterProps[],
53
- // ): TypeParameterProps[] {
54
- // return parameters.map((param) => {
55
- // return {
56
- // ...param,
57
- // symbol: new CSharpOutputSymbol(entry[0], {
58
- // scope: thisClassScope,
59
- // refkeys: entry[1],
60
- // }),
61
- // };
62
- // });
63
- // }
@@ -1,59 +0,0 @@
1
- import { List, refkey } from "@alloy-js/core";
2
- import { expect, it } from "vitest";
3
- import { TestNamespace } from "../../../test/utils.jsx";
4
- import { SourceFile } from "../SourceFile.jsx";
5
- import { VarDeclaration } from "./declaration.jsx";
6
-
7
- it("declares var without type", () => {
8
- expect(
9
- <TestNamespace>
10
- <VarDeclaration name="testVar">42</VarDeclaration>
11
- </TestNamespace>,
12
- ).toRenderTo(`
13
- var testVar = 42;
14
- `);
15
- });
16
-
17
- it("declares var with type", () => {
18
- expect(
19
- <TestNamespace>
20
- <VarDeclaration name="testVar" type="int">
21
- 42
22
- </VarDeclaration>
23
- </TestNamespace>,
24
- ).toRenderTo(`
25
- int testVar = 42;
26
- `);
27
- });
28
-
29
- it("name variables camel case", () => {
30
- expect(
31
- <TestNamespace>
32
- <VarDeclaration name="test_var">42</VarDeclaration>
33
- </TestNamespace>,
34
- ).toRenderTo(`
35
- var testVar = 42;
36
- `);
37
- });
38
-
39
- it("links refkey", () => {
40
- const key = refkey();
41
- expect(
42
- <TestNamespace>
43
- <SourceFile path="test.cs">
44
- <List>
45
- <VarDeclaration name="testVar" refkey={key}>
46
- 42
47
- </VarDeclaration>
48
- <VarDeclaration name="testVar2">{key}</VarDeclaration>
49
- </List>
50
- </SourceFile>
51
- </TestNamespace>,
52
- ).toRenderTo(`
53
- namespace TestCode
54
- {
55
- var testVar = 42;
56
- var testVar2 = testVar;
57
- }
58
- `);
59
- });