@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,172 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Block,
|
|
3
|
+
Children,
|
|
4
|
+
code,
|
|
5
|
+
List,
|
|
6
|
+
MemberDeclaration,
|
|
7
|
+
refkey,
|
|
8
|
+
Refkey,
|
|
9
|
+
Scope,
|
|
10
|
+
} from "@alloy-js/core";
|
|
11
|
+
import {
|
|
12
|
+
AccessModifiers,
|
|
13
|
+
computeModifiersPrefix,
|
|
14
|
+
getAccessModifier,
|
|
15
|
+
makeModifiers,
|
|
16
|
+
} from "../../modifiers.js";
|
|
17
|
+
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
18
|
+
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
19
|
+
import { CSharpMemberScope, useCSharpScope } from "../../symbols/scopes.js";
|
|
20
|
+
import { AttributeList, AttributesProp } from "../attributes/attributes.jsx";
|
|
21
|
+
import { DocWhen } from "../doc/comment.jsx";
|
|
22
|
+
|
|
23
|
+
/** Property modifiers. */
|
|
24
|
+
export interface PropertyModifiers {
|
|
25
|
+
readonly new?: boolean;
|
|
26
|
+
readonly static?: boolean;
|
|
27
|
+
readonly virtual?: boolean;
|
|
28
|
+
readonly sealed?: boolean;
|
|
29
|
+
readonly override?: boolean;
|
|
30
|
+
readonly abstract?: boolean;
|
|
31
|
+
readonly extern?: boolean;
|
|
32
|
+
readonly readonly?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Set required modifier on property
|
|
35
|
+
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required
|
|
36
|
+
*/
|
|
37
|
+
readonly required?: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const getModifiers = makeModifiers<PropertyModifiers>([
|
|
41
|
+
"new",
|
|
42
|
+
"static",
|
|
43
|
+
"virtual",
|
|
44
|
+
"sealed",
|
|
45
|
+
"override",
|
|
46
|
+
"abstract",
|
|
47
|
+
"extern",
|
|
48
|
+
"readonly",
|
|
49
|
+
"required",
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
/** Properties for {@link Property} component */
|
|
53
|
+
export interface PropertyProps extends AccessModifiers, PropertyModifiers {
|
|
54
|
+
name: string;
|
|
55
|
+
refkey?: Refkey;
|
|
56
|
+
|
|
57
|
+
/** Property type */
|
|
58
|
+
type: Children;
|
|
59
|
+
|
|
60
|
+
/** If property should have a getter */
|
|
61
|
+
get?: boolean;
|
|
62
|
+
|
|
63
|
+
/** If property should have a setter */
|
|
64
|
+
set?: boolean;
|
|
65
|
+
|
|
66
|
+
/** If property should only be set on the type creation */
|
|
67
|
+
init?: boolean;
|
|
68
|
+
|
|
69
|
+
/** Doc comment */
|
|
70
|
+
doc?: Children;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Property initializer
|
|
74
|
+
* @example `<ClassProperty name="My" get set nullable />`
|
|
75
|
+
*
|
|
76
|
+
* ```cs
|
|
77
|
+
* int? My { get; set; };
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
nullable?: boolean;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Property initializer
|
|
84
|
+
* @example `<ClassProperty name="My" get set init={42} />`
|
|
85
|
+
*
|
|
86
|
+
* ```cs
|
|
87
|
+
* int My { get; set; } = 42;
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
initializer?: Children;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Define attributes to attach
|
|
94
|
+
* @example
|
|
95
|
+
* ```tsx
|
|
96
|
+
* <Property name="MyProp" attributes={[
|
|
97
|
+
* <Attribute name="Test" />
|
|
98
|
+
* <Attribute name="Test2" args={["arg1", "arg2"]} />
|
|
99
|
+
* ]} />
|
|
100
|
+
* ```
|
|
101
|
+
* This will produce:
|
|
102
|
+
* ```csharp
|
|
103
|
+
* [Test]
|
|
104
|
+
* [Test2("arg1", "arg2")]
|
|
105
|
+
* int MyProp { get; set; }
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
attributes?: AttributesProp;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Render a C# class property.
|
|
113
|
+
*
|
|
114
|
+
* @example `<ClassProperty public name="My" get set />`
|
|
115
|
+
*
|
|
116
|
+
* ```cs
|
|
117
|
+
* public int My { get; set; };
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export function Property(props: PropertyProps) {
|
|
121
|
+
const name = useCSharpNamePolicy().getName(props.name, "class-property");
|
|
122
|
+
const scope = useCSharpScope();
|
|
123
|
+
if (
|
|
124
|
+
scope.kind !== "member" ||
|
|
125
|
+
(scope.name !== "class-decl" && scope.name !== "record-decl")
|
|
126
|
+
) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
"can't define an interface method outside of an interface scope",
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const propertySymbol = new CSharpOutputSymbol(name, {
|
|
133
|
+
scope,
|
|
134
|
+
refkeys: props.refkey ?? refkey(props.name),
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// scope for property declaration
|
|
138
|
+
const propertyScope = new CSharpMemberScope("property-decl", {
|
|
139
|
+
owner: propertySymbol,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const modifiers = computeModifiersPrefix([
|
|
143
|
+
getAccessModifier(props),
|
|
144
|
+
getModifiers(props),
|
|
145
|
+
]);
|
|
146
|
+
|
|
147
|
+
if (props.init && props.set) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`Cannot use 'init' and 'set' together on property '${name}'`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
// note that scope wraps the method decl so that the params get the correct scope
|
|
153
|
+
return (
|
|
154
|
+
<MemberDeclaration symbol={propertySymbol}>
|
|
155
|
+
<Scope value={propertyScope}>
|
|
156
|
+
<DocWhen doc={props.doc} />
|
|
157
|
+
<AttributeList attributes={props.attributes} endline />
|
|
158
|
+
{modifiers}
|
|
159
|
+
{props.type}
|
|
160
|
+
{props.nullable && "?"} {name}{" "}
|
|
161
|
+
<Block newline inline>
|
|
162
|
+
<List joiner=" ">
|
|
163
|
+
{props.get && "get;"}
|
|
164
|
+
{props.set && "set;"}
|
|
165
|
+
{props.init && "init;"}
|
|
166
|
+
</List>
|
|
167
|
+
</Block>
|
|
168
|
+
{props.initializer && code` = ${props.initializer};`}
|
|
169
|
+
</Scope>
|
|
170
|
+
</MemberDeclaration>
|
|
171
|
+
);
|
|
172
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { TestNamespace } from "../../../test/utils.jsx";
|
|
3
|
+
import { Property } from "../property/property.jsx";
|
|
4
|
+
import { RecordDeclaration } from "./declaration.jsx";
|
|
5
|
+
|
|
6
|
+
it("declares class with no members", () => {
|
|
7
|
+
expect(
|
|
8
|
+
<TestNamespace>
|
|
9
|
+
<RecordDeclaration name="TestRecord" />
|
|
10
|
+
</TestNamespace>,
|
|
11
|
+
).toRenderTo(`
|
|
12
|
+
record TestRecord;
|
|
13
|
+
`);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe("modifiers", () => {
|
|
17
|
+
it.each(["public", "private", "internal"])("%s", (mod) => {
|
|
18
|
+
expect(
|
|
19
|
+
<TestNamespace>
|
|
20
|
+
<RecordDeclaration {...{ [mod]: true }} name="TestRecord" />
|
|
21
|
+
</TestNamespace>,
|
|
22
|
+
).toRenderTo(`
|
|
23
|
+
${mod} record TestRecord;
|
|
24
|
+
`);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it.each(["partial"])("%s", (mod) => {
|
|
28
|
+
expect(
|
|
29
|
+
<TestNamespace>
|
|
30
|
+
<RecordDeclaration {...{ [mod]: true }} name="TestRecord" />
|
|
31
|
+
</TestNamespace>,
|
|
32
|
+
).toRenderTo(`
|
|
33
|
+
${mod} record TestRecord;
|
|
34
|
+
`);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("combines modifiers", () => {
|
|
38
|
+
expect(
|
|
39
|
+
<TestNamespace>
|
|
40
|
+
<RecordDeclaration public partial name="TestRecord" />
|
|
41
|
+
</TestNamespace>,
|
|
42
|
+
).toRenderTo(`
|
|
43
|
+
public partial record TestRecord;
|
|
44
|
+
`);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("specify doc comment", () => {
|
|
49
|
+
expect(
|
|
50
|
+
<TestNamespace>
|
|
51
|
+
<RecordDeclaration name="TestRecord" doc="This is a test" />
|
|
52
|
+
</TestNamespace>,
|
|
53
|
+
).toRenderTo(`
|
|
54
|
+
/// This is a test
|
|
55
|
+
record TestRecord;
|
|
56
|
+
`);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("specify class property inside", () => {
|
|
60
|
+
expect(
|
|
61
|
+
<TestNamespace>
|
|
62
|
+
<RecordDeclaration name="TestRecord" doc="This is a test">
|
|
63
|
+
<Property name="Prop" get set type="string" />
|
|
64
|
+
</RecordDeclaration>
|
|
65
|
+
</TestNamespace>,
|
|
66
|
+
).toRenderTo(`
|
|
67
|
+
/// This is a test
|
|
68
|
+
record TestRecord
|
|
69
|
+
{
|
|
70
|
+
string Prop { get; set; }
|
|
71
|
+
}
|
|
72
|
+
`);
|
|
73
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import * as core from "@alloy-js/core";
|
|
2
|
+
import {
|
|
3
|
+
AccessModifiers,
|
|
4
|
+
computeModifiersPrefix,
|
|
5
|
+
getAccessModifier,
|
|
6
|
+
makeModifiers,
|
|
7
|
+
} from "../../modifiers.js";
|
|
8
|
+
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
9
|
+
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
10
|
+
import { CSharpMemberScope } from "../../symbols/scopes.js";
|
|
11
|
+
import { DocWhen } from "../doc/comment.jsx";
|
|
12
|
+
import { Name } from "../Name.jsx";
|
|
13
|
+
|
|
14
|
+
export interface RecordModifiers {
|
|
15
|
+
readonly partial?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const getRecordModifiers = makeModifiers<RecordModifiers>(["partial"]);
|
|
19
|
+
|
|
20
|
+
/** Props to use the {@link RecordDeclaration} component */
|
|
21
|
+
export interface RecordDeclarationProps
|
|
22
|
+
extends Omit<core.DeclarationProps, "nameKind">,
|
|
23
|
+
AccessModifiers,
|
|
24
|
+
RecordModifiers {
|
|
25
|
+
name: string;
|
|
26
|
+
|
|
27
|
+
/** Doc comment */
|
|
28
|
+
doc?: core.Children;
|
|
29
|
+
refkey?: core.Refkey;
|
|
30
|
+
typeParameters?: Record<string, core.Refkey>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* CSharp record declaration.
|
|
35
|
+
* @example
|
|
36
|
+
* ```tsx
|
|
37
|
+
* <RecordDeclaration public name="IMyRecord">
|
|
38
|
+
* <RecordMember public name="MyProperty" type="int" />
|
|
39
|
+
* <RecordMethod public name="MyMethod" returnType="void">
|
|
40
|
+
* <Parameter name="value" type="int" />
|
|
41
|
+
* </RecordMethod>
|
|
42
|
+
* </RecordDeclaration>
|
|
43
|
+
* ```
|
|
44
|
+
* This will produce:
|
|
45
|
+
* ```csharp
|
|
46
|
+
* public record MyIface
|
|
47
|
+
* {
|
|
48
|
+
* public int MyProperty { get; set; }
|
|
49
|
+
* public void MyMethod(int value);
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function RecordDeclaration(props: RecordDeclarationProps) {
|
|
54
|
+
const name = useCSharpNamePolicy().getName(props.name!, "record");
|
|
55
|
+
|
|
56
|
+
const thisRecordSymbol = new CSharpOutputSymbol(name, {
|
|
57
|
+
refkeys: props.refkey,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// this creates a new scope for the record definition.
|
|
61
|
+
// members will automatically "inherit" this scope so
|
|
62
|
+
// that refkeys to them will produce the fully-qualified
|
|
63
|
+
// name e.g. Foo.Bar.
|
|
64
|
+
const thisRecordScope = new CSharpMemberScope("record-decl", {
|
|
65
|
+
owner: thisRecordSymbol,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
let typeParams: core.Children;
|
|
69
|
+
if (props.typeParameters) {
|
|
70
|
+
const typeParamNames = new Array<string>();
|
|
71
|
+
for (const entry of Object.entries(props.typeParameters)) {
|
|
72
|
+
typeParamNames.push(
|
|
73
|
+
useCSharpNamePolicy().getName(entry[0], "type-parameter"),
|
|
74
|
+
);
|
|
75
|
+
// create a symbol for each type param so its
|
|
76
|
+
// refkey resolves to the type param's name
|
|
77
|
+
new CSharpOutputSymbol(entry[0], {
|
|
78
|
+
scope: thisRecordScope,
|
|
79
|
+
refkeys: entry[1],
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
typeParams = (
|
|
83
|
+
<group>
|
|
84
|
+
{"<"}
|
|
85
|
+
<core.For each={typeParamNames} comma line>
|
|
86
|
+
{(name) => name}
|
|
87
|
+
</core.For>
|
|
88
|
+
{">"}
|
|
89
|
+
</group>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const modifiers = computeModifiersPrefix([
|
|
94
|
+
getAccessModifier(props),
|
|
95
|
+
getRecordModifiers(props),
|
|
96
|
+
]);
|
|
97
|
+
return (
|
|
98
|
+
<core.Declaration symbol={thisRecordSymbol}>
|
|
99
|
+
<DocWhen doc={props.doc} />
|
|
100
|
+
{modifiers}record <Name />
|
|
101
|
+
{typeParams}
|
|
102
|
+
{props.children ?
|
|
103
|
+
<core.Block newline>
|
|
104
|
+
<core.Scope value={thisRecordScope}>{props.children}</core.Scope>
|
|
105
|
+
</core.Block>
|
|
106
|
+
: ";"}
|
|
107
|
+
</core.Declaration>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { expect, it } from "vitest";
|
|
2
|
+
import { TypeParameterConstraints } from "./type-parameter-constraints.jsx";
|
|
3
|
+
|
|
4
|
+
it("renders nothing if there is no constraints", () => {
|
|
5
|
+
expect(<TypeParameterConstraints parameters={["A", "B"]} />).toRenderTo(``);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it("renders inline if there is only one", () => {
|
|
9
|
+
expect(
|
|
10
|
+
<>
|
|
11
|
+
{"code()"}
|
|
12
|
+
<TypeParameterConstraints
|
|
13
|
+
parameters={[{ name: "A", constraints: "string" }]}
|
|
14
|
+
/>
|
|
15
|
+
</>,
|
|
16
|
+
).toRenderTo(`
|
|
17
|
+
code() where A : string
|
|
18
|
+
`);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("renders multiple constraints", () => {
|
|
22
|
+
expect(
|
|
23
|
+
<>
|
|
24
|
+
{"code()"}
|
|
25
|
+
<TypeParameterConstraints
|
|
26
|
+
parameters={[{ name: "A", constraints: ["string", "int"] }]}
|
|
27
|
+
/>
|
|
28
|
+
</>,
|
|
29
|
+
).toRenderTo(`
|
|
30
|
+
code() where A : string, int
|
|
31
|
+
`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("renders newline if constraint is very long", () => {
|
|
35
|
+
expect(
|
|
36
|
+
<>
|
|
37
|
+
{"code()"}
|
|
38
|
+
<TypeParameterConstraints
|
|
39
|
+
parameters={[
|
|
40
|
+
{
|
|
41
|
+
name: "ThisIsQuiteALongName",
|
|
42
|
+
constraints: "VeryLongBuilderFactorySingletonThatShouldBeSplit",
|
|
43
|
+
},
|
|
44
|
+
]}
|
|
45
|
+
/>
|
|
46
|
+
</>,
|
|
47
|
+
).toRenderTo(`
|
|
48
|
+
code()
|
|
49
|
+
where ThisIsQuiteALongName : VeryLongBuilderFactorySingletonThatShouldBeSplit
|
|
50
|
+
`);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("renders multiple constraints on new lines if they are very long", () => {
|
|
54
|
+
expect(
|
|
55
|
+
<>
|
|
56
|
+
{"code()"}
|
|
57
|
+
<TypeParameterConstraints
|
|
58
|
+
parameters={[
|
|
59
|
+
{
|
|
60
|
+
name: "A",
|
|
61
|
+
constraints: [
|
|
62
|
+
"IVeryLongBuilderFactorySingletonThatShouldBeSplitA",
|
|
63
|
+
"IVeryLongBuilderFactorySingletonThatShouldBeSplitB",
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
]}
|
|
67
|
+
/>
|
|
68
|
+
</>,
|
|
69
|
+
).toRenderTo(`
|
|
70
|
+
code()
|
|
71
|
+
where A :
|
|
72
|
+
IVeryLongBuilderFactorySingletonThatShouldBeSplitA,
|
|
73
|
+
IVeryLongBuilderFactorySingletonThatShouldBeSplitB
|
|
74
|
+
`);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("declare type parameters using parameters", () => {
|
|
78
|
+
expect(
|
|
79
|
+
<>
|
|
80
|
+
{"code()"}
|
|
81
|
+
<TypeParameterConstraints
|
|
82
|
+
parameters={[
|
|
83
|
+
{ name: "A", constraints: "string" },
|
|
84
|
+
{ name: "B", constraints: "string" },
|
|
85
|
+
]}
|
|
86
|
+
/>
|
|
87
|
+
</>,
|
|
88
|
+
).toRenderTo(`
|
|
89
|
+
code()
|
|
90
|
+
where A : string
|
|
91
|
+
where B : string
|
|
92
|
+
`);
|
|
93
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Children, code, For, Indent } from "@alloy-js/core";
|
|
2
|
+
import { TypeParameterProps } from "./type-parameter.jsx";
|
|
3
|
+
import { normalizeParameters } from "./type-parameters.jsx";
|
|
4
|
+
|
|
5
|
+
export interface TypeParameterConstraintsProps {
|
|
6
|
+
/** Parameters */
|
|
7
|
+
parameters: (TypeParameterProps | string)[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function TypeParameterConstraints(
|
|
11
|
+
props: TypeParameterConstraintsProps,
|
|
12
|
+
): Children {
|
|
13
|
+
const parameters = normalizeParameters(props.parameters);
|
|
14
|
+
if (!parameters.some((x) => x.constraints)) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return (
|
|
18
|
+
<group>
|
|
19
|
+
<Indent line>
|
|
20
|
+
<For each={parameters} hardline>
|
|
21
|
+
{(param) => <TypeParameterConstraint {...param} />}
|
|
22
|
+
</For>
|
|
23
|
+
</Indent>
|
|
24
|
+
</group>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function TypeParameterConstraint(props: TypeParameterProps): Children {
|
|
29
|
+
const constraints = arrayify(props.constraints);
|
|
30
|
+
return (
|
|
31
|
+
<>
|
|
32
|
+
where {code`${props.name} : `}
|
|
33
|
+
<group>
|
|
34
|
+
<Indent softline>
|
|
35
|
+
<For each={constraints} comma line>
|
|
36
|
+
{(constraint) => code`${constraint}`}
|
|
37
|
+
</For>
|
|
38
|
+
</Indent>
|
|
39
|
+
</group>
|
|
40
|
+
</>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function arrayify<T>(value: T | T[]): T[] {
|
|
45
|
+
return Array.isArray(value) ? value : [value];
|
|
46
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Children, MemberDeclaration, refkey, Refkey } from "@alloy-js/core";
|
|
2
|
+
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
3
|
+
import { CSharpOutputSymbol } from "../../symbols/csharp-output-symbol.js";
|
|
4
|
+
import { useCSharpScope } from "../../symbols/scopes.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Information for a TypeScript generic type parameter.
|
|
8
|
+
*/
|
|
9
|
+
export interface TypeParameterProps {
|
|
10
|
+
/**
|
|
11
|
+
* The name of the type parameter.
|
|
12
|
+
*/
|
|
13
|
+
readonly name: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The parameter constraint
|
|
17
|
+
*/
|
|
18
|
+
readonly constraints?: Children | Children[];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A refkey or array of refkeys for this type parameter.
|
|
22
|
+
*/
|
|
23
|
+
readonly refkey?: Refkey | Refkey[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function TypeParameter(props: TypeParameterProps) {
|
|
27
|
+
const name = useCSharpNamePolicy().getName(props.name, "type-parameter");
|
|
28
|
+
const scope = useCSharpScope();
|
|
29
|
+
const symbol = new CSharpOutputSymbol(name, {
|
|
30
|
+
scope,
|
|
31
|
+
refkeys: props.refkey ?? refkey(props.name),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return <MemberDeclaration symbol={symbol}>{name}</MemberDeclaration>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { expect, it } from "vitest";
|
|
2
|
+
import { TestNamespace } from "../../../test/utils.jsx";
|
|
3
|
+
import { TypeParameters } from "./type-parameters.jsx";
|
|
4
|
+
|
|
5
|
+
it("render one", () => {
|
|
6
|
+
expect(
|
|
7
|
+
<TestNamespace>
|
|
8
|
+
<TypeParameters parameters={["A"]} />
|
|
9
|
+
</TestNamespace>,
|
|
10
|
+
).toRenderTo(`<A>`);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("render very long", () => {
|
|
14
|
+
expect(
|
|
15
|
+
<TestNamespace>
|
|
16
|
+
<TypeParameters
|
|
17
|
+
parameters={[
|
|
18
|
+
"SomeVeryVeryLongParamThatMightGetSplitOverMultipleLinesA",
|
|
19
|
+
"SomeVeryVeryLongParamThatMightGetSplitOverMultipleLinesB",
|
|
20
|
+
]}
|
|
21
|
+
/>
|
|
22
|
+
</TestNamespace>,
|
|
23
|
+
).toRenderTo(
|
|
24
|
+
`
|
|
25
|
+
<
|
|
26
|
+
SomeVeryVeryLongParamThatMightGetSplitOverMultipleLinesA,
|
|
27
|
+
SomeVeryVeryLongParamThatMightGetSplitOverMultipleLinesB>
|
|
28
|
+
`,
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("declare type parameters using parameters names", () => {
|
|
33
|
+
expect(
|
|
34
|
+
<TestNamespace>
|
|
35
|
+
<TypeParameters parameters={["A", "B"]} />
|
|
36
|
+
</TestNamespace>,
|
|
37
|
+
).toRenderTo(`<A, B>`);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("declare type parameters using parameters", () => {
|
|
41
|
+
expect(
|
|
42
|
+
<TestNamespace>
|
|
43
|
+
<TypeParameters parameters={[{ name: "A" }, { name: "B" }]} />
|
|
44
|
+
</TestNamespace>,
|
|
45
|
+
).toRenderTo(`<A, B>`);
|
|
46
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { For, Indent, taggedComponent } from "@alloy-js/core";
|
|
2
|
+
import { TypeParameter, TypeParameterProps } from "./type-parameter.jsx";
|
|
3
|
+
|
|
4
|
+
export const typeParametersTag = Symbol.for("csharp.type-parameters");
|
|
5
|
+
|
|
6
|
+
export interface TypeParametersProps {
|
|
7
|
+
/** Parameters */
|
|
8
|
+
parameters: (TypeParameterProps | string)[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Represent type parameters
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* <A, B extends string>
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export const TypeParameters = taggedComponent(
|
|
20
|
+
typeParametersTag,
|
|
21
|
+
function TypeParameters(props: TypeParametersProps) {
|
|
22
|
+
const typeParameters = normalizeParameters(props.parameters);
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<>
|
|
26
|
+
{"<"}
|
|
27
|
+
<group>
|
|
28
|
+
<Indent softline>
|
|
29
|
+
<For each={typeParameters} comma line>
|
|
30
|
+
{(param) => <TypeParameter {...param} />}
|
|
31
|
+
</For>
|
|
32
|
+
</Indent>
|
|
33
|
+
</group>
|
|
34
|
+
{">"}
|
|
35
|
+
</>
|
|
36
|
+
);
|
|
37
|
+
},
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
export function normalizeParameters(
|
|
41
|
+
parameters: (TypeParameterProps | string)[],
|
|
42
|
+
): TypeParameterProps[] {
|
|
43
|
+
return parameters.map((param) => {
|
|
44
|
+
if (typeof param === "string") {
|
|
45
|
+
return { name: param };
|
|
46
|
+
}
|
|
47
|
+
return param;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// export function declareParameter(
|
|
52
|
+
// parameters: TypeParameterProps[],
|
|
53
|
+
// ): TypeParameterProps[] {
|
|
54
|
+
// return parameters.map((param) => {
|
|
55
|
+
// return {
|
|
56
|
+
// ...param,
|
|
57
|
+
// symbol: new CSharpOutputSymbol(entry[0], {
|
|
58
|
+
// scope: thisClassScope,
|
|
59
|
+
// refkeys: entry[1],
|
|
60
|
+
// }),
|
|
61
|
+
// };
|
|
62
|
+
// });
|
|
63
|
+
// }
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { List, refkey } from "@alloy-js/core";
|
|
2
|
+
import { expect, it } from "vitest";
|
|
3
|
+
import { TestNamespace } from "../../../test/utils.jsx";
|
|
4
|
+
import { SourceFile } from "../SourceFile.jsx";
|
|
5
|
+
import { VarDeclaration } from "./declaration.jsx";
|
|
6
|
+
|
|
7
|
+
it("declares var without type", () => {
|
|
8
|
+
expect(
|
|
9
|
+
<TestNamespace>
|
|
10
|
+
<VarDeclaration name="testVar">42</VarDeclaration>
|
|
11
|
+
</TestNamespace>,
|
|
12
|
+
).toRenderTo(`
|
|
13
|
+
var testVar = 42;
|
|
14
|
+
`);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("declares var with type", () => {
|
|
18
|
+
expect(
|
|
19
|
+
<TestNamespace>
|
|
20
|
+
<VarDeclaration name="testVar" type="int">
|
|
21
|
+
42
|
|
22
|
+
</VarDeclaration>
|
|
23
|
+
</TestNamespace>,
|
|
24
|
+
).toRenderTo(`
|
|
25
|
+
int testVar = 42;
|
|
26
|
+
`);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("name variables camel case", () => {
|
|
30
|
+
expect(
|
|
31
|
+
<TestNamespace>
|
|
32
|
+
<VarDeclaration name="test_var">42</VarDeclaration>
|
|
33
|
+
</TestNamespace>,
|
|
34
|
+
).toRenderTo(`
|
|
35
|
+
var testVar = 42;
|
|
36
|
+
`);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("links refkey", () => {
|
|
40
|
+
const key = refkey();
|
|
41
|
+
expect(
|
|
42
|
+
<TestNamespace>
|
|
43
|
+
<SourceFile path="test.cs">
|
|
44
|
+
<List>
|
|
45
|
+
<VarDeclaration name="testVar" refkey={key}>
|
|
46
|
+
42
|
|
47
|
+
</VarDeclaration>
|
|
48
|
+
<VarDeclaration name="testVar2">{key}</VarDeclaration>
|
|
49
|
+
</List>
|
|
50
|
+
</SourceFile>
|
|
51
|
+
</TestNamespace>,
|
|
52
|
+
).toRenderTo(`
|
|
53
|
+
namespace TestCode
|
|
54
|
+
{
|
|
55
|
+
var testVar = 42;
|
|
56
|
+
var testVar2 = testVar;
|
|
57
|
+
}
|
|
58
|
+
`);
|
|
59
|
+
});
|