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