@alloy-js/csharp 0.18.0-dev.8 → 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/doc/comment.d.ts +12 -11
- package/dist/src/components/doc/comment.d.ts.map +1 -1
- package/dist/src/components/doc/comment.js +27 -10
- package/dist/src/components/doc/comment.test.js +98 -88
- 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 +9 -1
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +10 -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 +7 -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/doc/comment.test.tsx +80 -79
- package/src/components/doc/comment.tsx +44 -14
- package/src/components/doc/from-markdown.test.tsx +103 -0
- package/src/components/doc/from-markdown.tsx +58 -0
- package/src/components/index.ts +9 -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 +5838 -1626
- 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,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
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Children, DeclarationProps, Name, Refkey } from "@alloy-js/core";
|
|
2
|
+
import { useCSharpNamePolicy } from "../../name-policy.js";
|
|
3
|
+
import { Declaration } from "../Declaration.jsx";
|
|
4
|
+
|
|
5
|
+
/** Props for {@link VarDeclaration} component */
|
|
6
|
+
export interface VarDeclarationProps
|
|
7
|
+
extends Omit<DeclarationProps, "nameKind"> {
|
|
8
|
+
/** Variable name */
|
|
9
|
+
name: string;
|
|
10
|
+
/** Type of the variable declaration. If not specified, defaults to "var" */
|
|
11
|
+
type?: Children;
|
|
12
|
+
/** Variable refkey */
|
|
13
|
+
refkey?: Refkey;
|
|
14
|
+
/** Variable value */
|
|
15
|
+
children?: Children;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Render a variable declaration
|
|
20
|
+
*
|
|
21
|
+
* @example with var
|
|
22
|
+
* ```tsx
|
|
23
|
+
* <VarDeclaration name="myVar">42</VarDeclaration>
|
|
24
|
+
* ```
|
|
25
|
+
* This will render:
|
|
26
|
+
* ```csharp
|
|
27
|
+
* var myVar = 42;
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example with type
|
|
31
|
+
* ```tsx
|
|
32
|
+
* <VarDeclaration name="myVar" type="int">42</VarDeclaration>
|
|
33
|
+
* ```
|
|
34
|
+
* This will render:
|
|
35
|
+
* ```csharp
|
|
36
|
+
* int myVar = 42;
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function VarDeclaration(props: VarDeclarationProps) {
|
|
40
|
+
const name = useCSharpNamePolicy().getName(props.name, "variable");
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<Declaration name={name} refkey={props.refkey}>
|
|
44
|
+
{props.type ?? "var"} <Name /> = {props.children};
|
|
45
|
+
</Declaration>
|
|
46
|
+
);
|
|
47
|
+
}
|
package/src/name-policy.ts
CHANGED
|
@@ -5,10 +5,12 @@ import * as changecase from "change-case";
|
|
|
5
5
|
export type CSharpElements =
|
|
6
6
|
| "class"
|
|
7
7
|
| "constant"
|
|
8
|
+
| "variable"
|
|
8
9
|
| "enum"
|
|
9
10
|
| "enum-member"
|
|
10
11
|
| "function"
|
|
11
12
|
| "interface"
|
|
13
|
+
| "record"
|
|
12
14
|
| "class-member-private"
|
|
13
15
|
| "class-member-public"
|
|
14
16
|
| "class-method"
|
|
@@ -24,6 +26,7 @@ export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
|
|
|
24
26
|
case "enum":
|
|
25
27
|
case "enum-member":
|
|
26
28
|
case "interface":
|
|
29
|
+
case "record":
|
|
27
30
|
case "class-member-public":
|
|
28
31
|
case "class-method":
|
|
29
32
|
case "type-parameter":
|