@alloy-js/go 0.0.1

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 (264) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE +7 -0
  3. package/api-extractor.json +4 -0
  4. package/dist/src/builtins/fmt/fmt.d.ts +147 -0
  5. package/dist/src/builtins/fmt/fmt.d.ts.map +1 -0
  6. package/dist/src/builtins/fmt/fmt.js +148 -0
  7. package/dist/src/builtins/fmt/fmt.js.map +1 -0
  8. package/dist/src/builtins/index.d.ts +2004 -0
  9. package/dist/src/builtins/index.d.ts.map +1 -0
  10. package/dist/src/builtins/index.js +9 -0
  11. package/dist/src/builtins/index.js.map +1 -0
  12. package/dist/src/builtins/io/io.d.ts +648 -0
  13. package/dist/src/builtins/io/io.d.ts.map +1 -0
  14. package/dist/src/builtins/io/io.js +650 -0
  15. package/dist/src/builtins/io/io.js.map +1 -0
  16. package/dist/src/builtins/net/net.d.ts +1210 -0
  17. package/dist/src/builtins/net/net.d.ts.map +1 -0
  18. package/dist/src/builtins/net/net.js +1212 -0
  19. package/dist/src/builtins/net/net.js.map +1 -0
  20. package/dist/src/components/ImportStatement.d.ts +15 -0
  21. package/dist/src/components/ImportStatement.d.ts.map +1 -0
  22. package/dist/src/components/ImportStatement.js +90 -0
  23. package/dist/src/components/ImportStatement.js.map +1 -0
  24. package/dist/src/components/ModuleDirectory.d.ts +8 -0
  25. package/dist/src/components/ModuleDirectory.d.ts.map +1 -0
  26. package/dist/src/components/ModuleDirectory.js +20 -0
  27. package/dist/src/components/ModuleDirectory.js.map +1 -0
  28. package/dist/src/components/Name.d.ts +2 -0
  29. package/dist/src/components/Name.d.ts.map +1 -0
  30. package/dist/src/components/Name.js +12 -0
  31. package/dist/src/components/Name.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 +15 -0
  35. package/dist/src/components/Reference.js.map +1 -0
  36. package/dist/src/components/SourceDirectory.d.ts +6 -0
  37. package/dist/src/components/SourceDirectory.d.ts.map +1 -0
  38. package/dist/src/components/SourceDirectory.js +30 -0
  39. package/dist/src/components/SourceDirectory.js.map +1 -0
  40. package/dist/src/components/SourceFile.d.ts +16 -0
  41. package/dist/src/components/SourceFile.d.ts.map +1 -0
  42. package/dist/src/components/SourceFile.js +75 -0
  43. package/dist/src/components/SourceFile.js.map +1 -0
  44. package/dist/src/components/doc/comment.d.ts +21 -0
  45. package/dist/src/components/doc/comment.d.ts.map +1 -0
  46. package/dist/src/components/doc/comment.js +30 -0
  47. package/dist/src/components/doc/comment.js.map +1 -0
  48. package/dist/src/components/doc/comment.test.d.ts +2 -0
  49. package/dist/src/components/doc/comment.test.d.ts.map +1 -0
  50. package/dist/src/components/doc/comment.test.js +215 -0
  51. package/dist/src/components/doc/comment.test.js.map +1 -0
  52. package/dist/src/components/function/function.d.ts +31 -0
  53. package/dist/src/components/function/function.d.ts.map +1 -0
  54. package/dist/src/components/function/function.js +134 -0
  55. package/dist/src/components/function/function.js.map +1 -0
  56. package/dist/src/components/function/function.test.d.ts +2 -0
  57. package/dist/src/components/function/function.test.d.ts.map +1 -0
  58. package/dist/src/components/function/function.test.js +410 -0
  59. package/dist/src/components/function/function.test.js.map +1 -0
  60. package/dist/src/components/index.d.ts +16 -0
  61. package/dist/src/components/index.d.ts.map +1 -0
  62. package/dist/src/components/index.js +16 -0
  63. package/dist/src/components/index.js.map +1 -0
  64. package/dist/src/components/interface/declaration.d.ts +68 -0
  65. package/dist/src/components/interface/declaration.d.ts.map +1 -0
  66. package/dist/src/components/interface/declaration.js +181 -0
  67. package/dist/src/components/interface/declaration.js.map +1 -0
  68. package/dist/src/components/interface/interface.test.d.ts +2 -0
  69. package/dist/src/components/interface/interface.test.d.ts.map +1 -0
  70. package/dist/src/components/interface/interface.test.js +517 -0
  71. package/dist/src/components/interface/interface.test.js.map +1 -0
  72. package/dist/src/components/package.test.d.ts +2 -0
  73. package/dist/src/components/package.test.d.ts.map +1 -0
  74. package/dist/src/components/package.test.js +89 -0
  75. package/dist/src/components/package.test.js.map +1 -0
  76. package/dist/src/components/parameters/parameters.d.ts +15 -0
  77. package/dist/src/components/parameters/parameters.d.ts.map +1 -0
  78. package/dist/src/components/parameters/parameters.js +58 -0
  79. package/dist/src/components/parameters/parameters.js.map +1 -0
  80. package/dist/src/components/parameters/typeparameters.d.ts +25 -0
  81. package/dist/src/components/parameters/typeparameters.d.ts.map +1 -0
  82. package/dist/src/components/parameters/typeparameters.js +53 -0
  83. package/dist/src/components/parameters/typeparameters.js.map +1 -0
  84. package/dist/src/components/pointer/pointer.d.ts +9 -0
  85. package/dist/src/components/pointer/pointer.d.ts.map +1 -0
  86. package/dist/src/components/pointer/pointer.js +8 -0
  87. package/dist/src/components/pointer/pointer.js.map +1 -0
  88. package/dist/src/components/stc/index.d.ts +27 -0
  89. package/dist/src/components/stc/index.d.ts.map +1 -0
  90. package/dist/src/components/stc/index.js +27 -0
  91. package/dist/src/components/stc/index.js.map +1 -0
  92. package/dist/src/components/struct/declaration.d.ts +62 -0
  93. package/dist/src/components/struct/declaration.d.ts.map +1 -0
  94. package/dist/src/components/struct/declaration.js +161 -0
  95. package/dist/src/components/struct/declaration.js.map +1 -0
  96. package/dist/src/components/struct/struct.test.d.ts +2 -0
  97. package/dist/src/components/struct/struct.test.d.ts.map +1 -0
  98. package/dist/src/components/struct/struct.test.js +741 -0
  99. package/dist/src/components/struct/struct.test.js.map +1 -0
  100. package/dist/src/components/type/declaration.d.ts +29 -0
  101. package/dist/src/components/type/declaration.d.ts.map +1 -0
  102. package/dist/src/components/type/declaration.js +63 -0
  103. package/dist/src/components/type/declaration.js.map +1 -0
  104. package/dist/src/components/var/declaration.d.ts +27 -0
  105. package/dist/src/components/var/declaration.d.ts.map +1 -0
  106. package/dist/src/components/var/declaration.js +84 -0
  107. package/dist/src/components/var/declaration.js.map +1 -0
  108. package/dist/src/components/var/var-declaration.test.d.ts +2 -0
  109. package/dist/src/components/var/var-declaration.test.d.ts.map +1 -0
  110. package/dist/src/components/var/var-declaration.test.js +340 -0
  111. package/dist/src/components/var/var-declaration.test.js.map +1 -0
  112. package/dist/src/context/package.d.ts +9 -0
  113. package/dist/src/context/package.d.ts.map +1 -0
  114. package/dist/src/context/package.js +6 -0
  115. package/dist/src/context/package.js.map +1 -0
  116. package/dist/src/create-module.d.ts +47 -0
  117. package/dist/src/create-module.d.ts.map +1 -0
  118. package/dist/src/create-module.js +158 -0
  119. package/dist/src/create-module.js.map +1 -0
  120. package/dist/src/create-module.test.d.ts +2 -0
  121. package/dist/src/create-module.test.d.ts.map +1 -0
  122. package/dist/src/create-module.test.js +147 -0
  123. package/dist/src/create-module.test.js.map +1 -0
  124. package/dist/src/index.d.ts +6 -0
  125. package/dist/src/index.d.ts.map +1 -0
  126. package/dist/src/index.js +6 -0
  127. package/dist/src/index.js.map +1 -0
  128. package/dist/src/name-policy.d.ts +5 -0
  129. package/dist/src/name-policy.d.ts.map +1 -0
  130. package/dist/src/name-policy.js +27 -0
  131. package/dist/src/name-policy.js.map +1 -0
  132. package/dist/src/scopes/contexts.d.ts +9 -0
  133. package/dist/src/scopes/contexts.d.ts.map +1 -0
  134. package/dist/src/scopes/contexts.js +34 -0
  135. package/dist/src/scopes/contexts.js.map +1 -0
  136. package/dist/src/scopes/factories.d.ts +7 -0
  137. package/dist/src/scopes/factories.d.ts.map +1 -0
  138. package/dist/src/scopes/factories.js +16 -0
  139. package/dist/src/scopes/factories.js.map +1 -0
  140. package/dist/src/scopes/function.d.ts +9 -0
  141. package/dist/src/scopes/function.d.ts.map +1 -0
  142. package/dist/src/scopes/function.js +14 -0
  143. package/dist/src/scopes/function.js.map +1 -0
  144. package/dist/src/scopes/go.d.ts +8 -0
  145. package/dist/src/scopes/go.d.ts.map +1 -0
  146. package/dist/src/scopes/go.js +12 -0
  147. package/dist/src/scopes/go.js.map +1 -0
  148. package/dist/src/scopes/index.d.ts +10 -0
  149. package/dist/src/scopes/index.d.ts.map +1 -0
  150. package/dist/src/scopes/index.js +10 -0
  151. package/dist/src/scopes/index.js.map +1 -0
  152. package/dist/src/scopes/lexical.d.ts +8 -0
  153. package/dist/src/scopes/lexical.d.ts.map +1 -0
  154. package/dist/src/scopes/lexical.js +11 -0
  155. package/dist/src/scopes/lexical.js.map +1 -0
  156. package/dist/src/scopes/module.d.ts +9 -0
  157. package/dist/src/scopes/module.d.ts.map +1 -0
  158. package/dist/src/scopes/module.js +31 -0
  159. package/dist/src/scopes/module.js.map +1 -0
  160. package/dist/src/scopes/named-type.d.ts +23 -0
  161. package/dist/src/scopes/named-type.d.ts.map +1 -0
  162. package/dist/src/scopes/named-type.js +33 -0
  163. package/dist/src/scopes/named-type.js.map +1 -0
  164. package/dist/src/scopes/package.d.ts +11 -0
  165. package/dist/src/scopes/package.d.ts.map +1 -0
  166. package/dist/src/scopes/package.js +39 -0
  167. package/dist/src/scopes/package.js.map +1 -0
  168. package/dist/src/scopes/source-file.d.ts +17 -0
  169. package/dist/src/scopes/source-file.d.ts.map +1 -0
  170. package/dist/src/scopes/source-file.js +42 -0
  171. package/dist/src/scopes/source-file.js.map +1 -0
  172. package/dist/src/symbols/factories.d.ts +25 -0
  173. package/dist/src/symbols/factories.d.ts.map +1 -0
  174. package/dist/src/symbols/factories.js +114 -0
  175. package/dist/src/symbols/factories.js.map +1 -0
  176. package/dist/src/symbols/function.d.ts +10 -0
  177. package/dist/src/symbols/function.d.ts.map +1 -0
  178. package/dist/src/symbols/function.js +12 -0
  179. package/dist/src/symbols/function.js.map +1 -0
  180. package/dist/src/symbols/go.d.ts +22 -0
  181. package/dist/src/symbols/go.d.ts.map +1 -0
  182. package/dist/src/symbols/go.js +56 -0
  183. package/dist/src/symbols/go.js.map +1 -0
  184. package/dist/src/symbols/index.d.ts +7 -0
  185. package/dist/src/symbols/index.d.ts.map +1 -0
  186. package/dist/src/symbols/index.js +7 -0
  187. package/dist/src/symbols/index.js.map +1 -0
  188. package/dist/src/symbols/named-type.d.ts +24 -0
  189. package/dist/src/symbols/named-type.d.ts.map +1 -0
  190. package/dist/src/symbols/named-type.js +53 -0
  191. package/dist/src/symbols/named-type.js.map +1 -0
  192. package/dist/src/symbols/package.d.ts +18 -0
  193. package/dist/src/symbols/package.d.ts.map +1 -0
  194. package/dist/src/symbols/package.js +38 -0
  195. package/dist/src/symbols/package.js.map +1 -0
  196. package/dist/src/symbols/reference.d.ts +3 -0
  197. package/dist/src/symbols/reference.d.ts.map +1 -0
  198. package/dist/src/symbols/reference.js +76 -0
  199. package/dist/src/symbols/reference.js.map +1 -0
  200. package/dist/test/utils.d.ts +8 -0
  201. package/dist/test/utils.d.ts.map +1 -0
  202. package/dist/test/utils.js +68 -0
  203. package/dist/test/utils.js.map +1 -0
  204. package/dist/test/vitest.setup.d.ts +2 -0
  205. package/dist/test/vitest.setup.d.ts.map +1 -0
  206. package/dist/test/vitest.setup.js +2 -0
  207. package/dist/test/vitest.setup.js.map +1 -0
  208. package/dist/tsconfig.tsbuildinfo +1 -0
  209. package/package.json +45 -0
  210. package/src/builtins/fmt/fmt.ts +78 -0
  211. package/src/builtins/index.ts +9 -0
  212. package/src/builtins/io/io.ts +355 -0
  213. package/src/builtins/net/net.ts +502 -0
  214. package/src/components/ImportStatement.tsx +115 -0
  215. package/src/components/ModuleDirectory.tsx +18 -0
  216. package/src/components/Name.tsx +11 -0
  217. package/src/components/Reference.tsx +18 -0
  218. package/src/components/SourceDirectory.tsx +32 -0
  219. package/src/components/SourceFile.tsx +74 -0
  220. package/src/components/doc/comment.test.tsx +278 -0
  221. package/src/components/doc/comment.tsx +40 -0
  222. package/src/components/function/function.test.tsx +387 -0
  223. package/src/components/function/function.tsx +194 -0
  224. package/src/components/index.ts +15 -0
  225. package/src/components/interface/declaration.tsx +250 -0
  226. package/src/components/interface/interface.test.tsx +390 -0
  227. package/src/components/package.test.tsx +63 -0
  228. package/src/components/parameters/parameters.tsx +74 -0
  229. package/src/components/parameters/typeparameters.tsx +78 -0
  230. package/src/components/pointer/pointer.tsx +12 -0
  231. package/src/components/stc/index.ts +27 -0
  232. package/src/components/struct/declaration.tsx +228 -0
  233. package/src/components/struct/struct.test.tsx +558 -0
  234. package/src/components/type/declaration.tsx +93 -0
  235. package/src/components/var/declaration.tsx +138 -0
  236. package/src/components/var/var-declaration.test.tsx +236 -0
  237. package/src/context/package.ts +13 -0
  238. package/src/create-module.test.tsx +128 -0
  239. package/src/create-module.ts +347 -0
  240. package/src/index.ts +5 -0
  241. package/src/name-policy.ts +65 -0
  242. package/src/scopes/contexts.ts +45 -0
  243. package/src/scopes/factories.ts +29 -0
  244. package/src/scopes/function.ts +22 -0
  245. package/src/scopes/go.ts +18 -0
  246. package/src/scopes/index.ts +9 -0
  247. package/src/scopes/lexical.ts +14 -0
  248. package/src/scopes/module.ts +35 -0
  249. package/src/scopes/named-type.ts +49 -0
  250. package/src/scopes/package.ts +55 -0
  251. package/src/scopes/source-file.ts +66 -0
  252. package/src/symbols/factories.ts +234 -0
  253. package/src/symbols/function.ts +17 -0
  254. package/src/symbols/go.ts +82 -0
  255. package/src/symbols/index.ts +6 -0
  256. package/src/symbols/named-type.ts +90 -0
  257. package/src/symbols/package.ts +54 -0
  258. package/src/symbols/reference.ts +87 -0
  259. package/temp/api.json +8753 -0
  260. package/test/utils.tsx +77 -0
  261. package/test/vitest.setup.ts +1 -0
  262. package/tsconfig.json +15 -0
  263. package/tsdoc-metadata.json +11 -0
  264. package/vitest.config.ts +13 -0
@@ -0,0 +1,66 @@
1
+ import {
2
+ OutputScope,
3
+ OutputScopeOptions,
4
+ shallowReactive,
5
+ useScope,
6
+ } from "@alloy-js/core";
7
+ import { GoSymbol } from "../symbols/go.js";
8
+ import { PackageSymbol } from "../symbols/package.js";
9
+ import { GoLexicalScope } from "./lexical.js";
10
+ import { GoPackageScope } from "./package.js";
11
+
12
+ export type ImportRecords = Map<PackageSymbol, GoSymbol>;
13
+
14
+ export class GoSourceFileScope extends GoLexicalScope {
15
+ #imports = shallowReactive<ImportRecords>(new Map());
16
+
17
+ constructor(
18
+ name: string,
19
+ parent?: GoPackageScope,
20
+ options?: OutputScopeOptions,
21
+ ) {
22
+ super(name, parent, options);
23
+ }
24
+
25
+ get imports() {
26
+ return this.#imports;
27
+ }
28
+
29
+ get parent() {
30
+ return super.parent! as GoPackageScope;
31
+ }
32
+
33
+ set parent(v: GoPackageScope) {
34
+ super.parent = v;
35
+ }
36
+
37
+ addImport(import_: PackageSymbol) {
38
+ if (this.#imports.has(import_)) {
39
+ return this.#imports.get(import_)!;
40
+ }
41
+
42
+ const localSymbol = new GoSymbol(import_.name, this.values, {
43
+ aliasTarget: import_,
44
+ });
45
+
46
+ this.#imports.set(import_, localSymbol);
47
+
48
+ return localSymbol;
49
+ }
50
+
51
+ get enclosingPackage(): PackageSymbol | undefined {
52
+ return this.parent?.ownerSymbol;
53
+ }
54
+ }
55
+
56
+ export function useSourceFileScope() {
57
+ let scope: OutputScope | undefined = useScope();
58
+ while (scope) {
59
+ if (scope instanceof GoSourceFileScope) {
60
+ return scope;
61
+ }
62
+ scope = scope.parent;
63
+ }
64
+
65
+ return undefined;
66
+ }
@@ -0,0 +1,234 @@
1
+ import { Namekey, NamePolicyGetter } from "@alloy-js/core";
2
+ import { join } from "pathe";
3
+ import { GoElements, useGoNamePolicy } from "../name-policy.js";
4
+ import { useGoScope, useNamedTypeScope } from "../scopes/contexts.js";
5
+ import { GoFunctionScope } from "../scopes/function.js";
6
+ import { GoLexicalScope } from "../scopes/lexical.js";
7
+ import { useModule } from "../scopes/module.js";
8
+ import { GoNamedTypeScope } from "../scopes/named-type.js";
9
+ import { useEnclosingPackageScope } from "../scopes/package.js";
10
+ import { FunctionSymbol } from "./function.js";
11
+ import { GoSymbol, GoSymbolOptions } from "./go.js";
12
+ import {
13
+ NamedTypeSymbol,
14
+ NamedTypeSymbolOptions,
15
+ NamedTypeTypeKind,
16
+ } from "./named-type.js";
17
+ import { PackageSymbol } from "./package.js";
18
+
19
+ /**
20
+ * Create a symbol for a parameter in the current func scope.
21
+ */
22
+ export function createParameterSymbol(
23
+ originalName: string | Namekey,
24
+ options: GoSymbolOptions = {},
25
+ ) {
26
+ const scope = useGoScope();
27
+ if (!(scope instanceof GoFunctionScope)) {
28
+ throw new Error(`Can't create parameter symbol outside of a func scope.`);
29
+ }
30
+ return new GoSymbol(
31
+ originalName,
32
+ scope.parameters,
33
+ withNamePolicy(options, "parameter"),
34
+ );
35
+ }
36
+
37
+ export interface CreateTypeParameterSymbolOptions extends GoSymbolOptions {
38
+ scope?: GoFunctionScope | GoNamedTypeScope;
39
+ }
40
+ export function createTypeParameterSymbol(
41
+ originalName: string | Namekey,
42
+ options: CreateTypeParameterSymbolOptions = {},
43
+ ) {
44
+ const scope = options.scope ?? useGoScope();
45
+ if (
46
+ !(scope instanceof GoFunctionScope) &&
47
+ !(scope instanceof GoNamedTypeScope)
48
+ ) {
49
+ throw new Error(
50
+ "Can't create a type parameter symbol outside of a func or named type scope.",
51
+ );
52
+ }
53
+
54
+ return new GoSymbol(
55
+ originalName,
56
+ scope.typeParameters,
57
+ withNamePolicy(options, "type-parameter"),
58
+ );
59
+ }
60
+
61
+ export function createStructMemberSymbol(
62
+ originalName: string | Namekey,
63
+ options: NamedTypeSymbolOptions = {},
64
+ ) {
65
+ const scope = useNamedTypeScope();
66
+
67
+ if (
68
+ scope.ownerSymbol.typeKind !== "struct" &&
69
+ scope.ownerSymbol.typeKind !== "struct-member"
70
+ ) {
71
+ throw new Error(
72
+ `Can't define a field outside of a struct, kind ${scope.ownerSymbol.typeKind}.`,
73
+ );
74
+ }
75
+
76
+ return new NamedTypeSymbol(
77
+ originalName,
78
+ scope.members,
79
+ "struct-member",
80
+ withNamePolicy(options, "struct-member"),
81
+ );
82
+ }
83
+
84
+ export function createInterfaceMemberSymbol(
85
+ originalName: string | Namekey,
86
+ options: NamedTypeSymbolOptions = {},
87
+ ) {
88
+ const scope = useNamedTypeScope();
89
+
90
+ if (scope.ownerSymbol.typeKind !== "interface") {
91
+ throw new Error(
92
+ `Can't define a field outside of an interface, kind ${scope.ownerSymbol.typeKind}.`,
93
+ );
94
+ }
95
+
96
+ return new NamedTypeSymbol(
97
+ originalName,
98
+ scope.members,
99
+ "interface-member",
100
+ withNamePolicy(options, "interface-member"),
101
+ );
102
+ }
103
+
104
+ export function createPackageSymbol(name: string, path?: string) {
105
+ const scope = useEnclosingPackageScope();
106
+ const pkgSymbol = scope?.ownerSymbol;
107
+ if (!pkgSymbol) {
108
+ const mod = useModule();
109
+ const modName = mod.name;
110
+ const builtin = mod.builtin;
111
+ const pkgPath = path ? join(modName, path) : modName;
112
+ return new PackageSymbol(name, undefined, { path: pkgPath, builtin });
113
+ }
114
+ if (pkgSymbol.members.symbolNames.has(name)) {
115
+ return pkgSymbol.members.symbolNames.get(name)! as PackageSymbol;
116
+ }
117
+ return new PackageSymbol(name, pkgSymbol, { path });
118
+ }
119
+
120
+ export function createFunctionSymbol(
121
+ originalName: string | Namekey,
122
+ isMethod: boolean,
123
+ options: GoSymbolOptions = {},
124
+ ) {
125
+ const scope = useGoScope();
126
+ if (!isMethod && scope instanceof GoLexicalScope) {
127
+ return new FunctionSymbol(
128
+ originalName,
129
+ scope.types,
130
+ withNamePolicy(options, "function"),
131
+ );
132
+ }
133
+ return new FunctionSymbol(
134
+ originalName,
135
+ undefined,
136
+ withNamePolicy(options, "function"),
137
+ );
138
+ }
139
+
140
+ export function createPropertySymbol(
141
+ originalName: string | Namekey,
142
+ options: GoSymbolOptions,
143
+ ) {
144
+ const scope = useNamedTypeScope();
145
+ return new GoSymbol(
146
+ originalName,
147
+ scope.members,
148
+ withNamePolicy(options, "struct-member"),
149
+ );
150
+ }
151
+
152
+ export function createVariableSymbol(
153
+ originalName: string | Namekey,
154
+ options: GoSymbolOptions = {},
155
+ ) {
156
+ const scope = useGoScope();
157
+ if (!(scope instanceof GoLexicalScope)) {
158
+ throw new Error(
159
+ `Can't create variable symbol outside of a lexical scope, got a ${scope.constructor.name}.`,
160
+ );
161
+ }
162
+ return new GoSymbol(
163
+ originalName,
164
+ scope.values,
165
+ withNamePolicy(options, "variable"),
166
+ );
167
+ }
168
+
169
+ export function createTypeSymbol(
170
+ originalName: string | Namekey,
171
+ kind: NamedTypeTypeKind,
172
+ options: NamedTypeSymbolOptions = {},
173
+ ) {
174
+ const scope = useGoScope();
175
+ if (scope instanceof GoLexicalScope) {
176
+ return new NamedTypeSymbol(
177
+ originalName,
178
+ scope.types,
179
+ kind,
180
+ withNamePolicy(options, "type"),
181
+ );
182
+ }
183
+ throw new Error(
184
+ `Can't create type symbol outside of a lexical scope, got a ${scope.constructor.name}.`,
185
+ );
186
+ }
187
+
188
+ export function createNestedStructSymbol(
189
+ originalName: string | Namekey,
190
+ kind: NamedTypeTypeKind,
191
+ options: NamedTypeSymbolOptions = {},
192
+ ) {
193
+ const scope = useGoScope();
194
+ if (scope instanceof GoNamedTypeScope) {
195
+ return new NamedTypeSymbol(
196
+ originalName,
197
+ scope.ownerSymbol.members,
198
+ kind,
199
+ withNamePolicy(options, "struct-member"),
200
+ );
201
+ }
202
+ throw new Error(
203
+ `Can't create named anonymous type symbol outside of a NamedType scope, got a ${scope.constructor.name}.`,
204
+ );
205
+ }
206
+
207
+ let anonymousTypeID = 0;
208
+
209
+ export function createAnonymousTypeSymbol(
210
+ kind: NamedTypeTypeKind,
211
+ options: NamedTypeSymbolOptions = {},
212
+ ) {
213
+ const name =
214
+ "anonymous_" + anonymousTypeID++ + "_this_should_not_appear_in_output";
215
+ const scope = useGoScope();
216
+ if (scope instanceof GoLexicalScope) {
217
+ return new NamedTypeSymbol(name, scope.types, kind, options);
218
+ } else if (scope instanceof GoNamedTypeScope) {
219
+ return new NamedTypeSymbol(name, scope.ownerSymbol.members, kind, options);
220
+ }
221
+ throw new Error(
222
+ `Can't create anonymous type symbol outside of a lexical or NamedType scope, got a ${scope.constructor.name}.`,
223
+ );
224
+ }
225
+
226
+ function withNamePolicy<T extends { namePolicy?: NamePolicyGetter }>(
227
+ options: T,
228
+ elementType: GoElements,
229
+ ): GoSymbolOptions {
230
+ return {
231
+ ...options,
232
+ namePolicy: options.namePolicy ?? useGoNamePolicy().for(elementType),
233
+ };
234
+ }
@@ -0,0 +1,17 @@
1
+ import { Namekey, OutputSpace } from "@alloy-js/core";
2
+ import { GoSymbol, GoSymbolOptions } from "./go.js";
3
+
4
+ /**
5
+ * A symbol for a function in Go, including receivers.
6
+ */
7
+ export class FunctionSymbol extends GoSymbol {
8
+ public readonly symbolKind = "function";
9
+
10
+ constructor(
11
+ name: string | Namekey,
12
+ spaces: OutputSpace | undefined,
13
+ options: GoSymbolOptions = {},
14
+ ) {
15
+ super(name, spaces, options);
16
+ }
17
+ }
@@ -0,0 +1,82 @@
1
+ import {
2
+ Namekey,
3
+ OutputDeclarationSpace,
4
+ OutputMemberSpace,
5
+ OutputSpace,
6
+ OutputSymbol,
7
+ OutputSymbolOptions,
8
+ } from "@alloy-js/core";
9
+ import { GoScope } from "../scopes/go.js";
10
+ import { PackageSymbol } from "./package.js";
11
+
12
+ /**
13
+ * Options for creating a Go symbol.
14
+ */
15
+ export interface GoSymbolOptions extends OutputSymbolOptions {}
16
+
17
+ export type GoSymbolKinds =
18
+ | "symbol"
19
+ | "named-type"
20
+ | "function"
21
+ | "field"
22
+ | "package";
23
+
24
+ /**
25
+ * This is the base type for all symbols in Go.
26
+ *
27
+ * Many subtypes of this symbol exist for specific purposes. However, this symbol
28
+ * may be used in cases where a more specific symbol is not required.
29
+ */
30
+ export class GoSymbol extends OutputSymbol {
31
+ constructor(
32
+ name: string | Namekey,
33
+ spaces: OutputSpace[] | OutputSpace | undefined,
34
+ options: GoSymbolOptions = {},
35
+ ) {
36
+ super(name, spaces, options);
37
+ }
38
+
39
+ get enclosingPackage(): PackageSymbol | undefined {
40
+ if (this.spaces.length === 0) {
41
+ return undefined;
42
+ }
43
+
44
+ // todo: probably need to validate that a symbol can't belong to spaces in
45
+ // multiple packages.
46
+ const firstSpace = this.spaces[0];
47
+
48
+ if (firstSpace instanceof OutputMemberSpace) {
49
+ // this symbol is a member of something, so get the enclosing package from
50
+ // the symbol.
51
+
52
+ if (firstSpace.symbol.constructor.name === "PackageSymbol") {
53
+ // this is a package symbol, so return the package symbol itself.
54
+ // can't use instanceof here due to circular reference issues.
55
+ return firstSpace.symbol as PackageSymbol;
56
+ }
57
+
58
+ return (firstSpace.symbol as GoSymbol).enclosingPackage;
59
+ } else if (firstSpace instanceof OutputDeclarationSpace) {
60
+ // this symbol is in a lexical scope, so get the package symbol from the scope.
61
+ return (firstSpace.scope as GoScope).enclosingPackage;
62
+ }
63
+ throw new Error("No place to get package symbol from");
64
+ }
65
+
66
+ protected getGoCopyOptions(): GoSymbolOptions {
67
+ return {
68
+ ...this.getCopyOptions(),
69
+ };
70
+ }
71
+
72
+ protected initializeGoCopy(copy: GoSymbol) {
73
+ this.initializeCopy(copy);
74
+ }
75
+
76
+ copy(): OutputSymbol {
77
+ const options = this.getGoCopyOptions();
78
+ const copy = new GoSymbol(this.name, undefined, options);
79
+ this.initializeGoCopy(copy);
80
+ return copy;
81
+ }
82
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./factories.js";
2
+ export * from "./function.js";
3
+ export * from "./go.js";
4
+ export * from "./named-type.js";
5
+ export * from "./package.js";
6
+ export * from "./reference.js";
@@ -0,0 +1,90 @@
1
+ import {
2
+ Namekey,
3
+ OutputSpace,
4
+ track,
5
+ TrackOpTypes,
6
+ trigger,
7
+ TriggerOpTypes,
8
+ } from "@alloy-js/core";
9
+ import { TypeParameterProps } from "../components/parameters/typeparameters.jsx";
10
+ import { GoSymbol, GoSymbolOptions } from "./go.js";
11
+
12
+ // represents a symbol from a .go file. Struct, interface, etc.
13
+ export type NamedTypeTypeKind =
14
+ | "import"
15
+ | "function"
16
+ | "var"
17
+ | "type"
18
+ | "struct"
19
+ | "struct-member"
20
+ | "interface"
21
+ | "interface-member"
22
+ | "package";
23
+ export type NamedTypeSymbolKind = "named-type" | "package";
24
+
25
+ export interface NamedTypeSymbolOptions extends GoSymbolOptions {
26
+ typeParameters?: TypeParameterProps[];
27
+ }
28
+
29
+ /**
30
+ * A symbol for a named type in Go such as a struct, interface, and so forth.
31
+ */
32
+ export class NamedTypeSymbol extends GoSymbol {
33
+ public readonly symbolKind: NamedTypeSymbolKind = "named-type";
34
+ public static readonly memberSpaces = ["members"];
35
+
36
+ constructor(
37
+ name: string | Namekey,
38
+ spaces: OutputSpace[] | OutputSpace | undefined,
39
+ kind: NamedTypeTypeKind,
40
+ options?: NamedTypeSymbolOptions,
41
+ ) {
42
+ super(name, spaces, options);
43
+ this.#typeKind = kind;
44
+ this.#typeParameters = options?.typeParameters;
45
+ }
46
+
47
+ #typeKind: NamedTypeTypeKind;
48
+ get typeKind() {
49
+ track(this, TrackOpTypes.GET, "typeKind");
50
+ return this.#typeKind;
51
+ }
52
+ set typeKind(value: NamedTypeTypeKind) {
53
+ const old = this.#typeKind;
54
+ if (old === value) {
55
+ return;
56
+ }
57
+ this.#typeKind = value;
58
+ trigger(this, TriggerOpTypes.SET, "typeKind", value, old);
59
+ }
60
+
61
+ #typeParameters?: TypeParameterProps[];
62
+ get typeParameters() {
63
+ track(this, TrackOpTypes.GET, "typeParameters");
64
+ return this.#typeParameters;
65
+ }
66
+ set typeParameters(value: TypeParameterProps[] | undefined) {
67
+ const old = this.#typeParameters;
68
+ if (old === value) {
69
+ return;
70
+ }
71
+ this.#typeParameters = value;
72
+ trigger(this, TriggerOpTypes.SET, "typeParameters", value, old);
73
+ }
74
+
75
+ copy() {
76
+ const options = this.getGoCopyOptions();
77
+ const copy = new NamedTypeSymbol(
78
+ this.name,
79
+ undefined,
80
+ this.#typeKind,
81
+ options,
82
+ );
83
+ this.initializeGoCopy(copy);
84
+ return copy;
85
+ }
86
+
87
+ get members() {
88
+ return this.memberSpaceFor("members")!;
89
+ }
90
+ }
@@ -0,0 +1,54 @@
1
+ import { OutputSymbolOptions } from "@alloy-js/core";
2
+ import { join } from "pathe";
3
+ import { NamedTypeSymbol } from "./named-type.js";
4
+
5
+ export interface PackageSymbolOptions extends OutputSymbolOptions {
6
+ path?: string;
7
+ builtin?: boolean;
8
+ }
9
+
10
+ /**
11
+ * A symbol for a package in Go.
12
+ */
13
+ export class PackageSymbol extends NamedTypeSymbol {
14
+ public readonly symbolKind = "package";
15
+ constructor(
16
+ name: string,
17
+ parentPackage?: PackageSymbol,
18
+ options?: PackageSymbolOptions,
19
+ ) {
20
+ const space = parentPackage?.members;
21
+ super(name, space, "package", options);
22
+ this.#path = options?.path ?? name;
23
+ if (parentPackage) {
24
+ this.#fullyQualifiedName = join(
25
+ parentPackage.fullyQualifiedName,
26
+ this.#path,
27
+ );
28
+ } else {
29
+ this.#fullyQualifiedName = this.#path;
30
+ }
31
+ this.#builtin = options?.builtin || parentPackage?.builtin || false;
32
+ }
33
+
34
+ #path: string;
35
+ #fullyQualifiedName: string;
36
+ get fullyQualifiedName(): string {
37
+ return this.#fullyQualifiedName;
38
+ }
39
+
40
+ #builtin: boolean;
41
+ get builtin() {
42
+ return this.#builtin;
43
+ }
44
+
45
+ copy() {
46
+ const options = this.getGoCopyOptions();
47
+ const copy = new PackageSymbol(this.name, undefined, {
48
+ ...options,
49
+ path: this.#path,
50
+ });
51
+ this.initializeGoCopy(copy);
52
+ return copy;
53
+ }
54
+ }
@@ -0,0 +1,87 @@
1
+ import { memo, OutputSymbol, Refkey, resolve } from "@alloy-js/core";
2
+ import { GoScope } from "../scopes/go.js";
3
+ import { GoPackageScope } from "../scopes/package.js";
4
+ import { useSourceFileScope } from "../scopes/source-file.js";
5
+ import { GoSymbol } from "./go.js";
6
+ import { PackageSymbol } from "./package.js";
7
+
8
+ function closestPackageScope(
9
+ scopes: GoScope[],
10
+ decl: GoSymbol,
11
+ memberPath: GoSymbol[],
12
+ ): {
13
+ pkgSymbol: PackageSymbol | undefined;
14
+ members: GoSymbol[];
15
+ } {
16
+ const allMembers = [decl, ...memberPath];
17
+ for (let i = allMembers.length - 1; i >= 0; i--) {
18
+ const member = allMembers[i];
19
+ if (member instanceof PackageSymbol) {
20
+ return { pkgSymbol: member, members: allMembers.slice(i + 1) };
21
+ }
22
+ }
23
+ for (let i = scopes.length - 1; i >= 0; i--) {
24
+ const scope = scopes[i];
25
+ if (scope instanceof GoPackageScope) {
26
+ return {
27
+ pkgSymbol: scope.ownerSymbol,
28
+ members: allMembers,
29
+ };
30
+ }
31
+ }
32
+ return {
33
+ pkgSymbol: undefined,
34
+ members: allMembers,
35
+ };
36
+ }
37
+
38
+ // converts a refkey to its fully qualified name
39
+ // e.g. if refkey is for bar in enum type foo, and
40
+ // foo is in the same namespace as the refkey, then
41
+ // the result would be foo.bar.
42
+ export function ref(refkey: Refkey): () => [string, OutputSymbol | undefined] {
43
+ const refSfScope = useSourceFileScope()!;
44
+ const resolveResult = resolve<GoScope, GoSymbol>(refkey as Refkey);
45
+
46
+ return memo(() => {
47
+ if (resolveResult.value === undefined) {
48
+ return ["<Unresolved Symbol>", undefined];
49
+ }
50
+
51
+ const result = resolveResult.value;
52
+ const { pathDown, memberPath } = result;
53
+ const { lexicalDeclaration } = result;
54
+
55
+ const parts = [];
56
+
57
+ let localSymbol: GoSymbol | undefined;
58
+
59
+ const { pkgSymbol: pkgSymbol, members: pathFromPkg } = closestPackageScope(
60
+ pathDown,
61
+ lexicalDeclaration,
62
+ memberPath,
63
+ );
64
+ if (pkgSymbol) {
65
+ // we need to import the package
66
+ for (const pathMember of pathFromPkg) {
67
+ const memberName = pathMember.name;
68
+ const firstChar = memberName.charAt(0);
69
+ if (firstChar.toLowerCase() === firstChar) {
70
+ throw new Error(
71
+ `Can't reference non-exported symbol ${pathMember.name} from another package.`,
72
+ );
73
+ }
74
+ }
75
+ localSymbol = refSfScope.addImport(pkgSymbol);
76
+ }
77
+
78
+ if (localSymbol) {
79
+ parts.push(localSymbol.name);
80
+ }
81
+ for (const member of pathFromPkg) {
82
+ parts.push(member.name);
83
+ }
84
+
85
+ return [parts.join("."), result.symbol];
86
+ });
87
+ }