@alloy-js/csharp 0.1.0

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 (125) hide show
  1. package/LICENSE.txt +7 -0
  2. package/api-extractor.json +4 -0
  3. package/babel.config.cjs +4 -0
  4. package/dist/src/components/Class.d.ts +33 -0
  5. package/dist/src/components/Class.d.ts.map +1 -0
  6. package/dist/src/components/Class.js +170 -0
  7. package/dist/src/components/Class.js.map +1 -0
  8. package/dist/src/components/Declaration.d.ts +8 -0
  9. package/dist/src/components/Declaration.d.ts.map +1 -0
  10. package/dist/src/components/Declaration.js +18 -0
  11. package/dist/src/components/Declaration.js.map +1 -0
  12. package/dist/src/components/Enum.d.ts +13 -0
  13. package/dist/src/components/Enum.d.ts.map +1 -0
  14. package/dist/src/components/Enum.js +65 -0
  15. package/dist/src/components/Enum.js.map +1 -0
  16. package/dist/src/components/Name.d.ts +2 -0
  17. package/dist/src/components/Name.d.ts.map +1 -0
  18. package/dist/src/components/Name.js +12 -0
  19. package/dist/src/components/Name.js.map +1 -0
  20. package/dist/src/components/Namespace.d.ts +11 -0
  21. package/dist/src/components/Namespace.d.ts.map +1 -0
  22. package/dist/src/components/Namespace.js +35 -0
  23. package/dist/src/components/Namespace.js.map +1 -0
  24. package/dist/src/components/Parameters.d.ts +13 -0
  25. package/dist/src/components/Parameters.d.ts.map +1 -0
  26. package/dist/src/components/Parameters.js +34 -0
  27. package/dist/src/components/Parameters.js.map +1 -0
  28. package/dist/src/components/ProjectDirectory.d.ts +12 -0
  29. package/dist/src/components/ProjectDirectory.d.ts.map +1 -0
  30. package/dist/src/components/ProjectDirectory.js +48 -0
  31. package/dist/src/components/ProjectDirectory.js.map +1 -0
  32. package/dist/src/components/Reference.d.ts +6 -0
  33. package/dist/src/components/Reference.d.ts.map +1 -0
  34. package/dist/src/components/Reference.js +9 -0
  35. package/dist/src/components/Reference.js.map +1 -0
  36. package/dist/src/components/SourceFile.d.ts +12 -0
  37. package/dist/src/components/SourceFile.d.ts.map +1 -0
  38. package/dist/src/components/SourceFile.js +72 -0
  39. package/dist/src/components/SourceFile.js.map +1 -0
  40. package/dist/src/components/UsingDirective.d.ts +5 -0
  41. package/dist/src/components/UsingDirective.d.ts.map +1 -0
  42. package/dist/src/components/UsingDirective.js +13 -0
  43. package/dist/src/components/UsingDirective.js.map +1 -0
  44. package/dist/src/components/index.d.ts +11 -0
  45. package/dist/src/components/index.d.ts.map +1 -0
  46. package/dist/src/components/index.js +11 -0
  47. package/dist/src/components/index.js.map +1 -0
  48. package/dist/src/components/stc/index.d.ts +43 -0
  49. package/dist/src/components/stc/index.d.ts.map +1 -0
  50. package/dist/src/components/stc/index.js +13 -0
  51. package/dist/src/components/stc/index.js.map +1 -0
  52. package/dist/src/index.d.ts +5 -0
  53. package/dist/src/index.d.ts.map +1 -0
  54. package/dist/src/index.js +5 -0
  55. package/dist/src/index.js.map +1 -0
  56. package/dist/src/modifiers.d.ts +5 -0
  57. package/dist/src/modifiers.d.ts.map +1 -0
  58. package/dist/src/modifiers.js +35 -0
  59. package/dist/src/modifiers.js.map +1 -0
  60. package/dist/src/name-policy.d.ts +5 -0
  61. package/dist/src/name-policy.d.ts.map +1 -0
  62. package/dist/src/name-policy.js +30 -0
  63. package/dist/src/name-policy.js.map +1 -0
  64. package/dist/src/symbols/csharp-output-symbol.d.ts +7 -0
  65. package/dist/src/symbols/csharp-output-symbol.d.ts.map +1 -0
  66. package/dist/src/symbols/csharp-output-symbol.js +20 -0
  67. package/dist/src/symbols/csharp-output-symbol.js.map +1 -0
  68. package/dist/src/symbols/index.d.ts +4 -0
  69. package/dist/src/symbols/index.d.ts.map +1 -0
  70. package/dist/src/symbols/index.js +4 -0
  71. package/dist/src/symbols/index.js.map +1 -0
  72. package/dist/src/symbols/reference.d.ts +3 -0
  73. package/dist/src/symbols/reference.d.ts.map +1 -0
  74. package/dist/src/symbols/reference.js +55 -0
  75. package/dist/src/symbols/reference.js.map +1 -0
  76. package/dist/src/symbols/scopes.d.ts +16 -0
  77. package/dist/src/symbols/scopes.d.ts.map +1 -0
  78. package/dist/src/symbols/scopes.js +37 -0
  79. package/dist/src/symbols/scopes.js.map +1 -0
  80. package/dist/test/class.test.d.ts +2 -0
  81. package/dist/test/class.test.d.ts.map +1 -0
  82. package/dist/test/enum.test.d.ts +2 -0
  83. package/dist/test/enum.test.d.ts.map +1 -0
  84. package/dist/test/namespace.test.d.ts +2 -0
  85. package/dist/test/namespace.test.d.ts.map +1 -0
  86. package/dist/test/projectdirectory.test.d.ts +2 -0
  87. package/dist/test/projectdirectory.test.d.ts.map +1 -0
  88. package/dist/test/sourcefile.test.d.ts +2 -0
  89. package/dist/test/sourcefile.test.d.ts.map +1 -0
  90. package/dist/test/using.test.d.ts +2 -0
  91. package/dist/test/using.test.d.ts.map +1 -0
  92. package/dist/test/utils.d.ts +6 -0
  93. package/dist/test/utils.d.ts.map +1 -0
  94. package/dist/tsconfig.tsbuildinfo +1 -0
  95. package/package.json +51 -0
  96. package/src/components/Class.tsx +210 -0
  97. package/src/components/Declaration.tsx +17 -0
  98. package/src/components/Enum.tsx +74 -0
  99. package/src/components/Name.tsx +11 -0
  100. package/src/components/Namespace.tsx +39 -0
  101. package/src/components/Parameters.tsx +52 -0
  102. package/src/components/ProjectDirectory.tsx +54 -0
  103. package/src/components/Reference.tsx +13 -0
  104. package/src/components/SourceFile.tsx +69 -0
  105. package/src/components/UsingDirective.tsx +19 -0
  106. package/src/components/index.ts +10 -0
  107. package/src/components/stc/index.ts +13 -0
  108. package/src/index.ts +4 -0
  109. package/src/modifiers.ts +45 -0
  110. package/src/name-policy.ts +41 -0
  111. package/src/symbols/csharp-output-symbol.ts +26 -0
  112. package/src/symbols/index.ts +3 -0
  113. package/src/symbols/reference.ts +65 -0
  114. package/src/symbols/scopes.ts +60 -0
  115. package/temp/api.json +3474 -0
  116. package/test/class.test.tsx +249 -0
  117. package/test/enum.test.tsx +147 -0
  118. package/test/namespace.test.tsx +59 -0
  119. package/test/projectdirectory.test.tsx +106 -0
  120. package/test/sourcefile.test.tsx +46 -0
  121. package/test/using.test.tsx +97 -0
  122. package/test/utils.tsx +69 -0
  123. package/tsconfig.json +11 -0
  124. package/tsdoc-metadata.json +11 -0
  125. package/vitest.config.ts +18 -0
@@ -0,0 +1,26 @@
1
+ import * as core from "@alloy-js/core";
2
+ import { useNamespace } from "../components/Namespace.jsx";
3
+ import { CSharpOutputScope } from "./scopes.js";
4
+
5
+ // represents a symbol from a .cs file. Class, enum, interface etc.
6
+ export interface CSharpOutputSymbol extends core.OutputSymbol {
7
+ scope: CSharpOutputScope;
8
+ }
9
+
10
+ // creates a new C# symbol
11
+ export function createCSharpSymbol(props: core.DeclarationProps) {
12
+ const scope = core.useScope() as CSharpOutputScope;
13
+
14
+ const namespaceCtx = useNamespace();
15
+ if (!namespaceCtx) {
16
+ throw new Error("symbol must be declared inside a namespace");
17
+ }
18
+
19
+ const sym = scope.binder.createSymbol<CSharpOutputSymbol>({
20
+ name: props.name!,
21
+ scope,
22
+ refkey: props.refkey ?? core.refkey(props.name),
23
+ });
24
+
25
+ return sym;
26
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./csharp-output-symbol.js";
2
+ export * from "./reference.js";
3
+ export * from "./scopes.js";
@@ -0,0 +1,65 @@
1
+ import * as core from "@alloy-js/core";
2
+ import { useNamespace } from "../components/Namespace.jsx";
3
+ import { useSourceFile } from "../components/SourceFile.jsx";
4
+ import { CSharpOutputSymbol } from "./csharp-output-symbol.js";
5
+ import { CSharpOutputScope } from "./scopes.js";
6
+
7
+ // when resolving references across source files, the last element
8
+ // in pathDown will be the containing source file. we only need this
9
+ // here so we can find and remove this scope as it's not useful
10
+ // when building the reference
11
+ interface SourceFileScope extends core.OutputScope {
12
+ kind: "source-file";
13
+ }
14
+
15
+ type ReferenceScope = CSharpOutputScope | SourceFileScope;
16
+
17
+ // converts a refkey to its fully qualified name
18
+ // e.g. if refkey is for bar in enum type foo, and
19
+ // foo is in the same namespace as the refkey, then
20
+ // the result would be foo.bar.
21
+ export function ref(refkey: core.Refkey): () => string {
22
+ const targetNamespaceCtx = useNamespace();
23
+ const resolveResult = core.resolve<ReferenceScope, CSharpOutputSymbol>(
24
+ refkey as core.Refkey,
25
+ );
26
+
27
+ return core.memo(() => {
28
+ if (resolveResult.value === undefined) {
29
+ return "<Unresolved Symbol>";
30
+ }
31
+
32
+ // targetDeclaration is the symbol with the refkey
33
+ // pathDown is an array of scopes to that symbol and can be empty.
34
+ // the entries are top-down, so namespace, source-file, member
35
+ // - referencing a type within the same namespace will have an empty path
36
+ // - referencing a member (e.g. enum value) within the same namespace will
37
+ // have a single path entry that contains the owning type
38
+ // - referencing a type within the same namespace but in a different source
39
+ // file will have at least a source-file entry
40
+ // - referencing a symbol outside the current namespace will have at least
41
+ // two entries, namespace, source-file
42
+ const { targetDeclaration, pathDown } = resolveResult.value;
43
+
44
+ const sourceNamespace = pathDown.find((v) => {
45
+ return v.kind === "namespace";
46
+ });
47
+ if (sourceNamespace && sourceNamespace.name !== targetNamespaceCtx!.name) {
48
+ // the source symbol is in a different namespace that the target refkey.
49
+ // add the applicable using statement to the target's source file.
50
+ const targetSrc = useSourceFile();
51
+ targetSrc!.addUsing(sourceNamespace.name);
52
+ }
53
+
54
+ // we only need to build the fully-qualified name for members
55
+ // TODO: possibly a subset of members
56
+ const syms = (pathDown as ReferenceScope[])
57
+ .filter((v) => {
58
+ return v.kind === "member";
59
+ })
60
+ .map((s) => s.owner);
61
+ syms.push(targetDeclaration);
62
+
63
+ return syms.map((sym) => sym.name).join(".");
64
+ });
65
+ }
@@ -0,0 +1,60 @@
1
+ import * as core from "@alloy-js/core";
2
+ import { CSharpOutputSymbol } from "./csharp-output-symbol.js";
3
+
4
+ // indicates that the scope for a symbols is at the namespace level
5
+ export interface CSharpNamespaceScope extends core.OutputScope {
6
+ kind: "namespace";
7
+ }
8
+
9
+ // creates a new namespace scope
10
+ export function createCSharpNamespaceScope(
11
+ binder: core.Binder,
12
+ parent: core.OutputScope | undefined,
13
+ name: string,
14
+ ): CSharpNamespaceScope {
15
+ return binder.createScope<CSharpNamespaceScope>({
16
+ kind: "namespace",
17
+ name,
18
+ parent,
19
+ });
20
+ }
21
+
22
+ // the kind of member scope. i.e. are we in an enum, class, etc
23
+ export type CSharpMemberScopeName =
24
+ | "class-decl"
25
+ | "constructor-decl"
26
+ | "enum-decl"
27
+ | "method-decl";
28
+
29
+ // indicates that the scope for a symbol resides within a type
30
+ // e.g. for an enum value, class field etc, these would have
31
+ // member scope where the owner is the containing type.
32
+ export interface CSharpMemberScope extends core.OutputScope {
33
+ kind: "member";
34
+ name: CSharpMemberScopeName;
35
+ owner: CSharpOutputSymbol;
36
+ }
37
+
38
+ // creates a new member scope.
39
+ // parent is the owning symbol.
40
+ export function createCSharpMemberScope(
41
+ binder: core.Binder,
42
+ parent: core.OutputScope,
43
+ owner: CSharpOutputSymbol,
44
+ name: CSharpMemberScopeName,
45
+ ): CSharpMemberScope {
46
+ return binder.createScope<CSharpMemberScope>({
47
+ kind: "member",
48
+ name: name,
49
+ owner,
50
+ parent,
51
+ });
52
+ }
53
+
54
+ // contains the possible scopes where a declaration can reside
55
+ export type CSharpOutputScope = CSharpMemberScope | CSharpNamespaceScope;
56
+
57
+ // returns the current C# scope
58
+ export function useCSharpScope(): CSharpOutputScope {
59
+ return core.useScope() as CSharpOutputScope;
60
+ }