@alloy-js/csharp 0.18.0-dev.9 → 0.18.0
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/CHANGELOG.md +31 -0
- package/dist/src/components/ClassDeclaration.d.ts +39 -2
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.js +35 -34
- package/dist/src/components/ClassMethod.d.ts +33 -1
- package/dist/src/components/ClassMethod.d.ts.map +1 -1
- package/dist/src/components/ClassMethod.js +22 -7
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +1 -0
- package/dist/src/components/attributes/attributes.d.ts +39 -0
- package/dist/src/components/attributes/attributes.d.ts.map +1 -0
- package/dist/src/components/attributes/attributes.js +62 -0
- package/dist/src/components/attributes/attributes.test.d.ts +2 -0
- package/dist/src/components/attributes/attributes.test.d.ts.map +1 -0
- package/dist/src/components/attributes/attributes.test.js +75 -0
- package/dist/src/components/index.d.ts +7 -1
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +8 -2
- package/dist/src/components/interface/declaration.d.ts +32 -1
- package/dist/src/components/interface/declaration.d.ts.map +1 -1
- package/dist/src/components/interface/declaration.js +18 -25
- package/dist/src/components/interface/declaration.test.js +102 -0
- package/dist/src/components/interface/method.d.ts +33 -1
- package/dist/src/components/interface/method.d.ts.map +1 -1
- package/dist/src/components/interface/method.js +22 -7
- package/dist/src/components/interface/method.test.js +169 -0
- package/dist/src/components/interface/property.d.ts +36 -1
- package/dist/src/components/interface/property.d.ts.map +1 -1
- package/dist/src/components/interface/property.js +18 -4
- package/dist/src/components/interface/property.test.js +24 -0
- package/dist/src/components/parameters/parameters.d.ts +19 -0
- package/dist/src/components/parameters/parameters.d.ts.map +1 -0
- package/dist/src/components/parameters/parameters.js +43 -0
- package/dist/src/components/property/property.d.ts +80 -0
- package/dist/src/components/property/property.d.ts.map +1 -0
- package/dist/src/components/property/property.js +76 -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 +242 -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/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/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} +161 -33
- package/dist/test/project-directory.test.d.ts +2 -0
- package/dist/test/project-directory.test.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -6
- package/src/components/ClassDeclaration.tsx +65 -33
- package/src/components/ClassMethod.tsx +46 -4
- package/src/components/SourceFile.tsx +1 -0
- package/src/components/attributes/attributes.test.tsx +61 -0
- package/src/components/attributes/attributes.tsx +100 -0
- package/src/components/index.ts +7 -1
- package/src/components/interface/declaration.test.tsx +102 -0
- package/src/components/interface/declaration.tsx +43 -27
- package/src/components/interface/method.test.tsx +173 -0
- package/src/components/interface/method.tsx +45 -5
- package/src/components/interface/property.test.tsx +21 -0
- package/src/components/interface/property.tsx +43 -6
- package/src/components/parameters/parameters.tsx +74 -0
- package/src/components/property/property.test.tsx +209 -0
- package/src/components/property/property.tsx +172 -0
- package/src/components/record/declaration.test.tsx +73 -0
- package/src/components/record/declaration.tsx +109 -0
- 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/name-policy.ts +3 -0
- package/temp/api.json +4722 -2384
- package/test/{class.test.tsx → class-declaration.test.tsx} +144 -26
- package/dist/src/components/Parameters.d.ts +0 -13
- package/dist/src/components/Parameters.d.ts.map +0 -1
- package/dist/src/components/Parameters.js +0 -34
- 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
- package/src/components/Parameters.tsx +0 -51
- /package/dist/test/{projectdirectory.test.js → project-directory.test.js} +0 -0
- /package/test/{projectdirectory.test.tsx → project-directory.test.tsx} +0 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { memo as _$memo, createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { code, Declaration, For, Indent, refkey } from "@alloy-js/core";
|
|
3
|
+
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
4
|
+
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
5
|
+
import { useCSharpScope } from "../../symbols/scopes.js";
|
|
6
|
+
import { Name } from "../Name.js";
|
|
7
|
+
/** Define a parameter to be used in class or interface method. */
|
|
8
|
+
export function Parameter(props) {
|
|
9
|
+
const name = useCSharpNamePolicy().getName(props.name, "parameter");
|
|
10
|
+
const scope = useCSharpScope();
|
|
11
|
+
if (scope.kind !== "member" || scope.name !== "constructor-decl" && scope.name !== "method-decl") {
|
|
12
|
+
throw new Error("can't define a parameter outside of a constructor-decl or method-decl scope");
|
|
13
|
+
}
|
|
14
|
+
const memberSymbol = new CSharpOutputSymbol(name, {
|
|
15
|
+
scope,
|
|
16
|
+
refkeys: props.refkey ?? refkey(props.name)
|
|
17
|
+
});
|
|
18
|
+
return _$createComponent(Declaration, {
|
|
19
|
+
symbol: memberSymbol,
|
|
20
|
+
get children() {
|
|
21
|
+
return [_$memo(() => props.type), _$memo(() => props.optional ? "?" : ""), " ", _$createComponent(Name, {}), _$memo(() => props.default ? code` = ${props.default}` : "")];
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/** Render a collection of parameters */
|
|
26
|
+
export function Parameters(props) {
|
|
27
|
+
return _$createIntrinsic("group", {
|
|
28
|
+
get children() {
|
|
29
|
+
return ["(", _$memo(() => _$memo(() => !!props.parameters)() && _$createComponent(Indent, {
|
|
30
|
+
softline: true,
|
|
31
|
+
get children() {
|
|
32
|
+
return _$createComponent(For, {
|
|
33
|
+
get each() {
|
|
34
|
+
return props.parameters;
|
|
35
|
+
},
|
|
36
|
+
joiner: ", ",
|
|
37
|
+
children: param => _$createComponent(Parameter, param)
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
})), _$createIntrinsic("softline", {}), ")"];
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Children, Refkey } from "@alloy-js/core";
|
|
2
|
+
import { AccessModifiers } from "../../modifiers.js";
|
|
3
|
+
import { AttributesProp } from "../attributes/attributes.jsx";
|
|
4
|
+
/** Property modifiers. */
|
|
5
|
+
export interface PropertyModifiers {
|
|
6
|
+
readonly new?: boolean;
|
|
7
|
+
readonly static?: boolean;
|
|
8
|
+
readonly virtual?: boolean;
|
|
9
|
+
readonly sealed?: boolean;
|
|
10
|
+
readonly override?: boolean;
|
|
11
|
+
readonly abstract?: boolean;
|
|
12
|
+
readonly extern?: boolean;
|
|
13
|
+
readonly readonly?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Set required modifier on property
|
|
16
|
+
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required
|
|
17
|
+
*/
|
|
18
|
+
readonly required?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** Properties for {@link Property} component */
|
|
21
|
+
export interface PropertyProps extends AccessModifiers, PropertyModifiers {
|
|
22
|
+
name: string;
|
|
23
|
+
refkey?: Refkey;
|
|
24
|
+
/** Property type */
|
|
25
|
+
type: Children;
|
|
26
|
+
/** If property should have a getter */
|
|
27
|
+
get?: boolean;
|
|
28
|
+
/** If property should have a setter */
|
|
29
|
+
set?: boolean;
|
|
30
|
+
/** If property should only be set on the type creation */
|
|
31
|
+
init?: boolean;
|
|
32
|
+
/** Doc comment */
|
|
33
|
+
doc?: Children;
|
|
34
|
+
/**
|
|
35
|
+
* Property initializer
|
|
36
|
+
* @example `<ClassProperty name="My" get set nullable />`
|
|
37
|
+
*
|
|
38
|
+
* ```cs
|
|
39
|
+
* int? My { get; set; };
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
nullable?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Property initializer
|
|
45
|
+
* @example `<ClassProperty name="My" get set init={42} />`
|
|
46
|
+
*
|
|
47
|
+
* ```cs
|
|
48
|
+
* int My { get; set; } = 42;
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
initializer?: Children;
|
|
52
|
+
/**
|
|
53
|
+
* Define attributes to attach
|
|
54
|
+
* @example
|
|
55
|
+
* ```tsx
|
|
56
|
+
* <Property name="MyProp" attributes={[
|
|
57
|
+
* <Attribute name="Test" />
|
|
58
|
+
* <Attribute name="Test2" args={["arg1", "arg2"]} />
|
|
59
|
+
* ]} />
|
|
60
|
+
* ```
|
|
61
|
+
* This will produce:
|
|
62
|
+
* ```csharp
|
|
63
|
+
* [Test]
|
|
64
|
+
* [Test2("arg1", "arg2")]
|
|
65
|
+
* int MyProp { get; set; }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
attributes?: AttributesProp;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Render a C# class property.
|
|
72
|
+
*
|
|
73
|
+
* @example `<ClassProperty public name="My" get set />`
|
|
74
|
+
*
|
|
75
|
+
* ```cs
|
|
76
|
+
* public int My { get; set; };
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function Property(props: PropertyProps): Children;
|
|
80
|
+
//# 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;AAI5B,OAAO,EAAiB,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAG7E,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;IAC5B;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;CAC7B;AAcD,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;IAEvB;;;;;;;;;;;;;;;OAeG;IACH,UAAU,CAAC,EAAE,cAAc,CAAC;CAC7B;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,aAAa,YAoD5C"}
|
|
@@ -0,0 +1,76 @@
|
|
|
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 { AttributeList } from "../attributes/attributes.js";
|
|
8
|
+
import { DocWhen } from "../doc/comment.js";
|
|
9
|
+
|
|
10
|
+
/** Property modifiers. */
|
|
11
|
+
|
|
12
|
+
const getModifiers = makeModifiers(["new", "static", "virtual", "sealed", "override", "abstract", "extern", "readonly", "required"]);
|
|
13
|
+
|
|
14
|
+
/** Properties for {@link Property} component */
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Render a C# class property.
|
|
18
|
+
*
|
|
19
|
+
* @example `<ClassProperty public name="My" get set />`
|
|
20
|
+
*
|
|
21
|
+
* ```cs
|
|
22
|
+
* public int My { get; set; };
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function Property(props) {
|
|
26
|
+
const name = useCSharpNamePolicy().getName(props.name, "class-property");
|
|
27
|
+
const scope = useCSharpScope();
|
|
28
|
+
if (scope.kind !== "member" || scope.name !== "class-decl" && scope.name !== "record-decl") {
|
|
29
|
+
throw new Error("can't define an interface method outside of an interface scope");
|
|
30
|
+
}
|
|
31
|
+
const propertySymbol = new CSharpOutputSymbol(name, {
|
|
32
|
+
scope,
|
|
33
|
+
refkeys: props.refkey ?? refkey(props.name)
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// scope for property declaration
|
|
37
|
+
const propertyScope = new CSharpMemberScope("property-decl", {
|
|
38
|
+
owner: propertySymbol
|
|
39
|
+
});
|
|
40
|
+
const modifiers = computeModifiersPrefix([getAccessModifier(props), getModifiers(props)]);
|
|
41
|
+
if (props.init && props.set) {
|
|
42
|
+
throw new Error(`Cannot use 'init' and 'set' together on property '${name}'`);
|
|
43
|
+
}
|
|
44
|
+
// note that scope wraps the method decl so that the params get the correct scope
|
|
45
|
+
return _$createComponent(MemberDeclaration, {
|
|
46
|
+
symbol: propertySymbol,
|
|
47
|
+
get children() {
|
|
48
|
+
return _$createComponent(Scope, {
|
|
49
|
+
value: propertyScope,
|
|
50
|
+
get children() {
|
|
51
|
+
return [_$createComponent(DocWhen, {
|
|
52
|
+
get doc() {
|
|
53
|
+
return props.doc;
|
|
54
|
+
}
|
|
55
|
+
}), _$createComponent(AttributeList, {
|
|
56
|
+
get attributes() {
|
|
57
|
+
return props.attributes;
|
|
58
|
+
},
|
|
59
|
+
endline: true
|
|
60
|
+
}), modifiers, _$memo(() => props.type), _$memo(() => props.nullable && "?"), " ", name, " ", _$createComponent(Block, {
|
|
61
|
+
newline: true,
|
|
62
|
+
inline: true,
|
|
63
|
+
get children() {
|
|
64
|
+
return _$createComponent(List, {
|
|
65
|
+
joiner: " ",
|
|
66
|
+
get children() {
|
|
67
|
+
return [_$memo(() => props.get && "get;"), _$memo(() => props.set && "set;"), _$memo(() => props.init && "init;")];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}), _$memo(() => props.initializer && code` = ${props.initializer};`)];
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"property.test.d.ts","sourceRoot":"","sources":["../../../../src/components/property/property.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,242 @@
|
|
|
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 { Attribute } from "../attributes/attributes.js";
|
|
5
|
+
import { ClassDeclaration } from "../ClassDeclaration.js";
|
|
6
|
+
import { Property } from "./property.js";
|
|
7
|
+
const Wrapper = props => _$createComponent(TestNamespace, {
|
|
8
|
+
get children() {
|
|
9
|
+
return _$createComponent(ClassDeclaration, {
|
|
10
|
+
"public": true,
|
|
11
|
+
name: "TestClass",
|
|
12
|
+
get children() {
|
|
13
|
+
return props.children;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
describe("modifiers", () => {
|
|
19
|
+
describe("access modifiers", () => {
|
|
20
|
+
it.each(["public", "private", "protected", "internal"])("%s", accessModifier => {
|
|
21
|
+
expect(_$createComponent(Wrapper, {
|
|
22
|
+
get children() {
|
|
23
|
+
return _$createComponent(Property, _$mergeProps({
|
|
24
|
+
[accessModifier]: true
|
|
25
|
+
}, {
|
|
26
|
+
name: "TestProp",
|
|
27
|
+
type: "string",
|
|
28
|
+
get: true
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
})).toRenderTo(`
|
|
32
|
+
public class TestClass
|
|
33
|
+
{
|
|
34
|
+
${accessModifier} string TestProp { get; }
|
|
35
|
+
}
|
|
36
|
+
`);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
describe("property modifiers", () => {
|
|
40
|
+
it.each(["new", "static", "virtual", "sealed", "override", "abstract", "extern", "readonly", "required"])("%s", methodModifier => {
|
|
41
|
+
expect(_$createComponent(Wrapper, {
|
|
42
|
+
get children() {
|
|
43
|
+
return _$createComponent(Property, _$mergeProps({
|
|
44
|
+
[methodModifier]: true
|
|
45
|
+
}, {
|
|
46
|
+
name: "TestProp",
|
|
47
|
+
type: "string",
|
|
48
|
+
get: true
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
})).toRenderTo(`
|
|
52
|
+
public class TestClass
|
|
53
|
+
{
|
|
54
|
+
${methodModifier} string TestProp { get; }
|
|
55
|
+
}
|
|
56
|
+
`);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
it("combine modifiers", () => {
|
|
60
|
+
expect(_$createComponent(Wrapper, {
|
|
61
|
+
get children() {
|
|
62
|
+
return _$createComponent(Property, {
|
|
63
|
+
"public": true,
|
|
64
|
+
"new": true,
|
|
65
|
+
name: "TestProp",
|
|
66
|
+
type: "string",
|
|
67
|
+
get: true
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
})).toRenderTo(`
|
|
71
|
+
public class TestClass
|
|
72
|
+
{
|
|
73
|
+
public new string TestProp { get; }
|
|
74
|
+
}
|
|
75
|
+
`);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
it("applies PascalCase naming policy", () => {
|
|
79
|
+
expect(_$createComponent(Wrapper, {
|
|
80
|
+
get children() {
|
|
81
|
+
return _$createComponent(Property, {
|
|
82
|
+
name: "test_prop",
|
|
83
|
+
type: "string",
|
|
84
|
+
get: true
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
})).toRenderTo(`
|
|
88
|
+
public class TestClass
|
|
89
|
+
{
|
|
90
|
+
string TestProp { get; }
|
|
91
|
+
}
|
|
92
|
+
`);
|
|
93
|
+
});
|
|
94
|
+
it("has getter only", () => {
|
|
95
|
+
expect(_$createComponent(Wrapper, {
|
|
96
|
+
get children() {
|
|
97
|
+
return _$createComponent(Property, {
|
|
98
|
+
name: "TestProp",
|
|
99
|
+
type: "string",
|
|
100
|
+
get: true
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
})).toRenderTo(`
|
|
104
|
+
public class TestClass
|
|
105
|
+
{
|
|
106
|
+
string TestProp { get; }
|
|
107
|
+
}
|
|
108
|
+
`);
|
|
109
|
+
});
|
|
110
|
+
it("has setter only", () => {
|
|
111
|
+
expect(_$createComponent(Wrapper, {
|
|
112
|
+
get children() {
|
|
113
|
+
return _$createComponent(Property, {
|
|
114
|
+
name: "TestProp",
|
|
115
|
+
type: "string",
|
|
116
|
+
set: true
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
})).toRenderTo(`
|
|
120
|
+
public class TestClass
|
|
121
|
+
{
|
|
122
|
+
string TestProp { set; }
|
|
123
|
+
}
|
|
124
|
+
`);
|
|
125
|
+
});
|
|
126
|
+
it("has getter and setter", () => {
|
|
127
|
+
expect(_$createComponent(Wrapper, {
|
|
128
|
+
get children() {
|
|
129
|
+
return _$createComponent(Property, {
|
|
130
|
+
name: "TestProp",
|
|
131
|
+
type: "string",
|
|
132
|
+
get: true,
|
|
133
|
+
set: true
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
})).toRenderTo(`
|
|
137
|
+
public class TestClass
|
|
138
|
+
{
|
|
139
|
+
string TestProp { get; set; }
|
|
140
|
+
}
|
|
141
|
+
`);
|
|
142
|
+
});
|
|
143
|
+
it("has getter and init", () => {
|
|
144
|
+
expect(_$createComponent(Wrapper, {
|
|
145
|
+
get children() {
|
|
146
|
+
return _$createComponent(Property, {
|
|
147
|
+
name: "TestProp",
|
|
148
|
+
type: "string",
|
|
149
|
+
get: true,
|
|
150
|
+
init: true
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
})).toRenderTo(`
|
|
154
|
+
public class TestClass
|
|
155
|
+
{
|
|
156
|
+
string TestProp { get; init; }
|
|
157
|
+
}
|
|
158
|
+
`);
|
|
159
|
+
});
|
|
160
|
+
it("specify doc comment", () => {
|
|
161
|
+
expect(_$createComponent(TestNamespace, {
|
|
162
|
+
get children() {
|
|
163
|
+
return _$createComponent(ClassDeclaration, {
|
|
164
|
+
name: "Test",
|
|
165
|
+
get children() {
|
|
166
|
+
return _$createComponent(Property, {
|
|
167
|
+
name: "Method",
|
|
168
|
+
type: "string",
|
|
169
|
+
get: true,
|
|
170
|
+
set: true,
|
|
171
|
+
doc: "This is a test"
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
})).toRenderTo(`
|
|
177
|
+
class Test
|
|
178
|
+
{
|
|
179
|
+
/// This is a test
|
|
180
|
+
string Method { get; set; }
|
|
181
|
+
}
|
|
182
|
+
`);
|
|
183
|
+
});
|
|
184
|
+
it("specify nullable property", () => {
|
|
185
|
+
expect(_$createComponent(Wrapper, {
|
|
186
|
+
get children() {
|
|
187
|
+
return _$createComponent(Property, {
|
|
188
|
+
name: "TestProp",
|
|
189
|
+
type: "string",
|
|
190
|
+
nullable: true,
|
|
191
|
+
get: true,
|
|
192
|
+
set: true
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
})).toRenderTo(`
|
|
196
|
+
public class TestClass
|
|
197
|
+
{
|
|
198
|
+
string? TestProp { get; set; }
|
|
199
|
+
}
|
|
200
|
+
`);
|
|
201
|
+
});
|
|
202
|
+
it("specify initializer", () => {
|
|
203
|
+
expect(_$createComponent(Wrapper, {
|
|
204
|
+
get children() {
|
|
205
|
+
return _$createComponent(Property, {
|
|
206
|
+
name: "TestProp",
|
|
207
|
+
type: "string",
|
|
208
|
+
get: true,
|
|
209
|
+
set: true,
|
|
210
|
+
initializer: `"abc"`
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
})).toRenderTo(`
|
|
214
|
+
public class TestClass
|
|
215
|
+
{
|
|
216
|
+
string TestProp { get; set; } = "abc";
|
|
217
|
+
}
|
|
218
|
+
`);
|
|
219
|
+
});
|
|
220
|
+
it("specify attributes", () => {
|
|
221
|
+
expect(_$createComponent(Wrapper, {
|
|
222
|
+
get children() {
|
|
223
|
+
return _$createComponent(Property, {
|
|
224
|
+
name: "Test",
|
|
225
|
+
type: "int",
|
|
226
|
+
get attributes() {
|
|
227
|
+
return [_$createComponent(Attribute, {
|
|
228
|
+
name: "Test"
|
|
229
|
+
})];
|
|
230
|
+
},
|
|
231
|
+
get: true,
|
|
232
|
+
set: true
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
})).toRenderTo(`
|
|
236
|
+
public class TestClass
|
|
237
|
+
{
|
|
238
|
+
[Test]
|
|
239
|
+
int Test { get; set; }
|
|
240
|
+
}
|
|
241
|
+
`);
|
|
242
|
+
});
|
|
@@ -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"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic, memo as _$memo } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import * as core 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 } from "../../symbols/scopes.js";
|
|
7
|
+
import { DocWhen } from "../doc/comment.js";
|
|
8
|
+
import { Name } from "../Name.js";
|
|
9
|
+
const getRecordModifiers = makeModifiers(["partial"]);
|
|
10
|
+
|
|
11
|
+
/** Props to use the {@link RecordDeclaration} component */
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* CSharp record declaration.
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* <RecordDeclaration public name="IMyRecord">
|
|
18
|
+
* <RecordMember public name="MyProperty" type="int" />
|
|
19
|
+
* <RecordMethod public name="MyMethod" returnType="void">
|
|
20
|
+
* <Parameter name="value" type="int" />
|
|
21
|
+
* </RecordMethod>
|
|
22
|
+
* </RecordDeclaration>
|
|
23
|
+
* ```
|
|
24
|
+
* This will produce:
|
|
25
|
+
* ```csharp
|
|
26
|
+
* public record MyIface
|
|
27
|
+
* {
|
|
28
|
+
* public int MyProperty { get; set; }
|
|
29
|
+
* public void MyMethod(int value);
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function RecordDeclaration(props) {
|
|
34
|
+
const name = useCSharpNamePolicy().getName(props.name, "record");
|
|
35
|
+
const thisRecordSymbol = new CSharpOutputSymbol(name, {
|
|
36
|
+
refkeys: props.refkey
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// this creates a new scope for the record definition.
|
|
40
|
+
// members will automatically "inherit" this scope so
|
|
41
|
+
// that refkeys to them will produce the fully-qualified
|
|
42
|
+
// name e.g. Foo.Bar.
|
|
43
|
+
const thisRecordScope = new CSharpMemberScope("record-decl", {
|
|
44
|
+
owner: thisRecordSymbol
|
|
45
|
+
});
|
|
46
|
+
let typeParams;
|
|
47
|
+
if (props.typeParameters) {
|
|
48
|
+
const typeParamNames = new Array();
|
|
49
|
+
for (const entry of Object.entries(props.typeParameters)) {
|
|
50
|
+
typeParamNames.push(useCSharpNamePolicy().getName(entry[0], "type-parameter"));
|
|
51
|
+
// create a symbol for each type param so its
|
|
52
|
+
// refkey resolves to the type param's name
|
|
53
|
+
new CSharpOutputSymbol(entry[0], {
|
|
54
|
+
scope: thisRecordScope,
|
|
55
|
+
refkeys: entry[1]
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
typeParams = _$createIntrinsic("group", {
|
|
59
|
+
get children() {
|
|
60
|
+
return ["<", _$createComponent(core.For, {
|
|
61
|
+
each: typeParamNames,
|
|
62
|
+
comma: true,
|
|
63
|
+
line: true,
|
|
64
|
+
children: name => name
|
|
65
|
+
}), ">"];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const modifiers = computeModifiersPrefix([getAccessModifier(props), getRecordModifiers(props)]);
|
|
70
|
+
return _$createComponent(core.Declaration, {
|
|
71
|
+
symbol: thisRecordSymbol,
|
|
72
|
+
get children() {
|
|
73
|
+
return [_$createComponent(DocWhen, {
|
|
74
|
+
get doc() {
|
|
75
|
+
return props.doc;
|
|
76
|
+
}
|
|
77
|
+
}), modifiers, "record ", _$createComponent(Name, {}), typeParams, _$memo(() => _$memo(() => !!props.children)() ? _$createComponent(core.Block, {
|
|
78
|
+
newline: true,
|
|
79
|
+
get children() {
|
|
80
|
+
return _$createComponent(core.Scope, {
|
|
81
|
+
value: thisRecordScope,
|
|
82
|
+
get children() {
|
|
83
|
+
return props.children;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}) : ";")];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"declaration.test.d.ts","sourceRoot":"","sources":["../../../../src/components/record/declaration.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { 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 { Property } from "../property/property.js";
|
|
5
|
+
import { RecordDeclaration } from "./declaration.js";
|
|
6
|
+
it("declares class with no members", () => {
|
|
7
|
+
expect(_$createComponent(TestNamespace, {
|
|
8
|
+
get children() {
|
|
9
|
+
return _$createComponent(RecordDeclaration, {
|
|
10
|
+
name: "TestRecord"
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
})).toRenderTo(`
|
|
14
|
+
record TestRecord;
|
|
15
|
+
`);
|
|
16
|
+
});
|
|
17
|
+
describe("modifiers", () => {
|
|
18
|
+
it.each(["public", "private", "internal"])("%s", mod => {
|
|
19
|
+
expect(_$createComponent(TestNamespace, {
|
|
20
|
+
get children() {
|
|
21
|
+
return _$createComponent(RecordDeclaration, _$mergeProps({
|
|
22
|
+
[mod]: true
|
|
23
|
+
}, {
|
|
24
|
+
name: "TestRecord"
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
})).toRenderTo(`
|
|
28
|
+
${mod} record TestRecord;
|
|
29
|
+
`);
|
|
30
|
+
});
|
|
31
|
+
it.each(["partial"])("%s", mod => {
|
|
32
|
+
expect(_$createComponent(TestNamespace, {
|
|
33
|
+
get children() {
|
|
34
|
+
return _$createComponent(RecordDeclaration, _$mergeProps({
|
|
35
|
+
[mod]: true
|
|
36
|
+
}, {
|
|
37
|
+
name: "TestRecord"
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
})).toRenderTo(`
|
|
41
|
+
${mod} record TestRecord;
|
|
42
|
+
`);
|
|
43
|
+
});
|
|
44
|
+
it("combines modifiers", () => {
|
|
45
|
+
expect(_$createComponent(TestNamespace, {
|
|
46
|
+
get children() {
|
|
47
|
+
return _$createComponent(RecordDeclaration, {
|
|
48
|
+
"public": true,
|
|
49
|
+
partial: true,
|
|
50
|
+
name: "TestRecord"
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
})).toRenderTo(`
|
|
54
|
+
public partial record TestRecord;
|
|
55
|
+
`);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
it("specify doc comment", () => {
|
|
59
|
+
expect(_$createComponent(TestNamespace, {
|
|
60
|
+
get children() {
|
|
61
|
+
return _$createComponent(RecordDeclaration, {
|
|
62
|
+
name: "TestRecord",
|
|
63
|
+
doc: "This is a test"
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
})).toRenderTo(`
|
|
67
|
+
/// This is a test
|
|
68
|
+
record TestRecord;
|
|
69
|
+
`);
|
|
70
|
+
});
|
|
71
|
+
it("specify class property inside", () => {
|
|
72
|
+
expect(_$createComponent(TestNamespace, {
|
|
73
|
+
get children() {
|
|
74
|
+
return _$createComponent(RecordDeclaration, {
|
|
75
|
+
name: "TestRecord",
|
|
76
|
+
doc: "This is a test",
|
|
77
|
+
get children() {
|
|
78
|
+
return _$createComponent(Property, {
|
|
79
|
+
name: "Prop",
|
|
80
|
+
get: true,
|
|
81
|
+
set: true,
|
|
82
|
+
type: "string"
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
})).toRenderTo(`
|
|
88
|
+
/// This is a test
|
|
89
|
+
record TestRecord
|
|
90
|
+
{
|
|
91
|
+
string Prop { get; set; }
|
|
92
|
+
}
|
|
93
|
+
`);
|
|
94
|
+
});
|