@alloy-js/csharp 0.18.0-dev.2 → 0.18.0-dev.20
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/LICENSE +7 -0
- package/dist/src/components/ClassDeclaration.d.ts +74 -0
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
- package/dist/src/components/{Class.js → ClassDeclaration.js} +59 -32
- package/dist/src/components/ClassMethod.d.ts +25 -4
- package/dist/src/components/ClassMethod.d.ts.map +1 -1
- package/dist/src/components/ClassMethod.js +22 -3
- 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/Parameters.d.ts +6 -1
- package/dist/src/components/Parameters.d.ts.map +1 -1
- package/dist/src/components/Parameters.js +3 -2
- 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 +12 -3
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +13 -4
- package/dist/src/components/interface/declaration.d.ts +47 -0
- package/dist/src/components/interface/declaration.d.ts.map +1 -0
- package/dist/src/components/interface/declaration.js +77 -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 +153 -0
- package/dist/src/components/interface/method.d.ts +32 -0
- package/dist/src/components/interface/method.d.ts.map +1 -0
- package/dist/src/components/interface/method.js +69 -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 +254 -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/property/property.d.ts +57 -0
- package/dist/src/components/property/property.d.ts.map +1 -0
- package/dist/src/components/property/property.js +70 -0
- package/dist/src/components/property/property.test.d.ts +2 -0
- package/dist/src/components/property/property.test.d.ts.map +1 -0
- package/dist/src/components/property/property.test.js +218 -0
- package/dist/src/components/record/declaration.d.ts +35 -0
- package/dist/src/components/record/declaration.d.ts.map +1 -0
- package/dist/src/components/record/declaration.js +90 -0
- package/dist/src/components/record/declaration.test.d.ts +2 -0
- package/dist/src/components/record/declaration.test.d.ts.map +1 -0
- package/dist/src/components/record/declaration.test.js +94 -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/components/type-parameters/type-parameter-constraints.d.ts +8 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.d.ts.map +1 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.js +44 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts +2 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts.map +1 -0
- package/dist/src/components/type-parameters/type-parameter-constraints.test.js +67 -0
- package/dist/src/components/type-parameters/type-parameter.d.ts +20 -0
- package/dist/src/components/type-parameters/type-parameter.d.ts.map +1 -0
- package/dist/src/components/type-parameters/type-parameter.js +22 -0
- package/dist/src/components/type-parameters/type-parameters.d.ts +17 -0
- package/dist/src/components/type-parameters/type-parameters.d.ts.map +1 -0
- package/dist/src/components/type-parameters/type-parameters.js +54 -0
- package/dist/src/components/type-parameters/type-parameters.test.d.ts +2 -0
- package/dist/src/components/type-parameters/type-parameters.test.d.ts.map +1 -0
- package/dist/src/components/type-parameters/type-parameters.test.js +48 -0
- package/dist/src/components/var/declaration.d.ts +35 -0
- package/dist/src/components/var/declaration.d.ts.map +1 -0
- package/dist/src/components/var/declaration.js +40 -0
- package/dist/src/components/var/declaration.test.d.ts +2 -0
- package/dist/src/components/var/declaration.test.d.ts.map +1 -0
- package/dist/src/components/var/declaration.test.js +73 -0
- package/dist/src/modifiers.d.ts +10 -4
- package/dist/src/modifiers.d.ts.map +1 -1
- package/dist/src/modifiers.js +9 -32
- 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 +2 -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-declaration.test.js +463 -0
- package/dist/test/class-method.test.js +37 -14
- 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/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 +21 -21
- package/src/components/{Class.tsx → ClassDeclaration.tsx} +108 -43
- package/src/components/ClassMethod.tsx +53 -12
- package/src/components/{Enum.tsx → EnumDeclaration.tsx} +30 -6
- package/src/components/Parameters.tsx +10 -3
- 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 +12 -3
- package/src/components/interface/declaration.test.tsx +143 -0
- package/src/components/interface/declaration.tsx +105 -0
- package/src/components/interface/method.test.tsx +250 -0
- package/src/components/interface/method.tsx +105 -0
- package/src/components/interface/property.test.tsx +144 -0
- package/src/components/interface/property.tsx +107 -0
- package/src/components/property/property.test.tsx +187 -0
- package/src/components/property/property.tsx +146 -0
- package/src/components/record/declaration.test.tsx +73 -0
- package/src/components/record/declaration.tsx +109 -0
- package/src/components/stc/index.ts +2 -2
- package/src/components/type-parameters/type-parameter-constraints.test.tsx +93 -0
- package/src/components/type-parameters/type-parameter-constraints.tsx +46 -0
- package/src/components/type-parameters/type-parameter.tsx +35 -0
- package/src/components/type-parameters/type-parameters.test.tsx +46 -0
- package/src/components/type-parameters/type-parameters.tsx +63 -0
- package/src/components/var/declaration.test.tsx +59 -0
- package/src/components/var/declaration.tsx +47 -0
- package/src/modifiers.ts +25 -42
- package/src/name-policy.ts +5 -0
- package/temp/api.json +5227 -685
- package/test/class-declaration.test.tsx +418 -0
- package/test/class-method.test.tsx +24 -14
- 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/vitest.setup.ts +1 -0
- package/vitest.config.ts +3 -0
- package/dist/src/components/Class.d.ts +0 -26
- 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/class.test.js +0 -298
- package/dist/test/projectdirectory.test.d.ts +0 -2
- package/dist/test/projectdirectory.test.d.ts.map +0 -1
- package/test/class.test.tsx +0 -292
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createComponent as _$createComponent, memo as _$memo } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { Block, List, MemberDeclaration, refkey, Scope } from "@alloy-js/core";
|
|
3
|
+
import { computeModifiersPrefix, getAccessModifier, makeModifiers } from "../../modifiers.js";
|
|
4
|
+
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
5
|
+
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
6
|
+
import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
|
|
7
|
+
import { DocWhen } from "../doc/comment.js";
|
|
8
|
+
|
|
9
|
+
/** Method modifiers. Can only be one. */
|
|
10
|
+
|
|
11
|
+
const getModifiers = makeModifiers(["new"]);
|
|
12
|
+
|
|
13
|
+
// properties for creating a method
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Render a C# interface property.
|
|
17
|
+
*
|
|
18
|
+
* @example `<InterfaceProperty public name="My" get set />`
|
|
19
|
+
*
|
|
20
|
+
* ```cs
|
|
21
|
+
* public int My { get; set; };
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function InterfaceProperty(props) {
|
|
25
|
+
const name = useCSharpNamePolicy().getName(props.name, "class-property");
|
|
26
|
+
const scope = useCSharpScope();
|
|
27
|
+
if (scope.kind !== "member" || scope.name !== "interface-decl") {
|
|
28
|
+
throw new Error("can't define an interface method outside of an interface scope");
|
|
29
|
+
}
|
|
30
|
+
const propertySymbol = new CSharpOutputSymbol(name, {
|
|
31
|
+
scope,
|
|
32
|
+
refkeys: props.refkey ?? refkey(props.name)
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// scope for property declaration
|
|
36
|
+
const propertyScope = new CSharpMemberScope("property-decl", {
|
|
37
|
+
owner: propertySymbol
|
|
38
|
+
});
|
|
39
|
+
const modifiers = computeModifiersPrefix([getAccessModifier(props), getModifiers(props)]);
|
|
40
|
+
// note that scope wraps the method decl so that the params get the correct scope
|
|
41
|
+
return _$createComponent(MemberDeclaration, {
|
|
42
|
+
symbol: propertySymbol,
|
|
43
|
+
get children() {
|
|
44
|
+
return _$createComponent(Scope, {
|
|
45
|
+
value: propertyScope,
|
|
46
|
+
get children() {
|
|
47
|
+
return [_$createComponent(DocWhen, {
|
|
48
|
+
get doc() {
|
|
49
|
+
return props.doc;
|
|
50
|
+
}
|
|
51
|
+
}), modifiers, _$memo(() => props.type), _$memo(() => props.nullable && "?"), " ", name, " ", _$createComponent(Block, {
|
|
52
|
+
newline: true,
|
|
53
|
+
inline: true,
|
|
54
|
+
get children() {
|
|
55
|
+
return _$createComponent(List, {
|
|
56
|
+
joiner: " ",
|
|
57
|
+
get children() {
|
|
58
|
+
return [_$memo(() => props.get && "get;"), _$memo(() => props.set && "set;")];
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
})];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"property.test.d.ts","sourceRoot":"","sources":["../../../../src/components/interface/property.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { memo as _$memo, createComponent as _$createComponent, mergeProps as _$mergeProps } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { TestNamespace } from "../../../test/utils.js";
|
|
4
|
+
import { InterfaceDeclaration } from "./declaration.js";
|
|
5
|
+
import { InterfaceProperty } from "./property.js";
|
|
6
|
+
const Wrapper = props => _$createComponent(TestNamespace, {
|
|
7
|
+
get children() {
|
|
8
|
+
return _$createComponent(InterfaceDeclaration, {
|
|
9
|
+
"public": true,
|
|
10
|
+
name: "TestInterface",
|
|
11
|
+
get children() {
|
|
12
|
+
return props.children;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
describe("modifiers", () => {
|
|
18
|
+
describe("access modifiers", () => {
|
|
19
|
+
it.each(["public", "private", "protected", "internal"])("%s", accessModifier => {
|
|
20
|
+
expect(_$createComponent(Wrapper, {
|
|
21
|
+
get children() {
|
|
22
|
+
return _$createComponent(InterfaceProperty, _$mergeProps({
|
|
23
|
+
[accessModifier]: true
|
|
24
|
+
}, {
|
|
25
|
+
name: "TestProp",
|
|
26
|
+
type: "string",
|
|
27
|
+
get: true
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
})).toRenderTo(`
|
|
31
|
+
public interface TestInterface
|
|
32
|
+
{
|
|
33
|
+
${accessModifier} string TestProp { get; }
|
|
34
|
+
}
|
|
35
|
+
`);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
describe("method modifiers", () => {
|
|
39
|
+
it.each(["new"])("%s", methodModifier => {
|
|
40
|
+
expect(_$createComponent(Wrapper, {
|
|
41
|
+
get children() {
|
|
42
|
+
return _$createComponent(InterfaceProperty, _$mergeProps({
|
|
43
|
+
[methodModifier]: true
|
|
44
|
+
}, {
|
|
45
|
+
name: "TestProp",
|
|
46
|
+
type: "string",
|
|
47
|
+
get: true
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
})).toRenderTo(`
|
|
51
|
+
public interface TestInterface
|
|
52
|
+
{
|
|
53
|
+
${methodModifier} string TestProp { get; }
|
|
54
|
+
}
|
|
55
|
+
`);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
it("combine modifiers", () => {
|
|
59
|
+
expect(_$createComponent(Wrapper, {
|
|
60
|
+
get children() {
|
|
61
|
+
return _$createComponent(InterfaceProperty, {
|
|
62
|
+
"public": true,
|
|
63
|
+
"new": true,
|
|
64
|
+
name: "TestProp",
|
|
65
|
+
type: "string",
|
|
66
|
+
get: true
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
})).toRenderTo(`
|
|
70
|
+
public interface TestInterface
|
|
71
|
+
{
|
|
72
|
+
public new string TestProp { get; }
|
|
73
|
+
}
|
|
74
|
+
`);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
it("applies PascalCase naming policy", () => {
|
|
78
|
+
expect(_$createComponent(Wrapper, {
|
|
79
|
+
get children() {
|
|
80
|
+
return _$createComponent(InterfaceProperty, {
|
|
81
|
+
name: "test_prop",
|
|
82
|
+
type: "string",
|
|
83
|
+
get: true
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
})).toRenderTo(`
|
|
87
|
+
public interface TestInterface
|
|
88
|
+
{
|
|
89
|
+
string TestProp { get; }
|
|
90
|
+
}
|
|
91
|
+
`);
|
|
92
|
+
});
|
|
93
|
+
it("has getter only", () => {
|
|
94
|
+
expect(_$createComponent(Wrapper, {
|
|
95
|
+
get children() {
|
|
96
|
+
return _$createComponent(InterfaceProperty, {
|
|
97
|
+
name: "TestProp",
|
|
98
|
+
type: "string",
|
|
99
|
+
get: true
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
})).toRenderTo(`
|
|
103
|
+
public interface TestInterface
|
|
104
|
+
{
|
|
105
|
+
string TestProp { get; }
|
|
106
|
+
}
|
|
107
|
+
`);
|
|
108
|
+
});
|
|
109
|
+
it("has setter only", () => {
|
|
110
|
+
expect(_$createComponent(Wrapper, {
|
|
111
|
+
get children() {
|
|
112
|
+
return _$createComponent(InterfaceProperty, {
|
|
113
|
+
name: "TestProp",
|
|
114
|
+
type: "string",
|
|
115
|
+
set: true
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
})).toRenderTo(`
|
|
119
|
+
public interface TestInterface
|
|
120
|
+
{
|
|
121
|
+
string TestProp { set; }
|
|
122
|
+
}
|
|
123
|
+
`);
|
|
124
|
+
});
|
|
125
|
+
it("has getter and setter", () => {
|
|
126
|
+
expect(_$createComponent(Wrapper, {
|
|
127
|
+
get children() {
|
|
128
|
+
return _$createComponent(InterfaceProperty, {
|
|
129
|
+
name: "TestProp",
|
|
130
|
+
type: "string",
|
|
131
|
+
get: true,
|
|
132
|
+
set: true
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
})).toRenderTo(`
|
|
136
|
+
public interface TestInterface
|
|
137
|
+
{
|
|
138
|
+
string TestProp { get; set; }
|
|
139
|
+
}
|
|
140
|
+
`);
|
|
141
|
+
});
|
|
142
|
+
it("specify doc comment", () => {
|
|
143
|
+
expect(_$createComponent(TestNamespace, {
|
|
144
|
+
get children() {
|
|
145
|
+
return _$createComponent(InterfaceDeclaration, {
|
|
146
|
+
name: "Test",
|
|
147
|
+
get children() {
|
|
148
|
+
return _$createComponent(InterfaceProperty, {
|
|
149
|
+
name: "Method",
|
|
150
|
+
type: "string",
|
|
151
|
+
get: true,
|
|
152
|
+
set: true,
|
|
153
|
+
doc: "This is a test"
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
})).toRenderTo(`
|
|
159
|
+
interface Test
|
|
160
|
+
{
|
|
161
|
+
/// This is a test
|
|
162
|
+
string Method { get; set; }
|
|
163
|
+
}
|
|
164
|
+
`);
|
|
165
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Children, Refkey } from "@alloy-js/core";
|
|
2
|
+
import { AccessModifiers } from "../../modifiers.js";
|
|
3
|
+
/** Property modifiers. */
|
|
4
|
+
export interface PropertyModifiers {
|
|
5
|
+
readonly new?: boolean;
|
|
6
|
+
readonly static?: boolean;
|
|
7
|
+
readonly virtual?: boolean;
|
|
8
|
+
readonly sealed?: boolean;
|
|
9
|
+
readonly override?: boolean;
|
|
10
|
+
readonly abstract?: boolean;
|
|
11
|
+
readonly extern?: boolean;
|
|
12
|
+
readonly readonly?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** Properties for {@link Property} component */
|
|
15
|
+
export interface PropertyProps extends AccessModifiers, PropertyModifiers {
|
|
16
|
+
name: string;
|
|
17
|
+
refkey?: Refkey;
|
|
18
|
+
/** Property type */
|
|
19
|
+
type: Children;
|
|
20
|
+
/** If property should have a getter */
|
|
21
|
+
get?: boolean;
|
|
22
|
+
/** If property should have a setter */
|
|
23
|
+
set?: boolean;
|
|
24
|
+
/** If property should only be set on the type creation */
|
|
25
|
+
init?: boolean;
|
|
26
|
+
/** Doc comment */
|
|
27
|
+
doc?: Children;
|
|
28
|
+
/**
|
|
29
|
+
* Property initializer
|
|
30
|
+
* @example `<ClassProperty name="My" get set nullable />`
|
|
31
|
+
*
|
|
32
|
+
* ```cs
|
|
33
|
+
* int? My { get; set; };
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
nullable?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Property initializer
|
|
39
|
+
* @example `<ClassProperty name="My" get set init={42} />`
|
|
40
|
+
*
|
|
41
|
+
* ```cs
|
|
42
|
+
* int My { get; set; } = 42;
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
initializer?: Children;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Render a C# class property.
|
|
49
|
+
*
|
|
50
|
+
* @example `<ClassProperty public name="My" get set />`
|
|
51
|
+
*
|
|
52
|
+
* ```cs
|
|
53
|
+
* public int My { get; set; };
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function Property(props: PropertyProps): Children;
|
|
57
|
+
//# sourceMappingURL=property.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"property.d.ts","sourceRoot":"","sources":["../../../../src/components/property/property.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,QAAQ,EAKR,MAAM,EAEP,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,eAAe,EAIhB,MAAM,oBAAoB,CAAC;AAM5B,0BAA0B;AAC1B,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;CAC7B;AAaD,gDAAgD;AAChD,MAAM,WAAW,aAAc,SAAQ,eAAe,EAAE,iBAAiB;IACvE,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,oBAAoB;IACpB,IAAI,EAAE,QAAQ,CAAC;IAEf,uCAAuC;IACvC,GAAG,CAAC,EAAE,OAAO,CAAC;IAEd,uCAAuC;IACvC,GAAG,CAAC,EAAE,OAAO,CAAC;IAEd,0DAA0D;IAC1D,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf,kBAAkB;IAClB,GAAG,CAAC,EAAE,QAAQ,CAAC;IAEf;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,QAAQ,CAAC;CACxB;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,aAAa,YAmD5C"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createComponent as _$createComponent, memo as _$memo } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { Block, code, List, MemberDeclaration, refkey, Scope } from "@alloy-js/core";
|
|
3
|
+
import { computeModifiersPrefix, getAccessModifier, makeModifiers } from "../../modifiers.js";
|
|
4
|
+
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
5
|
+
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
6
|
+
import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
|
|
7
|
+
import { DocWhen } from "../doc/comment.js";
|
|
8
|
+
|
|
9
|
+
/** Property modifiers. */
|
|
10
|
+
|
|
11
|
+
const getModifiers = makeModifiers(["new", "static", "virtual", "sealed", "override", "abstract", "extern", "readonly"]);
|
|
12
|
+
|
|
13
|
+
/** Properties for {@link Property} component */
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Render a C# class property.
|
|
17
|
+
*
|
|
18
|
+
* @example `<ClassProperty public name="My" get set />`
|
|
19
|
+
*
|
|
20
|
+
* ```cs
|
|
21
|
+
* public int My { get; set; };
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function Property(props) {
|
|
25
|
+
const name = useCSharpNamePolicy().getName(props.name, "class-property");
|
|
26
|
+
const scope = useCSharpScope();
|
|
27
|
+
if (scope.kind !== "member" || scope.name !== "class-decl" && scope.name !== "record-decl") {
|
|
28
|
+
throw new Error("can't define an interface method outside of an interface scope");
|
|
29
|
+
}
|
|
30
|
+
const propertySymbol = new CSharpOutputSymbol(name, {
|
|
31
|
+
scope,
|
|
32
|
+
refkeys: props.refkey ?? refkey(props.name)
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// scope for property declaration
|
|
36
|
+
const propertyScope = new CSharpMemberScope("property-decl", {
|
|
37
|
+
owner: propertySymbol
|
|
38
|
+
});
|
|
39
|
+
const modifiers = computeModifiersPrefix([getAccessModifier(props), getModifiers(props)]);
|
|
40
|
+
if (props.init && props.set) {
|
|
41
|
+
throw new Error(`Cannot use 'init' and 'set' together on property '${name}'`);
|
|
42
|
+
}
|
|
43
|
+
// note that scope wraps the method decl so that the params get the correct scope
|
|
44
|
+
return _$createComponent(MemberDeclaration, {
|
|
45
|
+
symbol: propertySymbol,
|
|
46
|
+
get children() {
|
|
47
|
+
return _$createComponent(Scope, {
|
|
48
|
+
value: propertyScope,
|
|
49
|
+
get children() {
|
|
50
|
+
return [_$createComponent(DocWhen, {
|
|
51
|
+
get doc() {
|
|
52
|
+
return props.doc;
|
|
53
|
+
}
|
|
54
|
+
}), modifiers, _$memo(() => props.type), _$memo(() => props.nullable && "?"), " ", name, " ", _$createComponent(Block, {
|
|
55
|
+
newline: true,
|
|
56
|
+
inline: true,
|
|
57
|
+
get children() {
|
|
58
|
+
return _$createComponent(List, {
|
|
59
|
+
joiner: " ",
|
|
60
|
+
get children() {
|
|
61
|
+
return [_$memo(() => props.get && "get;"), _$memo(() => props.set && "set;"), _$memo(() => props.init && "init;")];
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}), _$memo(() => props.initializer && code` = ${props.initializer};`)];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"property.test.d.ts","sourceRoot":"","sources":["../../../../src/components/property/property.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { memo as _$memo, createComponent as _$createComponent, mergeProps as _$mergeProps } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { TestNamespace } from "../../../test/utils.js";
|
|
4
|
+
import { ClassDeclaration } from "../ClassDeclaration.js";
|
|
5
|
+
import { Property } from "./property.js";
|
|
6
|
+
const Wrapper = props => _$createComponent(TestNamespace, {
|
|
7
|
+
get children() {
|
|
8
|
+
return _$createComponent(ClassDeclaration, {
|
|
9
|
+
"public": true,
|
|
10
|
+
name: "TestClass",
|
|
11
|
+
get children() {
|
|
12
|
+
return props.children;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
describe("modifiers", () => {
|
|
18
|
+
describe("access modifiers", () => {
|
|
19
|
+
it.each(["public", "private", "protected", "internal"])("%s", accessModifier => {
|
|
20
|
+
expect(_$createComponent(Wrapper, {
|
|
21
|
+
get children() {
|
|
22
|
+
return _$createComponent(Property, _$mergeProps({
|
|
23
|
+
[accessModifier]: true
|
|
24
|
+
}, {
|
|
25
|
+
name: "TestProp",
|
|
26
|
+
type: "string",
|
|
27
|
+
get: true
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
})).toRenderTo(`
|
|
31
|
+
public class TestClass
|
|
32
|
+
{
|
|
33
|
+
${accessModifier} string TestProp { get; }
|
|
34
|
+
}
|
|
35
|
+
`);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
describe("property modifiers", () => {
|
|
39
|
+
it.each(["new", "static", "virtual", "sealed", "override", "abstract", "extern", "readonly"])("%s", methodModifier => {
|
|
40
|
+
expect(_$createComponent(Wrapper, {
|
|
41
|
+
get children() {
|
|
42
|
+
return _$createComponent(Property, _$mergeProps({
|
|
43
|
+
[methodModifier]: true
|
|
44
|
+
}, {
|
|
45
|
+
name: "TestProp",
|
|
46
|
+
type: "string",
|
|
47
|
+
get: true
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
})).toRenderTo(`
|
|
51
|
+
public class TestClass
|
|
52
|
+
{
|
|
53
|
+
${methodModifier} string TestProp { get; }
|
|
54
|
+
}
|
|
55
|
+
`);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
it("combine modifiers", () => {
|
|
59
|
+
expect(_$createComponent(Wrapper, {
|
|
60
|
+
get children() {
|
|
61
|
+
return _$createComponent(Property, {
|
|
62
|
+
"public": true,
|
|
63
|
+
"new": true,
|
|
64
|
+
name: "TestProp",
|
|
65
|
+
type: "string",
|
|
66
|
+
get: true
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
})).toRenderTo(`
|
|
70
|
+
public class TestClass
|
|
71
|
+
{
|
|
72
|
+
public new string TestProp { get; }
|
|
73
|
+
}
|
|
74
|
+
`);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
it("applies PascalCase naming policy", () => {
|
|
78
|
+
expect(_$createComponent(Wrapper, {
|
|
79
|
+
get children() {
|
|
80
|
+
return _$createComponent(Property, {
|
|
81
|
+
name: "test_prop",
|
|
82
|
+
type: "string",
|
|
83
|
+
get: true
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
})).toRenderTo(`
|
|
87
|
+
public class TestClass
|
|
88
|
+
{
|
|
89
|
+
string TestProp { get; }
|
|
90
|
+
}
|
|
91
|
+
`);
|
|
92
|
+
});
|
|
93
|
+
it("has getter only", () => {
|
|
94
|
+
expect(_$createComponent(Wrapper, {
|
|
95
|
+
get children() {
|
|
96
|
+
return _$createComponent(Property, {
|
|
97
|
+
name: "TestProp",
|
|
98
|
+
type: "string",
|
|
99
|
+
get: true
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
})).toRenderTo(`
|
|
103
|
+
public class TestClass
|
|
104
|
+
{
|
|
105
|
+
string TestProp { get; }
|
|
106
|
+
}
|
|
107
|
+
`);
|
|
108
|
+
});
|
|
109
|
+
it("has setter only", () => {
|
|
110
|
+
expect(_$createComponent(Wrapper, {
|
|
111
|
+
get children() {
|
|
112
|
+
return _$createComponent(Property, {
|
|
113
|
+
name: "TestProp",
|
|
114
|
+
type: "string",
|
|
115
|
+
set: true
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
})).toRenderTo(`
|
|
119
|
+
public class TestClass
|
|
120
|
+
{
|
|
121
|
+
string TestProp { set; }
|
|
122
|
+
}
|
|
123
|
+
`);
|
|
124
|
+
});
|
|
125
|
+
it("has getter and setter", () => {
|
|
126
|
+
expect(_$createComponent(Wrapper, {
|
|
127
|
+
get children() {
|
|
128
|
+
return _$createComponent(Property, {
|
|
129
|
+
name: "TestProp",
|
|
130
|
+
type: "string",
|
|
131
|
+
get: true,
|
|
132
|
+
set: true
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
})).toRenderTo(`
|
|
136
|
+
public class TestClass
|
|
137
|
+
{
|
|
138
|
+
string TestProp { get; set; }
|
|
139
|
+
}
|
|
140
|
+
`);
|
|
141
|
+
});
|
|
142
|
+
it("has getter and init", () => {
|
|
143
|
+
expect(_$createComponent(Wrapper, {
|
|
144
|
+
get children() {
|
|
145
|
+
return _$createComponent(Property, {
|
|
146
|
+
name: "TestProp",
|
|
147
|
+
type: "string",
|
|
148
|
+
get: true,
|
|
149
|
+
init: true
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
})).toRenderTo(`
|
|
153
|
+
public class TestClass
|
|
154
|
+
{
|
|
155
|
+
string TestProp { get; init; }
|
|
156
|
+
}
|
|
157
|
+
`);
|
|
158
|
+
});
|
|
159
|
+
it("specify doc comment", () => {
|
|
160
|
+
expect(_$createComponent(TestNamespace, {
|
|
161
|
+
get children() {
|
|
162
|
+
return _$createComponent(ClassDeclaration, {
|
|
163
|
+
name: "Test",
|
|
164
|
+
get children() {
|
|
165
|
+
return _$createComponent(Property, {
|
|
166
|
+
name: "Method",
|
|
167
|
+
type: "string",
|
|
168
|
+
get: true,
|
|
169
|
+
set: true,
|
|
170
|
+
doc: "This is a test"
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
})).toRenderTo(`
|
|
176
|
+
class Test
|
|
177
|
+
{
|
|
178
|
+
/// This is a test
|
|
179
|
+
string Method { get; set; }
|
|
180
|
+
}
|
|
181
|
+
`);
|
|
182
|
+
});
|
|
183
|
+
it("specify nullable property", () => {
|
|
184
|
+
expect(_$createComponent(Wrapper, {
|
|
185
|
+
get children() {
|
|
186
|
+
return _$createComponent(Property, {
|
|
187
|
+
name: "TestProp",
|
|
188
|
+
type: "string",
|
|
189
|
+
nullable: true,
|
|
190
|
+
get: true,
|
|
191
|
+
set: true
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
})).toRenderTo(`
|
|
195
|
+
public class TestClass
|
|
196
|
+
{
|
|
197
|
+
string? TestProp { get; set; }
|
|
198
|
+
}
|
|
199
|
+
`);
|
|
200
|
+
});
|
|
201
|
+
it("specify initializer", () => {
|
|
202
|
+
expect(_$createComponent(Wrapper, {
|
|
203
|
+
get children() {
|
|
204
|
+
return _$createComponent(Property, {
|
|
205
|
+
name: "TestProp",
|
|
206
|
+
type: "string",
|
|
207
|
+
get: true,
|
|
208
|
+
set: true,
|
|
209
|
+
initializer: `"abc"`
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
})).toRenderTo(`
|
|
213
|
+
public class TestClass
|
|
214
|
+
{
|
|
215
|
+
string TestProp { get; set; } = "abc";
|
|
216
|
+
}
|
|
217
|
+
`);
|
|
218
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as core from "@alloy-js/core";
|
|
2
|
+
import { AccessModifiers } from "../../modifiers.js";
|
|
3
|
+
export interface RecordModifiers {
|
|
4
|
+
readonly partial?: boolean;
|
|
5
|
+
}
|
|
6
|
+
/** Props to use the {@link RecordDeclaration} component */
|
|
7
|
+
export interface RecordDeclarationProps extends Omit<core.DeclarationProps, "nameKind">, AccessModifiers, RecordModifiers {
|
|
8
|
+
name: string;
|
|
9
|
+
/** Doc comment */
|
|
10
|
+
doc?: core.Children;
|
|
11
|
+
refkey?: core.Refkey;
|
|
12
|
+
typeParameters?: Record<string, core.Refkey>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* CSharp record declaration.
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* <RecordDeclaration public name="IMyRecord">
|
|
19
|
+
* <RecordMember public name="MyProperty" type="int" />
|
|
20
|
+
* <RecordMethod public name="MyMethod" returnType="void">
|
|
21
|
+
* <Parameter name="value" type="int" />
|
|
22
|
+
* </RecordMethod>
|
|
23
|
+
* </RecordDeclaration>
|
|
24
|
+
* ```
|
|
25
|
+
* This will produce:
|
|
26
|
+
* ```csharp
|
|
27
|
+
* public record MyIface
|
|
28
|
+
* {
|
|
29
|
+
* public int MyProperty { get; set; }
|
|
30
|
+
* public void MyMethod(int value);
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function RecordDeclaration(props: RecordDeclarationProps): core.Children;
|
|
35
|
+
//# sourceMappingURL=declaration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"declaration.d.ts","sourceRoot":"","sources":["../../../../src/components/record/declaration.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,gBAAgB,CAAC;AACvC,OAAO,EACL,eAAe,EAIhB,MAAM,oBAAoB,CAAC;AAO5B,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;CAC5B;AAID,2DAA2D;AAC3D,MAAM,WAAW,sBACf,SAAQ,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,UAAU,CAAC,EAC7C,eAAe,EACf,eAAe;IACjB,IAAI,EAAE,MAAM,CAAC;IAEb,kBAAkB;IAClB,GAAG,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC;IACpB,MAAM,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;CAC9C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,sBAAsB,iBAwD9D"}
|