@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,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Children,
|
|
3
|
+
createSymbolSlot,
|
|
4
|
+
Declaration,
|
|
5
|
+
For,
|
|
6
|
+
Indent,
|
|
7
|
+
Namekey,
|
|
8
|
+
Refkey,
|
|
9
|
+
} from "@alloy-js/core";
|
|
10
|
+
import { createParameterSymbol } from "../../symbols/factories.js";
|
|
11
|
+
import { Name } from "../Name.jsx";
|
|
12
|
+
|
|
13
|
+
export interface FunctionParameterProps {
|
|
14
|
+
name: string | Namekey;
|
|
15
|
+
type: Children;
|
|
16
|
+
variadic?: boolean;
|
|
17
|
+
refkey?: Refkey;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Define a parameter to be used in functions. */
|
|
21
|
+
export function FunctionParameter(props: FunctionParameterProps) {
|
|
22
|
+
const TypeSlot = createSymbolSlot();
|
|
23
|
+
|
|
24
|
+
const memberSymbol = createParameterSymbol(props.name, {
|
|
25
|
+
refkeys: props.refkey,
|
|
26
|
+
type: TypeSlot.firstSymbol,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<Declaration symbol={memberSymbol}>
|
|
31
|
+
<Name /> {props.variadic ? "..." : null}
|
|
32
|
+
<TypeSlot>{props.type}</TypeSlot>
|
|
33
|
+
</Declaration>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface FunctionParametersProps {
|
|
38
|
+
parameters: FunctionParameterProps[] | undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Render a collection of parameters */
|
|
42
|
+
export function FunctionParameters(props: FunctionParametersProps) {
|
|
43
|
+
const params = props.parameters ?? [];
|
|
44
|
+
for (let i = 0; i < (params.length ?? 0); i++) {
|
|
45
|
+
if (i < params.length - 1 && params[i].variadic) {
|
|
46
|
+
throw new Error("Variadic parameter must be the last parameter.");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return (
|
|
50
|
+
<group>
|
|
51
|
+
{"("}
|
|
52
|
+
{props.parameters && (
|
|
53
|
+
<Indent softline>
|
|
54
|
+
<For
|
|
55
|
+
each={params}
|
|
56
|
+
joiner={
|
|
57
|
+
<>
|
|
58
|
+
{","}
|
|
59
|
+
<ifBreak flatContents=" ">
|
|
60
|
+
<sbr />
|
|
61
|
+
</ifBreak>
|
|
62
|
+
</>
|
|
63
|
+
}
|
|
64
|
+
ender={<ifBreak>,</ifBreak>}
|
|
65
|
+
>
|
|
66
|
+
{(param) => <FunctionParameter {...param} />}
|
|
67
|
+
</For>
|
|
68
|
+
</Indent>
|
|
69
|
+
)}
|
|
70
|
+
<sbr />
|
|
71
|
+
{")"}
|
|
72
|
+
</group>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Children,
|
|
3
|
+
For,
|
|
4
|
+
Indent,
|
|
5
|
+
MemberDeclaration,
|
|
6
|
+
MemberName,
|
|
7
|
+
Namekey,
|
|
8
|
+
Refkey,
|
|
9
|
+
} from "@alloy-js/core";
|
|
10
|
+
import { createTypeParameterSymbol } from "../../symbols/factories.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Information for a Go generic type parameter.
|
|
14
|
+
*/
|
|
15
|
+
export interface TypeParameterProps {
|
|
16
|
+
/**
|
|
17
|
+
* The name of the type parameter.
|
|
18
|
+
*/
|
|
19
|
+
name: string | Namekey;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The parameter constraint
|
|
23
|
+
*/
|
|
24
|
+
constraint: Children;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A refkey for this type parameter.
|
|
28
|
+
*/
|
|
29
|
+
refkey?: Refkey;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function TypeParameter(props: TypeParameterProps) {
|
|
33
|
+
const symbol = createTypeParameterSymbol(props.name, {
|
|
34
|
+
refkeys: props.refkey,
|
|
35
|
+
});
|
|
36
|
+
return (
|
|
37
|
+
<MemberDeclaration symbol={symbol}>
|
|
38
|
+
<MemberName /> {props.constraint}
|
|
39
|
+
</MemberDeclaration>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface TypeParametersProps {
|
|
44
|
+
/** Parameters */
|
|
45
|
+
parameters?: TypeParameterProps[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function TypeParameters(props: TypeParametersProps) {
|
|
49
|
+
if (!props.parameters || props.parameters.length === 0) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<group>
|
|
55
|
+
{"["}
|
|
56
|
+
{props.parameters && (
|
|
57
|
+
<Indent softline>
|
|
58
|
+
<For
|
|
59
|
+
each={props.parameters}
|
|
60
|
+
joiner={
|
|
61
|
+
<>
|
|
62
|
+
{","}
|
|
63
|
+
<ifBreak flatContents=" ">
|
|
64
|
+
<sbr />
|
|
65
|
+
</ifBreak>
|
|
66
|
+
</>
|
|
67
|
+
}
|
|
68
|
+
ender={<ifBreak>,</ifBreak>}
|
|
69
|
+
>
|
|
70
|
+
{(param) => <TypeParameter {...param} />}
|
|
71
|
+
</For>
|
|
72
|
+
</Indent>
|
|
73
|
+
)}
|
|
74
|
+
<sbr />
|
|
75
|
+
{"]"}
|
|
76
|
+
</group>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as core from "@alloy-js/core";
|
|
2
|
+
import * as base from "../index.js";
|
|
3
|
+
|
|
4
|
+
export const LineComment = core.stc(base.LineComment);
|
|
5
|
+
export const BlockComment = core.stc(base.BlockComment);
|
|
6
|
+
export const FunctionDeclaration = core.stc(base.FunctionDeclaration);
|
|
7
|
+
export const FunctionReceiver = core.stc(base.FunctionReceiver);
|
|
8
|
+
export const InterfaceDeclaration = core.stc(base.InterfaceDeclaration);
|
|
9
|
+
export const InterfaceFunction = core.stc(base.InterfaceFunction);
|
|
10
|
+
export const InterfaceEmbed = core.stc(base.InterfaceEmbed);
|
|
11
|
+
export const FunctionParameter = core.stc(base.FunctionParameter);
|
|
12
|
+
export const FunctionParameters = core.stc(base.FunctionParameters);
|
|
13
|
+
export const Pointer = core.stc(base.Pointer);
|
|
14
|
+
export const StructDeclaration = core.stc(base.StructDeclaration);
|
|
15
|
+
export const StructMember = core.stc(base.StructMember);
|
|
16
|
+
export const StructEmbed = core.stc(base.StructEmbed);
|
|
17
|
+
export const TypeDeclarationGroup = core.stc(base.TypeDeclarationGroup);
|
|
18
|
+
export const TypeDeclaration = core.stc(base.TypeDeclaration);
|
|
19
|
+
export const VariableDeclarationGroup = core.stc(base.VariableDeclarationGroup);
|
|
20
|
+
export const VariableDeclaration = core.stc(base.VariableDeclaration);
|
|
21
|
+
export const ImportStatements = core.stc(base.ImportStatements);
|
|
22
|
+
export const SingleImportStatement = core.stc(base.SingleImportStatement);
|
|
23
|
+
export const ModuleDirectory = core.stc(base.ModuleDirectory);
|
|
24
|
+
export const Name = core.stc(base.Name);
|
|
25
|
+
export const Reference = core.stc(base.Reference);
|
|
26
|
+
export const SourceDirectory = core.stc(base.SourceDirectory);
|
|
27
|
+
export const SourceFile = core.stc(base.SourceFile);
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Block,
|
|
3
|
+
Children,
|
|
4
|
+
computed,
|
|
5
|
+
Declaration,
|
|
6
|
+
DeclarationContext,
|
|
7
|
+
effect,
|
|
8
|
+
memo,
|
|
9
|
+
Namekey,
|
|
10
|
+
Refkey,
|
|
11
|
+
Scope,
|
|
12
|
+
Show,
|
|
13
|
+
takeSymbols,
|
|
14
|
+
untrack,
|
|
15
|
+
useContext,
|
|
16
|
+
watch,
|
|
17
|
+
} from "@alloy-js/core";
|
|
18
|
+
import { useGoScope, useNamedTypeScope } from "../../scopes/contexts.js";
|
|
19
|
+
import { createNamedTypeScope } from "../../scopes/factories.js";
|
|
20
|
+
import { GoNamedTypeScope } from "../../scopes/named-type.js";
|
|
21
|
+
import {
|
|
22
|
+
createAnonymousTypeSymbol,
|
|
23
|
+
createStructMemberSymbol,
|
|
24
|
+
} from "../../symbols/factories.js";
|
|
25
|
+
import { GoSymbol } from "../../symbols/go.js";
|
|
26
|
+
import { NamedTypeSymbol } from "../../symbols/named-type.js";
|
|
27
|
+
import { LineComment } from "../doc/comment.js";
|
|
28
|
+
import { Name } from "../Name.js";
|
|
29
|
+
import { TypeDeclaration, TypeDeclarationProps } from "../type/declaration.jsx";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Properties for creating a struct declaration.
|
|
33
|
+
* This can be used for both named and anonymous structs.
|
|
34
|
+
* For named structs, use `StructTypeDeclarationProps` instead.
|
|
35
|
+
*/
|
|
36
|
+
export interface StructDeclarationProps {
|
|
37
|
+
refkey?: Refkey;
|
|
38
|
+
children?: Children;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Whether to render the struct in a single line.
|
|
42
|
+
* This will only compile if `children` is a single line as well.
|
|
43
|
+
* This is not common in Go, so use with caution.
|
|
44
|
+
*/
|
|
45
|
+
singleLine?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Properties for creating a named struct type declaration.
|
|
50
|
+
* This is a combination of `StructDeclarationProps` and `TypeDeclarationProps`.
|
|
51
|
+
*/
|
|
52
|
+
export interface StructTypeDeclarationProps
|
|
53
|
+
extends StructDeclarationProps,
|
|
54
|
+
TypeDeclarationProps {}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Wrapper for creating a named struct type declaration.
|
|
58
|
+
*/
|
|
59
|
+
export function StructTypeDeclaration(props: StructTypeDeclarationProps) {
|
|
60
|
+
return (
|
|
61
|
+
<TypeDeclaration {...props}>
|
|
62
|
+
<StructDeclaration {...props} />
|
|
63
|
+
</TypeDeclaration>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Go struct declaration.
|
|
69
|
+
* @example
|
|
70
|
+
* ```tsx
|
|
71
|
+
* <StructDeclaration>
|
|
72
|
+
* <StructEmbed pointer>{MyOtherStruct}</StructEmbed>
|
|
73
|
+
* <StructMember exported name="MyProperty" type="int" />
|
|
74
|
+
* </StructDeclaration>
|
|
75
|
+
* ```
|
|
76
|
+
* This will produce:
|
|
77
|
+
* ```go
|
|
78
|
+
* struct {
|
|
79
|
+
* MyOtherStruct
|
|
80
|
+
* MyProperty int
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function StructDeclaration(props: StructDeclarationProps) {
|
|
85
|
+
const scope = useGoScope();
|
|
86
|
+
let structScope: GoNamedTypeScope | undefined;
|
|
87
|
+
if (
|
|
88
|
+
!(scope instanceof GoNamedTypeScope) ||
|
|
89
|
+
scope.ownerSymbol.typeKind !== "type"
|
|
90
|
+
) {
|
|
91
|
+
const declSymbol = useContext(DeclarationContext) as
|
|
92
|
+
| NamedTypeSymbol
|
|
93
|
+
| undefined;
|
|
94
|
+
// this is an anonymous struct
|
|
95
|
+
if (!declSymbol) {
|
|
96
|
+
// this can only happen when instantiating a struct directly
|
|
97
|
+
const anonymousSymbol = createAnonymousTypeSymbol("struct");
|
|
98
|
+
const anonymousScope = createNamedTypeScope(anonymousSymbol);
|
|
99
|
+
structScope = anonymousScope;
|
|
100
|
+
} else {
|
|
101
|
+
// this is most likely a struct field
|
|
102
|
+
const anonymousScope = createNamedTypeScope(declSymbol);
|
|
103
|
+
structScope = anonymousScope;
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
scope.ownerSymbol.typeKind = "struct";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const content = memo(() => {
|
|
110
|
+
if (props.children) {
|
|
111
|
+
if (props.singleLine) {
|
|
112
|
+
return (
|
|
113
|
+
<>
|
|
114
|
+
{"{"} {props.children} {"}"}
|
|
115
|
+
</>
|
|
116
|
+
);
|
|
117
|
+
} else {
|
|
118
|
+
return (
|
|
119
|
+
<>
|
|
120
|
+
{" "}
|
|
121
|
+
<Block>{props.children}</Block>
|
|
122
|
+
</>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
return "{}";
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
return (
|
|
131
|
+
<>
|
|
132
|
+
struct
|
|
133
|
+
{structScope ?
|
|
134
|
+
<Scope value={structScope}>{content}</Scope>
|
|
135
|
+
: content}
|
|
136
|
+
</>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface StructMemberProps {
|
|
141
|
+
name: string | Namekey;
|
|
142
|
+
type: Children;
|
|
143
|
+
refkey?: Refkey;
|
|
144
|
+
tag?: string | Record<string, string>;
|
|
145
|
+
/** Doc comment */
|
|
146
|
+
doc?: Children;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function StructMember(props: StructMemberProps) {
|
|
150
|
+
const symbol = createStructMemberSymbol(props.name, {
|
|
151
|
+
refkeys: props.refkey,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const tagString = computed(() => {
|
|
155
|
+
if (typeof props.tag === "string") {
|
|
156
|
+
return props.tag;
|
|
157
|
+
} else if (typeof props.tag === "object") {
|
|
158
|
+
return Object.entries(props.tag)
|
|
159
|
+
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
160
|
+
.map(([key, value]) => `${key}:"${value}"`)
|
|
161
|
+
.join(" ");
|
|
162
|
+
}
|
|
163
|
+
return "";
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<Declaration symbol={symbol}>
|
|
168
|
+
<Show when={Boolean(props.doc)}>
|
|
169
|
+
<LineComment children={props.doc} />
|
|
170
|
+
<hbr />
|
|
171
|
+
</Show>
|
|
172
|
+
<Name /> {props.type}
|
|
173
|
+
{tagString.value === "" ?
|
|
174
|
+
""
|
|
175
|
+
: tagString.value.includes('"') ?
|
|
176
|
+
` \`${tagString.value}\``
|
|
177
|
+
: ` "${tagString.value}"`}
|
|
178
|
+
</Declaration>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface StructEmbedProps {
|
|
183
|
+
refkey?: Refkey;
|
|
184
|
+
children?: Children;
|
|
185
|
+
/** Doc comment */
|
|
186
|
+
doc?: Children;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function StructEmbed(props: StructEmbedProps) {
|
|
190
|
+
let typeName: string = "<Unresolved symbol>";
|
|
191
|
+
if (props.children) {
|
|
192
|
+
if (typeof props.children === "string") {
|
|
193
|
+
typeName = props.children;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const memberSymbol = createStructMemberSymbol(typeName, {
|
|
198
|
+
refkeys: props.refkey,
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
const taken = takeSymbols();
|
|
202
|
+
effect(() => {
|
|
203
|
+
if (taken.size !== 1) return;
|
|
204
|
+
untrack(() => {
|
|
205
|
+
const symbol = Array.from(taken)[0] as GoSymbol;
|
|
206
|
+
memberSymbol.name = symbol.name;
|
|
207
|
+
watch(
|
|
208
|
+
() => symbol.name,
|
|
209
|
+
() => {
|
|
210
|
+
memberSymbol.name = symbol.name;
|
|
211
|
+
},
|
|
212
|
+
);
|
|
213
|
+
const namedTypeScope = useNamedTypeScope();
|
|
214
|
+
// TODO: somehow mark it as "promoted" such that it cannot be constructed directly
|
|
215
|
+
symbol.copyMembersTo(namedTypeScope.ownerSymbol);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
return (
|
|
220
|
+
<Declaration symbol={memberSymbol}>
|
|
221
|
+
<Show when={Boolean(props.doc)}>
|
|
222
|
+
<LineComment children={props.doc} />
|
|
223
|
+
<hbr />
|
|
224
|
+
</Show>
|
|
225
|
+
{props.children}
|
|
226
|
+
</Declaration>
|
|
227
|
+
);
|
|
228
|
+
}
|