@alloy-js/csharp 0.18.0-dev.5 → 0.18.0-dev.7
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 +8 -2
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.js +3 -2
- package/dist/src/components/ClassMethod.d.ts +9 -2
- package/dist/src/components/ClassMethod.d.ts.map +1 -1
- package/dist/src/components/ClassMethod.js +5 -1
- package/dist/src/components/EnumDeclaration.d.ts +2 -2
- package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
- package/dist/src/components/index.d.ts +3 -1
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +3 -1
- package/dist/src/components/interface/declaration.d.ts +32 -0
- package/dist/src/components/interface/declaration.d.ts.map +1 -0
- package/dist/src/components/interface/declaration.js +85 -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 +56 -0
- package/dist/src/components/interface/method.d.ts +16 -0
- package/dist/src/components/interface/method.d.ts.map +1 -0
- package/dist/src/components/interface/method.js +54 -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 +110 -0
- package/dist/src/components/interface/property.d.ts +19 -0
- package/dist/src/components/interface/property.d.ts.map +1 -0
- package/dist/src/components/interface/property.js +54 -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 +141 -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/modifiers.d.ts +2 -9
- package/dist/src/modifiers.d.ts.map +1 -1
- package/dist/src/modifiers.js +6 -9
- 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.test.js +52 -10
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/components/ClassDeclaration.tsx +23 -4
- package/src/components/ClassMethod.tsx +19 -3
- package/src/components/EnumDeclaration.tsx +2 -2
- package/src/components/index.ts +3 -1
- package/src/components/interface/declaration.test.tsx +45 -0
- package/src/components/interface/declaration.tsx +104 -0
- package/src/components/interface/method.test.tsx +104 -0
- package/src/components/interface/method.tsx +77 -0
- package/src/components/interface/property.test.tsx +122 -0
- package/src/components/interface/property.tsx +85 -0
- package/src/modifiers.ts +16 -30
- package/src/name-policy.ts +2 -0
- package/temp/api.json +977 -277
- package/test/class.test.tsx +40 -10
package/src/components/index.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export * from "./ClassDeclaration.jsx";
|
|
2
|
-
export
|
|
2
|
+
export * from "./ClassMethod.jsx";
|
|
3
3
|
export * from "./Declaration.js";
|
|
4
4
|
export * from "./EnumDeclaration.jsx";
|
|
5
|
+
export * from "./interface/declaration.js";
|
|
6
|
+
export * from "./interface/method.js";
|
|
5
7
|
export * from "./Name.js";
|
|
6
8
|
export * from "./Namespace.js";
|
|
7
9
|
export * from "./Parameters.js";
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
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 { Name } from "../Name.jsx";
|
|
12
|
+
|
|
13
|
+
export interface InterfaceModifiers {
|
|
14
|
+
readonly partial?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const getInterfaceModifiers = makeModifiers<InterfaceModifiers>(["partial"]);
|
|
18
|
+
|
|
19
|
+
// properties for creating a class
|
|
20
|
+
export interface InterfaceDeclarationProps
|
|
21
|
+
extends Omit<core.DeclarationProps, "nameKind">,
|
|
22
|
+
AccessModifiers,
|
|
23
|
+
InterfaceModifiers {
|
|
24
|
+
name: string;
|
|
25
|
+
refkey?: core.Refkey;
|
|
26
|
+
typeParameters?: Record<string, core.Refkey>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* CSharp interface declaration.
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* <InterfaceDeclaration public name="IMyInterface">
|
|
34
|
+
* <InterfaceMember public name="MyProperty" type="int" />
|
|
35
|
+
* <InterfaceMethod public name="MyMethod" returnType="void">
|
|
36
|
+
* <Parameter name="value" type="int" />
|
|
37
|
+
* </InterfaceMethod>
|
|
38
|
+
* </InterfaceDeclaration>
|
|
39
|
+
* ```
|
|
40
|
+
* This will produce:
|
|
41
|
+
* ```csharp
|
|
42
|
+
* public interface MyIface
|
|
43
|
+
* {
|
|
44
|
+
* public int MyProperty { get; set; }
|
|
45
|
+
* public void MyMethod(int value);
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
|
|
50
|
+
const name = useCSharpNamePolicy().getName(props.name!, "interface");
|
|
51
|
+
|
|
52
|
+
const thisInterfaceSymbol = new CSharpOutputSymbol(name, {
|
|
53
|
+
refkeys: props.refkey,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// this creates a new scope for the interface definition.
|
|
57
|
+
// members will automatically "inherit" this scope so
|
|
58
|
+
// that refkeys to them will produce the fully-qualified
|
|
59
|
+
// name e.g. Foo.Bar.
|
|
60
|
+
const thisInterfaceScope = new CSharpMemberScope("interface-decl", {
|
|
61
|
+
owner: thisInterfaceSymbol,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
let typeParams: core.Children;
|
|
65
|
+
if (props.typeParameters) {
|
|
66
|
+
const typeParamNames = new Array<string>();
|
|
67
|
+
for (const entry of Object.entries(props.typeParameters)) {
|
|
68
|
+
typeParamNames.push(
|
|
69
|
+
useCSharpNamePolicy().getName(entry[0], "type-parameter"),
|
|
70
|
+
);
|
|
71
|
+
// create a symbol for each type param so its
|
|
72
|
+
// refkey resolves to the type param's name
|
|
73
|
+
new CSharpOutputSymbol(entry[0], {
|
|
74
|
+
scope: thisInterfaceScope,
|
|
75
|
+
refkeys: entry[1],
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
typeParams = (
|
|
79
|
+
<group>
|
|
80
|
+
{"<"}
|
|
81
|
+
<core.For each={typeParamNames} comma line>
|
|
82
|
+
{(name) => name}
|
|
83
|
+
</core.For>
|
|
84
|
+
{">"}
|
|
85
|
+
</group>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const modifiers = computeModifiersPrefix([
|
|
90
|
+
getAccessModifier(props),
|
|
91
|
+
getInterfaceModifiers(props),
|
|
92
|
+
]);
|
|
93
|
+
return (
|
|
94
|
+
<core.Declaration symbol={thisInterfaceSymbol}>
|
|
95
|
+
{modifiers}interface <Name />
|
|
96
|
+
{typeParams}
|
|
97
|
+
{props.children ?
|
|
98
|
+
<core.Block newline>
|
|
99
|
+
<core.Scope value={thisInterfaceScope}>{props.children}</core.Scope>
|
|
100
|
+
</core.Block>
|
|
101
|
+
: ";"}
|
|
102
|
+
</core.Declaration>
|
|
103
|
+
);
|
|
104
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
|
|
20
|
+
/** Method modifiers. Can only be one. */
|
|
21
|
+
export interface InterfaceMethodModifiers {
|
|
22
|
+
readonly new?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const getMethodModifier = makeModifiers<InterfaceMethodModifiers>(["new"]);
|
|
26
|
+
|
|
27
|
+
// properties for creating a method
|
|
28
|
+
export interface InterfaceMethodProps
|
|
29
|
+
extends AccessModifiers,
|
|
30
|
+
InterfaceMethodModifiers {
|
|
31
|
+
name: string;
|
|
32
|
+
refkey?: Refkey;
|
|
33
|
+
children?: Children;
|
|
34
|
+
parameters?: Array<ParameterProps>;
|
|
35
|
+
returns?: Children;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// a C# interface method
|
|
39
|
+
export function InterfaceMethod(props: InterfaceMethodProps) {
|
|
40
|
+
const name = useCSharpNamePolicy().getName(props.name, "class-method");
|
|
41
|
+
const scope = useCSharpScope();
|
|
42
|
+
if (scope.kind !== "member" || scope.name !== "interface-decl") {
|
|
43
|
+
throw new Error(
|
|
44
|
+
"can't define an interface method outside of an interface scope",
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const methodSymbol = new CSharpOutputSymbol(name, {
|
|
49
|
+
scope,
|
|
50
|
+
refkeys: props.refkey ?? refkey(props.name),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// scope for method declaration
|
|
54
|
+
const methodScope = new CSharpMemberScope("method-decl", {
|
|
55
|
+
owner: methodSymbol,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const params =
|
|
59
|
+
props.parameters ? <Parameters parameters={props.parameters} /> : "";
|
|
60
|
+
|
|
61
|
+
const modifiers = computeModifiersPrefix([
|
|
62
|
+
getAccessModifier(props),
|
|
63
|
+
getMethodModifier(props),
|
|
64
|
+
]);
|
|
65
|
+
// note that scope wraps the method decl so that the params get the correct scope
|
|
66
|
+
return (
|
|
67
|
+
<MemberDeclaration symbol={methodSymbol}>
|
|
68
|
+
<Scope value={methodScope}>
|
|
69
|
+
{modifiers}
|
|
70
|
+
{props.returns ?? "void"} {name}({params})
|
|
71
|
+
{props.children ?
|
|
72
|
+
<Block newline>{props.children}</Block>
|
|
73
|
+
: ";"}
|
|
74
|
+
</Scope>
|
|
75
|
+
</MemberDeclaration>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
|
|
20
|
+
/** Method modifiers. Can only be one. */
|
|
21
|
+
export interface InterfacePropertyModifiers {
|
|
22
|
+
readonly new?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const getMethodModifier = makeModifiers<InterfacePropertyModifiers>([
|
|
26
|
+
"new",
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
// properties for creating a method
|
|
30
|
+
export interface InterfacePropertyProps
|
|
31
|
+
extends AccessModifiers,
|
|
32
|
+
InterfacePropertyModifiers {
|
|
33
|
+
name: string;
|
|
34
|
+
refkey?: Refkey;
|
|
35
|
+
|
|
36
|
+
/** Property type */
|
|
37
|
+
type: Children;
|
|
38
|
+
|
|
39
|
+
/** If property should have a getter */
|
|
40
|
+
get?: boolean;
|
|
41
|
+
|
|
42
|
+
/** If property should have a setter */
|
|
43
|
+
set?: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// a C# interface property
|
|
47
|
+
export function InterfaceProperty(props: InterfacePropertyProps) {
|
|
48
|
+
const name = useCSharpNamePolicy().getName(props.name, "class-property");
|
|
49
|
+
const scope = useCSharpScope();
|
|
50
|
+
if (scope.kind !== "member" || scope.name !== "interface-decl") {
|
|
51
|
+
throw new Error(
|
|
52
|
+
"can't define an interface method outside of an interface scope",
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const propertySymbol = new CSharpOutputSymbol(name, {
|
|
57
|
+
scope,
|
|
58
|
+
refkeys: props.refkey ?? refkey(props.name),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// scope for property declaration
|
|
62
|
+
const propertyScope = new CSharpMemberScope("property-decl", {
|
|
63
|
+
owner: propertySymbol,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const modifiers = computeModifiersPrefix([
|
|
67
|
+
getAccessModifier(props),
|
|
68
|
+
getMethodModifier(props),
|
|
69
|
+
]);
|
|
70
|
+
// note that scope wraps the method decl so that the params get the correct scope
|
|
71
|
+
return (
|
|
72
|
+
<MemberDeclaration symbol={propertySymbol}>
|
|
73
|
+
<Scope value={propertyScope}>
|
|
74
|
+
{modifiers}
|
|
75
|
+
{props.type} {name}{" "}
|
|
76
|
+
<Block newline inline>
|
|
77
|
+
<List joiner=" ">
|
|
78
|
+
{props.get && "get;"}
|
|
79
|
+
{props.set && "set;"}
|
|
80
|
+
</List>
|
|
81
|
+
</Block>
|
|
82
|
+
</Scope>
|
|
83
|
+
</MemberDeclaration>
|
|
84
|
+
);
|
|
85
|
+
}
|
package/src/modifiers.ts
CHANGED
|
@@ -10,36 +10,13 @@ export interface AccessModifiers {
|
|
|
10
10
|
readonly file?: boolean;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
]
|
|
21
|
-
.filter((x) => x)
|
|
22
|
-
.join(" ");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** Method modifiers. Can only be one. */
|
|
26
|
-
export interface MethodModifiers {
|
|
27
|
-
readonly abstract?: boolean;
|
|
28
|
-
readonly sealed?: boolean;
|
|
29
|
-
readonly static?: boolean;
|
|
30
|
-
readonly virtual?: boolean;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function getMethodModifier(data: MethodModifiers): string {
|
|
34
|
-
return [
|
|
35
|
-
data.abstract && "abstract",
|
|
36
|
-
data.sealed && "sealed",
|
|
37
|
-
data.static && "static",
|
|
38
|
-
data.virtual && "virtual",
|
|
39
|
-
]
|
|
40
|
-
.filter((x) => x)
|
|
41
|
-
.join(" ");
|
|
42
|
-
}
|
|
13
|
+
export const getAccessModifier = makeModifiers<AccessModifiers>([
|
|
14
|
+
"public",
|
|
15
|
+
"protected",
|
|
16
|
+
"private",
|
|
17
|
+
"internal",
|
|
18
|
+
"file",
|
|
19
|
+
]);
|
|
43
20
|
|
|
44
21
|
export function getAsyncModifier(async?: boolean): string {
|
|
45
22
|
return async ? "async" : "";
|
|
@@ -52,3 +29,12 @@ export function computeModifiersPrefix(
|
|
|
52
29
|
const resolved = modifiers.filter((x) => x);
|
|
53
30
|
return resolved.length > 0 ? resolved.join(" ") + " " : "";
|
|
54
31
|
}
|
|
32
|
+
|
|
33
|
+
export function makeModifiers<T>(obj: Array<keyof T>) {
|
|
34
|
+
return (data: T) => {
|
|
35
|
+
return obj
|
|
36
|
+
.map((key) => (data[key] ? key : undefined))
|
|
37
|
+
.filter((x) => x)
|
|
38
|
+
.join(" ");
|
|
39
|
+
};
|
|
40
|
+
}
|
package/src/name-policy.ts
CHANGED
|
@@ -12,6 +12,7 @@ export type CSharpElements =
|
|
|
12
12
|
| "class-member-private"
|
|
13
13
|
| "class-member-public"
|
|
14
14
|
| "class-method"
|
|
15
|
+
| "class-property"
|
|
15
16
|
| "parameter"
|
|
16
17
|
| "type-parameter";
|
|
17
18
|
|
|
@@ -26,6 +27,7 @@ export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
|
|
|
26
27
|
case "class-member-public":
|
|
27
28
|
case "class-method":
|
|
28
29
|
case "type-parameter":
|
|
30
|
+
case "class-property":
|
|
29
31
|
return changecase.pascalCase(name);
|
|
30
32
|
case "constant":
|
|
31
33
|
return changecase.constantCase(name);
|