@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,387 @@
1
+ import { Children, code, Output, refkey, Refkey, render } from "@alloy-js/core";
2
+ import { describe, expect, it } from "vitest";
3
+ import { TestPackage } from "../../../test/utils.js";
4
+ import { ModuleDirectory } from "../ModuleDirectory.js";
5
+ import { SourceDirectory } from "../SourceDirectory.js";
6
+ import { SourceFile } from "../SourceFile.js";
7
+ import { StructDeclaration } from "../struct/declaration.js";
8
+ import { TypeDeclaration } from "../type/declaration.js";
9
+ import { FunctionDeclaration, FunctionReceiver } from "./function.js";
10
+
11
+ const Wrapper = (props: { children: Children; refkey: Refkey }) => (
12
+ <TestPackage>
13
+ <TypeDeclaration refkey={props.refkey} name="TestStruct">
14
+ <StructDeclaration />
15
+ <hbr />
16
+ </TypeDeclaration>
17
+ {props.children}
18
+ </TestPackage>
19
+ );
20
+
21
+ it("applies PascalCase naming policy when exported", () => {
22
+ expect(
23
+ <TestPackage>
24
+ <FunctionDeclaration name="MethodOne" />
25
+ </TestPackage>,
26
+ ).toRenderTo(`
27
+ package alloy
28
+
29
+ func MethodOne() {}
30
+ `);
31
+ });
32
+
33
+ it("applies camelCase naming policy when not exported", () => {
34
+ expect(
35
+ <TestPackage>
36
+ <FunctionDeclaration name="methodOne" />
37
+ </TestPackage>,
38
+ ).toRenderTo(`
39
+ package alloy
40
+
41
+ func methodOne() {}
42
+ `);
43
+ });
44
+
45
+ it("defines single-line params and return type", () => {
46
+ const params = [
47
+ {
48
+ name: "intParam",
49
+ type: "int",
50
+ },
51
+ {
52
+ name: "stringParam",
53
+ type: "string",
54
+ },
55
+ ];
56
+ const res = (
57
+ <TestPackage>
58
+ <FunctionDeclaration
59
+ name="MethodOne"
60
+ parameters={params}
61
+ returns="string"
62
+ />
63
+ </TestPackage>
64
+ );
65
+
66
+ expect(res).toRenderTo(`
67
+ package alloy
68
+
69
+ func MethodOne(intParam int, stringParam string) string {}
70
+ `);
71
+ });
72
+
73
+ it("defines multi-line params and return type", () => {
74
+ const params = [
75
+ {
76
+ name: "intParam",
77
+ type: "int",
78
+ },
79
+ {
80
+ name: "stringParam",
81
+ type: "string",
82
+ },
83
+ {
84
+ name: "stringParam2",
85
+ type: "string",
86
+ },
87
+ {
88
+ name: "stringParam3",
89
+ type: "string",
90
+ },
91
+ ];
92
+ const res = (
93
+ <TestPackage>
94
+ <FunctionDeclaration
95
+ name="MethodOne"
96
+ parameters={params}
97
+ returns="string"
98
+ />
99
+ </TestPackage>
100
+ );
101
+
102
+ expect(res).toRenderTo(`
103
+ package alloy
104
+
105
+ func MethodOne(
106
+ intParam int,
107
+ stringParam string,
108
+ stringParam2 string,
109
+ stringParam3 string,
110
+ ) string {}
111
+ `);
112
+ });
113
+
114
+ it("defines single-line return type", () => {
115
+ const res = (
116
+ <TestPackage>
117
+ <FunctionDeclaration name="MethodOne" returns={["string", "error"]} />
118
+ </TestPackage>
119
+ );
120
+
121
+ expect(res).toRenderTo(`
122
+ package alloy
123
+
124
+ func MethodOne() (string, error) {}
125
+ `);
126
+ });
127
+
128
+ it("defines multi-line return type", () => {
129
+ const res = (
130
+ <TestPackage>
131
+ <FunctionDeclaration
132
+ name="MethodOne"
133
+ returns={[
134
+ "string",
135
+ "string",
136
+ "string",
137
+ "string",
138
+ "string",
139
+ "string",
140
+ "string",
141
+ "string",
142
+ "string",
143
+ "string",
144
+ "error",
145
+ ]}
146
+ />
147
+ </TestPackage>
148
+ );
149
+
150
+ expect(res).toRenderTo(`
151
+ package alloy
152
+
153
+ func MethodOne() (
154
+ string,
155
+ string,
156
+ string,
157
+ string,
158
+ string,
159
+ string,
160
+ string,
161
+ string,
162
+ string,
163
+ string,
164
+ error,
165
+ ) {}
166
+ `);
167
+ });
168
+
169
+ it("specify doc comment", () => {
170
+ expect(
171
+ <TestPackage>
172
+ <FunctionDeclaration
173
+ name="MethodOne"
174
+ doc={
175
+ <>
176
+ {"This is a test"}
177
+ <hbr />
178
+ {"This is another test"}
179
+ </>
180
+ }
181
+ />
182
+ </TestPackage>,
183
+ ).toRenderTo(`
184
+ package alloy
185
+
186
+ // This is a test
187
+ // This is another test
188
+ func MethodOne() {}
189
+ `);
190
+ });
191
+
192
+ it("use single-line form", () => {
193
+ expect(
194
+ <TestPackage>
195
+ <FunctionDeclaration name="MethodOne" singleLine>
196
+ return nil
197
+ </FunctionDeclaration>
198
+ </TestPackage>,
199
+ ).toRenderTo(`
200
+ package alloy
201
+
202
+ func MethodOne() { return nil }
203
+ `);
204
+ });
205
+
206
+ it("use children", () => {
207
+ expect(
208
+ <TestPackage>
209
+ <FunctionDeclaration name="MethodOne">return nil</FunctionDeclaration>
210
+ </TestPackage>,
211
+ ).toRenderTo(`
212
+ package alloy
213
+
214
+ func MethodOne() {
215
+ return nil
216
+ }
217
+ `);
218
+ });
219
+
220
+ it("use multiple children", () => {
221
+ expect(
222
+ <TestPackage>
223
+ <FunctionDeclaration name="MethodOne">
224
+ {code`
225
+ lineOne()
226
+ lineTwo()
227
+ return nil
228
+ `}
229
+ </FunctionDeclaration>
230
+ </TestPackage>,
231
+ ).toRenderTo(`
232
+ package alloy
233
+
234
+ func MethodOne() {
235
+ lineOne()
236
+ lineTwo()
237
+ return nil
238
+ }
239
+ `);
240
+ });
241
+
242
+ it("use method", () => {
243
+ const ReceiverRefkey = refkey("ReceiverRefkey");
244
+
245
+ expect(
246
+ <Wrapper refkey={ReceiverRefkey}>
247
+ <FunctionDeclaration
248
+ name="MethodOne"
249
+ receiver={<FunctionReceiver name="s" type={ReceiverRefkey} />}
250
+ />
251
+ </Wrapper>,
252
+ ).toRenderTo(`
253
+ package alloy
254
+
255
+ type TestStruct struct{}
256
+ func (s TestStruct) MethodOne() {}
257
+ `);
258
+ });
259
+
260
+ it("use method variadic params", () => {
261
+ const ReceiverRefkey = refkey("ReceiverRefkey");
262
+
263
+ expect(
264
+ <Wrapper refkey={ReceiverRefkey}>
265
+ <FunctionDeclaration
266
+ name="MethodOne"
267
+ receiver={<FunctionReceiver name="s" type={ReceiverRefkey} />}
268
+ parameters={[
269
+ { name: "i", type: "int" },
270
+ { name: "args", type: "string", variadic: true },
271
+ ]}
272
+ />
273
+ </Wrapper>,
274
+ ).toRenderTo(`
275
+ package alloy
276
+
277
+ type TestStruct struct{}
278
+ func (s TestStruct) MethodOne(i int, args ...string) {}
279
+ `);
280
+ });
281
+
282
+ it("use method cross-package fail", () => {
283
+ const ReceiverRefkey = refkey("ReceiverRefkey");
284
+
285
+ expect(() =>
286
+ render(
287
+ <Output>
288
+ <ModuleDirectory name="github.com/alloy-framework/alloy">
289
+ <SourceDirectory path="hello">
290
+ <SourceFile path="types.go">
291
+ <TypeDeclaration refkey={ReceiverRefkey} name="TestStruct">
292
+ <StructDeclaration />
293
+ </TypeDeclaration>
294
+ </SourceFile>
295
+ </SourceDirectory>
296
+ <SourceDirectory path="world">
297
+ <SourceFile path="test.go">
298
+ <FunctionDeclaration
299
+ name="MethodOne"
300
+ receiver={<FunctionReceiver name="s" type={ReceiverRefkey} />}
301
+ />
302
+ </SourceFile>
303
+ </SourceDirectory>
304
+ </ModuleDirectory>
305
+ </Output>,
306
+ ),
307
+ ).toThrowError(
308
+ "Receiver symbol s must be in the same package as the type TestStruct.",
309
+ );
310
+ });
311
+
312
+ describe("type parameters", () => {
313
+ it("function type param", () => {
314
+ const T = refkey("T");
315
+ expect(
316
+ <TestPackage>
317
+ <FunctionDeclaration
318
+ name="MethodOne"
319
+ parameters={[{ name: "arg", type: T }]}
320
+ typeParameters={[{ name: "T", constraint: "any", refkey: T }]}
321
+ />
322
+ </TestPackage>,
323
+ ).toRenderTo(`
324
+ package alloy
325
+
326
+ func MethodOne[T any](arg T) {}
327
+ `);
328
+ });
329
+
330
+ it("method auto type param", () => {
331
+ const ReceiverRefkey = refkey("ReceiverRefkey");
332
+
333
+ expect(
334
+ <TestPackage>
335
+ <TypeDeclaration
336
+ refkey={ReceiverRefkey}
337
+ name="TestStruct"
338
+ typeParameters={[{ name: "T", constraint: "any" }]}
339
+ >
340
+ <StructDeclaration />
341
+ <hbr />
342
+ </TypeDeclaration>
343
+ <FunctionDeclaration
344
+ name="MethodOne"
345
+ receiver={<FunctionReceiver name="s" type={ReceiverRefkey} />}
346
+ />
347
+ </TestPackage>,
348
+ ).toRenderTo(`
349
+ package alloy
350
+
351
+ type TestStruct[T any] struct{}
352
+ func (s TestStruct[T any]) MethodOne() {}
353
+ `);
354
+ });
355
+
356
+ it("method type param", () => {
357
+ const ReceiverRefkey = refkey("ReceiverRefkey");
358
+
359
+ expect(
360
+ <TestPackage>
361
+ <TypeDeclaration
362
+ refkey={ReceiverRefkey}
363
+ name="TestStruct"
364
+ typeParameters={[{ name: "T", constraint: "any" }]}
365
+ >
366
+ <StructDeclaration />
367
+ <hbr />
368
+ </TypeDeclaration>
369
+ <FunctionDeclaration
370
+ name="MethodOne"
371
+ receiver={
372
+ <FunctionReceiver
373
+ name="s"
374
+ type={ReceiverRefkey}
375
+ typeParameters={[{ name: "U", constraint: "any" }]}
376
+ />
377
+ }
378
+ />
379
+ </TestPackage>,
380
+ ).toRenderTo(`
381
+ package alloy
382
+
383
+ type TestStruct[T any] struct{}
384
+ func (s TestStruct[U any]) MethodOne() {}
385
+ `);
386
+ });
387
+ });
@@ -0,0 +1,194 @@
1
+ import {
2
+ Block,
3
+ Children,
4
+ computed,
5
+ Declaration,
6
+ DeclarationContext,
7
+ effect,
8
+ For,
9
+ Indent,
10
+ Name,
11
+ Namekey,
12
+ Refkey,
13
+ Scope,
14
+ shallowRef,
15
+ Show,
16
+ takeSymbols,
17
+ useContext,
18
+ watch,
19
+ } from "@alloy-js/core";
20
+ import { useGoScope } from "../../scopes/contexts.js";
21
+ import { createFunctionScope } from "../../scopes/factories.js";
22
+ import { GoSourceFileScope } from "../../scopes/source-file.js";
23
+ import {
24
+ createFunctionSymbol,
25
+ createParameterSymbol,
26
+ } from "../../symbols/factories.js";
27
+ import { FunctionSymbol } from "../../symbols/function.js";
28
+ import { GoSymbol } from "../../symbols/go.js";
29
+ import { NamedTypeSymbol } from "../../symbols/named-type.js";
30
+ import { LineComment } from "../doc/comment.jsx";
31
+ import {
32
+ FunctionParameterProps,
33
+ FunctionParameters,
34
+ } from "../parameters/parameters.jsx";
35
+ import {
36
+ TypeParameterProps,
37
+ TypeParameters,
38
+ } from "../parameters/typeparameters.jsx";
39
+
40
+ // properties for creating a function
41
+ export interface FunctionProps {
42
+ name: string | Namekey;
43
+ parameters?: FunctionParameterProps[];
44
+ returns?: Children;
45
+
46
+ refkey?: Refkey;
47
+
48
+ /** Doc comment */
49
+ doc?: Children;
50
+
51
+ /** Go receiver (for methods) */
52
+ receiver?: Children;
53
+
54
+ /** Type parameters for generic functions */
55
+ typeParameters?: TypeParameterProps[];
56
+
57
+ /**
58
+ * Whether to render the function in a single line.
59
+ * This will only compile if `children` is a single line as well.
60
+ * This is not common in Go, so use with caution.
61
+ */
62
+ singleLine?: boolean;
63
+
64
+ children?: Children;
65
+ }
66
+
67
+ // a Go function
68
+ export function FunctionDeclaration(props: FunctionProps) {
69
+ const isFileScope = useGoScope() instanceof GoSourceFileScope;
70
+
71
+ if (props.receiver && !isFileScope) {
72
+ throw new Error("Methods can only be declared in file scope.");
73
+ }
74
+
75
+ const functionSymbol = createFunctionSymbol(props.name, !!props.receiver, {
76
+ refkeys: props.refkey,
77
+ });
78
+
79
+ // scope for function declaration
80
+ const functionScope = createFunctionScope();
81
+
82
+ // note that scope wraps the function declaration so that the params get the correct scope
83
+ return (
84
+ <Declaration symbol={functionSymbol}>
85
+ <Scope value={functionScope}>
86
+ <Show when={Boolean(props.doc)}>
87
+ <LineComment children={props.doc} />
88
+ <hbr />
89
+ </Show>
90
+ func{" "}
91
+ {props.receiver ?
92
+ <>({props.receiver}) </>
93
+ : null}
94
+ <Name />
95
+ <TypeParameters parameters={props.typeParameters} />
96
+ <FunctionParameters parameters={props.parameters} />{" "}
97
+ {props.returns ?
98
+ Array.isArray(props.returns) && props.returns.length > 1 ?
99
+ <>
100
+ <group>
101
+ {"("}
102
+ <Indent softline>
103
+ <For
104
+ each={props.returns}
105
+ joiner={
106
+ <>
107
+ {","}
108
+ <ifBreak flatContents=" ">
109
+ <sbr />
110
+ </ifBreak>
111
+ </>
112
+ }
113
+ ender={<ifBreak>,</ifBreak>}
114
+ >
115
+ {(ret) => ret}
116
+ </For>
117
+ </Indent>
118
+ <sbr />
119
+ {")"}
120
+ </group>{" "}
121
+ </>
122
+ : <>{props.returns} </>
123
+ : null}
124
+ {!props.children ?
125
+ "{}"
126
+ : props.singleLine ?
127
+ <>
128
+ {"{"} {props.children} {"}"}
129
+ </>
130
+ : <Block>{props.children}</Block>}
131
+ </Scope>
132
+ </Declaration>
133
+ );
134
+ }
135
+
136
+ export interface FuncReceiverProps {
137
+ name: string | Namekey;
138
+ type: Children;
139
+ refkey?: Refkey;
140
+ typeParameters?: TypeParameterProps[];
141
+ }
142
+
143
+ export function FunctionReceiver(props: FuncReceiverProps) {
144
+ const receiverSymbol = createParameterSymbol(props.name, {
145
+ refkeys: props.refkey,
146
+ });
147
+
148
+ const functionSymbol = useContext(DeclarationContext);
149
+ if (!(functionSymbol instanceof FunctionSymbol)) {
150
+ throw new Error("FuncReceiver must be used inside a function.");
151
+ }
152
+
153
+ const typeParams = shallowRef(props.typeParameters ?? []);
154
+
155
+ const taken = takeSymbols();
156
+ effect(() => {
157
+ if (taken.size !== 1) return;
158
+ const symbol = Array.from(taken)[0] as GoSymbol;
159
+ if (symbol.enclosingPackage !== receiverSymbol.enclosingPackage) {
160
+ throw new Error(
161
+ `Receiver symbol ${receiverSymbol.name} must be in the same package as the type ${symbol.name}.`,
162
+ );
163
+ }
164
+ if (!(symbol instanceof NamedTypeSymbol)) {
165
+ throw new Error(
166
+ `Receiver type must be a named type, got ${symbol.constructor.name}.`,
167
+ );
168
+ }
169
+
170
+ functionSymbol.spaces = symbol.members;
171
+ if (!props.typeParameters) {
172
+ typeParams.value = symbol.typeParameters ?? [];
173
+ }
174
+ watch(
175
+ () => symbol.typeParameters,
176
+ (newParams) => {
177
+ if (props.typeParameters) return;
178
+ typeParams.value = newParams ?? [];
179
+ },
180
+ );
181
+ });
182
+
183
+ const typeParamsComponent = computed(() => {
184
+ if (typeParams.value.length === 0) return null;
185
+ return <TypeParameters parameters={typeParams.value} />;
186
+ });
187
+
188
+ return (
189
+ <Declaration symbol={receiverSymbol}>
190
+ <Name /> {props.type}
191
+ {typeParamsComponent.value}
192
+ </Declaration>
193
+ );
194
+ }
@@ -0,0 +1,15 @@
1
+ export * from "./doc/comment.js";
2
+ export * from "./function/function.js";
3
+ export * from "./ImportStatement.js";
4
+ export * from "./interface/declaration.js";
5
+ export * from "./ModuleDirectory.js";
6
+ export * from "./Name.js";
7
+ export * from "./parameters/parameters.js";
8
+ export * from "./parameters/typeparameters.jsx";
9
+ export * from "./pointer/pointer.js";
10
+ export * from "./Reference.js";
11
+ export * from "./SourceDirectory.js";
12
+ export * from "./SourceFile.js";
13
+ export * from "./struct/declaration.js";
14
+ export * from "./type/declaration.js";
15
+ export * from "./var/declaration.js";