@alloy-js/typescript 0.9.0 → 0.11.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.
Files changed (203) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/src/builtins/node.js +1 -2
  3. package/dist/src/components/ArrayExpression.js +2 -5
  4. package/dist/src/components/ArrowFunction.d.ts +40 -0
  5. package/dist/src/components/ArrowFunction.d.ts.map +1 -0
  6. package/dist/src/components/ArrowFunction.js +67 -0
  7. package/dist/src/components/BarrelFile.js +1 -2
  8. package/dist/src/components/BlockScope.js +2 -4
  9. package/dist/src/components/CallSignature.js +2 -4
  10. package/dist/src/components/ClassDeclaration.js +2 -6
  11. package/dist/src/components/CommaList.js +2 -5
  12. package/dist/src/components/Declaration.js +3 -5
  13. package/dist/src/components/EnumDeclaration.js +2 -5
  14. package/dist/src/components/EnumMember.js +2 -4
  15. package/dist/src/components/ExportStatement.js +1 -2
  16. package/dist/src/components/FunctionBase.d.ts +60 -0
  17. package/dist/src/components/FunctionBase.d.ts.map +1 -0
  18. package/dist/src/components/FunctionBase.js +207 -0
  19. package/dist/src/components/FunctionCallExpression.js +2 -5
  20. package/dist/src/components/FunctionDeclaration.d.ts +3 -18
  21. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  22. package/dist/src/components/FunctionDeclaration.js +9 -157
  23. package/dist/src/components/FunctionExpression.d.ts +39 -0
  24. package/dist/src/components/FunctionExpression.d.ts.map +1 -0
  25. package/dist/src/components/FunctionExpression.js +66 -0
  26. package/dist/src/components/FunctionType.d.ts +39 -0
  27. package/dist/src/components/FunctionType.d.ts.map +1 -0
  28. package/dist/src/components/FunctionType.js +73 -0
  29. package/dist/src/components/IfStatement.js +2 -4
  30. package/dist/src/components/ImportStatement.js +1 -2
  31. package/dist/src/components/Interface.d.ts.map +1 -1
  32. package/dist/src/components/Interface.js +32 -11
  33. package/dist/src/components/InterfaceMethod.d.ts +43 -0
  34. package/dist/src/components/InterfaceMethod.d.ts.map +1 -0
  35. package/dist/src/components/InterfaceMethod.js +87 -0
  36. package/dist/src/components/JSDoc.js +1 -2
  37. package/dist/src/components/JSDocComment.js +2 -4
  38. package/dist/src/components/JSDocExample.js +2 -5
  39. package/dist/src/components/JSDocParam.js +2 -5
  40. package/dist/src/components/MemberChainExpression.js +2 -4
  41. package/dist/src/components/MemberDeclaration.d.ts +30 -0
  42. package/dist/src/components/MemberDeclaration.d.ts.map +1 -0
  43. package/dist/src/components/MemberDeclaration.js +29 -0
  44. package/dist/src/components/MemberExpression.d.ts +1 -2
  45. package/dist/src/components/MemberExpression.d.ts.map +1 -1
  46. package/dist/src/components/MemberExpression.js +2 -5
  47. package/dist/src/components/ObjectExpression.js +2 -5
  48. package/dist/src/components/PackageDirectory.js +2 -5
  49. package/dist/src/components/PackageJson.js +1 -2
  50. package/dist/src/components/PropertyName.js +1 -2
  51. package/dist/src/components/Reference.js +1 -2
  52. package/dist/src/components/SingleLineCommentBlock.js +2 -4
  53. package/dist/src/components/SourceFile.js +2 -4
  54. package/dist/src/components/SwitchStatement.js +2 -5
  55. package/dist/src/components/TsConfigJson.js +1 -2
  56. package/dist/src/components/TypeDeclaration.js +2 -6
  57. package/dist/src/components/ValueExpression.js +1 -2
  58. package/dist/src/components/VarDeclaration.js +2 -5
  59. package/dist/src/components/index.d.ts +7 -0
  60. package/dist/src/components/index.d.ts.map +1 -1
  61. package/dist/src/components/index.js +7 -1
  62. package/dist/src/components/stc/index.d.ts +8 -0
  63. package/dist/src/components/stc/index.d.ts.map +1 -1
  64. package/dist/src/components/stc/index.js +9 -2
  65. package/dist/src/context/private-scope.js +1 -2
  66. package/dist/src/create-package.js +1 -2
  67. package/dist/src/index.js +1 -2
  68. package/dist/src/name-conflict-resolver.js +1 -2
  69. package/dist/src/name-policy.js +1 -2
  70. package/dist/src/parameter-descriptor.d.ts +27 -17
  71. package/dist/src/parameter-descriptor.d.ts.map +1 -1
  72. package/dist/src/source-directory-data.js +1 -2
  73. package/dist/src/symbols/index.js +1 -2
  74. package/dist/src/symbols/reference.js +2 -3
  75. package/dist/src/symbols/scopes.js +1 -2
  76. package/dist/src/symbols/ts-member-scope.js +1 -2
  77. package/dist/src/symbols/ts-module-scope.js +1 -2
  78. package/dist/src/symbols/ts-output-symbol.js +1 -2
  79. package/dist/src/symbols/ts-package-scope.js +1 -2
  80. package/dist/src/utils.js +1 -2
  81. package/dist/test/array.test.js +105 -0
  82. package/dist/test/arrow-function.test.d.ts +2 -0
  83. package/dist/test/arrow-function.test.d.ts.map +1 -0
  84. package/dist/test/arrow-function.test.js +194 -0
  85. package/dist/test/barrel.test.js +53 -0
  86. package/dist/test/basic.test.js +91 -0
  87. package/dist/test/block-scope.test.js +30 -0
  88. package/dist/test/class.test.js +438 -0
  89. package/dist/test/enum-jsdoc.test.js +88 -0
  90. package/dist/test/enum.test.js +247 -0
  91. package/dist/test/externals.test.js +145 -0
  92. package/dist/test/function-call-expression.test.js +87 -0
  93. package/dist/test/function-declaration-jsdoc.test.js +91 -0
  94. package/dist/test/function-declaration.test.js +228 -0
  95. package/dist/test/function-expression.test.d.ts +2 -0
  96. package/dist/test/function-expression.test.d.ts.map +1 -0
  97. package/dist/test/function-expression.test.js +194 -0
  98. package/dist/test/function-type.test.d.ts +2 -0
  99. package/dist/test/function-type.test.d.ts.map +1 -0
  100. package/dist/test/function-type.test.js +122 -0
  101. package/dist/test/if-statement.test.js +25 -0
  102. package/dist/test/imports.test.js +434 -0
  103. package/dist/test/interface-jsdoc.test.js +178 -0
  104. package/dist/test/interface.test.d.ts.map +1 -1
  105. package/dist/test/interface.test.js +490 -0
  106. package/dist/test/jscomment.test.js +70 -0
  107. package/dist/test/jsdoc.test.js +134 -0
  108. package/dist/test/jsdocparam.test.js +164 -0
  109. package/dist/test/member-chain.test.js +141 -0
  110. package/dist/test/member-expression.test.js +359 -0
  111. package/dist/test/name-conflict.test.js +108 -0
  112. package/dist/test/name-policy.test.js +122 -0
  113. package/dist/test/package-external.test.js +196 -0
  114. package/dist/test/package-json.test.js +93 -0
  115. package/dist/test/package.test.js +136 -0
  116. package/dist/test/reference.test.js +81 -0
  117. package/dist/test/source-file.test.js +36 -0
  118. package/dist/test/switch-statement.test.js +61 -0
  119. package/dist/test/utils.js +45 -0
  120. package/dist/test/value-expression.test.js +30 -0
  121. package/dist/test/values.test.js +351 -0
  122. package/dist/test/var-declaration-jsdoc.test.js +50 -0
  123. package/dist/test/var-declaration.test.js +63 -0
  124. package/dist/test/vitest.d.js +1 -0
  125. package/dist/tsconfig.tsbuildinfo +1 -1
  126. package/package.json +12 -17
  127. package/src/components/ArrowFunction.tsx +87 -0
  128. package/src/components/Declaration.tsx +1 -1
  129. package/src/components/FunctionBase.tsx +271 -0
  130. package/src/components/FunctionDeclaration.tsx +13 -195
  131. package/src/components/FunctionExpression.tsx +86 -0
  132. package/src/components/FunctionType.tsx +88 -0
  133. package/src/components/Interface.tsx +33 -5
  134. package/src/components/InterfaceMethod.tsx +96 -0
  135. package/src/components/MemberDeclaration.tsx +70 -0
  136. package/src/components/MemberExpression.tsx +1 -1
  137. package/src/components/index.ts +11 -0
  138. package/src/components/stc/index.ts +8 -0
  139. package/src/parameter-descriptor.ts +29 -18
  140. package/src/symbols/reference.ts +4 -1
  141. package/temp/api.json +3099 -863
  142. package/test/arrow-function.test.tsx +188 -0
  143. package/test/class.test.tsx +1 -2
  144. package/test/function-call-expression.test.tsx +1 -2
  145. package/test/function-declaration.test.tsx +1 -2
  146. package/test/function-expression.test.tsx +195 -0
  147. package/test/function-type.test.tsx +118 -0
  148. package/test/interface.test.tsx +256 -6
  149. package/tsdoc-metadata.json +1 -1
  150. package/babel.config.cjs +0 -4
  151. package/dist/src/builtins/index.js.map +0 -1
  152. package/dist/src/builtins/node.js.map +0 -1
  153. package/dist/src/components/ArrayExpression.js.map +0 -1
  154. package/dist/src/components/BarrelFile.js.map +0 -1
  155. package/dist/src/components/BlockScope.js.map +0 -1
  156. package/dist/src/components/CallSignature.js.map +0 -1
  157. package/dist/src/components/ClassDeclaration.js.map +0 -1
  158. package/dist/src/components/CommaList.js.map +0 -1
  159. package/dist/src/components/Declaration.js.map +0 -1
  160. package/dist/src/components/EnumDeclaration.js.map +0 -1
  161. package/dist/src/components/EnumMember.js.map +0 -1
  162. package/dist/src/components/ExportStatement.js.map +0 -1
  163. package/dist/src/components/FunctionCallExpression.js.map +0 -1
  164. package/dist/src/components/FunctionDeclaration.js.map +0 -1
  165. package/dist/src/components/IfStatement.js.map +0 -1
  166. package/dist/src/components/ImportStatement.js.map +0 -1
  167. package/dist/src/components/Interface.js.map +0 -1
  168. package/dist/src/components/JSDoc.js.map +0 -1
  169. package/dist/src/components/JSDocComment.js.map +0 -1
  170. package/dist/src/components/JSDocExample.js.map +0 -1
  171. package/dist/src/components/JSDocParam.js.map +0 -1
  172. package/dist/src/components/MemberChainExpression.js.map +0 -1
  173. package/dist/src/components/MemberExpression.js.map +0 -1
  174. package/dist/src/components/ObjectExpression.js.map +0 -1
  175. package/dist/src/components/PackageDirectory.js.map +0 -1
  176. package/dist/src/components/PackageJson.js.map +0 -1
  177. package/dist/src/components/PropertyName.js.map +0 -1
  178. package/dist/src/components/Reference.js.map +0 -1
  179. package/dist/src/components/SingleLineCommentBlock.js.map +0 -1
  180. package/dist/src/components/SourceFile.js.map +0 -1
  181. package/dist/src/components/SwitchStatement.js.map +0 -1
  182. package/dist/src/components/TsConfigJson.js.map +0 -1
  183. package/dist/src/components/TypeDeclaration.js.map +0 -1
  184. package/dist/src/components/ValueExpression.js.map +0 -1
  185. package/dist/src/components/VarDeclaration.js.map +0 -1
  186. package/dist/src/components/index.js.map +0 -1
  187. package/dist/src/components/stc/index.js.map +0 -1
  188. package/dist/src/context/index.js.map +0 -1
  189. package/dist/src/context/private-scope.js.map +0 -1
  190. package/dist/src/create-package.js.map +0 -1
  191. package/dist/src/index.js.map +0 -1
  192. package/dist/src/name-conflict-resolver.js.map +0 -1
  193. package/dist/src/name-policy.js.map +0 -1
  194. package/dist/src/parameter-descriptor.js.map +0 -1
  195. package/dist/src/source-directory-data.js.map +0 -1
  196. package/dist/src/symbols/index.js.map +0 -1
  197. package/dist/src/symbols/reference.js.map +0 -1
  198. package/dist/src/symbols/scopes.js.map +0 -1
  199. package/dist/src/symbols/ts-member-scope.js.map +0 -1
  200. package/dist/src/symbols/ts-module-scope.js.map +0 -1
  201. package/dist/src/symbols/ts-output-symbol.js.map +0 -1
  202. package/dist/src/symbols/ts-package-scope.js.map +0 -1
  203. package/dist/src/utils.js.map +0 -1
@@ -0,0 +1,87 @@
1
+ import {
2
+ childrenArray,
3
+ findKeyedChild,
4
+ findUnkeyedChildren,
5
+ Scope,
6
+ } from "@alloy-js/core";
7
+ import { Children } from "@alloy-js/core/jsx-runtime";
8
+ import { getCallSignatureProps } from "../utils.js";
9
+ import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
10
+ import { Declaration } from "./Declaration.jsx";
11
+ import {
12
+ FunctionBody,
13
+ FunctionParameters,
14
+ getReturnType,
15
+ TypeParameters,
16
+ } from "./FunctionBase.jsx";
17
+
18
+ export interface ArrowFunctionProps extends CallSignatureProps {
19
+ async?: boolean;
20
+ children?: Children;
21
+ }
22
+
23
+ /**
24
+ * A TypeScript arrow function expression.
25
+ *
26
+ * @example
27
+ * ```tsx
28
+ * <FunctionExpression async parameters={[{ name: "a", type: "number" }, { name: "b", type: "number" }]}>
29
+ * return a + b;
30
+ * <FunctionExpression>
31
+ * ```
32
+ * renders to
33
+ * ```ts
34
+ * async (a, b) => { return a + b; }
35
+ * ```
36
+ *
37
+ * @remarks
38
+ *
39
+ * Providing parameters and type parameters can be accomplished in one of three
40
+ * ways:
41
+ *
42
+ * 1. As an array of {@link ParameterDescriptor}s or
43
+ * {@link TypeParameterDescriptor}s.
44
+ * 2. As raw content via the `parametersChildren` or `typeParametersChildren`
45
+ * props.
46
+ * 3. As a child of this component via the
47
+ * {@link (ArrowFunction:namespace).Parameters} or
48
+ * {@link (ArrowFunction:namespace).TypeParameters} components.
49
+ */
50
+ export function ArrowFunction(props: ArrowFunctionProps) {
51
+ const children = childrenArray(() => props.children);
52
+ const typeParametersChildren =
53
+ findKeyedChild(children, TypeParameters.tag) ?? undefined;
54
+ const parametersChildren =
55
+ findKeyedChild(children, FunctionParameters.tag) ?? undefined;
56
+ const bodyChildren = findKeyedChild(children, FunctionBody.tag) ?? undefined;
57
+ const filteredChildren = findUnkeyedChildren(children);
58
+ const returnType = getReturnType(props.returnType, { async: props.async });
59
+
60
+ const sBody = bodyChildren ?? (
61
+ <ArrowFunction.Body>{filteredChildren}</ArrowFunction.Body>
62
+ );
63
+
64
+ const asyncKwd = props.async ? "async " : "";
65
+
66
+ const callSignatureProps = getCallSignatureProps(props, {
67
+ parametersChildren,
68
+ typeParametersChildren,
69
+ });
70
+
71
+ return (
72
+ <>
73
+ <Declaration {...props} nameKind="function">
74
+ {asyncKwd}
75
+ <Scope kind="function">
76
+ <CallSignature {...callSignatureProps} returnType={returnType} />
77
+ {" => "}
78
+ {sBody}
79
+ </Scope>
80
+ </Declaration>
81
+ </>
82
+ );
83
+ }
84
+
85
+ ArrowFunction.TypeParameters = TypeParameters;
86
+ ArrowFunction.Parameters = FunctionParameters;
87
+ ArrowFunction.Body = FunctionBody;
@@ -109,7 +109,7 @@ export function Declaration(props: DeclarationProps) {
109
109
 
110
110
  let tsFlags: TSSymbolFlags = TSSymbolFlags.None;
111
111
  if (props.kind && props.kind === "type") {
112
- tsFlags &= TSSymbolFlags.TypeSymbol;
112
+ tsFlags |= TSSymbolFlags.TypeSymbol;
113
113
  }
114
114
 
115
115
  sym = createTSSymbol({
@@ -0,0 +1,271 @@
1
+ import {
2
+ Block,
3
+ code,
4
+ For,
5
+ Indent,
6
+ onCleanup,
7
+ Show,
8
+ taggedComponent,
9
+ } from "@alloy-js/core";
10
+ import type { Children } from "@alloy-js/core/jsx-runtime";
11
+ import { useTSNamePolicy } from "../name-policy.js";
12
+ import type {
13
+ FunctionTypeParameterDescriptor,
14
+ ParameterDescriptor,
15
+ } from "../parameter-descriptor.js";
16
+ import { TypeParameterDescriptor } from "../parameter-descriptor.js";
17
+ import {
18
+ createTSSymbol,
19
+ TSOutputSymbol,
20
+ TSSymbolFlags,
21
+ } from "../symbols/index.js";
22
+
23
+ const functionParametersTag = Symbol();
24
+ const typeParametersTag = Symbol();
25
+ const functionBodyTag = Symbol();
26
+
27
+ export interface FunctionBodyProps {
28
+ readonly children?: Children;
29
+ }
30
+
31
+ /** Props for function parameters */
32
+ export interface FunctionParametersProps {
33
+ /** Parameters */
34
+ readonly parameters?: ParameterDescriptor[] | string[];
35
+ /** Jsx Children */
36
+ readonly children?: Children;
37
+ }
38
+
39
+ /** Props for a function type(FunctionType or InterfaceMethod) */
40
+ export interface FunctionTypeParametersProps {
41
+ /** Parameters */
42
+ readonly parameters?: ParameterDescriptor[] | string[];
43
+ /** Jsx Children */
44
+ readonly children?: Children;
45
+ }
46
+
47
+ /** Props for type parameters */
48
+ export interface TypeParametersProps {
49
+ /** Parameters */
50
+ parameters?: TypeParameterDescriptor[] | string[];
51
+ /** Jsx Children */
52
+ children?: Children;
53
+ }
54
+
55
+ export const FunctionBody = taggedComponent(
56
+ functionBodyTag,
57
+ function Body(props: FunctionBodyProps) {
58
+ return <Block>{props.children}</Block>;
59
+ },
60
+ );
61
+
62
+ /**
63
+ * Represent function parameters(FunctionDeclaration, FunctionExpression, ArrowFunction, etc.)
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * a: string, b: string = "abc", c?: string,
68
+ * ```
69
+ */
70
+ export const FunctionParameters = taggedComponent(
71
+ functionParametersTag,
72
+ function Parameters(props: FunctionParametersProps) {
73
+ if (props.children) {
74
+ return props.children;
75
+ }
76
+
77
+ const parameters = normalizeAndDeclareParameters(props.parameters ?? []);
78
+ return (
79
+ <group>
80
+ <Indent softline trailingBreak>
81
+ <For each={parameters} comma line>
82
+ {(param) => parameter(param)}
83
+ </For>
84
+ <ifBreak>,</ifBreak>
85
+ </Indent>
86
+ </group>
87
+ );
88
+ },
89
+ );
90
+
91
+ /**
92
+ * Represent parameters for a function type(FunctionType or InterfaceMethod)
93
+ * This differ from {@link FunctionParameters} in that it doesn't allow default values.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * a: string, b?: string
98
+ * ```
99
+ */
100
+ export const FunctionTypeParameters = taggedComponent(
101
+ functionParametersTag,
102
+ function Parameters(props: FunctionTypeParametersProps) {
103
+ if (props.children) {
104
+ return props.children;
105
+ }
106
+
107
+ const parameters = normalizeAndDeclareParameters(props.parameters ?? []);
108
+ return (
109
+ <group>
110
+ <Indent softline trailingBreak>
111
+ <For each={parameters} comma line>
112
+ {(param) => parameter(param)}
113
+ </For>
114
+ <ifBreak>,</ifBreak>
115
+ </Indent>
116
+ </group>
117
+ );
118
+ },
119
+ );
120
+
121
+ function parameter(param: DeclaredParameterDescriptor) {
122
+ return (
123
+ <group>
124
+ {param.symbol.name}
125
+ <Show when={!!param.optional}>?</Show>
126
+ <Show when={!!param.type}>
127
+ <indent>: {param.type}</indent>
128
+ </Show>
129
+ </group>
130
+ );
131
+ }
132
+
133
+ interface DeclaredParameterDescriptor
134
+ extends Omit<ParameterDescriptor, "name"> {
135
+ symbol: TSOutputSymbol;
136
+ }
137
+ interface DeclaredFunctionTypeParameterDescriptor
138
+ extends Omit<FunctionTypeParameterDescriptor, "name"> {
139
+ symbol: TSOutputSymbol;
140
+ }
141
+
142
+ interface DeclaredTypeParameterDescriptor
143
+ extends Omit<TypeParameterDescriptor, "name"> {
144
+ symbol: TSOutputSymbol;
145
+ }
146
+
147
+ function normalizeAndDeclareParameters(
148
+ parameters: FunctionTypeParameterDescriptor[] | string[],
149
+ flags?: TSSymbolFlags,
150
+ ): DeclaredTypeParameterDescriptor[];
151
+ function normalizeAndDeclareParameters(
152
+ parameters: TypeParameterDescriptor[] | string[],
153
+ flags?: TSSymbolFlags,
154
+ ): DeclaredFunctionTypeParameterDescriptor[];
155
+ function normalizeAndDeclareParameters(
156
+ parameters: ParameterDescriptor[] | string[],
157
+ flags?: TSSymbolFlags,
158
+ ): DeclaredParameterDescriptor[];
159
+ function normalizeAndDeclareParameters(
160
+ parameters: string[],
161
+ flags?: TSSymbolFlags,
162
+ ): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[];
163
+ function normalizeAndDeclareParameters(
164
+ parameters:
165
+ | ParameterDescriptor[]
166
+ | FunctionTypeParameterDescriptor[]
167
+ | TypeParameterDescriptor[]
168
+ | string[],
169
+ flags: TSSymbolFlags = TSSymbolFlags.ParameterSymbol,
170
+ ): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[] {
171
+ const namePolicy = useTSNamePolicy();
172
+ if (parameters.length === 0) {
173
+ return [];
174
+ }
175
+ if (typeof parameters[0] === "string") {
176
+ return (parameters as string[]).map((paramName) => {
177
+ const symbol = createTSSymbol({
178
+ name: namePolicy.getName(
179
+ paramName,
180
+ flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
181
+ ),
182
+ tsFlags: flags,
183
+ });
184
+
185
+ return { refkeys: symbol.refkeys, symbol };
186
+ });
187
+ } else {
188
+ return (parameters as ParameterDescriptor[]).map((param) => {
189
+ const symbol = createTSSymbol({
190
+ name: namePolicy.getName(
191
+ param.name,
192
+ flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
193
+ ),
194
+ refkey: param.refkey,
195
+ tsFlags: flags,
196
+ metadata: param.metadata,
197
+ });
198
+
199
+ return {
200
+ ...param,
201
+ symbol,
202
+ };
203
+ });
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Represent type parameters
209
+ *
210
+ * @example
211
+ * ```ts
212
+ * <A, B extends string>
213
+ * ```
214
+ */
215
+ export const TypeParameters = taggedComponent(
216
+ typeParametersTag,
217
+ function TypeParameters(props: TypeParametersProps) {
218
+ if (props.children) {
219
+ return props.children;
220
+ }
221
+
222
+ if (!props.parameters) {
223
+ return undefined;
224
+ }
225
+
226
+ const typeParameters = normalizeAndDeclareParameters(props.parameters);
227
+
228
+ onCleanup(() => {
229
+ for (const param of typeParameters) {
230
+ param.symbol.binder.deleteSymbol(param.symbol);
231
+ }
232
+ });
233
+
234
+ return (
235
+ <>
236
+ {"<"}
237
+ <group>
238
+ <Indent softline>
239
+ <For each={typeParameters} comma line>
240
+ {(param) => typeParameter(param)}
241
+ </For>
242
+ <ifBreak>,</ifBreak>
243
+ </Indent>
244
+ </group>
245
+ {">"}
246
+ </>
247
+ );
248
+ },
249
+ );
250
+
251
+ function typeParameter(param: DeclaredTypeParameterDescriptor) {
252
+ return (
253
+ <group>
254
+ {param.symbol.name}
255
+ <Show when={!!param.extends}>
256
+ {" "}
257
+ extends
258
+ <indent> {param.extends}</indent>
259
+ </Show>
260
+ </group>
261
+ );
262
+ }
263
+
264
+ export function getReturnType(
265
+ returnType: Children | undefined,
266
+ options: { async?: boolean } = { async: false },
267
+ ) {
268
+ if (returnType) {
269
+ return options.async ? code`Promise<${returnType}>` : returnType;
270
+ }
271
+ }
@@ -1,31 +1,22 @@
1
1
  import {
2
- Block,
3
2
  childrenArray,
4
- code,
5
3
  findKeyedChild,
6
4
  findUnkeyedChildren,
7
- For,
8
- Indent,
9
5
  Name,
10
6
  Prose,
11
7
  Scope,
12
8
  Show,
13
- taggedComponent,
14
9
  } from "@alloy-js/core";
15
- import { Children, onCleanup } from "@alloy-js/core/jsx-runtime";
16
- import { useTSNamePolicy } from "../name-policy.js";
17
- import {
18
- ParameterDescriptor,
19
- TypeParameterDescriptor,
20
- } from "../parameter-descriptor.js";
21
- import {
22
- createTSSymbol,
23
- TSOutputSymbol,
24
- TSSymbolFlags,
25
- } from "../symbols/index.js";
10
+ import { Children } from "@alloy-js/core/jsx-runtime";
26
11
  import { getCallSignatureProps } from "../utils.js";
27
12
  import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
28
13
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
14
+ import {
15
+ FunctionBody,
16
+ FunctionParameters,
17
+ getReturnType,
18
+ TypeParameters,
19
+ } from "./FunctionBase.jsx";
29
20
  import { JSDoc } from "./JSDoc.jsx";
30
21
  import { JSDocParams } from "./JSDocParam.jsx";
31
22
 
@@ -36,10 +27,6 @@ export interface FunctionDeclarationProps
36
27
  children?: Children;
37
28
  }
38
29
 
39
- export const functionParametersTag = Symbol();
40
- export const functionTypeParametersTag = Symbol();
41
- export const functionBodyTag = Symbol();
42
-
43
30
  /**
44
31
  * A TypeScript function declaration.
45
32
  *
@@ -59,10 +46,10 @@ export const functionBodyTag = Symbol();
59
46
  export function FunctionDeclaration(props: FunctionDeclarationProps) {
60
47
  const children = childrenArray(() => props.children);
61
48
  const typeParametersChildren =
62
- findKeyedChild(children, functionTypeParametersTag) ?? undefined;
49
+ findKeyedChild(children, TypeParameters.tag) ?? undefined;
63
50
  const parametersChildren =
64
- findKeyedChild(children, functionParametersTag) ?? undefined;
65
- const bodyChildren = findKeyedChild(children, functionBodyTag) ?? undefined;
51
+ findKeyedChild(children, FunctionParameters.tag) ?? undefined;
52
+ const bodyChildren = findKeyedChild(children, FunctionBody.tag) ?? undefined;
66
53
  const filteredChildren = findUnkeyedChildren(children);
67
54
  const returnType = getReturnType(props.returnType, { async: props.async });
68
55
 
@@ -99,175 +86,6 @@ export function FunctionDeclaration(props: FunctionDeclarationProps) {
99
86
  );
100
87
  }
101
88
 
102
- function getReturnType(
103
- returnType: Children | undefined,
104
- options: { async?: boolean } = { async: false },
105
- ) {
106
- if (returnType) {
107
- return options.async ? code`Promise<${returnType}>` : returnType;
108
- }
109
- }
110
-
111
- interface DeclaredParameterDescriptor
112
- extends Omit<ParameterDescriptor, "name"> {
113
- symbol: TSOutputSymbol;
114
- }
115
-
116
- interface DeclaredTypeParameterDescriptor
117
- extends Omit<TypeParameterDescriptor, "name"> {
118
- symbol: TSOutputSymbol;
119
- }
120
- function normalizeAndDeclareParameters(
121
- parameters: TypeParameterDescriptor[] | string[],
122
- flags?: TSSymbolFlags,
123
- ): DeclaredTypeParameterDescriptor[];
124
- function normalizeAndDeclareParameters(
125
- parameters: ParameterDescriptor[] | string[],
126
- flags?: TSSymbolFlags,
127
- ): DeclaredParameterDescriptor[];
128
- function normalizeAndDeclareParameters(
129
- parameters: string[],
130
- flags?: TSSymbolFlags,
131
- ): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[];
132
- function normalizeAndDeclareParameters(
133
- parameters: ParameterDescriptor[] | TypeParameterDescriptor[] | string[],
134
- flags: TSSymbolFlags = TSSymbolFlags.ParameterSymbol,
135
- ): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[] {
136
- const namePolicy = useTSNamePolicy();
137
- if (parameters.length === 0) {
138
- return [];
139
- }
140
- if (typeof parameters[0] === "string") {
141
- return (parameters as string[]).map((paramName) => {
142
- const symbol = createTSSymbol({
143
- name: namePolicy.getName(
144
- paramName,
145
- flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
146
- ),
147
- tsFlags: flags,
148
- });
149
-
150
- return { refkeys: symbol.refkeys, symbol };
151
- });
152
- } else {
153
- return (parameters as ParameterDescriptor[]).map((param) => {
154
- const symbol = createTSSymbol({
155
- name: namePolicy.getName(
156
- param.name,
157
- flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
158
- ),
159
- refkey: param.refkey,
160
- tsFlags: flags,
161
- metadata: param.metadata,
162
- });
163
-
164
- return {
165
- ...param,
166
- symbol,
167
- };
168
- });
169
- }
170
- }
171
-
172
- export interface FunctionTypeParametersProps {
173
- parameters?: ParameterDescriptor[] | string[];
174
- children?: Children;
175
- }
176
-
177
- FunctionDeclaration.TypeParameters = taggedComponent(
178
- functionTypeParametersTag,
179
- function TypeParameters(props: FunctionTypeParametersProps) {
180
- if (props.children) {
181
- return props.children;
182
- }
183
-
184
- if (!props.parameters) {
185
- return undefined;
186
- }
187
-
188
- const typeParameters = normalizeAndDeclareParameters(props.parameters);
189
-
190
- onCleanup(() => {
191
- for (const param of typeParameters) {
192
- param.symbol.binder.deleteSymbol(param.symbol);
193
- }
194
- });
195
-
196
- return (
197
- <>
198
- {"<"}
199
- <group>
200
- <Indent softline>
201
- <For each={typeParameters} comma line>
202
- {(param) => typeParameter(param)}
203
- </For>
204
- <ifBreak>,</ifBreak>
205
- </Indent>
206
- </group>
207
- {">"}
208
- </>
209
- );
210
- },
211
- );
212
-
213
- function typeParameter(param: DeclaredTypeParameterDescriptor) {
214
- return (
215
- <group>
216
- {param.symbol.name}
217
- <Show when={!!param.extends}>
218
- {" "}
219
- extends
220
- <indent> {param.extends}</indent>
221
- </Show>
222
- </group>
223
- );
224
- }
225
-
226
- export interface FunctionParametersProps {
227
- parameters?: ParameterDescriptor[] | string[];
228
- children?: Children;
229
- }
230
-
231
- FunctionDeclaration.Parameters = taggedComponent(
232
- functionParametersTag,
233
- function Parameters(props: FunctionParametersProps) {
234
- if (props.children) {
235
- return props.children;
236
- }
237
-
238
- const parameters = normalizeAndDeclareParameters(props.parameters ?? []);
239
- return (
240
- <group>
241
- <Indent softline trailingBreak>
242
- <For each={parameters} comma line>
243
- {(param) => parameter(param)}
244
- </For>
245
- <ifBreak>,</ifBreak>
246
- </Indent>
247
- </group>
248
- );
249
- },
250
- );
251
-
252
- function parameter(param: DeclaredParameterDescriptor) {
253
- return (
254
- <group>
255
- {param.symbol.name}
256
- <Show when={!!param.optional}>?</Show>
257
- <Show when={!!param.type}>
258
- <indent>: {param.type}</indent>
259
- </Show>
260
- </group>
261
- );
262
- }
263
-
264
- export interface FunctionBodyProps {
265
- children?: Children;
266
- }
267
-
268
- FunctionDeclaration.Body = taggedComponent(
269
- functionBodyTag,
270
- function Body(props: FunctionBodyProps) {
271
- return <Block>{props.children}</Block>;
272
- },
273
- );
89
+ FunctionDeclaration.TypeParameters = TypeParameters;
90
+ FunctionDeclaration.Parameters = FunctionParameters;
91
+ FunctionDeclaration.Body = FunctionBody;
@@ -0,0 +1,86 @@
1
+ import {
2
+ childrenArray,
3
+ findKeyedChild,
4
+ findUnkeyedChildren,
5
+ Scope,
6
+ } from "@alloy-js/core";
7
+ import { Children } from "@alloy-js/core/jsx-runtime";
8
+ import { getCallSignatureProps } from "../utils.js";
9
+ import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
10
+ import { Declaration } from "./Declaration.jsx";
11
+ import {
12
+ FunctionBody,
13
+ FunctionParameters,
14
+ getReturnType,
15
+ TypeParameters,
16
+ } from "./FunctionBase.jsx";
17
+
18
+ export interface FunctionExpressionProps extends CallSignatureProps {
19
+ async?: boolean;
20
+ children?: Children;
21
+ }
22
+
23
+ /**
24
+ * A TypeScript function expression.
25
+ *
26
+ * @example
27
+ * ```tsx
28
+ * <FunctionExpression async parameters={[{ name: "a", type: "number" }, { name: "b", type: "number" }]}>
29
+ * return a + b;
30
+ * <FunctionExpression>
31
+ * ```
32
+ * renders to
33
+ * ```ts
34
+ * async function (a, b) { return a + b; }
35
+ * ```
36
+ * @remarks
37
+ *
38
+ * Providing parameters and type parameters can be accomplished in one of three
39
+ * ways:
40
+ *
41
+ * 1. As an array of {@link ParameterDescriptor}s or
42
+ * {@link TypeParameterDescriptor}s.
43
+ * 2. As raw content via the `parametersChildren` or `typeParametersChildren`
44
+ * props.
45
+ * 3. As a child of this component via the
46
+ * {@link (FunctionExpression:namespace).Parameters} or
47
+ * {@link (FunctionExpression:namespace).TypeParameters} components.
48
+ */
49
+ export function FunctionExpression(props: FunctionExpressionProps) {
50
+ const children = childrenArray(() => props.children);
51
+ const typeParametersChildren =
52
+ findKeyedChild(children, TypeParameters.tag) ?? undefined;
53
+ const parametersChildren =
54
+ findKeyedChild(children, FunctionParameters.tag) ?? undefined;
55
+ const bodyChildren = findKeyedChild(children, FunctionBody.tag) ?? undefined;
56
+ const filteredChildren = findUnkeyedChildren(children);
57
+ const returnType = getReturnType(props.returnType, { async: props.async });
58
+
59
+ const sBody = bodyChildren ?? (
60
+ <FunctionExpression.Body>{filteredChildren}</FunctionExpression.Body>
61
+ );
62
+
63
+ const asyncKwd = props.async ? "async " : "";
64
+
65
+ const callSignatureProps = getCallSignatureProps(props, {
66
+ parametersChildren,
67
+ typeParametersChildren,
68
+ });
69
+
70
+ return (
71
+ <>
72
+ <Declaration {...props} nameKind="function">
73
+ {asyncKwd}
74
+ {"function "}
75
+ <Scope kind="function">
76
+ <CallSignature {...callSignatureProps} returnType={returnType} />{" "}
77
+ {sBody}
78
+ </Scope>
79
+ </Declaration>
80
+ </>
81
+ );
82
+ }
83
+
84
+ FunctionExpression.TypeParameters = TypeParameters;
85
+ FunctionExpression.Parameters = FunctionParameters;
86
+ FunctionExpression.Body = FunctionBody;