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

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 (45) hide show
  1. package/dist/src/components/ClassDeclaration.d.ts +47 -0
  2. package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
  3. package/dist/src/components/{Class.js → ClassDeclaration.js} +33 -8
  4. package/dist/src/components/ClassMethod.d.ts +2 -4
  5. package/dist/src/components/ClassMethod.d.ts.map +1 -1
  6. package/dist/src/components/ClassMethod.js +2 -2
  7. package/dist/src/components/EnumDeclaration.d.ts +34 -0
  8. package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
  9. package/dist/src/components/{Enum.js → EnumDeclaration.js} +25 -5
  10. package/dist/src/components/index.d.ts +2 -2
  11. package/dist/src/components/index.d.ts.map +1 -1
  12. package/dist/src/components/index.js +2 -2
  13. package/dist/src/components/stc/index.d.ts +2 -2
  14. package/dist/src/components/stc/index.d.ts.map +1 -1
  15. package/dist/src/components/stc/index.js +2 -2
  16. package/dist/src/modifiers.d.ts +17 -4
  17. package/dist/src/modifiers.d.ts.map +1 -1
  18. package/dist/src/modifiers.js +9 -29
  19. package/dist/test/class-method.test.js +16 -14
  20. package/dist/test/class.test.js +35 -35
  21. package/dist/test/enum.test.js +12 -12
  22. package/dist/test/namespace.test.js +8 -8
  23. package/dist/test/projectdirectory.test.js +8 -8
  24. package/dist/test/sourcefile.test.js +4 -4
  25. package/dist/test/using.test.js +9 -9
  26. package/dist/tsconfig.tsbuildinfo +1 -1
  27. package/package.json +1 -1
  28. package/src/components/{Class.tsx → ClassDeclaration.tsx} +45 -16
  29. package/src/components/ClassMethod.tsx +6 -10
  30. package/src/components/{Enum.tsx → EnumDeclaration.tsx} +30 -6
  31. package/src/components/index.ts +2 -2
  32. package/src/components/stc/index.ts +2 -2
  33. package/src/modifiers.ts +36 -39
  34. package/temp/api.json +407 -266
  35. package/test/class-method.test.tsx +8 -14
  36. package/test/class.test.tsx +35 -56
  37. package/test/enum.test.tsx +11 -11
  38. package/test/namespace.test.tsx +4 -4
  39. package/test/projectdirectory.test.tsx +4 -4
  40. package/test/sourcefile.test.tsx +2 -2
  41. package/test/using.test.tsx +9 -9
  42. package/dist/src/components/Class.d.ts +0 -26
  43. package/dist/src/components/Class.d.ts.map +0 -1
  44. package/dist/src/components/Enum.d.ts +0 -15
  45. package/dist/src/components/Enum.d.ts.map +0 -1
@@ -7,12 +7,12 @@ import {
7
7
  Scope,
8
8
  } from "@alloy-js/core";
9
9
  import {
10
- AccessModifier,
10
+ AccessModifiers,
11
11
  computeModifiersPrefix,
12
12
  getAccessModifier,
13
13
  getAsyncModifier,
14
14
  getMethodModifier,
15
- MethodModifier,
15
+ MethodModifiers,
16
16
  } from "../modifiers.js";
17
17
  import { useCSharpNamePolicy } from "../name-policy.js";
18
18
  import { CSharpOutputSymbol } from "../symbols/csharp-output-symbol.js";
@@ -20,12 +20,10 @@ import { CSharpMemberScope, useCSharpScope } from "../symbols/scopes.js";
20
20
  import { ParameterProps, Parameters } from "./Parameters.jsx";
21
21
 
22
22
  // properties for creating a method
23
- export interface ClassMethodProps {
23
+ export interface ClassMethodProps extends AccessModifiers, MethodModifiers {
24
24
  name: string;
25
25
  refkey?: Refkey;
26
26
  children?: Children;
27
- accessModifier?: AccessModifier;
28
- methodModifier?: MethodModifier;
29
27
  parameters?: Array<ParameterProps>;
30
28
  returns?: Children;
31
29
 
@@ -58,8 +56,8 @@ export function ClassMethod(props: ClassMethodProps) {
58
56
  const returns = props.returns ?? (props.async ? "Task" : "void");
59
57
 
60
58
  const modifiers = computeModifiersPrefix([
61
- getAccessModifier(props.accessModifier),
62
- getMethodModifier(props.methodModifier),
59
+ getAccessModifier(props),
60
+ getMethodModifier(props),
63
61
  getAsyncModifier(props.async),
64
62
  ]);
65
63
  // note that scope wraps the method decl so that the params get the correct scope
@@ -68,9 +66,7 @@ export function ClassMethod(props: ClassMethodProps) {
68
66
  <Scope value={methodScope}>
69
67
  {modifiers}
70
68
  {returns} {name}({params})
71
- {props.methodModifier === "abstract" ?
72
- ";"
73
- : <Block newline>{props.children}</Block>}
69
+ {props.abstract ? ";" : <Block newline>{props.children}</Block>}
74
70
  </Scope>
75
71
  </MemberDeclaration>
76
72
  );
@@ -1,20 +1,42 @@
1
1
  import * as core from "@alloy-js/core";
2
- import { AccessModifier, getAccessModifier } from "../modifiers.js";
2
+ import {
3
+ AccessModifiers,
4
+ computeModifiersPrefix,
5
+ getAccessModifier,
6
+ } from "../modifiers.js";
3
7
  import { useCSharpNamePolicy } from "../name-policy.js";
4
8
  import { CSharpOutputSymbol } from "../symbols/csharp-output-symbol.js";
5
9
  import { CSharpMemberScope, useCSharpScope } from "../symbols/scopes.js";
6
10
  import { Name } from "./Name.jsx";
7
11
 
8
12
  // properties for creating an enum
9
- export interface EnumProps {
13
+ export interface EnumProps extends AccessModifiers {
10
14
  name: string;
11
15
  refkey?: core.Refkey;
12
16
  children?: core.Children;
13
- accessModifier?: AccessModifier;
14
17
  }
15
18
 
16
- // a C# enum declaration
17
- export function Enum(props: EnumProps) {
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: EnumProps) {
18
40
  const name = useCSharpNamePolicy().getName(props.name!, "enum");
19
41
  const scope = useCSharpScope();
20
42
 
@@ -32,10 +54,12 @@ export function Enum(props: EnumProps) {
32
54
  owner: thisEnumSymbol,
33
55
  });
34
56
 
57
+ const modifiers = computeModifiersPrefix([getAccessModifier(props)]);
58
+
35
59
  if (thisEnumScope.owner)
36
60
  return (
37
61
  <core.Declaration symbol={thisEnumSymbol}>
38
- {getAccessModifier(props.accessModifier)}enum <Name />
62
+ {modifiers}enum <Name />
39
63
  {!props.children && ";"}
40
64
  {props.children && (
41
65
  <core.Scope value={thisEnumScope}>
@@ -1,7 +1,7 @@
1
- export * from "./Class.js";
1
+ export * from "./ClassDeclaration.jsx";
2
2
  export { ClassMethod, type ClassMethodProps } from "./ClassMethod.jsx";
3
3
  export * from "./Declaration.js";
4
- export * from "./Enum.js";
4
+ export * from "./EnumDeclaration.jsx";
5
5
  export * from "./Name.js";
6
6
  export * from "./Namespace.js";
7
7
  export * from "./Parameters.js";
@@ -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);
package/src/modifiers.ts CHANGED
@@ -1,51 +1,48 @@
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";
13
+ export function getAccessModifier(data: AccessModifiers): string {
14
+ return [
15
+ data.public && "public",
16
+ data.protected && "protected",
17
+ data.private && "private",
18
+ data.internal && "internal",
19
+ data.file && "file",
20
+ ]
21
+ .filter((x) => x)
22
+ .join(" ");
23
+ }
31
24
 
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
- };
25
+ /** Method modifiers. Can only be one. */
26
+ export interface MethodModifiers {
27
+ readonly abstract?: boolean;
28
+ readonly sealed?: boolean;
29
+ readonly static?: boolean;
30
+ readonly virtual?: boolean;
31
+ }
40
32
 
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] : "";
33
+ export function getMethodModifier(data: MethodModifiers): string {
34
+ return [
35
+ data.abstract && "abstract",
36
+ data.sealed && "sealed",
37
+ data.static && "static",
38
+ data.virtual && "virtual",
39
+ ]
40
+ .filter((x) => x)
41
+ .join(" ");
45
42
  }
46
43
 
47
44
  export function getAsyncModifier(async?: boolean): string {
48
- return async ? "async " : "";
45
+ return async ? "async" : "";
49
46
  }
50
47
 
51
48
  /** Resolve the modifier prefix */
@@ -53,5 +50,5 @@ export function computeModifiersPrefix(
53
50
  modifiers: Array<string | undefined>,
54
51
  ): string {
55
52
  const resolved = modifiers.filter((x) => x);
56
- return resolved.length > 0 ? resolved.join("") : "";
53
+ return resolved.length > 0 ? resolved.join(" ") + " " : "";
57
54
  }