@alloy-js/csharp 0.19.0-dev.0 → 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 (40) 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/interface/declaration.test.js +5 -5
  8. package/dist/src/components/interface/method.test.js +4 -4
  9. package/dist/src/components/property/property.d.ts.map +1 -1
  10. package/dist/src/components/property/property.js +1 -1
  11. package/dist/src/components/stc/index.d.ts +1 -0
  12. package/dist/src/components/stc/index.d.ts.map +1 -1
  13. package/dist/src/components/stc/index.js +2 -1
  14. package/dist/src/components/struct/declaration.d.ts +68 -0
  15. package/dist/src/components/struct/declaration.d.ts.map +1 -0
  16. package/dist/src/components/struct/declaration.js +78 -0
  17. package/dist/src/components/struct/declaration.test.d.ts +2 -0
  18. package/dist/src/components/struct/declaration.test.d.ts.map +1 -0
  19. package/dist/src/components/struct/declaration.test.js +171 -0
  20. package/dist/src/components/var/declaration.test.js +2 -2
  21. package/dist/src/name-policy.d.ts +1 -1
  22. package/dist/src/name-policy.d.ts.map +1 -1
  23. package/dist/src/name-policy.js +1 -0
  24. package/dist/test/class-declaration.test.js +5 -5
  25. package/dist/test/class-method.test.js +23 -0
  26. package/dist/tsconfig.tsbuildinfo +1 -1
  27. package/package.json +3 -3
  28. package/src/components/ClassMethod.tsx +23 -1
  29. package/src/components/index.ts +1 -0
  30. package/src/components/interface/declaration.test.tsx +5 -5
  31. package/src/components/interface/method.test.tsx +4 -4
  32. package/src/components/property/property.tsx +3 -1
  33. package/src/components/stc/index.ts +1 -0
  34. package/src/components/struct/declaration.test.tsx +137 -0
  35. package/src/components/struct/declaration.tsx +129 -0
  36. package/src/components/var/declaration.test.tsx +2 -2
  37. package/src/name-policy.ts +2 -0
  38. package/temp/api.json +419 -1
  39. package/test/class-declaration.test.tsx +5 -5
  40. package/test/class-method.test.tsx +18 -0
@@ -224,10 +224,10 @@ describe("with type parameters", () => {
224
224
  ).toRenderTo(`
225
225
  namespace TestCode
226
226
  {
227
- public interface TestInterface
228
- {
229
- public T Test<T, U>(T paramA);
230
- }
227
+ public interface TestInterface
228
+ {
229
+ public T Test<T, U>(T paramA);
230
+ }
231
231
  }
232
232
  `);
233
233
  });
@@ -122,7 +122,9 @@ export function Property(props: PropertyProps) {
122
122
  const scope = useCSharpScope();
123
123
  if (
124
124
  scope.kind !== "member" ||
125
- (scope.name !== "class-decl" && scope.name !== "record-decl")
125
+ (scope.name !== "class-decl" &&
126
+ scope.name !== "record-decl" &&
127
+ scope.name !== "struct-decl")
126
128
  ) {
127
129
  throw new Error(
128
130
  "can't define an interface method outside of an interface scope",
@@ -11,3 +11,4 @@ export const Parameter = core.stc(base.Parameter);
11
11
  export const Parameters = core.stc(base.Parameters);
12
12
  export const ProjectDirectory = core.stc(base.ProjectDirectory);
13
13
  export const UsingDirective = core.stc(base.UsingDirective);
14
+ export const StructDeclaration = core.stc(base.StructDeclaration);
@@ -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
+ }
@@ -52,8 +52,8 @@ it("links refkey", () => {
52
52
  ).toRenderTo(`
53
53
  namespace TestCode
54
54
  {
55
- var testVar = 42;
56
- var testVar2 = testVar;
55
+ var testVar = 42;
56
+ var testVar2 = testVar;
57
57
  }
58
58
  `);
59
59
  });
@@ -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":