@alloy-js/typescript 0.7.0 → 0.9.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 (211) hide show
  1. package/CHANGELOG.md +35 -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/MemberExpression.d.ts +62 -0
  50. package/dist/src/components/MemberExpression.d.ts.map +1 -0
  51. package/dist/src/components/MemberExpression.js +179 -0
  52. package/dist/src/components/MemberExpression.js.map +1 -0
  53. package/dist/src/components/ObjectExpression.d.ts +1 -1
  54. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  55. package/dist/src/components/ObjectExpression.js +9 -2
  56. package/dist/src/components/ObjectExpression.js.map +1 -1
  57. package/dist/src/components/PropertyName.d.ts +19 -0
  58. package/dist/src/components/PropertyName.d.ts.map +1 -0
  59. package/dist/src/components/PropertyName.js +36 -0
  60. package/dist/src/components/PropertyName.js.map +1 -0
  61. package/dist/src/components/SingleLineCommentBlock.d.ts +10 -0
  62. package/dist/src/components/SingleLineCommentBlock.d.ts.map +1 -0
  63. package/dist/src/components/SingleLineCommentBlock.js +20 -0
  64. package/dist/src/components/SingleLineCommentBlock.js.map +1 -0
  65. package/dist/src/components/SourceFile.d.ts +6 -0
  66. package/dist/src/components/SourceFile.d.ts.map +1 -1
  67. package/dist/src/components/SourceFile.js +31 -0
  68. package/dist/src/components/SourceFile.js.map +1 -1
  69. package/dist/src/components/SwitchStatement.d.ts +31 -0
  70. package/dist/src/components/SwitchStatement.d.ts.map +1 -0
  71. package/dist/src/components/SwitchStatement.js +48 -0
  72. package/dist/src/components/SwitchStatement.js.map +1 -0
  73. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  74. package/dist/src/components/TypeDeclaration.js +16 -3
  75. package/dist/src/components/TypeDeclaration.js.map +1 -1
  76. package/dist/src/components/VarDeclaration.d.ts +1 -1
  77. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  78. package/dist/src/components/VarDeclaration.js +19 -6
  79. package/dist/src/components/VarDeclaration.js.map +1 -1
  80. package/dist/src/components/index.d.ts +7 -0
  81. package/dist/src/components/index.d.ts.map +1 -1
  82. package/dist/src/components/index.js +7 -0
  83. package/dist/src/components/index.js.map +1 -1
  84. package/dist/src/components/stc/index.d.ts +35 -112
  85. package/dist/src/components/stc/index.d.ts.map +1 -1
  86. package/dist/src/components/stc/index.js +8 -1
  87. package/dist/src/components/stc/index.js.map +1 -1
  88. package/dist/src/context/index.d.ts +2 -0
  89. package/dist/src/context/index.d.ts.map +1 -0
  90. package/dist/src/context/index.js +1 -0
  91. package/dist/src/context/index.js.map +1 -0
  92. package/dist/src/context/private-scope.d.ts +18 -0
  93. package/dist/src/context/private-scope.d.ts.map +1 -0
  94. package/dist/src/context/private-scope.js +9 -0
  95. package/dist/src/context/private-scope.js.map +1 -0
  96. package/dist/src/index.d.ts +1 -0
  97. package/dist/src/index.d.ts.map +1 -1
  98. package/dist/src/index.js +1 -0
  99. package/dist/src/index.js.map +1 -1
  100. package/dist/src/parameter-descriptor.d.ts +60 -0
  101. package/dist/src/parameter-descriptor.d.ts.map +1 -0
  102. package/dist/src/parameter-descriptor.js +1 -0
  103. package/dist/src/parameter-descriptor.js.map +1 -0
  104. package/dist/src/symbols/reference.d.ts.map +1 -1
  105. package/dist/src/symbols/reference.js +50 -12
  106. package/dist/src/symbols/reference.js.map +1 -1
  107. package/dist/src/symbols/ts-member-scope.d.ts +1 -1
  108. package/dist/src/symbols/ts-member-scope.d.ts.map +1 -1
  109. package/dist/src/symbols/ts-member-scope.js +2 -0
  110. package/dist/src/symbols/ts-member-scope.js.map +1 -1
  111. package/dist/src/symbols/ts-output-symbol.d.ts +7 -4
  112. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  113. package/dist/src/symbols/ts-output-symbol.js +11 -2
  114. package/dist/src/symbols/ts-output-symbol.js.map +1 -1
  115. package/dist/src/utils.d.ts +7 -0
  116. package/dist/src/utils.d.ts.map +1 -1
  117. package/dist/src/utils.js +17 -0
  118. package/dist/src/utils.js.map +1 -1
  119. package/dist/test/block-scope.test.d.ts +2 -0
  120. package/dist/test/block-scope.test.d.ts.map +1 -0
  121. package/dist/test/enum-jsdoc.test.d.ts +2 -0
  122. package/dist/test/enum-jsdoc.test.d.ts.map +1 -0
  123. package/dist/test/function-declaration-jsdoc.test.d.ts +2 -0
  124. package/dist/test/function-declaration-jsdoc.test.d.ts.map +1 -0
  125. package/dist/test/if-statement.test.d.ts +2 -0
  126. package/dist/test/if-statement.test.d.ts.map +1 -0
  127. package/dist/test/interface-jsdoc.test.d.ts +2 -0
  128. package/dist/test/interface-jsdoc.test.d.ts.map +1 -0
  129. package/dist/test/jscomment.test.d.ts +2 -0
  130. package/dist/test/jscomment.test.d.ts.map +1 -0
  131. package/dist/test/jsdoc.test.d.ts.map +1 -1
  132. package/dist/test/jsdocparam.test.d.ts +2 -0
  133. package/dist/test/jsdocparam.test.d.ts.map +1 -0
  134. package/dist/test/member-expression.test.d.ts +2 -0
  135. package/dist/test/member-expression.test.d.ts.map +1 -0
  136. package/dist/test/name-conflict.test.d.ts +2 -0
  137. package/dist/test/name-conflict.test.d.ts.map +1 -0
  138. package/dist/test/package-json.test.d.ts +2 -0
  139. package/dist/test/package-json.test.d.ts.map +1 -0
  140. package/dist/test/source-file.test.d.ts +2 -0
  141. package/dist/test/source-file.test.d.ts.map +1 -0
  142. package/dist/test/switch-statement.test.d.ts +2 -0
  143. package/dist/test/switch-statement.test.d.ts.map +1 -0
  144. package/dist/test/var-declaration-jsdoc.test.d.ts +2 -0
  145. package/dist/test/var-declaration-jsdoc.test.d.ts.map +1 -0
  146. package/dist/tsconfig.tsbuildinfo +1 -1
  147. package/package.json +3 -3
  148. package/src/components/ArrayExpression.tsx +13 -12
  149. package/src/components/BlockScope.tsx +31 -0
  150. package/src/components/CallSignature.tsx +67 -0
  151. package/src/components/ClassDeclaration.tsx +103 -50
  152. package/src/components/Declaration.tsx +72 -27
  153. package/src/components/EnumDeclaration.tsx +26 -18
  154. package/src/components/EnumMember.tsx +24 -4
  155. package/src/components/FunctionCallExpression.tsx +1 -1
  156. package/src/components/FunctionDeclaration.tsx +106 -82
  157. package/src/components/IfStatement.tsx +51 -0
  158. package/src/components/Interface.tsx +61 -14
  159. package/src/components/JSDoc.tsx +1 -10
  160. package/src/components/JSDocParam.tsx +115 -0
  161. package/src/components/MemberExpression.tsx +237 -0
  162. package/src/components/ObjectExpression.tsx +8 -3
  163. package/src/components/PropertyName.tsx +50 -0
  164. package/src/components/SingleLineCommentBlock.tsx +21 -0
  165. package/src/components/SourceFile.tsx +29 -0
  166. package/src/components/SwitchStatement.tsx +78 -0
  167. package/src/components/TypeDeclaration.tsx +11 -4
  168. package/src/components/VarDeclaration.tsx +19 -11
  169. package/src/components/index.ts +7 -0
  170. package/src/components/stc/index.ts +8 -1
  171. package/src/context/index.ts +1 -0
  172. package/src/context/private-scope.ts +23 -0
  173. package/src/index.ts +1 -0
  174. package/src/parameter-descriptor.ts +71 -0
  175. package/src/symbols/reference.ts +70 -18
  176. package/src/symbols/ts-member-scope.ts +6 -2
  177. package/src/symbols/ts-output-symbol.ts +24 -11
  178. package/src/utils.ts +33 -0
  179. package/temp/api.json +3819 -1915
  180. package/test/array.test.tsx +60 -0
  181. package/test/basic.test.tsx +2 -2
  182. package/test/block-scope.test.tsx +29 -0
  183. package/test/class.test.tsx +173 -5
  184. package/test/enum-jsdoc.test.tsx +74 -0
  185. package/test/enum.test.tsx +50 -3
  186. package/test/function-call-expression.test.tsx +5 -2
  187. package/test/function-declaration-jsdoc.test.tsx +89 -0
  188. package/test/function-declaration.test.tsx +13 -9
  189. package/test/if-statement.test.tsx +28 -0
  190. package/test/interface-jsdoc.test.tsx +164 -0
  191. package/test/interface.test.tsx +14 -0
  192. package/test/jscomment.test.tsx +104 -0
  193. package/test/jsdoc.test.tsx +9 -15
  194. package/test/jsdocparam.test.tsx +188 -0
  195. package/test/member-expression.test.tsx +297 -0
  196. package/test/name-conflict.test.tsx +105 -0
  197. package/test/name-policy.test.tsx +1 -1
  198. package/test/package-json.test.tsx +93 -0
  199. package/test/reference.test.tsx +6 -2
  200. package/test/source-file.test.tsx +38 -0
  201. package/test/switch-statement.test.tsx +50 -0
  202. package/test/value-expression.test.tsx +9 -0
  203. package/test/var-declaration-jsdoc.test.tsx +70 -0
  204. package/test/var-declaration.test.tsx +1 -1
  205. package/tsconfig.json +1 -8
  206. package/vitest.config.ts +2 -10
  207. package/dist/src/components/JSDocParagraph.d.ts +0 -10
  208. package/dist/src/components/JSDocParagraph.d.ts.map +0 -1
  209. package/dist/src/components/JSDocParagraph.js +0 -23
  210. package/dist/src/components/JSDocParagraph.js.map +0 -1
  211. package/src/components/JSDocParagraph.tsx +0 -35
@@ -0,0 +1,297 @@
1
+ import { refkey, StatementList } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { describe, expect, it } from "vitest";
4
+ import { MemberExpression } from "../src/components/MemberExpression.jsx";
5
+ import { ObjectExpression } from "../src/components/stc/index.js";
6
+ import { VarDeclaration } from "../src/components/VarDeclaration.jsx";
7
+ import { toSourceText } from "./utils.js";
8
+
9
+ it("renders basic member expression with dot notation", () => {
10
+ expect(
11
+ toSourceText(
12
+ <MemberExpression>
13
+ <MemberExpression.Part id="obj" />
14
+ <MemberExpression.Part id="property" />
15
+ </MemberExpression>,
16
+ ),
17
+ ).toBe(d`
18
+ obj.property
19
+ `);
20
+ });
21
+
22
+ it("renders member expression with bracket notation for invalid identifiers", () => {
23
+ expect(
24
+ toSourceText(
25
+ <MemberExpression>
26
+ <MemberExpression.Part id="obj" />
27
+ <MemberExpression.Part id="property-name" />
28
+ </MemberExpression>,
29
+ ),
30
+ ).toBe(d`
31
+ obj["property-name"]
32
+ `);
33
+ });
34
+
35
+ it("handles nullish chaining", () => {
36
+ expect(
37
+ toSourceText(
38
+ <MemberExpression>
39
+ <MemberExpression.Part id="obj" nullish={true} />
40
+ <MemberExpression.Part id="property" />
41
+ </MemberExpression>,
42
+ ),
43
+ ).toBe(d`
44
+ obj?.property
45
+ `);
46
+ });
47
+
48
+ it("supports multiple levels of nesting", () => {
49
+ expect(
50
+ toSourceText(
51
+ <MemberExpression>
52
+ <MemberExpression.Part id="a" />
53
+ <MemberExpression.Part id="b" />
54
+ <MemberExpression.Part id="c" />
55
+ <MemberExpression.Part id="d" />
56
+ </MemberExpression>,
57
+ ),
58
+ ).toBe(d`
59
+ a.b.c.d
60
+ `);
61
+ });
62
+
63
+ it("ignores non-part children", () => {
64
+ expect(
65
+ toSourceText(
66
+ <MemberExpression>
67
+ <MemberExpression.Part id="obj" />
68
+ <MemberExpression.Part id="property" />
69
+ </MemberExpression>,
70
+ ),
71
+ ).toBe(d`
72
+ obj.property
73
+ `);
74
+ });
75
+
76
+ it("flattens nested member expressions", () => {
77
+ expect(
78
+ toSourceText(
79
+ <MemberExpression>
80
+ <MemberExpression.Part id="outer" />
81
+ <MemberExpression>
82
+ <MemberExpression.Part id="inner" />
83
+ <MemberExpression.Part id="prop" />
84
+ </MemberExpression>
85
+ <MemberExpression.Part id="last" />
86
+ </MemberExpression>,
87
+ ),
88
+ ).toBe(d`
89
+ outer.inner.prop.last
90
+ `);
91
+ });
92
+
93
+ it("handles a mix of dot and bracket notation", () => {
94
+ expect(
95
+ toSourceText(
96
+ <MemberExpression>
97
+ <MemberExpression.Part id="obj" />
98
+ <MemberExpression.Part id="normalProp" />
99
+ <MemberExpression.Part id="special-prop" />
100
+ <MemberExpression.Part id="123" />
101
+ </MemberExpression>,
102
+ ),
103
+ ).toBe(d`
104
+ obj.normalProp["special-prop"]["123"]
105
+ `);
106
+ });
107
+
108
+ it("handles nullish chaining at multiple levels", () => {
109
+ expect(
110
+ toSourceText(
111
+ <MemberExpression>
112
+ <MemberExpression.Part id="a" />
113
+ <MemberExpression.Part id="b" nullish={true} />
114
+ <MemberExpression.Part id="c" />
115
+ <MemberExpression.Part id="d" nullish={true} />
116
+ <MemberExpression.Part id="e" />
117
+ </MemberExpression>,
118
+ ),
119
+ ).toBe(d`
120
+ a.b?.c.d?.e
121
+ `);
122
+ });
123
+
124
+ describe("with refkeys", () => {
125
+ it("handles symbols correctly", () => {
126
+ const rk1 = refkey();
127
+ const rk2 = refkey();
128
+ expect(
129
+ toSourceText(
130
+ <StatementList>
131
+ <VarDeclaration name="test1" refkey={rk1} initializer={1} />
132
+ <VarDeclaration name="test1" refkey={rk2} initializer={2} />
133
+ <MemberExpression>
134
+ <MemberExpression.Part refkey={rk1} />
135
+ <MemberExpression.Part refkey={rk2} />
136
+ </MemberExpression>
137
+ </StatementList>,
138
+ ),
139
+ ).toBe(d`
140
+ const test1 = 1;
141
+ const test1_2 = 2;
142
+ test1.test1_2;
143
+ `);
144
+ });
145
+
146
+ it("handles late resolved refkeys correctly", () => {
147
+ const rk1 = refkey();
148
+ const rk2 = refkey();
149
+ expect(
150
+ toSourceText(
151
+ <StatementList>
152
+ <MemberExpression>
153
+ <MemberExpression.Part refkey={rk1} />
154
+ <MemberExpression.Part refkey={rk2} />
155
+ </MemberExpression>
156
+ <VarDeclaration name="test1" refkey={rk1} initializer={1} />
157
+ <VarDeclaration name="test1" refkey={rk2} initializer={2} />
158
+ </StatementList>,
159
+ ),
160
+ ).toBe(d`
161
+ test1.test1_2;
162
+ const test1 = 1;
163
+ const test1_2 = 2;
164
+ `);
165
+ });
166
+ });
167
+
168
+ describe("with function calls", () => {
169
+ it("handles simple function calls correctly", () => {
170
+ expect(
171
+ toSourceText(
172
+ <MemberExpression>
173
+ <MemberExpression.Part id="myFunction" />
174
+ <MemberExpression.Part args={[1, 2]} />
175
+ </MemberExpression>,
176
+ ),
177
+ ).toBe(d`
178
+ myFunction(1, 2)
179
+ `);
180
+ });
181
+
182
+ it("handles nullish function calls correctly", () => {
183
+ expect(
184
+ toSourceText(
185
+ <MemberExpression>
186
+ <MemberExpression.Part id="method1" nullish />
187
+ <MemberExpression.Part nullish args={[1, 2]} />
188
+ <MemberExpression.Part nullish args={[]} />
189
+ <MemberExpression.Part id="method2" nullish />
190
+ <MemberExpression.Part args={[]} />
191
+ <MemberExpression.Part id="prop" />
192
+ </MemberExpression>,
193
+ ),
194
+ ).toBe(d`
195
+ method1?.(1, 2)?.()?.method2?.().prop
196
+ `);
197
+ });
198
+
199
+ it("handles function calls returning nullish correctly", () => {
200
+ expect(
201
+ toSourceText(
202
+ <MemberExpression>
203
+ <MemberExpression.Part id="myFunction" />
204
+ <MemberExpression.Part args={[1, 2]} nullish />
205
+ <MemberExpression.Part id="prop" />
206
+ </MemberExpression>,
207
+ ),
208
+ ).toBe(d`
209
+ myFunction(1, 2)?.prop
210
+ `);
211
+ });
212
+ });
213
+
214
+ describe("formatting", () => {
215
+ describe("simple chains", () => {
216
+ it("just dots", () => {
217
+ expect(
218
+ toSourceText(
219
+ <MemberExpression>
220
+ <MemberExpression.Part id="four" />
221
+ <MemberExpression.Part id="four" />
222
+ <MemberExpression.Part id="four" />
223
+ <MemberExpression.Part id="four" />
224
+ <MemberExpression.Part id="four" />
225
+ <MemberExpression.Part id="four" />
226
+ </MemberExpression>,
227
+ { printWidth: 12 },
228
+ ),
229
+ ).toBe(d`
230
+ four.four
231
+ .four.four
232
+ .four.four
233
+ `);
234
+ });
235
+
236
+ it("bracket breaks", () => {
237
+ expect(
238
+ toSourceText(
239
+ <MemberExpression>
240
+ <MemberExpression.Part id="obj" />
241
+ <MemberExpression.Part id="property-name" />
242
+ <MemberExpression.Part id="prop" />
243
+ </MemberExpression>,
244
+ { printWidth: 12 },
245
+ ),
246
+ ).toBe(d`
247
+ obj[
248
+ "property-name"
249
+ ].prop
250
+ `);
251
+ });
252
+ });
253
+
254
+ describe("call chains", () => {
255
+ it("handles single calls", () => {
256
+ expect(
257
+ toSourceText(
258
+ <MemberExpression>
259
+ <MemberExpression.Part id="z" />
260
+ <MemberExpression.Part id="object" />
261
+ <MemberExpression.Part
262
+ args={[<ObjectExpression jsValue={{ x: 1 }} />]}
263
+ />
264
+ </MemberExpression>,
265
+ { printWidth: 12 },
266
+ ),
267
+ ).toBe(d`
268
+ z.object({
269
+ x: 1,
270
+ })
271
+ `);
272
+ });
273
+
274
+ it.skip("handles multiple calls", () => {
275
+ expect(
276
+ toSourceText(
277
+ <MemberExpression>
278
+ <MemberExpression.Part id="z" />
279
+ <MemberExpression.Part id="object" />
280
+ <MemberExpression.Part
281
+ args={[<ObjectExpression jsValue={{ x: 1 }} />]}
282
+ />
283
+ <MemberExpression.Part id="partial" />
284
+ <MemberExpression.Part args={[]} />
285
+ </MemberExpression>,
286
+ { printWidth: 12 },
287
+ ),
288
+ ).toBe(d`
289
+ z
290
+ .object({
291
+ x: 1,
292
+ })
293
+ .partial()
294
+ `);
295
+ });
296
+ });
297
+ });
@@ -0,0 +1,105 @@
1
+ import { Output, render } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { camelCase } from "change-case";
4
+ import { expect, it } from "vitest";
5
+ import {
6
+ FunctionDeclaration,
7
+ SourceFile,
8
+ TSOutputSymbol,
9
+ TSSymbolFlags,
10
+ } from "../src/index.js";
11
+ import { ParameterDescriptor } from "../src/parameter-descriptor.js";
12
+ it("handles custom name conflict resolver based on metadata", () => {
13
+ function resolver(name: string, symbols: TSOutputSymbol[]) {
14
+ const goodNamedSymbols = symbols.filter(
15
+ (s) => ~s.tsFlags & TSSymbolFlags.LocalImportSymbol,
16
+ );
17
+ const badNamedSymbols = symbols.filter(
18
+ (s) => s.tsFlags & TSSymbolFlags.LocalImportSymbol,
19
+ );
20
+ let nameCount = 1;
21
+
22
+ // always rename local import symbols (we don't care about them)
23
+ for (const sym of badNamedSymbols) {
24
+ sym.name = name + "_" + nameCount++;
25
+ }
26
+
27
+ // otherwise check the symbol metadata to see how to rename.
28
+
29
+ // group by symbol metadata
30
+ const groupedSymbols = new Map<string | undefined, TSOutputSymbol[]>([
31
+ ["body", []],
32
+ ["query", []],
33
+ ["header", []],
34
+ [undefined, []],
35
+ ]);
36
+ for (const sym of goodNamedSymbols) {
37
+ const sourceLocation = sym.metadata.sourceLocation as
38
+ | "body"
39
+ | "query"
40
+ | "header"
41
+ | undefined;
42
+ groupedSymbols.get(sourceLocation)!.push(sym);
43
+ }
44
+
45
+ for (const [sourceLocation, symbols] of groupedSymbols) {
46
+ let locationCount = 0;
47
+ if (sourceLocation === undefined) {
48
+ for (const sym of symbols) {
49
+ if (locationCount > 0) {
50
+ sym.name = name + locationCount;
51
+ }
52
+
53
+ locationCount++;
54
+ }
55
+ } else if (symbols.length < 0) {
56
+ continue;
57
+ } else if (symbols.length === 1) {
58
+ symbols[0].name = camelCase(name + "_" + sourceLocation);
59
+ } else {
60
+ for (const sym of symbols) {
61
+ sym.name = camelCase(
62
+ name + "_" + sourceLocation + locationCount++,
63
+ {},
64
+ );
65
+ }
66
+ }
67
+ }
68
+ }
69
+
70
+ const testParameters: ParameterDescriptor[] = [
71
+ { name: "foo" },
72
+ { name: "foo" },
73
+ { name: "foo", metadata: { sourceLocation: "body" } },
74
+ { name: "foo", metadata: { sourceLocation: "query" } },
75
+ { name: "foo", metadata: { sourceLocation: "query" } },
76
+ { name: "foo", metadata: { sourceLocation: "header" } },
77
+ { name: "foo", metadata: { sourceLocation: "header" } },
78
+ { name: "foo", metadata: { sourceLocation: "header" } },
79
+ ];
80
+
81
+ const res = render(
82
+ <Output nameConflictResolver={resolver}>
83
+ <SourceFile path="test.ts">
84
+ <FunctionDeclaration
85
+ export
86
+ name="conflicty"
87
+ parameters={testParameters}
88
+ />
89
+ </SourceFile>
90
+ </Output>,
91
+ );
92
+
93
+ expect(res.contents[0].contents).toBe(d`
94
+ export function conflicty(
95
+ foo,
96
+ foo1,
97
+ fooBody,
98
+ fooQuery0,
99
+ fooQuery1,
100
+ fooHeader0,
101
+ fooHeader1,
102
+ fooHeader2,
103
+ ) {}
104
+ `);
105
+ });
@@ -74,7 +74,7 @@ it("appends _ to reserved words", () => {
74
74
  <ts.FunctionDeclaration
75
75
  name="default"
76
76
  refkey={ref1}
77
- parameters={{ await: "any" }}
77
+ parameters={[{ name: "await", type: "any" }]}
78
78
  />
79
79
  <hbr />
80
80
  <StatementList>
@@ -0,0 +1,93 @@
1
+ import { Output, render } from "@alloy-js/core";
2
+ import "@alloy-js/core/testing";
3
+ import { it } from "vitest";
4
+ import { PackageJsonFile } from "../src/components/PackageJson.jsx";
5
+ import { assertFileContents } from "./utils.js";
6
+
7
+ it("creates a simple package.json", () => {
8
+ const res = render(
9
+ <Output>
10
+ <PackageJsonFile name="test" version="1.0.0" />
11
+ </Output>,
12
+ );
13
+
14
+ assertFileContents(res, {
15
+ "package.json": `
16
+ {
17
+ "name": "test",
18
+ "version": "1.0.0",
19
+ "type": "module"
20
+ }
21
+ `,
22
+ });
23
+ });
24
+
25
+ it("makes a complex package.json", () => {
26
+ const res = render(
27
+ <Output>
28
+ <PackageJsonFile
29
+ name="test"
30
+ version="1.0.0"
31
+ author="Brian Terlson"
32
+ license="MIT"
33
+ description="A test package!"
34
+ homepage="https://example.com"
35
+ repository="https://github.com/alloy-project/alloy"
36
+ keywords={["test", "package"]}
37
+ type="commonjs"
38
+ scripts={{
39
+ build: "tsc -p .",
40
+ test: "vitest run",
41
+ "test:watch": "vitest -w",
42
+ }}
43
+ dependencies={{
44
+ "@alloy-js/core": "^0.1.0",
45
+ "@alloy-js/typescript": "^0.1.0",
46
+ }}
47
+ devDependencies={{
48
+ typescript: "^5.5.2",
49
+ }}
50
+ exports={{
51
+ ".": "./lib/index.js",
52
+ "./complex": {
53
+ main: "./lib/index.js",
54
+ export: "./lib/index.mjs",
55
+ },
56
+ }}
57
+ />
58
+ </Output>,
59
+ );
60
+
61
+ assertFileContents(res, {
62
+ "package.json": `
63
+ {
64
+ "name": "test",
65
+ "version": "1.0.0",
66
+ "author": "Brian Terlson",
67
+ "description": "A test package!",
68
+ "license": "MIT",
69
+ "homepage": "https://example.com",
70
+ "type": "commonjs",
71
+ "dependencies": {
72
+ "@alloy-js/core": "^0.1.0",
73
+ "@alloy-js/typescript": "^0.1.0"
74
+ },
75
+ "devDependencies": {
76
+ "typescript": "^5.5.2"
77
+ },
78
+ "scripts": {
79
+ "build": "tsc -p .",
80
+ "test": "vitest run",
81
+ "test:watch": "vitest -w"
82
+ },
83
+ "exports": {
84
+ ".": "./lib/index.js",
85
+ "./complex": {
86
+ "main": "./lib/index.js",
87
+ "export": "./lib/index.mjs"
88
+ }
89
+ }
90
+ }
91
+ `,
92
+ });
93
+ });
@@ -9,7 +9,9 @@ it("works with back references", () => {
9
9
  const res = render(
10
10
  <Output>
11
11
  <ts.SourceFile path="test1.ts">
12
- <Declaration name="foo">const foo = 1;</Declaration>
12
+ <Declaration name="foo" refkey={refkey("foo")}>
13
+ const foo = 1;
14
+ </Declaration>
13
15
  </ts.SourceFile>
14
16
 
15
17
  <ts.SourceFile path="test2.ts">
@@ -37,7 +39,9 @@ it("works with forward references", () => {
37
39
  const v = <Reference refkey={refkey("foo")} />;
38
40
  </ts.SourceFile>
39
41
  <ts.SourceFile path="test1.ts">
40
- <Declaration name="foo">const foo = 1;</Declaration>
42
+ <Declaration name="foo" refkey={refkey("foo")}>
43
+ const foo = 1;
44
+ </Declaration>
41
45
  </ts.SourceFile>
42
46
  </Output>,
43
47
  );
@@ -0,0 +1,38 @@
1
+ import { Output, render } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import { SourceFile } from "../src/components/SourceFile.jsx";
5
+
6
+ it("Includes header", () => {
7
+ const tree = render(
8
+ <Output>
9
+ <SourceFile path="hi.ts" headerComment="This is a header">
10
+ hello!
11
+ </SourceFile>
12
+ </Output>,
13
+ );
14
+
15
+ expect(tree.contents[0].contents).toEqual(d`
16
+ // This is a header
17
+
18
+ hello!`);
19
+ });
20
+
21
+ it("Line wraps a header", () => {
22
+ const tree = render(
23
+ <Output>
24
+ <SourceFile
25
+ path="hi.ts"
26
+ headerComment="This is a very long header that is very long and should be wrapped. Meaning, it should be split into multiple lines."
27
+ >
28
+ hello!
29
+ </SourceFile>
30
+ </Output>,
31
+ );
32
+
33
+ expect(tree.contents[0].contents).toEqual(d`
34
+ // This is a very long header that is very long and should be wrapped. Meaning,
35
+ // it should be split into multiple lines.
36
+
37
+ hello!`);
38
+ });
@@ -0,0 +1,50 @@
1
+ import { List } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import {
5
+ CaseClause,
6
+ SwitchStatement,
7
+ } from "../src/components/SwitchStatement.jsx";
8
+ import { toSourceText } from "./utils.jsx";
9
+
10
+ it("handles various cases", () => {
11
+ const text = toSourceText(
12
+ <SwitchStatement expression="x">
13
+ <List hardline>
14
+ <CaseClause expression={'"test"'}>// expression</CaseClause>
15
+ <CaseClause jsValue={"test"}>// jsValue</CaseClause>
16
+ <CaseClause expression={"'break'"} break>
17
+ // break
18
+ </CaseClause>
19
+ <CaseClause expression={"'block'"} block>
20
+ // block
21
+ </CaseClause>
22
+ <CaseClause expression={"'breakblock'"} break block>
23
+ // block
24
+ </CaseClause>
25
+ <CaseClause default>// default</CaseClause>
26
+ </List>
27
+ </SwitchStatement>,
28
+ );
29
+
30
+ expect(text).toBe(d`
31
+ switch (x) {
32
+ case "test":
33
+ // expression
34
+ case "test":
35
+ // jsValue
36
+ case 'break':
37
+ // break
38
+ break;
39
+ case 'block': {
40
+ // block
41
+ }
42
+ case 'breakblock': {
43
+ // block
44
+ break;
45
+ }
46
+ default :
47
+ // default
48
+ }
49
+ `);
50
+ });
@@ -28,6 +28,15 @@ it.each([
28
28
  }
29
29
  `,
30
30
  ],
31
+ [
32
+ { "mangled-name": 1, "@pagination": 2 },
33
+ d`
34
+ {
35
+ "mangled-name": 1,
36
+ "@pagination": 2,
37
+ }
38
+ `,
39
+ ],
31
40
  ])("works - %o => %s", (jsValue, expectedSource) => {
32
41
  expect(toSourceText(<ValueExpression jsValue={jsValue} />)).toBe(
33
42
  expectedSource,
@@ -0,0 +1,70 @@
1
+ import { d } from "@alloy-js/core/testing";
2
+ import { expect, it } from "vitest";
3
+ import * as ts from "../src/index.js";
4
+ import { toSourceText } from "./utils.js";
5
+
6
+ it("renders a variable declaration with documentation", () => {
7
+ const res = toSourceText(
8
+ <>
9
+ <ts.VarDeclaration
10
+ name="myVar"
11
+ const
12
+ doc="This is a variable documentation"
13
+ >
14
+ 123
15
+ </ts.VarDeclaration>
16
+ ;
17
+ </>,
18
+ );
19
+ expect(res).toEqual(d`
20
+ /**
21
+ * This is a variable documentation
22
+ **/
23
+ const myVar = 123;
24
+ `);
25
+ });
26
+
27
+ it("renders a let declaration with documentation", () => {
28
+ const res = toSourceText(
29
+ <>
30
+ <ts.VarDeclaration
31
+ let
32
+ name="counter"
33
+ doc="A counter variable that can be changed"
34
+ type="number"
35
+ >
36
+ 0
37
+ </ts.VarDeclaration>
38
+ ;
39
+ </>,
40
+ );
41
+ expect(res).toEqual(d`
42
+ /**
43
+ * A counter variable that can be changed
44
+ **/
45
+ let counter: number = 0;
46
+ `);
47
+ });
48
+
49
+ it("renders an exported variable declaration with documentation", () => {
50
+ const res = toSourceText(
51
+ <>
52
+ <ts.VarDeclaration
53
+ export
54
+ name="API_URL"
55
+ const
56
+ doc="The base URL for API calls"
57
+ type="string"
58
+ >
59
+ "https://api.example.com"
60
+ </ts.VarDeclaration>
61
+ ;
62
+ </>,
63
+ );
64
+ expect(res).toEqual(d`
65
+ /**
66
+ * The base URL for API calls
67
+ **/
68
+ export const API_URL: string = "https://api.example.com";
69
+ `);
70
+ });