@alloy-js/csharp 0.19.0-dev.1 → 0.19.0-dev.2

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 (32) hide show
  1. package/dist/src/components/ClassMethod.d.ts +14 -0
  2. package/dist/src/components/ClassMethod.d.ts.map +1 -1
  3. package/dist/src/components/ClassMethod.js +1 -1
  4. package/dist/src/components/index.d.ts +1 -0
  5. package/dist/src/components/index.d.ts.map +1 -1
  6. package/dist/src/components/index.js +1 -0
  7. package/dist/src/components/property/property.d.ts.map +1 -1
  8. package/dist/src/components/property/property.js +1 -1
  9. package/dist/src/components/stc/index.d.ts +1 -0
  10. package/dist/src/components/stc/index.d.ts.map +1 -1
  11. package/dist/src/components/stc/index.js +2 -1
  12. package/dist/src/components/struct/declaration.d.ts +68 -0
  13. package/dist/src/components/struct/declaration.d.ts.map +1 -0
  14. package/dist/src/components/struct/declaration.js +78 -0
  15. package/dist/src/components/struct/declaration.test.d.ts +2 -0
  16. package/dist/src/components/struct/declaration.test.d.ts.map +1 -0
  17. package/dist/src/components/struct/declaration.test.js +171 -0
  18. package/dist/src/name-policy.d.ts +1 -1
  19. package/dist/src/name-policy.d.ts.map +1 -1
  20. package/dist/src/name-policy.js +1 -0
  21. package/dist/test/class-method.test.js +23 -0
  22. package/dist/tsconfig.tsbuildinfo +1 -1
  23. package/package.json +2 -2
  24. package/src/components/ClassMethod.tsx +23 -1
  25. package/src/components/index.ts +1 -0
  26. package/src/components/property/property.tsx +3 -1
  27. package/src/components/stc/index.ts +1 -0
  28. package/src/components/struct/declaration.test.tsx +137 -0
  29. package/src/components/struct/declaration.tsx +129 -0
  30. package/src/name-policy.ts +2 -0
  31. package/temp/api.json +419 -1
  32. package/test/class-method.test.tsx +18 -0
@@ -0,0 +1,137 @@
1
+ import { List, refkey } from "@alloy-js/core";
2
+ import { describe, expect, it } from "vitest";
3
+ import { TestNamespace } from "../../../test/utils.jsx";
4
+ import { Attribute } from "../attributes/attributes.jsx";
5
+ import { Property } from "../property/property.jsx";
6
+ import { SourceFile } from "../SourceFile.jsx";
7
+ import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
8
+ import { StructDeclaration } from "./declaration.jsx";
9
+
10
+ it("declares struct with no members", () => {
11
+ expect(
12
+ <TestNamespace>
13
+ <StructDeclaration name="Test" />
14
+ </TestNamespace>,
15
+ ).toRenderTo(`
16
+ struct Test;
17
+ `);
18
+ });
19
+
20
+ describe("modifiers", () => {
21
+ it.each(["public", "private", "internal"])("%s", (mod) => {
22
+ expect(
23
+ <TestNamespace>
24
+ <StructDeclaration {...{ [mod]: true }} name="Test" />
25
+ </TestNamespace>,
26
+ ).toRenderTo(`
27
+ ${mod} struct Test;
28
+ `);
29
+ });
30
+
31
+ it.each(["partial"])("%s", (mod) => {
32
+ expect(
33
+ <TestNamespace>
34
+ <StructDeclaration {...{ [mod]: true }} name="Test" />
35
+ </TestNamespace>,
36
+ ).toRenderTo(`
37
+ ${mod} struct Test;
38
+ `);
39
+ });
40
+
41
+ it("combines modifiers", () => {
42
+ expect(
43
+ <TestNamespace>
44
+ <StructDeclaration public partial name="Test" />
45
+ </TestNamespace>,
46
+ ).toRenderTo(`
47
+ public partial struct Test;
48
+ `);
49
+ });
50
+ });
51
+
52
+ it("specify doc comment", () => {
53
+ expect(
54
+ <TestNamespace>
55
+ <StructDeclaration name="Test" doc="This is a test" />
56
+ </TestNamespace>,
57
+ ).toRenderTo(`
58
+ /// This is a test
59
+ struct Test;
60
+ `);
61
+ });
62
+
63
+ describe("with type parameters", () => {
64
+ it("reference parameters", () => {
65
+ const typeParameters: TypeParameterProps[] = [
66
+ {
67
+ name: "T",
68
+ refkey: refkey(),
69
+ },
70
+ {
71
+ name: "U",
72
+ refkey: refkey(),
73
+ },
74
+ ];
75
+
76
+ expect(
77
+ <TestNamespace>
78
+ <SourceFile path="Test.cs">
79
+ <StructDeclaration public name="Test" typeParameters={typeParameters}>
80
+ <List>
81
+ <Property name="PropA" type={typeParameters[0].refkey} get set />
82
+ <Property name="PropB" type={typeParameters[1].refkey} get set />
83
+ </List>
84
+ </StructDeclaration>
85
+ </SourceFile>
86
+ </TestNamespace>,
87
+ ).toRenderTo(`
88
+ namespace TestCode
89
+ {
90
+ public struct Test<T, U>
91
+ {
92
+ T PropA { get; set; }
93
+ U PropB { get; set; }
94
+ }
95
+ }
96
+ `);
97
+ });
98
+
99
+ it("defines with constraints", () => {
100
+ const typeParameters: TypeParameterProps[] = [
101
+ {
102
+ name: "T",
103
+ constraints: "IFoo",
104
+ },
105
+ {
106
+ name: "U",
107
+ constraints: "IBar",
108
+ },
109
+ ];
110
+
111
+ expect(
112
+ <TestNamespace>
113
+ <StructDeclaration public name="Test" typeParameters={typeParameters}>
114
+ // Body
115
+ </StructDeclaration>
116
+ </TestNamespace>,
117
+ ).toRenderTo(`
118
+ public struct Test<T, U>
119
+ where T : IFoo
120
+ where U : IBar
121
+ {
122
+ // Body
123
+ }
124
+ `);
125
+ });
126
+ });
127
+
128
+ it("specify attributes", () => {
129
+ expect(
130
+ <TestNamespace>
131
+ <StructDeclaration name="Test" attributes={[<Attribute name="Test" />]} />
132
+ </TestNamespace>,
133
+ ).toRenderTo(`
134
+ [Test]
135
+ struct Test;
136
+ `);
137
+ });
@@ -0,0 +1,129 @@
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 { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
12
+ import { DocWhen } from "../doc/comment.jsx";
13
+ import { Name } from "../Name.jsx";
14
+ import { TypeParameterConstraints } from "../type-parameters/type-parameter-constraints.jsx";
15
+ import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
16
+ import { TypeParameters } from "../type-parameters/type-parameters.jsx";
17
+
18
+ export interface StructModifiers {
19
+ readonly new?: boolean;
20
+ readonly readonly?: boolean;
21
+ readonly ref?: boolean;
22
+ readonly partial?: boolean;
23
+ }
24
+
25
+ const getStructModifiers = makeModifiers<StructModifiers>([
26
+ "new",
27
+ "readonly",
28
+ "ref",
29
+ "partial",
30
+ ]);
31
+
32
+ // properties for creating a class
33
+ export interface StructDeclarationProps
34
+ extends Omit<core.DeclarationProps, "nameKind">,
35
+ AccessModifiers,
36
+ StructModifiers {
37
+ name: string;
38
+
39
+ /** Doc comment */
40
+ doc?: core.Children;
41
+ refkey?: core.Refkey;
42
+
43
+ /**
44
+ * Type parameters for the struct
45
+ *
46
+ * @example
47
+ * ```tsx
48
+ * <StructDeclaration name="IList" typeParameters={["T"]} />
49
+ * ```
50
+ * This will produce:
51
+ * ```csharp
52
+ * public struct IList<T>
53
+ * ```
54
+ */
55
+ typeParameters?: (TypeParameterProps | string)[];
56
+
57
+ /**
58
+ * Define attributes to attach
59
+ * @example
60
+ * ```tsx
61
+ * <StructDeclaration name="MyStruct" attributes={[
62
+ * <Attribute name="Test" />
63
+ * <Attribute name="Test2" args={["arg1", "arg2"]} />
64
+ * ]} />
65
+ * ```
66
+ * This will produce:
67
+ * ```csharp
68
+ * [Test]
69
+ * [Test2("arg1", "arg2")]
70
+ * public struct MyStruct
71
+ * ```
72
+ */
73
+ attributes?: AttributesProp;
74
+ }
75
+
76
+ /**
77
+ * CSharp struct declaration.
78
+ * @example
79
+ * ```tsx
80
+ * <StructDeclaration public name="IMyStruct">
81
+ * <StructMember public name="MyProperty" type="int" />
82
+ * <StructMethod public name="MyMethod" returnType="void">
83
+ * <Parameter name="value" type="int" />
84
+ * </StructMethod>
85
+ * </StructDeclaration>
86
+ * ```
87
+ * This will produce:
88
+ * ```csharp
89
+ * public struct MyIface
90
+ * {
91
+ * public int MyProperty { get; set; }
92
+ * public void MyMethod(int value);
93
+ * }
94
+ * ```
95
+ */
96
+ export function StructDeclaration(props: StructDeclarationProps) {
97
+ const name = useCSharpNamePolicy().getName(props.name!, "struct");
98
+
99
+ const thisStructSymbol = new CSharpOutputSymbol(name, {
100
+ refkeys: props.refkey,
101
+ });
102
+
103
+ const thisStructScope = new CSharpMemberScope("struct-decl", {
104
+ owner: thisStructSymbol,
105
+ });
106
+
107
+ const modifiers = computeModifiersPrefix([
108
+ getAccessModifier(props),
109
+ getStructModifiers(props),
110
+ ]);
111
+ return (
112
+ <core.Declaration symbol={thisStructSymbol}>
113
+ <DocWhen doc={props.doc} />
114
+ <AttributeList attributes={props.attributes} endline />
115
+ {modifiers}struct <Name />
116
+ {props.typeParameters && (
117
+ <TypeParameters parameters={props.typeParameters} />
118
+ )}
119
+ {props.typeParameters && (
120
+ <TypeParameterConstraints parameters={props.typeParameters} />
121
+ )}
122
+ {props.children ?
123
+ <core.Block newline>
124
+ <core.Scope value={thisStructScope}>{props.children}</core.Scope>
125
+ </core.Block>
126
+ : ";"}
127
+ </core.Declaration>
128
+ );
129
+ }
@@ -6,6 +6,7 @@ export type CSharpElements =
6
6
  | "class"
7
7
  | "constant"
8
8
  | "variable"
9
+ | "struct"
9
10
  | "enum"
10
11
  | "enum-member"
11
12
  | "function"
@@ -23,6 +24,7 @@ export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
23
24
  return core.createNamePolicy((name, element) => {
24
25
  switch (element) {
25
26
  case "class":
27
+ case "struct":
26
28
  case "enum":
27
29
  case "enum-member":
28
30
  case "interface":