@alloy-js/csharp 0.23.0-dev.5 → 0.23.0-dev.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dev/src/components/namespace/namespace.js +16 -22
- package/dist/dev/src/components/namespace/namespace.js.map +1 -1
- package/dist/dev/src/components/namespace/namespace.test.js +120 -33
- package/dist/dev/src/components/namespace/namespace.test.js.map +1 -1
- package/dist/dev/src/components/namespace-scopes.js +10 -3
- package/dist/dev/src/components/namespace-scopes.js.map +1 -1
- package/dist/dev/src/components/namespace.ref.test.js +69 -0
- package/dist/dev/src/components/namespace.ref.test.js.map +1 -1
- package/dist/src/components/namespace/namespace.d.ts.map +1 -1
- package/dist/src/components/namespace/namespace.js +11 -13
- package/dist/src/components/namespace/namespace.js.map +1 -1
- package/dist/src/components/namespace/namespace.test.js +61 -2
- package/dist/src/components/namespace/namespace.test.js.map +1 -1
- package/dist/src/components/namespace-scopes.d.ts +16 -0
- package/dist/src/components/namespace-scopes.d.ts.map +1 -1
- package/dist/src/components/namespace-scopes.js +8 -1
- package/dist/src/components/namespace-scopes.js.map +1 -1
- package/dist/src/components/namespace.ref.test.js +41 -0
- package/dist/src/components/namespace.ref.test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/components/namespace/namespace.test.tsx +42 -1
- package/src/components/namespace/namespace.tsx +16 -8
- package/src/components/namespace-scopes.tsx +25 -1
- package/src/components/namespace.ref.test.tsx +29 -0
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { TestNamespace } from "#test/utils.jsx";
|
|
2
|
-
import { Output } from "@alloy-js/core";
|
|
2
|
+
import { Output, refkey } from "@alloy-js/core";
|
|
3
3
|
import { d } from "@alloy-js/core/testing";
|
|
4
4
|
import { expect, it } from "vitest";
|
|
5
|
+
import { createCSharpNamePolicy } from "../../name-policy.js";
|
|
5
6
|
import { ClassDeclaration } from "../class/declaration.jsx";
|
|
7
|
+
import { Constructor } from "../constructor/constructor.jsx";
|
|
8
|
+
import { Field } from "../field/field.jsx";
|
|
6
9
|
import { SourceFile } from "../source-file/source-file.jsx";
|
|
10
|
+
import { StructDeclaration } from "../struct/declaration.jsx";
|
|
7
11
|
import { Namespace } from "./namespace.jsx";
|
|
8
12
|
|
|
9
13
|
it("defines multiple namespaces and source files with unique content", () => {
|
|
@@ -141,3 +145,40 @@ it("define nested namespace in sourcefile", () => {
|
|
|
141
145
|
}
|
|
142
146
|
`);
|
|
143
147
|
});
|
|
148
|
+
|
|
149
|
+
it("contains a struct with a private field initialized by a constructor", () => {
|
|
150
|
+
const fieldRefkey = refkey();
|
|
151
|
+
const paramRefkey = refkey();
|
|
152
|
+
|
|
153
|
+
const tree = (
|
|
154
|
+
<Output namePolicy={createCSharpNamePolicy()}>
|
|
155
|
+
<SourceFile path="MyStruct.cs">
|
|
156
|
+
<Namespace name="TestNamespace.Test">
|
|
157
|
+
<StructDeclaration public name="MyStruct">
|
|
158
|
+
<Field private name="value" type="int" refkey={fieldRefkey} />
|
|
159
|
+
<hbr />
|
|
160
|
+
<Constructor
|
|
161
|
+
public
|
|
162
|
+
parameters={[{ name: "value", type: "int", refkey: paramRefkey }]}
|
|
163
|
+
>
|
|
164
|
+
{fieldRefkey} = {paramRefkey};
|
|
165
|
+
</Constructor>
|
|
166
|
+
</StructDeclaration>
|
|
167
|
+
</Namespace>
|
|
168
|
+
</SourceFile>
|
|
169
|
+
</Output>
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
expect(tree).toRenderTo(d`
|
|
173
|
+
namespace TestNamespace.Test {
|
|
174
|
+
public struct MyStruct
|
|
175
|
+
{
|
|
176
|
+
private int _value;
|
|
177
|
+
public MyStruct(int value)
|
|
178
|
+
{
|
|
179
|
+
_value = value;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
`);
|
|
184
|
+
});
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { Block, Namekey, Refkey } from "@alloy-js/core";
|
|
2
2
|
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
NamespaceContext,
|
|
5
|
+
useNamespaceContext,
|
|
6
|
+
} from "../../contexts/namespace.js";
|
|
4
7
|
import { useSourceFileScope } from "../../scopes/source-file.js";
|
|
5
8
|
import { createNamespaceSymbol } from "../../symbols/factories.js";
|
|
6
|
-
import {
|
|
9
|
+
import { NamespaceScopes } from "../namespace-scopes.jsx";
|
|
7
10
|
import { NamespaceName } from "./namespace-name.jsx";
|
|
8
11
|
|
|
9
12
|
export interface NamespaceProps {
|
|
@@ -25,16 +28,21 @@ export function Namespace(props: NamespaceProps) {
|
|
|
25
28
|
</NamespaceContext.Provider>
|
|
26
29
|
);
|
|
27
30
|
} else {
|
|
31
|
+
const nsContext = useNamespaceContext();
|
|
32
|
+
const hasOuterNamespace = nsContext && !nsContext.symbol.isGlobal;
|
|
33
|
+
|
|
28
34
|
sfScope.hasBlockNamespace = true;
|
|
29
35
|
return (
|
|
30
36
|
<>
|
|
31
|
-
namespace
|
|
37
|
+
namespace{" "}
|
|
38
|
+
<NamespaceName
|
|
39
|
+
symbol={namespaceSymbol}
|
|
40
|
+
relative={!!hasOuterNamespace}
|
|
41
|
+
/>{" "}
|
|
32
42
|
<Block>
|
|
33
|
-
<
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
</NamespaceScope>
|
|
37
|
-
</NamespaceContext.Provider>
|
|
43
|
+
<NamespaceScopes symbol={namespaceSymbol} stopAt={nsContext?.symbol}>
|
|
44
|
+
{props.children}
|
|
45
|
+
</NamespaceScopes>
|
|
38
46
|
</Block>
|
|
39
47
|
</>
|
|
40
48
|
);
|
|
@@ -19,17 +19,41 @@ export function NamespaceScope(props: NamespaceScopProps) {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export interface NamespaceScopesProps {
|
|
22
|
+
/**
|
|
23
|
+
* Target namespace whose scope chain should be established for `children`.
|
|
24
|
+
*
|
|
25
|
+
* For dotted namespaces, this is typically the most nested segment symbol.
|
|
26
|
+
*/
|
|
22
27
|
symbol: NamespaceSymbol;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Optional ancestor namespace that is already in scope.
|
|
31
|
+
*
|
|
32
|
+
* Wrapping stops before this symbol to avoid duplicating existing namespace
|
|
33
|
+
* scopes in the reference chain.
|
|
34
|
+
*/
|
|
35
|
+
stopAt?: NamespaceSymbol;
|
|
23
36
|
children: Children;
|
|
24
37
|
}
|
|
25
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Applies `NamespaceScope` wrappers from `symbol` up through its enclosing
|
|
41
|
+
* namespaces until the root (or `stopAt`, when provided).
|
|
42
|
+
*/
|
|
26
43
|
export function NamespaceScopes(props: NamespaceScopesProps) {
|
|
27
44
|
function wrapWithScope(symbol: NamespaceSymbol, children: Children) {
|
|
45
|
+
if (symbol === props.stopAt) {
|
|
46
|
+
return children;
|
|
47
|
+
}
|
|
48
|
+
|
|
28
49
|
const scopeChildren = (
|
|
29
50
|
<NamespaceScope symbol={symbol}>{children}</NamespaceScope>
|
|
30
51
|
);
|
|
31
52
|
|
|
32
|
-
if (
|
|
53
|
+
if (
|
|
54
|
+
symbol.enclosingNamespace &&
|
|
55
|
+
symbol.enclosingNamespace !== props.stopAt
|
|
56
|
+
) {
|
|
33
57
|
return wrapWithScope(symbol.enclosingNamespace, scopeChildren);
|
|
34
58
|
}
|
|
35
59
|
|
|
@@ -162,3 +162,32 @@ it("can be referenced by refkey", () => {
|
|
|
162
162
|
TestClass;
|
|
163
163
|
`);
|
|
164
164
|
});
|
|
165
|
+
|
|
166
|
+
it("references types across sibling namespaces under the same parent", () => {
|
|
167
|
+
const classRef = refkey();
|
|
168
|
+
|
|
169
|
+
const tree = (
|
|
170
|
+
<Output>
|
|
171
|
+
<SourceFile path="test.cs">
|
|
172
|
+
<Namespace name="Parent">
|
|
173
|
+
<Namespace name="Models">
|
|
174
|
+
<ClassDeclaration name="User" refkey={classRef} />
|
|
175
|
+
</Namespace>
|
|
176
|
+
<hbr />
|
|
177
|
+
<Namespace name="Services">{classRef};</Namespace>
|
|
178
|
+
</Namespace>
|
|
179
|
+
</SourceFile>
|
|
180
|
+
</Output>
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
expect(tree).toRenderTo(`
|
|
184
|
+
namespace Parent {
|
|
185
|
+
namespace Models {
|
|
186
|
+
class User;
|
|
187
|
+
}
|
|
188
|
+
namespace Services {
|
|
189
|
+
Models.User;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
`);
|
|
193
|
+
});
|