@alloy-js/csharp 0.23.0-dev.7 → 0.23.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.
@@ -9,6 +9,7 @@ Properties for [Constructor](../constructor/) component.
9
9
 
10
10
 
11
11
  <Constructor
12
+ baseConstructor={Children[]}
12
13
  doc={Children}
13
14
  file
14
15
  internal
@@ -17,6 +18,7 @@ Properties for [Constructor](../constructor/) component.
17
18
  protected
18
19
  public
19
20
  refkey={Refkey}
21
+ thisConstructor={Children[]}
20
22
  >
21
23
  {children}
22
24
  </Constructor>
@@ -29,6 +31,7 @@ Properties for [Constructor](../constructor/) component.
29
31
 
30
32
 
31
33
  Constructor({
34
+ baseConstructor: Children[],
32
35
  doc: Children,
33
36
  file: boolean,
34
37
  internal: boolean,
@@ -37,19 +40,22 @@ Properties for [Constructor](../constructor/) component.
37
40
  protected: boolean,
38
41
  public: boolean,
39
42
  refkey: Refkey,
43
+ thisConstructor: Children[],
40
44
  }).children(children)
41
45
  ```
42
46
 
43
47
  ## Props
44
48
 
45
- | | | |
46
- | ---------- | -------------------------------------------------- | ---------------------- |
47
- | children | optional [Children](../../../core/types/children/) | Constructor body |
48
- | doc | optional [Children](../../../core/types/children/) | Doc comment |
49
- | file | optional boolean | |
50
- | internal | optional boolean | |
51
- | parameters | optional [ParameterProps](../parameter/)\[] | Constructor parameters |
52
- | private | optional boolean | |
53
- | protected | optional boolean | |
54
- | public | optional boolean | |
55
- | refkey | optional [Refkey](../../../core/types/refkey/) | Refkey |
49
+ | | | |
50
+ | --------------- | ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
51
+ | baseConstructor | optional [Children](../../../core/types/children/)\[] | Arguments to pass to the base class constructor. Renders `: base(args)` between parameter list and body. |
52
+ | children | optional [Children](../../../core/types/children/) | Constructor body |
53
+ | doc | optional [Children](../../../core/types/children/) | Doc comment |
54
+ | file | optional boolean | |
55
+ | internal | optional boolean | |
56
+ | parameters | optional [ParameterProps](../parameter/)\[] | Constructor parameters |
57
+ | private | optional boolean | |
58
+ | protected | optional boolean | |
59
+ | public | optional boolean | |
60
+ | refkey | optional [Refkey](../../../core/types/refkey/) | Refkey |
61
+ | thisConstructor | optional [Children](../../../core/types/children/)\[] | Arguments to pass to another constructor in the same class. Renders `: this(args)` between parameter list and body. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/csharp",
3
- "version": "0.23.0-dev.7",
3
+ "version": "0.23.0-dev.8",
4
4
  "description": "Alloy components for CSharp language.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -60,3 +60,67 @@ it("renders doc comment", () => {
60
60
  }
61
61
  `);
62
62
  });
63
+
64
+ it("renders : base() initializer", () => {
65
+ expect(
66
+ <TestNamespace>
67
+ <ClassDeclaration public name="DerivedClass" baseType="BaseClass">
68
+ <Constructor
69
+ public
70
+ parameters={[{ name: "name", type: "string" }]}
71
+ baseConstructor={["name"]}
72
+ >
73
+ // body
74
+ </Constructor>
75
+ </ClassDeclaration>
76
+ </TestNamespace>,
77
+ ).toRenderTo(`
78
+ public class DerivedClass : BaseClass
79
+ {
80
+ public DerivedClass(string name) : base(name)
81
+ {
82
+ // body
83
+ }
84
+ }
85
+ `);
86
+ });
87
+
88
+ it("renders : this() initializer", () => {
89
+ expect(
90
+ <TestNamespace>
91
+ <ClassDeclaration public name="MyClass">
92
+ <Constructor public thisConstructor={["0", "0"]}>
93
+ // body
94
+ </Constructor>
95
+ </ClassDeclaration>
96
+ </TestNamespace>,
97
+ ).toRenderTo(`
98
+ public class MyClass
99
+ {
100
+ public MyClass() : this(0, 0)
101
+ {
102
+ // body
103
+ }
104
+ }
105
+ `);
106
+ });
107
+
108
+ it("renders : base() with no arguments", () => {
109
+ expect(
110
+ <TestNamespace>
111
+ <ClassDeclaration public name="DerivedClass" baseType="BaseClass">
112
+ <Constructor public baseConstructor={[]}>
113
+ // body
114
+ </Constructor>
115
+ </ClassDeclaration>
116
+ </TestNamespace>,
117
+ ).toRenderTo(`
118
+ public class DerivedClass : BaseClass
119
+ {
120
+ public DerivedClass() : base()
121
+ {
122
+ // body
123
+ }
124
+ }
125
+ `);
126
+ });
@@ -1,5 +1,12 @@
1
1
  import { MethodScope } from "#components/method-scope.jsx";
2
- import { Block, MemberDeclaration, MemberName, Refkey } from "@alloy-js/core";
2
+ import {
3
+ Block,
4
+ For,
5
+ Indent,
6
+ MemberDeclaration,
7
+ MemberName,
8
+ Refkey,
9
+ } from "@alloy-js/core";
3
10
  import { Children } from "@alloy-js/core/jsx-runtime";
4
11
  import {
5
12
  AccessModifiers,
@@ -24,11 +31,51 @@ export interface ConstructorProps extends AccessModifiers {
24
31
  /** Refkey */
25
32
  refkey?: Refkey;
26
33
 
34
+ /**
35
+ * Arguments to pass to the base class constructor.
36
+ * Renders `: base(args)` between parameter list and body.
37
+ *
38
+ * @example
39
+ * ```tsx
40
+ * <Constructor public baseConstructor={["name", "42"]}>
41
+ * ```
42
+ * This will produce:
43
+ * ```csharp
44
+ * public MyClass(...) : base(name, 42)
45
+ * {
46
+ * }
47
+ * ```
48
+ */
49
+ baseConstructor?: Children[];
50
+
51
+ /**
52
+ * Arguments to pass to another constructor in the same class.
53
+ * Renders `: this(args)` between parameter list and body.
54
+ *
55
+ * @example
56
+ * ```tsx
57
+ * <Constructor public thisConstructor={["0", "0"]}>
58
+ * ```
59
+ * This will produce:
60
+ * ```csharp
61
+ * public MyClass() : this(0, 0)
62
+ * {
63
+ * }
64
+ * ```
65
+ */
66
+ thisConstructor?: Children[];
67
+
27
68
  /** Constructor body */
28
69
  children?: Children;
29
70
  }
30
71
 
31
72
  export function Constructor(props: ConstructorProps) {
73
+ if (props.baseConstructor && props.thisConstructor) {
74
+ throw new Error(
75
+ "Cannot use both 'baseConstructor' and 'thisConstructor' on the same constructor",
76
+ );
77
+ }
78
+
32
79
  const scope = useNamedTypeScope();
33
80
 
34
81
  const name = scope.ownerSymbol.name;
@@ -39,6 +86,13 @@ export function Constructor(props: ConstructorProps) {
39
86
 
40
87
  const modifiers = computeModifiersPrefix([getAccessModifier(props)]);
41
88
 
89
+ const initializer =
90
+ props.baseConstructor ?
91
+ <ConstructorInitializer keyword="base" args={props.baseConstructor} />
92
+ : props.thisConstructor ?
93
+ <ConstructorInitializer keyword="this" args={props.thisConstructor} />
94
+ : null;
95
+
42
96
  return (
43
97
  <MemberDeclaration symbol={ctorSymbol}>
44
98
  <MethodScope>
@@ -46,8 +100,34 @@ export function Constructor(props: ConstructorProps) {
46
100
  {modifiers}
47
101
  <MemberName />
48
102
  <Parameters parameters={props.parameters} />
103
+ {initializer}
49
104
  <Block newline>{props.children}</Block>
50
105
  </MethodScope>
51
106
  </MemberDeclaration>
52
107
  );
53
108
  }
109
+
110
+ interface ConstructorInitializerProps {
111
+ keyword: "base" | "this";
112
+ args: Children[];
113
+ }
114
+
115
+ function ConstructorInitializer(props: ConstructorInitializerProps) {
116
+ return (
117
+ <group>
118
+ {" : "}
119
+ {props.keyword}(
120
+ <Indent nobreak>
121
+ <For each={props.args} joiner={", "}>
122
+ {(arg) => (
123
+ <>
124
+ <softline />
125
+ {arg}
126
+ </>
127
+ )}
128
+ </For>
129
+ </Indent>
130
+ <softline />)
131
+ </group>
132
+ );
133
+ }
package/temp/api.json CHANGED
@@ -2305,6 +2305,38 @@
2305
2305
  "name": "ConstructorProps",
2306
2306
  "preserveMemberOrder": false,
2307
2307
  "members": [
2308
+ {
2309
+ "kind": "PropertySignature",
2310
+ "canonicalReference": "@alloy-js/csharp!ConstructorProps#baseConstructor:member",
2311
+ "docComment": "/**\n * Arguments to pass to the base class constructor.\n * Renders `: base(args)` between parameter list and body.\n *\n * @example\n * ```tsx\n * <Constructor public baseConstructor={[\"name\", \"42\"]}>\n * ```\n *\n * This will produce:\n * ```csharp\n * public MyClass(...) : base(name, 42)\n * {\n * }\n * ```\n *\n */\n",
2312
+ "excerptTokens": [
2313
+ {
2314
+ "kind": "Content",
2315
+ "text": "baseConstructor?: "
2316
+ },
2317
+ {
2318
+ "kind": "Reference",
2319
+ "text": "Children",
2320
+ "canonicalReference": "@alloy-js/core!Children:type"
2321
+ },
2322
+ {
2323
+ "kind": "Content",
2324
+ "text": "[]"
2325
+ },
2326
+ {
2327
+ "kind": "Content",
2328
+ "text": ";"
2329
+ }
2330
+ ],
2331
+ "isReadonly": false,
2332
+ "isOptional": true,
2333
+ "releaseTag": "Public",
2334
+ "name": "baseConstructor",
2335
+ "propertyTypeTokenRange": {
2336
+ "startIndex": 1,
2337
+ "endIndex": 3
2338
+ }
2339
+ },
2308
2340
  {
2309
2341
  "kind": "PropertySignature",
2310
2342
  "canonicalReference": "@alloy-js/csharp!ConstructorProps#children:member",
@@ -2420,6 +2452,38 @@
2420
2452
  "startIndex": 1,
2421
2453
  "endIndex": 2
2422
2454
  }
2455
+ },
2456
+ {
2457
+ "kind": "PropertySignature",
2458
+ "canonicalReference": "@alloy-js/csharp!ConstructorProps#thisConstructor:member",
2459
+ "docComment": "/**\n * Arguments to pass to another constructor in the same class.\n * Renders `: this(args)` between parameter list and body.\n *\n * @example\n * ```tsx\n * <Constructor public thisConstructor={[\"0\", \"0\"]}>\n * ```\n *\n * This will produce:\n * ```csharp\n * public MyClass() : this(0, 0)\n * {\n * }\n * ```\n *\n */\n",
2460
+ "excerptTokens": [
2461
+ {
2462
+ "kind": "Content",
2463
+ "text": "thisConstructor?: "
2464
+ },
2465
+ {
2466
+ "kind": "Reference",
2467
+ "text": "Children",
2468
+ "canonicalReference": "@alloy-js/core!Children:type"
2469
+ },
2470
+ {
2471
+ "kind": "Content",
2472
+ "text": "[]"
2473
+ },
2474
+ {
2475
+ "kind": "Content",
2476
+ "text": ";"
2477
+ }
2478
+ ],
2479
+ "isReadonly": false,
2480
+ "isOptional": true,
2481
+ "releaseTag": "Public",
2482
+ "name": "thisConstructor",
2483
+ "propertyTypeTokenRange": {
2484
+ "startIndex": 1,
2485
+ "endIndex": 3
2486
+ }
2423
2487
  }
2424
2488
  ],
2425
2489
  "extendsTokenRanges": [