@alloy-js/csharp 0.21.0-dev.1 → 0.21.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 (131) hide show
  1. package/dist/src/components/enum/declaration.d.ts +2 -2
  2. package/dist/src/components/enum/declaration.d.ts.map +1 -1
  3. package/dist/src/components/enum/declaration.js +3 -3
  4. package/dist/src/components/enum/declaration.js.map +1 -1
  5. package/dist/src/components/enum/declaration.ref.test.js +1 -1
  6. package/dist/src/components/enum/declaration.test.js +15 -1
  7. package/dist/src/components/enum/declaration.test.js.map +1 -1
  8. package/dist/src/components/enum/member.d.ts +2 -2
  9. package/dist/src/components/enum/member.d.ts.map +1 -1
  10. package/dist/src/components/enum/member.js +3 -3
  11. package/dist/src/components/enum/member.js.map +1 -1
  12. package/dist/src/components/enum/member.test.js +24 -1
  13. package/dist/src/components/enum/member.test.js.map +1 -1
  14. package/dist/src/components/field/field.d.ts +2 -2
  15. package/dist/src/components/field/field.d.ts.map +1 -1
  16. package/dist/src/components/field/field.js.map +1 -1
  17. package/dist/src/components/field/field.test.js +19 -1
  18. package/dist/src/components/field/field.test.js.map +1 -1
  19. package/dist/src/components/index.d.ts +1 -1
  20. package/dist/src/components/index.d.ts.map +1 -1
  21. package/dist/src/components/index.js +1 -2
  22. package/dist/src/components/index.js.map +1 -1
  23. package/dist/src/components/interface/declaration.d.ts +1 -1
  24. package/dist/src/components/interface/declaration.d.ts.map +1 -1
  25. package/dist/src/components/interface/declaration.js +3 -3
  26. package/dist/src/components/interface/declaration.js.map +1 -1
  27. package/dist/src/components/interface/declaration.test.js +14 -1
  28. package/dist/src/components/interface/declaration.test.js.map +1 -1
  29. package/dist/src/components/interface/method.d.ts +2 -2
  30. package/dist/src/components/interface/method.d.ts.map +1 -1
  31. package/dist/src/components/interface/method.js.map +1 -1
  32. package/dist/src/components/interface/method.test.js +17 -1
  33. package/dist/src/components/interface/method.test.js.map +1 -1
  34. package/dist/src/components/interface/property.d.ts +2 -2
  35. package/dist/src/components/interface/property.d.ts.map +1 -1
  36. package/dist/src/components/interface/property.js.map +1 -1
  37. package/dist/src/components/interface/property.test.js +19 -0
  38. package/dist/src/components/interface/property.test.js.map +1 -1
  39. package/dist/src/components/namespace/namespace-name.d.ts +10 -0
  40. package/dist/src/components/namespace/namespace-name.d.ts.map +1 -0
  41. package/dist/src/components/namespace/namespace-name.js +20 -0
  42. package/dist/src/components/namespace/namespace-name.js.map +1 -0
  43. package/dist/src/components/{namespace.d.ts → namespace/namespace.d.ts} +3 -1
  44. package/dist/src/components/namespace/namespace.d.ts.map +1 -0
  45. package/dist/src/components/{namespace.js → namespace/namespace.js} +12 -13
  46. package/dist/src/components/namespace/namespace.js.map +1 -0
  47. package/dist/src/components/namespace/namespace.test.d.ts.map +1 -0
  48. package/dist/src/components/namespace/namespace.test.js +207 -0
  49. package/dist/src/components/namespace/namespace.test.js.map +1 -0
  50. package/dist/src/components/namespace.ref.test.js +42 -10
  51. package/dist/src/components/namespace.ref.test.js.map +1 -1
  52. package/dist/src/components/record/declaration.d.ts +1 -1
  53. package/dist/src/components/record/declaration.d.ts.map +1 -1
  54. package/dist/src/components/record/declaration.js +3 -4
  55. package/dist/src/components/record/declaration.js.map +1 -1
  56. package/dist/src/components/record/declaration.test.js +14 -1
  57. package/dist/src/components/record/declaration.test.js.map +1 -1
  58. package/dist/src/components/source-file/source-file.d.ts.map +1 -1
  59. package/dist/src/components/source-file/source-file.js +4 -2
  60. package/dist/src/components/source-file/source-file.js.map +1 -1
  61. package/dist/src/components/source-file/source-file.test.js +1 -1
  62. package/dist/src/components/source-file/using.test.js +1 -1
  63. package/dist/src/components/struct/declaration.d.ts +1 -1
  64. package/dist/src/components/struct/declaration.d.ts.map +1 -1
  65. package/dist/src/components/struct/declaration.js +3 -3
  66. package/dist/src/components/struct/declaration.js.map +1 -1
  67. package/dist/src/components/struct/declaration.test.js +14 -1
  68. package/dist/src/components/struct/declaration.test.js.map +1 -1
  69. package/dist/src/components/type-parameters/type-parameter.d.ts +2 -2
  70. package/dist/src/components/type-parameters/type-parameter.d.ts.map +1 -1
  71. package/dist/src/components/type-parameters/type-parameter.js.map +1 -1
  72. package/dist/src/components/type-parameters/type-parameters.test.js +14 -0
  73. package/dist/src/components/type-parameters/type-parameters.test.js.map +1 -1
  74. package/dist/src/components/var/declaration.d.ts +2 -2
  75. package/dist/src/components/var/declaration.d.ts.map +1 -1
  76. package/dist/src/components/var/declaration.js.map +1 -1
  77. package/dist/src/components/var/declaration.test.js +22 -1
  78. package/dist/src/components/var/declaration.test.js.map +1 -1
  79. package/dist/src/name-policy.d.ts +1 -1
  80. package/dist/src/name-policy.d.ts.map +1 -1
  81. package/dist/src/name-policy.js +1 -0
  82. package/dist/src/name-policy.js.map +1 -1
  83. package/dist/src/symbols/factories.d.ts +1 -1
  84. package/dist/src/symbols/factories.d.ts.map +1 -1
  85. package/dist/src/symbols/factories.js +27 -7
  86. package/dist/src/symbols/factories.js.map +1 -1
  87. package/dist/src/symbols/namespace.d.ts +2 -2
  88. package/dist/src/symbols/namespace.d.ts.map +1 -1
  89. package/dist/src/symbols/namespace.js.map +1 -1
  90. package/dist/tsconfig.tsbuildinfo +1 -1
  91. package/package.json +2 -2
  92. package/src/components/enum/declaration.ref.test.tsx +1 -1
  93. package/src/components/enum/declaration.test.tsx +14 -1
  94. package/src/components/enum/declaration.tsx +4 -3
  95. package/src/components/enum/member.test.tsx +22 -1
  96. package/src/components/enum/member.tsx +4 -4
  97. package/src/components/field/field.test.tsx +14 -1
  98. package/src/components/field/field.tsx +2 -2
  99. package/src/components/index.ts +1 -1
  100. package/src/components/interface/declaration.test.tsx +11 -1
  101. package/src/components/interface/declaration.tsx +3 -4
  102. package/src/components/interface/method.test.tsx +14 -1
  103. package/src/components/interface/method.tsx +2 -1
  104. package/src/components/interface/property.test.tsx +13 -0
  105. package/src/components/interface/property.tsx +2 -1
  106. package/src/components/namespace/namespace-name.tsx +31 -0
  107. package/src/components/namespace/namespace.test.tsx +145 -0
  108. package/src/components/{namespace.tsx → namespace/namespace.tsx} +12 -15
  109. package/src/components/namespace.ref.test.tsx +34 -9
  110. package/src/components/record/declaration.test.tsx +11 -1
  111. package/src/components/record/declaration.tsx +3 -4
  112. package/src/components/source-file/source-file.test.tsx +1 -1
  113. package/src/components/source-file/source-file.tsx +2 -2
  114. package/src/components/source-file/using.test.tsx +1 -1
  115. package/src/components/struct/declaration.test.tsx +11 -1
  116. package/src/components/struct/declaration.tsx +3 -4
  117. package/src/components/type-parameters/type-parameter.tsx +2 -1
  118. package/src/components/type-parameters/type-parameters.test.tsx +9 -0
  119. package/src/components/var/declaration.test.tsx +16 -1
  120. package/src/components/var/declaration.tsx +2 -1
  121. package/src/name-policy.ts +3 -1
  122. package/src/symbols/factories.ts +47 -10
  123. package/src/symbols/namespace.ts +2 -2
  124. package/temp/api.json +184 -35
  125. package/dist/src/components/namespace.d.ts.map +0 -1
  126. package/dist/src/components/namespace.js.map +0 -1
  127. package/dist/src/components/namespace.test.d.ts.map +0 -1
  128. package/dist/src/components/namespace.test.js +0 -79
  129. package/dist/src/components/namespace.test.js.map +0 -1
  130. package/src/components/namespace.test.tsx +0 -52
  131. /package/dist/src/components/{namespace.test.d.ts → namespace/namespace.test.d.ts} +0 -0
@@ -26,7 +26,7 @@ export interface RecordDeclarationProps
26
26
  extends Omit<core.DeclarationProps, "nameKind">,
27
27
  AccessModifiers,
28
28
  RecordModifiers {
29
- name: string;
29
+ name: string | core.Namekey;
30
30
 
31
31
  /** Doc comment */
32
32
  doc?: core.Children;
@@ -73,12 +73,11 @@ export interface RecordDeclarationProps
73
73
  * ```
74
74
  */
75
75
  export function RecordDeclaration(props: RecordDeclarationProps) {
76
- const name = useCSharpNamePolicy().getName(props.name!, "record");
77
-
78
76
  // records don't have their own type kind but instead use class or struct
79
77
  // depending on what kind of record we have.
80
- const thisRecordSymbol = createNamedTypeSymbol(name, "record", {
78
+ const thisRecordSymbol = createNamedTypeSymbol(props.name, "record", {
81
79
  refkeys: props.refkey,
80
+ namePolicy: useCSharpNamePolicy().for("record"),
82
81
  });
83
82
 
84
83
  const thisRecordScope = createClassScope(thisRecordSymbol);
@@ -1,5 +1,5 @@
1
1
  import { ClassDeclaration } from "#components/class/declaration.jsx";
2
- import { Namespace } from "#components/namespace.jsx";
2
+ import { Namespace } from "#components/namespace/namespace.jsx";
3
3
  import { Children, FormatOptions, Indent, Output, Prose } from "@alloy-js/core";
4
4
  import { describe, expect, it } from "vitest";
5
5
  import { CSharpFormatOptions } from "../../contexts/format-options.js";
@@ -1,4 +1,5 @@
1
1
  import { NamespaceScopes } from "#components/namespace-scopes.jsx";
2
+ import { NamespaceName } from "#components/namespace/namespace-name.jsx";
2
3
  import { Reference } from "#components/Reference.jsx";
3
4
  import { Usings } from "#components/using/using.jsx";
4
5
  import {
@@ -42,7 +43,6 @@ export function SourceFile(props: SourceFileProps) {
42
43
  const nsContext = useNamespaceContext();
43
44
  const globalNs = getGlobalNamespace(useBinder());
44
45
  const nsSymbol = nsContext ? nsContext.symbol : globalNs;
45
- const nsRef = nsContext ? nsContext.symbol.name : undefined;
46
46
  const usings = computed(() => {
47
47
  return (
48
48
  Array.from(sourceFileScope.usings) as (NamespaceSymbol | string)[]
@@ -78,7 +78,7 @@ export function SourceFile(props: SourceFileProps) {
78
78
  {nsSymbol === globalNs ?
79
79
  content
80
80
  : <>
81
- namespace {nsRef}
81
+ namespace <NamespaceName symbol={nsSymbol} />
82
82
  {sourceFileScope.hasBlockNamespace ?
83
83
  <>
84
84
  {" "}
@@ -2,7 +2,7 @@ import { ClassDeclaration } from "#components/class/declaration.jsx";
2
2
  import { EnumDeclaration } from "#components/enum/declaration.jsx";
3
3
  import { EnumMember } from "#components/enum/member.jsx";
4
4
  import { Method } from "#components/method/method.jsx";
5
- import { Namespace } from "#components/namespace.jsx";
5
+ import { Namespace } from "#components/namespace/namespace.jsx";
6
6
  import { SourceFile } from "#components/source-file/source-file.jsx";
7
7
  import { Output, refkey } from "@alloy-js/core";
8
8
  import * as coretest from "@alloy-js/core/testing";
@@ -1,4 +1,4 @@
1
- import { List, refkey } from "@alloy-js/core";
1
+ import { List, namekey, refkey } from "@alloy-js/core";
2
2
  import { describe, expect, it } from "vitest";
3
3
  import { TestNamespace } from "../../../test/utils.jsx";
4
4
  import { Attribute } from "../attributes/attributes.jsx";
@@ -19,6 +19,16 @@ it("declares struct with no members", () => {
19
19
  `);
20
20
  });
21
21
 
22
+ it("takes a namekey", () => {
23
+ expect(
24
+ <TestNamespace>
25
+ <StructDeclaration name={namekey("my-struct")} />
26
+ </TestNamespace>,
27
+ ).toRenderTo(`
28
+ struct MyStruct;
29
+ `);
30
+ });
31
+
22
32
  describe("modifiers", () => {
23
33
  it.each(["public", "private", "internal"])("%s", (mod) => {
24
34
  expect(
@@ -35,7 +35,7 @@ export interface StructDeclarationProps
35
35
  extends Omit<core.DeclarationProps, "nameKind">,
36
36
  AccessModifiers,
37
37
  StructModifiers {
38
- name: string;
38
+ name: string | core.Namekey;
39
39
 
40
40
  /** Doc comment */
41
41
  doc?: core.Children;
@@ -98,10 +98,9 @@ export interface StructDeclarationProps
98
98
  * ```
99
99
  */
100
100
  export function StructDeclaration(props: StructDeclarationProps) {
101
- const name = useCSharpNamePolicy().getName(props.name!, "struct");
102
-
103
- const thisStructSymbol = createNamedTypeSymbol(name, "struct", {
101
+ const thisStructSymbol = createNamedTypeSymbol(props.name, "struct", {
104
102
  refkeys: props.refkey,
103
+ namePolicy: useCSharpNamePolicy().for("struct"),
105
104
  });
106
105
 
107
106
  const thisStructScope = createNamedTypeScope(thisStructSymbol);
@@ -2,6 +2,7 @@ import {
2
2
  Children,
3
3
  MemberDeclaration,
4
4
  MemberName,
5
+ Namekey,
5
6
  Refkey,
6
7
  } from "@alloy-js/core";
7
8
  import { createTypeParameterSymbol } from "../../symbols/factories.js";
@@ -13,7 +14,7 @@ export interface TypeParameterProps {
13
14
  /**
14
15
  * The name of the type parameter.
15
16
  */
16
- readonly name: string;
17
+ readonly name: string | Namekey;
17
18
 
18
19
  /**
19
20
  * The parameter constraint
@@ -1,3 +1,4 @@
1
+ import { namekey } from "@alloy-js/core";
1
2
  import { expect, it } from "vitest";
2
3
  import { TestNamespace } from "../../../test/utils.jsx";
3
4
  import { TypeParameters } from "./type-parameters.jsx";
@@ -44,3 +45,11 @@ it("declare type parameters using parameters", () => {
44
45
  </TestNamespace>,
45
46
  ).toRenderTo(`<A, B>`);
46
47
  });
48
+
49
+ it("takes a namekey", () => {
50
+ expect(
51
+ <TestNamespace>
52
+ <TypeParameters parameters={[{ name: namekey("my-param") }]} />
53
+ </TestNamespace>,
54
+ ).toRenderTo(`<MyParam>`);
55
+ });
@@ -1,4 +1,4 @@
1
- import { List, refkey } from "@alloy-js/core";
1
+ import { List, namekey, refkey } from "@alloy-js/core";
2
2
  import { expect, it } from "vitest";
3
3
  import { TestNamespace } from "../../../test/utils.jsx";
4
4
  import { VarDeclaration } from "./declaration.jsx";
@@ -51,3 +51,18 @@ it("links refkey", () => {
51
51
  var testVar2 = testVar;
52
52
  `);
53
53
  });
54
+
55
+ it("links namekey", () => {
56
+ const key = namekey("test-var");
57
+ expect(
58
+ <TestNamespace>
59
+ <List>
60
+ <VarDeclaration name={key}>42</VarDeclaration>
61
+ <VarDeclaration name="testVar2">{key}</VarDeclaration>
62
+ </List>
63
+ </TestNamespace>,
64
+ ).toRenderTo(`
65
+ var testVar = 42;
66
+ var testVar2 = testVar;
67
+ `);
68
+ });
@@ -3,6 +3,7 @@ import {
3
3
  Declaration,
4
4
  DeclarationProps,
5
5
  Name,
6
+ Namekey,
6
7
  Refkey,
7
8
  } from "@alloy-js/core";
8
9
  import { createVariableSymbol } from "../../symbols/factories.js";
@@ -11,7 +12,7 @@ import { createVariableSymbol } from "../../symbols/factories.js";
11
12
  export interface VarDeclarationProps
12
13
  extends Omit<DeclarationProps, "nameKind"> {
13
14
  /** Variable name */
14
- name: string;
15
+ name: string | Namekey;
15
16
  /** Type of the variable declaration. If not specified, defaults to "var" */
16
17
  type?: Children;
17
18
  /** Variable refkey */
@@ -17,7 +17,8 @@ export type CSharpElements =
17
17
  | "class-method"
18
18
  | "class-property"
19
19
  | "parameter"
20
- | "type-parameter";
20
+ | "type-parameter"
21
+ | "namespace";
21
22
 
22
23
  // creates the C# naming policy
23
24
  export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
@@ -33,6 +34,7 @@ export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
33
34
  case "class-method":
34
35
  case "type-parameter":
35
36
  case "class-property":
37
+ case "namespace":
36
38
  return changecase.pascalCase(name);
37
39
  case "constant":
38
40
  return changecase.constantCase(name);
@@ -6,6 +6,7 @@ import {
6
6
  useBinder,
7
7
  } from "@alloy-js/core";
8
8
  import { getGlobalNamespace } from "../contexts/global-namespace.js";
9
+ import { useNamespaceContext } from "../contexts/namespace.js";
9
10
  import { CSharpElements, useCSharpNamePolicy } from "../name-policy.js";
10
11
  import { CSharpClassScope } from "../scopes/class.js";
11
12
  import { useCSharpScope, useNamedTypeScope } from "../scopes/contexts.js";
@@ -13,10 +14,7 @@ import { CSharpScope } from "../scopes/csharp.js";
13
14
  import { CSharpLexicalScope } from "../scopes/lexical.js";
14
15
  import { CSharpMethodScope } from "../scopes/method.js";
15
16
  import { CSharpNamedTypeScope } from "../scopes/named-type.js";
16
- import {
17
- CSharpNamespaceScope,
18
- useEnclosingNamespaceScope,
19
- } from "../scopes/namespace.js";
17
+ import { CSharpNamespaceScope } from "../scopes/namespace.js";
20
18
  import { CSharpSourceFileScope } from "../scopes/source-file.js";
21
19
  import { CSharpSymbol, CSharpSymbolOptions } from "./csharp.js";
22
20
  import { MethodKinds, MethodSymbol } from "./method.js";
@@ -118,13 +116,52 @@ export function createNamedTypeSymbol(
118
116
  );
119
117
  }
120
118
 
121
- export function createNamespaceSymbol(name: string) {
122
- const scope = useEnclosingNamespaceScope();
123
- const nsSymbol = scope?.ownerSymbol ?? getGlobalNamespace(useBinder());
124
- if (nsSymbol.members.symbolNames.has(name)) {
125
- return nsSymbol.members.symbolNames.get(name)! as NamespaceSymbol;
119
+ export function createNamespaceSymbol(
120
+ name: string | Namekey | (string | Namekey)[],
121
+ options: CSharpSymbolOptions = {},
122
+ ): NamespaceSymbol {
123
+ const scope = useNamespaceContext();
124
+ const parentSymbol = scope?.symbol ?? getGlobalNamespace(useBinder());
125
+ const names = normalizeNamespaceName(name);
126
+ let current = parentSymbol;
127
+ for (const name of names) {
128
+ current = createNamespaceSymbolInternal(name, current, options);
129
+ }
130
+ return current;
131
+ }
132
+
133
+ function normalizeNamespaceName(
134
+ name: string | Namekey | (string | Namekey)[],
135
+ ): (string | Namekey)[] {
136
+ if (Array.isArray(name)) {
137
+ return name;
138
+ }
139
+ if (typeof name === "string" && name.includes(".")) {
140
+ return name.split(".");
126
141
  }
127
- return withCleanup(new NamespaceSymbol(name, nsSymbol));
142
+ return [name];
143
+ }
144
+
145
+ function createNamespaceSymbolInternal(
146
+ name: string | Namekey,
147
+ parentSymbol: NamespaceSymbol,
148
+ options: CSharpSymbolOptions = {},
149
+ ): NamespaceSymbol {
150
+ const namePolicy =
151
+ options.namePolicy ?? useCSharpNamePolicy().for("namespace");
152
+ const expectedName = namePolicy(typeof name === "string" ? name : name.name);
153
+ if (parentSymbol.members.symbolNames.has(expectedName)) {
154
+ return parentSymbol.members.symbolNames.get(
155
+ expectedName,
156
+ )! as NamespaceSymbol;
157
+ }
158
+ return withCleanup(
159
+ new NamespaceSymbol(
160
+ name,
161
+ parentSymbol,
162
+ withNamePolicy(options, "namespace"),
163
+ ),
164
+ );
128
165
  }
129
166
 
130
167
  export interface CreateMethodSymbolOptions extends CSharpSymbolOptions {
@@ -1,4 +1,4 @@
1
- import { OutputSymbolOptions } from "@alloy-js/core";
1
+ import { Namekey, OutputSymbolOptions } from "@alloy-js/core";
2
2
  import { NamedTypeSymbol } from "./named-type.js";
3
3
 
4
4
  export interface NamespaceSymbolOptions extends OutputSymbolOptions {
@@ -11,7 +11,7 @@ export interface NamespaceSymbolOptions extends OutputSymbolOptions {
11
11
  export class NamespaceSymbol extends NamedTypeSymbol {
12
12
  public readonly symbolKind = "namespace";
13
13
  constructor(
14
- name: string,
14
+ name: string | Namekey,
15
15
  parentNamespace?: NamespaceSymbol,
16
16
  options?: NamespaceSymbolOptions,
17
17
  ) {