@alloy-js/csharp 0.20.0-dev.5 → 0.20.0-dev.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/src/components/SourceFile.d.ts +5 -0
  2. package/dist/src/components/SourceFile.d.ts.map +1 -1
  3. package/dist/src/components/SourceFile.js +18 -58
  4. package/dist/src/components/SourceFile.js.map +1 -1
  5. package/dist/src/components/access-expression/access-expression.d.ts +54 -0
  6. package/dist/src/components/access-expression/access-expression.d.ts.map +1 -0
  7. package/dist/src/components/access-expression/access-expression.js +277 -0
  8. package/dist/src/components/access-expression/access-expression.js.map +1 -0
  9. package/dist/src/components/access-expression/access-expression.test.d.ts +2 -0
  10. package/dist/src/components/access-expression/access-expression.test.d.ts.map +1 -0
  11. package/dist/src/components/access-expression/access-expression.test.js +336 -0
  12. package/dist/src/components/access-expression/access-expression.test.js.map +1 -0
  13. package/dist/src/components/access-expression/part-descriptors.d.ts +32 -0
  14. package/dist/src/components/access-expression/part-descriptors.d.ts.map +1 -0
  15. package/dist/src/components/access-expression/part-descriptors.js +99 -0
  16. package/dist/src/components/access-expression/part-descriptors.js.map +1 -0
  17. package/dist/src/components/class/declaration.d.ts.map +1 -1
  18. package/dist/src/components/class/declaration.js.map +1 -1
  19. package/dist/src/components/index.d.ts +1 -0
  20. package/dist/src/components/index.d.ts.map +1 -1
  21. package/dist/src/components/index.js +1 -0
  22. package/dist/src/components/index.js.map +1 -1
  23. package/dist/src/components/invocation-expression/invocation-expression.d.ts +29 -0
  24. package/dist/src/components/invocation-expression/invocation-expression.d.ts.map +1 -0
  25. package/dist/src/components/invocation-expression/invocation-expression.js +70 -0
  26. package/dist/src/components/invocation-expression/invocation-expression.js.map +1 -0
  27. package/dist/src/components/invocation-expression/invocation-expression.test.d.ts +2 -0
  28. package/dist/src/components/invocation-expression/invocation-expression.test.d.ts.map +1 -0
  29. package/dist/src/components/invocation-expression/invocation-expression.test.js +105 -0
  30. package/dist/src/components/invocation-expression/invocation-expression.test.js.map +1 -0
  31. package/dist/src/components/parameters/parameters.d.ts +1 -2
  32. package/dist/src/components/parameters/parameters.d.ts.map +1 -1
  33. package/dist/src/components/parameters/parameters.js +2 -1
  34. package/dist/src/components/parameters/parameters.js.map +1 -1
  35. package/dist/src/components/parameters/parameters.test.js +66 -0
  36. package/dist/src/components/parameters/parameters.test.js.map +1 -1
  37. package/dist/src/components/property/property.d.ts +2 -2
  38. package/dist/src/components/property/property.d.ts.map +1 -1
  39. package/dist/src/components/property/property.js +10 -3
  40. package/dist/src/components/property/property.js.map +1 -1
  41. package/dist/src/scopes/csharp.d.ts +2 -0
  42. package/dist/src/scopes/csharp.d.ts.map +1 -1
  43. package/dist/src/scopes/csharp.js +3 -0
  44. package/dist/src/scopes/csharp.js.map +1 -1
  45. package/dist/src/symbols/csharp.d.ts +11 -0
  46. package/dist/src/symbols/csharp.d.ts.map +1 -1
  47. package/dist/src/symbols/csharp.js +23 -0
  48. package/dist/src/symbols/csharp.js.map +1 -1
  49. package/dist/src/symbols/factories.d.ts.map +1 -1
  50. package/dist/src/symbols/factories.js +12 -6
  51. package/dist/src/symbols/factories.js.map +1 -1
  52. package/dist/src/symbols/reference.d.ts +2 -2
  53. package/dist/src/symbols/reference.d.ts.map +1 -1
  54. package/dist/src/symbols/reference.js +17 -5
  55. package/dist/src/symbols/reference.js.map +1 -1
  56. package/dist/tsconfig.tsbuildinfo +1 -1
  57. package/package.json +2 -2
  58. package/src/components/SourceFile.tsx +27 -30
  59. package/src/components/access-expression/access-expression.test.tsx +284 -0
  60. package/src/components/access-expression/access-expression.tsx +375 -0
  61. package/src/components/access-expression/part-descriptors.ts +175 -0
  62. package/src/components/class/declaration.tsx +1 -0
  63. package/src/components/index.ts +1 -0
  64. package/src/components/invocation-expression/invocation-expression.test.tsx +101 -0
  65. package/src/components/invocation-expression/invocation-expression.tsx +60 -0
  66. package/src/components/parameters/parameters.test.tsx +46 -0
  67. package/src/components/parameters/parameters.tsx +2 -2
  68. package/src/components/property/property.tsx +8 -2
  69. package/src/scopes/csharp.ts +5 -0
  70. package/src/symbols/csharp.ts +32 -0
  71. package/src/symbols/factories.ts +31 -15
  72. package/src/symbols/{reference.ts → reference.tsx} +11 -9
  73. package/temp/api.json +271 -36
@@ -0,0 +1,60 @@
1
+ import { Children, For, Indent, Show, Wrap } from "@alloy-js/core";
2
+
3
+ export interface InvocationExpressionProps {
4
+ target: Children;
5
+ /**
6
+ * Arguments to pass to the invocation.
7
+ */
8
+ args?: Children[];
9
+ /**
10
+ * Generic type arguments for the invocation.
11
+ */
12
+ typeArgs?: Children[];
13
+ }
14
+
15
+ /**
16
+ * A call to a function or method.
17
+ *
18
+ * @example
19
+ *
20
+ * ```jsx
21
+ * <InvocationExpression target="Foo" typeArgs={["T"]} args={["x"]} />
22
+ * ```
23
+ *
24
+ * Renders to:
25
+ *
26
+ * ```csharp
27
+ * Foo<T>("x");
28
+ * ```
29
+ */
30
+ export function InvocationExpression(props: InvocationExpressionProps) {
31
+ return (
32
+ <group>
33
+ {props.target}
34
+ <Show when={!!props.typeArgs && props.typeArgs.length > 0}>
35
+ {"<"}
36
+ <Wrap
37
+ when={props.typeArgs!.length > 1}
38
+ with={Indent}
39
+ props={{ softline: true, trailingBreak: true }}
40
+ >
41
+ <For each={props.typeArgs ?? []} comma line>
42
+ {(typeArg) => typeArg}
43
+ </For>
44
+ </Wrap>
45
+ {">"}
46
+ </Show>
47
+ (
48
+ <Wrap
49
+ when={!!props.args && props.args.length > 1}
50
+ with={Indent}
51
+ props={{ softline: true, trailingBreak: true }}
52
+ >
53
+ <For each={props.args ?? []} comma line>
54
+ {(arg) => arg}
55
+ </For>
56
+ </Wrap>
57
+ )
58
+ </group>
59
+ );
60
+ }
@@ -1,5 +1,6 @@
1
1
  import { ClassDeclaration } from "#components/class/declaration.jsx";
2
2
  import { Method } from "#components/method/method.jsx";
3
+ import { Property } from "#components/property/property.jsx";
3
4
  import { List, memberRefkey, namekey } from "@alloy-js/core";
4
5
  import { Children } from "@alloy-js/core/jsx-runtime";
5
6
  import { expect, it } from "vitest";
@@ -69,3 +70,48 @@ it("members can be referenced", () => {
69
70
  }
70
71
  `);
71
72
  });
73
+
74
+ it("members can be referenced when the parameter is nullable", () => {
75
+ const propTypeKey = namekey("PropType");
76
+ const propTypePropKey = namekey("Field");
77
+ const classKey = namekey("TestType");
78
+ const propKey = namekey("TestProp");
79
+ const param1Key = namekey("param1");
80
+
81
+ expect(
82
+ <TestNamespace>
83
+ <List>
84
+ <ClassDeclaration name={propTypeKey}>
85
+ <Property name={propTypePropKey} type={"string"} nullable />
86
+ </ClassDeclaration>
87
+ <ClassDeclaration name={classKey}>
88
+ <Property name={propKey} type={propTypeKey} nullable />
89
+ </ClassDeclaration>
90
+ <ClassDeclaration name="Test">
91
+ <Method
92
+ name="Test"
93
+ parameters={[{ name: param1Key, type: classKey, optional: true }]}
94
+ >
95
+ return {memberRefkey(param1Key, propKey, propTypePropKey)};
96
+ </Method>
97
+ </ClassDeclaration>
98
+ </List>
99
+ </TestNamespace>,
100
+ ).toRenderTo(`
101
+ class PropType
102
+ {
103
+ string? Field { }
104
+ }
105
+ class TestType
106
+ {
107
+ PropType? TestProp { }
108
+ }
109
+ class Test
110
+ {
111
+ void Test(TestType? param1)
112
+ {
113
+ return param1?.TestProp?.Field;
114
+ }
115
+ }
116
+ `);
117
+ });
@@ -6,7 +6,6 @@ import {
6
6
  For,
7
7
  Indent,
8
8
  Namekey,
9
- OutputSymbol,
10
9
  Refkey,
11
10
  } from "@alloy-js/core";
12
11
  import { createParameterSymbol } from "../../symbols/factories.js";
@@ -19,8 +18,8 @@ export interface ParameterProps {
19
18
  optional?: boolean;
20
19
  /** Default value for the parameter */
21
20
  default?: Children;
21
+
22
22
  refkey?: Refkey;
23
- symbol?: OutputSymbol;
24
23
  }
25
24
 
26
25
  /** Define a parameter to be used in class or interface method. */
@@ -30,6 +29,7 @@ export function Parameter(props: ParameterProps) {
30
29
  const memberSymbol = createParameterSymbol(props.name, {
31
30
  refkeys: props.refkey,
32
31
  type: TypeSlot.firstSymbol,
32
+ isNullable: props.optional,
33
33
  });
34
34
 
35
35
  return (
@@ -2,9 +2,11 @@ import {
2
2
  Block,
3
3
  Children,
4
4
  code,
5
+ createSymbolSlot,
5
6
  List,
6
7
  MemberDeclaration,
7
8
  MemberName,
9
+ Namekey,
8
10
  Refkey,
9
11
  } from "@alloy-js/core";
10
12
  import {
@@ -48,7 +50,7 @@ const getModifiers = makeModifiers<PropertyModifiers>([
48
50
 
49
51
  /** Properties for {@link Property} component */
50
52
  export interface PropertyProps extends AccessModifiers, PropertyModifiers {
51
- name: string;
53
+ name: Namekey | string;
52
54
  refkey?: Refkey;
53
55
 
54
56
  /** Property type */
@@ -115,8 +117,12 @@ export interface PropertyProps extends AccessModifiers, PropertyModifiers {
115
117
  * ```
116
118
  */
117
119
  export function Property(props: PropertyProps) {
120
+ const TypeSlot = createSymbolSlot();
121
+
118
122
  const propertySymbol = createPropertySymbol(props.name, {
119
123
  refkeys: props.refkey,
124
+ isNullable: props.nullable,
125
+ type: TypeSlot.firstSymbol,
120
126
  });
121
127
 
122
128
  const modifiers = computeModifiersPrefix([
@@ -135,7 +141,7 @@ export function Property(props: PropertyProps) {
135
141
  <DocWhen doc={props.doc} />
136
142
  <AttributeList attributes={props.attributes} endline />
137
143
  {modifiers}
138
- {props.type}
144
+ <TypeSlot>{props.type}</TypeSlot>
139
145
  {props.nullable && "?"} <MemberName />{" "}
140
146
  <Block newline inline>
141
147
  <List joiner=" ">
@@ -1,4 +1,5 @@
1
1
  import { OutputScope, OutputScopeOptions } from "@alloy-js/core";
2
+ import type { CSharpSymbol } from "../symbols/csharp.js";
2
3
  import { NamespaceSymbol } from "../symbols/namespace.js";
3
4
 
4
5
  export class CSharpScope extends OutputScope {
@@ -15,4 +16,8 @@ export class CSharpScope extends OutputScope {
15
16
  get enclosingNamespace() {
16
17
  return this.#namespaceSymbol;
17
18
  }
19
+
20
+ get ownerSymbol(): CSharpSymbol | undefined {
21
+ return super.ownerSymbol as CSharpSymbol | undefined;
22
+ }
18
23
  }
@@ -33,6 +33,10 @@ export interface CSharpSymbolOptions extends OutputSymbolOptions {
33
33
  isSealed?: boolean;
34
34
  isExtern?: boolean;
35
35
  isReadOnly?: boolean;
36
+ /**
37
+ * Whether the value held by this symbol could be null.
38
+ */
39
+ isNullable?: boolean;
36
40
  }
37
41
 
38
42
  export type CSharpSymbolKinds =
@@ -72,6 +76,7 @@ export class CSharpSymbol extends OutputSymbol {
72
76
  this.#isSealed = options.isSealed ?? false;
73
77
  this.#isExtern = options.isExtern ?? false;
74
78
  this.#isReadOnly = options.isReadOnly ?? false;
79
+ this.#isNullable = options.isNullable; // undefined means unset, here.
75
80
  }
76
81
 
77
82
  get enclosingNamespace(): NamespaceSymbol | undefined {
@@ -280,6 +285,33 @@ export class CSharpSymbol extends OutputSymbol {
280
285
  trigger(this, TriggerOpTypes.SET, "isReadOnly", value, old);
281
286
  }
282
287
  #isReadOnly: boolean = false;
288
+
289
+ #isNullable: boolean | undefined = undefined;
290
+
291
+ /**
292
+ * Whether this symbol might contain null. True if this symbol has a
293
+ * `typeSymbol` and that symbol is nullable, or else when this symbol has the
294
+ * `nullable` option set.
295
+ */
296
+ get isNullable() {
297
+ if (this.hasTypeSymbol && this.#isNullable === undefined) {
298
+ return (this.type! as CSharpSymbol).isNullable;
299
+ }
300
+
301
+ track(this, TrackOpTypes.GET, "isNullable");
302
+ return !!this.#isNullable;
303
+ }
304
+
305
+ set isNullable(value: boolean) {
306
+ const old = this.#isNullable;
307
+ if (old === value) {
308
+ return;
309
+ }
310
+
311
+ this.#isNullable = value;
312
+
313
+ trigger(this, TriggerOpTypes.SET, "isNullable", value, old);
314
+ }
283
315
  }
284
316
 
285
317
  export function accessibilityFromProps(props: AccessModifiers) {
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  Namekey,
3
3
  NamePolicyGetter,
4
+ onCleanup,
4
5
  OutputSymbolOptions,
5
6
  useBinder,
6
7
  } from "@alloy-js/core";
@@ -99,13 +100,22 @@ export function createFieldSymbol(
99
100
  );
100
101
  }
101
102
 
103
+ function withCleanup<T extends CSharpSymbol>(sym: T): T {
104
+ onCleanup(() => {
105
+ sym.delete();
106
+ });
107
+ return sym;
108
+ }
109
+
102
110
  export function createNamedTypeSymbol(
103
111
  name: string | Namekey,
104
112
  kind: NamedTypeTypeKind,
105
113
  options?: OutputSymbolOptions,
106
114
  ) {
107
115
  const scope = useNamedTypeScope();
108
- return new NamedTypeSymbol(name, scope.ownerSymbol.members, kind, options);
116
+ return withCleanup(
117
+ new NamedTypeSymbol(name, scope.ownerSymbol.members, kind, options),
118
+ );
109
119
  }
110
120
 
111
121
  export function createNamespaceSymbol(name: string) {
@@ -114,7 +124,7 @@ export function createNamespaceSymbol(name: string) {
114
124
  if (nsSymbol.members.symbolNames.has(name)) {
115
125
  return nsSymbol.members.symbolNames.get(name)! as NamespaceSymbol;
116
126
  }
117
- return new NamespaceSymbol(name, nsSymbol);
127
+ return withCleanup(new NamespaceSymbol(name, nsSymbol));
118
128
  }
119
129
 
120
130
  export interface CreateMethodSymbolOptions extends CSharpSymbolOptions {
@@ -137,11 +147,13 @@ export function createMethodSymbol(
137
147
  );
138
148
  }
139
149
 
140
- return new MethodSymbol(
141
- originalName,
142
- scope.members,
143
- options.methodKind ?? "ordinary",
144
- withNamePolicy(options, "class-method"),
150
+ return withCleanup(
151
+ new MethodSymbol(
152
+ originalName,
153
+ scope.members,
154
+ options.methodKind ?? "ordinary",
155
+ withNamePolicy(options, "class-method"),
156
+ ),
145
157
  );
146
158
  }
147
159
 
@@ -150,10 +162,12 @@ export function createPropertySymbol(
150
162
  options: CSharpSymbolOptions,
151
163
  ) {
152
164
  const scope = useNamedTypeScope();
153
- return new CSharpSymbol(
154
- name,
155
- scope.members,
156
- withNamePolicy(options, "class-property"),
165
+ return withCleanup(
166
+ new CSharpSymbol(
167
+ name,
168
+ scope.members,
169
+ withNamePolicy(options, "class-property"),
170
+ ),
157
171
  );
158
172
  }
159
173
 
@@ -181,10 +195,12 @@ export function createVariableSymbol(
181
195
  `Can't create variable symbol outside of a lexical scope, got a ${scope.constructor.name}.`,
182
196
  );
183
197
  }
184
- return new CSharpSymbol(
185
- originalName,
186
- scope.localVariables,
187
- withNamePolicy(options, "variable"),
198
+ return withCleanup(
199
+ new CSharpSymbol(
200
+ originalName,
201
+ scope.localVariables,
202
+ withNamePolicy(options, "variable"),
203
+ ),
188
204
  );
189
205
  }
190
206
 
@@ -1,4 +1,5 @@
1
- import { memo, OutputSymbol, Refkey, resolve } from "@alloy-js/core";
1
+ import { AccessExpression } from "#components/access-expression/access-expression.jsx";
2
+ import { Children, memo, OutputSymbol, Refkey, resolve } from "@alloy-js/core";
2
3
  import { CSharpScope } from "../scopes/csharp.js";
3
4
  import { CSharpNamespaceScope } from "../scopes/namespace.js";
4
5
  import { useSourceFileScope } from "../scopes/source-file.js";
@@ -9,10 +10,11 @@ import { NamespaceSymbol } from "./namespace.js";
9
10
  // e.g. if refkey is for bar in enum type foo, and
10
11
  // foo is in the same namespace as the refkey, then
11
12
  // the result would be foo.bar.
12
- export function ref(refkey: Refkey): () => [string, OutputSymbol | undefined] {
13
+ export function ref(
14
+ refkey: Refkey,
15
+ ): () => [Children, OutputSymbol | undefined] {
13
16
  const refSfScope = useSourceFileScope()!;
14
17
  const resolveResult = resolve<CSharpScope, CSharpSymbol>(refkey as Refkey);
15
-
16
18
  return memo(() => {
17
19
  if (resolveResult.value === undefined) {
18
20
  return ["<Unresolved Symbol>", undefined];
@@ -22,7 +24,6 @@ export function ref(refkey: Refkey): () => [string, OutputSymbol | undefined] {
22
24
  const { commonScope, pathDown, memberPath } = result;
23
25
  let { lexicalDeclaration } = result;
24
26
 
25
- const parts = [];
26
27
  if (!commonScope) {
27
28
  // this shouldn't be possible in csharp.
28
29
  return ["<Unresolved Symbol>", undefined];
@@ -47,16 +48,17 @@ export function ref(refkey: Refkey): () => [string, OutputSymbol | undefined] {
47
48
  refSfScope.addUsing(nsToUse!);
48
49
  }
49
50
 
51
+ const parts = [];
52
+
50
53
  for (const nsScope of pathDown) {
51
- parts.push(nsScope.ownerSymbol!.name);
54
+ parts.push(<AccessExpression.Part symbol={nsScope.ownerSymbol!} />);
52
55
  }
53
56
 
54
- parts.push(lexicalDeclaration.name);
55
-
57
+ parts.push(<AccessExpression.Part symbol={lexicalDeclaration} />);
56
58
  for (const member of memberPath) {
57
- parts.push(member.name);
59
+ parts.push(<AccessExpression.Part symbol={member} />);
58
60
  }
59
61
 
60
- return [parts.join("."), result.symbol];
62
+ return [<AccessExpression children={parts} />, result.symbol];
61
63
  });
62
64
  }