@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,138 @@
1
+ import {
2
+ Children,
3
+ ComponentContext,
4
+ computed,
5
+ createContext,
6
+ Declaration,
7
+ Indent,
8
+ List,
9
+ memo,
10
+ Name,
11
+ Namekey,
12
+ Refkey,
13
+ Show,
14
+ useContext,
15
+ } from "@alloy-js/core";
16
+ import { useGoScope } from "../../scopes/contexts.js";
17
+ import { GoSourceFileScope } from "../../scopes/source-file.js";
18
+ import { createVariableSymbol } from "../../symbols/factories.js";
19
+ import { LineComment } from "../doc/comment.jsx";
20
+
21
+ export interface VariableDeclarationProps {
22
+ /** Variable name */
23
+ name: string | Namekey;
24
+ /** Type of the variable declaration */
25
+ type?: Children;
26
+ /** Variable refkey */
27
+ refkey?: Refkey;
28
+ /** Documentation comment */
29
+ doc?: Children;
30
+ /** Whether this is a `const` declaration */
31
+ const?: boolean;
32
+ /** Initializer expression */
33
+ children?: Children;
34
+ }
35
+
36
+ export interface VariableDeclarationGroupProps {
37
+ const?: boolean;
38
+ children: Children;
39
+ }
40
+
41
+ export interface VariableDeclarationGroupContext {
42
+ const: boolean;
43
+ active: boolean;
44
+ }
45
+
46
+ export const VariableDeclarationGroupContext: ComponentContext<VariableDeclarationGroupContext> =
47
+ createContext<VariableDeclarationGroupContext>();
48
+
49
+ export function VariableDeclarationGroup(props: VariableDeclarationGroupProps) {
50
+ const keyword = computed(() => (props.const ? "const" : "var"));
51
+
52
+ return (
53
+ <VariableDeclarationGroupContext.Provider
54
+ value={{ active: true, const: !!props.const }}
55
+ >
56
+ {keyword} (
57
+ <Indent trailingBreak>
58
+ <List hardline>{props.children}</List>
59
+ </Indent>
60
+ )
61
+ </VariableDeclarationGroupContext.Provider>
62
+ );
63
+ }
64
+
65
+ export function VariableDeclaration(props: VariableDeclarationProps) {
66
+ const isFileScope = useGoScope() instanceof GoSourceFileScope;
67
+ const declarationGroupContext = useContext(VariableDeclarationGroupContext);
68
+ const inDeclarationGroup = !!declarationGroupContext?.active;
69
+ const isConst = declarationGroupContext?.const ?? props.const;
70
+ const symbol = createVariableSymbol(props.name, { refkeys: props.refkey });
71
+ const content = memo(() => {
72
+ const keyword = isConst ? "const" : "var";
73
+
74
+ if (inDeclarationGroup) {
75
+ if (!props.type && !props.children) {
76
+ if (!isConst) {
77
+ throw new Error(
78
+ "Variable declaration must have a type or initializer.",
79
+ );
80
+ }
81
+ return <Name />;
82
+ } else if (props.type && props.children) {
83
+ return (
84
+ <>
85
+ <Name /> {props.type} = {props.children}
86
+ </>
87
+ );
88
+ } else if (props.type) {
89
+ if (declarationGroupContext?.const) {
90
+ throw new Error("Const declaration must have an initializer.");
91
+ }
92
+ return (
93
+ <>
94
+ <Name /> {props.type}
95
+ </>
96
+ );
97
+ } else if (props.children) {
98
+ return (
99
+ <>
100
+ <Name /> = {props.children}
101
+ </>
102
+ );
103
+ }
104
+ } else {
105
+ if (!props.type && !props.children) {
106
+ throw new Error(
107
+ "Variable declaration must have a type or initializer.",
108
+ );
109
+ } else if (!props.children || isConst || props.type || isFileScope) {
110
+ return (
111
+ <>
112
+ {keyword} <Name /> {props.type}
113
+ {props.type && props.children ? " " : ""}
114
+ {props.children ? "= " : ""}
115
+ {props.children}
116
+ </>
117
+ );
118
+ } else {
119
+ return (
120
+ <>
121
+ <Name /> :{props.children ? "= " : ""}
122
+ {props.children}
123
+ </>
124
+ );
125
+ }
126
+ }
127
+ });
128
+
129
+ return (
130
+ <>
131
+ <Show when={Boolean(props.doc)}>
132
+ <LineComment children={props.doc} />
133
+ <hbr />
134
+ </Show>
135
+ <Declaration symbol={symbol}>{content}</Declaration>
136
+ </>
137
+ );
138
+ }
@@ -0,0 +1,236 @@
1
+ import { Output, refkey, render } from "@alloy-js/core";
2
+ import "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import { assertFileContents } from "../../../test/utils.js";
5
+ import * as go from "../index.js";
6
+
7
+ it("works variable", () => {
8
+ expect(
9
+ <Output>
10
+ <go.ModuleDirectory name="github.com/alloy-framework/alloy">
11
+ <go.SourceDirectory path=".">
12
+ <go.SourceFile path="test.go">
13
+ <go.VariableDeclaration name="hi">12</go.VariableDeclaration>
14
+ </go.SourceFile>
15
+ </go.SourceDirectory>
16
+ </go.ModuleDirectory>
17
+ </Output>,
18
+ ).toRenderTo(`
19
+ package alloy
20
+
21
+ var hi = 12
22
+ `);
23
+ });
24
+
25
+ it("works variable group", () => {
26
+ expect(
27
+ <Output>
28
+ <go.ModuleDirectory name="github.com/alloy-framework/alloy">
29
+ <go.SourceDirectory path=".">
30
+ <go.SourceFile path="test.go">
31
+ <go.VariableDeclarationGroup>
32
+ <go.VariableDeclaration name="hi1">12</go.VariableDeclaration>
33
+ <go.VariableDeclaration name="hi2" type="string">
34
+ 12
35
+ </go.VariableDeclaration>
36
+ <go.VariableDeclaration
37
+ name="hi3"
38
+ type="string"
39
+ ></go.VariableDeclaration>
40
+ </go.VariableDeclarationGroup>
41
+ </go.SourceFile>
42
+ </go.SourceDirectory>
43
+ </go.ModuleDirectory>
44
+ </Output>,
45
+ ).toRenderTo(`
46
+ package alloy
47
+
48
+ var (
49
+ hi1 = 12
50
+ hi2 string = 12
51
+ hi3 string
52
+ )
53
+ `);
54
+ });
55
+
56
+ it("works const group", () => {
57
+ expect(
58
+ <Output>
59
+ <go.ModuleDirectory name="github.com/alloy-framework/alloy">
60
+ <go.SourceDirectory path=".">
61
+ <go.SourceFile path="test.go">
62
+ <go.VariableDeclarationGroup const>
63
+ <go.VariableDeclaration name="hi1">12</go.VariableDeclaration>
64
+ <go.VariableDeclaration name="hi2" type="string">
65
+ 12
66
+ </go.VariableDeclaration>
67
+ </go.VariableDeclarationGroup>
68
+ </go.SourceFile>
69
+ </go.SourceDirectory>
70
+ </go.ModuleDirectory>
71
+ </Output>,
72
+ ).toRenderTo(`
73
+ package alloy
74
+
75
+ const (
76
+ hi1 = 12
77
+ hi2 string = 12
78
+ )
79
+ `);
80
+ });
81
+
82
+ it("works end-to-end", () => {
83
+ const TestType = refkey("TestType");
84
+
85
+ const res = render(
86
+ <Output>
87
+ <go.ModuleDirectory name="github.com/alloy-framework/alloy">
88
+ <go.SourceDirectory path=".">
89
+ <go.SourceFile path="types.go">
90
+ <go.TypeDeclaration name="TestType" refkey={TestType}>
91
+ string
92
+ </go.TypeDeclaration>
93
+ </go.SourceFile>
94
+ <go.SourceFile path="test.go">
95
+ <go.VariableDeclaration name="hi" type={TestType}>
96
+ "hello"
97
+ </go.VariableDeclaration>
98
+ </go.SourceFile>
99
+ </go.SourceDirectory>
100
+ </go.ModuleDirectory>
101
+ </Output>,
102
+ );
103
+
104
+ assertFileContents(res, {
105
+ "types.go": `
106
+ package alloy
107
+
108
+ type TestType string
109
+ `,
110
+ "test.go": `
111
+ package alloy
112
+
113
+ var hi TestType = "hello"
114
+ `,
115
+ });
116
+ });
117
+
118
+ it("throws end-to-end cross-package with unexported type", () => {
119
+ const TestType = refkey("TestType");
120
+
121
+ expect(() =>
122
+ render(
123
+ <Output>
124
+ <go.ModuleDirectory name="github.com/alloy-framework/alloy">
125
+ <go.SourceDirectory path="hello">
126
+ <go.SourceFile path="types.go">
127
+ <go.TypeDeclaration name="testType" refkey={TestType}>
128
+ string
129
+ </go.TypeDeclaration>
130
+ </go.SourceFile>
131
+ </go.SourceDirectory>
132
+ <go.SourceDirectory path="world">
133
+ <go.SourceFile path="test.go">
134
+ <go.VariableDeclaration name="hi" type={TestType}>
135
+ "hello"
136
+ </go.VariableDeclaration>
137
+ </go.SourceFile>
138
+ </go.SourceDirectory>
139
+ </go.ModuleDirectory>
140
+ </Output>,
141
+ ),
142
+ ).toThrowError(
143
+ "Can't reference non-exported symbol testType from another package.",
144
+ );
145
+ });
146
+
147
+ it("works end-to-end cross-package", () => {
148
+ const TestType = refkey("TestType");
149
+
150
+ const res = render(
151
+ <Output>
152
+ <go.ModuleDirectory name="github.com/alloy-framework/alloy">
153
+ <go.SourceDirectory path="hello">
154
+ <go.SourceFile path="types.go">
155
+ <go.TypeDeclaration name="TestType" refkey={TestType}>
156
+ string
157
+ </go.TypeDeclaration>
158
+ </go.SourceFile>
159
+ </go.SourceDirectory>
160
+ <go.SourceDirectory path="world">
161
+ <go.SourceFile path="test.go">
162
+ <go.VariableDeclaration name="hi" type={TestType}>
163
+ "hello"
164
+ </go.VariableDeclaration>
165
+ </go.SourceFile>
166
+ </go.SourceDirectory>
167
+ </go.ModuleDirectory>
168
+ </Output>,
169
+ );
170
+
171
+ assertFileContents(res, {
172
+ "hello/types.go": `
173
+ package hello
174
+
175
+ type TestType string
176
+ `,
177
+ "world/test.go": `
178
+ package world
179
+
180
+ import "github.com/alloy-framework/alloy/hello"
181
+
182
+ var hi hello.TestType = "hello"
183
+ `,
184
+ });
185
+ });
186
+
187
+ it("works with conflict resolution", () => {
188
+ const TestType1 = refkey("TestType1");
189
+ const TestType2 = refkey("TestType2");
190
+
191
+ const res = render(
192
+ <Output>
193
+ <go.ModuleDirectory name="github.com/alloy-framework/alloy">
194
+ <go.SourceDirectory path="hello" name="hello">
195
+ <go.SourceFile path="types.go">
196
+ <go.TypeDeclaration name="TestType" refkey={TestType1}>
197
+ string
198
+ </go.TypeDeclaration>
199
+ </go.SourceFile>
200
+ </go.SourceDirectory>
201
+ <go.SourceDirectory path="hello2" name="hello">
202
+ <go.SourceFile path="types.go">
203
+ <go.TypeDeclaration name="TestType" refkey={TestType2}>
204
+ string
205
+ </go.TypeDeclaration>
206
+ </go.SourceFile>
207
+ </go.SourceDirectory>
208
+ <go.SourceDirectory path="world">
209
+ <go.SourceFile path="test.go">
210
+ <go.VariableDeclaration name="hi1" type={TestType1}>
211
+ "hello"
212
+ </go.VariableDeclaration>
213
+ <hbr />
214
+ <go.VariableDeclaration name="hi2" type={TestType2}>
215
+ "hello"
216
+ </go.VariableDeclaration>
217
+ </go.SourceFile>
218
+ </go.SourceDirectory>
219
+ </go.ModuleDirectory>
220
+ </Output>,
221
+ );
222
+
223
+ assertFileContents(res, {
224
+ "world/test.go": `
225
+ package world
226
+
227
+ import (
228
+ "github.com/alloy-framework/alloy/hello"
229
+ hello_2 "github.com/alloy-framework/alloy/hello2"
230
+ )
231
+
232
+ var hi1 hello.TestType = "hello"
233
+ var hi2 hello_2.TestType = "hello"
234
+ `,
235
+ });
236
+ });
@@ -0,0 +1,13 @@
1
+ import { ComponentContext, createContext, useContext } from "@alloy-js/core";
2
+ import { PackageSymbol } from "../symbols/package.js";
3
+
4
+ interface PackageContext {
5
+ symbol: PackageSymbol;
6
+ }
7
+
8
+ export const PackageContext: ComponentContext<PackageContext> =
9
+ createContext<PackageContext>();
10
+
11
+ export function usePackageContext() {
12
+ return useContext(PackageContext);
13
+ }
@@ -0,0 +1,128 @@
1
+ import { TestPackage } from "#test/utils.jsx";
2
+ import { namekey, toRefkey } from "@alloy-js/core";
3
+ import { expect, it } from "vitest";
4
+ import { fmt } from "./builtins/fmt/fmt.js";
5
+ import { VariableDeclaration } from "./components/index.js";
6
+ import { createModule, StrictDescriptor } from "./create-module.js";
7
+
8
+ it("Can reference types which haven't been created yet", () => {
9
+ const std1 = createModule("std1", {
10
+ kind: "package",
11
+ members: {
12
+ SomeType: {
13
+ kind: "struct",
14
+ members: {
15
+ Special: { kind: "field", type: () => std2.SomeType2 },
16
+ },
17
+ },
18
+ },
19
+ } satisfies StrictDescriptor);
20
+
21
+ const std2 = createModule("std2", {
22
+ kind: "package",
23
+ members: {
24
+ SomeType2: {
25
+ kind: "struct",
26
+ members: {
27
+ Hello: { kind: "field" },
28
+ },
29
+ },
30
+ },
31
+ } satisfies StrictDescriptor);
32
+
33
+ const test = namekey("test");
34
+
35
+ expect(
36
+ <TestPackage>
37
+ <VariableDeclaration name={test} type={std1.SomeType} />
38
+ </TestPackage>,
39
+ ).toRenderTo(`
40
+ package alloy
41
+
42
+ import "std1"
43
+
44
+ var test std1.SomeType
45
+ `);
46
+ });
47
+
48
+ it("renders as a refkey", () => {
49
+ const mod = createModule("std", {
50
+ kind: "package",
51
+ members: {
52
+ BinaryReader: {
53
+ kind: "struct",
54
+ members: {},
55
+ },
56
+ },
57
+ } satisfies StrictDescriptor);
58
+
59
+ expect(
60
+ <TestPackage>
61
+ <VariableDeclaration name="test" type={mod.BinaryReader} />
62
+ </TestPackage>,
63
+ ).toRenderTo(`
64
+ package alloy
65
+
66
+ import "std"
67
+
68
+ var test std.BinaryReader
69
+ `);
70
+ });
71
+
72
+ it("has a refkey getter", () => {
73
+ const std = createModule("std", {
74
+ kind: "package",
75
+ members: {
76
+ SomeType: {
77
+ kind: "struct",
78
+ members: {
79
+ Special: { kind: "field" },
80
+ },
81
+ },
82
+ },
83
+ } satisfies StrictDescriptor);
84
+
85
+ const ioKey = toRefkey(std);
86
+ expect(ioKey).toBeDefined();
87
+ const brKey = toRefkey(std.SomeType);
88
+ expect(brKey).toBeDefined();
89
+ });
90
+
91
+ it("has built-ins available", () => {
92
+ const template = <TestPackage>{fmt.Println}("Hello, World!")</TestPackage>;
93
+
94
+ expect(template).toRenderTo(`
95
+ package alloy
96
+
97
+ import "fmt"
98
+
99
+ fmt.Println("Hello, World!")
100
+ `);
101
+ });
102
+
103
+ it("can render multiple times", () => {
104
+ const mod = createModule("std", {
105
+ kind: "package",
106
+ members: {
107
+ BinaryReader: {
108
+ kind: "struct",
109
+ members: {},
110
+ },
111
+ },
112
+ } satisfies StrictDescriptor);
113
+
114
+ expect(<TestPackage>{mod.BinaryReader}</TestPackage>).toRenderTo(`
115
+ package alloy
116
+
117
+ import "std"
118
+
119
+ std.BinaryReader
120
+ `);
121
+ expect(<TestPackage>{mod.BinaryReader}</TestPackage>).toRenderTo(`
122
+ package alloy
123
+
124
+ import "std"
125
+
126
+ std.BinaryReader
127
+ `);
128
+ });