@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
@@ -45,3 +45,63 @@ it("accepts only children", () => {
45
45
  [[4, 5, 6]]
46
46
  `);
47
47
  });
48
+
49
+ it("renders a falsy jsValue", () => {
50
+ expect(
51
+ toSourceText(<ArrayExpression jsValue={[undefined]}></ArrayExpression>),
52
+ ).toBe(d`
53
+ [undefined]
54
+ `);
55
+ });
56
+
57
+ it("renders a falsy jsValue with no leading comma", () => {
58
+ expect(toSourceText(<ArrayExpression jsValue={[null]}></ArrayExpression>))
59
+ .toBe(d`
60
+ [null]
61
+ `);
62
+ });
63
+
64
+ it("renders a falsy jsValue and children", () => {
65
+ expect(
66
+ toSourceText(<ArrayExpression jsValue={[false]}>"Hello"</ArrayExpression>),
67
+ ).toBe(d`
68
+ [false, "Hello"]
69
+ `);
70
+ });
71
+
72
+ it("renders a falsy jsValue but not invisible children", () => {
73
+ expect(
74
+ toSourceText(
75
+ <ArrayExpression jsValue={[false]}>
76
+ {null}
77
+ {undefined}
78
+ </ArrayExpression>,
79
+ ),
80
+ ).toBe(d`
81
+ [false]
82
+ `);
83
+ });
84
+
85
+ it("renders a falsy jsValue and visible children", () => {
86
+ expect(
87
+ toSourceText(<ArrayExpression jsValue={[false]}>"Hello"</ArrayExpression>),
88
+ ).toBe(d`
89
+ [false, "Hello"]
90
+ `);
91
+ });
92
+
93
+ it("renders a falsy jsValue and visible falsy children", () => {
94
+ expect(
95
+ toSourceText(<ArrayExpression jsValue={[false]}>false</ArrayExpression>),
96
+ ).toBe(d`
97
+ [false, false]
98
+ `);
99
+ });
100
+
101
+ it("renders a falsy jsValue but not invisible falsy children", () => {
102
+ expect(
103
+ toSourceText(<ArrayExpression jsValue={[false]}>{false}</ArrayExpression>),
104
+ ).toBe(d`
105
+ [false]
106
+ `);
107
+ });
@@ -40,7 +40,7 @@ it("works", () => {
40
40
  <ts.FunctionDeclaration
41
41
  name={"say" + fnSpec.greeting}
42
42
  refkey={greetKey}
43
- parameters={{ str: "string" }}
43
+ parameters={[{ name: "str", type: "string" }]}
44
44
  >
45
45
  return "{fnSpec.greeting} " + str;
46
46
  </ts.FunctionDeclaration>
@@ -48,7 +48,7 @@ it("works", () => {
48
48
  <ts.FunctionDeclaration
49
49
  name={"say" + fnSpec.farewell}
50
50
  refkey={farewellKey}
51
- parameters={{ str: "string" }}
51
+ parameters={[{ name: "str", type: "string" }]}
52
52
  >
53
53
  return "{fnSpec.farewell} " + str;
54
54
  </ts.FunctionDeclaration>
@@ -0,0 +1,29 @@
1
+ import { d } from "@alloy-js/core/testing";
2
+ import { expect, it } from "vitest";
3
+ import { BlockScope } from "../src/components/BlockScope.jsx";
4
+ import { VarDeclaration } from "../src/index.js";
5
+ import { toSourceText } from "./utils.jsx";
6
+
7
+ it("creates a scope", () => {
8
+ const text = toSourceText(
9
+ <>
10
+ <VarDeclaration name="x" initializer="hi" />;<hbr />
11
+ <BlockScope name="foo">
12
+ <VarDeclaration name="x" initializer="hello" />;
13
+ </BlockScope>
14
+ </>,
15
+ );
16
+
17
+ expect(text).toBe(d`
18
+ const x = hi;
19
+ {
20
+ const x = hello;
21
+ }
22
+ `);
23
+ });
24
+
25
+ it("renders an empty block properly", () => {
26
+ const text = toSourceText(<BlockScope></BlockScope>);
27
+
28
+ expect(text).toBe("{}");
29
+ });
@@ -61,6 +61,8 @@ describe("instance members", () => {
61
61
 
62
62
  it("can be referenced", () => {
63
63
  const one = refkey();
64
+ const privateOne = refkey();
65
+
64
66
  const res = toSourceText(
65
67
  <ts.ClassDeclaration name="Foo">
66
68
  <StatementList>
@@ -68,6 +70,10 @@ describe("instance members", () => {
68
70
  1
69
71
  </ts.ClassField>
70
72
  <ts.ClassField name="two">{one} + 1</ts.ClassField>
73
+ <ts.ClassField name="one" refkey={privateOne} jsPrivate>
74
+ 1
75
+ </ts.ClassField>
76
+ <ts.ClassField name="three">{privateOne} + 1</ts.ClassField>
71
77
  </StatementList>
72
78
  </ts.ClassDeclaration>,
73
79
  );
@@ -76,6 +82,8 @@ describe("instance members", () => {
76
82
  class Foo {
77
83
  one = 1;
78
84
  two = this.one + 1;
85
+ #one = 1;
86
+ three = this.#one + 1;
79
87
  }
80
88
  `);
81
89
  });
@@ -97,6 +105,64 @@ describe("instance members", () => {
97
105
  );
98
106
  }).toThrow(/Cannot resolve member symbols/);
99
107
  });
108
+
109
+ it("cannot be referenced outside the class when protected", () => {
110
+ const one = refkey();
111
+ expect(() => {
112
+ toSourceText(
113
+ <>
114
+ <ts.ClassDeclaration name="Foo">
115
+ <StatementList>
116
+ <ts.ClassField name="one" refkey={one} protected>
117
+ 1
118
+ </ts.ClassField>
119
+ </StatementList>
120
+ </ts.ClassDeclaration>
121
+ {one}
122
+ </>,
123
+ );
124
+ }).toThrow(/Cannot resolve member symbols/);
125
+ });
126
+
127
+ it("cannot be referenced outside the class when private", () => {
128
+ const one = refkey();
129
+ expect(() => {
130
+ toSourceText(
131
+ <>
132
+ <ts.ClassDeclaration name="Foo">
133
+ <StatementList>
134
+ <ts.ClassField name="one" refkey={one} jsPrivate>
135
+ 1
136
+ </ts.ClassField>
137
+ </StatementList>
138
+ </ts.ClassDeclaration>
139
+ {one}
140
+ </>,
141
+ );
142
+ }).toThrow(/Cannot resolve private member symbols/);
143
+ });
144
+
145
+ it("works with invalid identifier names", () => {
146
+ const one = refkey();
147
+
148
+ const res = toSourceText(
149
+ <ts.ClassDeclaration name="Foo">
150
+ <StatementList>
151
+ <ts.ClassField name="o-n-e" refkey={one}>
152
+ 1
153
+ </ts.ClassField>
154
+ <ts.ClassField name="t-w-o">{one} + 1</ts.ClassField>
155
+ </StatementList>
156
+ </ts.ClassDeclaration>,
157
+ );
158
+
159
+ expect(res).toEqual(d`
160
+ class Foo {
161
+ "o-n-e" = 1;
162
+ "t-w-o" = this["o-n-e"] + 1;
163
+ }
164
+ `);
165
+ });
100
166
  });
101
167
 
102
168
  describe("static members", () => {
@@ -108,6 +174,10 @@ describe("static members", () => {
108
174
  <ts.ClassField static name="one" refkey={one}>
109
175
  1
110
176
  </ts.ClassField>
177
+
178
+ <ts.ClassField static name="two" jsPrivate>
179
+ 2
180
+ </ts.ClassField>
111
181
  </StatementList>
112
182
  </ts.ClassDeclaration>,
113
183
  );
@@ -115,12 +185,14 @@ describe("static members", () => {
115
185
  expect(res).toBe(d`
116
186
  class Foo {
117
187
  static one = 1;
188
+ static #two = 2;
118
189
  }
119
190
  `);
120
191
  });
121
192
 
122
193
  it("can be referenced", () => {
123
194
  const one = refkey();
195
+ const two = refkey();
124
196
  const res = toSourceText(
125
197
  <>
126
198
  <ts.ClassDeclaration name="Foo">
@@ -128,6 +200,10 @@ describe("static members", () => {
128
200
  <ts.ClassField static name="one" refkey={one}>
129
201
  1
130
202
  </ts.ClassField>
203
+ <ts.ClassField static name="two" refkey={two} jsPrivate>
204
+ 2
205
+ </ts.ClassField>
206
+ <ts.ClassMethod name="getTwo">return {two};</ts.ClassMethod>
131
207
  </StatementList>
132
208
  </ts.ClassDeclaration>
133
209
  <hbr />
@@ -138,10 +214,34 @@ describe("static members", () => {
138
214
  expect(res).toBe(d`
139
215
  class Foo {
140
216
  static one = 1;
217
+ static #two = 2;
218
+ getTwo() {
219
+ return Foo.#two;
220
+ };
141
221
  }
142
222
  Foo.one;
143
223
  `);
144
224
  });
225
+
226
+ it("cannot be referenced outside the class when private", () => {
227
+ const two = refkey();
228
+
229
+ expect(() =>
230
+ toSourceText(
231
+ <>
232
+ <ts.ClassDeclaration name="Foo">
233
+ <StatementList>
234
+ <ts.ClassField static name="two" refkey={two} jsPrivate>
235
+ 2
236
+ </ts.ClassField>
237
+ </StatementList>
238
+ </ts.ClassDeclaration>
239
+ <hbr />
240
+ {two};
241
+ </>,
242
+ ),
243
+ ).toThrow(/Cannot resolve/);
244
+ });
145
245
  });
146
246
 
147
247
  describe("instance methods", () => {
@@ -159,14 +259,17 @@ describe("instance methods", () => {
159
259
  <ts.ClassMethod name="four" protected />
160
260
  <ts.ClassMethod
161
261
  name="five"
162
- parameters={{ a: "string", b: "string" }}
262
+ parameters={[
263
+ { name: "a", type: "string" },
264
+ { name: "b", type: "string" },
265
+ ]}
163
266
  />
164
267
  <ts.ClassMethod
165
268
  name="six"
166
- parameters={{
167
- a: { type: "number", refkey: a },
168
- b: { type: "number", refkey: b },
169
- }}
269
+ parameters={[
270
+ { name: "a", type: "number", refkey: a },
271
+ { name: "b", type: "number", refkey: b },
272
+ ]}
170
273
  returnType="number"
171
274
  >
172
275
  {one} = {a} + {b};
@@ -191,3 +294,68 @@ describe("instance methods", () => {
191
294
  `);
192
295
  });
193
296
  });
297
+
298
+ it("renders a class with docs for the class and its members", () => {
299
+ const res = toSourceText(
300
+ <ts.ClassDeclaration name="Foo" doc="This is a class documentation">
301
+ <ts.ClassField name="bar" doc="This is a field documentation">
302
+ 123
303
+ </ts.ClassField>
304
+ ;<hbr />
305
+ <ts.ClassMethod name="baz" doc="This is a method documentation">
306
+ return 123;
307
+ </ts.ClassMethod>
308
+ </ts.ClassDeclaration>,
309
+ );
310
+ expect(res).toEqual(d`
311
+ /**
312
+ * This is a class documentation
313
+ **/
314
+ class Foo {
315
+ /**
316
+ * This is a field documentation
317
+ **/
318
+ bar = 123;
319
+ /**
320
+ * This is a method documentation
321
+ **/
322
+ baz() {
323
+ return 123;
324
+ }
325
+ }
326
+ `);
327
+ });
328
+
329
+ it("renders a method with parameter docs", () => {
330
+ const res = toSourceText(
331
+ <ts.ClassDeclaration name="Foo">
332
+ <ts.ClassMethod
333
+ name="bar"
334
+ doc="Method documentation"
335
+ returnType="void"
336
+ parameters={[
337
+ { name: "a", type: "number", doc: "Parameter a doc" },
338
+ {
339
+ name: "b",
340
+ type: "string",
341
+ doc: "Line 1 for b. This is a long description that should continue in the next line.",
342
+ },
343
+ ]}
344
+ />
345
+ </ts.ClassDeclaration>,
346
+ { printWidth: 40 },
347
+ );
348
+ expect(res).toEqual(d`
349
+ class Foo {
350
+ /**
351
+ * Method documentation
352
+ *
353
+ * @param {number} a - Parameter a doc
354
+ * @param {string} b - Line 1 for b.
355
+ * This is a long description that
356
+ * should continue in the next line.
357
+ **/
358
+ bar(a: number, b: string): void {}
359
+ }
360
+ `);
361
+ });
@@ -0,0 +1,74 @@
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 an enum declaration with documentation", () => {
7
+ const res = toSourceText(
8
+ <ts.EnumDeclaration
9
+ name="Color"
10
+ doc="Color enumeration for the application"
11
+ >
12
+ <ts.CommaList>
13
+ <ts.EnumMember name="Red" />
14
+ <ts.EnumMember name="Green" />
15
+ <ts.EnumMember name="Blue" />
16
+ </ts.CommaList>
17
+ </ts.EnumDeclaration>,
18
+ );
19
+ expect(res).toEqual(d`
20
+ /**
21
+ * Color enumeration for the application
22
+ **/
23
+ enum Color {
24
+ Red,
25
+ Green,
26
+ Blue,
27
+ }
28
+ `);
29
+ });
30
+
31
+ it("renders an enum declaration with documented members", () => {
32
+ const res = toSourceText(
33
+ <ts.EnumDeclaration
34
+ name="Direction"
35
+ doc="Represents cardinal directions. Used for navigation purposes"
36
+ >
37
+ <ts.CommaList>
38
+ <ts.EnumMember name="North" jsValue={0} doc={["Bound to the north"]} />
39
+ <ts.EnumMember
40
+ name="East"
41
+ jsValue={1}
42
+ doc={["Bound to the East", "A second doc paragraph"]}
43
+ />
44
+ <ts.EnumMember name="South" jsValue={2} doc="Opposite to North" />
45
+ <ts.EnumMember name="West" jsValue={3} doc="Going west!" />
46
+ </ts.CommaList>
47
+ </ts.EnumDeclaration>,
48
+ );
49
+ expect(res).toEqual(d`
50
+ /**
51
+ * Represents cardinal directions. Used for navigation purposes
52
+ **/
53
+ enum Direction {
54
+ /**
55
+ * Bound to the north
56
+ **/
57
+ North = 0,
58
+ /**
59
+ * Bound to the East
60
+ *
61
+ * A second doc paragraph
62
+ **/
63
+ East = 1,
64
+ /**
65
+ * Opposite to North
66
+ **/
67
+ South = 2,
68
+ /**
69
+ * Going west!
70
+ **/
71
+ West = 3,
72
+ }
73
+ `);
74
+ });
@@ -4,6 +4,46 @@ import { describe, expect, it } from "vitest";
4
4
 
5
5
  import * as ts from "../src/index.js";
6
6
 
7
+ it("renders an enum with only named members, no value", () => {
8
+ expect(
9
+ <Output>
10
+ <ts.SourceFile path="foo.ts">
11
+ <ts.EnumDeclaration name="MyEnum">
12
+ <ts.CommaList>
13
+ <ts.EnumMember name="foo" />
14
+ <ts.EnumMember name="bar" />
15
+ </ts.CommaList>
16
+ </ts.EnumDeclaration>
17
+ </ts.SourceFile>
18
+ </Output>,
19
+ ).toRenderTo(`
20
+ enum MyEnum {
21
+ foo,
22
+ bar,
23
+ }
24
+ `);
25
+ });
26
+
27
+ it("renders enum member with falsy value", () => {
28
+ expect(
29
+ <Output>
30
+ <ts.SourceFile path="foo.ts">
31
+ <ts.EnumDeclaration name="MyEnum">
32
+ <ts.CommaList>
33
+ <ts.EnumMember name="foo" jsValue={0} />
34
+ <ts.EnumMember name="bar" jsValue={""} />
35
+ </ts.CommaList>
36
+ </ts.EnumDeclaration>
37
+ </ts.SourceFile>
38
+ </Output>,
39
+ ).toRenderTo(`
40
+ enum MyEnum {
41
+ foo = 0,
42
+ bar = "",
43
+ }
44
+ `);
45
+ });
46
+
7
47
  it("renders an enum with enum children", () => {
8
48
  expect(
9
49
  <Output>
@@ -112,6 +152,7 @@ it("uses the naming policy", () => {
112
152
  describe("symbols", () => {
113
153
  it("can reference enum members by refkey", () => {
114
154
  const memberOne = refkey();
155
+ const memberTwo = refkey();
115
156
 
116
157
  expect(
117
158
  <Output>
@@ -119,19 +160,25 @@ describe("symbols", () => {
119
160
  <ts.EnumDeclaration name="MyEnum">
120
161
  <ts.CommaList>
121
162
  <ts.EnumMember name="foo" jsValue="1" refkey={memberOne} />
122
- <ts.EnumMember name="bar" jsValue={2} />
163
+ <ts.EnumMember
164
+ name="mangled-name"
165
+ jsValue={2}
166
+ refkey={memberTwo}
167
+ />
123
168
  </ts.CommaList>
124
169
  </ts.EnumDeclaration>
125
170
  <hbr />
126
- {memberOne};
171
+ {memberOne};<hbr />
172
+ {memberTwo};
127
173
  </ts.SourceFile>
128
174
  </Output>,
129
175
  ).toRenderTo(`
130
176
  enum MyEnum {
131
177
  foo = "1",
132
- bar = 2,
178
+ "mangled-name" = 2,
133
179
  }
134
180
  MyEnum.foo;
181
+ MyEnum["mangled-name"];
135
182
  `);
136
183
  });
137
184
  });
@@ -16,11 +16,14 @@ it("can declare and call a function with parameters", () => {
16
16
  <Output>
17
17
  <PackageDirectory path="." name="test" version="1.0.0">
18
18
  <SourceFile path="index.ts">
19
- <VarDeclaration name="foo" value={`"Foo"`} const />;<hbr />
19
+ <VarDeclaration name="foo" initializer={`"Foo"`} const />;<hbr />
20
20
  <FunctionDeclaration
21
21
  refkey={functionRefkey}
22
22
  name="bar"
23
- parameters={{ "foo?": "string", "baz?": "number" }}
23
+ parameters={[
24
+ { name: "foo", optional: true, type: "string" },
25
+ { name: "baz", optional: true, type: "number" },
26
+ ]}
24
27
  >
25
28
  <StatementList>
26
29
  <>const message = foo ? foo : "Hello, World!"</>
@@ -0,0 +1,89 @@
1
+ import { refkey, StatementList } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import * as ts from "../src/index.js";
5
+ import { toSourceText } from "./utils.js";
6
+
7
+ it("renders a function declaration with documentation", () => {
8
+ const res = toSourceText(
9
+ <ts.FunctionDeclaration
10
+ name="greet"
11
+ doc="A function that greets a person"
12
+ parameters={[{ name: "name", type: "string" }]}
13
+ returnType="string"
14
+ />,
15
+ );
16
+ expect(res).toEqual(d`
17
+ /**
18
+ * A function that greets a person
19
+ *
20
+ * @param {string} name
21
+ **/
22
+ function greet(name: string): string {}
23
+ `);
24
+ });
25
+
26
+ it("With documented parameters", () => {
27
+ const res = toSourceText(
28
+ <ts.FunctionDeclaration
29
+ export
30
+ name="calculateTotal"
31
+ doc="Calculates the total price including tax"
32
+ parameters={[
33
+ { name: "price", type: "number" },
34
+ { name: "taxRate", type: "number", optional: true },
35
+ ]}
36
+ returnType="number"
37
+ />,
38
+ );
39
+ expect(res).toEqual(d`
40
+ /**
41
+ * Calculates the total price including tax
42
+ *
43
+ * @param {number} price
44
+ * @param {number} [taxRate]
45
+ **/
46
+ export function calculateTotal(price: number, taxRate?: number): number {}
47
+ `);
48
+ });
49
+
50
+ it("renders a function with a reference", () => {
51
+ const userRef = refkey();
52
+
53
+ const res = toSourceText(
54
+ <>
55
+ <ts.InterfaceDeclaration name="User" refkey={userRef}>
56
+ <StatementList>
57
+ <ts.InterfaceMember name="id" type="number" />
58
+ <ts.InterfaceMember name="name" type="string" />
59
+ </StatementList>
60
+ </ts.InterfaceDeclaration>
61
+ <hbr />
62
+ <hbr />
63
+ <ts.FunctionDeclaration
64
+ name="createUser"
65
+ doc="Creates a new user"
66
+ parameters={[
67
+ {
68
+ name: "user",
69
+ type: userRef,
70
+ doc: "The user object to create",
71
+ },
72
+ ]}
73
+ returnType="void"
74
+ />
75
+ </>,
76
+ );
77
+ expect(res).toEqual(d`
78
+ interface User {
79
+ id: number;
80
+ name: string;
81
+ }
82
+
83
+ /**
84
+ * Creates a new user
85
+ *
86
+ * @param {User} user - The user object to create
87
+ **/
88
+ function createUser(user: User): void {}`);
89
+ });
@@ -2,11 +2,8 @@ import { Props, refkey } from "@alloy-js/core";
2
2
  import { d } from "@alloy-js/core/testing";
3
3
  import { describe, expect, it } from "vitest";
4
4
  import { StatementList } from "../../core/src/components/StatementList.jsx";
5
- import {
6
- FunctionDeclaration,
7
- ParameterDescriptor,
8
- VarDeclaration,
9
- } from "../src/index.js";
5
+ import { FunctionDeclaration, VarDeclaration } from "../src/index.js";
6
+ import { ParameterDescriptor } from "../src/parameter-descriptor.js";
10
7
  import { toSourceText } from "./utils.js";
11
8
 
12
9
  it("works", () => {
@@ -76,7 +73,10 @@ it("supports type parameters by descriptor object", () => {
76
73
  const decl = (
77
74
  <FunctionDeclaration
78
75
  name="foo"
79
- typeParameters={{ a: "any", b: "any" }}
76
+ typeParameters={[
77
+ { name: "a", extends: "any" },
78
+ { name: "b", extends: "any" },
79
+ ]}
80
80
  ></FunctionDeclaration>
81
81
  );
82
82
 
@@ -164,7 +164,7 @@ describe("symbols", () => {
164
164
  <>
165
165
  <FunctionDeclaration
166
166
  name="foo"
167
- parameters={{ sym: { type: "any", refkey: rk } }}
167
+ parameters={[{ name: "sym", type: "any", refkey: rk }]}
168
168
  >
169
169
  <FunctionDeclaration name="bar">{rk}</FunctionDeclaration>
170
170
  </FunctionDeclaration>
@@ -183,7 +183,10 @@ describe("symbols", () => {
183
183
  it("creates symbols for parameters and addresses conflicts", () => {
184
184
  const decl = (
185
185
  <>
186
- <FunctionDeclaration name="foo" parameters={{ conflict: "any" }}>
186
+ <FunctionDeclaration
187
+ name="foo"
188
+ parameters={[{ name: "conflict", type: "any" }]}
189
+ >
187
190
  <VarDeclaration name="conflict">1</VarDeclaration>;
188
191
  </FunctionDeclaration>
189
192
  </>
@@ -198,13 +201,14 @@ describe("symbols", () => {
198
201
 
199
202
  it("create optional parameters", () => {
200
203
  const paramDesc: ParameterDescriptor = {
204
+ name: "foo",
201
205
  refkey: refkey(),
202
206
  type: "any",
203
207
  optional: true,
204
208
  };
205
209
  const decl = (
206
210
  <>
207
- <FunctionDeclaration name="foo" parameters={{ foo: paramDesc }}>
211
+ <FunctionDeclaration name="foo" parameters={[paramDesc]}>
208
212
  console.log(foo);
209
213
  </FunctionDeclaration>
210
214
  </>