@alloy-js/csharp 0.18.0-dev.24 → 0.18.0-dev.4
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/Class.d.ts +23 -0
- package/dist/src/components/Class.d.ts.map +1 -0
- package/dist/src/components/{ClassDeclaration.js → Class.js} +37 -67
- package/dist/src/components/ClassMethod.d.ts +3 -44
- package/dist/src/components/ClassMethod.d.ts.map +1 -1
- package/dist/src/components/ClassMethod.js +8 -32
- package/dist/src/components/Enum.d.ts +14 -0
- package/dist/src/components/Enum.d.ts.map +1 -0
- package/dist/src/components/{EnumDeclaration.js → Enum.js} +2 -21
- package/dist/src/components/Parameters.d.ts +13 -0
- package/dist/src/components/Parameters.d.ts.map +1 -0
- package/dist/src/components/Parameters.js +34 -0
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +0 -1
- package/dist/src/components/index.d.ts +4 -14
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +5 -15
- 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 +9 -2
- package/dist/src/modifiers.d.ts.map +1 -1
- package/dist/src/modifiers.js +9 -6
- 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 +0 -2
- package/dist/test/class-method.test.js +2 -23
- package/dist/test/class.test.d.ts +2 -0
- package/dist/test/class.test.d.ts.map +1 -0
- package/dist/test/{class-declaration.test.js → class.test.js} +51 -234
- package/dist/test/enum.test.js +6 -6
- package/dist/test/namespace.test.js +4 -4
- package/dist/test/projectdirectory.test.d.ts +2 -0
- package/dist/test/projectdirectory.test.d.ts.map +1 -0
- package/dist/test/{project-directory.test.js → projectdirectory.test.js} +4 -4
- package/dist/test/sourcefile.test.js +2 -2
- package/dist/test/using.test.js +4 -4
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -4
- package/src/components/{ClassDeclaration.tsx → Class.tsx} +38 -116
- package/src/components/ClassMethod.tsx +7 -70
- package/src/components/{EnumDeclaration.tsx → Enum.tsx} +3 -22
- package/src/components/Parameters.tsx +51 -0
- package/src/components/SourceFile.tsx +0 -1
- package/src/components/index.ts +4 -14
- package/src/components/stc/index.ts +2 -2
- package/src/modifiers.ts +30 -16
- package/src/name-policy.ts +0 -5
- package/temp/api.json +1194 -6218
- package/test/class-method.test.tsx +3 -19
- package/test/class.test.tsx +257 -0
- package/test/enum.test.tsx +11 -13
- package/test/namespace.test.tsx +4 -4
- package/test/{project-directory.test.tsx → projectdirectory.test.tsx} +4 -4
- package/test/sourcefile.test.tsx +2 -2
- package/test/using.test.tsx +6 -14
- package/vitest.config.ts +0 -3
- package/dist/src/components/ClassDeclaration.d.ts +0 -92
- package/dist/src/components/ClassDeclaration.d.ts.map +0 -1
- package/dist/src/components/EnumDeclaration.d.ts +0 -34
- package/dist/src/components/EnumDeclaration.d.ts.map +0 -1
- package/dist/src/components/attributes/attributes.d.ts +0 -39
- package/dist/src/components/attributes/attributes.d.ts.map +0 -1
- package/dist/src/components/attributes/attributes.js +0 -62
- package/dist/src/components/attributes/attributes.test.d.ts +0 -2
- package/dist/src/components/attributes/attributes.test.d.ts.map +0 -1
- package/dist/src/components/attributes/attributes.test.js +0 -75
- package/dist/src/components/doc/comment.d.ts +0 -70
- package/dist/src/components/doc/comment.d.ts.map +0 -1
- package/dist/src/components/doc/comment.js +0 -88
- package/dist/src/components/doc/comment.test.d.ts +0 -2
- package/dist/src/components/doc/comment.test.d.ts.map +0 -1
- package/dist/src/components/doc/comment.test.js +0 -348
- package/dist/src/components/doc/from-markdown.d.ts +0 -6
- package/dist/src/components/doc/from-markdown.d.ts.map +0 -1
- package/dist/src/components/doc/from-markdown.js +0 -58
- package/dist/src/components/doc/from-markdown.test.d.ts +0 -2
- package/dist/src/components/doc/from-markdown.test.d.ts.map +0 -1
- package/dist/src/components/doc/from-markdown.test.js +0 -83
- package/dist/src/components/interface/declaration.d.ts +0 -65
- package/dist/src/components/interface/declaration.d.ts.map +0 -1
- package/dist/src/components/interface/declaration.js +0 -83
- package/dist/src/components/interface/declaration.test.d.ts +0 -2
- package/dist/src/components/interface/declaration.test.d.ts.map +0 -1
- package/dist/src/components/interface/declaration.test.js +0 -171
- package/dist/src/components/interface/method.d.ts +0 -50
- package/dist/src/components/interface/method.d.ts.map +0 -1
- package/dist/src/components/interface/method.js +0 -74
- package/dist/src/components/interface/method.test.d.ts +0 -2
- package/dist/src/components/interface/method.test.d.ts.map +0 -1
- package/dist/src/components/interface/method.test.js +0 -300
- package/dist/src/components/interface/property.d.ts +0 -56
- package/dist/src/components/interface/property.d.ts.map +0 -1
- package/dist/src/components/interface/property.js +0 -73
- package/dist/src/components/interface/property.test.d.ts +0 -2
- package/dist/src/components/interface/property.test.d.ts.map +0 -1
- package/dist/src/components/interface/property.test.js +0 -189
- package/dist/src/components/parameters/parameters.d.ts +0 -19
- package/dist/src/components/parameters/parameters.d.ts.map +0 -1
- package/dist/src/components/parameters/parameters.js +0 -43
- package/dist/src/components/property/property.d.ts +0 -80
- package/dist/src/components/property/property.d.ts.map +0 -1
- package/dist/src/components/property/property.js +0 -76
- package/dist/src/components/property/property.test.d.ts +0 -2
- package/dist/src/components/property/property.test.d.ts.map +0 -1
- package/dist/src/components/property/property.test.js +0 -242
- package/dist/src/components/record/declaration.d.ts +0 -35
- package/dist/src/components/record/declaration.d.ts.map +0 -1
- package/dist/src/components/record/declaration.js +0 -90
- package/dist/src/components/record/declaration.test.d.ts +0 -2
- package/dist/src/components/record/declaration.test.d.ts.map +0 -1
- package/dist/src/components/record/declaration.test.js +0 -94
- package/dist/src/components/type-parameters/type-parameter-constraints.d.ts +0 -8
- package/dist/src/components/type-parameters/type-parameter-constraints.d.ts.map +0 -1
- package/dist/src/components/type-parameters/type-parameter-constraints.js +0 -44
- package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts +0 -2
- package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts.map +0 -1
- package/dist/src/components/type-parameters/type-parameter-constraints.test.js +0 -67
- package/dist/src/components/type-parameters/type-parameter.d.ts +0 -20
- package/dist/src/components/type-parameters/type-parameter.d.ts.map +0 -1
- package/dist/src/components/type-parameters/type-parameter.js +0 -22
- package/dist/src/components/type-parameters/type-parameters.d.ts +0 -17
- package/dist/src/components/type-parameters/type-parameters.d.ts.map +0 -1
- package/dist/src/components/type-parameters/type-parameters.js +0 -54
- package/dist/src/components/type-parameters/type-parameters.test.d.ts +0 -2
- package/dist/src/components/type-parameters/type-parameters.test.d.ts.map +0 -1
- package/dist/src/components/type-parameters/type-parameters.test.js +0 -48
- package/dist/src/components/var/declaration.d.ts +0 -35
- package/dist/src/components/var/declaration.d.ts.map +0 -1
- package/dist/src/components/var/declaration.js +0 -40
- package/dist/src/components/var/declaration.test.d.ts +0 -2
- package/dist/src/components/var/declaration.test.d.ts.map +0 -1
- package/dist/src/components/var/declaration.test.js +0 -73
- package/dist/test/class-declaration.test.d.ts +0 -2
- package/dist/test/class-declaration.test.d.ts.map +0 -1
- package/dist/test/project-directory.test.d.ts +0 -2
- package/dist/test/project-directory.test.d.ts.map +0 -1
- package/dist/test/vitest.setup.d.ts +0 -2
- package/dist/test/vitest.setup.d.ts.map +0 -1
- package/dist/test/vitest.setup.js +0 -1
- package/src/components/attributes/attributes.test.tsx +0 -61
- package/src/components/attributes/attributes.tsx +0 -100
- package/src/components/doc/comment.test.tsx +0 -337
- package/src/components/doc/comment.tsx +0 -152
- package/src/components/doc/from-markdown.test.tsx +0 -103
- package/src/components/doc/from-markdown.tsx +0 -58
- package/src/components/interface/declaration.test.tsx +0 -158
- package/src/components/interface/declaration.tsx +0 -125
- package/src/components/interface/method.test.tsx +0 -293
- package/src/components/interface/method.tsx +0 -122
- package/src/components/interface/property.test.tsx +0 -165
- package/src/components/interface/property.tsx +0 -127
- package/src/components/parameters/parameters.tsx +0 -74
- package/src/components/property/property.test.tsx +0 -209
- package/src/components/property/property.tsx +0 -172
- package/src/components/record/declaration.test.tsx +0 -73
- package/src/components/record/declaration.tsx +0 -109
- package/src/components/type-parameters/type-parameter-constraints.test.tsx +0 -93
- package/src/components/type-parameters/type-parameter-constraints.tsx +0 -46
- package/src/components/type-parameters/type-parameter.tsx +0 -35
- package/src/components/type-parameters/type-parameters.test.tsx +0 -46
- package/src/components/type-parameters/type-parameters.tsx +0 -63
- package/src/components/var/declaration.test.tsx +0 -59
- package/src/components/var/declaration.tsx +0 -47
- package/test/class-declaration.test.tsx +0 -430
- package/test/vitest.setup.ts +0 -1
|
@@ -1,165 +0,0 @@
|
|
|
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 { Attribute } from "../attributes/attributes.jsx";
|
|
5
|
-
import { InterfaceDeclaration } from "./declaration.jsx";
|
|
6
|
-
import { InterfaceProperty } from "./property.jsx";
|
|
7
|
-
|
|
8
|
-
const Wrapper = (props: { children: Children }) => (
|
|
9
|
-
<TestNamespace>
|
|
10
|
-
<InterfaceDeclaration public name="TestInterface">
|
|
11
|
-
{props.children}
|
|
12
|
-
</InterfaceDeclaration>
|
|
13
|
-
</TestNamespace>
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
describe("modifiers", () => {
|
|
17
|
-
describe("access modifiers", () => {
|
|
18
|
-
it.each(["public", "private", "protected", "internal"] as const)(
|
|
19
|
-
"%s",
|
|
20
|
-
(accessModifier) => {
|
|
21
|
-
expect(
|
|
22
|
-
<Wrapper>
|
|
23
|
-
<InterfaceProperty
|
|
24
|
-
{...{ [accessModifier]: true }}
|
|
25
|
-
name="TestProp"
|
|
26
|
-
type="string"
|
|
27
|
-
get
|
|
28
|
-
/>
|
|
29
|
-
</Wrapper>,
|
|
30
|
-
).toRenderTo(`
|
|
31
|
-
public interface TestInterface
|
|
32
|
-
{
|
|
33
|
-
${accessModifier} string TestProp { get; }
|
|
34
|
-
}
|
|
35
|
-
`);
|
|
36
|
-
},
|
|
37
|
-
);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
describe("method modifiers", () => {
|
|
41
|
-
it.each(["new"] as const)("%s", (methodModifier) => {
|
|
42
|
-
expect(
|
|
43
|
-
<Wrapper>
|
|
44
|
-
<InterfaceProperty
|
|
45
|
-
{...{ [methodModifier]: true }}
|
|
46
|
-
name="TestProp"
|
|
47
|
-
type="string"
|
|
48
|
-
get
|
|
49
|
-
/>
|
|
50
|
-
</Wrapper>,
|
|
51
|
-
).toRenderTo(`
|
|
52
|
-
public interface TestInterface
|
|
53
|
-
{
|
|
54
|
-
${methodModifier} string TestProp { get; }
|
|
55
|
-
}
|
|
56
|
-
`);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("combine modifiers", () => {
|
|
61
|
-
expect(
|
|
62
|
-
<Wrapper>
|
|
63
|
-
<InterfaceProperty public new name="TestProp" type="string" get />
|
|
64
|
-
</Wrapper>,
|
|
65
|
-
).toRenderTo(`
|
|
66
|
-
public interface TestInterface
|
|
67
|
-
{
|
|
68
|
-
public new string TestProp { get; }
|
|
69
|
-
}
|
|
70
|
-
`);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("applies PascalCase naming policy", () => {
|
|
75
|
-
expect(
|
|
76
|
-
<Wrapper>
|
|
77
|
-
<InterfaceProperty name="test_prop" type="string" get />
|
|
78
|
-
</Wrapper>,
|
|
79
|
-
).toRenderTo(`
|
|
80
|
-
public interface TestInterface
|
|
81
|
-
{
|
|
82
|
-
string TestProp { get; }
|
|
83
|
-
}
|
|
84
|
-
`);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it("has getter only", () => {
|
|
88
|
-
expect(
|
|
89
|
-
<Wrapper>
|
|
90
|
-
<InterfaceProperty name="TestProp" type="string" get />
|
|
91
|
-
</Wrapper>,
|
|
92
|
-
).toRenderTo(`
|
|
93
|
-
public interface TestInterface
|
|
94
|
-
{
|
|
95
|
-
string TestProp { get; }
|
|
96
|
-
}
|
|
97
|
-
`);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it("has setter only", () => {
|
|
101
|
-
expect(
|
|
102
|
-
<Wrapper>
|
|
103
|
-
<InterfaceProperty name="TestProp" type="string" set />
|
|
104
|
-
</Wrapper>,
|
|
105
|
-
).toRenderTo(`
|
|
106
|
-
public interface TestInterface
|
|
107
|
-
{
|
|
108
|
-
string TestProp { set; }
|
|
109
|
-
}
|
|
110
|
-
`);
|
|
111
|
-
});
|
|
112
|
-
it("has getter and setter", () => {
|
|
113
|
-
expect(
|
|
114
|
-
<Wrapper>
|
|
115
|
-
<InterfaceProperty name="TestProp" type="string" get set />
|
|
116
|
-
</Wrapper>,
|
|
117
|
-
).toRenderTo(`
|
|
118
|
-
public interface TestInterface
|
|
119
|
-
{
|
|
120
|
-
string TestProp { get; set; }
|
|
121
|
-
}
|
|
122
|
-
`);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it("specify doc comment", () => {
|
|
126
|
-
expect(
|
|
127
|
-
<TestNamespace>
|
|
128
|
-
<InterfaceDeclaration name="Test">
|
|
129
|
-
<InterfaceProperty
|
|
130
|
-
name="Method"
|
|
131
|
-
type="string"
|
|
132
|
-
get
|
|
133
|
-
set
|
|
134
|
-
doc="This is a test"
|
|
135
|
-
/>
|
|
136
|
-
</InterfaceDeclaration>
|
|
137
|
-
</TestNamespace>,
|
|
138
|
-
).toRenderTo(`
|
|
139
|
-
interface Test
|
|
140
|
-
{
|
|
141
|
-
/// This is a test
|
|
142
|
-
string Method { get; set; }
|
|
143
|
-
}
|
|
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
|
-
});
|
|
@@ -1,127 +0,0 @@
|
|
|
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 { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
|
|
20
|
-
import { DocWhen } from "../doc/comment.jsx";
|
|
21
|
-
|
|
22
|
-
/** Method modifiers. Can only be one. */
|
|
23
|
-
export interface InterfacePropertyModifiers {
|
|
24
|
-
readonly new?: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const getModifiers = makeModifiers<InterfacePropertyModifiers>(["new"]);
|
|
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
|
-
/** Doc comment */
|
|
46
|
-
doc?: Children;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Property initializer
|
|
50
|
-
* @example `<ClassProperty name="My" get set nullable />`
|
|
51
|
-
*
|
|
52
|
-
* ```cs
|
|
53
|
-
* int? My { get; set; };
|
|
54
|
-
* ```
|
|
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;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Render a C# interface property.
|
|
79
|
-
*
|
|
80
|
-
* @example `<InterfaceProperty public name="My" get set />`
|
|
81
|
-
*
|
|
82
|
-
* ```cs
|
|
83
|
-
* public int My { get; set; };
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
export function InterfaceProperty(props: InterfacePropertyProps) {
|
|
87
|
-
const name = useCSharpNamePolicy().getName(props.name, "class-property");
|
|
88
|
-
const scope = useCSharpScope();
|
|
89
|
-
if (scope.kind !== "member" || scope.name !== "interface-decl") {
|
|
90
|
-
throw new Error(
|
|
91
|
-
"can't define an interface method outside of an interface scope",
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const propertySymbol = new CSharpOutputSymbol(name, {
|
|
96
|
-
scope,
|
|
97
|
-
refkeys: props.refkey ?? refkey(props.name),
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
// scope for property declaration
|
|
101
|
-
const propertyScope = new CSharpMemberScope("property-decl", {
|
|
102
|
-
owner: propertySymbol,
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
const modifiers = computeModifiersPrefix([
|
|
106
|
-
getAccessModifier(props),
|
|
107
|
-
getModifiers(props),
|
|
108
|
-
]);
|
|
109
|
-
// note that scope wraps the method decl so that the params get the correct scope
|
|
110
|
-
return (
|
|
111
|
-
<MemberDeclaration symbol={propertySymbol}>
|
|
112
|
-
<Scope value={propertyScope}>
|
|
113
|
-
<DocWhen doc={props.doc} />
|
|
114
|
-
<AttributeList attributes={props.attributes} endline />
|
|
115
|
-
{modifiers}
|
|
116
|
-
{props.type}
|
|
117
|
-
{props.nullable && "?"} {name}{" "}
|
|
118
|
-
<Block newline inline>
|
|
119
|
-
<List joiner=" ">
|
|
120
|
-
{props.get && "get;"}
|
|
121
|
-
{props.set && "set;"}
|
|
122
|
-
</List>
|
|
123
|
-
</Block>
|
|
124
|
-
</Scope>
|
|
125
|
-
</MemberDeclaration>
|
|
126
|
-
);
|
|
127
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Children,
|
|
3
|
-
code,
|
|
4
|
-
Declaration,
|
|
5
|
-
For,
|
|
6
|
-
Indent,
|
|
7
|
-
OutputSymbol,
|
|
8
|
-
refkey,
|
|
9
|
-
Refkey,
|
|
10
|
-
} from "@alloy-js/core";
|
|
11
|
-
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
12
|
-
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
13
|
-
import { useCSharpScope } from "../../symbols/scopes.js";
|
|
14
|
-
import { Name } from "../Name.jsx";
|
|
15
|
-
|
|
16
|
-
export interface ParameterProps {
|
|
17
|
-
name: string;
|
|
18
|
-
type: Children;
|
|
19
|
-
/** If the parmaeter is optional(without default value) */
|
|
20
|
-
optional?: boolean;
|
|
21
|
-
/** Default value for the parameter */
|
|
22
|
-
default?: Children;
|
|
23
|
-
refkey?: Refkey;
|
|
24
|
-
symbol?: OutputSymbol;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** Define a parameter to be used in class or interface method. */
|
|
28
|
-
export function Parameter(props: ParameterProps) {
|
|
29
|
-
const name = useCSharpNamePolicy().getName(props.name, "parameter");
|
|
30
|
-
const scope = useCSharpScope();
|
|
31
|
-
if (
|
|
32
|
-
scope.kind !== "member" ||
|
|
33
|
-
(scope.name !== "constructor-decl" && scope.name !== "method-decl")
|
|
34
|
-
) {
|
|
35
|
-
throw new Error(
|
|
36
|
-
"can't define a parameter outside of a constructor-decl or method-decl scope",
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const memberSymbol = new CSharpOutputSymbol(name, {
|
|
41
|
-
scope,
|
|
42
|
-
refkeys: props.refkey ?? refkey(props.name),
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
return (
|
|
46
|
-
<Declaration symbol={memberSymbol}>
|
|
47
|
-
{props.type}
|
|
48
|
-
{props.optional ? "?" : ""} <Name />
|
|
49
|
-
{props.default ? code` = ${props.default}` : ""}
|
|
50
|
-
</Declaration>
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface ParametersProps {
|
|
55
|
-
parameters: ParameterProps[] | undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Render a collection of parameters */
|
|
59
|
-
export function Parameters(props: ParametersProps) {
|
|
60
|
-
return (
|
|
61
|
-
<group>
|
|
62
|
-
{"("}
|
|
63
|
-
{props.parameters && (
|
|
64
|
-
<Indent softline>
|
|
65
|
-
<For each={props.parameters} joiner={", "}>
|
|
66
|
-
{(param) => <Parameter {...param} />}
|
|
67
|
-
</For>
|
|
68
|
-
</Indent>
|
|
69
|
-
)}
|
|
70
|
-
<softline />
|
|
71
|
-
{")"}
|
|
72
|
-
</group>
|
|
73
|
-
);
|
|
74
|
-
}
|
|
@@ -1,209 +0,0 @@
|
|
|
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 { Attribute } from "../attributes/attributes.jsx";
|
|
5
|
-
import { ClassDeclaration } from "../ClassDeclaration.jsx";
|
|
6
|
-
import { Property } from "./property.jsx";
|
|
7
|
-
|
|
8
|
-
const Wrapper = (props: { children: Children }) => (
|
|
9
|
-
<TestNamespace>
|
|
10
|
-
<ClassDeclaration public name="TestClass">
|
|
11
|
-
{props.children}
|
|
12
|
-
</ClassDeclaration>
|
|
13
|
-
</TestNamespace>
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
describe("modifiers", () => {
|
|
17
|
-
describe("access modifiers", () => {
|
|
18
|
-
it.each(["public", "private", "protected", "internal"] as const)(
|
|
19
|
-
"%s",
|
|
20
|
-
(accessModifier) => {
|
|
21
|
-
expect(
|
|
22
|
-
<Wrapper>
|
|
23
|
-
<Property
|
|
24
|
-
{...{ [accessModifier]: true }}
|
|
25
|
-
name="TestProp"
|
|
26
|
-
type="string"
|
|
27
|
-
get
|
|
28
|
-
/>
|
|
29
|
-
</Wrapper>,
|
|
30
|
-
).toRenderTo(`
|
|
31
|
-
public class TestClass
|
|
32
|
-
{
|
|
33
|
-
${accessModifier} string TestProp { get; }
|
|
34
|
-
}
|
|
35
|
-
`);
|
|
36
|
-
},
|
|
37
|
-
);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
describe("property modifiers", () => {
|
|
41
|
-
it.each([
|
|
42
|
-
"new",
|
|
43
|
-
"static",
|
|
44
|
-
"virtual",
|
|
45
|
-
"sealed",
|
|
46
|
-
"override",
|
|
47
|
-
"abstract",
|
|
48
|
-
"extern",
|
|
49
|
-
"readonly",
|
|
50
|
-
"required",
|
|
51
|
-
] as const)("%s", (methodModifier) => {
|
|
52
|
-
expect(
|
|
53
|
-
<Wrapper>
|
|
54
|
-
<Property
|
|
55
|
-
{...{ [methodModifier]: true }}
|
|
56
|
-
name="TestProp"
|
|
57
|
-
type="string"
|
|
58
|
-
get
|
|
59
|
-
/>
|
|
60
|
-
</Wrapper>,
|
|
61
|
-
).toRenderTo(`
|
|
62
|
-
public class TestClass
|
|
63
|
-
{
|
|
64
|
-
${methodModifier} string TestProp { get; }
|
|
65
|
-
}
|
|
66
|
-
`);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it("combine modifiers", () => {
|
|
71
|
-
expect(
|
|
72
|
-
<Wrapper>
|
|
73
|
-
<Property public new name="TestProp" type="string" get />
|
|
74
|
-
</Wrapper>,
|
|
75
|
-
).toRenderTo(`
|
|
76
|
-
public class TestClass
|
|
77
|
-
{
|
|
78
|
-
public new string TestProp { get; }
|
|
79
|
-
}
|
|
80
|
-
`);
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("applies PascalCase naming policy", () => {
|
|
85
|
-
expect(
|
|
86
|
-
<Wrapper>
|
|
87
|
-
<Property name="test_prop" type="string" get />
|
|
88
|
-
</Wrapper>,
|
|
89
|
-
).toRenderTo(`
|
|
90
|
-
public class TestClass
|
|
91
|
-
{
|
|
92
|
-
string TestProp { get; }
|
|
93
|
-
}
|
|
94
|
-
`);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it("has getter only", () => {
|
|
98
|
-
expect(
|
|
99
|
-
<Wrapper>
|
|
100
|
-
<Property name="TestProp" type="string" get />
|
|
101
|
-
</Wrapper>,
|
|
102
|
-
).toRenderTo(`
|
|
103
|
-
public class TestClass
|
|
104
|
-
{
|
|
105
|
-
string TestProp { get; }
|
|
106
|
-
}
|
|
107
|
-
`);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it("has setter only", () => {
|
|
111
|
-
expect(
|
|
112
|
-
<Wrapper>
|
|
113
|
-
<Property name="TestProp" type="string" set />
|
|
114
|
-
</Wrapper>,
|
|
115
|
-
).toRenderTo(`
|
|
116
|
-
public class TestClass
|
|
117
|
-
{
|
|
118
|
-
string TestProp { set; }
|
|
119
|
-
}
|
|
120
|
-
`);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it("has getter and setter", () => {
|
|
124
|
-
expect(
|
|
125
|
-
<Wrapper>
|
|
126
|
-
<Property name="TestProp" type="string" get set />
|
|
127
|
-
</Wrapper>,
|
|
128
|
-
).toRenderTo(`
|
|
129
|
-
public class TestClass
|
|
130
|
-
{
|
|
131
|
-
string TestProp { get; set; }
|
|
132
|
-
}
|
|
133
|
-
`);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it("has getter and init", () => {
|
|
137
|
-
expect(
|
|
138
|
-
<Wrapper>
|
|
139
|
-
<Property name="TestProp" type="string" get init />
|
|
140
|
-
</Wrapper>,
|
|
141
|
-
).toRenderTo(`
|
|
142
|
-
public class TestClass
|
|
143
|
-
{
|
|
144
|
-
string TestProp { get; init; }
|
|
145
|
-
}
|
|
146
|
-
`);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("specify doc comment", () => {
|
|
150
|
-
expect(
|
|
151
|
-
<TestNamespace>
|
|
152
|
-
<ClassDeclaration name="Test">
|
|
153
|
-
<Property name="Method" type="string" get set doc="This is a test" />
|
|
154
|
-
</ClassDeclaration>
|
|
155
|
-
</TestNamespace>,
|
|
156
|
-
).toRenderTo(`
|
|
157
|
-
class Test
|
|
158
|
-
{
|
|
159
|
-
/// This is a test
|
|
160
|
-
string Method { get; set; }
|
|
161
|
-
}
|
|
162
|
-
`);
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it("specify nullable property", () => {
|
|
166
|
-
expect(
|
|
167
|
-
<Wrapper>
|
|
168
|
-
<Property name="TestProp" type="string" nullable get set />
|
|
169
|
-
</Wrapper>,
|
|
170
|
-
).toRenderTo(`
|
|
171
|
-
public class TestClass
|
|
172
|
-
{
|
|
173
|
-
string? TestProp { get; set; }
|
|
174
|
-
}
|
|
175
|
-
`);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it("specify initializer", () => {
|
|
179
|
-
expect(
|
|
180
|
-
<Wrapper>
|
|
181
|
-
<Property name="TestProp" type="string" get set initializer={`"abc"`} />
|
|
182
|
-
</Wrapper>,
|
|
183
|
-
).toRenderTo(`
|
|
184
|
-
public class TestClass
|
|
185
|
-
{
|
|
186
|
-
string TestProp { get; set; } = "abc";
|
|
187
|
-
}
|
|
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
|
-
});
|