@alloy-js/go 0.1.0-dev.2 → 0.1.0-dev.3

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.
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  Block,
3
3
  Children,
4
- computed,
4
+ createSymbolSlot,
5
5
  Declaration,
6
6
  DeclarationContext,
7
7
  effect,
@@ -9,13 +9,11 @@ import {
9
9
  Indent,
10
10
  Name,
11
11
  Namekey,
12
+ onCleanup,
12
13
  Refkey,
13
14
  Scope,
14
- shallowRef,
15
15
  Show,
16
- takeSymbols,
17
16
  useContext,
18
- watch,
19
17
  } from "@alloy-js/core";
20
18
  import { useGoScope } from "../../scopes/contexts.js";
21
19
  import { createFunctionScope } from "../../scopes/factories.js";
@@ -27,6 +25,7 @@ import {
27
25
  import { FunctionSymbol } from "../../symbols/function.js";
28
26
  import { GoSymbol } from "../../symbols/go.js";
29
27
  import { NamedTypeSymbol } from "../../symbols/named-type.js";
28
+ import { TypeParameterSymbol } from "../../symbols/type-parameter.js";
30
29
  import { LineComment } from "../doc/comment.jsx";
31
30
  import {
32
31
  FunctionParameterProps,
@@ -150,45 +149,55 @@ export function FunctionReceiver(props: FuncReceiverProps) {
150
149
  throw new Error("FuncReceiver must be used inside a function.");
151
150
  }
152
151
 
153
- const typeParams = shallowRef(props.typeParameters ?? []);
152
+ const FunctionTypeSlot = createSymbolSlot();
154
153
 
155
- const taken = takeSymbols();
154
+ // Add the function symbol to the receiver type's members
156
155
  effect(() => {
157
- if (taken.size !== 1) return;
158
- const symbol = Array.from(taken)[0] as GoSymbol;
159
- if (symbol.enclosingPackage !== receiverSymbol.enclosingPackage) {
156
+ const typeSymbol = FunctionTypeSlot.firstSymbol.value as
157
+ | GoSymbol
158
+ | undefined;
159
+ if (!typeSymbol) {
160
+ return;
161
+ }
162
+ if (typeSymbol.enclosingPackage !== receiverSymbol.enclosingPackage) {
160
163
  throw new Error(
161
- `Receiver symbol ${receiverSymbol.name} must be in the same package as the type ${symbol.name}.`,
164
+ `Receiver symbol ${receiverSymbol.name} must be in the same package as the type ${typeSymbol.name}.`,
162
165
  );
163
166
  }
164
- if (!(symbol instanceof NamedTypeSymbol)) {
167
+ if (!(typeSymbol instanceof NamedTypeSymbol)) {
165
168
  throw new Error(
166
- `Receiver type must be a named type, got ${symbol.constructor.name}.`,
169
+ `Receiver type must be a named type, got ${typeSymbol.constructor.name}.`,
167
170
  );
168
171
  }
169
172
 
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
- });
173
+ functionSymbol.receiverSymbol = typeSymbol;
182
174
 
183
- const typeParamsComponent = computed(() => {
184
- if (typeParams.value.length === 0) return null;
185
- return <TypeParameters parameters={typeParams.value} />;
175
+ typeSymbol.members.add(functionSymbol);
176
+
177
+ onCleanup(() => {
178
+ functionSymbol.receiverSymbol = undefined;
179
+ typeSymbol.members.delete(functionSymbol);
180
+ });
186
181
  });
187
182
 
188
183
  return (
189
184
  <Declaration symbol={receiverSymbol}>
190
- <Name /> {props.type}
191
- {typeParamsComponent.value}
185
+ <Name /> <FunctionTypeSlot>{props.type}</FunctionTypeSlot>
186
+ <Show when={!!props.typeParameters}>
187
+ <TypeParameters parameters={props.typeParameters} />
188
+ </Show>
189
+ <Show when={!props.typeParameters && !!functionSymbol.receiverSymbol}>
190
+ <TypeParameters
191
+ parameters={[...functionSymbol.receiverSymbol!.typeParameters].map(
192
+ (s) => {
193
+ return {
194
+ name: s.name,
195
+ constraint: (s as TypeParameterSymbol).constraint,
196
+ };
197
+ },
198
+ )}
199
+ />
200
+ </Show>
192
201
  </Declaration>
193
202
  );
194
203
  }
@@ -15,6 +15,7 @@ import {
15
15
  import { createNamedTypeScope } from "../../scopes/factories.js";
16
16
  import { createTypeSymbol } from "../../symbols/factories.js";
17
17
  import { NamedTypeSymbol } from "../../symbols/named-type.js";
18
+ import { TypeParameterSymbol } from "../../symbols/type-parameter.js";
18
19
  import { LineComment } from "../doc/comment.js";
19
20
  import {
20
21
  TypeParameterProps,
@@ -64,13 +65,21 @@ export interface TypeDeclarationProps {
64
65
  export function TypeDeclaration(props: TypeDeclarationProps) {
65
66
  const typeGroup = useContext(TypeDeclarationGroupContext);
66
67
 
67
- const symbol =
68
- props.symbol ??
69
- createTypeSymbol(props.name, "type", {
68
+ let symbol = props.symbol;
69
+ if (!symbol) {
70
+ symbol = createTypeSymbol(props.name, "type", {
70
71
  refkeys: props.refkey,
71
- typeParameters: props.typeParameters,
72
72
  // TODO: set aliasTarget when alias is true
73
73
  });
74
+
75
+ for (const typeParameter of props.typeParameters ?? []) {
76
+ new TypeParameterSymbol(typeParameter.name, symbol.typeParameters, {
77
+ refkeys: typeParameter.refkey,
78
+ constraint: typeParameter.constraint,
79
+ });
80
+ }
81
+ }
82
+
74
83
  const typeScope = createNamedTypeScope(symbol);
75
84
 
76
85
  return (
@@ -1,5 +1,13 @@
1
- import { Namekey, OutputSpace } from "@alloy-js/core";
1
+ import {
2
+ Namekey,
3
+ OutputSpace,
4
+ track,
5
+ TrackOpTypes,
6
+ trigger,
7
+ TriggerOpTypes,
8
+ } from "@alloy-js/core";
2
9
  import { GoSymbol, GoSymbolOptions } from "./go.js";
10
+ import { NamedTypeSymbol } from "./named-type.js";
3
11
 
4
12
  /**
5
13
  * A symbol for a function in Go, including receivers.
@@ -14,4 +22,25 @@ export class FunctionSymbol extends GoSymbol {
14
22
  ) {
15
23
  super(name, spaces, options);
16
24
  }
25
+
26
+ #receiverSymbol?: NamedTypeSymbol = undefined;
27
+
28
+ get receiverSymbol(): NamedTypeSymbol | undefined {
29
+ track(this, TrackOpTypes.GET, "receiverSymbol");
30
+ return this.#receiverSymbol;
31
+ }
32
+
33
+ set receiverSymbol(value: NamedTypeSymbol | undefined) {
34
+ if (this.#receiverSymbol === value) {
35
+ return;
36
+ }
37
+ trigger(
38
+ this,
39
+ TriggerOpTypes.SET,
40
+ "receiverSymbol",
41
+ value,
42
+ this.#receiverSymbol,
43
+ );
44
+ this.#receiverSymbol = value;
45
+ }
17
46
  }
@@ -6,7 +6,6 @@ import {
6
6
  trigger,
7
7
  TriggerOpTypes,
8
8
  } from "@alloy-js/core";
9
- import { TypeParameterProps } from "../components/parameters/typeparameters.jsx";
10
9
  import { GoSymbol, GoSymbolOptions } from "./go.js";
11
10
 
12
11
  // represents a symbol from a .go file. Struct, interface, etc.
@@ -23,7 +22,7 @@ export type NamedTypeTypeKind =
23
22
  export type NamedTypeSymbolKind = "named-type" | "package";
24
23
 
25
24
  export interface NamedTypeSymbolOptions extends GoSymbolOptions {
26
- typeParameters?: TypeParameterProps[];
25
+ typeParameters?: GoSymbol[];
27
26
  }
28
27
 
29
28
  /**
@@ -31,7 +30,7 @@ export interface NamedTypeSymbolOptions extends GoSymbolOptions {
31
30
  */
32
31
  export class NamedTypeSymbol extends GoSymbol {
33
32
  public readonly symbolKind: NamedTypeSymbolKind = "named-type";
34
- public static readonly memberSpaces = ["members"];
33
+ public static readonly memberSpaces = ["members", "typeParameters"];
35
34
 
36
35
  constructor(
37
36
  name: string | Namekey,
@@ -41,7 +40,6 @@ export class NamedTypeSymbol extends GoSymbol {
41
40
  ) {
42
41
  super(name, spaces, options);
43
42
  this.#typeKind = kind;
44
- this.#typeParameters = options?.typeParameters;
45
43
  }
46
44
 
47
45
  #typeKind: NamedTypeTypeKind;
@@ -58,18 +56,8 @@ export class NamedTypeSymbol extends GoSymbol {
58
56
  trigger(this, TriggerOpTypes.SET, "typeKind", value, old);
59
57
  }
60
58
 
61
- #typeParameters?: TypeParameterProps[];
62
59
  get typeParameters() {
63
- track(this, TrackOpTypes.GET, "typeParameters");
64
- return this.#typeParameters;
65
- }
66
- set typeParameters(value: TypeParameterProps[] | undefined) {
67
- const old = this.#typeParameters;
68
- if (old === value) {
69
- return;
70
- }
71
- this.#typeParameters = value;
72
- trigger(this, TriggerOpTypes.SET, "typeParameters", value, old);
60
+ return this.memberSpaceFor("typeParameters")!;
73
61
  }
74
62
 
75
63
  copy() {
@@ -0,0 +1,26 @@
1
+ import { Children, Namekey, OutputSpace } from "@alloy-js/core";
2
+ import { GoSymbol, GoSymbolOptions } from "./go.js";
3
+
4
+ interface TypeParameterSymbolOptions extends GoSymbolOptions {
5
+ constraint?: Children;
6
+ }
7
+ /**
8
+ * A symbol for type parameters in Go.
9
+ */
10
+ export class TypeParameterSymbol extends GoSymbol {
11
+ public readonly symbolKind = "type-parameter";
12
+
13
+ constructor(
14
+ name: string | Namekey,
15
+ spaces: OutputSpace | undefined,
16
+ options: TypeParameterSymbolOptions = {},
17
+ ) {
18
+ super(name, spaces, options);
19
+ this.#constraint = options.constraint ?? "any";
20
+ }
21
+
22
+ #constraint: Children;
23
+ get constraint(): Children {
24
+ return this.#constraint;
25
+ }
26
+ }
package/temp/api.json CHANGED
@@ -2258,6 +2258,50 @@
2258
2258
  }
2259
2259
  ]
2260
2260
  },
2261
+ {
2262
+ "kind": "Property",
2263
+ "canonicalReference": "@alloy-js/go!FunctionSymbol#receiverSymbol:member",
2264
+ "docComment": "",
2265
+ "excerptTokens": [
2266
+ {
2267
+ "kind": "Content",
2268
+ "text": "get receiverSymbol(): "
2269
+ },
2270
+ {
2271
+ "kind": "Reference",
2272
+ "text": "NamedTypeSymbol",
2273
+ "canonicalReference": "@alloy-js/go!NamedTypeSymbol:class"
2274
+ },
2275
+ {
2276
+ "kind": "Content",
2277
+ "text": " | undefined"
2278
+ },
2279
+ {
2280
+ "kind": "Content",
2281
+ "text": ";\n\nset receiverSymbol(value: "
2282
+ },
2283
+ {
2284
+ "kind": "Reference",
2285
+ "text": "NamedTypeSymbol",
2286
+ "canonicalReference": "@alloy-js/go!NamedTypeSymbol:class"
2287
+ },
2288
+ {
2289
+ "kind": "Content",
2290
+ "text": " | undefined);"
2291
+ }
2292
+ ],
2293
+ "isReadonly": false,
2294
+ "isOptional": false,
2295
+ "releaseTag": "Public",
2296
+ "name": "receiverSymbol",
2297
+ "propertyTypeTokenRange": {
2298
+ "startIndex": 1,
2299
+ "endIndex": 3
2300
+ },
2301
+ "isStatic": false,
2302
+ "isProtected": false,
2303
+ "isAbstract": false
2304
+ },
2261
2305
  {
2262
2306
  "kind": "Property",
2263
2307
  "canonicalReference": "@alloy-js/go!FunctionSymbol#symbolKind:member",
@@ -5324,30 +5368,21 @@
5324
5368
  "kind": "Content",
5325
5369
  "text": "get typeParameters(): "
5326
5370
  },
5327
- {
5328
- "kind": "Reference",
5329
- "text": "TypeParameterProps",
5330
- "canonicalReference": "@alloy-js/go!TypeParameterProps:interface"
5331
- },
5332
- {
5333
- "kind": "Content",
5334
- "text": "[] | undefined"
5335
- },
5336
5371
  {
5337
5372
  "kind": "Content",
5338
- "text": ";\n\nset typeParameters(value: "
5373
+ "text": "import(\"@alloy-js/core\")."
5339
5374
  },
5340
5375
  {
5341
5376
  "kind": "Reference",
5342
- "text": "TypeParameterProps",
5343
- "canonicalReference": "@alloy-js/go!TypeParameterProps:interface"
5377
+ "text": "OutputMemberSpace",
5378
+ "canonicalReference": "@alloy-js/core!OutputMemberSpace:class"
5344
5379
  },
5345
5380
  {
5346
5381
  "kind": "Content",
5347
- "text": "[] | undefined);"
5382
+ "text": ";"
5348
5383
  }
5349
5384
  ],
5350
- "isReadonly": false,
5385
+ "isReadonly": true,
5351
5386
  "isOptional": false,
5352
5387
  "releaseTag": "Public",
5353
5388
  "name": "typeParameters",
@@ -5427,8 +5462,8 @@
5427
5462
  },
5428
5463
  {
5429
5464
  "kind": "Reference",
5430
- "text": "TypeParameterProps",
5431
- "canonicalReference": "@alloy-js/go!TypeParameterProps:interface"
5465
+ "text": "GoSymbol",
5466
+ "canonicalReference": "@alloy-js/go!GoSymbol:class"
5432
5467
  },
5433
5468
  {
5434
5469
  "kind": "Content",