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

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 (168) hide show
  1. package/dist/src/components/Class.d.ts +26 -0
  2. package/dist/src/components/Class.d.ts.map +1 -0
  3. package/dist/src/components/{ClassDeclaration.js → Class.js} +38 -70
  4. package/dist/src/components/ClassMethod.d.ts +5 -44
  5. package/dist/src/components/ClassMethod.d.ts.map +1 -1
  6. package/dist/src/components/ClassMethod.js +9 -33
  7. package/dist/src/components/Enum.d.ts +15 -0
  8. package/dist/src/components/Enum.d.ts.map +1 -0
  9. package/dist/src/components/{EnumDeclaration.js → Enum.js} +5 -25
  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 +4 -10
  22. package/dist/src/modifiers.d.ts.map +1 -1
  23. package/dist/src/modifiers.js +32 -9
  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 +14 -37
  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.test.js +298 -0
  31. package/dist/test/enum.test.js +12 -12
  32. package/dist/test/namespace.test.js +8 -8
  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} +8 -8
  36. package/dist/test/sourcefile.test.js +4 -4
  37. package/dist/test/using.test.js +9 -9
  38. package/dist/tsconfig.tsbuildinfo +1 -1
  39. package/package.json +3 -4
  40. package/src/components/Class.tsx +149 -0
  41. package/src/components/ClassMethod.tsx +15 -74
  42. package/src/components/{EnumDeclaration.tsx → Enum.tsx} +6 -30
  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 +42 -25
  48. package/src/name-policy.ts +0 -5
  49. package/temp/api.json +1298 -6463
  50. package/test/class-method.test.tsx +14 -24
  51. package/test/class.test.tsx +292 -0
  52. package/test/enum.test.tsx +11 -11
  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 +9 -9
  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/class-declaration.test.js +0 -481
  137. package/dist/test/project-directory.test.d.ts +0 -2
  138. package/dist/test/project-directory.test.d.ts.map +0 -1
  139. package/dist/test/vitest.setup.d.ts +0 -2
  140. package/dist/test/vitest.setup.d.ts.map +0 -1
  141. package/dist/test/vitest.setup.js +0 -1
  142. package/src/components/ClassDeclaration.tsx +0 -233
  143. package/src/components/attributes/attributes.test.tsx +0 -61
  144. package/src/components/attributes/attributes.tsx +0 -100
  145. package/src/components/doc/comment.test.tsx +0 -337
  146. package/src/components/doc/comment.tsx +0 -152
  147. package/src/components/doc/from-markdown.test.tsx +0 -103
  148. package/src/components/doc/from-markdown.tsx +0 -58
  149. package/src/components/interface/declaration.test.tsx +0 -158
  150. package/src/components/interface/declaration.tsx +0 -125
  151. package/src/components/interface/method.test.tsx +0 -293
  152. package/src/components/interface/method.tsx +0 -122
  153. package/src/components/interface/property.test.tsx +0 -165
  154. package/src/components/interface/property.tsx +0 -127
  155. package/src/components/parameters/parameters.tsx +0 -74
  156. package/src/components/property/property.test.tsx +0 -209
  157. package/src/components/property/property.tsx +0 -172
  158. package/src/components/record/declaration.test.tsx +0 -73
  159. package/src/components/record/declaration.tsx +0 -109
  160. package/src/components/type-parameters/type-parameter-constraints.test.tsx +0 -93
  161. package/src/components/type-parameters/type-parameter-constraints.tsx +0 -46
  162. package/src/components/type-parameters/type-parameter.tsx +0 -35
  163. package/src/components/type-parameters/type-parameters.test.tsx +0 -46
  164. package/src/components/type-parameters/type-parameters.tsx +0 -63
  165. package/src/components/var/declaration.test.tsx +0 -59
  166. package/src/components/var/declaration.tsx +0 -47
  167. package/test/class-declaration.test.tsx +0 -430
  168. package/test/vitest.setup.ts +0 -1
@@ -0,0 +1,149 @@
1
+ import * as core from "@alloy-js/core";
2
+ import { AccessModifier, getAccessModifier } from "../modifiers.js";
3
+ import { CSharpElements, useCSharpNamePolicy } from "../name-policy.js";
4
+ import { CSharpOutputSymbol } from "../symbols/csharp-output-symbol.js";
5
+ import { CSharpMemberScope, useCSharpScope } from "../symbols/scopes.js";
6
+ import { Name } from "./Name.js";
7
+ import { ParameterProps, Parameters } from "./Parameters.js";
8
+
9
+ // properties for creating a class
10
+ export interface ClassProps extends Omit<core.DeclarationProps, "nameKind"> {
11
+ name: string;
12
+ refkey?: core.Refkey;
13
+ accessModifier?: AccessModifier;
14
+ typeParameters?: Record<string, core.Refkey>;
15
+ }
16
+
17
+ // a C# class declaration
18
+ export function Class(props: ClassProps) {
19
+ const name = useCSharpNamePolicy().getName(props.name!, "class");
20
+
21
+ const thisClassSymbol = new CSharpOutputSymbol(name, {
22
+ refkeys: props.refkey,
23
+ });
24
+
25
+ // this creates a new scope for the class definition.
26
+ // members will automatically "inherit" this scope so
27
+ // that refkeys to them will produce the fully-qualified
28
+ // name e.g. Foo.Bar.
29
+ const thisClassScope = new CSharpMemberScope("class-decl", {
30
+ owner: thisClassSymbol,
31
+ });
32
+
33
+ let typeParams: core.Children;
34
+ if (props.typeParameters) {
35
+ const typeParamNames = new Array<string>();
36
+ for (const entry of Object.entries(props.typeParameters)) {
37
+ typeParamNames.push(
38
+ useCSharpNamePolicy().getName(entry[0], "type-parameter"),
39
+ );
40
+ // create a symbol for each type param so its
41
+ // refkey resolves to the type param's name
42
+ new CSharpOutputSymbol(entry[0], {
43
+ scope: thisClassScope,
44
+ refkeys: entry[1],
45
+ });
46
+ }
47
+ typeParams = (
48
+ <group>
49
+ {"<"}
50
+ <core.For each={typeParamNames} comma line>
51
+ {(name) => name}
52
+ </core.For>
53
+ {">"}
54
+ </group>
55
+ );
56
+ }
57
+
58
+ return (
59
+ <core.Declaration symbol={thisClassSymbol}>
60
+ {getAccessModifier(props.accessModifier)}class <Name />
61
+ {typeParams}
62
+ {!props.children && ";"}
63
+ {props.children && (
64
+ <core.Block newline>
65
+ <core.Scope value={thisClassScope}>{props.children}</core.Scope>
66
+ </core.Block>
67
+ )}
68
+ </core.Declaration>
69
+ );
70
+ }
71
+
72
+ export interface ClassConstructorProps {
73
+ accessModifier?: AccessModifier;
74
+ parameters?: Array<ParameterProps>;
75
+ refkey?: core.Refkey;
76
+ symbol?: core.OutputSymbol;
77
+ children?: core.Children;
78
+ }
79
+
80
+ // a C# class constructor
81
+ export function ClassConstructor(props: ClassConstructorProps) {
82
+ const scope = useCSharpScope();
83
+ if (scope.kind !== "member" || scope.name !== "class-decl") {
84
+ throw new Error(
85
+ "can't define a class constructor outside of a class-decl scope",
86
+ );
87
+ }
88
+
89
+ // fetch the class name from the scope
90
+ const name = useCSharpNamePolicy().getName(scope.owner!.name, "class-method");
91
+ const ctorSymbol = new CSharpOutputSymbol(name, {
92
+ scope,
93
+ refkeys: props.refkey ?? core.refkey(name),
94
+ });
95
+
96
+ // scope for ctor declaration
97
+ const ctorDeclScope = new CSharpMemberScope("constructor-decl", {
98
+ owner: ctorSymbol,
99
+ });
100
+
101
+ const accessModifier = getAccessModifier(props.accessModifier);
102
+ const params =
103
+ props.parameters ? <Parameters parameters={props.parameters} /> : "";
104
+
105
+ // note that scope wraps the ctor decl so that the params get the correct scope
106
+ return (
107
+ <core.Declaration symbol={ctorSymbol}>
108
+ <core.Scope value={ctorDeclScope}>
109
+ {accessModifier}
110
+ <Name />({params})<core.Block newline>{props.children}</core.Block>
111
+ </core.Scope>
112
+ </core.Declaration>
113
+ );
114
+ }
115
+
116
+ // properties for creating a class member
117
+ export interface ClassMemberProps {
118
+ name: string;
119
+ type: core.Children;
120
+ accessModifier?: AccessModifier;
121
+ refkey?: core.Refkey;
122
+ }
123
+
124
+ // a C# class member (i.e. a field within a class like "private int count")
125
+ export function ClassMember(props: ClassMemberProps) {
126
+ let nameElement: CSharpElements = "class-member-private";
127
+ if (props.accessModifier === "public") {
128
+ nameElement = "class-member-public";
129
+ }
130
+ const name = useCSharpNamePolicy().getName(props.name, nameElement);
131
+ const scope = useCSharpScope();
132
+ if (scope.kind !== "member" || scope.name !== "class-decl") {
133
+ throw new Error(
134
+ "can't define a class member outside of a class-decl scope",
135
+ );
136
+ }
137
+
138
+ const memberSymbol = new CSharpOutputSymbol(name, {
139
+ scope,
140
+ refkeys: props.refkey ?? core.refkey(props.name),
141
+ });
142
+
143
+ return (
144
+ <core.Declaration symbol={memberSymbol}>
145
+ {getAccessModifier(props.accessModifier)}
146
+ {props.type} <Name />
147
+ </core.Declaration>
148
+ );
149
+ }
@@ -7,44 +7,25 @@ import {
7
7
  Scope,
8
8
  } from "@alloy-js/core";
9
9
  import {
10
- AccessModifiers,
10
+ AccessModifier,
11
11
  computeModifiersPrefix,
12
12
  getAccessModifier,
13
13
  getAsyncModifier,
14
- makeModifiers,
14
+ getMethodModifier,
15
+ MethodModifier,
15
16
  } from "../modifiers.js";
16
17
  import { useCSharpNamePolicy } from "../name-policy.js";
17
18
  import { CSharpOutputSymbol } from "../symbols/csharp-output-symbol.js";
18
19
  import { CSharpMemberScope, useCSharpScope } from "../symbols/scopes.js";
19
- import { AttributeList, AttributesProp } from "./attributes/attributes.jsx";
20
- import { DocWhen } from "./doc/comment.jsx";
21
- import { ParameterProps, Parameters } from "./parameters/parameters.jsx";
22
- import { TypeParameterConstraints } from "./type-parameters/type-parameter-constraints.jsx";
23
- import { TypeParameterProps } from "./type-parameters/type-parameter.jsx";
24
- import { TypeParameters } from "./type-parameters/type-parameters.jsx";
25
-
26
- /** Method modifiers. Can only be one. */
27
- export interface ClassMethodModifiers {
28
- readonly abstract?: boolean;
29
- readonly sealed?: boolean;
30
- readonly static?: boolean;
31
- readonly virtual?: boolean;
32
- }
33
-
34
- const getMethodModifier = makeModifiers<ClassMethodModifiers>([
35
- "abstract",
36
- "sealed",
37
- "static",
38
- "virtual",
39
- ]);
20
+ import { ParameterProps, Parameters } from "./Parameters.jsx";
40
21
 
41
22
  // properties for creating a method
42
- export interface ClassMethodProps
43
- extends AccessModifiers,
44
- ClassMethodModifiers {
23
+ export interface ClassMethodProps {
45
24
  name: string;
46
25
  refkey?: Refkey;
47
26
  children?: Children;
27
+ accessModifier?: AccessModifier;
28
+ methodModifier?: MethodModifier;
48
29
  parameters?: Array<ParameterProps>;
49
30
  returns?: Children;
50
31
 
@@ -52,41 +33,6 @@ export interface ClassMethodProps
52
33
  * If true, the method will be declared as an async method.
53
34
  */
54
35
  async?: boolean;
55
-
56
- /** Doc comment */
57
- doc?: Children;
58
-
59
- /**
60
- * Type parameters for the method
61
- *
62
- * @example
63
- * ```tsx
64
- * <InterfaceMethod name="Test" typeParameters={["T"]} />
65
- * ```
66
- * This will produce:
67
- * ```csharp
68
- * public void Test<T>()
69
- * ```
70
- */
71
- typeParameters?: (TypeParameterProps | string)[];
72
-
73
- /**
74
- * Define attributes to attach
75
- * @example
76
- * ```tsx
77
- * <ClassMethod name="MyMethod" attributes={[
78
- * <Attribute name="Test" />
79
- * <Attribute name="Test2" args={["arg1", "arg2"]} />
80
- * ]} />
81
- * ```
82
- * This will produce:
83
- * ```csharp
84
- * [Test]
85
- * [Test2("arg1", "arg2")]
86
- * public void MyMethod() { }
87
- * ```
88
- */
89
- attributes?: AttributesProp;
90
36
  }
91
37
 
92
38
  // a C# class method
@@ -107,29 +53,24 @@ export function ClassMethod(props: ClassMethodProps) {
107
53
  owner: methodSymbol,
108
54
  });
109
55
 
56
+ const params =
57
+ props.parameters ? <Parameters parameters={props.parameters} /> : "";
110
58
  const returns = props.returns ?? (props.async ? "Task" : "void");
111
59
 
112
60
  const modifiers = computeModifiersPrefix([
113
- getAccessModifier(props),
114
- getMethodModifier(props),
61
+ getAccessModifier(props.accessModifier),
62
+ getMethodModifier(props.methodModifier),
115
63
  getAsyncModifier(props.async),
116
64
  ]);
117
65
  // note that scope wraps the method decl so that the params get the correct scope
118
66
  return (
119
67
  <MemberDeclaration symbol={methodSymbol}>
120
68
  <Scope value={methodScope}>
121
- <DocWhen doc={props.doc} />
122
- <AttributeList attributes={props.attributes} endline />
123
69
  {modifiers}
124
- {returns} {name}
125
- {props.typeParameters && (
126
- <TypeParameters parameters={props.typeParameters} />
127
- )}
128
- <Parameters parameters={props.parameters} />
129
- {props.typeParameters && (
130
- <TypeParameterConstraints parameters={props.typeParameters} />
131
- )}
132
- {props.abstract ? ";" : <Block newline>{props.children}</Block>}
70
+ {returns} {name}({params})
71
+ {props.methodModifier === "abstract" ?
72
+ ";"
73
+ : <Block newline>{props.children}</Block>}
133
74
  </Scope>
134
75
  </MemberDeclaration>
135
76
  );
@@ -1,42 +1,20 @@
1
1
  import * as core from "@alloy-js/core";
2
- import {
3
- AccessModifiers,
4
- computeModifiersPrefix,
5
- getAccessModifier,
6
- } from "../modifiers.js";
2
+ import { AccessModifier, getAccessModifier } from "../modifiers.js";
7
3
  import { useCSharpNamePolicy } from "../name-policy.js";
8
4
  import { CSharpOutputSymbol } from "../symbols/csharp-output-symbol.js";
9
5
  import { CSharpMemberScope, useCSharpScope } from "../symbols/scopes.js";
10
6
  import { Name } from "./Name.jsx";
11
7
 
12
8
  // properties for creating an enum
13
- export interface EnumDeclarationProps extends AccessModifiers {
9
+ export interface EnumProps {
14
10
  name: string;
15
11
  refkey?: core.Refkey;
16
12
  children?: core.Children;
13
+ accessModifier?: AccessModifier;
17
14
  }
18
15
 
19
- /**
20
- * A C# enum declaration
21
- * @example
22
- * ```tsx
23
- * <EnumDeclaration public name="Color">
24
- * <EnumMember name="Red" />
25
- * <EnumMember name="Green" />
26
- * <EnumMember name="Blue" />
27
- * </EnumDeclaration>
28
- * ```
29
- * This will produce:
30
- * ```csharp
31
- * public enum Color
32
- * {
33
- * Red,
34
- * Green,
35
- * Blue
36
- * }
37
- * ```
38
- */
39
- export function EnumDeclaration(props: EnumDeclarationProps) {
16
+ // a C# enum declaration
17
+ export function Enum(props: EnumProps) {
40
18
  const name = useCSharpNamePolicy().getName(props.name!, "enum");
41
19
  const scope = useCSharpScope();
42
20
 
@@ -54,12 +32,10 @@ export function EnumDeclaration(props: EnumDeclarationProps) {
54
32
  owner: thisEnumSymbol,
55
33
  });
56
34
 
57
- const modifiers = computeModifiersPrefix([getAccessModifier(props)]);
58
-
59
35
  if (thisEnumScope.owner)
60
36
  return (
61
37
  <core.Declaration symbol={thisEnumSymbol}>
62
- {modifiers}enum <Name />
38
+ {getAccessModifier(props.accessModifier)}enum <Name />
63
39
  {!props.children && ";"}
64
40
  {props.children && (
65
41
  <core.Scope value={thisEnumScope}>
@@ -0,0 +1,51 @@
1
+ import * as core 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
+ import { Name } from "./Name.js";
6
+
7
+ export interface ParameterProps {
8
+ name: string;
9
+ type: core.Children;
10
+ refkey?: core.Refkey;
11
+ symbol?: core.OutputSymbol;
12
+ }
13
+
14
+ // a constructor/method parameter
15
+ export function Parameter(props: ParameterProps) {
16
+ const name = useCSharpNamePolicy().getName(props.name, "parameter");
17
+ const scope = useCSharpScope();
18
+ if (
19
+ scope.kind !== "member" ||
20
+ (scope.name !== "constructor-decl" && scope.name !== "method-decl")
21
+ ) {
22
+ throw new Error(
23
+ "can't define a parameter outside of a constructor-decl or method-decl scope",
24
+ );
25
+ }
26
+
27
+ const memberSymbol = new CSharpOutputSymbol(name, {
28
+ scope,
29
+ refkeys: props.refkey ?? core.refkey(props.name),
30
+ });
31
+
32
+ return (
33
+ <core.Declaration symbol={memberSymbol}>
34
+ {props.type} <Name />
35
+ </core.Declaration>
36
+ );
37
+ }
38
+
39
+ export interface ParametersProps {
40
+ // param name and type
41
+ parameters: Array<ParameterProps>;
42
+ }
43
+
44
+ // a collection of parameters
45
+ export function Parameters(props: ParametersProps) {
46
+ return (
47
+ <core.For each={props.parameters} joiner={", "}>
48
+ {(param) => <Parameter {...param} />}
49
+ </core.For>
50
+ );
51
+ }
@@ -58,7 +58,6 @@ export function SourceFile(props: SourceFileProps) {
58
58
  filetype="cs"
59
59
  reference={Reference}
60
60
  tabWidth={4}
61
- printWidth={120}
62
61
  >
63
62
  <SourceFileContext.Provider value={sourceFileCtx}>
64
63
  <core.Scope name={props.path} kind="source-file">
@@ -1,21 +1,11 @@
1
- export * from "./attributes/attributes.jsx";
2
- export * from "./ClassDeclaration.jsx";
3
- export * from "./ClassMethod.jsx";
1
+ export * from "./Class.js";
2
+ export { ClassMethod, type ClassMethodProps } from "./ClassMethod.jsx";
4
3
  export * from "./Declaration.js";
5
- export * from "./doc/comment.jsx";
6
- export * from "./doc/from-markdown.jsx";
7
- export * from "./EnumDeclaration.jsx";
8
- export * from "./interface/declaration.js";
9
- export * from "./interface/method.js";
10
- export * from "./interface/property.js";
4
+ export * from "./Enum.js";
11
5
  export * from "./Name.js";
12
6
  export * from "./Namespace.js";
13
- export * from "./parameters/parameters.jsx";
7
+ export * from "./Parameters.js";
14
8
  export * from "./ProjectDirectory.js";
15
- export * from "./property/property.jsx";
16
- export * from "./record/declaration.js";
17
9
  export * from "./Reference.js";
18
10
  export * from "./SourceFile.js";
19
- export type { TypeParameterProps } from "./type-parameters/type-parameter.jsx";
20
11
  export * from "./UsingDirective.js";
21
- export * from "./var/declaration.jsx";
@@ -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 ClassDeclaration = core.stc(base.ClassDeclaration);
4
+ export const Class = core.stc(base.Class);
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 EnumDeclaration = core.stc(base.EnumDeclaration);
8
+ export const Enum = core.stc(base.Enum);
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);
package/src/modifiers.ts CHANGED
@@ -1,25 +1,51 @@
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";
3
11
 
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;
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] : "";
11
28
  }
12
29
 
13
- export const getAccessModifier = makeModifiers<AccessModifiers>([
14
- "public",
15
- "protected",
16
- "private",
17
- "internal",
18
- "file",
19
- ]);
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
+ }
20
46
 
21
47
  export function getAsyncModifier(async?: boolean): string {
22
- return async ? "async" : "";
48
+ return async ? "async " : "";
23
49
  }
24
50
 
25
51
  /** Resolve the modifier prefix */
@@ -27,14 +53,5 @@ export function computeModifiersPrefix(
27
53
  modifiers: Array<string | undefined>,
28
54
  ): string {
29
55
  const resolved = modifiers.filter((x) => x);
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
- };
56
+ return resolved.length > 0 ? resolved.join("") : "";
40
57
  }
@@ -5,16 +5,13 @@ import * as changecase from "change-case";
5
5
  export type CSharpElements =
6
6
  | "class"
7
7
  | "constant"
8
- | "variable"
9
8
  | "enum"
10
9
  | "enum-member"
11
10
  | "function"
12
11
  | "interface"
13
- | "record"
14
12
  | "class-member-private"
15
13
  | "class-member-public"
16
14
  | "class-method"
17
- | "class-property"
18
15
  | "parameter"
19
16
  | "type-parameter";
20
17
 
@@ -26,11 +23,9 @@ export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
26
23
  case "enum":
27
24
  case "enum-member":
28
25
  case "interface":
29
- case "record":
30
26
  case "class-member-public":
31
27
  case "class-method":
32
28
  case "type-parameter":
33
- case "class-property":
34
29
  return changecase.pascalCase(name);
35
30
  case "constant":
36
31
  return changecase.constantCase(name);