@alloy-js/csharp 0.18.0-dev.2 → 0.18.0-dev.21

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 (158) hide show
  1. package/LICENSE +7 -0
  2. package/dist/src/components/ClassDeclaration.d.ts +74 -0
  3. package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
  4. package/dist/src/components/{Class.js → ClassDeclaration.js} +64 -38
  5. package/dist/src/components/ClassMethod.d.ts +26 -5
  6. package/dist/src/components/ClassMethod.d.ts.map +1 -1
  7. package/dist/src/components/ClassMethod.js +27 -9
  8. package/dist/src/components/EnumDeclaration.d.ts +34 -0
  9. package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
  10. package/dist/src/components/{Enum.js → EnumDeclaration.js} +25 -5
  11. package/dist/src/components/doc/comment.d.ts +70 -0
  12. package/dist/src/components/doc/comment.d.ts.map +1 -0
  13. package/dist/src/components/doc/comment.js +88 -0
  14. package/dist/src/components/doc/comment.test.d.ts +2 -0
  15. package/dist/src/components/doc/comment.test.d.ts.map +1 -0
  16. package/dist/src/components/doc/comment.test.js +348 -0
  17. package/dist/src/components/doc/from-markdown.d.ts +6 -0
  18. package/dist/src/components/doc/from-markdown.d.ts.map +1 -0
  19. package/dist/src/components/doc/from-markdown.js +58 -0
  20. package/dist/src/components/doc/from-markdown.test.d.ts +2 -0
  21. package/dist/src/components/doc/from-markdown.test.d.ts.map +1 -0
  22. package/dist/src/components/doc/from-markdown.test.js +83 -0
  23. package/dist/src/components/index.d.ts +13 -4
  24. package/dist/src/components/index.d.ts.map +1 -1
  25. package/dist/src/components/index.js +14 -5
  26. package/dist/src/components/interface/declaration.d.ts +47 -0
  27. package/dist/src/components/interface/declaration.d.ts.map +1 -0
  28. package/dist/src/components/interface/declaration.js +77 -0
  29. package/dist/src/components/interface/declaration.test.d.ts +2 -0
  30. package/dist/src/components/interface/declaration.test.d.ts.map +1 -0
  31. package/dist/src/components/interface/declaration.test.js +153 -0
  32. package/dist/src/components/interface/method.d.ts +32 -0
  33. package/dist/src/components/interface/method.d.ts.map +1 -0
  34. package/dist/src/components/interface/method.js +68 -0
  35. package/dist/src/components/interface/method.test.d.ts +2 -0
  36. package/dist/src/components/interface/method.test.d.ts.map +1 -0
  37. package/dist/src/components/interface/method.test.js +279 -0
  38. package/dist/src/components/interface/property.d.ts +38 -0
  39. package/dist/src/components/interface/property.d.ts.map +1 -0
  40. package/dist/src/components/interface/property.js +67 -0
  41. package/dist/src/components/interface/property.test.d.ts +2 -0
  42. package/dist/src/components/interface/property.test.d.ts.map +1 -0
  43. package/dist/src/components/interface/property.test.js +165 -0
  44. package/dist/src/components/parameters/parameters.d.ts +19 -0
  45. package/dist/src/components/parameters/parameters.d.ts.map +1 -0
  46. package/dist/src/components/parameters/parameters.js +43 -0
  47. package/dist/src/components/property/property.d.ts +57 -0
  48. package/dist/src/components/property/property.d.ts.map +1 -0
  49. package/dist/src/components/property/property.js +70 -0
  50. package/dist/src/components/property/property.test.d.ts +2 -0
  51. package/dist/src/components/property/property.test.d.ts.map +1 -0
  52. package/dist/src/components/property/property.test.js +218 -0
  53. package/dist/src/components/record/declaration.d.ts +35 -0
  54. package/dist/src/components/record/declaration.d.ts.map +1 -0
  55. package/dist/src/components/record/declaration.js +90 -0
  56. package/dist/src/components/record/declaration.test.d.ts +2 -0
  57. package/dist/src/components/record/declaration.test.d.ts.map +1 -0
  58. package/dist/src/components/record/declaration.test.js +94 -0
  59. package/dist/src/components/stc/index.d.ts +2 -2
  60. package/dist/src/components/stc/index.d.ts.map +1 -1
  61. package/dist/src/components/stc/index.js +2 -2
  62. package/dist/src/components/type-parameters/type-parameter-constraints.d.ts +8 -0
  63. package/dist/src/components/type-parameters/type-parameter-constraints.d.ts.map +1 -0
  64. package/dist/src/components/type-parameters/type-parameter-constraints.js +44 -0
  65. package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts +2 -0
  66. package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts.map +1 -0
  67. package/dist/src/components/type-parameters/type-parameter-constraints.test.js +67 -0
  68. package/dist/src/components/type-parameters/type-parameter.d.ts +20 -0
  69. package/dist/src/components/type-parameters/type-parameter.d.ts.map +1 -0
  70. package/dist/src/components/type-parameters/type-parameter.js +22 -0
  71. package/dist/src/components/type-parameters/type-parameters.d.ts +17 -0
  72. package/dist/src/components/type-parameters/type-parameters.d.ts.map +1 -0
  73. package/dist/src/components/type-parameters/type-parameters.js +54 -0
  74. package/dist/src/components/type-parameters/type-parameters.test.d.ts +2 -0
  75. package/dist/src/components/type-parameters/type-parameters.test.d.ts.map +1 -0
  76. package/dist/src/components/type-parameters/type-parameters.test.js +48 -0
  77. package/dist/src/components/var/declaration.d.ts +35 -0
  78. package/dist/src/components/var/declaration.d.ts.map +1 -0
  79. package/dist/src/components/var/declaration.js +40 -0
  80. package/dist/src/components/var/declaration.test.d.ts +2 -0
  81. package/dist/src/components/var/declaration.test.d.ts.map +1 -0
  82. package/dist/src/components/var/declaration.test.js +73 -0
  83. package/dist/src/modifiers.d.ts +10 -4
  84. package/dist/src/modifiers.d.ts.map +1 -1
  85. package/dist/src/modifiers.js +9 -32
  86. package/dist/src/name-policy.d.ts +1 -1
  87. package/dist/src/name-policy.d.ts.map +1 -1
  88. package/dist/src/name-policy.js +2 -0
  89. package/dist/test/class-declaration.test.d.ts +2 -0
  90. package/dist/test/class-declaration.test.d.ts.map +1 -0
  91. package/dist/test/class-declaration.test.js +463 -0
  92. package/dist/test/class-method.test.js +37 -14
  93. package/dist/test/enum.test.js +12 -12
  94. package/dist/test/namespace.test.js +8 -8
  95. package/dist/test/project-directory.test.d.ts +2 -0
  96. package/dist/test/project-directory.test.d.ts.map +1 -0
  97. package/dist/test/{projectdirectory.test.js → project-directory.test.js} +8 -8
  98. package/dist/test/sourcefile.test.js +4 -4
  99. package/dist/test/using.test.js +9 -9
  100. package/dist/test/vitest.setup.d.ts +2 -0
  101. package/dist/test/vitest.setup.d.ts.map +1 -0
  102. package/dist/test/vitest.setup.js +1 -0
  103. package/dist/tsconfig.tsbuildinfo +1 -1
  104. package/package.json +21 -21
  105. package/src/components/ClassDeclaration.tsx +213 -0
  106. package/src/components/ClassMethod.tsx +54 -15
  107. package/src/components/{Enum.tsx → EnumDeclaration.tsx} +30 -6
  108. package/src/components/doc/comment.test.tsx +337 -0
  109. package/src/components/doc/comment.tsx +152 -0
  110. package/src/components/doc/from-markdown.test.tsx +103 -0
  111. package/src/components/doc/from-markdown.tsx +58 -0
  112. package/src/components/index.ts +13 -4
  113. package/src/components/interface/declaration.test.tsx +143 -0
  114. package/src/components/interface/declaration.tsx +105 -0
  115. package/src/components/interface/method.test.tsx +278 -0
  116. package/src/components/interface/method.tsx +102 -0
  117. package/src/components/interface/property.test.tsx +144 -0
  118. package/src/components/interface/property.tsx +107 -0
  119. package/src/components/parameters/parameters.tsx +74 -0
  120. package/src/components/property/property.test.tsx +187 -0
  121. package/src/components/property/property.tsx +146 -0
  122. package/src/components/record/declaration.test.tsx +73 -0
  123. package/src/components/record/declaration.tsx +109 -0
  124. package/src/components/stc/index.ts +2 -2
  125. package/src/components/type-parameters/type-parameter-constraints.test.tsx +93 -0
  126. package/src/components/type-parameters/type-parameter-constraints.tsx +46 -0
  127. package/src/components/type-parameters/type-parameter.tsx +35 -0
  128. package/src/components/type-parameters/type-parameters.test.tsx +46 -0
  129. package/src/components/type-parameters/type-parameters.tsx +63 -0
  130. package/src/components/var/declaration.test.tsx +59 -0
  131. package/src/components/var/declaration.tsx +47 -0
  132. package/src/modifiers.ts +25 -42
  133. package/src/name-policy.ts +5 -0
  134. package/temp/api.json +5228 -686
  135. package/test/class-declaration.test.tsx +418 -0
  136. package/test/class-method.test.tsx +24 -14
  137. package/test/enum.test.tsx +11 -11
  138. package/test/namespace.test.tsx +4 -4
  139. package/test/{projectdirectory.test.tsx → project-directory.test.tsx} +4 -4
  140. package/test/sourcefile.test.tsx +2 -2
  141. package/test/using.test.tsx +9 -9
  142. package/test/vitest.setup.ts +1 -0
  143. package/vitest.config.ts +3 -0
  144. package/dist/src/components/Class.d.ts +0 -26
  145. package/dist/src/components/Class.d.ts.map +0 -1
  146. package/dist/src/components/Enum.d.ts +0 -15
  147. package/dist/src/components/Enum.d.ts.map +0 -1
  148. package/dist/src/components/Parameters.d.ts +0 -13
  149. package/dist/src/components/Parameters.d.ts.map +0 -1
  150. package/dist/src/components/Parameters.js +0 -34
  151. package/dist/test/class.test.d.ts +0 -2
  152. package/dist/test/class.test.d.ts.map +0 -1
  153. package/dist/test/class.test.js +0 -298
  154. package/dist/test/projectdirectory.test.d.ts +0 -2
  155. package/dist/test/projectdirectory.test.d.ts.map +0 -1
  156. package/src/components/Class.tsx +0 -149
  157. package/src/components/Parameters.tsx +0 -51
  158. package/test/class.test.tsx +0 -292
@@ -0,0 +1,73 @@
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
+ });
@@ -0,0 +1,109 @@
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,11 +1,11 @@
1
1
  import * as core from "@alloy-js/core";
2
2
  import * as base from "../index.js";
3
3
 
4
- export const Class = core.stc(base.Class);
4
+ export const ClassDeclaration = core.stc(base.ClassDeclaration);
5
5
  export const ClassConstructor = core.stc(base.ClassConstructor);
6
6
  export const ClassMember = core.stc(base.ClassMember);
7
7
  export const ClassMethod = core.stc(base.ClassMethod);
8
- export const Enum = core.stc(base.Enum);
8
+ export const EnumDeclaration = core.stc(base.EnumDeclaration);
9
9
  export const EnumMember = core.stc(base.EnumMember);
10
10
  export const Parameter = core.stc(base.Parameter);
11
11
  export const Parameters = core.stc(base.Parameters);
@@ -0,0 +1,93 @@
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
+ });
@@ -0,0 +1,46 @@
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
+ }
@@ -0,0 +1,35 @@
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
+ }
@@ -0,0 +1,46 @@
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
+ });
@@ -0,0 +1,63 @@
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
+ // }
@@ -0,0 +1,59 @@
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
+ });
@@ -0,0 +1,47 @@
1
+ import { Children, DeclarationProps, Name, Refkey } from "@alloy-js/core";
2
+ import { useCSharpNamePolicy } from "../../name-policy.js";
3
+ import { Declaration } from "../Declaration.jsx";
4
+
5
+ /** Props for {@link VarDeclaration} component */
6
+ export interface VarDeclarationProps
7
+ extends Omit<DeclarationProps, "nameKind"> {
8
+ /** Variable name */
9
+ name: string;
10
+ /** Type of the variable declaration. If not specified, defaults to "var" */
11
+ type?: Children;
12
+ /** Variable refkey */
13
+ refkey?: Refkey;
14
+ /** Variable value */
15
+ children?: Children;
16
+ }
17
+
18
+ /**
19
+ * Render a variable declaration
20
+ *
21
+ * @example with var
22
+ * ```tsx
23
+ * <VarDeclaration name="myVar">42</VarDeclaration>
24
+ * ```
25
+ * This will render:
26
+ * ```csharp
27
+ * var myVar = 42;
28
+ * ```
29
+ *
30
+ * @example with type
31
+ * ```tsx
32
+ * <VarDeclaration name="myVar" type="int">42</VarDeclaration>
33
+ * ```
34
+ * This will render:
35
+ * ```csharp
36
+ * int myVar = 42;
37
+ * ```
38
+ */
39
+ export function VarDeclaration(props: VarDeclarationProps) {
40
+ const name = useCSharpNamePolicy().getName(props.name, "variable");
41
+
42
+ return (
43
+ <Declaration name={name} refkey={props.refkey}>
44
+ {props.type ?? "var"} <Name /> = {props.children};
45
+ </Declaration>
46
+ );
47
+ }
package/src/modifiers.ts CHANGED
@@ -1,51 +1,25 @@
1
1
  // the possible C# access modifiers
2
2
  // https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers
3
- export type AccessModifier =
4
- | "public"
5
- | "protected"
6
- | "private"
7
- | "internal"
8
- | "protected-internal"
9
- | "private-protected"
10
- | "file";
11
3
 
12
- // maps the above access modifier value to its C# syntax.
13
- // note that the C# keyword includes a trailing space
14
- const accessModifierLookup: Record<AccessModifier, string> = {
15
- public: "public ",
16
- protected: "protected ",
17
- private: "private ",
18
- internal: "internal ",
19
- "protected-internal": "protected internal ",
20
- "private-protected": "private protected ",
21
- file: "file ",
22
- };
23
-
24
- // returns the C# syntax for the specified access modifier.
25
- // if no access modifier is specified, the empty string is returned.
26
- export function getAccessModifier(accessModifier?: AccessModifier): string {
27
- return accessModifier ? accessModifierLookup[accessModifier] : "";
4
+ /** Access modifiers. */
5
+ export interface AccessModifiers {
6
+ readonly public?: boolean;
7
+ readonly protected?: boolean;
8
+ readonly private?: boolean;
9
+ readonly internal?: boolean;
10
+ readonly file?: boolean;
28
11
  }
29
12
 
30
- export type MethodModifier = "abstract" | "sealed" | "static" | "virtual";
31
-
32
- // maps the above method modifier value to its C# syntax.
33
- // note that the C# keyword includes a trailing space
34
- const methodModifierLookup: Record<MethodModifier, string> = {
35
- abstract: "abstract ",
36
- sealed: "sealed ",
37
- static: "static ",
38
- virtual: "virtual ",
39
- };
40
-
41
- // returns the C# syntax for the specified method modifier.
42
- // if no method modifier is specified, the empty string is returned.
43
- export function getMethodModifier(methodModifier?: MethodModifier): string {
44
- return methodModifier ? methodModifierLookup[methodModifier] : "";
45
- }
13
+ export const getAccessModifier = makeModifiers<AccessModifiers>([
14
+ "public",
15
+ "protected",
16
+ "private",
17
+ "internal",
18
+ "file",
19
+ ]);
46
20
 
47
21
  export function getAsyncModifier(async?: boolean): string {
48
- return async ? "async " : "";
22
+ return async ? "async" : "";
49
23
  }
50
24
 
51
25
  /** Resolve the modifier prefix */
@@ -53,5 +27,14 @@ export function computeModifiersPrefix(
53
27
  modifiers: Array<string | undefined>,
54
28
  ): string {
55
29
  const resolved = modifiers.filter((x) => x);
56
- return resolved.length > 0 ? resolved.join("") : "";
30
+ return resolved.length > 0 ? resolved.join(" ") + " " : "";
31
+ }
32
+
33
+ export function makeModifiers<T>(obj: Array<keyof T>) {
34
+ return (data: T) => {
35
+ return obj
36
+ .map((key) => (data[key] ? key : undefined))
37
+ .filter((x) => x)
38
+ .join(" ");
39
+ };
57
40
  }