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