@alloy-js/typescript 0.6.0 → 0.8.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 (230) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/dist/src/components/ArrayExpression.d.ts.map +1 -1
  3. package/dist/src/components/ArrayExpression.js +9 -3
  4. package/dist/src/components/ArrayExpression.js.map +1 -1
  5. package/dist/src/components/BlockScope.d.ts +13 -0
  6. package/dist/src/components/BlockScope.d.ts.map +1 -0
  7. package/dist/src/components/BlockScope.js +20 -0
  8. package/dist/src/components/BlockScope.js.map +1 -0
  9. package/dist/src/components/CallSignature.d.ts +38 -0
  10. package/dist/src/components/CallSignature.d.ts.map +1 -0
  11. package/dist/src/components/CallSignature.js +26 -0
  12. package/dist/src/components/CallSignature.js.map +1 -0
  13. package/dist/src/components/ClassDeclaration.d.ts +3 -4
  14. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  15. package/dist/src/components/ClassDeclaration.js +99 -41
  16. package/dist/src/components/ClassDeclaration.js.map +1 -1
  17. package/dist/src/components/Declaration.d.ts +21 -4
  18. package/dist/src/components/Declaration.d.ts.map +1 -1
  19. package/dist/src/components/Declaration.js +40 -21
  20. package/dist/src/components/Declaration.js.map +1 -1
  21. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  22. package/dist/src/components/EnumDeclaration.js +19 -6
  23. package/dist/src/components/EnumDeclaration.js.map +1 -1
  24. package/dist/src/components/EnumMember.d.ts +8 -0
  25. package/dist/src/components/EnumMember.d.ts.map +1 -1
  26. package/dist/src/components/EnumMember.js +27 -5
  27. package/dist/src/components/EnumMember.js.map +1 -1
  28. package/dist/src/components/ExportStatement.d.ts.map +1 -1
  29. package/dist/src/components/ExportStatement.js +3 -1
  30. package/dist/src/components/ExportStatement.js.map +1 -1
  31. package/dist/src/components/FunctionCallExpression.js +1 -1
  32. package/dist/src/components/FunctionCallExpression.js.map +1 -1
  33. package/dist/src/components/FunctionDeclaration.d.ts +21 -12
  34. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  35. package/dist/src/components/FunctionDeclaration.js +80 -45
  36. package/dist/src/components/FunctionDeclaration.js.map +1 -1
  37. package/dist/src/components/IfStatement.d.ts +25 -0
  38. package/dist/src/components/IfStatement.d.ts.map +1 -0
  39. package/dist/src/components/IfStatement.js +34 -0
  40. package/dist/src/components/IfStatement.js.map +1 -0
  41. package/dist/src/components/Interface.d.ts +10 -2
  42. package/dist/src/components/Interface.d.ts.map +1 -1
  43. package/dist/src/components/Interface.js +66 -6
  44. package/dist/src/components/Interface.js.map +1 -1
  45. package/dist/src/components/JSDoc.d.ts.map +1 -1
  46. package/dist/src/components/JSDoc.js +1 -5
  47. package/dist/src/components/JSDoc.js.map +1 -1
  48. package/dist/src/components/JSDocParam.d.ts +22 -0
  49. package/dist/src/components/JSDocParam.d.ts.map +1 -0
  50. package/dist/src/components/JSDocParam.js +121 -0
  51. package/dist/src/components/JSDocParam.js.map +1 -0
  52. package/dist/src/components/MemberChainExpression.d.ts +2 -2
  53. package/dist/src/components/MemberChainExpression.d.ts.map +1 -1
  54. package/dist/src/components/MemberChainExpression.js +43 -7
  55. package/dist/src/components/MemberChainExpression.js.map +1 -1
  56. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  57. package/dist/src/components/ObjectExpression.js +9 -2
  58. package/dist/src/components/ObjectExpression.js.map +1 -1
  59. package/dist/src/components/PackageDirectory.d.ts +1 -3
  60. package/dist/src/components/PackageDirectory.d.ts.map +1 -1
  61. package/dist/src/components/PackageDirectory.js +9 -16
  62. package/dist/src/components/PackageDirectory.js.map +1 -1
  63. package/dist/src/components/PackageJson.d.ts +39 -0
  64. package/dist/src/components/PackageJson.d.ts.map +1 -1
  65. package/dist/src/components/PackageJson.js +30 -12
  66. package/dist/src/components/PackageJson.js.map +1 -1
  67. package/dist/src/components/PropertyName.d.ts +19 -0
  68. package/dist/src/components/PropertyName.d.ts.map +1 -0
  69. package/dist/src/components/PropertyName.js +36 -0
  70. package/dist/src/components/PropertyName.js.map +1 -0
  71. package/dist/src/components/SingleLineCommentBlock.d.ts +10 -0
  72. package/dist/src/components/SingleLineCommentBlock.d.ts.map +1 -0
  73. package/dist/src/components/SingleLineCommentBlock.js +20 -0
  74. package/dist/src/components/SingleLineCommentBlock.js.map +1 -0
  75. package/dist/src/components/SourceFile.d.ts +6 -0
  76. package/dist/src/components/SourceFile.d.ts.map +1 -1
  77. package/dist/src/components/SourceFile.js +31 -0
  78. package/dist/src/components/SourceFile.js.map +1 -1
  79. package/dist/src/components/SwitchStatement.d.ts +31 -0
  80. package/dist/src/components/SwitchStatement.d.ts.map +1 -0
  81. package/dist/src/components/SwitchStatement.js +48 -0
  82. package/dist/src/components/SwitchStatement.js.map +1 -0
  83. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  84. package/dist/src/components/TypeDeclaration.js +16 -3
  85. package/dist/src/components/TypeDeclaration.js.map +1 -1
  86. package/dist/src/components/VarDeclaration.d.ts +1 -1
  87. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  88. package/dist/src/components/VarDeclaration.js +20 -6
  89. package/dist/src/components/VarDeclaration.js.map +1 -1
  90. package/dist/src/components/index.d.ts +7 -0
  91. package/dist/src/components/index.d.ts.map +1 -1
  92. package/dist/src/components/index.js +7 -0
  93. package/dist/src/components/index.js.map +1 -1
  94. package/dist/src/components/stc/index.d.ts +35 -112
  95. package/dist/src/components/stc/index.d.ts.map +1 -1
  96. package/dist/src/components/stc/index.js +8 -1
  97. package/dist/src/components/stc/index.js.map +1 -1
  98. package/dist/src/context/index.d.ts +2 -0
  99. package/dist/src/context/index.d.ts.map +1 -0
  100. package/dist/src/context/index.js +1 -0
  101. package/dist/src/context/index.js.map +1 -0
  102. package/dist/src/context/private-scope.d.ts +18 -0
  103. package/dist/src/context/private-scope.d.ts.map +1 -0
  104. package/dist/src/context/private-scope.js +9 -0
  105. package/dist/src/context/private-scope.js.map +1 -0
  106. package/dist/src/index.d.ts +1 -0
  107. package/dist/src/index.d.ts.map +1 -1
  108. package/dist/src/index.js +1 -0
  109. package/dist/src/index.js.map +1 -1
  110. package/dist/src/parameter-descriptor.d.ts +60 -0
  111. package/dist/src/parameter-descriptor.d.ts.map +1 -0
  112. package/dist/src/parameter-descriptor.js +1 -0
  113. package/dist/src/parameter-descriptor.js.map +1 -0
  114. package/dist/src/symbols/reference.d.ts.map +1 -1
  115. package/dist/src/symbols/reference.js +55 -14
  116. package/dist/src/symbols/reference.js.map +1 -1
  117. package/dist/src/symbols/ts-member-scope.d.ts +1 -1
  118. package/dist/src/symbols/ts-member-scope.d.ts.map +1 -1
  119. package/dist/src/symbols/ts-member-scope.js +2 -0
  120. package/dist/src/symbols/ts-member-scope.js.map +1 -1
  121. package/dist/src/symbols/ts-output-symbol.d.ts +7 -3
  122. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  123. package/dist/src/symbols/ts-output-symbol.js +14 -2
  124. package/dist/src/symbols/ts-output-symbol.js.map +1 -1
  125. package/dist/src/symbols/ts-package-scope.d.ts +0 -15
  126. package/dist/src/symbols/ts-package-scope.d.ts.map +1 -1
  127. package/dist/src/symbols/ts-package-scope.js +0 -8
  128. package/dist/src/symbols/ts-package-scope.js.map +1 -1
  129. package/dist/src/utils.d.ts +7 -0
  130. package/dist/src/utils.d.ts.map +1 -1
  131. package/dist/src/utils.js +17 -0
  132. package/dist/src/utils.js.map +1 -1
  133. package/dist/test/block-scope.test.d.ts +2 -0
  134. package/dist/test/block-scope.test.d.ts.map +1 -0
  135. package/dist/test/enum-jsdoc.test.d.ts +2 -0
  136. package/dist/test/enum-jsdoc.test.d.ts.map +1 -0
  137. package/dist/test/function-declaration-jsdoc.test.d.ts +2 -0
  138. package/dist/test/function-declaration-jsdoc.test.d.ts.map +1 -0
  139. package/dist/test/if-statement.test.d.ts +2 -0
  140. package/dist/test/if-statement.test.d.ts.map +1 -0
  141. package/dist/test/interface-jsdoc.test.d.ts +2 -0
  142. package/dist/test/interface-jsdoc.test.d.ts.map +1 -0
  143. package/dist/test/jscomment.test.d.ts +2 -0
  144. package/dist/test/jscomment.test.d.ts.map +1 -0
  145. package/dist/test/jsdoc.test.d.ts.map +1 -1
  146. package/dist/test/jsdocparam.test.d.ts +2 -0
  147. package/dist/test/jsdocparam.test.d.ts.map +1 -0
  148. package/dist/test/name-conflict.test.d.ts +2 -0
  149. package/dist/test/name-conflict.test.d.ts.map +1 -0
  150. package/dist/test/package-json.test.d.ts +2 -0
  151. package/dist/test/package-json.test.d.ts.map +1 -0
  152. package/dist/test/source-file.test.d.ts +2 -0
  153. package/dist/test/source-file.test.d.ts.map +1 -0
  154. package/dist/test/switch-statement.test.d.ts +2 -0
  155. package/dist/test/switch-statement.test.d.ts.map +1 -0
  156. package/dist/test/var-declaration-jsdoc.test.d.ts +2 -0
  157. package/dist/test/var-declaration-jsdoc.test.d.ts.map +1 -0
  158. package/dist/tsconfig.tsbuildinfo +1 -1
  159. package/package.json +3 -3
  160. package/src/components/ArrayExpression.tsx +13 -8
  161. package/src/components/BlockScope.tsx +31 -0
  162. package/src/components/CallSignature.tsx +67 -0
  163. package/src/components/ClassDeclaration.tsx +103 -50
  164. package/src/components/Declaration.tsx +72 -22
  165. package/src/components/EnumDeclaration.tsx +27 -19
  166. package/src/components/EnumMember.tsx +24 -4
  167. package/src/components/ExportStatement.tsx +3 -1
  168. package/src/components/FunctionCallExpression.tsx +1 -1
  169. package/src/components/FunctionDeclaration.tsx +107 -81
  170. package/src/components/IfStatement.tsx +51 -0
  171. package/src/components/Interface.tsx +61 -14
  172. package/src/components/JSDoc.tsx +1 -10
  173. package/src/components/JSDocParam.tsx +115 -0
  174. package/src/components/MemberChainExpression.tsx +37 -5
  175. package/src/components/ObjectExpression.tsx +7 -2
  176. package/src/components/PackageDirectory.tsx +12 -22
  177. package/src/components/PackageJson.tsx +86 -27
  178. package/src/components/PropertyName.tsx +50 -0
  179. package/src/components/SingleLineCommentBlock.tsx +21 -0
  180. package/src/components/SourceFile.tsx +29 -0
  181. package/src/components/SwitchStatement.tsx +78 -0
  182. package/src/components/TypeDeclaration.tsx +11 -4
  183. package/src/components/VarDeclaration.tsx +20 -12
  184. package/src/components/index.ts +7 -0
  185. package/src/components/stc/index.ts +8 -1
  186. package/src/context/index.ts +1 -0
  187. package/src/context/private-scope.ts +23 -0
  188. package/src/index.ts +1 -0
  189. package/src/parameter-descriptor.ts +71 -0
  190. package/src/symbols/reference.ts +77 -22
  191. package/src/symbols/ts-member-scope.ts +6 -2
  192. package/src/symbols/ts-output-symbol.ts +27 -10
  193. package/src/symbols/ts-package-scope.ts +0 -28
  194. package/src/utils.ts +33 -0
  195. package/temp/api.json +5372 -3410
  196. package/test/array.test.tsx +72 -0
  197. package/test/basic.test.tsx +2 -2
  198. package/test/block-scope.test.tsx +29 -0
  199. package/test/class.test.tsx +173 -5
  200. package/test/enum-jsdoc.test.tsx +74 -0
  201. package/test/enum.test.tsx +50 -3
  202. package/test/function-call-expression.test.tsx +5 -2
  203. package/test/function-declaration-jsdoc.test.tsx +89 -0
  204. package/test/function-declaration.test.tsx +13 -9
  205. package/test/if-statement.test.tsx +28 -0
  206. package/test/imports.test.tsx +25 -10
  207. package/test/interface-jsdoc.test.tsx +164 -0
  208. package/test/interface.test.tsx +17 -3
  209. package/test/jscomment.test.tsx +104 -0
  210. package/test/jsdoc.test.tsx +9 -15
  211. package/test/jsdocparam.test.tsx +188 -0
  212. package/test/member-chain.test.tsx +77 -0
  213. package/test/name-conflict.test.tsx +105 -0
  214. package/test/name-policy.test.tsx +1 -1
  215. package/test/package-external.test.tsx +98 -3
  216. package/test/package-json.test.tsx +93 -0
  217. package/test/package.test.tsx +56 -3
  218. package/test/reference.test.tsx +6 -2
  219. package/test/source-file.test.tsx +38 -0
  220. package/test/switch-statement.test.tsx +50 -0
  221. package/test/value-expression.test.tsx +9 -0
  222. package/test/var-declaration-jsdoc.test.tsx +70 -0
  223. package/test/var-declaration.test.tsx +1 -1
  224. package/tsconfig.json +1 -8
  225. package/vitest.config.ts +2 -10
  226. package/dist/src/components/JSDocParagraph.d.ts +0 -10
  227. package/dist/src/components/JSDocParagraph.d.ts.map +0 -1
  228. package/dist/src/components/JSDocParagraph.js +0 -23
  229. package/dist/src/components/JSDocParagraph.js.map +0 -1
  230. package/src/components/JSDocParagraph.tsx +0 -35
@@ -7,39 +7,32 @@ import {
7
7
  For,
8
8
  Indent,
9
9
  Name,
10
- Refkey,
11
- refkey,
10
+ Prose,
12
11
  Scope,
13
12
  Show,
14
13
  taggedComponent,
15
14
  } from "@alloy-js/core";
16
15
  import { Children, onCleanup } from "@alloy-js/core/jsx-runtime";
17
16
  import { useTSNamePolicy } from "../name-policy.js";
17
+ import {
18
+ ParameterDescriptor,
19
+ TypeParameterDescriptor,
20
+ } from "../parameter-descriptor.js";
18
21
  import {
19
22
  createTSSymbol,
20
23
  TSOutputSymbol,
21
24
  TSSymbolFlags,
22
25
  } from "../symbols/index.js";
26
+ import { getCallSignatureProps } from "../utils.js";
27
+ import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
23
28
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
29
+ import { JSDoc } from "./JSDoc.jsx";
30
+ import { JSDocParams } from "./JSDocParam.jsx";
24
31
 
25
- export interface ParameterDescriptor {
26
- type?: Children;
27
- refkey: Refkey;
28
- optional?: boolean;
29
- }
30
-
31
- function isParameterDescriptor(
32
- value: Children | ParameterDescriptor,
33
- ): value is ParameterDescriptor {
34
- return (
35
- typeof value === "object" && value !== null && Object.hasOwn(value, "type")
36
- );
37
- }
38
- export interface FunctionDeclarationProps extends BaseDeclarationProps {
32
+ export interface FunctionDeclarationProps
33
+ extends BaseDeclarationProps,
34
+ CallSignatureProps {
39
35
  async?: boolean;
40
- parameters?: Record<string, Children | ParameterDescriptor>;
41
- typeParameters?: Record<string, Children | ParameterDescriptor> | string[];
42
- returnType?: Children;
43
36
  children?: Children;
44
37
  }
45
38
 
@@ -47,44 +40,62 @@ export const functionParametersTag = Symbol();
47
40
  export const functionTypeParametersTag = Symbol();
48
41
  export const functionBodyTag = Symbol();
49
42
 
43
+ /**
44
+ * A TypeScript function declaration.
45
+ *
46
+ * @remarks
47
+ *
48
+ * Providing parameters and type parameters can be accomplished in one of three
49
+ * ways:
50
+ *
51
+ * 1. As an array of {@link ParameterDescriptor}s or
52
+ * {@link TypeParameterDescriptor}s.
53
+ * 2. As raw content via the `parametersChildren` or `typeParametersChildren`
54
+ * props.
55
+ * 3. As a child of this component via the
56
+ * {@link (FunctionDeclaration:namespace).Parameters} or
57
+ * {@link (FunctionDeclaration:namespace).TypeParameters} components.
58
+ */
50
59
  export function FunctionDeclaration(props: FunctionDeclarationProps) {
51
60
  const children = childrenArray(() => props.children);
52
- const typeParametersChild = findKeyedChild(
53
- children,
54
- functionTypeParametersTag,
55
- );
56
- const parametersChild = findKeyedChild(children, functionParametersTag);
57
- const bodyChild = findKeyedChild(children, functionBodyTag);
61
+ const typeParametersChildren =
62
+ findKeyedChild(children, functionTypeParametersTag) ?? undefined;
63
+ const parametersChildren =
64
+ findKeyedChild(children, functionParametersTag) ?? undefined;
65
+ const bodyChildren = findKeyedChild(children, functionBodyTag) ?? undefined;
58
66
  const filteredChildren = findUnkeyedChildren(children);
59
67
  const returnType = getReturnType(props.returnType, { async: props.async });
60
- const sReturnType = returnType ? <>: {returnType}</> : undefined;
61
68
 
62
- const sTypeParameters =
63
- typeParametersChild ?
64
- <>
65
- {"<"}
66
- {typeParametersChild}
67
- {">"}
68
- </>
69
- : <FunctionDeclaration.TypeParameters parameters={props.typeParameters} />;
70
-
71
- const sParams = parametersChild ?? (
72
- <FunctionDeclaration.Parameters parameters={props.parameters} />
73
- );
74
-
75
- const sBody = bodyChild ?? (
69
+ const sBody = bodyChildren ?? (
76
70
  <FunctionDeclaration.Body>{filteredChildren}</FunctionDeclaration.Body>
77
71
  );
78
72
 
79
73
  const asyncKwd = props.async ? "async " : "";
80
74
 
75
+ const callSignatureProps = getCallSignatureProps(props, {
76
+ parametersChildren,
77
+ typeParametersChildren,
78
+ });
79
+
81
80
  return (
82
- <Declaration {...props} nameKind="function">
83
- {asyncKwd}function <Name />
84
- <Scope name={props.name} kind="function">
85
- {sTypeParameters}({sParams}){sReturnType} {sBody}
86
- </Scope>
87
- </Declaration>
81
+ <>
82
+ <Show when={Boolean(props.doc)}>
83
+ <JSDoc>
84
+ {props.doc && <Prose children={props.doc} />}
85
+ {Array.isArray(props.parameters) && (
86
+ <JSDocParams parameters={props.parameters} />
87
+ )}
88
+ </JSDoc>
89
+ <hbr />
90
+ </Show>
91
+ <Declaration {...props} nameKind="function">
92
+ {asyncKwd}function <Name />
93
+ <Scope name={props.name} kind="function">
94
+ <CallSignature {...callSignatureProps} returnType={returnType} />{" "}
95
+ {sBody}
96
+ </Scope>
97
+ </Declaration>
98
+ </>
88
99
  );
89
100
  }
90
101
 
@@ -97,54 +108,69 @@ function getReturnType(
97
108
  }
98
109
  }
99
110
 
100
- interface DeclaredParameterDescriptor extends ParameterDescriptor {
111
+ interface DeclaredParameterDescriptor
112
+ extends Omit<ParameterDescriptor, "name"> {
101
113
  symbol: TSOutputSymbol;
102
114
  }
103
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[];
104
128
  function normalizeAndDeclareParameters(
105
- parameters: Record<string, Children | ParameterDescriptor> | string[],
129
+ parameters: string[],
130
+ flags?: TSSymbolFlags,
131
+ ): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[];
132
+ function normalizeAndDeclareParameters(
133
+ parameters: ParameterDescriptor[] | TypeParameterDescriptor[] | string[],
106
134
  flags: TSSymbolFlags = TSSymbolFlags.ParameterSymbol,
107
- ): DeclaredParameterDescriptor[] {
135
+ ): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[] {
108
136
  const namePolicy = useTSNamePolicy();
109
- if (Array.isArray(parameters)) {
110
- return parameters.map((paramName) => {
137
+ if (parameters.length === 0) {
138
+ return [];
139
+ }
140
+ if (typeof parameters[0] === "string") {
141
+ return (parameters as string[]).map((paramName) => {
111
142
  const symbol = createTSSymbol({
112
143
  name: namePolicy.getName(
113
144
  paramName,
114
145
  flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
115
146
  ),
116
- refkey: refkey(),
117
147
  tsFlags: flags,
118
148
  });
119
149
 
120
- return { refkey: symbol.refkey, symbol };
150
+ return { refkeys: symbol.refkeys, symbol };
121
151
  });
122
- }
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
+ });
123
163
 
124
- return Object.entries(parameters).map(([paramName, value]) => {
125
- const descriptor: DeclaredParameterDescriptor =
126
- isParameterDescriptor(value) ? value : (
127
- ({
128
- refkey: refkey(),
129
- type: value,
130
- } as any)
131
- );
132
-
133
- const symbol = createTSSymbol({
134
- name: namePolicy.getName(
135
- paramName,
136
- flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
137
- ),
138
- refkey: descriptor.refkey,
139
- tsFlags: flags,
164
+ return {
165
+ ...param,
166
+ symbol,
167
+ };
140
168
  });
141
- descriptor.symbol = symbol;
142
- return descriptor;
143
- });
169
+ }
144
170
  }
145
171
 
146
172
  export interface FunctionTypeParametersProps {
147
- parameters?: Record<string, Children | ParameterDescriptor> | string[];
173
+ parameters?: ParameterDescriptor[] | string[];
148
174
  children?: Children;
149
175
  }
150
176
 
@@ -171,7 +197,7 @@ FunctionDeclaration.TypeParameters = taggedComponent(
171
197
  <>
172
198
  {"<"}
173
199
  <group>
174
- <Indent break="soft">
200
+ <Indent softline>
175
201
  <For each={typeParameters} comma line>
176
202
  {(param) => typeParameter(param)}
177
203
  </For>
@@ -184,21 +210,21 @@ FunctionDeclaration.TypeParameters = taggedComponent(
184
210
  },
185
211
  );
186
212
 
187
- function typeParameter(param: DeclaredParameterDescriptor) {
213
+ function typeParameter(param: DeclaredTypeParameterDescriptor) {
188
214
  return (
189
215
  <group>
190
216
  {param.symbol.name}
191
- <Show when={!!param.type}>
217
+ <Show when={!!param.extends}>
192
218
  {" "}
193
219
  extends
194
- <indent> {param.type}</indent>
220
+ <indent> {param.extends}</indent>
195
221
  </Show>
196
222
  </group>
197
223
  );
198
224
  }
199
225
 
200
226
  export interface FunctionParametersProps {
201
- parameters?: Record<string, Children | ParameterDescriptor>;
227
+ parameters?: ParameterDescriptor[] | string[];
202
228
  children?: Children;
203
229
  }
204
230
 
@@ -209,10 +235,10 @@ FunctionDeclaration.Parameters = taggedComponent(
209
235
  return props.children;
210
236
  }
211
237
 
212
- const parameters = normalizeAndDeclareParameters(props.parameters ?? {});
238
+ const parameters = normalizeAndDeclareParameters(props.parameters ?? []);
213
239
  return (
214
240
  <group>
215
- <Indent break="soft">
241
+ <Indent softline trailingBreak>
216
242
  <For each={parameters} comma line>
217
243
  {(param) => parameter(param)}
218
244
  </For>
@@ -0,0 +1,51 @@
1
+ import { Block } from "@alloy-js/core";
2
+ import { Children } from "@alloy-js/core/jsx-runtime";
3
+
4
+ export interface IfStatementProps {
5
+ condition: Children;
6
+ children: Children;
7
+ }
8
+
9
+ /**
10
+ * An if statement.
11
+ */
12
+ export function IfStatement(props: IfStatementProps) {
13
+ return (
14
+ <>
15
+ if ({props.condition}) <Block>{props.children}</Block>
16
+ </>
17
+ );
18
+ }
19
+
20
+ export interface ElseIfClauseProps {
21
+ condition: Children;
22
+ children: Children;
23
+ }
24
+
25
+ /**
26
+ * An else clause with an if statement.
27
+ */
28
+ export function ElseIfClause(props: ElseIfClauseProps) {
29
+ return (
30
+ <>
31
+ {" "}
32
+ else if ({props.condition}) <Block>{props.children}</Block>
33
+ </>
34
+ );
35
+ }
36
+
37
+ export interface ElseClauseProps {
38
+ children: Children;
39
+ }
40
+
41
+ /**
42
+ * The else clause of an if statement.
43
+ */
44
+ export function ElseClause(props: ElseClauseProps) {
45
+ return (
46
+ <>
47
+ {" "}
48
+ else <Block>{props.children}</Block>
49
+ </>
50
+ );
51
+ }
@@ -1,6 +1,17 @@
1
- import { Block, Children, Name } from "@alloy-js/core";
1
+ import {
2
+ Block,
3
+ Children,
4
+ MemberDeclaration,
5
+ Name,
6
+ OutputSymbolFlags,
7
+ Refkey,
8
+ Show,
9
+ } from "@alloy-js/core";
2
10
  import { useTSNamePolicy } from "../name-policy.js";
3
11
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
12
+ import { JSDoc } from "./JSDoc.jsx";
13
+
14
+ import { PropertyName } from "./PropertyName.jsx";
4
15
 
5
16
  export interface InterfaceDeclarationProps extends BaseDeclarationProps {
6
17
  extends?: Children;
@@ -17,12 +28,20 @@ export interface InterfaceDeclarationProps extends BaseDeclarationProps {
17
28
  */
18
29
  export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
19
30
  const extendsPart = props.extends ? <> extends {props.extends}</> : "";
31
+ const flags = OutputSymbolFlags.StaticMemberContainer;
20
32
 
21
33
  return (
22
- <Declaration {...props} nameKind="interface">
23
- interface <Name />
24
- {extendsPart} <InterfaceExpression>{props.children}</InterfaceExpression>
25
- </Declaration>
34
+ <>
35
+ <Show when={Boolean(props.doc)}>
36
+ <JSDoc children={props.doc} />
37
+ <hbr />
38
+ </Show>
39
+ <Declaration {...props} nameKind="interface" flags={flags}>
40
+ interface <Name />
41
+ {extendsPart}{" "}
42
+ <InterfaceExpression>{props.children}</InterfaceExpression>
43
+ </Declaration>
44
+ </>
26
45
  );
27
46
  }
28
47
 
@@ -41,16 +60,24 @@ export interface InterfaceMemberProps {
41
60
  children?: Children;
42
61
  optional?: boolean;
43
62
  readonly?: boolean;
63
+ doc?: Children;
64
+ refkey?: Refkey | Refkey[];
44
65
  }
45
66
 
46
67
  /**
47
- * Create a TypeScript interface declaration.
68
+ * Create a TypeScript interface member.
69
+ *
70
+ * An interface member can either provide a `name` prop to create a named
71
+ * property, or an `indexer` prop to define an indexer for the interface.
72
+ *
73
+ * The type of the member can be provided either as the `type` prop or as the
74
+ * children of the component.
48
75
  */
49
76
  export function InterfaceMember(props: InterfaceMemberProps) {
50
- const namer = useTSNamePolicy();
51
77
  const type = props.type ?? props.children;
52
78
  const optionality = props.optional ? "?" : "";
53
79
  const readonly = props.readonly ? "readonly " : "";
80
+
54
81
  if (props.indexer) {
55
82
  return (
56
83
  <>
@@ -58,12 +85,32 @@ export function InterfaceMember(props: InterfaceMemberProps) {
58
85
  </>
59
86
  );
60
87
  } else {
61
- return (
62
- <>
63
- {readonly}
64
- {namer.getName(props.name!, "interface-member")}
65
- {optionality}: {type}
66
- </>
67
- );
88
+ const namer = useTSNamePolicy();
89
+ const name = namer.getName(props.name!, "interface-member");
90
+ if (props.refkey) {
91
+ return (
92
+ <MemberDeclaration static name={name} refkey={props.refkey}>
93
+ <Show when={Boolean(props.doc)}>
94
+ <JSDoc children={props.doc} />
95
+ <hbr />
96
+ </Show>
97
+ {readonly}
98
+ <PropertyName />
99
+ {optionality}: {type}
100
+ </MemberDeclaration>
101
+ );
102
+ } else {
103
+ return (
104
+ <>
105
+ <Show when={Boolean(props.doc)}>
106
+ <JSDoc children={props.doc} />
107
+ <hbr />
108
+ </Show>
109
+ {readonly}
110
+ <PropertyName name={name} />
111
+ {optionality}: {type}
112
+ </>
113
+ );
114
+ }
68
115
  }
69
116
  }
@@ -13,16 +13,7 @@ export interface JSDocProps {
13
13
  export function JSDoc(props: JSDocProps) {
14
14
  return (
15
15
  <JSDocComment>
16
- <List
17
- joiner={
18
- <>
19
- <hbr />
20
- <hbr />
21
- </>
22
- }
23
- >
24
- {childrenArray(() => props.children)}
25
- </List>
16
+ <List doubleHardline>{childrenArray(() => props.children)}</List>
26
17
  </JSDocComment>
27
18
  );
28
19
  }
@@ -0,0 +1,115 @@
1
+ import { For, Prose, Show } from "@alloy-js/core";
2
+ import { Children } from "@alloy-js/core/jsx-runtime";
3
+ import { ParameterDescriptor } from "../parameter-descriptor.js";
4
+
5
+ export interface JSDocParamsProps {
6
+ parameters: ParameterDescriptor[] | string[];
7
+ }
8
+
9
+ /**
10
+ * A component that creates a JSDoc block with `@param` tags for each parameter.
11
+ */
12
+ export function JSDocParams(props: JSDocParamsProps) {
13
+ const parameters = normalizeParametersForDoc(props.parameters);
14
+ return (
15
+ <For each={parameters}>
16
+ {(param) => (
17
+ <JSDocParam
18
+ name={param.name}
19
+ type={param.type}
20
+ optional={param.optional}
21
+ hyphen
22
+ >
23
+ {param.doc}
24
+ </JSDocParam>
25
+ )}
26
+ </For>
27
+ );
28
+ }
29
+
30
+ function normalizeParametersForDoc(
31
+ parameters: ParameterDescriptor[] | string[],
32
+ ): ParameterDescriptor[] {
33
+ if (parameters.some((p) => typeof p === "string")) {
34
+ return [];
35
+ }
36
+
37
+ return parameters as ParameterDescriptor[];
38
+ }
39
+
40
+ export interface JSDocParamProps {
41
+ name: Children;
42
+ type?: Children;
43
+ children?: Children;
44
+ hyphen?: boolean;
45
+ optional?: boolean;
46
+ defaultValue?: Children;
47
+ }
48
+
49
+ /**
50
+ * Create a JSDoc parameter set off with `@param`.
51
+ */
52
+ export function JSDocParam(props: JSDocParamProps) {
53
+ return (
54
+ <>
55
+ {"@param "}
56
+ <JSDocParamType type={props.type} />
57
+ <JSDocParamName
58
+ name={props.name}
59
+ optional={props.optional}
60
+ defaultValue={props.defaultValue}
61
+ />
62
+ <JSDocParamDescription hyphen={props.hyphen} children={props.children} />
63
+ </>
64
+ );
65
+ }
66
+
67
+ interface JSDocParamTypeProps {
68
+ type?: Children;
69
+ }
70
+
71
+ function JSDocParamType(props: JSDocParamTypeProps) {
72
+ return (
73
+ <>
74
+ <Show when={Boolean(props.type)}>
75
+ {"{"}
76
+ {props.type}
77
+ {"} "}
78
+ </Show>
79
+ </>
80
+ );
81
+ }
82
+
83
+ interface JSDocParamNameProps {
84
+ name: Children;
85
+ optional?: boolean;
86
+ defaultValue?: Children;
87
+ }
88
+
89
+ function JSDocParamName(props: JSDocParamNameProps) {
90
+ return (
91
+ <>
92
+ <Show when={props.optional}>{"["}</Show>
93
+ {props.name}
94
+ <Show when={Boolean(props.defaultValue)}>={props.defaultValue}</Show>
95
+ <Show when={props.optional}>{"]"}</Show>
96
+ </>
97
+ );
98
+ }
99
+
100
+ interface JSDocParamDescriptionProps {
101
+ children?: Children;
102
+ hyphen?: boolean;
103
+ }
104
+
105
+ function JSDocParamDescription(props: JSDocParamDescriptionProps) {
106
+ return (
107
+ <Show when={Boolean(props.children)}>
108
+ <Show when={props.hyphen} children={" - "} />
109
+ <Show when={!props.hyphen} children={" "} />
110
+ <align width={2}>
111
+ <Prose>{props.children}</Prose>
112
+ </align>
113
+ </Show>
114
+ );
115
+ }
@@ -8,8 +8,8 @@ export interface MemberChainExpressionProps {
8
8
 
9
9
  /**
10
10
  * Create a member chain expression, which is a member expression comprised of
11
- * either children or {@link FunctionCallExpression} components. It is formatted
12
- * such that when a linebreak is required, the FunctionCallExpression
11
+ * either children, {@link FunctionCallExpression} components, or nested member
12
+ * chain expression components. Other component types are ignored.
13
13
  */
14
14
  export function MemberChainExpression(props: MemberChainExpressionProps) {
15
15
  // chunks are constructed by consuming as many non-call expressions as
@@ -17,7 +17,7 @@ export function MemberChainExpression(props: MemberChainExpressionProps) {
17
17
  // any subsequent call expressions
18
18
 
19
19
  const chunks = computed(() => {
20
- const children = childrenArray(() => props.children);
20
+ const children = flattenCallChains(childrenArray(() => props.children));
21
21
  const chunks: Children[][] = [];
22
22
 
23
23
  let currentChunk: Children[] = [];
@@ -37,13 +37,29 @@ export function MemberChainExpression(props: MemberChainExpressionProps) {
37
37
  }
38
38
  }
39
39
 
40
+ if (currentChunk.length > 0) {
41
+ chunks.push(currentChunk);
42
+ }
43
+
40
44
  return [chunks[0], chunks.slice(1)];
41
45
  });
46
+ const groupId = Symbol();
42
47
 
43
48
  return (
44
49
  <group>
45
- <List joiner="." children={chunks.value[0]} />
46
- <Show when={chunks.value[1].length > 0}>
50
+ <group id={groupId}>
51
+ <List joiner="." children={chunks.value[0]} />
52
+ </group>
53
+ <Show when={chunks.value[1].length === 1}>
54
+ <For each={chunks.value[1]} softline>
55
+ {(chunk) => (
56
+ <>
57
+ .<List joiner="." children={chunk} />
58
+ </>
59
+ )}
60
+ </For>
61
+ </Show>
62
+ <Show when={chunks.value[1].length > 1}>
47
63
  <indent>
48
64
  <For each={chunks.value[1]} softline>
49
65
  {(chunk) => (
@@ -57,3 +73,19 @@ export function MemberChainExpression(props: MemberChainExpressionProps) {
57
73
  </group>
58
74
  );
59
75
  }
76
+
77
+ function flattenCallChains(children: Children[]): Children[] {
78
+ const flatChildren = [];
79
+ for (const child of children) {
80
+ if (
81
+ isComponentCreator(child) &&
82
+ child.component === MemberChainExpression
83
+ ) {
84
+ flatChildren.push(...flattenCallChains(child.props.children));
85
+ } else {
86
+ flatChildren.push(child);
87
+ }
88
+ }
89
+
90
+ return flatChildren;
91
+ }
@@ -13,7 +13,9 @@ import {
13
13
  Switch,
14
14
  Wrap,
15
15
  } from "@alloy-js/core";
16
+ import { useTSNamePolicy } from "../name-policy.js";
16
17
  import { createTSSymbol } from "../symbols/index.js";
18
+ import { PropertyName } from "./PropertyName.jsx";
17
19
  import { ValueExpression } from "./ValueExpression.js";
18
20
 
19
21
  export interface ObjectExpressionProps {
@@ -96,7 +98,10 @@ export interface ObjectPropertyProps {
96
98
  export function ObjectProperty(props: ObjectPropertyProps) {
97
99
  let name;
98
100
  if (props.name) {
99
- name = props.name;
101
+ const namer = useTSNamePolicy();
102
+ name = (
103
+ <PropertyName name={namer.getName(props.name, "object-member-data")} />
104
+ );
100
105
  } else if (props.nameExpression) {
101
106
  name = <>[{props.nameExpression}]</>;
102
107
  } else {
@@ -126,7 +131,7 @@ export function ObjectProperty(props: ObjectPropertyProps) {
126
131
  sym ? createAssignmentContext(sym) : undefined;
127
132
  return (
128
133
  <>
129
- {sym ? sym.name : name}:{" "}
134
+ {name}:{" "}
130
135
  <AssignmentContext.Provider value={assignmentContext}>
131
136
  {value}
132
137
  </AssignmentContext.Provider>