@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,250 @@
1
+ import {
2
+ Block,
3
+ Children,
4
+ Declaration,
5
+ DeclarationContext,
6
+ effect,
7
+ For,
8
+ memo,
9
+ Namekey,
10
+ Refkey,
11
+ Scope,
12
+ Show,
13
+ takeSymbols,
14
+ useContext,
15
+ watch,
16
+ } from "@alloy-js/core";
17
+ import { useGoScope, useNamedTypeScope } from "../../scopes/contexts.js";
18
+ import {
19
+ createFunctionScope,
20
+ createNamedTypeScope,
21
+ } from "../../scopes/factories.js";
22
+ import { GoNamedTypeScope } from "../../scopes/named-type.js";
23
+ import {
24
+ createAnonymousTypeSymbol,
25
+ createInterfaceMemberSymbol,
26
+ } from "../../symbols/factories.js";
27
+ import { GoSymbol } from "../../symbols/go.js";
28
+ import { NamedTypeSymbol } from "../../symbols/named-type.js";
29
+ import { LineComment } from "../doc/comment.js";
30
+ import { Name } from "../Name.js";
31
+ import {
32
+ FunctionParameterProps,
33
+ FunctionParameters,
34
+ } from "../parameters/parameters.js";
35
+ import { TypeDeclaration, TypeDeclarationProps } from "../type/declaration.jsx";
36
+
37
+ /**
38
+ * Properties for creating an interface declaration.
39
+ * This can be used for both named and anonymous interfaces.
40
+ * For named interfaces, use `InterfaceTypeDeclarationProps` instead.
41
+ */
42
+ export interface InterfaceDeclarationProps {
43
+ refkey?: Refkey;
44
+ children?: Children;
45
+
46
+ /**
47
+ * Whether to render the interface in a single line.
48
+ * This will only compile if `children` is a single line as well.
49
+ * This is not common in Go, so use with caution.
50
+ */
51
+ singleLine?: boolean;
52
+ }
53
+
54
+ /**
55
+ * Properties for creating a named interface type declaration.
56
+ * This is a combination of `InterfaceDeclarationProps` and `TypeDeclarationProps`.
57
+ */
58
+ export interface InterfaceTypeDeclarationProps
59
+ extends InterfaceDeclarationProps,
60
+ TypeDeclarationProps {}
61
+
62
+ /**
63
+ * Wrapper for creating a named interface type declaration.
64
+ */
65
+ export function InterfaceTypeDeclaration(props: InterfaceTypeDeclarationProps) {
66
+ return (
67
+ <TypeDeclaration {...props}>
68
+ <InterfaceDeclaration {...props} />
69
+ </TypeDeclaration>
70
+ );
71
+ }
72
+
73
+ /**
74
+ * Go interface declaration.
75
+ * @example
76
+ * ```tsx
77
+ * <InterfaceDeclaration>
78
+ * <InterfaceEmbed>{MyOtherInterface}</InterfaceEmbed>
79
+ * <InterfaceFunction name="MyFunc" parameters={params} returns="int" />
80
+ * </InterfaceDeclaration>
81
+ * ```
82
+ * This will produce:
83
+ * ```go
84
+ * interface {
85
+ * MyOtherInterface
86
+ * func MyFunc(param Param) int
87
+ * }
88
+ * ```
89
+ */
90
+ export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
91
+ const scope = useGoScope();
92
+ let interfaceScope: GoNamedTypeScope | undefined;
93
+ if (
94
+ !(scope instanceof GoNamedTypeScope) ||
95
+ scope.ownerSymbol.typeKind !== "type"
96
+ ) {
97
+ const declSymbol = useContext(DeclarationContext) as
98
+ | NamedTypeSymbol
99
+ | undefined;
100
+ // this is an anonymous interface
101
+ if (!declSymbol) {
102
+ // this can only happen when instantiating an interface directly
103
+ const anonymousSymbol = createAnonymousTypeSymbol("interface");
104
+ const anonymousScope = createNamedTypeScope(anonymousSymbol);
105
+ interfaceScope = anonymousScope;
106
+ } else {
107
+ // this is most likely a struct field
108
+ const anonymousScope = createNamedTypeScope(declSymbol);
109
+ interfaceScope = anonymousScope;
110
+ }
111
+ } else {
112
+ scope.ownerSymbol.typeKind = "interface";
113
+ }
114
+
115
+ const content = memo(() => {
116
+ if (props.children) {
117
+ if (props.singleLine) {
118
+ return (
119
+ <>
120
+ {"{"} {props.children} {"}"}
121
+ </>
122
+ );
123
+ } else {
124
+ return (
125
+ <>
126
+ {" "}
127
+ <Block>{props.children}</Block>
128
+ </>
129
+ );
130
+ }
131
+ } else {
132
+ return "{}";
133
+ }
134
+ });
135
+
136
+ return (
137
+ <>
138
+ interface
139
+ {interfaceScope ?
140
+ <Scope value={interfaceScope}>{content}</Scope>
141
+ : content}
142
+ </>
143
+ );
144
+ }
145
+
146
+ export interface InterfaceFunctionProps {
147
+ name: string | Namekey;
148
+ parameters?: FunctionParameterProps[];
149
+ returns?: Children;
150
+ refkey?: Refkey;
151
+ /** Doc comment */
152
+ doc?: Children;
153
+ }
154
+
155
+ export function InterfaceFunction(props: InterfaceFunctionProps) {
156
+ const symbol = createInterfaceMemberSymbol(props.name, {
157
+ refkeys: props.refkey,
158
+ });
159
+ const functionScope = createFunctionScope();
160
+
161
+ return (
162
+ <Declaration symbol={symbol}>
163
+ <Scope value={functionScope}>
164
+ <Show when={Boolean(props.doc)}>
165
+ <LineComment children={props.doc} />
166
+ <hbr />
167
+ </Show>
168
+ func <Name />
169
+ <FunctionParameters parameters={props.parameters} />
170
+ {props.returns ?
171
+ <> {props.returns}</>
172
+ : null}
173
+ </Scope>
174
+ </Declaration>
175
+ );
176
+ }
177
+
178
+ export interface InterfaceEmbedProps {
179
+ refkey?: Refkey;
180
+ children?: Children;
181
+ /** Doc comment */
182
+ doc?: Children;
183
+ }
184
+
185
+ export function InterfaceEmbed(props: InterfaceEmbedProps) {
186
+ let typeName: string = "<Unresolved symbol>";
187
+ if (props.children) {
188
+ if (typeof props.children === "string") {
189
+ typeName = props.children;
190
+ }
191
+ }
192
+
193
+ const memberSymbol = createInterfaceMemberSymbol(typeName, {
194
+ refkeys: props.refkey,
195
+ });
196
+
197
+ const taken = takeSymbols();
198
+ effect(() => {
199
+ if (taken.size !== 1) return;
200
+ const symbol = Array.from(taken)[0] as GoSymbol;
201
+ memberSymbol.name = symbol.name;
202
+ watch(
203
+ () => symbol.name,
204
+ () => {
205
+ memberSymbol.name = symbol.name;
206
+ },
207
+ );
208
+ const namedTypeScope = useNamedTypeScope();
209
+ symbol.copyMembersTo(namedTypeScope.ownerSymbol);
210
+ });
211
+
212
+ return (
213
+ <Declaration symbol={memberSymbol}>
214
+ <Show when={Boolean(props.doc)}>
215
+ <LineComment children={props.doc} />
216
+ <hbr />
217
+ </Show>
218
+ {props.children}
219
+ </Declaration>
220
+ );
221
+ }
222
+
223
+ export interface TypeConstraintProps {
224
+ constraints?: Children[];
225
+ children?: Children;
226
+ }
227
+
228
+ export function TypeConstraint(props: TypeConstraintProps) {
229
+ const constraints: Children[] = [];
230
+ if (!props.constraints || props.constraints.length === 0) {
231
+ if (props.children) {
232
+ constraints.push(props.children);
233
+ } else {
234
+ throw new Error("TypeConstraint requires constraints or children.");
235
+ }
236
+ } else {
237
+ if (props.children) {
238
+ throw new Error(
239
+ "TypeConstraint cannot have both constraints and children.",
240
+ );
241
+ }
242
+ constraints.push(...props.constraints);
243
+ }
244
+
245
+ return (
246
+ <For each={constraints} joiner=" | ">
247
+ {(constraint) => <>{constraint}</>}
248
+ </For>
249
+ );
250
+ }
@@ -0,0 +1,390 @@
1
+ import { List, Output, refkey, render } from "@alloy-js/core";
2
+ import { describe, expect, it } from "vitest";
3
+ import { assertFileContents, TestPackage } from "../../../test/utils.js";
4
+ import { ModuleDirectory } from "../ModuleDirectory.jsx";
5
+ import { SourceDirectory } from "../SourceDirectory.jsx";
6
+ import { SourceFile } from "../SourceFile.jsx";
7
+ import { TypeDeclaration } from "../type/declaration.js";
8
+ import {
9
+ InterfaceDeclaration,
10
+ InterfaceEmbed,
11
+ InterfaceFunction,
12
+ TypeConstraint,
13
+ } from "./declaration.jsx";
14
+
15
+ function Wrapper(props: { children: any }) {
16
+ return (
17
+ <TestPackage>
18
+ <TypeDeclaration name="TestInterface">
19
+ <InterfaceDeclaration>{props.children}</InterfaceDeclaration>
20
+ </TypeDeclaration>
21
+ </TestPackage>
22
+ );
23
+ }
24
+
25
+ it("declares interface with no members", () => {
26
+ expect(
27
+ <TestPackage>
28
+ <TypeDeclaration name="test">
29
+ <InterfaceDeclaration />
30
+ </TypeDeclaration>
31
+ </TestPackage>,
32
+ ).toRenderTo(`
33
+ package alloy
34
+
35
+ type test interface{}
36
+ `);
37
+ });
38
+
39
+ it("specify doc comment", () => {
40
+ expect(
41
+ <TestPackage>
42
+ <TypeDeclaration name="test" doc="This is a test">
43
+ <InterfaceDeclaration />
44
+ </TypeDeclaration>
45
+ </TestPackage>,
46
+ ).toRenderTo(`
47
+ package alloy
48
+
49
+ // This is a test
50
+ type test interface{}
51
+ `);
52
+ });
53
+
54
+ // describe("with type parameters", () => {
55
+ // it("reference parameters", () => {
56
+ // const typeParameters: TypeParameterProps[] = [
57
+ // {
58
+ // name: "T",
59
+ // refkey: refkey(),
60
+ // },
61
+ // {
62
+ // name: "U",
63
+ // refkey: refkey(),
64
+ // },
65
+ // ];
66
+ //
67
+ // expect(
68
+ // <TestPackage>
69
+ // <TypeDeclaration name="Test">
70
+ // <InterfaceDeclaration public name="Test" typeParameters={typeParameters}>
71
+ // <List>
72
+ // <Property name="PropA" type={typeParameters[0].refkey} get set />
73
+ // <Property name="PropB" type={typeParameters[1].refkey} get set />
74
+ // </List>
75
+ // </InterfaceDeclaration>
76
+ // </TypeDeclaration>
77
+ // </TestPackage>,
78
+ // ).toRenderTo(`
79
+ // public struct Test<T, U>
80
+ // {
81
+ // T PropA { get; set; }
82
+ // U PropB { get; set; }
83
+ // }
84
+ // `);
85
+ // });
86
+ //
87
+ // it("defines with constraints", () => {
88
+ // const typeParameters: TypeParameterProps[] = [
89
+ // {
90
+ // name: "T",
91
+ // constraints: "IFoo",
92
+ // },
93
+ // {
94
+ // name: "U",
95
+ // constraints: "IBar",
96
+ // },
97
+ // ];
98
+ //
99
+ // expect(
100
+ // <TestPackage>
101
+ // <TypeDeclaration name="Test">
102
+ // <InterfaceDeclaration public name="Test" typeParameters={typeParameters}>
103
+ // // Body
104
+ // </InterfaceDeclaration>
105
+ // </TypeDeclaration>
106
+ // </TestPackage>,
107
+ // ).toRenderTo(`
108
+ // public struct Test<T, U>
109
+ // where T : IFoo
110
+ // where U : IBar
111
+ // {
112
+ // // Body
113
+ // }
114
+ // `);
115
+ // });
116
+ // });
117
+
118
+ it("defines fields", () => {
119
+ expect(
120
+ <TestPackage>
121
+ <TypeDeclaration name="TestInterface">
122
+ <InterfaceDeclaration>
123
+ <List>
124
+ <InterfaceFunction
125
+ name="FunctionOne"
126
+ parameters={[{ name: "hi", type: "string" }]}
127
+ returns={"string"}
128
+ />
129
+ <InterfaceFunction name="functionTwo" returns="int" />
130
+ </List>
131
+ </InterfaceDeclaration>
132
+ </TypeDeclaration>
133
+ </TestPackage>,
134
+ ).toRenderTo(`
135
+ package alloy
136
+
137
+ type TestInterface interface {
138
+ func FunctionOne(hi string) string
139
+ func functionTwo() int
140
+ }
141
+ `);
142
+ });
143
+
144
+ describe("naming", () => {
145
+ it("public field is PascalCase", () => {
146
+ expect(
147
+ <Wrapper>
148
+ <List>
149
+ <InterfaceFunction name="MemberOne" returns="string" />
150
+ </List>
151
+ </Wrapper>,
152
+ ).toRenderTo(`
153
+ package alloy
154
+
155
+ type TestInterface interface {
156
+ func MemberOne() string
157
+ }
158
+ `);
159
+ });
160
+
161
+ it("private field are camelCase", () => {
162
+ expect(
163
+ <Wrapper>
164
+ <List>
165
+ <InterfaceFunction name="memberOne" returns="string" />
166
+ </List>
167
+ </Wrapper>,
168
+ ).toRenderTo(`
169
+ package alloy
170
+
171
+ type TestInterface interface {
172
+ func memberOne() string
173
+ }
174
+ `);
175
+ });
176
+ });
177
+
178
+ describe("embedded", () => {
179
+ it("embed using children", () => {
180
+ const TestInterfaceEmbed = refkey("TestInterfaceEmbed");
181
+ expect(
182
+ <TestPackage>
183
+ <TypeDeclaration name="TestInterface">
184
+ <InterfaceDeclaration>
185
+ <List>
186
+ <InterfaceFunction name="MemberOne" returns="string" />
187
+ </List>
188
+ </InterfaceDeclaration>
189
+ </TypeDeclaration>
190
+ <hbr />
191
+ <TypeDeclaration name="TestInterface2">
192
+ <InterfaceDeclaration>
193
+ <List>
194
+ <InterfaceEmbed refkey={TestInterfaceEmbed}>
195
+ TestInterface
196
+ </InterfaceEmbed>
197
+ <InterfaceFunction name="MemberOne" returns="string" />
198
+ </List>
199
+ </InterfaceDeclaration>
200
+ </TypeDeclaration>
201
+ <hbr />
202
+ {TestInterfaceEmbed}
203
+ </TestPackage>,
204
+ ).toRenderTo(`
205
+ package alloy
206
+
207
+ type TestInterface interface {
208
+ func MemberOne() string
209
+ }
210
+ type TestInterface2 interface {
211
+ TestInterface
212
+ func MemberOne() string
213
+ }
214
+ TestInterface2.TestInterface
215
+ `);
216
+ });
217
+
218
+ it("embed using refkey", () => {
219
+ const TestInterface = refkey("TestInterface");
220
+ const TestInterfaceEmbed = refkey("TestInterfaceEmbed");
221
+
222
+ expect(
223
+ <TestPackage>
224
+ <TypeDeclaration name="TestInterface" refkey={TestInterface}>
225
+ <InterfaceDeclaration>
226
+ <List>
227
+ <InterfaceFunction name="MemberOne" returns="string" />
228
+ </List>
229
+ </InterfaceDeclaration>
230
+ </TypeDeclaration>
231
+ <hbr />
232
+ <TypeDeclaration name="TestInterface2">
233
+ <InterfaceDeclaration>
234
+ <List>
235
+ <InterfaceEmbed refkey={TestInterfaceEmbed}>
236
+ {TestInterface}
237
+ </InterfaceEmbed>
238
+ <InterfaceFunction name="MemberOne" returns="string" />
239
+ </List>
240
+ </InterfaceDeclaration>
241
+ </TypeDeclaration>
242
+ <hbr />
243
+ {TestInterfaceEmbed}
244
+ </TestPackage>,
245
+ ).toRenderTo(`
246
+ package alloy
247
+
248
+ type TestInterface interface {
249
+ func MemberOne() string
250
+ }
251
+ type TestInterface2 interface {
252
+ TestInterface
253
+ func MemberOne() string
254
+ }
255
+ TestInterface2.TestInterface
256
+ `);
257
+ });
258
+
259
+ it("embed using cross-package refkey", () => {
260
+ const TestInterface = refkey("TestInterface");
261
+ const TestInterfaceEmbed = refkey("TestInterfaceEmbed");
262
+
263
+ const res = render(
264
+ <Output>
265
+ <ModuleDirectory name="github.com/alloy-framework/alloy">
266
+ <SourceDirectory path=".">
267
+ <SourceDirectory path="hello">
268
+ <SourceFile path="types.go">
269
+ <TypeDeclaration name="TestInterface" refkey={TestInterface}>
270
+ <InterfaceDeclaration>
271
+ <List>
272
+ <InterfaceFunction name="MemberOne" returns="string" />
273
+ </List>
274
+ </InterfaceDeclaration>
275
+ </TypeDeclaration>
276
+ </SourceFile>
277
+ </SourceDirectory>
278
+ <SourceFile path="Test.go">
279
+ <TypeDeclaration name="TestInterface2">
280
+ <InterfaceDeclaration>
281
+ <List>
282
+ <InterfaceEmbed refkey={TestInterfaceEmbed}>
283
+ {TestInterface}
284
+ </InterfaceEmbed>
285
+ <InterfaceFunction name="MemberOne" returns="string" />
286
+ </List>
287
+ </InterfaceDeclaration>
288
+ </TypeDeclaration>
289
+ <hbr />
290
+ {TestInterfaceEmbed}
291
+ </SourceFile>
292
+ </SourceDirectory>
293
+ </ModuleDirectory>
294
+ </Output>,
295
+ );
296
+
297
+ assertFileContents(res, {
298
+ "hello/types.go": `
299
+ package hello
300
+
301
+ type TestInterface interface {
302
+ func MemberOne() string
303
+ }
304
+ `,
305
+ "Test.go": `
306
+ package alloy
307
+
308
+ import "github.com/alloy-framework/alloy/hello"
309
+
310
+ type TestInterface2 interface {
311
+ hello.TestInterface
312
+ func MemberOne() string
313
+ }
314
+ TestInterface2.TestInterface
315
+ `,
316
+ });
317
+ });
318
+ });
319
+
320
+ describe("constraints", () => {
321
+ it("test single constraint", () => {
322
+ const TestInterface = refkey("TestInterface");
323
+ expect(
324
+ <TestPackage>
325
+ <TypeDeclaration name="TestInterface" refkey={TestInterface}>
326
+ <InterfaceDeclaration>
327
+ <List>
328
+ <InterfaceFunction name="MemberOne" returns="string" />
329
+ </List>
330
+ </InterfaceDeclaration>
331
+ </TypeDeclaration>
332
+ <hbr />
333
+ <TypeDeclaration name="TestInterface2">
334
+ <InterfaceDeclaration>
335
+ <List>
336
+ <TypeConstraint>{TestInterface}</TypeConstraint>
337
+ <InterfaceFunction name="MemberOne" returns="string" />
338
+ </List>
339
+ </InterfaceDeclaration>
340
+ </TypeDeclaration>
341
+ </TestPackage>,
342
+ ).toRenderTo(`
343
+ package alloy
344
+
345
+ type TestInterface interface {
346
+ func MemberOne() string
347
+ }
348
+ type TestInterface2 interface {
349
+ TestInterface
350
+ func MemberOne() string
351
+ }
352
+ `);
353
+ });
354
+
355
+ it("test multiple constraint", () => {
356
+ const TestInterface = refkey("TestInterface");
357
+ expect(
358
+ <TestPackage>
359
+ <TypeDeclaration name="TestInterface" refkey={TestInterface}>
360
+ <InterfaceDeclaration>
361
+ <List>
362
+ <InterfaceFunction name="MemberOne" returns="string" />
363
+ </List>
364
+ </InterfaceDeclaration>
365
+ </TypeDeclaration>
366
+ <hbr />
367
+ <TypeDeclaration name="TestInterface2">
368
+ <InterfaceDeclaration>
369
+ <List>
370
+ <TypeConstraint>{TestInterface}</TypeConstraint>
371
+ <TypeConstraint constraints={[TestInterface, "string", "~int"]} />
372
+ <InterfaceFunction name="MemberOne" returns="string" />
373
+ </List>
374
+ </InterfaceDeclaration>
375
+ </TypeDeclaration>
376
+ </TestPackage>,
377
+ ).toRenderTo(`
378
+ package alloy
379
+
380
+ type TestInterface interface {
381
+ func MemberOne() string
382
+ }
383
+ type TestInterface2 interface {
384
+ TestInterface
385
+ TestInterface | string | ~int
386
+ func MemberOne() string
387
+ }
388
+ `);
389
+ });
390
+ });
@@ -0,0 +1,63 @@
1
+ import { Output } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import { ModuleDirectory } from "./ModuleDirectory.jsx";
5
+ import { SourceDirectory } from "./SourceDirectory.jsx";
6
+ import { SourceFile } from "./SourceFile.jsx";
7
+ import { VariableDeclaration } from "./var/declaration.jsx";
8
+
9
+ it("defines multiple packages and source files with unique content", () => {
10
+ const tree = (
11
+ <Output>
12
+ <ModuleDirectory name="github.com/alloy-framework/alloy">
13
+ <SourceDirectory path="package1">
14
+ <SourceFile path="Model1.go">
15
+ <VariableDeclaration name="Model1" type="string">
16
+ "This is Model1"
17
+ </VariableDeclaration>
18
+ </SourceFile>
19
+ <SourceFile path="Model2.go">
20
+ <VariableDeclaration name="Model2" type="string">
21
+ "This is Model2"
22
+ </VariableDeclaration>
23
+ </SourceFile>
24
+ </SourceDirectory>
25
+ <SourceDirectory path="package2">
26
+ <SourceFile path="Model3.go">
27
+ <VariableDeclaration name="Model3" type="string">
28
+ "This is Model3"
29
+ </VariableDeclaration>
30
+ </SourceFile>
31
+ <SourceFile path="Model4.go">
32
+ <VariableDeclaration name="Model4" type="string">
33
+ "This is Model4"
34
+ </VariableDeclaration>
35
+ </SourceFile>
36
+ </SourceDirectory>
37
+ </ModuleDirectory>
38
+ </Output>
39
+ );
40
+
41
+ expect(tree).toRenderTo({
42
+ "package1/Model1.go": d`
43
+ package package1
44
+
45
+ var Model1 string = "This is Model1"
46
+ `,
47
+ "package1/Model2.go": d`
48
+ package package1
49
+
50
+ var Model2 string = "This is Model2"
51
+ `,
52
+ "package2/Model3.go": d`
53
+ package package2
54
+
55
+ var Model3 string = "This is Model3"
56
+ `,
57
+ "package2/Model4.go": d`
58
+ package package2
59
+
60
+ var Model4 string = "This is Model4"
61
+ `,
62
+ });
63
+ });