@alloy-js/csharp 0.18.0-dev.3 → 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.
@@ -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,16 +1,19 @@
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
19
  // a C# enum declaration
@@ -32,10 +35,12 @@ export function Enum(props: EnumProps) {
32
35
  owner: thisEnumSymbol,
33
36
  });
34
37
 
38
+ const modifiers = computeModifiersPrefix([getAccessModifier(props)]);
39
+
35
40
  if (thisEnumScope.owner)
36
41
  return (
37
42
  <core.Declaration symbol={thisEnumSymbol}>
38
- {getAccessModifier(props.accessModifier)}enum <Name />
43
+ {modifiers}enum <Name />
39
44
  {!props.children && ";"}
40
45
  {props.children && (
41
46
  <core.Scope value={thisEnumScope}>
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
  }