@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.
- package/CHANGELOG.md +1 -0
- package/LICENSE +7 -0
- package/api-extractor.json +4 -0
- package/dist/src/builtins/fmt/fmt.d.ts +147 -0
- package/dist/src/builtins/fmt/fmt.d.ts.map +1 -0
- package/dist/src/builtins/fmt/fmt.js +148 -0
- package/dist/src/builtins/fmt/fmt.js.map +1 -0
- package/dist/src/builtins/index.d.ts +2004 -0
- package/dist/src/builtins/index.d.ts.map +1 -0
- package/dist/src/builtins/index.js +9 -0
- package/dist/src/builtins/index.js.map +1 -0
- package/dist/src/builtins/io/io.d.ts +648 -0
- package/dist/src/builtins/io/io.d.ts.map +1 -0
- package/dist/src/builtins/io/io.js +650 -0
- package/dist/src/builtins/io/io.js.map +1 -0
- package/dist/src/builtins/net/net.d.ts +1210 -0
- package/dist/src/builtins/net/net.d.ts.map +1 -0
- package/dist/src/builtins/net/net.js +1212 -0
- package/dist/src/builtins/net/net.js.map +1 -0
- package/dist/src/components/ImportStatement.d.ts +15 -0
- package/dist/src/components/ImportStatement.d.ts.map +1 -0
- package/dist/src/components/ImportStatement.js +90 -0
- package/dist/src/components/ImportStatement.js.map +1 -0
- package/dist/src/components/ModuleDirectory.d.ts +8 -0
- package/dist/src/components/ModuleDirectory.d.ts.map +1 -0
- package/dist/src/components/ModuleDirectory.js +20 -0
- package/dist/src/components/ModuleDirectory.js.map +1 -0
- package/dist/src/components/Name.d.ts +2 -0
- package/dist/src/components/Name.d.ts.map +1 -0
- package/dist/src/components/Name.js +12 -0
- package/dist/src/components/Name.js.map +1 -0
- package/dist/src/components/Reference.d.ts +6 -0
- package/dist/src/components/Reference.d.ts.map +1 -0
- package/dist/src/components/Reference.js +15 -0
- package/dist/src/components/Reference.js.map +1 -0
- package/dist/src/components/SourceDirectory.d.ts +6 -0
- package/dist/src/components/SourceDirectory.d.ts.map +1 -0
- package/dist/src/components/SourceDirectory.js +30 -0
- package/dist/src/components/SourceDirectory.js.map +1 -0
- package/dist/src/components/SourceFile.d.ts +16 -0
- package/dist/src/components/SourceFile.d.ts.map +1 -0
- package/dist/src/components/SourceFile.js +75 -0
- package/dist/src/components/SourceFile.js.map +1 -0
- package/dist/src/components/doc/comment.d.ts +21 -0
- package/dist/src/components/doc/comment.d.ts.map +1 -0
- package/dist/src/components/doc/comment.js +30 -0
- package/dist/src/components/doc/comment.js.map +1 -0
- package/dist/src/components/doc/comment.test.d.ts +2 -0
- package/dist/src/components/doc/comment.test.d.ts.map +1 -0
- package/dist/src/components/doc/comment.test.js +215 -0
- package/dist/src/components/doc/comment.test.js.map +1 -0
- package/dist/src/components/function/function.d.ts +31 -0
- package/dist/src/components/function/function.d.ts.map +1 -0
- package/dist/src/components/function/function.js +134 -0
- package/dist/src/components/function/function.js.map +1 -0
- package/dist/src/components/function/function.test.d.ts +2 -0
- package/dist/src/components/function/function.test.d.ts.map +1 -0
- package/dist/src/components/function/function.test.js +410 -0
- package/dist/src/components/function/function.test.js.map +1 -0
- package/dist/src/components/index.d.ts +16 -0
- package/dist/src/components/index.d.ts.map +1 -0
- package/dist/src/components/index.js +16 -0
- package/dist/src/components/index.js.map +1 -0
- package/dist/src/components/interface/declaration.d.ts +68 -0
- package/dist/src/components/interface/declaration.d.ts.map +1 -0
- package/dist/src/components/interface/declaration.js +181 -0
- package/dist/src/components/interface/declaration.js.map +1 -0
- package/dist/src/components/interface/interface.test.d.ts +2 -0
- package/dist/src/components/interface/interface.test.d.ts.map +1 -0
- package/dist/src/components/interface/interface.test.js +517 -0
- package/dist/src/components/interface/interface.test.js.map +1 -0
- package/dist/src/components/package.test.d.ts +2 -0
- package/dist/src/components/package.test.d.ts.map +1 -0
- package/dist/src/components/package.test.js +89 -0
- package/dist/src/components/package.test.js.map +1 -0
- package/dist/src/components/parameters/parameters.d.ts +15 -0
- package/dist/src/components/parameters/parameters.d.ts.map +1 -0
- package/dist/src/components/parameters/parameters.js +58 -0
- package/dist/src/components/parameters/parameters.js.map +1 -0
- package/dist/src/components/parameters/typeparameters.d.ts +25 -0
- package/dist/src/components/parameters/typeparameters.d.ts.map +1 -0
- package/dist/src/components/parameters/typeparameters.js +53 -0
- package/dist/src/components/parameters/typeparameters.js.map +1 -0
- package/dist/src/components/pointer/pointer.d.ts +9 -0
- package/dist/src/components/pointer/pointer.d.ts.map +1 -0
- package/dist/src/components/pointer/pointer.js +8 -0
- package/dist/src/components/pointer/pointer.js.map +1 -0
- package/dist/src/components/stc/index.d.ts +27 -0
- package/dist/src/components/stc/index.d.ts.map +1 -0
- package/dist/src/components/stc/index.js +27 -0
- package/dist/src/components/stc/index.js.map +1 -0
- package/dist/src/components/struct/declaration.d.ts +62 -0
- package/dist/src/components/struct/declaration.d.ts.map +1 -0
- package/dist/src/components/struct/declaration.js +161 -0
- package/dist/src/components/struct/declaration.js.map +1 -0
- package/dist/src/components/struct/struct.test.d.ts +2 -0
- package/dist/src/components/struct/struct.test.d.ts.map +1 -0
- package/dist/src/components/struct/struct.test.js +741 -0
- package/dist/src/components/struct/struct.test.js.map +1 -0
- package/dist/src/components/type/declaration.d.ts +29 -0
- package/dist/src/components/type/declaration.d.ts.map +1 -0
- package/dist/src/components/type/declaration.js +63 -0
- package/dist/src/components/type/declaration.js.map +1 -0
- package/dist/src/components/var/declaration.d.ts +27 -0
- package/dist/src/components/var/declaration.d.ts.map +1 -0
- package/dist/src/components/var/declaration.js +84 -0
- package/dist/src/components/var/declaration.js.map +1 -0
- package/dist/src/components/var/var-declaration.test.d.ts +2 -0
- package/dist/src/components/var/var-declaration.test.d.ts.map +1 -0
- package/dist/src/components/var/var-declaration.test.js +340 -0
- package/dist/src/components/var/var-declaration.test.js.map +1 -0
- package/dist/src/context/package.d.ts +9 -0
- package/dist/src/context/package.d.ts.map +1 -0
- package/dist/src/context/package.js +6 -0
- package/dist/src/context/package.js.map +1 -0
- package/dist/src/create-module.d.ts +47 -0
- package/dist/src/create-module.d.ts.map +1 -0
- package/dist/src/create-module.js +158 -0
- package/dist/src/create-module.js.map +1 -0
- package/dist/src/create-module.test.d.ts +2 -0
- package/dist/src/create-module.test.d.ts.map +1 -0
- package/dist/src/create-module.test.js +147 -0
- package/dist/src/create-module.test.js.map +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +6 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/name-policy.d.ts +5 -0
- package/dist/src/name-policy.d.ts.map +1 -0
- package/dist/src/name-policy.js +27 -0
- package/dist/src/name-policy.js.map +1 -0
- package/dist/src/scopes/contexts.d.ts +9 -0
- package/dist/src/scopes/contexts.d.ts.map +1 -0
- package/dist/src/scopes/contexts.js +34 -0
- package/dist/src/scopes/contexts.js.map +1 -0
- package/dist/src/scopes/factories.d.ts +7 -0
- package/dist/src/scopes/factories.d.ts.map +1 -0
- package/dist/src/scopes/factories.js +16 -0
- package/dist/src/scopes/factories.js.map +1 -0
- package/dist/src/scopes/function.d.ts +9 -0
- package/dist/src/scopes/function.d.ts.map +1 -0
- package/dist/src/scopes/function.js +14 -0
- package/dist/src/scopes/function.js.map +1 -0
- package/dist/src/scopes/go.d.ts +8 -0
- package/dist/src/scopes/go.d.ts.map +1 -0
- package/dist/src/scopes/go.js +12 -0
- package/dist/src/scopes/go.js.map +1 -0
- package/dist/src/scopes/index.d.ts +10 -0
- package/dist/src/scopes/index.d.ts.map +1 -0
- package/dist/src/scopes/index.js +10 -0
- package/dist/src/scopes/index.js.map +1 -0
- package/dist/src/scopes/lexical.d.ts +8 -0
- package/dist/src/scopes/lexical.d.ts.map +1 -0
- package/dist/src/scopes/lexical.js +11 -0
- package/dist/src/scopes/lexical.js.map +1 -0
- package/dist/src/scopes/module.d.ts +9 -0
- package/dist/src/scopes/module.d.ts.map +1 -0
- package/dist/src/scopes/module.js +31 -0
- package/dist/src/scopes/module.js.map +1 -0
- package/dist/src/scopes/named-type.d.ts +23 -0
- package/dist/src/scopes/named-type.d.ts.map +1 -0
- package/dist/src/scopes/named-type.js +33 -0
- package/dist/src/scopes/named-type.js.map +1 -0
- package/dist/src/scopes/package.d.ts +11 -0
- package/dist/src/scopes/package.d.ts.map +1 -0
- package/dist/src/scopes/package.js +39 -0
- package/dist/src/scopes/package.js.map +1 -0
- package/dist/src/scopes/source-file.d.ts +17 -0
- package/dist/src/scopes/source-file.d.ts.map +1 -0
- package/dist/src/scopes/source-file.js +42 -0
- package/dist/src/scopes/source-file.js.map +1 -0
- package/dist/src/symbols/factories.d.ts +25 -0
- package/dist/src/symbols/factories.d.ts.map +1 -0
- package/dist/src/symbols/factories.js +114 -0
- package/dist/src/symbols/factories.js.map +1 -0
- package/dist/src/symbols/function.d.ts +10 -0
- package/dist/src/symbols/function.d.ts.map +1 -0
- package/dist/src/symbols/function.js +12 -0
- package/dist/src/symbols/function.js.map +1 -0
- package/dist/src/symbols/go.d.ts +22 -0
- package/dist/src/symbols/go.d.ts.map +1 -0
- package/dist/src/symbols/go.js +56 -0
- package/dist/src/symbols/go.js.map +1 -0
- package/dist/src/symbols/index.d.ts +7 -0
- package/dist/src/symbols/index.d.ts.map +1 -0
- package/dist/src/symbols/index.js +7 -0
- package/dist/src/symbols/index.js.map +1 -0
- package/dist/src/symbols/named-type.d.ts +24 -0
- package/dist/src/symbols/named-type.d.ts.map +1 -0
- package/dist/src/symbols/named-type.js +53 -0
- package/dist/src/symbols/named-type.js.map +1 -0
- package/dist/src/symbols/package.d.ts +18 -0
- package/dist/src/symbols/package.d.ts.map +1 -0
- package/dist/src/symbols/package.js +38 -0
- package/dist/src/symbols/package.js.map +1 -0
- package/dist/src/symbols/reference.d.ts +3 -0
- package/dist/src/symbols/reference.d.ts.map +1 -0
- package/dist/src/symbols/reference.js +76 -0
- package/dist/src/symbols/reference.js.map +1 -0
- package/dist/test/utils.d.ts +8 -0
- package/dist/test/utils.d.ts.map +1 -0
- package/dist/test/utils.js +68 -0
- package/dist/test/utils.js.map +1 -0
- package/dist/test/vitest.setup.d.ts +2 -0
- package/dist/test/vitest.setup.d.ts.map +1 -0
- package/dist/test/vitest.setup.js +2 -0
- package/dist/test/vitest.setup.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +45 -0
- package/src/builtins/fmt/fmt.ts +78 -0
- package/src/builtins/index.ts +9 -0
- package/src/builtins/io/io.ts +355 -0
- package/src/builtins/net/net.ts +502 -0
- package/src/components/ImportStatement.tsx +115 -0
- package/src/components/ModuleDirectory.tsx +18 -0
- package/src/components/Name.tsx +11 -0
- package/src/components/Reference.tsx +18 -0
- package/src/components/SourceDirectory.tsx +32 -0
- package/src/components/SourceFile.tsx +74 -0
- package/src/components/doc/comment.test.tsx +278 -0
- package/src/components/doc/comment.tsx +40 -0
- package/src/components/function/function.test.tsx +387 -0
- package/src/components/function/function.tsx +194 -0
- package/src/components/index.ts +15 -0
- package/src/components/interface/declaration.tsx +250 -0
- package/src/components/interface/interface.test.tsx +390 -0
- package/src/components/package.test.tsx +63 -0
- package/src/components/parameters/parameters.tsx +74 -0
- package/src/components/parameters/typeparameters.tsx +78 -0
- package/src/components/pointer/pointer.tsx +12 -0
- package/src/components/stc/index.ts +27 -0
- package/src/components/struct/declaration.tsx +228 -0
- package/src/components/struct/struct.test.tsx +558 -0
- package/src/components/type/declaration.tsx +93 -0
- package/src/components/var/declaration.tsx +138 -0
- package/src/components/var/var-declaration.test.tsx +236 -0
- package/src/context/package.ts +13 -0
- package/src/create-module.test.tsx +128 -0
- package/src/create-module.ts +347 -0
- package/src/index.ts +5 -0
- package/src/name-policy.ts +65 -0
- package/src/scopes/contexts.ts +45 -0
- package/src/scopes/factories.ts +29 -0
- package/src/scopes/function.ts +22 -0
- package/src/scopes/go.ts +18 -0
- package/src/scopes/index.ts +9 -0
- package/src/scopes/lexical.ts +14 -0
- package/src/scopes/module.ts +35 -0
- package/src/scopes/named-type.ts +49 -0
- package/src/scopes/package.ts +55 -0
- package/src/scopes/source-file.ts +66 -0
- package/src/symbols/factories.ts +234 -0
- package/src/symbols/function.ts +17 -0
- package/src/symbols/go.ts +82 -0
- package/src/symbols/index.ts +6 -0
- package/src/symbols/named-type.ts +90 -0
- package/src/symbols/package.ts +54 -0
- package/src/symbols/reference.ts +87 -0
- package/temp/api.json +8753 -0
- package/test/utils.tsx +77 -0
- package/test/vitest.setup.ts +1 -0
- package/tsconfig.json +15 -0
- package/tsdoc-metadata.json +11 -0
- 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";
|