@alloy-js/csharp 0.17.0 → 0.18.0-dev.11
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.
- package/dist/src/components/ClassDeclaration.d.ts +57 -0
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
- package/dist/src/components/{Class.js → ClassDeclaration.js} +42 -53
- package/dist/src/components/ClassMethod.d.ts +25 -0
- package/dist/src/components/ClassMethod.d.ts.map +1 -0
- package/dist/src/components/ClassMethod.js +60 -0
- package/dist/src/components/EnumDeclaration.d.ts +34 -0
- package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
- package/dist/src/components/{Enum.js → EnumDeclaration.js} +25 -5
- package/dist/src/components/class/property.d.ts +55 -0
- package/dist/src/components/class/property.d.ts.map +1 -0
- package/dist/src/components/class/property.js +67 -0
- package/dist/src/components/class/property.test.d.ts +2 -0
- package/dist/src/components/class/property.test.d.ts.map +1 -0
- package/dist/src/components/class/property.test.js +201 -0
- package/dist/src/components/doc/comment.d.ts +70 -0
- package/dist/src/components/doc/comment.d.ts.map +1 -0
- package/dist/src/components/doc/comment.js +88 -0
- package/dist/src/components/doc/comment.test.d.ts +2 -0
- package/dist/src/components/doc/comment.test.d.ts.map +1 -0
- package/dist/src/components/doc/comment.test.js +348 -0
- package/dist/src/components/doc/from-markdown.d.ts +6 -0
- package/dist/src/components/doc/from-markdown.d.ts.map +1 -0
- package/dist/src/components/doc/from-markdown.js +58 -0
- package/dist/src/components/doc/from-markdown.test.d.ts +2 -0
- package/dist/src/components/doc/from-markdown.test.d.ts.map +1 -0
- package/dist/src/components/doc/from-markdown.test.js +83 -0
- package/dist/src/components/index.d.ts +9 -2
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +9 -2
- package/dist/src/components/interface/declaration.d.ts +34 -0
- package/dist/src/components/interface/declaration.d.ts.map +1 -0
- package/dist/src/components/interface/declaration.js +90 -0
- package/dist/src/components/interface/declaration.test.d.ts +2 -0
- package/dist/src/components/interface/declaration.test.d.ts.map +1 -0
- package/dist/src/components/interface/declaration.test.js +69 -0
- package/dist/src/components/interface/method.d.ts +18 -0
- package/dist/src/components/interface/method.d.ts.map +1 -0
- package/dist/src/components/interface/method.js +59 -0
- package/dist/src/components/interface/method.test.d.ts +2 -0
- package/dist/src/components/interface/method.test.d.ts.map +1 -0
- package/dist/src/components/interface/method.test.js +131 -0
- package/dist/src/components/interface/property.d.ts +38 -0
- package/dist/src/components/interface/property.d.ts.map +1 -0
- package/dist/src/components/interface/property.js +67 -0
- package/dist/src/components/interface/property.test.d.ts +2 -0
- package/dist/src/components/interface/property.test.d.ts.map +1 -0
- package/dist/src/components/interface/property.test.js +165 -0
- package/dist/src/components/stc/index.d.ts +2 -2
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +2 -2
- package/dist/src/modifiers.d.ts +13 -4
- package/dist/src/modifiers.d.ts.map +1 -1
- package/dist/src/modifiers.js +13 -27
- package/dist/src/name-policy.d.ts +1 -1
- package/dist/src/name-policy.d.ts.map +1 -1
- package/dist/src/name-policy.js +1 -0
- package/dist/test/class-declaration.test.d.ts +2 -0
- package/dist/test/class-declaration.test.d.ts.map +1 -0
- package/dist/test/{class.test.js → class-declaration.test.js} +123 -73
- package/dist/test/class-method.test.d.ts +2 -0
- package/dist/test/class-method.test.d.ts.map +1 -0
- package/dist/test/class-method.test.js +161 -0
- package/dist/test/enum.test.js +12 -12
- package/dist/test/namespace.test.js +8 -8
- package/dist/test/project-directory.test.d.ts +2 -0
- package/dist/test/project-directory.test.d.ts.map +1 -0
- package/dist/test/{projectdirectory.test.js → project-directory.test.js} +8 -8
- package/dist/test/sourcefile.test.js +4 -4
- package/dist/test/using.test.js +9 -9
- package/dist/test/utils.d.ts +3 -0
- package/dist/test/utils.d.ts.map +1 -1
- package/dist/test/utils.js +15 -0
- package/dist/test/vitest.setup.d.ts +2 -0
- package/dist/test/vitest.setup.d.ts.map +1 -0
- package/dist/test/vitest.setup.js +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -6
- package/src/components/{Class.tsx → ClassDeclaration.tsx} +68 -66
- package/src/components/ClassMethod.tsx +94 -0
- package/src/components/{Enum.tsx → EnumDeclaration.tsx} +30 -6
- package/src/components/class/property.test.tsx +180 -0
- package/src/components/class/property.tsx +135 -0
- package/src/components/doc/comment.test.tsx +337 -0
- package/src/components/doc/comment.tsx +152 -0
- package/src/components/doc/from-markdown.test.tsx +103 -0
- package/src/components/doc/from-markdown.tsx +58 -0
- package/src/components/index.ts +9 -2
- package/src/components/interface/declaration.test.tsx +56 -0
- package/src/components/interface/declaration.tsx +109 -0
- package/src/components/interface/method.test.tsx +120 -0
- package/src/components/interface/method.tsx +82 -0
- package/src/components/interface/property.test.tsx +144 -0
- package/src/components/interface/property.tsx +107 -0
- package/src/components/stc/index.ts +2 -2
- package/src/modifiers.ts +32 -37
- package/src/name-policy.ts +2 -0
- package/temp/api.json +4419 -567
- package/test/{class.test.tsx → class-declaration.test.tsx} +103 -99
- package/test/class-method.test.tsx +147 -0
- package/test/enum.test.tsx +11 -11
- package/test/namespace.test.tsx +4 -4
- package/test/{projectdirectory.test.tsx → project-directory.test.tsx} +4 -4
- package/test/sourcefile.test.tsx +2 -2
- package/test/using.test.tsx +9 -9
- package/test/utils.tsx +9 -0
- package/test/vitest.setup.ts +1 -0
- package/tsconfig.json +2 -1
- package/vitest.config.ts +3 -0
- package/dist/src/components/Class.d.ts +0 -36
- package/dist/src/components/Class.d.ts.map +0 -1
- package/dist/src/components/Enum.d.ts +0 -15
- package/dist/src/components/Enum.d.ts.map +0 -1
- package/dist/test/class.test.d.ts +0 -2
- package/dist/test/class.test.d.ts.map +0 -1
- package/dist/test/projectdirectory.test.d.ts +0 -2
- package/dist/test/projectdirectory.test.d.ts.map +0 -1
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { TestNamespace } from "../../../test/utils.jsx";
|
|
3
|
+
import { InterfaceDeclaration } from "./declaration.jsx";
|
|
4
|
+
|
|
5
|
+
it("declares class with no members", () => {
|
|
6
|
+
expect(
|
|
7
|
+
<TestNamespace>
|
|
8
|
+
<InterfaceDeclaration name="TestInterface" />
|
|
9
|
+
</TestNamespace>,
|
|
10
|
+
).toRenderTo(`
|
|
11
|
+
interface TestInterface;
|
|
12
|
+
`);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("modifiers", () => {
|
|
16
|
+
it.each(["public", "private", "internal"])("%s", (mod) => {
|
|
17
|
+
expect(
|
|
18
|
+
<TestNamespace>
|
|
19
|
+
<InterfaceDeclaration {...{ [mod]: true }} name="TestInterface" />
|
|
20
|
+
</TestNamespace>,
|
|
21
|
+
).toRenderTo(`
|
|
22
|
+
${mod} interface TestInterface;
|
|
23
|
+
`);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it.each(["partial"])("%s", (mod) => {
|
|
27
|
+
expect(
|
|
28
|
+
<TestNamespace>
|
|
29
|
+
<InterfaceDeclaration {...{ [mod]: true }} name="TestInterface" />
|
|
30
|
+
</TestNamespace>,
|
|
31
|
+
).toRenderTo(`
|
|
32
|
+
${mod} interface TestInterface;
|
|
33
|
+
`);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("combines modifiers", () => {
|
|
37
|
+
expect(
|
|
38
|
+
<TestNamespace>
|
|
39
|
+
<InterfaceDeclaration public partial name="TestInterface" />
|
|
40
|
+
</TestNamespace>,
|
|
41
|
+
).toRenderTo(`
|
|
42
|
+
public partial interface TestInterface;
|
|
43
|
+
`);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("specify doc comment", () => {
|
|
48
|
+
expect(
|
|
49
|
+
<TestNamespace>
|
|
50
|
+
<InterfaceDeclaration name="TestInterface" doc="This is a test" />
|
|
51
|
+
</TestNamespace>,
|
|
52
|
+
).toRenderTo(`
|
|
53
|
+
/// This is a test
|
|
54
|
+
interface TestInterface;
|
|
55
|
+
`);
|
|
56
|
+
});
|
|
@@ -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 InterfaceModifiers {
|
|
15
|
+
readonly partial?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const getInterfaceModifiers = makeModifiers<InterfaceModifiers>(["partial"]);
|
|
19
|
+
|
|
20
|
+
// properties for creating a class
|
|
21
|
+
export interface InterfaceDeclarationProps
|
|
22
|
+
extends Omit<core.DeclarationProps, "nameKind">,
|
|
23
|
+
AccessModifiers,
|
|
24
|
+
InterfaceModifiers {
|
|
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 interface declaration.
|
|
35
|
+
* @example
|
|
36
|
+
* ```tsx
|
|
37
|
+
* <InterfaceDeclaration public name="IMyInterface">
|
|
38
|
+
* <InterfaceMember public name="MyProperty" type="int" />
|
|
39
|
+
* <InterfaceMethod public name="MyMethod" returnType="void">
|
|
40
|
+
* <Parameter name="value" type="int" />
|
|
41
|
+
* </InterfaceMethod>
|
|
42
|
+
* </InterfaceDeclaration>
|
|
43
|
+
* ```
|
|
44
|
+
* This will produce:
|
|
45
|
+
* ```csharp
|
|
46
|
+
* public interface MyIface
|
|
47
|
+
* {
|
|
48
|
+
* public int MyProperty { get; set; }
|
|
49
|
+
* public void MyMethod(int value);
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
|
|
54
|
+
const name = useCSharpNamePolicy().getName(props.name!, "interface");
|
|
55
|
+
|
|
56
|
+
const thisInterfaceSymbol = new CSharpOutputSymbol(name, {
|
|
57
|
+
refkeys: props.refkey,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// this creates a new scope for the interface 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 thisInterfaceScope = new CSharpMemberScope("interface-decl", {
|
|
65
|
+
owner: thisInterfaceSymbol,
|
|
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: thisInterfaceScope,
|
|
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
|
+
getInterfaceModifiers(props),
|
|
96
|
+
]);
|
|
97
|
+
return (
|
|
98
|
+
<core.Declaration symbol={thisInterfaceSymbol}>
|
|
99
|
+
<DocWhen doc={props.doc} />
|
|
100
|
+
{modifiers}interface <Name />
|
|
101
|
+
{typeParams}
|
|
102
|
+
{props.children ?
|
|
103
|
+
<core.Block newline>
|
|
104
|
+
<core.Scope value={thisInterfaceScope}>{props.children}</core.Scope>
|
|
105
|
+
</core.Block>
|
|
106
|
+
: ";"}
|
|
107
|
+
</core.Declaration>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { TestNamespace } from "../../../test/utils.jsx";
|
|
4
|
+
import { InterfaceDeclaration } from "./declaration.jsx";
|
|
5
|
+
import { InterfaceMethod } from "./method.jsx";
|
|
6
|
+
|
|
7
|
+
const Wrapper = (props: { children: Children }) => (
|
|
8
|
+
<TestNamespace>
|
|
9
|
+
<InterfaceDeclaration public name="TestInterface">
|
|
10
|
+
{props.children}
|
|
11
|
+
</InterfaceDeclaration>
|
|
12
|
+
</TestNamespace>
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
describe("modifiers", () => {
|
|
16
|
+
describe("access modifiers", () => {
|
|
17
|
+
it.each(["public", "private", "protected", "internal"] as const)(
|
|
18
|
+
"%s",
|
|
19
|
+
(accessModifier) => {
|
|
20
|
+
expect(
|
|
21
|
+
<Wrapper>
|
|
22
|
+
<InterfaceMethod {...{ [accessModifier]: true }} name="MethodOne" />
|
|
23
|
+
</Wrapper>,
|
|
24
|
+
).toRenderTo(`
|
|
25
|
+
public interface TestInterface
|
|
26
|
+
{
|
|
27
|
+
${accessModifier} void MethodOne();
|
|
28
|
+
}
|
|
29
|
+
`);
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("method modifiers", () => {
|
|
35
|
+
it.each(["new"] as const)("%s", (methodModifier) => {
|
|
36
|
+
expect(
|
|
37
|
+
<Wrapper>
|
|
38
|
+
<InterfaceMethod {...{ [methodModifier]: true }} name="MethodOne" />
|
|
39
|
+
</Wrapper>,
|
|
40
|
+
).toRenderTo(`
|
|
41
|
+
public interface TestInterface
|
|
42
|
+
{
|
|
43
|
+
${methodModifier} void MethodOne();
|
|
44
|
+
}
|
|
45
|
+
`);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("combine modifiers", () => {
|
|
50
|
+
expect(
|
|
51
|
+
<Wrapper>
|
|
52
|
+
<InterfaceMethod returns="Task" public new name="MethodOne" />
|
|
53
|
+
</Wrapper>,
|
|
54
|
+
).toRenderTo(`
|
|
55
|
+
public interface TestInterface
|
|
56
|
+
{
|
|
57
|
+
public new Task MethodOne();
|
|
58
|
+
}
|
|
59
|
+
`);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("applies PascalCase naming policy", () => {
|
|
64
|
+
expect(
|
|
65
|
+
<Wrapper>
|
|
66
|
+
<InterfaceMethod name="method_one" />
|
|
67
|
+
</Wrapper>,
|
|
68
|
+
).toRenderTo(`
|
|
69
|
+
public interface TestInterface
|
|
70
|
+
{
|
|
71
|
+
void MethodOne();
|
|
72
|
+
}
|
|
73
|
+
`);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("defines params and return type", () => {
|
|
77
|
+
const params = [
|
|
78
|
+
{
|
|
79
|
+
name: "intParam",
|
|
80
|
+
type: "int",
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: "stringParam",
|
|
84
|
+
type: "string",
|
|
85
|
+
},
|
|
86
|
+
];
|
|
87
|
+
const res = (
|
|
88
|
+
<Wrapper>
|
|
89
|
+
<InterfaceMethod
|
|
90
|
+
public
|
|
91
|
+
name="MethodOne"
|
|
92
|
+
parameters={params}
|
|
93
|
+
returns="string"
|
|
94
|
+
/>
|
|
95
|
+
</Wrapper>
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
expect(res).toRenderTo(`
|
|
99
|
+
public interface TestInterface
|
|
100
|
+
{
|
|
101
|
+
public string MethodOne(int intParam, string stringParam);
|
|
102
|
+
}
|
|
103
|
+
`);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("specify doc comment", () => {
|
|
107
|
+
expect(
|
|
108
|
+
<TestNamespace>
|
|
109
|
+
<InterfaceDeclaration name="Test">
|
|
110
|
+
<InterfaceMethod name="Method" doc="This is a test" />
|
|
111
|
+
</InterfaceDeclaration>
|
|
112
|
+
</TestNamespace>,
|
|
113
|
+
).toRenderTo(`
|
|
114
|
+
interface Test
|
|
115
|
+
{
|
|
116
|
+
/// This is a test
|
|
117
|
+
void Method();
|
|
118
|
+
}
|
|
119
|
+
`);
|
|
120
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Block,
|
|
3
|
+
Children,
|
|
4
|
+
MemberDeclaration,
|
|
5
|
+
refkey,
|
|
6
|
+
Refkey,
|
|
7
|
+
Scope,
|
|
8
|
+
} from "@alloy-js/core";
|
|
9
|
+
import {
|
|
10
|
+
AccessModifiers,
|
|
11
|
+
computeModifiersPrefix,
|
|
12
|
+
getAccessModifier,
|
|
13
|
+
makeModifiers,
|
|
14
|
+
} from "../../modifiers.js";
|
|
15
|
+
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
16
|
+
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
17
|
+
import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
|
|
18
|
+
import { ParameterProps, Parameters } from "../Parameters.jsx";
|
|
19
|
+
import { DocWhen } from "../doc/comment.jsx";
|
|
20
|
+
|
|
21
|
+
/** Method modifiers. Can only be one. */
|
|
22
|
+
export interface InterfaceMethodModifiers {
|
|
23
|
+
readonly new?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const getMethodModifier = makeModifiers<InterfaceMethodModifiers>(["new"]);
|
|
27
|
+
|
|
28
|
+
// properties for creating a method
|
|
29
|
+
export interface InterfaceMethodProps
|
|
30
|
+
extends AccessModifiers,
|
|
31
|
+
InterfaceMethodModifiers {
|
|
32
|
+
name: string;
|
|
33
|
+
refkey?: Refkey;
|
|
34
|
+
children?: Children;
|
|
35
|
+
parameters?: Array<ParameterProps>;
|
|
36
|
+
returns?: Children;
|
|
37
|
+
|
|
38
|
+
/** Doc comment */
|
|
39
|
+
doc?: Children;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// a C# interface method
|
|
43
|
+
export function InterfaceMethod(props: InterfaceMethodProps) {
|
|
44
|
+
const name = useCSharpNamePolicy().getName(props.name, "class-method");
|
|
45
|
+
const scope = useCSharpScope();
|
|
46
|
+
if (scope.kind !== "member" || scope.name !== "interface-decl") {
|
|
47
|
+
throw new Error(
|
|
48
|
+
"can't define an interface method outside of an interface scope",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const methodSymbol = new CSharpOutputSymbol(name, {
|
|
53
|
+
scope,
|
|
54
|
+
refkeys: props.refkey ?? refkey(props.name),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// scope for method declaration
|
|
58
|
+
const methodScope = new CSharpMemberScope("method-decl", {
|
|
59
|
+
owner: methodSymbol,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const params =
|
|
63
|
+
props.parameters ? <Parameters parameters={props.parameters} /> : "";
|
|
64
|
+
|
|
65
|
+
const modifiers = computeModifiersPrefix([
|
|
66
|
+
getAccessModifier(props),
|
|
67
|
+
getMethodModifier(props),
|
|
68
|
+
]);
|
|
69
|
+
// note that scope wraps the method decl so that the params get the correct scope
|
|
70
|
+
return (
|
|
71
|
+
<MemberDeclaration symbol={methodSymbol}>
|
|
72
|
+
<Scope value={methodScope}>
|
|
73
|
+
<DocWhen doc={props.doc} />
|
|
74
|
+
{modifiers}
|
|
75
|
+
{props.returns ?? "void"} {name}({params})
|
|
76
|
+
{props.children ?
|
|
77
|
+
<Block newline>{props.children}</Block>
|
|
78
|
+
: ";"}
|
|
79
|
+
</Scope>
|
|
80
|
+
</MemberDeclaration>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { TestNamespace } from "../../../test/utils.jsx";
|
|
4
|
+
import { InterfaceDeclaration } from "./declaration.jsx";
|
|
5
|
+
import { InterfaceProperty } from "./property.jsx";
|
|
6
|
+
|
|
7
|
+
const Wrapper = (props: { children: Children }) => (
|
|
8
|
+
<TestNamespace>
|
|
9
|
+
<InterfaceDeclaration public name="TestInterface">
|
|
10
|
+
{props.children}
|
|
11
|
+
</InterfaceDeclaration>
|
|
12
|
+
</TestNamespace>
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
describe("modifiers", () => {
|
|
16
|
+
describe("access modifiers", () => {
|
|
17
|
+
it.each(["public", "private", "protected", "internal"] as const)(
|
|
18
|
+
"%s",
|
|
19
|
+
(accessModifier) => {
|
|
20
|
+
expect(
|
|
21
|
+
<Wrapper>
|
|
22
|
+
<InterfaceProperty
|
|
23
|
+
{...{ [accessModifier]: true }}
|
|
24
|
+
name="TestProp"
|
|
25
|
+
type="string"
|
|
26
|
+
get
|
|
27
|
+
/>
|
|
28
|
+
</Wrapper>,
|
|
29
|
+
).toRenderTo(`
|
|
30
|
+
public interface TestInterface
|
|
31
|
+
{
|
|
32
|
+
${accessModifier} string TestProp { get; }
|
|
33
|
+
}
|
|
34
|
+
`);
|
|
35
|
+
},
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("method modifiers", () => {
|
|
40
|
+
it.each(["new"] as const)("%s", (methodModifier) => {
|
|
41
|
+
expect(
|
|
42
|
+
<Wrapper>
|
|
43
|
+
<InterfaceProperty
|
|
44
|
+
{...{ [methodModifier]: true }}
|
|
45
|
+
name="TestProp"
|
|
46
|
+
type="string"
|
|
47
|
+
get
|
|
48
|
+
/>
|
|
49
|
+
</Wrapper>,
|
|
50
|
+
).toRenderTo(`
|
|
51
|
+
public interface TestInterface
|
|
52
|
+
{
|
|
53
|
+
${methodModifier} string TestProp { get; }
|
|
54
|
+
}
|
|
55
|
+
`);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("combine modifiers", () => {
|
|
60
|
+
expect(
|
|
61
|
+
<Wrapper>
|
|
62
|
+
<InterfaceProperty public new name="TestProp" type="string" get />
|
|
63
|
+
</Wrapper>,
|
|
64
|
+
).toRenderTo(`
|
|
65
|
+
public interface TestInterface
|
|
66
|
+
{
|
|
67
|
+
public new string TestProp { get; }
|
|
68
|
+
}
|
|
69
|
+
`);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("applies PascalCase naming policy", () => {
|
|
74
|
+
expect(
|
|
75
|
+
<Wrapper>
|
|
76
|
+
<InterfaceProperty name="test_prop" type="string" get />
|
|
77
|
+
</Wrapper>,
|
|
78
|
+
).toRenderTo(`
|
|
79
|
+
public interface TestInterface
|
|
80
|
+
{
|
|
81
|
+
string TestProp { get; }
|
|
82
|
+
}
|
|
83
|
+
`);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("has getter only", () => {
|
|
87
|
+
expect(
|
|
88
|
+
<Wrapper>
|
|
89
|
+
<InterfaceProperty name="TestProp" type="string" get />
|
|
90
|
+
</Wrapper>,
|
|
91
|
+
).toRenderTo(`
|
|
92
|
+
public interface TestInterface
|
|
93
|
+
{
|
|
94
|
+
string TestProp { get; }
|
|
95
|
+
}
|
|
96
|
+
`);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("has setter only", () => {
|
|
100
|
+
expect(
|
|
101
|
+
<Wrapper>
|
|
102
|
+
<InterfaceProperty name="TestProp" type="string" set />
|
|
103
|
+
</Wrapper>,
|
|
104
|
+
).toRenderTo(`
|
|
105
|
+
public interface TestInterface
|
|
106
|
+
{
|
|
107
|
+
string TestProp { set; }
|
|
108
|
+
}
|
|
109
|
+
`);
|
|
110
|
+
});
|
|
111
|
+
it("has getter and setter", () => {
|
|
112
|
+
expect(
|
|
113
|
+
<Wrapper>
|
|
114
|
+
<InterfaceProperty name="TestProp" type="string" get set />
|
|
115
|
+
</Wrapper>,
|
|
116
|
+
).toRenderTo(`
|
|
117
|
+
public interface TestInterface
|
|
118
|
+
{
|
|
119
|
+
string TestProp { get; set; }
|
|
120
|
+
}
|
|
121
|
+
`);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("specify doc comment", () => {
|
|
125
|
+
expect(
|
|
126
|
+
<TestNamespace>
|
|
127
|
+
<InterfaceDeclaration name="Test">
|
|
128
|
+
<InterfaceProperty
|
|
129
|
+
name="Method"
|
|
130
|
+
type="string"
|
|
131
|
+
get
|
|
132
|
+
set
|
|
133
|
+
doc="This is a test"
|
|
134
|
+
/>
|
|
135
|
+
</InterfaceDeclaration>
|
|
136
|
+
</TestNamespace>,
|
|
137
|
+
).toRenderTo(`
|
|
138
|
+
interface Test
|
|
139
|
+
{
|
|
140
|
+
/// This is a test
|
|
141
|
+
string Method { get; set; }
|
|
142
|
+
}
|
|
143
|
+
`);
|
|
144
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Block,
|
|
3
|
+
Children,
|
|
4
|
+
List,
|
|
5
|
+
MemberDeclaration,
|
|
6
|
+
refkey,
|
|
7
|
+
Refkey,
|
|
8
|
+
Scope,
|
|
9
|
+
} from "@alloy-js/core";
|
|
10
|
+
import {
|
|
11
|
+
AccessModifiers,
|
|
12
|
+
computeModifiersPrefix,
|
|
13
|
+
getAccessModifier,
|
|
14
|
+
makeModifiers,
|
|
15
|
+
} from "../../modifiers.js";
|
|
16
|
+
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
17
|
+
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
18
|
+
import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
|
|
19
|
+
import { DocWhen } from "../doc/comment.jsx";
|
|
20
|
+
|
|
21
|
+
/** Method modifiers. Can only be one. */
|
|
22
|
+
export interface InterfacePropertyModifiers {
|
|
23
|
+
readonly new?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const getModifiers = makeModifiers<InterfacePropertyModifiers>(["new"]);
|
|
27
|
+
|
|
28
|
+
// properties for creating a method
|
|
29
|
+
export interface InterfacePropertyProps
|
|
30
|
+
extends AccessModifiers,
|
|
31
|
+
InterfacePropertyModifiers {
|
|
32
|
+
name: string;
|
|
33
|
+
refkey?: Refkey;
|
|
34
|
+
|
|
35
|
+
/** Property type */
|
|
36
|
+
type: Children;
|
|
37
|
+
|
|
38
|
+
/** If property should have a getter */
|
|
39
|
+
get?: boolean;
|
|
40
|
+
|
|
41
|
+
/** If property should have a setter */
|
|
42
|
+
set?: boolean;
|
|
43
|
+
|
|
44
|
+
/** Doc comment */
|
|
45
|
+
doc?: Children;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Property initializer
|
|
49
|
+
* @example `<ClassProperty name="My" get set nullable />`
|
|
50
|
+
*
|
|
51
|
+
* ```cs
|
|
52
|
+
* int? My { get; set; };
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
nullable?: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Render a C# interface property.
|
|
60
|
+
*
|
|
61
|
+
* @example `<InterfaceProperty public name="My" get set />`
|
|
62
|
+
*
|
|
63
|
+
* ```cs
|
|
64
|
+
* public int My { get; set; };
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export function InterfaceProperty(props: InterfacePropertyProps) {
|
|
68
|
+
const name = useCSharpNamePolicy().getName(props.name, "class-property");
|
|
69
|
+
const scope = useCSharpScope();
|
|
70
|
+
if (scope.kind !== "member" || scope.name !== "interface-decl") {
|
|
71
|
+
throw new Error(
|
|
72
|
+
"can't define an interface method outside of an interface scope",
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const propertySymbol = new CSharpOutputSymbol(name, {
|
|
77
|
+
scope,
|
|
78
|
+
refkeys: props.refkey ?? refkey(props.name),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// scope for property declaration
|
|
82
|
+
const propertyScope = new CSharpMemberScope("property-decl", {
|
|
83
|
+
owner: propertySymbol,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const modifiers = computeModifiersPrefix([
|
|
87
|
+
getAccessModifier(props),
|
|
88
|
+
getModifiers(props),
|
|
89
|
+
]);
|
|
90
|
+
// note that scope wraps the method decl so that the params get the correct scope
|
|
91
|
+
return (
|
|
92
|
+
<MemberDeclaration symbol={propertySymbol}>
|
|
93
|
+
<Scope value={propertyScope}>
|
|
94
|
+
<DocWhen doc={props.doc} />
|
|
95
|
+
{modifiers}
|
|
96
|
+
{props.type}
|
|
97
|
+
{props.nullable && "?"} {name}{" "}
|
|
98
|
+
<Block newline inline>
|
|
99
|
+
<List joiner=" ">
|
|
100
|
+
{props.get && "get;"}
|
|
101
|
+
{props.set && "set;"}
|
|
102
|
+
</List>
|
|
103
|
+
</Block>
|
|
104
|
+
</Scope>
|
|
105
|
+
</MemberDeclaration>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as core from "@alloy-js/core";
|
|
2
2
|
import * as base from "../index.js";
|
|
3
3
|
|
|
4
|
-
export const
|
|
4
|
+
export const ClassDeclaration = core.stc(base.ClassDeclaration);
|
|
5
5
|
export const ClassConstructor = core.stc(base.ClassConstructor);
|
|
6
6
|
export const ClassMember = core.stc(base.ClassMember);
|
|
7
7
|
export const ClassMethod = core.stc(base.ClassMethod);
|
|
8
|
-
export const
|
|
8
|
+
export const EnumDeclaration = core.stc(base.EnumDeclaration);
|
|
9
9
|
export const EnumMember = core.stc(base.EnumMember);
|
|
10
10
|
export const Parameter = core.stc(base.Parameter);
|
|
11
11
|
export const Parameters = core.stc(base.Parameters);
|