@alloy-js/typescript 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/src/builtins/node.js +1 -2
  3. package/dist/src/components/ArrayExpression.js +2 -5
  4. package/dist/src/components/ArrowFunction.d.ts +40 -0
  5. package/dist/src/components/ArrowFunction.d.ts.map +1 -0
  6. package/dist/src/components/ArrowFunction.js +67 -0
  7. package/dist/src/components/BarrelFile.js +1 -2
  8. package/dist/src/components/BlockScope.js +2 -4
  9. package/dist/src/components/CallSignature.js +2 -4
  10. package/dist/src/components/ClassDeclaration.js +2 -6
  11. package/dist/src/components/CommaList.js +2 -5
  12. package/dist/src/components/Declaration.js +3 -5
  13. package/dist/src/components/EnumDeclaration.js +2 -5
  14. package/dist/src/components/EnumMember.js +2 -4
  15. package/dist/src/components/ExportStatement.js +1 -2
  16. package/dist/src/components/FunctionBase.d.ts +60 -0
  17. package/dist/src/components/FunctionBase.d.ts.map +1 -0
  18. package/dist/src/components/FunctionBase.js +207 -0
  19. package/dist/src/components/FunctionCallExpression.js +2 -5
  20. package/dist/src/components/FunctionDeclaration.d.ts +3 -18
  21. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  22. package/dist/src/components/FunctionDeclaration.js +9 -157
  23. package/dist/src/components/FunctionExpression.d.ts +39 -0
  24. package/dist/src/components/FunctionExpression.d.ts.map +1 -0
  25. package/dist/src/components/FunctionExpression.js +66 -0
  26. package/dist/src/components/FunctionType.d.ts +39 -0
  27. package/dist/src/components/FunctionType.d.ts.map +1 -0
  28. package/dist/src/components/FunctionType.js +73 -0
  29. package/dist/src/components/IfStatement.js +2 -4
  30. package/dist/src/components/ImportStatement.js +1 -2
  31. package/dist/src/components/Interface.d.ts.map +1 -1
  32. package/dist/src/components/Interface.js +32 -11
  33. package/dist/src/components/InterfaceMethod.d.ts +43 -0
  34. package/dist/src/components/InterfaceMethod.d.ts.map +1 -0
  35. package/dist/src/components/InterfaceMethod.js +87 -0
  36. package/dist/src/components/JSDoc.js +1 -2
  37. package/dist/src/components/JSDocComment.js +2 -4
  38. package/dist/src/components/JSDocExample.js +2 -5
  39. package/dist/src/components/JSDocParam.js +2 -5
  40. package/dist/src/components/MemberChainExpression.js +2 -4
  41. package/dist/src/components/MemberDeclaration.d.ts +30 -0
  42. package/dist/src/components/MemberDeclaration.d.ts.map +1 -0
  43. package/dist/src/components/MemberDeclaration.js +29 -0
  44. package/dist/src/components/MemberExpression.js +2 -5
  45. package/dist/src/components/ObjectExpression.js +2 -5
  46. package/dist/src/components/PackageDirectory.js +2 -5
  47. package/dist/src/components/PackageJson.js +1 -2
  48. package/dist/src/components/PropertyName.js +1 -2
  49. package/dist/src/components/Reference.js +1 -2
  50. package/dist/src/components/SingleLineCommentBlock.js +2 -4
  51. package/dist/src/components/SourceFile.js +2 -4
  52. package/dist/src/components/SwitchStatement.js +2 -5
  53. package/dist/src/components/TsConfigJson.js +1 -2
  54. package/dist/src/components/TypeDeclaration.js +2 -6
  55. package/dist/src/components/ValueExpression.js +1 -2
  56. package/dist/src/components/VarDeclaration.js +2 -5
  57. package/dist/src/components/index.d.ts +6 -0
  58. package/dist/src/components/index.d.ts.map +1 -1
  59. package/dist/src/components/index.js +6 -1
  60. package/dist/src/components/stc/index.d.ts +7 -0
  61. package/dist/src/components/stc/index.d.ts.map +1 -1
  62. package/dist/src/components/stc/index.js +8 -2
  63. package/dist/src/context/private-scope.js +1 -2
  64. package/dist/src/create-package.js +1 -2
  65. package/dist/src/index.js +1 -2
  66. package/dist/src/name-conflict-resolver.js +1 -2
  67. package/dist/src/name-policy.js +1 -2
  68. package/dist/src/parameter-descriptor.d.ts +27 -17
  69. package/dist/src/parameter-descriptor.d.ts.map +1 -1
  70. package/dist/src/source-directory-data.js +1 -2
  71. package/dist/src/symbols/index.js +1 -2
  72. package/dist/src/symbols/reference.js +2 -3
  73. package/dist/src/symbols/scopes.js +1 -2
  74. package/dist/src/symbols/ts-member-scope.js +1 -2
  75. package/dist/src/symbols/ts-module-scope.js +1 -2
  76. package/dist/src/symbols/ts-output-symbol.js +1 -2
  77. package/dist/src/symbols/ts-package-scope.js +1 -2
  78. package/dist/src/utils.js +1 -2
  79. package/dist/test/array.test.js +105 -0
  80. package/dist/test/arrow-function.test.d.ts +2 -0
  81. package/dist/test/arrow-function.test.d.ts.map +1 -0
  82. package/dist/test/arrow-function.test.js +194 -0
  83. package/dist/test/barrel.test.js +53 -0
  84. package/dist/test/basic.test.js +91 -0
  85. package/dist/test/block-scope.test.js +30 -0
  86. package/dist/test/class.test.js +438 -0
  87. package/dist/test/enum-jsdoc.test.js +88 -0
  88. package/dist/test/enum.test.js +247 -0
  89. package/dist/test/externals.test.js +145 -0
  90. package/dist/test/function-call-expression.test.js +87 -0
  91. package/dist/test/function-declaration-jsdoc.test.js +91 -0
  92. package/dist/test/function-declaration.test.js +228 -0
  93. package/dist/test/function-expression.test.d.ts +2 -0
  94. package/dist/test/function-expression.test.d.ts.map +1 -0
  95. package/dist/test/function-expression.test.js +194 -0
  96. package/dist/test/function-type.test.d.ts +2 -0
  97. package/dist/test/function-type.test.d.ts.map +1 -0
  98. package/dist/test/function-type.test.js +122 -0
  99. package/dist/test/if-statement.test.js +25 -0
  100. package/dist/test/imports.test.js +434 -0
  101. package/dist/test/interface-jsdoc.test.js +178 -0
  102. package/dist/test/interface.test.d.ts.map +1 -1
  103. package/dist/test/interface.test.js +490 -0
  104. package/dist/test/jscomment.test.js +70 -0
  105. package/dist/test/jsdoc.test.js +134 -0
  106. package/dist/test/jsdocparam.test.js +164 -0
  107. package/dist/test/member-chain.test.js +141 -0
  108. package/dist/test/member-expression.test.js +359 -0
  109. package/dist/test/name-conflict.test.js +108 -0
  110. package/dist/test/name-policy.test.js +122 -0
  111. package/dist/test/package-external.test.js +196 -0
  112. package/dist/test/package-json.test.js +93 -0
  113. package/dist/test/package.test.js +136 -0
  114. package/dist/test/reference.test.js +81 -0
  115. package/dist/test/source-file.test.js +36 -0
  116. package/dist/test/switch-statement.test.js +61 -0
  117. package/dist/test/utils.js +45 -0
  118. package/dist/test/value-expression.test.js +30 -0
  119. package/dist/test/values.test.js +351 -0
  120. package/dist/test/var-declaration-jsdoc.test.js +50 -0
  121. package/dist/test/var-declaration.test.js +63 -0
  122. package/dist/test/vitest.d.js +1 -0
  123. package/dist/tsconfig.tsbuildinfo +1 -1
  124. package/package.json +12 -17
  125. package/src/components/ArrowFunction.tsx +87 -0
  126. package/src/components/Declaration.tsx +1 -1
  127. package/src/components/FunctionBase.tsx +271 -0
  128. package/src/components/FunctionDeclaration.tsx +13 -195
  129. package/src/components/FunctionExpression.tsx +86 -0
  130. package/src/components/FunctionType.tsx +88 -0
  131. package/src/components/Interface.tsx +33 -5
  132. package/src/components/InterfaceMethod.tsx +96 -0
  133. package/src/components/MemberDeclaration.tsx +70 -0
  134. package/src/components/index.ts +10 -0
  135. package/src/components/stc/index.ts +7 -0
  136. package/src/parameter-descriptor.ts +29 -18
  137. package/src/symbols/reference.ts +4 -1
  138. package/temp/api.json +2605 -680
  139. package/test/arrow-function.test.tsx +188 -0
  140. package/test/class.test.tsx +1 -2
  141. package/test/function-call-expression.test.tsx +1 -2
  142. package/test/function-declaration.test.tsx +1 -2
  143. package/test/function-expression.test.tsx +195 -0
  144. package/test/function-type.test.tsx +118 -0
  145. package/test/interface.test.tsx +256 -6
  146. package/tsdoc-metadata.json +1 -1
  147. package/babel.config.cjs +0 -4
  148. package/dist/src/builtins/index.js.map +0 -1
  149. package/dist/src/builtins/node.js.map +0 -1
  150. package/dist/src/components/ArrayExpression.js.map +0 -1
  151. package/dist/src/components/BarrelFile.js.map +0 -1
  152. package/dist/src/components/BlockScope.js.map +0 -1
  153. package/dist/src/components/CallSignature.js.map +0 -1
  154. package/dist/src/components/ClassDeclaration.js.map +0 -1
  155. package/dist/src/components/CommaList.js.map +0 -1
  156. package/dist/src/components/Declaration.js.map +0 -1
  157. package/dist/src/components/EnumDeclaration.js.map +0 -1
  158. package/dist/src/components/EnumMember.js.map +0 -1
  159. package/dist/src/components/ExportStatement.js.map +0 -1
  160. package/dist/src/components/FunctionCallExpression.js.map +0 -1
  161. package/dist/src/components/FunctionDeclaration.js.map +0 -1
  162. package/dist/src/components/IfStatement.js.map +0 -1
  163. package/dist/src/components/ImportStatement.js.map +0 -1
  164. package/dist/src/components/Interface.js.map +0 -1
  165. package/dist/src/components/JSDoc.js.map +0 -1
  166. package/dist/src/components/JSDocComment.js.map +0 -1
  167. package/dist/src/components/JSDocExample.js.map +0 -1
  168. package/dist/src/components/JSDocParam.js.map +0 -1
  169. package/dist/src/components/MemberChainExpression.js.map +0 -1
  170. package/dist/src/components/MemberExpression.js.map +0 -1
  171. package/dist/src/components/ObjectExpression.js.map +0 -1
  172. package/dist/src/components/PackageDirectory.js.map +0 -1
  173. package/dist/src/components/PackageJson.js.map +0 -1
  174. package/dist/src/components/PropertyName.js.map +0 -1
  175. package/dist/src/components/Reference.js.map +0 -1
  176. package/dist/src/components/SingleLineCommentBlock.js.map +0 -1
  177. package/dist/src/components/SourceFile.js.map +0 -1
  178. package/dist/src/components/SwitchStatement.js.map +0 -1
  179. package/dist/src/components/TsConfigJson.js.map +0 -1
  180. package/dist/src/components/TypeDeclaration.js.map +0 -1
  181. package/dist/src/components/ValueExpression.js.map +0 -1
  182. package/dist/src/components/VarDeclaration.js.map +0 -1
  183. package/dist/src/components/index.js.map +0 -1
  184. package/dist/src/components/stc/index.js.map +0 -1
  185. package/dist/src/context/index.js.map +0 -1
  186. package/dist/src/context/private-scope.js.map +0 -1
  187. package/dist/src/create-package.js.map +0 -1
  188. package/dist/src/index.js.map +0 -1
  189. package/dist/src/name-conflict-resolver.js.map +0 -1
  190. package/dist/src/name-policy.js.map +0 -1
  191. package/dist/src/parameter-descriptor.js.map +0 -1
  192. package/dist/src/source-directory-data.js.map +0 -1
  193. package/dist/src/symbols/index.js.map +0 -1
  194. package/dist/src/symbols/reference.js.map +0 -1
  195. package/dist/src/symbols/scopes.js.map +0 -1
  196. package/dist/src/symbols/ts-member-scope.js.map +0 -1
  197. package/dist/src/symbols/ts-module-scope.js.map +0 -1
  198. package/dist/src/symbols/ts-output-symbol.js.map +0 -1
  199. package/dist/src/symbols/ts-package-scope.js.map +0 -1
  200. package/dist/src/utils.js.map +0 -1
@@ -0,0 +1,490 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { NamePolicyContext, refkey, StatementList } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { d } from "@alloy-js/core/testing";
5
+ import { describe, expect, it } from "vitest";
6
+ import * as ts from "../src/components/index.js";
7
+ import { Reference } from "../src/components/Reference.js";
8
+ import { createTSNamePolicy } from "../src/name-policy.js";
9
+ import { toSourceText } from "./utils.js";
10
+ it("declares interfaces", () => {
11
+ const res = toSourceText(_$createComponent(ts.InterfaceDeclaration, {
12
+ name: "Foo"
13
+ }));
14
+ expect(res).toEqual(d`
15
+ interface Foo {}
16
+ `);
17
+ });
18
+ it("accepts export and default", () => {
19
+ const res = toSourceText(_$createComponent(ts.InterfaceDeclaration, {
20
+ name: "Foo",
21
+ "export": true,
22
+ "default": true
23
+ }));
24
+ expect(res).toEqual(d`
25
+ export default interface Foo {}
26
+ `);
27
+ });
28
+ it("creates extends", () => {
29
+ const res = toSourceText(_$createComponent(ts.InterfaceDeclaration, {
30
+ name: "Foo",
31
+ "extends": "string"
32
+ }));
33
+ expect(res).toEqual(d`
34
+ interface Foo extends string {}
35
+ `);
36
+ });
37
+ it("can create members", () => {
38
+ const res = toSourceText(_$createComponent(ts.InterfaceDeclaration, {
39
+ name: "Foo",
40
+ get refkey() {
41
+ return refkey("Foo");
42
+ },
43
+ get children() {
44
+ return _$createComponent(StatementList, {
45
+ get children() {
46
+ return [_$createComponent(ts.InterfaceMember, {
47
+ name: "member",
48
+ type: "string"
49
+ }), _$createComponent(ts.InterfaceMember, {
50
+ name: "circular",
51
+ get type() {
52
+ return _$createComponent(Reference, {
53
+ get refkey() {
54
+ return refkey("Foo");
55
+ }
56
+ });
57
+ }
58
+ }), _$createComponent(ts.InterfaceMember, {
59
+ indexer: "str: string",
60
+ type: "number"
61
+ })];
62
+ }
63
+ });
64
+ }
65
+ }));
66
+ expect(res).toEqual(d`
67
+ interface Foo {
68
+ member: string;
69
+ circular: Foo;
70
+ [str: string]: number;
71
+ }
72
+ `);
73
+ });
74
+ it("can create optional members", () => {
75
+ const res = toSourceText(_$createComponent(ts.InterfaceDeclaration, {
76
+ name: "Foo",
77
+ get refkey() {
78
+ return refkey("Foo");
79
+ },
80
+ get children() {
81
+ return _$createComponent(StatementList, {
82
+ get children() {
83
+ return [_$createComponent(ts.InterfaceMember, {
84
+ name: "member",
85
+ type: "string"
86
+ }), _$createComponent(ts.InterfaceMember, {
87
+ optional: true,
88
+ name: "circular",
89
+ get type() {
90
+ return _$createComponent(Reference, {
91
+ get refkey() {
92
+ return refkey("Foo");
93
+ }
94
+ });
95
+ }
96
+ }), _$createComponent(ts.InterfaceMember, {
97
+ indexer: "str: string",
98
+ type: "number"
99
+ })];
100
+ }
101
+ });
102
+ }
103
+ }));
104
+ expect(res).toEqual(d`
105
+ interface Foo {
106
+ member: string;
107
+ circular?: Foo;
108
+ [str: string]: number;
109
+ }
110
+ `);
111
+ });
112
+ it("can create readonly members", () => {
113
+ const res = toSourceText(_$createComponent(ts.InterfaceDeclaration, {
114
+ name: "Foo",
115
+ get refkey() {
116
+ return refkey("Foo");
117
+ },
118
+ get children() {
119
+ return _$createComponent(StatementList, {
120
+ get children() {
121
+ return [_$createComponent(ts.InterfaceMember, {
122
+ readonly: true,
123
+ name: "member",
124
+ type: "string"
125
+ }), _$createComponent(ts.InterfaceMember, {
126
+ name: "circular",
127
+ get type() {
128
+ return _$createComponent(Reference, {
129
+ get refkey() {
130
+ return refkey("Foo");
131
+ }
132
+ });
133
+ }
134
+ }), _$createComponent(ts.InterfaceMember, {
135
+ indexer: "str: string",
136
+ type: "number"
137
+ })];
138
+ }
139
+ });
140
+ }
141
+ }));
142
+ expect(res).toEqual(d`
143
+ interface Foo {
144
+ readonly member: string;
145
+ circular: Foo;
146
+ [str: string]: number;
147
+ }
148
+ `);
149
+ });
150
+ it("can reference member", () => {
151
+ const ref = refkey("Foo");
152
+ const res = toSourceText([_$createComponent(ts.InterfaceDeclaration, {
153
+ name: "Foo",
154
+ get children() {
155
+ return _$createComponent(ts.InterfaceMember, {
156
+ name: "prop",
157
+ type: "string",
158
+ refkey: ref
159
+ });
160
+ }
161
+ }), _$createIntrinsic("line", {}), _$createComponent(ts.InterfaceDeclaration, {
162
+ name: "Bar",
163
+ get children() {
164
+ return _$createComponent(ts.InterfaceMember, {
165
+ name: "ref",
166
+ get type() {
167
+ return _$createComponent(Reference, {
168
+ refkey: ref
169
+ });
170
+ }
171
+ });
172
+ }
173
+ })]);
174
+ expect(res).toEqual(d`
175
+ interface Foo {
176
+ prop: string
177
+ }
178
+ interface Bar {
179
+ ref: Foo["prop"]
180
+ }
181
+ `);
182
+ });
183
+ describe("interface expressions", () => {
184
+ it("basic", () => {
185
+ const res = toSourceText(_$createComponent(ts.InterfaceExpression, {
186
+ get children() {
187
+ return [_$createComponent(ts.InterfaceMember, {
188
+ name: "member",
189
+ type: "string"
190
+ }), ";"];
191
+ }
192
+ }));
193
+ expect(res).toEqual(d`
194
+ {
195
+ member: string;
196
+ }
197
+ `);
198
+ });
199
+ it("nested", () => {
200
+ const res = toSourceText(_$createComponent(ts.InterfaceExpression, {
201
+ get children() {
202
+ return _$createComponent(ts.InterfaceMember, {
203
+ name: "outer",
204
+ get children() {
205
+ return _$createComponent(ts.InterfaceExpression, {
206
+ get children() {
207
+ return [_$createComponent(ts.InterfaceMember, {
208
+ name: "inner",
209
+ type: "string"
210
+ }), ";"];
211
+ }
212
+ });
213
+ }
214
+ });
215
+ }
216
+ }));
217
+ expect(res).toEqual(d`
218
+ {
219
+ outer: {
220
+ inner: string;
221
+ }
222
+ }
223
+ `);
224
+ });
225
+ it("can reference member when nested in a declaration", () => {
226
+ const ref = refkey("Foo");
227
+ const res = toSourceText([_$createComponent(ts.InterfaceDeclaration, {
228
+ name: "Foo",
229
+ get children() {
230
+ return _$createComponent(ts.InterfaceMember, {
231
+ name: "outer",
232
+ get children() {
233
+ return _$createComponent(ts.InterfaceExpression, {
234
+ get children() {
235
+ return _$createComponent(ts.InterfaceMember, {
236
+ name: "inner",
237
+ type: "string",
238
+ refkey: ref
239
+ });
240
+ }
241
+ });
242
+ }
243
+ });
244
+ }
245
+ }), _$createIntrinsic("line", {}), _$createComponent(ts.InterfaceDeclaration, {
246
+ name: "Bar",
247
+ get children() {
248
+ return _$createComponent(ts.InterfaceMember, {
249
+ name: "ref",
250
+ get type() {
251
+ return _$createComponent(Reference, {
252
+ refkey: ref
253
+ });
254
+ }
255
+ });
256
+ }
257
+ })]);
258
+ expect(res).toEqual(d`
259
+ interface Foo {
260
+ outer: {
261
+ inner: string
262
+ }
263
+ }
264
+ interface Bar {
265
+ ref: Foo["outer"]["inner"]
266
+ }
267
+ `);
268
+ });
269
+ });
270
+ it("supports the naming policy", () => {
271
+ const policy = createTSNamePolicy();
272
+ const res = toSourceText(_$createComponent(NamePolicyContext.Provider, {
273
+ value: policy,
274
+ get children() {
275
+ return _$createComponent(ts.InterfaceDeclaration, {
276
+ name: "interface_name",
277
+ get children() {
278
+ return [_$createComponent(ts.InterfaceMember, {
279
+ name: "member_property",
280
+ type: "string"
281
+ }), ";"];
282
+ }
283
+ });
284
+ }
285
+ }));
286
+ expect(res).toEqual(d`
287
+ interface InterfaceName {
288
+ memberProperty: string;
289
+ }
290
+ `);
291
+ });
292
+ it("handles invalid identifier names", () => {
293
+ const res = toSourceText(_$createComponent(ts.InterfaceExpression, {
294
+ get children() {
295
+ return [_$createComponent(ts.InterfaceMember, {
296
+ name: "invalid-name",
297
+ type: "string"
298
+ }), ";"];
299
+ }
300
+ }));
301
+ expect(res).toEqual(d`
302
+ {
303
+ "invalid-name": string;
304
+ }
305
+ `);
306
+ });
307
+ describe("method members", () => {
308
+ it("render basic", () => {
309
+ expect(toSourceText(_$createComponent(ts.InterfaceDeclaration, {
310
+ name: "Foo",
311
+ get children() {
312
+ return _$createComponent(ts.InterfaceMethod, {
313
+ name: "foo"
314
+ });
315
+ }
316
+ }))).toBe(d`
317
+ interface Foo {
318
+ foo(): void
319
+ }
320
+ `);
321
+ });
322
+ it("render in interface", () => {
323
+ expect(toSourceText(_$createComponent(ts.InterfaceDeclaration, {
324
+ name: "Foo",
325
+ get children() {
326
+ return _$createComponent(ts.InterfaceMethod, {
327
+ name: "foo"
328
+ });
329
+ }
330
+ }))).toBe(d`
331
+ interface Foo {
332
+ foo(): void
333
+ }
334
+ `);
335
+ });
336
+ it("can be an async function", () => {
337
+ expect(toSourceText(_$createComponent(ts.InterfaceDeclaration, {
338
+ name: "Foo",
339
+ get children() {
340
+ return _$createComponent(ts.InterfaceMethod, {
341
+ async: true,
342
+ name: "foo"
343
+ });
344
+ }
345
+ }))).toBe(d`
346
+ interface Foo {
347
+ foo(): Promise<void>
348
+ }
349
+ `);
350
+ });
351
+ it("can be an async function with returnType", () => {
352
+ expect(toSourceText(_$createComponent(ts.InterfaceDeclaration, {
353
+ name: "Foo",
354
+ get children() {
355
+ return _$createComponent(ts.InterfaceMethod, {
356
+ async: true,
357
+ name: "foo",
358
+ returnType: "Foo"
359
+ });
360
+ }
361
+ }))).toBe(d`
362
+ interface Foo {
363
+ foo(): Promise<Foo>
364
+ }
365
+ `);
366
+ });
367
+ it("can be an async function with returnType element", () => {
368
+ function Foo(_props) {
369
+ return ["Foo"];
370
+ }
371
+ expect(toSourceText(_$createComponent(ts.InterfaceDeclaration, {
372
+ name: "Foo",
373
+ get children() {
374
+ return _$createComponent(ts.InterfaceMethod, {
375
+ async: true,
376
+ name: "foo",
377
+ get returnType() {
378
+ return _$createComponent(Foo, {});
379
+ }
380
+ });
381
+ }
382
+ }))).toBe(d`
383
+ interface Foo {
384
+ foo(): Promise<Foo>
385
+ }
386
+ `);
387
+ });
388
+ it("supports parameters by element", () => {
389
+ const decl = _$createComponent(ts.InterfaceDeclaration, {
390
+ name: "Foo",
391
+ get children() {
392
+ return _$createComponent(ts.InterfaceMethod, {
393
+ name: "foo",
394
+ get children() {
395
+ return _$createComponent(ts.InterfaceMethod.Parameters, {
396
+ children: "a, b"
397
+ });
398
+ }
399
+ });
400
+ }
401
+ });
402
+ expect(toSourceText(decl)).toBe(d`
403
+ interface Foo {
404
+ foo(a, b): void
405
+ }
406
+ `);
407
+ });
408
+ it("supports type parameters by descriptor object", () => {
409
+ const decl = _$createComponent(ts.InterfaceDeclaration, {
410
+ name: "Foo",
411
+ get children() {
412
+ return _$createComponent(ts.InterfaceMethod, {
413
+ name: "foo",
414
+ typeParameters: [{
415
+ name: "a",
416
+ extends: "any"
417
+ }, {
418
+ name: "b",
419
+ extends: "any"
420
+ }]
421
+ });
422
+ }
423
+ });
424
+ expect(toSourceText(decl)).toBe(d`
425
+ interface Foo {
426
+ foo<a extends any, b extends any>(): void
427
+ }
428
+ `);
429
+ });
430
+ it("supports type parameters by descriptor array", () => {
431
+ const decl = _$createComponent(ts.InterfaceDeclaration, {
432
+ name: "Foo",
433
+ get children() {
434
+ return _$createComponent(ts.InterfaceMethod, {
435
+ name: "foo",
436
+ typeParameters: ["a", "b"]
437
+ });
438
+ }
439
+ });
440
+ expect(toSourceText(decl)).toBe(d`
441
+ interface Foo {
442
+ foo<a, b>(): void
443
+ }
444
+ `);
445
+ });
446
+ it("supports type parameters by element", () => {
447
+ const decl = _$createComponent(ts.InterfaceDeclaration, {
448
+ name: "Foo",
449
+ get children() {
450
+ return _$createComponent(ts.InterfaceMethod, {
451
+ name: "foo",
452
+ get children() {
453
+ return _$createComponent(ts.InterfaceMethod.TypeParameters, {
454
+ children: "a, b"
455
+ });
456
+ }
457
+ });
458
+ }
459
+ });
460
+ expect(toSourceText(decl)).toBe(d`
461
+ interface Foo {
462
+ foo<a, b>(): void
463
+ }
464
+ `);
465
+ });
466
+ describe("symbols", () => {
467
+ it("create optional parameters", () => {
468
+ const paramDesc = {
469
+ name: "foo",
470
+ refkey: refkey(),
471
+ type: "any",
472
+ optional: true
473
+ };
474
+ const decl = _$createComponent(ts.InterfaceDeclaration, {
475
+ name: "Foo",
476
+ get children() {
477
+ return _$createComponent(ts.InterfaceMethod, {
478
+ name: "foo",
479
+ parameters: [paramDesc]
480
+ });
481
+ }
482
+ });
483
+ expect(toSourceText(decl)).toBe(d`
484
+ interface Foo {
485
+ foo(foo?: any): void
486
+ }
487
+ `);
488
+ });
489
+ });
490
+ });
@@ -0,0 +1,70 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { List } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { d } from "@alloy-js/core/testing";
5
+ import { describe, expect, it } from "vitest";
6
+ import { SingleLineCommentBlock } from "../src/components/SingleLineCommentBlock.js";
7
+ describe("JSComment", () => {
8
+ it("formats properly a simple comment", () => {
9
+ const template = [_$createComponent(SingleLineCommentBlock, {
10
+ children: "Hello!"
11
+ }), _$createIntrinsic("hbr", {}), "Should not be part of the comment"];
12
+ expect(template).toRenderTo(d`
13
+ // Hello!
14
+ Should not be part of the comment
15
+ `, {
16
+ printWidth: 40
17
+ });
18
+ });
19
+ it("should not add extra line breaks", () => {
20
+ const template = [_$createComponent(List, {
21
+ hardline: true,
22
+ get children() {
23
+ return _$createComponent(SingleLineCommentBlock, {
24
+ get children() {
25
+ return ["Hello!", _$createIntrinsic("hbr", {}), _$createIntrinsic("hbr", {}), "This is another line"];
26
+ }
27
+ });
28
+ }
29
+ }), _$createIntrinsic("hbr", {}), "Hello"];
30
+ expect(template).toRenderTo(d`
31
+ // Hello!
32
+ //
33
+ // This is another line
34
+ Hello
35
+ `, {
36
+ printWidth: 40
37
+ });
38
+ });
39
+ it("formats properly multiple children", () => {
40
+ const template = [_$createComponent(SingleLineCommentBlock, {
41
+ get children() {
42
+ return ["Hello!", _$createIntrinsic("hbr", {}), _$createIntrinsic("hbr", {}), "This is another line"];
43
+ }
44
+ }), _$createIntrinsic("hbr", {}), "foo should be outside the comment"];
45
+ expect(template).toRenderTo(d`
46
+ // Hello!
47
+ //
48
+ // This is another line
49
+ foo should be outside the comment
50
+ `, {
51
+ printWidth: 40
52
+ });
53
+ });
54
+ it("It correctly do word wrapping", () => {
55
+ const template = [_$createComponent(SingleLineCommentBlock, {
56
+ children: "This is a very long line that should be broken into multiple lines. It should also be aligned properly. The line breaks in this paragraph should not be carried over into the JSDoc comment."
57
+ }), _$createIntrinsic("hbr", {}), ["This should not be part of the comment"]];
58
+ expect(template).toRenderTo(d`
59
+ // This is a very long line that should
60
+ // be broken into multiple lines. It
61
+ // should also be aligned properly. The
62
+ // line breaks in this paragraph should
63
+ // not be carried over into the JSDoc
64
+ // comment.
65
+ This should not be part of the comment
66
+ `, {
67
+ printWidth: 40
68
+ });
69
+ });
70
+ });
@@ -0,0 +1,134 @@
1
+ import { createIntrinsic as _$createIntrinsic, createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { Prose } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { d } from "@alloy-js/core/testing";
5
+ import { describe, expect, it } from "vitest";
6
+ import { JSDoc } from "../src/components/JSDoc.js";
7
+ import { JSDocComment } from "../src/components/JSDocComment.js";
8
+ import { JSDocExample } from "../src/components/JSDocExample.js";
9
+ import { FunctionCallExpression } from "../src/index.js";
10
+ describe("JSDoc", () => {
11
+ it("formats properly", () => {
12
+ const template = _$createComponent(JSDocComment, {
13
+ get children() {
14
+ return ["Hello!", _$createIntrinsic("hbr", {}), _$createIntrinsic("hbr", {}), "This is another line"];
15
+ }
16
+ });
17
+ expect(template).toRenderTo(d`
18
+ /**
19
+ * Hello!
20
+ *
21
+ * This is another line
22
+ **/
23
+ `, {
24
+ printWidth: 40
25
+ });
26
+ });
27
+ });
28
+ describe("Prose", () => {
29
+ it("formats properly", () => {
30
+ const template = _$createComponent(JSDoc, {
31
+ get children() {
32
+ return [_$createComponent(Prose, {
33
+ children: "This is a long paragraph that should end up being broken into multiple paragraphs. Moreover, the line breaks in this paragraph should not be carried over into the JSDoc comment."
34
+ }), _$createComponent(Prose, {
35
+ children: "This is another paragraph. There should be a couple breaks in front of it."
36
+ })];
37
+ }
38
+ });
39
+ expect(template).toRenderTo(d`
40
+ /**
41
+ * This is a long paragraph that should
42
+ * end up being broken into multiple
43
+ * paragraphs. Moreover, the line breaks
44
+ * in this paragraph should not be
45
+ * carried over into the JSDoc comment.
46
+ *
47
+ * This is another paragraph. There
48
+ * should be a couple breaks in front of
49
+ * it.
50
+ **/
51
+ `, {
52
+ printWidth: 40
53
+ });
54
+ });
55
+ });
56
+ describe("JSDocExample", () => {
57
+ it("creates unfenced code samples", () => {
58
+ const template = _$createComponent(JSDoc, {
59
+ get children() {
60
+ return [_$createComponent(Prose, {
61
+ children: "This API is awesome, and it comes with an example!"
62
+ }), _$createComponent(JSDocExample, {
63
+ children: "console.log(\"Hello world!\")"
64
+ })];
65
+ }
66
+ });
67
+ expect(template).toRenderTo(d`
68
+ /**
69
+ * This API is awesome, and it comes
70
+ * with an example!
71
+ *
72
+ * @example
73
+ * console.log("Hello world!")
74
+ **/
75
+ `, {
76
+ printWidth: 40
77
+ });
78
+ });
79
+ it("creates fenced code samples", () => {
80
+ const template = _$createComponent(JSDoc, {
81
+ get children() {
82
+ return [_$createComponent(Prose, {
83
+ children: "This API is awesome, and it comes with an example!"
84
+ }), _$createComponent(JSDocExample, {
85
+ fenced: true,
86
+ language: "ts",
87
+ children: "console.log(\"Hello world!\")"
88
+ })];
89
+ }
90
+ });
91
+ expect(template).toRenderTo(d`
92
+ /**
93
+ * This API is awesome, and it comes
94
+ * with an example!
95
+ *
96
+ * @example
97
+ * \`\`\`ts
98
+ * console.log("Hello world!")
99
+ * \`\`\`
100
+ **/
101
+ `, {
102
+ printWidth: 40
103
+ });
104
+ });
105
+ it("creates code samples using other components", () => {
106
+ const template = _$createComponent(JSDoc, {
107
+ get children() {
108
+ return [_$createComponent(Prose, {
109
+ children: "This API is awesome, and it comes with an example!"
110
+ }), _$createComponent(JSDocExample, {
111
+ language: "ts",
112
+ get children() {
113
+ return ["// make a function call.", _$createIntrinsic("hbr", {}), _$createComponent(FunctionCallExpression, {
114
+ target: "console.log",
115
+ args: ["arg1", "arg2"]
116
+ }), ";"];
117
+ }
118
+ })];
119
+ }
120
+ });
121
+ expect(template).toRenderTo(d`
122
+ /**
123
+ * This API is awesome, and it comes
124
+ * with an example!
125
+ *
126
+ * @example
127
+ * // make a function call.
128
+ * console.log(arg1, arg2);
129
+ **/
130
+ `, {
131
+ printWidth: 40
132
+ });
133
+ });
134
+ });