@alloy-js/csharp 0.18.0-dev.12 → 0.18.0-dev.14

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.
@@ -103,6 +103,58 @@ it("defines params and return type", () => {
103
103
  `);
104
104
  });
105
105
 
106
+ it("defines optional param", () => {
107
+ const res = (
108
+ <Wrapper>
109
+ <InterfaceMethod
110
+ public
111
+ name="MethodOne"
112
+ parameters={[
113
+ {
114
+ name: "intParam",
115
+ type: "int",
116
+ optional: true,
117
+ },
118
+ ]}
119
+ returns="string"
120
+ />
121
+ </Wrapper>
122
+ );
123
+
124
+ expect(res).toRenderTo(`
125
+ public interface TestInterface
126
+ {
127
+ public string MethodOne(int? intParam);
128
+ }
129
+ `);
130
+ });
131
+
132
+ it("defines optional param with default", () => {
133
+ const res = (
134
+ <Wrapper>
135
+ <InterfaceMethod
136
+ public
137
+ name="MethodOne"
138
+ parameters={[
139
+ {
140
+ name: "intParam",
141
+ type: "int",
142
+ default: 12,
143
+ },
144
+ ]}
145
+ returns="string"
146
+ />
147
+ </Wrapper>
148
+ );
149
+
150
+ expect(res).toRenderTo(`
151
+ public interface TestInterface
152
+ {
153
+ public string MethodOne(int intParam = 12);
154
+ }
155
+ `);
156
+ });
157
+
106
158
  it("specify doc comment", () => {
107
159
  expect(
108
160
  <TestNamespace>
@@ -0,0 +1,73 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { TestNamespace } from "../../../test/utils.jsx";
3
+ import { ClassProperty } from "../class/property.jsx";
4
+ import { RecordDeclaration } from "./declaration.jsx";
5
+
6
+ it("declares class with no members", () => {
7
+ expect(
8
+ <TestNamespace>
9
+ <RecordDeclaration name="TestRecord" />
10
+ </TestNamespace>,
11
+ ).toRenderTo(`
12
+ record TestRecord;
13
+ `);
14
+ });
15
+
16
+ describe("modifiers", () => {
17
+ it.each(["public", "private", "internal"])("%s", (mod) => {
18
+ expect(
19
+ <TestNamespace>
20
+ <RecordDeclaration {...{ [mod]: true }} name="TestRecord" />
21
+ </TestNamespace>,
22
+ ).toRenderTo(`
23
+ ${mod} record TestRecord;
24
+ `);
25
+ });
26
+
27
+ it.each(["partial"])("%s", (mod) => {
28
+ expect(
29
+ <TestNamespace>
30
+ <RecordDeclaration {...{ [mod]: true }} name="TestRecord" />
31
+ </TestNamespace>,
32
+ ).toRenderTo(`
33
+ ${mod} record TestRecord;
34
+ `);
35
+ });
36
+
37
+ it("combines modifiers", () => {
38
+ expect(
39
+ <TestNamespace>
40
+ <RecordDeclaration public partial name="TestRecord" />
41
+ </TestNamespace>,
42
+ ).toRenderTo(`
43
+ public partial record TestRecord;
44
+ `);
45
+ });
46
+ });
47
+
48
+ it("specify doc comment", () => {
49
+ expect(
50
+ <TestNamespace>
51
+ <RecordDeclaration name="TestRecord" doc="This is a test" />
52
+ </TestNamespace>,
53
+ ).toRenderTo(`
54
+ /// This is a test
55
+ record TestRecord;
56
+ `);
57
+ });
58
+
59
+ it("specify class property inside", () => {
60
+ expect(
61
+ <TestNamespace>
62
+ <RecordDeclaration name="TestRecord" doc="This is a test">
63
+ <ClassProperty name="Prop" get set type="string" />
64
+ </RecordDeclaration>
65
+ </TestNamespace>,
66
+ ).toRenderTo(`
67
+ /// This is a test
68
+ record TestRecord
69
+ {
70
+ string Prop { get; set; }
71
+ }
72
+ `);
73
+ });
@@ -0,0 +1,109 @@
1
+ import * as core from "@alloy-js/core";
2
+ import {
3
+ AccessModifiers,
4
+ computeModifiersPrefix,
5
+ getAccessModifier,
6
+ makeModifiers,
7
+ } from "../../modifiers.js";
8
+ import { useCSharpNamePolicy } from "../../name-policy.js";
9
+ import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
10
+ import { CSharpMemberScope } from "../../symbols/scopes.js";
11
+ import { DocWhen } from "../doc/comment.jsx";
12
+ import { Name } from "../Name.jsx";
13
+
14
+ export interface RecordModifiers {
15
+ readonly partial?: boolean;
16
+ }
17
+
18
+ const getRecordModifiers = makeModifiers<RecordModifiers>(["partial"]);
19
+
20
+ // properties for creating a class
21
+ export interface RecordDeclarationProps
22
+ extends Omit<core.DeclarationProps, "nameKind">,
23
+ AccessModifiers,
24
+ RecordModifiers {
25
+ name: string;
26
+
27
+ /** Doc comment */
28
+ doc?: core.Children;
29
+ refkey?: core.Refkey;
30
+ typeParameters?: Record<string, core.Refkey>;
31
+ }
32
+
33
+ /**
34
+ * CSharp record declaration.
35
+ * @example
36
+ * ```tsx
37
+ * <RecordDeclaration public name="IMyRecord">
38
+ * <RecordMember public name="MyProperty" type="int" />
39
+ * <RecordMethod public name="MyMethod" returnType="void">
40
+ * <Parameter name="value" type="int" />
41
+ * </RecordMethod>
42
+ * </RecordDeclaration>
43
+ * ```
44
+ * This will produce:
45
+ * ```csharp
46
+ * public record MyIface
47
+ * {
48
+ * public int MyProperty { get; set; }
49
+ * public void MyMethod(int value);
50
+ * }
51
+ * ```
52
+ */
53
+ export function RecordDeclaration(props: RecordDeclarationProps) {
54
+ const name = useCSharpNamePolicy().getName(props.name!, "record");
55
+
56
+ const thisRecordSymbol = new CSharpOutputSymbol(name, {
57
+ refkeys: props.refkey,
58
+ });
59
+
60
+ // this creates a new scope for the record definition.
61
+ // members will automatically "inherit" this scope so
62
+ // that refkeys to them will produce the fully-qualified
63
+ // name e.g. Foo.Bar.
64
+ const thisRecordScope = new CSharpMemberScope("record-decl", {
65
+ owner: thisRecordSymbol,
66
+ });
67
+
68
+ let typeParams: core.Children;
69
+ if (props.typeParameters) {
70
+ const typeParamNames = new Array<string>();
71
+ for (const entry of Object.entries(props.typeParameters)) {
72
+ typeParamNames.push(
73
+ useCSharpNamePolicy().getName(entry[0], "type-parameter"),
74
+ );
75
+ // create a symbol for each type param so its
76
+ // refkey resolves to the type param's name
77
+ new CSharpOutputSymbol(entry[0], {
78
+ scope: thisRecordScope,
79
+ refkeys: entry[1],
80
+ });
81
+ }
82
+ typeParams = (
83
+ <group>
84
+ {"<"}
85
+ <core.For each={typeParamNames} comma line>
86
+ {(name) => name}
87
+ </core.For>
88
+ {">"}
89
+ </group>
90
+ );
91
+ }
92
+
93
+ const modifiers = computeModifiersPrefix([
94
+ getAccessModifier(props),
95
+ getRecordModifiers(props),
96
+ ]);
97
+ return (
98
+ <core.Declaration symbol={thisRecordSymbol}>
99
+ <DocWhen doc={props.doc} />
100
+ {modifiers}record <Name />
101
+ {typeParams}
102
+ {props.children ?
103
+ <core.Block newline>
104
+ <core.Scope value={thisRecordScope}>{props.children}</core.Scope>
105
+ </core.Block>
106
+ : ";"}
107
+ </core.Declaration>
108
+ );
109
+ }
@@ -9,6 +9,7 @@ export type CSharpElements =
9
9
  | "enum-member"
10
10
  | "function"
11
11
  | "interface"
12
+ | "record"
12
13
  | "class-member-private"
13
14
  | "class-member-public"
14
15
  | "class-method"
@@ -24,6 +25,7 @@ export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
24
25
  case "enum":
25
26
  case "enum-member":
26
27
  case "interface":
28
+ case "record":
27
29
  case "class-member-public":
28
30
  case "class-method":
29
31
  case "type-parameter":
package/temp/api.json CHANGED
@@ -2221,7 +2221,7 @@
2221
2221
  },
2222
2222
  {
2223
2223
  "kind": "Content",
2224
- "text": "\"class\" | \"constant\" | \"enum\" | \"enum-member\" | \"function\" | \"interface\" | \"class-member-private\" | \"class-member-public\" | \"class-method\" | \"class-property\" | \"parameter\" | \"type-parameter\""
2224
+ "text": "\"class\" | \"constant\" | \"enum\" | \"enum-member\" | \"function\" | \"interface\" | \"record\" | \"class-member-private\" | \"class-member-public\" | \"class-method\" | \"class-property\" | \"parameter\" | \"type-parameter\""
2225
2225
  },
2226
2226
  {
2227
2227
  "kind": "Content",
@@ -6189,7 +6189,7 @@
6189
6189
  {
6190
6190
  "kind": "Function",
6191
6191
  "canonicalReference": "@alloy-js/csharp!Parameter:function(1)",
6192
- "docComment": "",
6192
+ "docComment": "/**\n * Define a parameter to be used in class or interface method.\n */\n",
6193
6193
  "excerptTokens": [
6194
6194
  {
6195
6195
  "kind": "Content",
@@ -6248,6 +6248,34 @@
6248
6248
  "name": "ParameterProps",
6249
6249
  "preserveMemberOrder": false,
6250
6250
  "members": [
6251
+ {
6252
+ "kind": "PropertySignature",
6253
+ "canonicalReference": "@alloy-js/csharp!ParameterProps#default:member",
6254
+ "docComment": "/**\n * Default value for the parameter\n */\n",
6255
+ "excerptTokens": [
6256
+ {
6257
+ "kind": "Content",
6258
+ "text": "default?: "
6259
+ },
6260
+ {
6261
+ "kind": "Reference",
6262
+ "text": "core.Children",
6263
+ "canonicalReference": "@alloy-js/core!Children:type"
6264
+ },
6265
+ {
6266
+ "kind": "Content",
6267
+ "text": ";"
6268
+ }
6269
+ ],
6270
+ "isReadonly": false,
6271
+ "isOptional": true,
6272
+ "releaseTag": "Public",
6273
+ "name": "default",
6274
+ "propertyTypeTokenRange": {
6275
+ "startIndex": 1,
6276
+ "endIndex": 2
6277
+ }
6278
+ },
6251
6279
  {
6252
6280
  "kind": "PropertySignature",
6253
6281
  "canonicalReference": "@alloy-js/csharp!ParameterProps#name:member",
@@ -6275,6 +6303,33 @@
6275
6303
  "endIndex": 2
6276
6304
  }
6277
6305
  },
6306
+ {
6307
+ "kind": "PropertySignature",
6308
+ "canonicalReference": "@alloy-js/csharp!ParameterProps#optional:member",
6309
+ "docComment": "/**\n * If the parmaeter is optional(without default value)\n */\n",
6310
+ "excerptTokens": [
6311
+ {
6312
+ "kind": "Content",
6313
+ "text": "optional?: "
6314
+ },
6315
+ {
6316
+ "kind": "Content",
6317
+ "text": "boolean"
6318
+ },
6319
+ {
6320
+ "kind": "Content",
6321
+ "text": ";"
6322
+ }
6323
+ ],
6324
+ "isReadonly": false,
6325
+ "isOptional": true,
6326
+ "releaseTag": "Public",
6327
+ "name": "optional",
6328
+ "propertyTypeTokenRange": {
6329
+ "startIndex": 1,
6330
+ "endIndex": 2
6331
+ }
6332
+ },
6278
6333
  {
6279
6334
  "kind": "PropertySignature",
6280
6335
  "canonicalReference": "@alloy-js/csharp!ParameterProps#refkey:member",
@@ -6433,15 +6488,6 @@
6433
6488
  "kind": "Content",
6434
6489
  "text": "parameters: "
6435
6490
  },
6436
- {
6437
- "kind": "Reference",
6438
- "text": "Array",
6439
- "canonicalReference": "!Array:interface"
6440
- },
6441
- {
6442
- "kind": "Content",
6443
- "text": "<"
6444
- },
6445
6491
  {
6446
6492
  "kind": "Reference",
6447
6493
  "text": "ParameterProps",
@@ -6449,7 +6495,7 @@
6449
6495
  },
6450
6496
  {
6451
6497
  "kind": "Content",
6452
- "text": ">"
6498
+ "text": "[]"
6453
6499
  },
6454
6500
  {
6455
6501
  "kind": "Content",
@@ -6462,7 +6508,7 @@
6462
6508
  "name": "parameters",
6463
6509
  "propertyTypeTokenRange": {
6464
6510
  "startIndex": 1,
6465
- "endIndex": 5
6511
+ "endIndex": 3
6466
6512
  }
6467
6513
  }
6468
6514
  ],