@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.
@@ -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
- useMemberDeclaration,
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 {...props} nameKind="interface" flags={flags} kind="type">
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
- <InterfaceExpression>{filteredChildren}</InterfaceExpression>
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 parentMemberSym = useMemberDeclaration();
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
- <Wrap
102
- when={!!parentMemberSym}
103
- with={MemberScope}
104
- props={{ owner: parentMemberSym! }}
105
- >
134
+ <MemberScope owner={symbol}>
106
135
  <Block>{props.children}</Block>
107
- </Wrap>
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
- const memberScope = useMemberScope();
149
-
150
- if (memberScope) {
151
- return (
152
- <MemberDeclaration
153
- static
154
- exactName={name}
155
- kind="type"
156
- nameKind="interface-member"
157
- flags={OutputSymbolFlags.StaticMember}
158
- nullish={props.nullish ?? props.optional}
159
- refkey={props.refkey}
160
- >
161
- <Show when={Boolean(props.doc)}>
162
- <JSDoc children={props.doc} />
163
- <hbr />
164
- </Show>
165
- {readonly}
166
- <PropertyName />
167
- {optionality}: {type}
168
- </MemberDeclaration>
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
- return (
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<A extends unknown[]>(
35
- fn: (...args: A) => Children,
36
- ): (...args: A) => Children {
37
- return (...args: A) => {
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
- {fn(...args)}
42
+ <Comp {...props} />
43
43
  </TypeRefContextDef.Provider>
44
44
  );
45
45
  }
46
46
 
47
- return fn(...args);
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 { InterfaceExpression, InterfaceMember } from "../Interface.jsx";
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
- <InterfaceExpression>
8
- <InterfaceMember name="foo" type="string" />
9
- </InterfaceExpression>,
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
- <InterfaceExpression>
20
- <InterfaceMember name="foo">
21
- <InterfaceExpression>
22
- <InterfaceMember name="bar" type="string" />
23
- </InterfaceExpression>
24
- </InterfaceMember>
25
- </InterfaceExpression>,
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
- <List>
38
- <InterfaceExpression>
39
- <InterfaceMember name="bar" type="string" />
40
- </InterfaceExpression>
41
- <InterfaceExpression>
42
- <InterfaceMember name="bar" type="string" />
43
- </InterfaceExpression>
44
- </List>,
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
- it("separate union", () => {
65
+
66
+ it("separate expressions when in interface declaration", () => {
55
67
  expect(
56
- <List>
57
- <InterfaceExpression>
58
- <InterfaceMember name="bar" type="string" />
59
- </InterfaceExpression>
60
- <InterfaceExpression>
61
- <InterfaceMember name="bar" type="string" />
62
- </InterfaceExpression>
63
- </List>,
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
- bar: string
67
- }
68
- {
69
- bar: string
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<A extends "
3622
+ "text": "export declare function ensureTypeRefContext<TProps>(Comp: "
3623
3623
  },
3624
3624
  {
3625
- "kind": "Content",
3626
- "text": "unknown[]"
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": "(...args: A) => "
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": "(...args: A) => "
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": 6,
3662
- "endIndex": 8
3653
+ "startIndex": 4,
3654
+ "endIndex": 6
3663
3655
  },
3664
3656
  "releaseTag": "Public",
3665
3657
  "overloadIndex": 1,
3666
3658
  "parameters": [
3667
3659
  {
3668
- "parameterName": "fn",
3660
+ "parameterName": "Comp",
3669
3661
  "parameterTypeTokenRange": {
3670
- "startIndex": 3,
3671
- "endIndex": 5
3662
+ "startIndex": 1,
3663
+ "endIndex": 3
3672
3664
  },
3673
3665
  "isOptional": false
3674
3666
  }
3675
3667
  ],
3676
3668
  "typeParameters": [
3677
3669
  {
3678
- "typeParameterName": "A",
3670
+ "typeParameterName": "TProps",
3679
3671
  "constraintTokenRange": {
3680
- "startIndex": 1,
3681
- "endIndex": 2
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>