@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,247 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, refkey } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { describe, expect, it } from "vitest";
5
+ import * as ts from "../src/index.js";
6
+ it("renders an enum with only named members, no value", () => {
7
+ expect(_$createComponent(Output, {
8
+ get children() {
9
+ return _$createComponent(ts.SourceFile, {
10
+ path: "foo.ts",
11
+ get children() {
12
+ return _$createComponent(ts.EnumDeclaration, {
13
+ name: "MyEnum",
14
+ get children() {
15
+ return _$createComponent(ts.CommaList, {
16
+ get children() {
17
+ return [_$createComponent(ts.EnumMember, {
18
+ name: "foo"
19
+ }), _$createComponent(ts.EnumMember, {
20
+ name: "bar"
21
+ })];
22
+ }
23
+ });
24
+ }
25
+ });
26
+ }
27
+ });
28
+ }
29
+ })).toRenderTo(`
30
+ enum MyEnum {
31
+ foo,
32
+ bar,
33
+ }
34
+ `);
35
+ });
36
+ it("renders enum member with falsy value", () => {
37
+ expect(_$createComponent(Output, {
38
+ get children() {
39
+ return _$createComponent(ts.SourceFile, {
40
+ path: "foo.ts",
41
+ get children() {
42
+ return _$createComponent(ts.EnumDeclaration, {
43
+ name: "MyEnum",
44
+ get children() {
45
+ return _$createComponent(ts.CommaList, {
46
+ get children() {
47
+ return [_$createComponent(ts.EnumMember, {
48
+ name: "foo",
49
+ jsValue: 0
50
+ }), _$createComponent(ts.EnumMember, {
51
+ name: "bar",
52
+ jsValue: ""
53
+ })];
54
+ }
55
+ });
56
+ }
57
+ });
58
+ }
59
+ });
60
+ }
61
+ })).toRenderTo(`
62
+ enum MyEnum {
63
+ foo = 0,
64
+ bar = "",
65
+ }
66
+ `);
67
+ });
68
+ it("renders an enum with enum children", () => {
69
+ expect(_$createComponent(Output, {
70
+ get children() {
71
+ return _$createComponent(ts.SourceFile, {
72
+ path: "foo.ts",
73
+ get children() {
74
+ return _$createComponent(ts.EnumDeclaration, {
75
+ name: "MyEnum",
76
+ get children() {
77
+ return _$createComponent(ts.CommaList, {
78
+ get children() {
79
+ return [_$createComponent(ts.EnumMember, {
80
+ name: "foo",
81
+ value: "1"
82
+ }), _$createComponent(ts.EnumMember, {
83
+ name: "bar",
84
+ value: "2"
85
+ })];
86
+ }
87
+ });
88
+ }
89
+ });
90
+ }
91
+ });
92
+ }
93
+ })).toRenderTo(`
94
+ enum MyEnum {
95
+ foo = 1,
96
+ bar = 2,
97
+ }
98
+ `);
99
+ });
100
+ it("renders an enum with enum children specifying JS Value", () => {
101
+ expect(_$createComponent(Output, {
102
+ get children() {
103
+ return _$createComponent(ts.SourceFile, {
104
+ path: "foo.ts",
105
+ get children() {
106
+ return _$createComponent(ts.EnumDeclaration, {
107
+ name: "MyEnum",
108
+ get children() {
109
+ return _$createComponent(ts.CommaList, {
110
+ get children() {
111
+ return [_$createComponent(ts.EnumMember, {
112
+ name: "foo",
113
+ jsValue: "1"
114
+ }), _$createComponent(ts.EnumMember, {
115
+ name: "bar",
116
+ jsValue: 2
117
+ })];
118
+ }
119
+ });
120
+ }
121
+ });
122
+ }
123
+ });
124
+ }
125
+ })).toRenderTo(`
126
+ enum MyEnum {
127
+ foo = "1",
128
+ bar = 2,
129
+ }
130
+ `);
131
+ });
132
+ it("renders an enum given a JS value", () => {
133
+ const members = {
134
+ foo: 1,
135
+ bar: 2
136
+ };
137
+ expect(_$createComponent(Output, {
138
+ get children() {
139
+ return _$createComponent(ts.SourceFile, {
140
+ path: "foo.ts",
141
+ get children() {
142
+ return _$createComponent(ts.EnumDeclaration, {
143
+ name: "MyEnum",
144
+ jsValue: members
145
+ });
146
+ }
147
+ });
148
+ }
149
+ })).toRenderTo(`
150
+ enum MyEnum {
151
+ foo = 1,
152
+ bar = 2,
153
+ }
154
+ `);
155
+ });
156
+ it("renders an enum given a JS value and children", () => {
157
+ const members = {
158
+ foo: 1,
159
+ bar: 2
160
+ };
161
+ expect(_$createComponent(Output, {
162
+ get children() {
163
+ return _$createComponent(ts.SourceFile, {
164
+ path: "foo.ts",
165
+ get children() {
166
+ return _$createComponent(ts.EnumDeclaration, {
167
+ name: "MyEnum",
168
+ jsValue: members,
169
+ children: "custom = \"hello\""
170
+ });
171
+ }
172
+ });
173
+ }
174
+ })).toRenderTo(`
175
+ enum MyEnum {
176
+ foo = 1,
177
+ bar = 2,
178
+ custom = "hello"
179
+ }
180
+ `);
181
+ });
182
+ it("uses the naming policy", () => {
183
+ const members = {
184
+ "foo-prop": 1,
185
+ "bar-prop": 2
186
+ };
187
+ const policy = ts.createTSNamePolicy();
188
+ expect(_$createComponent(Output, {
189
+ namePolicy: policy,
190
+ get children() {
191
+ return _$createComponent(ts.SourceFile, {
192
+ path: "foo.ts",
193
+ get children() {
194
+ return _$createComponent(ts.EnumDeclaration, {
195
+ name: "my-enum",
196
+ jsValue: members
197
+ });
198
+ }
199
+ });
200
+ }
201
+ })).toRenderTo(`
202
+ enum MyEnum {
203
+ FooProp = 1,
204
+ BarProp = 2,
205
+ }
206
+ `);
207
+ });
208
+ describe("symbols", () => {
209
+ it("can reference enum members by refkey", () => {
210
+ const memberOne = refkey();
211
+ const memberTwo = refkey();
212
+ expect(_$createComponent(Output, {
213
+ get children() {
214
+ return _$createComponent(ts.SourceFile, {
215
+ path: "foo.ts",
216
+ get children() {
217
+ return [_$createComponent(ts.EnumDeclaration, {
218
+ name: "MyEnum",
219
+ get children() {
220
+ return _$createComponent(ts.CommaList, {
221
+ get children() {
222
+ return [_$createComponent(ts.EnumMember, {
223
+ name: "foo",
224
+ jsValue: "1",
225
+ refkey: memberOne
226
+ }), _$createComponent(ts.EnumMember, {
227
+ name: "mangled-name",
228
+ jsValue: 2,
229
+ refkey: memberTwo
230
+ })];
231
+ }
232
+ });
233
+ }
234
+ }), _$createIntrinsic("hbr", {}), memberOne, ";", _$createIntrinsic("hbr", {}), memberTwo, ";"];
235
+ }
236
+ });
237
+ }
238
+ })).toRenderTo(`
239
+ enum MyEnum {
240
+ foo = "1",
241
+ "mangled-name" = 2,
242
+ }
243
+ MyEnum.foo;
244
+ MyEnum["mangled-name"];
245
+ `);
246
+ });
247
+ });
@@ -0,0 +1,145 @@
1
+ import { memo as _$memo, createIntrinsic as _$createIntrinsic, createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, render } from "@alloy-js/core";
3
+ import { it } from "vitest";
4
+ import { fs } from "../src/builtins/node.js";
5
+ import { createPackage, FunctionDeclaration, PackageDirectory, SourceFile } from "../src/index.js";
6
+ import { assertFileContents } from "./utils.js";
7
+ it("can import builtins", () => {
8
+ const testLib = createPackage({
9
+ name: "testLib",
10
+ version: "1.0.0",
11
+ descriptor: {
12
+ ".": {
13
+ default: "defaultExport",
14
+ named: ["foo", "bar"]
15
+ },
16
+ "./subpath": {
17
+ named: ["nice", "cool"]
18
+ }
19
+ }
20
+ });
21
+ const res = render(_$createComponent(Output, {
22
+ externals: [testLib, fs],
23
+ get children() {
24
+ return _$createComponent(PackageDirectory, {
25
+ path: ".",
26
+ name: "test",
27
+ version: "1.0.0",
28
+ get children() {
29
+ return _$createComponent(SourceFile, {
30
+ path: "index.ts",
31
+ get children() {
32
+ return [_$memo(() => testLib["./subpath"].nice), ";", _$createIntrinsic("hbr", {}), "await ", _$memo(() => fs["./promises"].readFile), "();"];
33
+ }
34
+ });
35
+ }
36
+ });
37
+ }
38
+ }));
39
+ assertFileContents(res, {
40
+ "package.json": `
41
+ {
42
+ "name": "test",
43
+ "version": "1.0.0",
44
+ "type": "module",
45
+ "dependencies": {
46
+ "testLib": "1.0.0"
47
+ },
48
+ "devDependencies": {
49
+ "typescript": "^5.5.2"
50
+ }
51
+ }
52
+ `,
53
+ "index.ts": `
54
+ import { nice } from "testLib/subpath";
55
+ import { readFile } from "node:fs/promises";
56
+
57
+ nice;
58
+ await readFile();
59
+ `
60
+ });
61
+ });
62
+ it("can import builtins without a package", () => {
63
+ const testLib = createPackage({
64
+ name: "testLib",
65
+ version: "1.0.0",
66
+ descriptor: {
67
+ ".": {
68
+ default: "defaultExport",
69
+ named: ["foo", "bar"]
70
+ },
71
+ "./subpath": {
72
+ named: ["nice", "cool"]
73
+ }
74
+ }
75
+ });
76
+ const res = render(_$createComponent(Output, {
77
+ externals: [testLib, fs],
78
+ get children() {
79
+ return _$createComponent(SourceFile, {
80
+ path: "index.ts",
81
+ get children() {
82
+ return [_$memo(() => testLib["./subpath"].nice), ";", _$createIntrinsic("hbr", {}), "await ", _$memo(() => fs["./promises"].readFile), "();"];
83
+ }
84
+ });
85
+ }
86
+ }));
87
+ assertFileContents(res, {
88
+ "index.ts": `
89
+ import { nice } from "testLib/subpath";
90
+ import { readFile } from "node:fs/promises";
91
+
92
+ nice;
93
+ await readFile();
94
+ `
95
+ });
96
+ });
97
+ it("can import builtins without a package", () => {
98
+ const testLib = createPackage({
99
+ name: "testLib",
100
+ version: "1.0.0",
101
+ descriptor: {
102
+ ".": {
103
+ default: "defaultExport",
104
+ named: ["foo", "bar"]
105
+ },
106
+ "./subpath": {
107
+ named: ["nice", "cool"]
108
+ }
109
+ }
110
+ });
111
+ const res = render(_$createComponent(Output, {
112
+ externals: [testLib, fs],
113
+ get children() {
114
+ return _$createComponent(SourceFile, {
115
+ path: "index.ts",
116
+ get children() {
117
+ return _$createComponent(FunctionDeclaration, {
118
+ name: "foo",
119
+ get children() {
120
+ return _$createComponent(FunctionDeclaration, {
121
+ name: "bar",
122
+ get children() {
123
+ return [_$memo(() => testLib["./subpath"].nice), ";", _$createIntrinsic("hbr", {}), "await ", _$memo(() => fs["./promises"].readFile), "();"];
124
+ }
125
+ });
126
+ }
127
+ });
128
+ }
129
+ });
130
+ }
131
+ }));
132
+ assertFileContents(res, {
133
+ "index.ts": `
134
+ import { nice } from "testLib/subpath";
135
+ import { readFile } from "node:fs/promises";
136
+
137
+ function foo() {
138
+ function bar() {
139
+ nice;
140
+ await readFile();
141
+ }
142
+ }
143
+ `
144
+ });
145
+ });
@@ -0,0 +1,87 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, refkey, render, StatementList } from "@alloy-js/core";
3
+ import { it } from "vitest";
4
+ import { FunctionCallExpression, FunctionDeclaration, PackageDirectory, SourceFile, VarDeclaration } from "../../typescript/src/index.js";
5
+ import { assertFileContents } from "./utils.js";
6
+ it("can declare and call a function with parameters", () => {
7
+ const functionRefkey = refkey();
8
+ const res = render(_$createComponent(Output, {
9
+ get children() {
10
+ return _$createComponent(PackageDirectory, {
11
+ path: ".",
12
+ name: "test",
13
+ version: "1.0.0",
14
+ get children() {
15
+ return _$createComponent(SourceFile, {
16
+ path: "index.ts",
17
+ get children() {
18
+ return [_$createComponent(VarDeclaration, {
19
+ name: "foo",
20
+ initializer: `"Foo"`,
21
+ "const": true
22
+ }), ";", _$createIntrinsic("hbr", {}), _$createComponent(FunctionDeclaration, {
23
+ refkey: functionRefkey,
24
+ name: "bar",
25
+ parameters: [{
26
+ name: "foo",
27
+ optional: true,
28
+ type: "string"
29
+ }, {
30
+ name: "baz",
31
+ optional: true,
32
+ type: "number"
33
+ }],
34
+ get children() {
35
+ return _$createComponent(StatementList, {
36
+ get children() {
37
+ return [["const message = foo ? foo : \"Hello, World!\""], ["console.log(message)"], ["if(baz) console.log(baz)"]];
38
+ }
39
+ });
40
+ }
41
+ }), _$createIntrinsic("hbr", {}), _$createComponent(StatementList, {
42
+ get children() {
43
+ return [_$createComponent(FunctionCallExpression, {
44
+ target: functionRefkey,
45
+ args: [`"Hello!"`]
46
+ }), _$createComponent(FunctionCallExpression, {
47
+ target: functionRefkey
48
+ }), _$createComponent(FunctionCallExpression, {
49
+ target: functionRefkey,
50
+ get args() {
51
+ return [["\"Hello ", ["World"], "!\""]];
52
+ }
53
+ }), _$createComponent(FunctionCallExpression, {
54
+ target: functionRefkey,
55
+ get args() {
56
+ return [["\"Hey there!\""]];
57
+ }
58
+ }), _$createComponent(FunctionCallExpression, {
59
+ target: functionRefkey,
60
+ get args() {
61
+ return [["\"Hello World\""], ["12345"]];
62
+ }
63
+ })];
64
+ }
65
+ })];
66
+ }
67
+ });
68
+ }
69
+ });
70
+ }
71
+ }));
72
+ assertFileContents(res, {
73
+ "index.ts": `
74
+ const foo = "Foo";
75
+ function bar(foo?: string, baz?: number) {
76
+ const message = foo ? foo : "Hello, World!";
77
+ console.log(message);
78
+ if(baz) console.log(baz);
79
+ }
80
+ bar("Hello!");
81
+ bar();
82
+ bar("Hello World!");
83
+ bar("Hey there!");
84
+ bar("Hello World", 12345);
85
+ `
86
+ });
87
+ });
@@ -0,0 +1,91 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { refkey, StatementList } from "@alloy-js/core";
3
+ import { d } from "@alloy-js/core/testing";
4
+ import { expect, it } from "vitest";
5
+ import * as ts from "../src/index.js";
6
+ import { toSourceText } from "./utils.js";
7
+ it("renders a function declaration with documentation", () => {
8
+ const res = toSourceText(_$createComponent(ts.FunctionDeclaration, {
9
+ name: "greet",
10
+ doc: "A function that greets a person",
11
+ parameters: [{
12
+ name: "name",
13
+ type: "string"
14
+ }],
15
+ returnType: "string"
16
+ }));
17
+ expect(res).toEqual(d`
18
+ /**
19
+ * A function that greets a person
20
+ *
21
+ * @param {string} name
22
+ **/
23
+ function greet(name: string): string {}
24
+ `);
25
+ });
26
+ it("With documented parameters", () => {
27
+ const res = toSourceText(_$createComponent(ts.FunctionDeclaration, {
28
+ "export": true,
29
+ name: "calculateTotal",
30
+ doc: "Calculates the total price including tax",
31
+ parameters: [{
32
+ name: "price",
33
+ type: "number"
34
+ }, {
35
+ name: "taxRate",
36
+ type: "number",
37
+ optional: true
38
+ }],
39
+ returnType: "number"
40
+ }));
41
+ expect(res).toEqual(d`
42
+ /**
43
+ * Calculates the total price including tax
44
+ *
45
+ * @param {number} price
46
+ * @param {number} [taxRate]
47
+ **/
48
+ export function calculateTotal(price: number, taxRate?: number): number {}
49
+ `);
50
+ });
51
+ it("renders a function with a reference", () => {
52
+ const userRef = refkey();
53
+ const res = toSourceText([_$createComponent(ts.InterfaceDeclaration, {
54
+ name: "User",
55
+ refkey: userRef,
56
+ get children() {
57
+ return _$createComponent(StatementList, {
58
+ get children() {
59
+ return [_$createComponent(ts.InterfaceMember, {
60
+ name: "id",
61
+ type: "number"
62
+ }), _$createComponent(ts.InterfaceMember, {
63
+ name: "name",
64
+ type: "string"
65
+ })];
66
+ }
67
+ });
68
+ }
69
+ }), _$createIntrinsic("hbr", {}), _$createIntrinsic("hbr", {}), _$createComponent(ts.FunctionDeclaration, {
70
+ name: "createUser",
71
+ doc: "Creates a new user",
72
+ parameters: [{
73
+ name: "user",
74
+ type: userRef,
75
+ doc: "The user object to create"
76
+ }],
77
+ returnType: "void"
78
+ })]);
79
+ expect(res).toEqual(d`
80
+ interface User {
81
+ id: number;
82
+ name: string;
83
+ }
84
+
85
+ /**
86
+ * Creates a new user
87
+ *
88
+ * @param {User} user - The user object to create
89
+ **/
90
+ function createUser(user: User): void {}`);
91
+ });