@alloy-js/typescript 0.7.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 (202) hide show
  1. package/CHANGELOG.md +28 -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 -8
  18. package/dist/src/components/Declaration.d.ts.map +1 -1
  19. package/dist/src/components/Declaration.js +39 -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 +18 -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/FunctionCallExpression.js +1 -1
  29. package/dist/src/components/FunctionCallExpression.js.map +1 -1
  30. package/dist/src/components/FunctionDeclaration.d.ts +21 -13
  31. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  32. package/dist/src/components/FunctionDeclaration.js +79 -45
  33. package/dist/src/components/FunctionDeclaration.js.map +1 -1
  34. package/dist/src/components/IfStatement.d.ts +25 -0
  35. package/dist/src/components/IfStatement.d.ts.map +1 -0
  36. package/dist/src/components/IfStatement.js +34 -0
  37. package/dist/src/components/IfStatement.js.map +1 -0
  38. package/dist/src/components/Interface.d.ts +10 -2
  39. package/dist/src/components/Interface.d.ts.map +1 -1
  40. package/dist/src/components/Interface.js +66 -6
  41. package/dist/src/components/Interface.js.map +1 -1
  42. package/dist/src/components/JSDoc.d.ts.map +1 -1
  43. package/dist/src/components/JSDoc.js +1 -5
  44. package/dist/src/components/JSDoc.js.map +1 -1
  45. package/dist/src/components/JSDocParam.d.ts +22 -0
  46. package/dist/src/components/JSDocParam.d.ts.map +1 -0
  47. package/dist/src/components/JSDocParam.js +121 -0
  48. package/dist/src/components/JSDocParam.js.map +1 -0
  49. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  50. package/dist/src/components/ObjectExpression.js +9 -2
  51. package/dist/src/components/ObjectExpression.js.map +1 -1
  52. package/dist/src/components/PropertyName.d.ts +19 -0
  53. package/dist/src/components/PropertyName.d.ts.map +1 -0
  54. package/dist/src/components/PropertyName.js +36 -0
  55. package/dist/src/components/PropertyName.js.map +1 -0
  56. package/dist/src/components/SingleLineCommentBlock.d.ts +10 -0
  57. package/dist/src/components/SingleLineCommentBlock.d.ts.map +1 -0
  58. package/dist/src/components/SingleLineCommentBlock.js +20 -0
  59. package/dist/src/components/SingleLineCommentBlock.js.map +1 -0
  60. package/dist/src/components/SourceFile.d.ts +6 -0
  61. package/dist/src/components/SourceFile.d.ts.map +1 -1
  62. package/dist/src/components/SourceFile.js +31 -0
  63. package/dist/src/components/SourceFile.js.map +1 -1
  64. package/dist/src/components/SwitchStatement.d.ts +31 -0
  65. package/dist/src/components/SwitchStatement.d.ts.map +1 -0
  66. package/dist/src/components/SwitchStatement.js +48 -0
  67. package/dist/src/components/SwitchStatement.js.map +1 -0
  68. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  69. package/dist/src/components/TypeDeclaration.js +16 -3
  70. package/dist/src/components/TypeDeclaration.js.map +1 -1
  71. package/dist/src/components/VarDeclaration.d.ts +1 -1
  72. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  73. package/dist/src/components/VarDeclaration.js +19 -6
  74. package/dist/src/components/VarDeclaration.js.map +1 -1
  75. package/dist/src/components/index.d.ts +7 -0
  76. package/dist/src/components/index.d.ts.map +1 -1
  77. package/dist/src/components/index.js +7 -0
  78. package/dist/src/components/index.js.map +1 -1
  79. package/dist/src/components/stc/index.d.ts +35 -112
  80. package/dist/src/components/stc/index.d.ts.map +1 -1
  81. package/dist/src/components/stc/index.js +8 -1
  82. package/dist/src/components/stc/index.js.map +1 -1
  83. package/dist/src/context/index.d.ts +2 -0
  84. package/dist/src/context/index.d.ts.map +1 -0
  85. package/dist/src/context/index.js +1 -0
  86. package/dist/src/context/index.js.map +1 -0
  87. package/dist/src/context/private-scope.d.ts +18 -0
  88. package/dist/src/context/private-scope.d.ts.map +1 -0
  89. package/dist/src/context/private-scope.js +9 -0
  90. package/dist/src/context/private-scope.js.map +1 -0
  91. package/dist/src/index.d.ts +1 -0
  92. package/dist/src/index.d.ts.map +1 -1
  93. package/dist/src/index.js +1 -0
  94. package/dist/src/index.js.map +1 -1
  95. package/dist/src/parameter-descriptor.d.ts +60 -0
  96. package/dist/src/parameter-descriptor.d.ts.map +1 -0
  97. package/dist/src/parameter-descriptor.js +1 -0
  98. package/dist/src/parameter-descriptor.js.map +1 -0
  99. package/dist/src/symbols/reference.d.ts.map +1 -1
  100. package/dist/src/symbols/reference.js +50 -12
  101. package/dist/src/symbols/reference.js.map +1 -1
  102. package/dist/src/symbols/ts-member-scope.d.ts +1 -1
  103. package/dist/src/symbols/ts-member-scope.d.ts.map +1 -1
  104. package/dist/src/symbols/ts-member-scope.js +2 -0
  105. package/dist/src/symbols/ts-member-scope.js.map +1 -1
  106. package/dist/src/symbols/ts-output-symbol.d.ts +7 -4
  107. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  108. package/dist/src/symbols/ts-output-symbol.js +11 -2
  109. package/dist/src/symbols/ts-output-symbol.js.map +1 -1
  110. package/dist/src/utils.d.ts +7 -0
  111. package/dist/src/utils.d.ts.map +1 -1
  112. package/dist/src/utils.js +17 -0
  113. package/dist/src/utils.js.map +1 -1
  114. package/dist/test/block-scope.test.d.ts +2 -0
  115. package/dist/test/block-scope.test.d.ts.map +1 -0
  116. package/dist/test/enum-jsdoc.test.d.ts +2 -0
  117. package/dist/test/enum-jsdoc.test.d.ts.map +1 -0
  118. package/dist/test/function-declaration-jsdoc.test.d.ts +2 -0
  119. package/dist/test/function-declaration-jsdoc.test.d.ts.map +1 -0
  120. package/dist/test/if-statement.test.d.ts +2 -0
  121. package/dist/test/if-statement.test.d.ts.map +1 -0
  122. package/dist/test/interface-jsdoc.test.d.ts +2 -0
  123. package/dist/test/interface-jsdoc.test.d.ts.map +1 -0
  124. package/dist/test/jscomment.test.d.ts +2 -0
  125. package/dist/test/jscomment.test.d.ts.map +1 -0
  126. package/dist/test/jsdoc.test.d.ts.map +1 -1
  127. package/dist/test/jsdocparam.test.d.ts +2 -0
  128. package/dist/test/jsdocparam.test.d.ts.map +1 -0
  129. package/dist/test/name-conflict.test.d.ts +2 -0
  130. package/dist/test/name-conflict.test.d.ts.map +1 -0
  131. package/dist/test/package-json.test.d.ts +2 -0
  132. package/dist/test/package-json.test.d.ts.map +1 -0
  133. package/dist/test/source-file.test.d.ts +2 -0
  134. package/dist/test/source-file.test.d.ts.map +1 -0
  135. package/dist/test/switch-statement.test.d.ts +2 -0
  136. package/dist/test/switch-statement.test.d.ts.map +1 -0
  137. package/dist/test/var-declaration-jsdoc.test.d.ts +2 -0
  138. package/dist/test/var-declaration-jsdoc.test.d.ts.map +1 -0
  139. package/dist/tsconfig.tsbuildinfo +1 -1
  140. package/package.json +3 -3
  141. package/src/components/ArrayExpression.tsx +13 -12
  142. package/src/components/BlockScope.tsx +31 -0
  143. package/src/components/CallSignature.tsx +67 -0
  144. package/src/components/ClassDeclaration.tsx +103 -50
  145. package/src/components/Declaration.tsx +72 -27
  146. package/src/components/EnumDeclaration.tsx +26 -18
  147. package/src/components/EnumMember.tsx +24 -4
  148. package/src/components/FunctionCallExpression.tsx +1 -1
  149. package/src/components/FunctionDeclaration.tsx +106 -82
  150. package/src/components/IfStatement.tsx +51 -0
  151. package/src/components/Interface.tsx +61 -14
  152. package/src/components/JSDoc.tsx +1 -10
  153. package/src/components/JSDocParam.tsx +115 -0
  154. package/src/components/ObjectExpression.tsx +7 -2
  155. package/src/components/PropertyName.tsx +50 -0
  156. package/src/components/SingleLineCommentBlock.tsx +21 -0
  157. package/src/components/SourceFile.tsx +29 -0
  158. package/src/components/SwitchStatement.tsx +78 -0
  159. package/src/components/TypeDeclaration.tsx +11 -4
  160. package/src/components/VarDeclaration.tsx +19 -11
  161. package/src/components/index.ts +7 -0
  162. package/src/components/stc/index.ts +8 -1
  163. package/src/context/index.ts +1 -0
  164. package/src/context/private-scope.ts +23 -0
  165. package/src/index.ts +1 -0
  166. package/src/parameter-descriptor.ts +71 -0
  167. package/src/symbols/reference.ts +70 -18
  168. package/src/symbols/ts-member-scope.ts +6 -2
  169. package/src/symbols/ts-output-symbol.ts +24 -11
  170. package/src/utils.ts +33 -0
  171. package/temp/api.json +3816 -1925
  172. package/test/array.test.tsx +60 -0
  173. package/test/basic.test.tsx +2 -2
  174. package/test/block-scope.test.tsx +29 -0
  175. package/test/class.test.tsx +173 -5
  176. package/test/enum-jsdoc.test.tsx +74 -0
  177. package/test/enum.test.tsx +50 -3
  178. package/test/function-call-expression.test.tsx +5 -2
  179. package/test/function-declaration-jsdoc.test.tsx +89 -0
  180. package/test/function-declaration.test.tsx +13 -9
  181. package/test/if-statement.test.tsx +28 -0
  182. package/test/interface-jsdoc.test.tsx +164 -0
  183. package/test/interface.test.tsx +14 -0
  184. package/test/jscomment.test.tsx +104 -0
  185. package/test/jsdoc.test.tsx +9 -15
  186. package/test/jsdocparam.test.tsx +188 -0
  187. package/test/name-conflict.test.tsx +105 -0
  188. package/test/name-policy.test.tsx +1 -1
  189. package/test/package-json.test.tsx +93 -0
  190. package/test/reference.test.tsx +6 -2
  191. package/test/source-file.test.tsx +38 -0
  192. package/test/switch-statement.test.tsx +50 -0
  193. package/test/value-expression.test.tsx +9 -0
  194. package/test/var-declaration-jsdoc.test.tsx +70 -0
  195. package/test/var-declaration.test.tsx +1 -1
  196. package/tsconfig.json +1 -8
  197. package/vitest.config.ts +2 -10
  198. package/dist/src/components/JSDocParagraph.d.ts +0 -10
  199. package/dist/src/components/JSDocParagraph.d.ts.map +0 -1
  200. package/dist/src/components/JSDocParagraph.js +0 -23
  201. package/dist/src/components/JSDocParagraph.js.map +0 -1
  202. package/src/components/JSDocParagraph.tsx +0 -35
@@ -7,40 +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
- refkeys?: Refkey[];
29
- optional?: boolean;
30
- }
31
-
32
- function isParameterDescriptor(
33
- value: Children | ParameterDescriptor,
34
- ): value is ParameterDescriptor {
35
- return (
36
- typeof value === "object" && value !== null && Object.hasOwn(value, "type")
37
- );
38
- }
39
- export interface FunctionDeclarationProps extends BaseDeclarationProps {
32
+ export interface FunctionDeclarationProps
33
+ extends BaseDeclarationProps,
34
+ CallSignatureProps {
40
35
  async?: boolean;
41
- parameters?: Record<string, Children | ParameterDescriptor>;
42
- typeParameters?: Record<string, Children | ParameterDescriptor> | string[];
43
- returnType?: Children;
44
36
  children?: Children;
45
37
  }
46
38
 
@@ -48,44 +40,62 @@ export const functionParametersTag = Symbol();
48
40
  export const functionTypeParametersTag = Symbol();
49
41
  export const functionBodyTag = Symbol();
50
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
+ */
51
59
  export function FunctionDeclaration(props: FunctionDeclarationProps) {
52
60
  const children = childrenArray(() => props.children);
53
- const typeParametersChild = findKeyedChild(
54
- children,
55
- functionTypeParametersTag,
56
- );
57
- const parametersChild = findKeyedChild(children, functionParametersTag);
58
- 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;
59
66
  const filteredChildren = findUnkeyedChildren(children);
60
67
  const returnType = getReturnType(props.returnType, { async: props.async });
61
- const sReturnType = returnType ? <>: {returnType}</> : undefined;
62
68
 
63
- const sTypeParameters =
64
- typeParametersChild ?
65
- <>
66
- {"<"}
67
- {typeParametersChild}
68
- {">"}
69
- </>
70
- : <FunctionDeclaration.TypeParameters parameters={props.typeParameters} />;
71
-
72
- const sParams = parametersChild ?? (
73
- <FunctionDeclaration.Parameters parameters={props.parameters} />
74
- );
75
-
76
- const sBody = bodyChild ?? (
69
+ const sBody = bodyChildren ?? (
77
70
  <FunctionDeclaration.Body>{filteredChildren}</FunctionDeclaration.Body>
78
71
  );
79
72
 
80
73
  const asyncKwd = props.async ? "async " : "";
81
74
 
75
+ const callSignatureProps = getCallSignatureProps(props, {
76
+ parametersChildren,
77
+ typeParametersChildren,
78
+ });
79
+
82
80
  return (
83
- <Declaration {...props} nameKind="function">
84
- {asyncKwd}function <Name />
85
- <Scope name={props.name} kind="function">
86
- {sTypeParameters}({sParams}){sReturnType} {sBody}
87
- </Scope>
88
- </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
+ </>
89
99
  );
90
100
  }
91
101
 
@@ -98,55 +108,69 @@ function getReturnType(
98
108
  }
99
109
  }
100
110
 
101
- interface DeclaredParameterDescriptor extends ParameterDescriptor {
111
+ interface DeclaredParameterDescriptor
112
+ extends Omit<ParameterDescriptor, "name"> {
102
113
  symbol: TSOutputSymbol;
103
114
  }
104
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[];
105
128
  function normalizeAndDeclareParameters(
106
- parameters: Record<string, Children | ParameterDescriptor> | string[],
129
+ parameters: string[],
130
+ flags?: TSSymbolFlags,
131
+ ): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[];
132
+ function normalizeAndDeclareParameters(
133
+ parameters: ParameterDescriptor[] | TypeParameterDescriptor[] | string[],
107
134
  flags: TSSymbolFlags = TSSymbolFlags.ParameterSymbol,
108
- ): DeclaredParameterDescriptor[] {
135
+ ): DeclaredParameterDescriptor[] | DeclaredTypeParameterDescriptor[] {
109
136
  const namePolicy = useTSNamePolicy();
110
- if (Array.isArray(parameters)) {
111
- 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) => {
112
142
  const symbol = createTSSymbol({
113
143
  name: namePolicy.getName(
114
144
  paramName,
115
145
  flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
116
146
  ),
117
- refkey: refkey(),
118
147
  tsFlags: flags,
119
148
  });
120
149
 
121
150
  return { refkeys: symbol.refkeys, symbol };
122
151
  });
123
- }
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
+ });
124
163
 
125
- return Object.entries(parameters).map(([paramName, value]) => {
126
- const descriptor: DeclaredParameterDescriptor =
127
- isParameterDescriptor(value) ? value : (
128
- ({
129
- refkey: refkey(),
130
- type: value,
131
- } as any)
132
- );
133
-
134
- const symbol = createTSSymbol({
135
- name: namePolicy.getName(
136
- paramName,
137
- flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
138
- ),
139
- refkey: descriptor.refkey,
140
- refkeys: descriptor.refkeys,
141
- tsFlags: flags,
164
+ return {
165
+ ...param,
166
+ symbol,
167
+ };
142
168
  });
143
- descriptor.symbol = symbol;
144
- return descriptor;
145
- });
169
+ }
146
170
  }
147
171
 
148
172
  export interface FunctionTypeParametersProps {
149
- parameters?: Record<string, Children | ParameterDescriptor> | string[];
173
+ parameters?: ParameterDescriptor[] | string[];
150
174
  children?: Children;
151
175
  }
152
176
 
@@ -173,7 +197,7 @@ FunctionDeclaration.TypeParameters = taggedComponent(
173
197
  <>
174
198
  {"<"}
175
199
  <group>
176
- <Indent break="soft">
200
+ <Indent softline>
177
201
  <For each={typeParameters} comma line>
178
202
  {(param) => typeParameter(param)}
179
203
  </For>
@@ -186,21 +210,21 @@ FunctionDeclaration.TypeParameters = taggedComponent(
186
210
  },
187
211
  );
188
212
 
189
- function typeParameter(param: DeclaredParameterDescriptor) {
213
+ function typeParameter(param: DeclaredTypeParameterDescriptor) {
190
214
  return (
191
215
  <group>
192
216
  {param.symbol.name}
193
- <Show when={!!param.type}>
217
+ <Show when={!!param.extends}>
194
218
  {" "}
195
219
  extends
196
- <indent> {param.type}</indent>
220
+ <indent> {param.extends}</indent>
197
221
  </Show>
198
222
  </group>
199
223
  );
200
224
  }
201
225
 
202
226
  export interface FunctionParametersProps {
203
- parameters?: Record<string, Children | ParameterDescriptor>;
227
+ parameters?: ParameterDescriptor[] | string[];
204
228
  children?: Children;
205
229
  }
206
230
 
@@ -211,10 +235,10 @@ FunctionDeclaration.Parameters = taggedComponent(
211
235
  return props.children;
212
236
  }
213
237
 
214
- const parameters = normalizeAndDeclareParameters(props.parameters ?? {});
238
+ const parameters = normalizeAndDeclareParameters(props.parameters ?? []);
215
239
  return (
216
240
  <group>
217
- <Indent break="soft">
241
+ <Indent softline trailingBreak>
218
242
  <For each={parameters} comma line>
219
243
  {(param) => parameter(param)}
220
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
+ }
@@ -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>
@@ -0,0 +1,50 @@
1
+ import { MemberDeclarationContext, useContext } from "@alloy-js/core";
2
+ import { TSOutputSymbol, TSSymbolFlags } from "../symbols/ts-output-symbol.js";
3
+ import { isValidJSIdentifier } from "../utils.js";
4
+
5
+ export interface PropertyNameProps {
6
+ /**
7
+ * The name of the property.
8
+ */
9
+ name?: string;
10
+
11
+ /**
12
+ * Whether the property is a private property. If `true`, the property will be
13
+ * prefixed with `#`.
14
+ */
15
+ private?: boolean;
16
+ }
17
+
18
+ /**
19
+ * A TypeScript property name for an interface, class, or object member. If the
20
+ * name is not a valid JavaScript identifier, it will be quoted. If a `name` prop
21
+ * is provided, it will be used as the property name. Otherwise, the name will be
22
+ * taken from the {@link (MemberDeclarationContext:variable)}.
23
+ */
24
+ export function PropertyName(props: PropertyNameProps) {
25
+ if (props.name) {
26
+ if (props.private) {
27
+ return "#" + props.name;
28
+ }
29
+ return quoteIfNeeded(props.name);
30
+ } else {
31
+ const declSymbol = useContext(MemberDeclarationContext) as TSOutputSymbol;
32
+ if (!declSymbol) {
33
+ return "(no member declaration context)";
34
+ }
35
+
36
+ if (declSymbol.tsFlags & TSSymbolFlags.PrivateMember) {
37
+ return <>#{declSymbol.name}</>;
38
+ } else {
39
+ return <>{quoteIfNeeded(declSymbol.name)}</>;
40
+ }
41
+ }
42
+ }
43
+
44
+ function quoteIfNeeded(name: string) {
45
+ if (isValidJSIdentifier(name)) {
46
+ return name;
47
+ } else {
48
+ return `"${name.replace(/"/g, '\\"')}"`;
49
+ }
50
+ }
@@ -0,0 +1,21 @@
1
+ import { Prose } from "@alloy-js/core";
2
+ import { Children } from "@alloy-js/core/jsx-runtime";
3
+
4
+ export interface SingleLineCommentBlockProps {
5
+ children: Children;
6
+ }
7
+
8
+ /**
9
+ * A single line comment block. The children are rendered as a prose element, which means that they
10
+ * are broken into multiple lines
11
+ */
12
+ export function SingleLineCommentBlock(props: SingleLineCommentBlockProps) {
13
+ return (
14
+ <>
15
+ //{" "}
16
+ <align string="// ">
17
+ <Prose>{props.children}</Prose>
18
+ </align>
19
+ </>
20
+ );
21
+ }