@alloy-js/csharp 0.18.0-dev.22 → 0.18.0-dev.24

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 (52) hide show
  1. package/dist/src/components/ClassDeclaration.d.ts +18 -0
  2. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  3. package/dist/src/components/ClassDeclaration.js +6 -0
  4. package/dist/src/components/ClassMethod.d.ts +18 -0
  5. package/dist/src/components/ClassMethod.d.ts.map +1 -1
  6. package/dist/src/components/ClassMethod.js +6 -0
  7. package/dist/src/components/SourceFile.d.ts.map +1 -1
  8. package/dist/src/components/SourceFile.js +1 -0
  9. package/dist/src/components/attributes/attributes.d.ts +39 -0
  10. package/dist/src/components/attributes/attributes.d.ts.map +1 -0
  11. package/dist/src/components/attributes/attributes.js +62 -0
  12. package/dist/src/components/attributes/attributes.test.d.ts +2 -0
  13. package/dist/src/components/attributes/attributes.test.d.ts.map +1 -0
  14. package/dist/src/components/attributes/attributes.test.js +75 -0
  15. package/dist/src/components/index.d.ts +1 -0
  16. package/dist/src/components/index.d.ts.map +1 -1
  17. package/dist/src/components/index.js +1 -0
  18. package/dist/src/components/interface/declaration.d.ts +18 -0
  19. package/dist/src/components/interface/declaration.d.ts.map +1 -1
  20. package/dist/src/components/interface/declaration.js +6 -0
  21. package/dist/src/components/interface/declaration.test.js +18 -0
  22. package/dist/src/components/interface/method.d.ts +18 -0
  23. package/dist/src/components/interface/method.d.ts.map +1 -1
  24. package/dist/src/components/interface/method.js +6 -0
  25. package/dist/src/components/interface/method.test.js +21 -0
  26. package/dist/src/components/interface/property.d.ts +18 -0
  27. package/dist/src/components/interface/property.d.ts.map +1 -1
  28. package/dist/src/components/interface/property.js +6 -0
  29. package/dist/src/components/interface/property.test.js +24 -0
  30. package/dist/src/components/property/property.d.ts +18 -0
  31. package/dist/src/components/property/property.d.ts.map +1 -1
  32. package/dist/src/components/property/property.js +6 -0
  33. package/dist/src/components/property/property.test.js +24 -0
  34. package/dist/test/class-declaration.test.js +18 -0
  35. package/dist/tsconfig.tsbuildinfo +1 -1
  36. package/package.json +1 -1
  37. package/src/components/ClassDeclaration.tsx +20 -0
  38. package/src/components/ClassMethod.tsx +20 -0
  39. package/src/components/SourceFile.tsx +1 -0
  40. package/src/components/attributes/attributes.test.tsx +61 -0
  41. package/src/components/attributes/attributes.tsx +100 -0
  42. package/src/components/index.ts +1 -0
  43. package/src/components/interface/declaration.test.tsx +15 -0
  44. package/src/components/interface/declaration.tsx +20 -0
  45. package/src/components/interface/method.test.tsx +15 -0
  46. package/src/components/interface/method.tsx +20 -0
  47. package/src/components/interface/property.test.tsx +21 -0
  48. package/src/components/interface/property.tsx +20 -0
  49. package/src/components/property/property.test.tsx +21 -0
  50. package/src/components/property/property.tsx +20 -0
  51. package/temp/api.json +596 -0
  52. package/test/class-declaration.test.tsx +12 -0
@@ -58,6 +58,7 @@ export function SourceFile(props: SourceFileProps) {
58
58
  filetype="cs"
59
59
  reference={Reference}
60
60
  tabWidth={4}
61
+ printWidth={120}
61
62
  >
62
63
  <SourceFileContext.Provider value={sourceFileCtx}>
63
64
  <core.Scope name={props.path} kind="source-file">
@@ -0,0 +1,61 @@
1
+ import { expect, it } from "vitest";
2
+ import { Attribute, AttributeList } from "./attributes.jsx";
3
+
4
+ it("define attribute", () => {
5
+ expect(<Attribute name="Test" />).toRenderTo(`
6
+ [Test]
7
+ `);
8
+ });
9
+
10
+ it("define attribute with single arg", () => {
11
+ expect(<Attribute name="Test" args={[`"abc"`]} />).toRenderTo(`
12
+ [Test("abc")]
13
+ `);
14
+ });
15
+
16
+ it("define attribute with multiple arg", () => {
17
+ expect(<Attribute name="Test" args={[`"abc"`, `"def"`]} />).toRenderTo(`
18
+ [Test("abc", "def")]
19
+ `);
20
+ });
21
+
22
+ it("define attribute list with Attribute components", () => {
23
+ expect(
24
+ <AttributeList
25
+ attributes={[<Attribute name="TestA" />, <Attribute name="TestB" />]}
26
+ />,
27
+ ).toRenderTo(`
28
+ [TestA]
29
+ [TestB]
30
+ `);
31
+ });
32
+
33
+ it("define attribute list with attribute names", () => {
34
+ expect(<AttributeList attributes={["TestA", "TestB"]} />).toRenderTo(`
35
+ [TestA]
36
+ [TestB]
37
+ `);
38
+ });
39
+
40
+ it("define attribute list with attribute props", () => {
41
+ expect(
42
+ <AttributeList
43
+ attributes={[{ name: "TestA" }, { name: "TestB", args: [`"test"`] }]}
44
+ />,
45
+ ).toRenderTo(`
46
+ [TestA]
47
+ [TestB("test")]
48
+ `);
49
+ });
50
+
51
+ it("define attribute list with children", () => {
52
+ expect(
53
+ <AttributeList>
54
+ <Attribute name="TestA" />
55
+ <Attribute name="TestB" />
56
+ </AttributeList>,
57
+ ).toRenderTo(`
58
+ [TestA]
59
+ [TestB]
60
+ `);
61
+ });
@@ -0,0 +1,100 @@
1
+ import {
2
+ Children,
3
+ findKeyedChildren,
4
+ For,
5
+ Indent,
6
+ taggedComponent,
7
+ } from "@alloy-js/core";
8
+
9
+ export interface AttributeItem {
10
+ name: string;
11
+ args?: string[];
12
+ }
13
+
14
+ export type AttributesProp = Array<string | AttributeProps | Children>;
15
+
16
+ export interface AttributeListProps {
17
+ /** If the attribute list should finish with a hard line if there is any attribute */
18
+ endline?: boolean;
19
+ attributes?: AttributesProp;
20
+ children?: Children[];
21
+ }
22
+
23
+ /**
24
+ * Render each attributes in a new line.
25
+ */
26
+ export function AttributeList(props: AttributeListProps) {
27
+ const attributes =
28
+ props.attributes ??
29
+ (props.children && findKeyedChildren(props.children, Attribute.tag));
30
+
31
+ if (!attributes) {
32
+ return null;
33
+ }
34
+
35
+ return (
36
+ <>
37
+ <For each={attributes} line>
38
+ {(arg) => renderAttribute(arg)}
39
+ </For>
40
+ {props.endline && attributes.length > 0 && <hbr />}
41
+ </>
42
+ );
43
+ }
44
+
45
+ function renderAttribute(attr: string | AttributeProps | Children): Children {
46
+ if (typeof attr === "string") {
47
+ return <Attribute name={attr} />;
48
+ } else if (typeof attr === "object" && attr && "name" in attr) {
49
+ return <Attribute {...attr} />;
50
+ } else {
51
+ return attr;
52
+ }
53
+ }
54
+
55
+ export interface AttributeProps {
56
+ /** Attribute name */
57
+ name: Children;
58
+
59
+ /** Argument */
60
+ args?: Children[];
61
+ }
62
+
63
+ const AttributeTag = Symbol("AttributeTag");
64
+ /**
65
+ * Render a csharp attribute.
66
+ *
67
+ * @example
68
+ * ```tsx
69
+ * <Attribute name="Test" /><hbr/>
70
+ * <Attribute name="Test" args={["arg1", "arg2"]} />
71
+ * ```
72
+ *
73
+ * will render:
74
+ * ```csharp
75
+ * [Test]
76
+ * [Test("arg1", "arg2")]
77
+ * ```
78
+ */
79
+ export const Attribute = taggedComponent(
80
+ AttributeTag,
81
+ (props: AttributeProps) => {
82
+ return (
83
+ <group>
84
+ [{props.name}
85
+ {props.args && props.args.length > 0 && (
86
+ <>
87
+ (
88
+ <Indent softline>
89
+ <For each={props.args ?? []} comma line>
90
+ {(arg) => arg}
91
+ </For>
92
+ </Indent>
93
+ )
94
+ </>
95
+ )}
96
+ ]
97
+ </group>
98
+ );
99
+ },
100
+ );
@@ -1,3 +1,4 @@
1
+ export * from "./attributes/attributes.jsx";
1
2
  export * from "./ClassDeclaration.jsx";
2
3
  export * from "./ClassMethod.jsx";
3
4
  export * from "./Declaration.js";
@@ -1,6 +1,7 @@
1
1
  import { List, refkey } from "@alloy-js/core";
2
2
  import { describe, expect, it } from "vitest";
3
3
  import { TestNamespace } from "../../../test/utils.jsx";
4
+ import { Attribute } from "../attributes/attributes.jsx";
4
5
  import { SourceFile } from "../SourceFile.jsx";
5
6
  import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
6
7
  import { InterfaceDeclaration } from "./declaration.jsx";
@@ -141,3 +142,17 @@ describe("with type parameters", () => {
141
142
  `);
142
143
  });
143
144
  });
145
+
146
+ it("specify attributes", () => {
147
+ expect(
148
+ <TestNamespace>
149
+ <InterfaceDeclaration
150
+ name="Test"
151
+ attributes={[<Attribute name="Test" />]}
152
+ />
153
+ </TestNamespace>,
154
+ ).toRenderTo(`
155
+ [Test]
156
+ interface Test;
157
+ `);
158
+ });
@@ -8,6 +8,7 @@ import {
8
8
  import { useCSharpNamePolicy } from "../../name-policy.js";
9
9
  import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
10
10
  import { CSharpMemberScope } from "../../symbols/scopes.js";
11
+ import { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
11
12
  import { DocWhen } from "../doc/comment.jsx";
12
13
  import { Name } from "../Name.jsx";
13
14
  import { TypeParameterConstraints } from "../type-parameters/type-parameter-constraints.jsx";
@@ -44,6 +45,24 @@ export interface InterfaceDeclarationProps
44
45
  * ```
45
46
  */
46
47
  typeParameters?: (TypeParameterProps | string)[];
48
+
49
+ /**
50
+ * Define attributes to attach
51
+ * @example
52
+ * ```tsx
53
+ * <InterfaceDeclaration name="MyInterface" attributes={[
54
+ * <Attribute name="Test" />
55
+ * <Attribute name="Test2" args={["arg1", "arg2"]} />
56
+ * ]} />
57
+ * ```
58
+ * This will produce:
59
+ * ```csharp
60
+ * [Test]
61
+ * [Test2("arg1", "arg2")]
62
+ * public interface MyInterface
63
+ * ```
64
+ */
65
+ attributes?: AttributesProp;
47
66
  }
48
67
 
49
68
  /**
@@ -88,6 +107,7 @@ export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
88
107
  return (
89
108
  <core.Declaration symbol={thisInterfaceSymbol}>
90
109
  <DocWhen doc={props.doc} />
110
+ <AttributeList attributes={props.attributes} endline />
91
111
  {modifiers}interface <Name />
92
112
  {props.typeParameters && (
93
113
  <TypeParameters parameters={props.typeParameters} />
@@ -2,6 +2,7 @@ import { refkey } from "@alloy-js/core";
2
2
  import { Children } from "@alloy-js/core/jsx-runtime";
3
3
  import { describe, expect, it } from "vitest";
4
4
  import { TestNamespace } from "../../../test/utils.jsx";
5
+ import { Attribute } from "../attributes/attributes.jsx";
5
6
  import { SourceFile } from "../SourceFile.jsx";
6
7
  import { TypeParameterProps } from "../type-parameters/type-parameter.jsx";
7
8
  import { InterfaceDeclaration } from "./declaration.jsx";
@@ -174,6 +175,20 @@ it("specify doc comment", () => {
174
175
  `);
175
176
  });
176
177
 
178
+ it("specify attributes", () => {
179
+ expect(
180
+ <Wrapper>
181
+ <InterfaceMethod name="Test" attributes={[<Attribute name="Test" />]} />
182
+ </Wrapper>,
183
+ ).toRenderTo(`
184
+ public interface TestInterface
185
+ {
186
+ [Test]
187
+ void Test();
188
+ }
189
+ `);
190
+ });
191
+
177
192
  describe("with type parameters", () => {
178
193
  it("reference parameters", () => {
179
194
  const typeParameters: TypeParameterProps[] = [
@@ -15,6 +15,7 @@ import {
15
15
  import { useCSharpNamePolicy } from "../../name-policy.js";
16
16
  import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
17
17
  import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
18
+ import { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
18
19
  import { DocWhen } from "../doc/comment.jsx";
19
20
  import { ParameterProps, Parameters } from "../parameters/parameters.jsx";
20
21
  import { TypeParameterConstraints } from "../type-parameters/type-parameter-constraints.jsx";
@@ -53,6 +54,24 @@ export interface InterfaceMethodProps
53
54
 
54
55
  /** Doc comment */
55
56
  doc?: Children;
57
+
58
+ /**
59
+ * Define attributes to attach
60
+ * @example
61
+ * ```tsx
62
+ * <InterfaceMethod name="MyMethod" attributes={[
63
+ * <Attribute name="Test" />
64
+ * <Attribute name="Test2" args={["arg1", "arg2"]} />
65
+ * ]} />
66
+ * ```
67
+ * This will produce:
68
+ * ```csharp
69
+ * [Test]
70
+ * [Test2("arg1", "arg2")]
71
+ * void MyMethod();
72
+ * ```
73
+ */
74
+ attributes?: AttributesProp;
56
75
  }
57
76
 
58
77
  // a C# interface method
@@ -84,6 +103,7 @@ export function InterfaceMethod(props: InterfaceMethodProps) {
84
103
  <MemberDeclaration symbol={methodSymbol}>
85
104
  <Scope value={methodScope}>
86
105
  <DocWhen doc={props.doc} />
106
+ <AttributeList attributes={props.attributes} endline />
87
107
  {modifiers}
88
108
  {props.returns ?? "void"} {name}
89
109
  {props.typeParameters && (
@@ -1,6 +1,7 @@
1
1
  import { Children } from "@alloy-js/core/jsx-runtime";
2
2
  import { describe, expect, it } from "vitest";
3
3
  import { TestNamespace } from "../../../test/utils.jsx";
4
+ import { Attribute } from "../attributes/attributes.jsx";
4
5
  import { InterfaceDeclaration } from "./declaration.jsx";
5
6
  import { InterfaceProperty } from "./property.jsx";
6
7
 
@@ -142,3 +143,23 @@ it("specify doc comment", () => {
142
143
  }
143
144
  `);
144
145
  });
146
+
147
+ it("specify attributes", () => {
148
+ expect(
149
+ <Wrapper>
150
+ <InterfaceProperty
151
+ name="Test"
152
+ type="int"
153
+ attributes={[<Attribute name="Test" />]}
154
+ get
155
+ set
156
+ />
157
+ </Wrapper>,
158
+ ).toRenderTo(`
159
+ public interface TestInterface
160
+ {
161
+ [Test]
162
+ int Test { get; set; }
163
+ }
164
+ `);
165
+ });
@@ -16,6 +16,7 @@ import {
16
16
  import { useCSharpNamePolicy } from "../../name-policy.js";
17
17
  import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
18
18
  import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
19
+ import { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
19
20
  import { DocWhen } from "../doc/comment.jsx";
20
21
 
21
22
  /** Method modifiers. Can only be one. */
@@ -53,6 +54,24 @@ export interface InterfacePropertyProps
53
54
  * ```
54
55
  */
55
56
  nullable?: boolean;
57
+
58
+ /**
59
+ * Define attributes to attach
60
+ * @example
61
+ * ```tsx
62
+ * <InterfaceProperty name="MyProp" attributes={[
63
+ * <Attribute name="Test" />
64
+ * <Attribute name="Test2" args={["arg1", "arg2"]} />
65
+ * ]} />
66
+ * ```
67
+ * This will produce:
68
+ * ```csharp
69
+ * [Test]
70
+ * [Test2("arg1", "arg2")]
71
+ * int MyProp { get; set; }
72
+ * ```
73
+ */
74
+ attributes?: AttributesProp;
56
75
  }
57
76
 
58
77
  /**
@@ -92,6 +111,7 @@ export function InterfaceProperty(props: InterfacePropertyProps) {
92
111
  <MemberDeclaration symbol={propertySymbol}>
93
112
  <Scope value={propertyScope}>
94
113
  <DocWhen doc={props.doc} />
114
+ <AttributeList attributes={props.attributes} endline />
95
115
  {modifiers}
96
116
  {props.type}
97
117
  {props.nullable && "?"} {name}{" "}
@@ -1,6 +1,7 @@
1
1
  import { Children } from "@alloy-js/core/jsx-runtime";
2
2
  import { describe, expect, it } from "vitest";
3
3
  import { TestNamespace } from "../../../test/utils.jsx";
4
+ import { Attribute } from "../attributes/attributes.jsx";
4
5
  import { ClassDeclaration } from "../ClassDeclaration.jsx";
5
6
  import { Property } from "./property.jsx";
6
7
 
@@ -186,3 +187,23 @@ it("specify initializer", () => {
186
187
  }
187
188
  `);
188
189
  });
190
+
191
+ it("specify attributes", () => {
192
+ expect(
193
+ <Wrapper>
194
+ <Property
195
+ name="Test"
196
+ type="int"
197
+ attributes={[<Attribute name="Test" />]}
198
+ get
199
+ set
200
+ />
201
+ </Wrapper>,
202
+ ).toRenderTo(`
203
+ public class TestClass
204
+ {
205
+ [Test]
206
+ int Test { get; set; }
207
+ }
208
+ `);
209
+ });
@@ -17,6 +17,7 @@ import {
17
17
  import { useCSharpNamePolicy } from "../../name-policy.js";
18
18
  import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
19
19
  import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
20
+ import { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
20
21
  import { DocWhen } from "../doc/comment.jsx";
21
22
 
22
23
  /** Property modifiers. */
@@ -87,6 +88,24 @@ export interface PropertyProps extends AccessModifiers, PropertyModifiers {
87
88
  * ```
88
89
  */
89
90
  initializer?: Children;
91
+
92
+ /**
93
+ * Define attributes to attach
94
+ * @example
95
+ * ```tsx
96
+ * <Property name="MyProp" attributes={[
97
+ * <Attribute name="Test" />
98
+ * <Attribute name="Test2" args={["arg1", "arg2"]} />
99
+ * ]} />
100
+ * ```
101
+ * This will produce:
102
+ * ```csharp
103
+ * [Test]
104
+ * [Test2("arg1", "arg2")]
105
+ * int MyProp { get; set; }
106
+ * ```
107
+ */
108
+ attributes?: AttributesProp;
90
109
  }
91
110
 
92
111
  /**
@@ -135,6 +154,7 @@ export function Property(props: PropertyProps) {
135
154
  <MemberDeclaration symbol={propertySymbol}>
136
155
  <Scope value={propertyScope}>
137
156
  <DocWhen doc={props.doc} />
157
+ <AttributeList attributes={props.attributes} endline />
138
158
  {modifiers}
139
159
  {props.type}
140
160
  {props.nullable && "?"} {name}{" "}