@alloy-js/csharp 0.19.0-dev.5 → 0.19.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.
@@ -1,6 +1,13 @@
1
1
  import { Children, Refkey } from "@alloy-js/core";
2
2
  import { AccessModifiers } from "../../modifiers.js";
3
- export interface FieldProps extends AccessModifiers {
3
+ /** Field modifiers. */
4
+ export interface FieldModifiers {
5
+ readonly new?: boolean;
6
+ readonly static?: boolean;
7
+ readonly readonly?: boolean;
8
+ readonly volatile?: boolean;
9
+ }
10
+ export interface FieldProps extends AccessModifiers, FieldModifiers {
4
11
  name: string;
5
12
  type: Children;
6
13
  refkey?: Refkey;
@@ -1 +1 @@
1
- {"version":3,"file":"field.d.ts","sourceRoot":"","sources":["../../../../src/components/field/field.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAA6B,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EACL,eAAe,EAGhB,MAAM,oBAAoB,CAAC;AAM5B,MAAM,WAAW,UAAW,SAAQ,eAAe;IACjD,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,QAAQ,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kBAAkB;IAClB,GAAG,CAAC,EAAE,QAAQ,CAAC;CAChB;AAED,wBAAwB;AACxB,wBAAgB,KAAK,CAAC,KAAK,EAAE,UAAU,YA6BtC"}
1
+ {"version":3,"file":"field.d.ts","sourceRoot":"","sources":["../../../../src/components/field/field.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAA6B,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EACL,eAAe,EAIhB,MAAM,oBAAoB,CAAC;AAM5B,uBAAuB;AACvB,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;CAC7B;AASD,MAAM,WAAW,UAAW,SAAQ,eAAe,EAAE,cAAc;IACjE,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,QAAQ,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kBAAkB;IAClB,GAAG,CAAC,EAAE,QAAQ,CAAC;CAChB;AAED,wBAAwB;AACxB,wBAAgB,KAAK,CAAC,KAAK,EAAE,UAAU,YAiCtC"}
@@ -1,14 +1,18 @@
1
1
  import { createComponent as _$createComponent, memo as _$memo } from "@alloy-js/core/jsx-runtime";
2
2
  import { Declaration, Name, refkey } from "@alloy-js/core";
3
- import { computeModifiersPrefix, getAccessModifier } from "../../modifiers.js";
3
+ import { computeModifiersPrefix, getAccessModifier, makeModifiers } from "../../modifiers.js";
4
4
  import { useCSharpNamePolicy } from "../../name-policy.js";
5
5
  import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
6
6
  import { useCSharpScope } from "../../symbols/scopes.js";
7
7
  import { DocWhen } from "../doc/comment.js";
8
+
9
+ /** Field modifiers. */
10
+
11
+ const getModifiers = makeModifiers(["new", "static", "readonly", "volatile"]);
8
12
  /** Render a c# field */
9
13
  export function Field(props) {
10
14
  let nameElement = "class-member-private";
11
- if (props.public) {
15
+ if (props.public || props.protected || props.internal) {
12
16
  nameElement = "class-member-public";
13
17
  }
14
18
  const name = useCSharpNamePolicy().getName(props.name, nameElement);
@@ -20,7 +24,7 @@ export function Field(props) {
20
24
  scope,
21
25
  refkeys: props.refkey ?? refkey(props.name)
22
26
  });
23
- const modifiers = computeModifiersPrefix([getAccessModifier(props)]);
27
+ const modifiers = computeModifiersPrefix([getAccessModifier(props), getModifiers(props)]);
24
28
  return _$createComponent(Declaration, {
25
29
  symbol: memberSymbol,
26
30
  get children() {
@@ -28,7 +32,7 @@ export function Field(props) {
28
32
  get doc() {
29
33
  return props.doc;
30
34
  }
31
- }), modifiers, _$memo(() => props.type), " ", _$createComponent(Name, {})];
35
+ }), modifiers, _$memo(() => props.type), " ", _$createComponent(Name, {}), ";"];
32
36
  }
33
37
  });
34
38
  }
@@ -1,29 +1,36 @@
1
- import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
- import { StatementList } from "@alloy-js/core";
3
- import { expect, it } from "vitest";
1
+ import { memo as _$memo, createComponent as _$createComponent, mergeProps as _$mergeProps } from "@alloy-js/core/jsx-runtime";
2
+ import { List } from "@alloy-js/core";
3
+ import { describe, expect, it } from "vitest";
4
4
  import { TestNamespace } from "../../../test/utils.js";
5
5
  import { ClassDeclaration } from "../ClassDeclaration.js";
6
6
  import { Field } from "./field.js";
7
- it("declares some field", () => {
8
- expect(_$createComponent(TestNamespace, {
7
+ function Wrapper(props) {
8
+ return _$createComponent(TestNamespace, {
9
9
  get children() {
10
10
  return _$createComponent(ClassDeclaration, {
11
11
  "public": true,
12
12
  name: "TestClass",
13
13
  get children() {
14
- return _$createComponent(StatementList, {
15
- get children() {
16
- return [_$createComponent(Field, {
17
- "public": true,
18
- name: "MemberOne",
19
- type: "string"
20
- }), _$createComponent(Field, {
21
- "private": true,
22
- name: "MemberTwo",
23
- type: "int"
24
- })];
25
- }
26
- });
14
+ return props.children;
15
+ }
16
+ });
17
+ }
18
+ });
19
+ }
20
+ it("declares multiple fields", () => {
21
+ expect(_$createComponent(Wrapper, {
22
+ get children() {
23
+ return _$createComponent(List, {
24
+ get children() {
25
+ return [_$createComponent(Field, {
26
+ "public": true,
27
+ name: "MemberOne",
28
+ type: "string"
29
+ }), _$createComponent(Field, {
30
+ "public": true,
31
+ name: "MemberTwo",
32
+ type: "int"
33
+ })];
27
34
  }
28
35
  });
29
36
  }
@@ -31,7 +38,106 @@ it("declares some field", () => {
31
38
  public class TestClass
32
39
  {
33
40
  public string MemberOne;
34
- private int memberTwo;
41
+ public int MemberTwo;
42
+ }
43
+ `);
44
+ });
45
+ describe("modifiers", () => {
46
+ describe("access modifiers", () => {
47
+ it.each(["public", "private", "protected", "internal"])("%s", accessModifier => {
48
+ expect(_$createComponent(Wrapper, {
49
+ get children() {
50
+ return _$createComponent(Field, _$mergeProps({
51
+ [accessModifier]: true
52
+ }, {
53
+ name: "TestProp",
54
+ type: "string"
55
+ }));
56
+ }
57
+ })).toRenderTo(`
58
+ public class TestClass
59
+ {
60
+ ${accessModifier} string ${accessModifier === "private" ? "_testProp" : "TestProp"};
61
+ }
62
+ `);
63
+ });
64
+ });
65
+ describe("modifiers", () => {
66
+ it.each(["new", "static", "readonly", "volatile"])("%s", methodModifier => {
67
+ expect(_$createComponent(Wrapper, {
68
+ get children() {
69
+ return _$createComponent(Field, _$mergeProps({
70
+ [methodModifier]: true
71
+ }, {
72
+ name: "TestField",
73
+ type: "string"
74
+ }));
75
+ }
76
+ })).toRenderTo(`
77
+ public class TestClass
78
+ {
79
+ ${methodModifier} string _testField;
80
+ }
81
+ `);
82
+ });
83
+ });
84
+ it("combine modifiers", () => {
85
+ expect(_$createComponent(Wrapper, {
86
+ get children() {
87
+ return _$createComponent(Field, {
88
+ "public": true,
89
+ "new": true,
90
+ name: "TestField",
91
+ type: "string"
92
+ });
93
+ }
94
+ })).toRenderTo(`
95
+ public class TestClass
96
+ {
97
+ public new string TestField;
98
+ }
99
+ `);
100
+ });
101
+ });
102
+ describe("naming", () => {
103
+ it("public field are PascalCase", () => {
104
+ expect(_$createComponent(Wrapper, {
105
+ get children() {
106
+ return _$createComponent(List, {
107
+ get children() {
108
+ return _$createComponent(Field, {
109
+ "public": true,
110
+ name: "member_one",
111
+ type: "string"
112
+ });
113
+ }
114
+ });
115
+ }
116
+ })).toRenderTo(`
117
+ public class TestClass
118
+ {
119
+ public string MemberOne;
120
+ }
121
+ `);
122
+ });
123
+ it("private field are camelCase with _ prefix", () => {
124
+ expect(_$createComponent(Wrapper, {
125
+ get children() {
126
+ return _$createComponent(List, {
127
+ get children() {
128
+ return _$createComponent(Field, {
129
+ "private": true,
130
+ name: "member_one",
131
+ type: "string"
132
+ });
133
+ }
134
+ });
135
+ }
136
+ })).toRenderTo(`
137
+ public class TestClass
138
+ {
139
+ private string _memberOne;
35
140
  }
36
141
  `);
142
+ });
37
143
  });
@@ -9,6 +9,9 @@ export interface MethodModifiers {
9
9
  readonly sealed?: boolean;
10
10
  readonly static?: boolean;
11
11
  readonly virtual?: boolean;
12
+ readonly override?: boolean;
13
+ readonly extern?: boolean;
14
+ readonly readonly?: boolean;
12
15
  }
13
16
  export interface MethodProps extends AccessModifiers, MethodModifiers {
14
17
  name: string;
@@ -1 +1 @@
1
- {"version":3,"file":"method.d.ts","sourceRoot":"","sources":["../../../../src/components/method/method.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,QAAQ,EAGR,MAAM,EAEP,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,eAAe,EAKhB,MAAM,oBAAoB,CAAC;AAI5B,OAAO,EAAiB,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAE7E,OAAO,EAAE,cAAc,EAAc,MAAM,8BAA8B,CAAC;AAE1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAG3E,yCAAyC;AACzC,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;CAC5B;AAUD,MAAM,WAAW,WAAY,SAAQ,eAAe,EAAE,eAAe;IACnE,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,UAAU,CAAC,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;IACnC,OAAO,CAAC,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IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,UAAU,CAAC,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;IACnC,OAAO,CAAC,EAAE,QAAQ,CAAC;IAEnB;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB,kBAAkB;IAClB,GAAG,CAAC,EAAE,QAAQ,CAAC;IAEf;;;;;;;;;;;OAWG;IACH,cAAc,CAAC,EAAE,CAAC,kBAAkB,GAAG,MAAM,CAAC,EAAE,CAAC;IAEjD;;;;;;;;;;;;;;;OAeG;IACH,UAAU,CAAC,EAAE,cAAc,CAAC;IAE5B;;;;;;;;;;;;OAYG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAGD,wBAAgB,MAAM,CAAC,KAAK,EAAE,WAAW,YAoDxC"}
@@ -12,7 +12,7 @@ import { TypeParameters } from "../type-parameters/type-parameters.js";
12
12
 
13
13
  /** Method modifiers. Can only be one. */
14
14
 
15
- const getMethodModifier = makeModifiers(["abstract", "sealed", "static", "virtual"]);
15
+ const getMethodModifier = makeModifiers(["abstract", "sealed", "static", "virtual", "override", "extern", "readonly"]);
16
16
 
17
17
  // properties for creating a method
18
18
 
@@ -62,7 +62,11 @@ export function Method(props) {
62
62
  get parameters() {
63
63
  return props.typeParameters;
64
64
  }
65
- })), _$memo(() => _$memo(() => !!props.abstract)() ? ";" : _$memo(() => !!props.expression)() ? [" => ", _$memo(() => props.children)] : _$createComponent(Block, {
65
+ })), _$memo(() => _$memo(() => !!props.abstract)() ? ";" : _$memo(() => !!props.expression)() ? _$createComponent(ExpressionBody, {
66
+ get children() {
67
+ return props.children;
68
+ }
69
+ }) : _$createComponent(Block, {
66
70
  newline: true,
67
71
  get children() {
68
72
  return props.children;
@@ -72,4 +76,7 @@ export function Method(props) {
72
76
  });
73
77
  }
74
78
  });
75
- }
79
+ }
80
+ const ExpressionBody = props => {
81
+ return [" => ", _$memo(() => props.children), ";"];
82
+ };
@@ -169,7 +169,7 @@ it("use expression body form", () => {
169
169
  name: "Method",
170
170
  doc: "This is a test",
171
171
  expression: true,
172
- children: "this.MyProperty.Value;"
172
+ children: "this.MyProperty.Value"
173
173
  });
174
174
  }
175
175
  });
@@ -1,5 +1,5 @@
1
1
  import { createComponent as _$createComponent, mergeProps as _$mergeProps } from "@alloy-js/core/jsx-runtime";
2
- import { List, refkey, StatementList } from "@alloy-js/core";
2
+ import { List, refkey } from "@alloy-js/core";
3
3
  import { describe, expect, it } from "vitest";
4
4
  import { TestNamespace } from "../../../test/utils.js";
5
5
  import { Attribute } from "../attributes/attributes.js";
@@ -217,14 +217,14 @@ it("defines fields", () => {
217
217
  "public": true,
218
218
  name: "TestClass",
219
219
  get children() {
220
- return _$createComponent(StatementList, {
220
+ return _$createComponent(List, {
221
221
  get children() {
222
222
  return [_$createComponent(Field, {
223
223
  "public": true,
224
224
  name: "MemberOne",
225
225
  type: "string"
226
226
  }), _$createComponent(Field, {
227
- "private": true,
227
+ "public": true,
228
228
  name: "MemberTwo",
229
229
  type: "int"
230
230
  })];
@@ -237,7 +237,7 @@ it("defines fields", () => {
237
237
  public struct TestClass
238
238
  {
239
239
  public string MemberOne;
240
- private int memberTwo;
240
+ public int MemberTwo;
241
241
  }
242
242
  `);
243
243
  });
@@ -1 +1 @@
1
- {"version":3,"file":"name-policy.d.ts","sourceRoot":"","sources":["../../src/name-policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,gBAAgB,CAAC;AAIvC,MAAM,MAAM,cAAc,GACtB,OAAO,GACP,UAAU,GACV,UAAU,GACV,QAAQ,GACR,MAAM,GACN,aAAa,GACb,UAAU,GACV,WAAW,GACX,QAAQ,GACR,sBAAsB,GACtB,qBAAqB,GACrB,cAAc,GACd,gBAAgB,GAChB,WAAW,GACX,gBAAgB,CAAC;AAGrB,wBAAgB,sBAAsB,IAAI,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,CAoBxE;AAGD,wBAAgB,mBAAmB,IAAI,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,CAErE"}
1
+ {"version":3,"file":"name-policy.d.ts","sourceRoot":"","sources":["../../src/name-policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,gBAAgB,CAAC;AAIvC,MAAM,MAAM,cAAc,GACtB,OAAO,GACP,UAAU,GACV,UAAU,GACV,QAAQ,GACR,MAAM,GACN,aAAa,GACb,UAAU,GACV,WAAW,GACX,QAAQ,GACR,sBAAsB,GACtB,qBAAqB,GACrB,cAAc,GACd,gBAAgB,GAChB,WAAW,GACX,gBAAgB,CAAC;AAGrB,wBAAgB,sBAAsB,IAAI,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,CAsBxE;AAGD,wBAAgB,mBAAmB,IAAI,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,CAErE"}
@@ -20,6 +20,8 @@ export function createCSharpNamePolicy() {
20
20
  return changecase.pascalCase(name);
21
21
  case "constant":
22
22
  return changecase.constantCase(name);
23
+ case "class-member-private":
24
+ return `_${changecase.camelCase(name)}`;
23
25
  default:
24
26
  return changecase.camelCase(name);
25
27
  }
@@ -106,14 +106,14 @@ it("declares class with some members", () => {
106
106
  "public": true,
107
107
  name: "TestClass",
108
108
  get children() {
109
- return _$createComponent(core.StatementList, {
109
+ return _$createComponent(List, {
110
110
  get children() {
111
111
  return [_$createComponent(Field, {
112
112
  "public": true,
113
113
  name: "MemberOne",
114
114
  type: "string"
115
115
  }), _$createComponent(Field, {
116
- "private": true,
116
+ "public": true,
117
117
  name: "MemberTwo",
118
118
  type: "int"
119
119
  })];
@@ -127,7 +127,7 @@ it("declares class with some members", () => {
127
127
  public class TestClass
128
128
  {
129
129
  public string MemberOne;
130
- private int memberTwo;
130
+ public int MemberTwo;
131
131
  }
132
132
  }
133
133
  `);
@@ -217,7 +217,7 @@ it("uses refkeys for members, params, and return type", () => {
217
217
  "private": true,
218
218
  name: "MemberOne",
219
219
  type: enumTypeRefkey
220
- }), ";", _$createIntrinsic("hbr", {}), _$createComponent(csharp.Method, {
220
+ }), _$createIntrinsic("hbr", {}), _$createComponent(csharp.Method, {
221
221
  "public": true,
222
222
  name: "MethodOne",
223
223
  parameters: params,
@@ -246,7 +246,7 @@ it("uses refkeys for members, params, and return type", () => {
246
246
  public class TestResult;
247
247
  public class TestClass
248
248
  {
249
- private TestEnum memberOne;
249
+ private TestEnum _memberOne;
250
250
  public TestResult MethodOne(int intParam, TestInput bodyParam)
251
251
  {
252
252
  return new TestResult();
@@ -393,12 +393,12 @@ it("declares class with constructor params and assigns values to fields", () =>
393
393
  name: "name",
394
394
  type: "string",
395
395
  refkey: thisNameRefkey
396
- }), ";", _$createIntrinsic("hbr", {}), _$createComponent(Field, {
396
+ }), _$createIntrinsic("hbr", {}), _$createComponent(Field, {
397
397
  "private": true,
398
398
  name: "size",
399
399
  type: "int",
400
400
  refkey: thisSizeRefkey
401
- }), ";", _$createIntrinsic("hbr", {}), _$createComponent(Constructor, {
401
+ }), _$createIntrinsic("hbr", {}), _$createComponent(Constructor, {
402
402
  "public": true,
403
403
  parameters: ctorParams,
404
404
  get children() {
@@ -415,12 +415,12 @@ it("declares class with constructor params and assigns values to fields", () =>
415
415
  {
416
416
  public class TestClass
417
417
  {
418
- private string name;
419
- private int size;
418
+ private string _name;
419
+ private int _size;
420
420
  public TestClass(string name, int size)
421
421
  {
422
- name = name;
423
- size = size;
422
+ _name = name;
423
+ _size = size;
424
424
  }
425
425
  }
426
426
  }
@@ -460,7 +460,7 @@ it("supports class member doc comments", () => {
460
460
  class Test
461
461
  {
462
462
  /// This is a member
463
- public int Member
463
+ public int Member;
464
464
  }
465
465
  `);
466
466
  });