@alloy-js/typescript 0.19.0-dev.4 → 0.19.0-dev.5
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/src/components/Interface.d.ts.map +1 -1
- package/dist/src/components/Interface.js +67 -56
- package/dist/src/components/TypeRefContext.d.ts +2 -2
- package/dist/src/components/TypeRefContext.d.ts.map +1 -1
- package/dist/src/components/TypeRefContext.js +5 -5
- package/dist/src/components/interface/expression.test.js +73 -44
- package/dist/test/utils.d.ts +3 -0
- package/dist/test/utils.d.ts.map +1 -1
- package/dist/test/utils.js +12 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/components/Interface.tsx +80 -49
- package/src/components/TypeRefContext.tsx +7 -7
- package/src/components/interface/expression.test.tsx +52 -33
- package/temp/api.json +14 -22
- package/test/utils.tsx +8 -0
|
@@ -2,6 +2,9 @@ import {
|
|
|
2
2
|
Block,
|
|
3
3
|
Children,
|
|
4
4
|
childrenArray,
|
|
5
|
+
createSymbolSlot,
|
|
6
|
+
effect,
|
|
7
|
+
emitSymbol,
|
|
5
8
|
findKeyedChild,
|
|
6
9
|
findUnkeyedChildren,
|
|
7
10
|
MemberScope,
|
|
@@ -9,15 +12,14 @@ import {
|
|
|
9
12
|
OutputSymbolFlags,
|
|
10
13
|
Refkey,
|
|
11
14
|
Show,
|
|
12
|
-
|
|
13
|
-
useMemberScope,
|
|
14
|
-
Wrap,
|
|
15
|
+
takeSymbols,
|
|
15
16
|
} from "@alloy-js/core";
|
|
16
17
|
import { useTSNamePolicy } from "../name-policy.js";
|
|
17
18
|
import { BaseDeclarationProps, Declaration } from "./Declaration.js";
|
|
18
19
|
import { JSDoc } from "./JSDoc.jsx";
|
|
19
20
|
|
|
20
21
|
import { TypeParameterDescriptor } from "../parameter-descriptor.js";
|
|
22
|
+
import { TSOutputSymbol, TSSymbolFlags } from "../symbols/ts-output-symbol.js";
|
|
21
23
|
import { TypeParameters } from "./FunctionBase.jsx";
|
|
22
24
|
import { MemberDeclaration } from "./MemberDeclaration.jsx";
|
|
23
25
|
import { PropertyName } from "./PropertyName.jsx";
|
|
@@ -34,6 +36,20 @@ export interface InterfaceDeclarationProps extends BaseDeclarationProps {
|
|
|
34
36
|
|
|
35
37
|
const _InterfaceDeclaration = ensureTypeRefContext(
|
|
36
38
|
(props: InterfaceDeclarationProps) => {
|
|
39
|
+
const ExprSlot = createSymbolSlot();
|
|
40
|
+
|
|
41
|
+
effect(() => {
|
|
42
|
+
if (ExprSlot.ref.value) {
|
|
43
|
+
const takenSymbols = ExprSlot.ref.value;
|
|
44
|
+
for (const symbol of takenSymbols) {
|
|
45
|
+
// ignore non-transient symbols (likely not the result of an expression).
|
|
46
|
+
if (symbol.flags & OutputSymbolFlags.Transient) {
|
|
47
|
+
symbol.moveTo(sym);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
37
53
|
const children = childrenArray(() => props.children);
|
|
38
54
|
|
|
39
55
|
const typeParametersChildren =
|
|
@@ -51,19 +67,34 @@ const _InterfaceDeclaration = ensureTypeRefContext(
|
|
|
51
67
|
const extendsPart = props.extends ? <> extends {props.extends}</> : "";
|
|
52
68
|
const flags = OutputSymbolFlags.StaticMemberContainer;
|
|
53
69
|
|
|
70
|
+
const name = useTSNamePolicy().getName(props.name, "interface");
|
|
54
71
|
const filteredChildren = findUnkeyedChildren(children);
|
|
55
|
-
|
|
72
|
+
const sym = new TSOutputSymbol(name, {
|
|
73
|
+
refkeys: props.refkey,
|
|
74
|
+
default: props.default,
|
|
75
|
+
export: props.export,
|
|
76
|
+
metadata: props.metadata,
|
|
77
|
+
tsFlags: TSSymbolFlags.TypeSymbol,
|
|
78
|
+
});
|
|
56
79
|
return (
|
|
57
80
|
<>
|
|
58
81
|
<Show when={Boolean(props.doc)}>
|
|
59
82
|
<JSDoc children={props.doc} />
|
|
60
83
|
<hbr />
|
|
61
84
|
</Show>
|
|
62
|
-
<Declaration
|
|
85
|
+
<Declaration
|
|
86
|
+
{...props}
|
|
87
|
+
nameKind="interface"
|
|
88
|
+
flags={flags}
|
|
89
|
+
kind="type"
|
|
90
|
+
symbol={sym}
|
|
91
|
+
>
|
|
63
92
|
interface <Name />
|
|
64
93
|
{sTypeParameters}
|
|
65
94
|
{extendsPart}{" "}
|
|
66
|
-
<
|
|
95
|
+
<ExprSlot>
|
|
96
|
+
<InterfaceExpression>{filteredChildren}</InterfaceExpression>
|
|
97
|
+
</ExprSlot>
|
|
67
98
|
</Declaration>
|
|
68
99
|
</>
|
|
69
100
|
);
|
|
@@ -91,20 +122,18 @@ export interface InterfaceExpressionProps {
|
|
|
91
122
|
|
|
92
123
|
export const InterfaceExpression = ensureTypeRefContext(
|
|
93
124
|
(props: InterfaceExpressionProps) => {
|
|
94
|
-
const
|
|
125
|
+
const symbol = new TSOutputSymbol("", {
|
|
126
|
+
flags:
|
|
127
|
+
OutputSymbolFlags.StaticMemberContainer | OutputSymbolFlags.Transient,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
emitSymbol(symbol);
|
|
95
131
|
|
|
96
|
-
if (parentMemberSym) {
|
|
97
|
-
parentMemberSym.flags |= OutputSymbolFlags.StaticMemberContainer;
|
|
98
|
-
}
|
|
99
132
|
return (
|
|
100
133
|
<group>
|
|
101
|
-
<
|
|
102
|
-
when={!!parentMemberSym}
|
|
103
|
-
with={MemberScope}
|
|
104
|
-
props={{ owner: parentMemberSym! }}
|
|
105
|
-
>
|
|
134
|
+
<MemberScope owner={symbol}>
|
|
106
135
|
<Block>{props.children}</Block>
|
|
107
|
-
</
|
|
136
|
+
</MemberScope>
|
|
108
137
|
</group>
|
|
109
138
|
);
|
|
110
139
|
},
|
|
@@ -145,40 +174,42 @@ export function InterfaceMember(props: InterfaceMemberProps) {
|
|
|
145
174
|
} else {
|
|
146
175
|
const namer = useTSNamePolicy();
|
|
147
176
|
const name = namer.getName(props.name!, "interface-member");
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
);
|
|
177
|
+
let sym = undefined;
|
|
178
|
+
if (props.name) {
|
|
179
|
+
sym = new TSOutputSymbol(name, {
|
|
180
|
+
refkeys: props.refkey,
|
|
181
|
+
flags: OutputSymbolFlags.StaticMember,
|
|
182
|
+
tsFlags:
|
|
183
|
+
TSSymbolFlags.TypeSymbol |
|
|
184
|
+
((props.nullish ?? props.optional) ?
|
|
185
|
+
TSSymbolFlags.Nullish
|
|
186
|
+
: TSSymbolFlags.None),
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const taken = takeSymbols();
|
|
190
|
+
|
|
191
|
+
effect(() => {
|
|
192
|
+
if (taken.size > 1) return;
|
|
193
|
+
const symbol = Array.from(taken)[0];
|
|
194
|
+
if (symbol?.flags & OutputSymbolFlags.Transient) {
|
|
195
|
+
symbol.moveTo(sym!);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
170
198
|
} else {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
<Show when={Boolean(props.doc)}>
|
|
174
|
-
<JSDoc children={props.doc} />
|
|
175
|
-
<hbr />
|
|
176
|
-
</Show>
|
|
177
|
-
{readonly}
|
|
178
|
-
<PropertyName name={name} />
|
|
179
|
-
{optionality}: {type}
|
|
180
|
-
</>
|
|
181
|
-
);
|
|
199
|
+
// noop
|
|
200
|
+
takeSymbols();
|
|
182
201
|
}
|
|
202
|
+
|
|
203
|
+
return (
|
|
204
|
+
<MemberDeclaration symbol={sym}>
|
|
205
|
+
<Show when={Boolean(props.doc)}>
|
|
206
|
+
<JSDoc children={props.doc} />
|
|
207
|
+
<hbr />
|
|
208
|
+
</Show>
|
|
209
|
+
{readonly}
|
|
210
|
+
<PropertyName />
|
|
211
|
+
{optionality}: {type}
|
|
212
|
+
</MemberDeclaration>
|
|
213
|
+
);
|
|
183
214
|
}
|
|
184
215
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Children } from "@alloy-js/core";
|
|
1
|
+
import type { Children, Component } from "@alloy-js/core";
|
|
2
2
|
import {
|
|
3
3
|
isTypeRefContext,
|
|
4
4
|
TypeRefContext as TypeRefContextDef,
|
|
@@ -31,19 +31,19 @@ export const TypeRefContext = ({ children }: TypeRefContextProps) => {
|
|
|
31
31
|
* If not it will wrap in a {@link TypeRefContext} component.
|
|
32
32
|
* If yes it will not add an extra node and return the original component.
|
|
33
33
|
*/
|
|
34
|
-
export function ensureTypeRefContext<
|
|
35
|
-
|
|
36
|
-
): (
|
|
37
|
-
return (
|
|
34
|
+
export function ensureTypeRefContext<TProps>(
|
|
35
|
+
Comp: Component<TProps>,
|
|
36
|
+
): (props: TProps) => Children {
|
|
37
|
+
return (props: TProps) => {
|
|
38
38
|
const ref = isTypeRefContext();
|
|
39
39
|
if (!ref) {
|
|
40
40
|
return (
|
|
41
41
|
<TypeRefContextDef.Provider value={{ type: true }}>
|
|
42
|
-
{
|
|
42
|
+
<Comp {...props} />
|
|
43
43
|
</TypeRefContextDef.Provider>
|
|
44
44
|
);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
return
|
|
47
|
+
return <Comp {...props} />;
|
|
48
48
|
};
|
|
49
49
|
}
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { List } from "@alloy-js/core";
|
|
2
2
|
import { expect, it } from "vitest";
|
|
3
|
-
import {
|
|
3
|
+
import { TestFile } from "../../../test/utils.jsx";
|
|
4
|
+
import {
|
|
5
|
+
InterfaceDeclaration,
|
|
6
|
+
InterfaceExpression,
|
|
7
|
+
InterfaceMember,
|
|
8
|
+
} from "../Interface.jsx";
|
|
4
9
|
|
|
5
10
|
it("render expression", () => {
|
|
6
11
|
expect(
|
|
7
|
-
<
|
|
8
|
-
<
|
|
9
|
-
|
|
12
|
+
<TestFile>
|
|
13
|
+
<InterfaceExpression>
|
|
14
|
+
<InterfaceMember name="foo" type="string" />
|
|
15
|
+
</InterfaceExpression>
|
|
16
|
+
</TestFile>,
|
|
10
17
|
).toRenderTo(`
|
|
11
18
|
{
|
|
12
19
|
foo: string
|
|
@@ -16,13 +23,15 @@ it("render expression", () => {
|
|
|
16
23
|
|
|
17
24
|
it("render nested", () => {
|
|
18
25
|
expect(
|
|
19
|
-
<
|
|
20
|
-
<
|
|
21
|
-
<
|
|
22
|
-
<
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
<TestFile>
|
|
27
|
+
<InterfaceExpression>
|
|
28
|
+
<InterfaceMember name="foo">
|
|
29
|
+
<InterfaceExpression>
|
|
30
|
+
<InterfaceMember name="bar" type="string" />
|
|
31
|
+
</InterfaceExpression>
|
|
32
|
+
</InterfaceMember>
|
|
33
|
+
</InterfaceExpression>
|
|
34
|
+
</TestFile>,
|
|
26
35
|
).toRenderTo(`
|
|
27
36
|
{
|
|
28
37
|
foo: {
|
|
@@ -34,14 +43,16 @@ it("render nested", () => {
|
|
|
34
43
|
|
|
35
44
|
it("separate expressions don't conflict in name", () => {
|
|
36
45
|
expect(
|
|
37
|
-
<
|
|
38
|
-
<
|
|
39
|
-
<
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
<
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
<TestFile>
|
|
47
|
+
<List>
|
|
48
|
+
<InterfaceExpression>
|
|
49
|
+
<InterfaceMember name="bar" type="string" />
|
|
50
|
+
</InterfaceExpression>
|
|
51
|
+
<InterfaceExpression>
|
|
52
|
+
<InterfaceMember name="bar" type="string" />
|
|
53
|
+
</InterfaceExpression>
|
|
54
|
+
</List>
|
|
55
|
+
</TestFile>,
|
|
45
56
|
).toRenderTo(`
|
|
46
57
|
{
|
|
47
58
|
bar: string
|
|
@@ -51,22 +62,30 @@ it("separate expressions don't conflict in name", () => {
|
|
|
51
62
|
}
|
|
52
63
|
`);
|
|
53
64
|
});
|
|
54
|
-
|
|
65
|
+
|
|
66
|
+
it("separate expressions when in interface declaration", () => {
|
|
55
67
|
expect(
|
|
56
|
-
<
|
|
57
|
-
<
|
|
58
|
-
<InterfaceMember name="
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
68
|
+
<TestFile>
|
|
69
|
+
<InterfaceDeclaration name="Test">
|
|
70
|
+
<InterfaceMember name="foo">
|
|
71
|
+
<List joiner=" | ">
|
|
72
|
+
<InterfaceExpression>
|
|
73
|
+
<InterfaceMember name="bar" type="string" />
|
|
74
|
+
</InterfaceExpression>
|
|
75
|
+
<InterfaceExpression>
|
|
76
|
+
<InterfaceMember name="bar" type="string" />
|
|
77
|
+
</InterfaceExpression>
|
|
78
|
+
</List>
|
|
79
|
+
</InterfaceMember>
|
|
80
|
+
</InterfaceDeclaration>
|
|
81
|
+
</TestFile>,
|
|
64
82
|
).toRenderTo(`
|
|
65
|
-
{
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
83
|
+
interface Test {
|
|
84
|
+
foo: {
|
|
85
|
+
bar: string
|
|
86
|
+
} | {
|
|
87
|
+
bar: string
|
|
88
|
+
}
|
|
70
89
|
}
|
|
71
90
|
`);
|
|
72
91
|
});
|
package/temp/api.json
CHANGED
|
@@ -3619,24 +3619,16 @@
|
|
|
3619
3619
|
"excerptTokens": [
|
|
3620
3620
|
{
|
|
3621
3621
|
"kind": "Content",
|
|
3622
|
-
"text": "export declare function ensureTypeRefContext<
|
|
3622
|
+
"text": "export declare function ensureTypeRefContext<TProps>(Comp: "
|
|
3623
3623
|
},
|
|
3624
3624
|
{
|
|
3625
|
-
"kind": "
|
|
3626
|
-
"text": "
|
|
3627
|
-
|
|
3628
|
-
{
|
|
3629
|
-
"kind": "Content",
|
|
3630
|
-
"text": ">(fn: "
|
|
3625
|
+
"kind": "Reference",
|
|
3626
|
+
"text": "Component",
|
|
3627
|
+
"canonicalReference": "@alloy-js/core!Component:interface"
|
|
3631
3628
|
},
|
|
3632
3629
|
{
|
|
3633
3630
|
"kind": "Content",
|
|
3634
|
-
"text": "
|
|
3635
|
-
},
|
|
3636
|
-
{
|
|
3637
|
-
"kind": "Reference",
|
|
3638
|
-
"text": "Children",
|
|
3639
|
-
"canonicalReference": "@alloy-js/core!Children:type"
|
|
3631
|
+
"text": "<TProps>"
|
|
3640
3632
|
},
|
|
3641
3633
|
{
|
|
3642
3634
|
"kind": "Content",
|
|
@@ -3644,7 +3636,7 @@
|
|
|
3644
3636
|
},
|
|
3645
3637
|
{
|
|
3646
3638
|
"kind": "Content",
|
|
3647
|
-
"text": "(
|
|
3639
|
+
"text": "(props: TProps) => "
|
|
3648
3640
|
},
|
|
3649
3641
|
{
|
|
3650
3642
|
"kind": "Reference",
|
|
@@ -3658,27 +3650,27 @@
|
|
|
3658
3650
|
],
|
|
3659
3651
|
"fileUrlPath": "src/components/TypeRefContext.tsx",
|
|
3660
3652
|
"returnTypeTokenRange": {
|
|
3661
|
-
"startIndex":
|
|
3662
|
-
"endIndex":
|
|
3653
|
+
"startIndex": 4,
|
|
3654
|
+
"endIndex": 6
|
|
3663
3655
|
},
|
|
3664
3656
|
"releaseTag": "Public",
|
|
3665
3657
|
"overloadIndex": 1,
|
|
3666
3658
|
"parameters": [
|
|
3667
3659
|
{
|
|
3668
|
-
"parameterName": "
|
|
3660
|
+
"parameterName": "Comp",
|
|
3669
3661
|
"parameterTypeTokenRange": {
|
|
3670
|
-
"startIndex":
|
|
3671
|
-
"endIndex":
|
|
3662
|
+
"startIndex": 1,
|
|
3663
|
+
"endIndex": 3
|
|
3672
3664
|
},
|
|
3673
3665
|
"isOptional": false
|
|
3674
3666
|
}
|
|
3675
3667
|
],
|
|
3676
3668
|
"typeParameters": [
|
|
3677
3669
|
{
|
|
3678
|
-
"typeParameterName": "
|
|
3670
|
+
"typeParameterName": "TProps",
|
|
3679
3671
|
"constraintTokenRange": {
|
|
3680
|
-
"startIndex":
|
|
3681
|
-
"endIndex":
|
|
3672
|
+
"startIndex": 0,
|
|
3673
|
+
"endIndex": 0
|
|
3682
3674
|
},
|
|
3683
3675
|
"defaultTypeTokenRange": {
|
|
3684
3676
|
"startIndex": 0,
|
package/test/utils.tsx
CHANGED
|
@@ -10,6 +10,14 @@ import { dedent } from "@alloy-js/core/testing";
|
|
|
10
10
|
import { expect } from "vitest";
|
|
11
11
|
import * as ts from "../src/index.js";
|
|
12
12
|
|
|
13
|
+
export function TestFile(props: { children?: Children }) {
|
|
14
|
+
return (
|
|
15
|
+
<Output>
|
|
16
|
+
<ts.SourceFile path="test.ts">{props.children}</ts.SourceFile>
|
|
17
|
+
</Output>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
13
21
|
export function toSourceText(c: Children, options?: PrintTreeOptions): string {
|
|
14
22
|
const res = render(
|
|
15
23
|
<Output>
|