@alloy-js/typescript 0.10.0 → 0.12.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 (244) hide show
  1. package/CHANGELOG.md +28 -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.d.ts +2 -0
  11. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  12. package/dist/src/components/ClassDeclaration.js +16 -8
  13. package/dist/src/components/CommaList.js +2 -5
  14. package/dist/src/components/Declaration.js +3 -5
  15. package/dist/src/components/EnumDeclaration.js +2 -5
  16. package/dist/src/components/EnumMember.js +2 -4
  17. package/dist/src/components/ExportStatement.js +1 -2
  18. package/dist/src/components/FunctionBase.d.ts +60 -0
  19. package/dist/src/components/FunctionBase.d.ts.map +1 -0
  20. package/dist/src/components/FunctionBase.js +217 -0
  21. package/dist/src/components/FunctionCallExpression.js +2 -5
  22. package/dist/src/components/FunctionDeclaration.d.ts +3 -18
  23. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  24. package/dist/src/components/FunctionDeclaration.js +9 -157
  25. package/dist/src/components/FunctionExpression.d.ts +39 -0
  26. package/dist/src/components/FunctionExpression.d.ts.map +1 -0
  27. package/dist/src/components/FunctionExpression.js +66 -0
  28. package/dist/src/components/FunctionType.d.ts +38 -0
  29. package/dist/src/components/FunctionType.d.ts.map +1 -0
  30. package/dist/src/components/FunctionType.js +77 -0
  31. package/dist/src/components/IfStatement.js +2 -4
  32. package/dist/src/components/ImportStatement.d.ts.map +1 -1
  33. package/dist/src/components/ImportStatement.js +10 -4
  34. package/dist/src/components/Interface.d.ts +3 -2
  35. package/dist/src/components/Interface.d.ts.map +1 -1
  36. package/dist/src/components/Interface.js +40 -15
  37. package/dist/src/components/InterfaceMethod.d.ts +43 -0
  38. package/dist/src/components/InterfaceMethod.d.ts.map +1 -0
  39. package/dist/src/components/InterfaceMethod.js +87 -0
  40. package/dist/src/components/JSDoc.js +1 -2
  41. package/dist/src/components/JSDocComment.js +3 -5
  42. package/dist/src/components/JSDocExample.js +2 -5
  43. package/dist/src/components/JSDocParam.js +2 -5
  44. package/dist/src/components/MemberChainExpression.js +2 -4
  45. package/dist/src/components/MemberDeclaration.d.ts +34 -0
  46. package/dist/src/components/MemberDeclaration.d.ts.map +1 -0
  47. package/dist/src/components/MemberDeclaration.js +29 -0
  48. package/dist/src/components/MemberExpression.d.ts.map +1 -1
  49. package/dist/src/components/MemberExpression.js +5 -7
  50. package/dist/src/components/ObjectExpression.js +2 -5
  51. package/dist/src/components/PackageDirectory.js +2 -5
  52. package/dist/src/components/PackageJson.js +1 -2
  53. package/dist/src/components/PropertyName.js +1 -2
  54. package/dist/src/components/Reference.d.ts +8 -1
  55. package/dist/src/components/Reference.d.ts.map +1 -1
  56. package/dist/src/components/Reference.js +8 -4
  57. package/dist/src/components/SingleLineCommentBlock.js +2 -4
  58. package/dist/src/components/SourceFile.js +2 -4
  59. package/dist/src/components/SwitchStatement.js +2 -5
  60. package/dist/src/components/TsConfigJson.js +1 -2
  61. package/dist/src/components/TypeDeclaration.d.ts +1 -1
  62. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  63. package/dist/src/components/TypeDeclaration.js +4 -7
  64. package/dist/src/components/TypeRefContext.d.ts +22 -0
  65. package/dist/src/components/TypeRefContext.d.ts.map +1 -0
  66. package/dist/src/components/TypeRefContext.js +41 -0
  67. package/dist/src/components/ValueExpression.js +1 -2
  68. package/dist/src/components/VarDeclaration.d.ts +1 -0
  69. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  70. package/dist/src/components/VarDeclaration.js +11 -8
  71. package/dist/src/components/index.d.ts +7 -0
  72. package/dist/src/components/index.d.ts.map +1 -1
  73. package/dist/src/components/index.js +7 -1
  74. package/dist/src/components/stc/index.d.ts +8 -0
  75. package/dist/src/components/stc/index.d.ts.map +1 -1
  76. package/dist/src/components/stc/index.js +9 -2
  77. package/dist/src/context/private-scope.js +1 -2
  78. package/dist/src/context/type-ref-context.d.ts +15 -0
  79. package/dist/src/context/type-ref-context.d.ts.map +1 -0
  80. package/dist/src/context/type-ref-context.js +12 -0
  81. package/dist/src/create-package.js +1 -2
  82. package/dist/src/index.js +1 -2
  83. package/dist/src/name-conflict-resolver.js +1 -2
  84. package/dist/src/name-policy.js +1 -2
  85. package/dist/src/parameter-descriptor.d.ts +33 -17
  86. package/dist/src/parameter-descriptor.d.ts.map +1 -1
  87. package/dist/src/source-directory-data.js +1 -2
  88. package/dist/src/symbols/index.js +1 -2
  89. package/dist/src/symbols/reference.d.ts +4 -1
  90. package/dist/src/symbols/reference.d.ts.map +1 -1
  91. package/dist/src/symbols/reference.js +13 -8
  92. package/dist/src/symbols/scopes.js +1 -2
  93. package/dist/src/symbols/ts-member-scope.js +1 -2
  94. package/dist/src/symbols/ts-module-scope.d.ts +4 -1
  95. package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
  96. package/dist/src/symbols/ts-module-scope.js +11 -5
  97. package/dist/src/symbols/ts-output-symbol.d.ts +2 -1
  98. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  99. package/dist/src/symbols/ts-output-symbol.js +2 -2
  100. package/dist/src/symbols/ts-package-scope.js +1 -2
  101. package/dist/src/utils.js +1 -2
  102. package/dist/test/array.test.js +105 -0
  103. package/dist/test/arrow-function.test.d.ts +2 -0
  104. package/dist/test/arrow-function.test.d.ts.map +1 -0
  105. package/dist/test/arrow-function.test.js +194 -0
  106. package/dist/test/barrel.test.js +53 -0
  107. package/dist/test/basic.test.js +91 -0
  108. package/dist/test/block-scope.test.js +30 -0
  109. package/dist/test/class.test.js +438 -0
  110. package/dist/test/enum-jsdoc.test.js +88 -0
  111. package/dist/test/enum.test.js +247 -0
  112. package/dist/test/externals.test.js +145 -0
  113. package/dist/test/function-call-expression.test.js +87 -0
  114. package/dist/test/function-declaration-jsdoc.test.js +91 -0
  115. package/dist/test/function-declaration.test.js +228 -0
  116. package/dist/test/function-expression.test.d.ts +2 -0
  117. package/dist/test/function-expression.test.d.ts.map +1 -0
  118. package/dist/test/function-expression.test.js +194 -0
  119. package/dist/test/function-type.test.d.ts +2 -0
  120. package/dist/test/function-type.test.d.ts.map +1 -0
  121. package/dist/test/function-type.test.js +122 -0
  122. package/dist/test/if-statement.test.js +25 -0
  123. package/dist/test/imports.test.d.ts.map +1 -1
  124. package/dist/test/imports.test.js +640 -0
  125. package/dist/test/interface-jsdoc.test.js +178 -0
  126. package/dist/test/interface.test.d.ts.map +1 -1
  127. package/dist/test/interface.test.js +490 -0
  128. package/dist/test/jscomment.test.js +70 -0
  129. package/dist/test/jsdoc.test.js +134 -0
  130. package/dist/test/jsdocparam.test.js +164 -0
  131. package/dist/test/member-chain.test.js +141 -0
  132. package/dist/test/member-expression.test.js +575 -0
  133. package/dist/test/name-conflict.test.js +108 -0
  134. package/dist/test/name-policy.test.js +122 -0
  135. package/dist/test/package-external.test.js +196 -0
  136. package/dist/test/package-json.test.js +93 -0
  137. package/dist/test/package.test.js +136 -0
  138. package/dist/test/reference.test.js +81 -0
  139. package/dist/test/source-file.test.js +36 -0
  140. package/dist/test/switch-statement.test.js +61 -0
  141. package/dist/test/utils.js +45 -0
  142. package/dist/test/value-expression.test.js +30 -0
  143. package/dist/test/values.test.js +351 -0
  144. package/dist/test/var-declaration-jsdoc.test.js +50 -0
  145. package/dist/test/var-declaration.test.js +63 -0
  146. package/dist/test/vitest.d.js +1 -0
  147. package/dist/tsconfig.tsbuildinfo +1 -1
  148. package/package.json +13 -18
  149. package/src/components/ArrowFunction.tsx +87 -0
  150. package/src/components/ClassDeclaration.tsx +19 -3
  151. package/src/components/Declaration.tsx +1 -1
  152. package/src/components/FunctionBase.tsx +282 -0
  153. package/src/components/FunctionDeclaration.tsx +13 -195
  154. package/src/components/FunctionExpression.tsx +86 -0
  155. package/src/components/FunctionType.tsx +93 -0
  156. package/src/components/ImportStatement.tsx +30 -3
  157. package/src/components/Interface.tsx +58 -23
  158. package/src/components/InterfaceMethod.tsx +96 -0
  159. package/src/components/JSDocComment.tsx +1 -1
  160. package/src/components/MemberDeclaration.tsx +76 -0
  161. package/src/components/MemberExpression.tsx +7 -2
  162. package/src/components/Reference.tsx +12 -2
  163. package/src/components/TypeDeclaration.tsx +16 -13
  164. package/src/components/TypeRefContext.tsx +49 -0
  165. package/src/components/VarDeclaration.tsx +6 -2
  166. package/src/components/index.ts +11 -0
  167. package/src/components/stc/index.ts +8 -0
  168. package/src/context/type-ref-context.tsx +19 -0
  169. package/src/parameter-descriptor.ts +35 -17
  170. package/src/symbols/reference.ts +21 -6
  171. package/src/symbols/ts-module-scope.ts +19 -4
  172. package/src/symbols/ts-output-symbol.ts +1 -0
  173. package/temp/api.json +4999 -2681
  174. package/test/arrow-function.test.tsx +188 -0
  175. package/test/class.test.tsx +5 -6
  176. package/test/enum-jsdoc.test.tsx +6 -6
  177. package/test/function-call-expression.test.tsx +1 -2
  178. package/test/function-declaration-jsdoc.test.tsx +3 -3
  179. package/test/function-declaration.test.tsx +1 -2
  180. package/test/function-expression.test.tsx +195 -0
  181. package/test/function-type.test.tsx +118 -0
  182. package/test/imports.test.tsx +165 -1
  183. package/test/interface-jsdoc.test.tsx +9 -9
  184. package/test/interface.test.tsx +256 -6
  185. package/test/jsdoc.test.tsx +5 -5
  186. package/test/jsdocparam.test.tsx +8 -8
  187. package/test/member-expression.test.tsx +171 -2
  188. package/test/var-declaration-jsdoc.test.tsx +3 -3
  189. package/test/var-declaration.test.tsx +1 -1
  190. package/tsdoc-metadata.json +1 -1
  191. package/babel.config.cjs +0 -4
  192. package/dist/src/builtins/index.js.map +0 -1
  193. package/dist/src/builtins/node.js.map +0 -1
  194. package/dist/src/components/ArrayExpression.js.map +0 -1
  195. package/dist/src/components/BarrelFile.js.map +0 -1
  196. package/dist/src/components/BlockScope.js.map +0 -1
  197. package/dist/src/components/CallSignature.js.map +0 -1
  198. package/dist/src/components/ClassDeclaration.js.map +0 -1
  199. package/dist/src/components/CommaList.js.map +0 -1
  200. package/dist/src/components/Declaration.js.map +0 -1
  201. package/dist/src/components/EnumDeclaration.js.map +0 -1
  202. package/dist/src/components/EnumMember.js.map +0 -1
  203. package/dist/src/components/ExportStatement.js.map +0 -1
  204. package/dist/src/components/FunctionCallExpression.js.map +0 -1
  205. package/dist/src/components/FunctionDeclaration.js.map +0 -1
  206. package/dist/src/components/IfStatement.js.map +0 -1
  207. package/dist/src/components/ImportStatement.js.map +0 -1
  208. package/dist/src/components/Interface.js.map +0 -1
  209. package/dist/src/components/JSDoc.js.map +0 -1
  210. package/dist/src/components/JSDocComment.js.map +0 -1
  211. package/dist/src/components/JSDocExample.js.map +0 -1
  212. package/dist/src/components/JSDocParam.js.map +0 -1
  213. package/dist/src/components/MemberChainExpression.js.map +0 -1
  214. package/dist/src/components/MemberExpression.js.map +0 -1
  215. package/dist/src/components/ObjectExpression.js.map +0 -1
  216. package/dist/src/components/PackageDirectory.js.map +0 -1
  217. package/dist/src/components/PackageJson.js.map +0 -1
  218. package/dist/src/components/PropertyName.js.map +0 -1
  219. package/dist/src/components/Reference.js.map +0 -1
  220. package/dist/src/components/SingleLineCommentBlock.js.map +0 -1
  221. package/dist/src/components/SourceFile.js.map +0 -1
  222. package/dist/src/components/SwitchStatement.js.map +0 -1
  223. package/dist/src/components/TsConfigJson.js.map +0 -1
  224. package/dist/src/components/TypeDeclaration.js.map +0 -1
  225. package/dist/src/components/ValueExpression.js.map +0 -1
  226. package/dist/src/components/VarDeclaration.js.map +0 -1
  227. package/dist/src/components/index.js.map +0 -1
  228. package/dist/src/components/stc/index.js.map +0 -1
  229. package/dist/src/context/index.js.map +0 -1
  230. package/dist/src/context/private-scope.js.map +0 -1
  231. package/dist/src/create-package.js.map +0 -1
  232. package/dist/src/index.js.map +0 -1
  233. package/dist/src/name-conflict-resolver.js.map +0 -1
  234. package/dist/src/name-policy.js.map +0 -1
  235. package/dist/src/parameter-descriptor.js.map +0 -1
  236. package/dist/src/source-directory-data.js.map +0 -1
  237. package/dist/src/symbols/index.js.map +0 -1
  238. package/dist/src/symbols/reference.js.map +0 -1
  239. package/dist/src/symbols/scopes.js.map +0 -1
  240. package/dist/src/symbols/ts-member-scope.js.map +0 -1
  241. package/dist/src/symbols/ts-module-scope.js.map +0 -1
  242. package/dist/src/symbols/ts-output-symbol.js.map +0 -1
  243. package/dist/src/symbols/ts-package-scope.js.map +0 -1
  244. package/dist/src/utils.js.map +0 -1
@@ -0,0 +1,640 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { List, Output, SourceDirectory, StatementList, refkey, render } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { describe, it } from "vitest";
5
+ import * as ts from "../src/components/index.js";
6
+ import { Reference } from "../src/components/Reference.js";
7
+ import { TypeRefContext } from "../src/context/type-ref-context.js";
8
+ import { tsNameConflictResolver } from "../src/name-conflict-resolver.js";
9
+ import { assertFileContents } from "./utils.js";
10
+ it("works with default imports", () => {
11
+ const res = render(_$createComponent(Output, {
12
+ get children() {
13
+ return [_$createComponent(ts.SourceFile, {
14
+ path: "test1.ts",
15
+ get children() {
16
+ return _$createComponent(ts.FunctionDeclaration, {
17
+ "export": true,
18
+ "default": true,
19
+ name: "asdf",
20
+ get refkey() {
21
+ return refkey("test");
22
+ }
23
+ });
24
+ }
25
+ }), _$createComponent(ts.SourceFile, {
26
+ path: "test2.ts",
27
+ get children() {
28
+ return ["const v = ", _$createComponent(Reference, {
29
+ get refkey() {
30
+ return refkey("test");
31
+ }
32
+ }), ";"];
33
+ }
34
+ })];
35
+ }
36
+ }));
37
+ assertFileContents(res, {
38
+ "test1.ts": `
39
+ export default function asdf() {}
40
+ `,
41
+ "test2.ts": `
42
+ import asdf from "./test1.js";
43
+
44
+ const v = asdf;
45
+ `
46
+ });
47
+ });
48
+ it("works with named imports", () => {
49
+ const res = render(_$createComponent(Output, {
50
+ get children() {
51
+ return [_$createComponent(ts.SourceFile, {
52
+ path: "test1.ts",
53
+ get children() {
54
+ return _$createComponent(ts.FunctionDeclaration, {
55
+ "export": true,
56
+ name: "test",
57
+ get refkey() {
58
+ return refkey("test");
59
+ }
60
+ });
61
+ }
62
+ }), _$createComponent(ts.SourceFile, {
63
+ path: "test2.ts",
64
+ get children() {
65
+ return ["const v = ", _$createComponent(Reference, {
66
+ get refkey() {
67
+ return refkey("test");
68
+ }
69
+ }), ";"];
70
+ }
71
+ })];
72
+ }
73
+ }));
74
+ assertFileContents(res, {
75
+ "test1.ts": `
76
+ export function test() {}
77
+ `,
78
+ "test2.ts": `
79
+ import { test } from "./test1.js";
80
+
81
+ const v = test;
82
+ `
83
+ });
84
+ });
85
+ it("works with default and named imports", () => {
86
+ const res = render(_$createComponent(Output, {
87
+ get children() {
88
+ return [_$createComponent(ts.SourceFile, {
89
+ path: "test1.ts",
90
+ get children() {
91
+ return [_$createComponent(ts.FunctionDeclaration, {
92
+ "export": true,
93
+ "default": true,
94
+ name: "test1",
95
+ get refkey() {
96
+ return refkey("test1");
97
+ }
98
+ }), _$createIntrinsic("hbr", {}), _$createComponent(ts.FunctionDeclaration, {
99
+ "export": true,
100
+ name: "test2",
101
+ get refkey() {
102
+ return refkey("test2");
103
+ }
104
+ })];
105
+ }
106
+ }), _$createComponent(ts.SourceFile, {
107
+ path: "test2.ts",
108
+ get children() {
109
+ return ["const v1 = ", _$createComponent(Reference, {
110
+ get refkey() {
111
+ return refkey("test1");
112
+ }
113
+ }), ";", _$createIntrinsic("hbr", {}), "const v2 = ", _$createComponent(Reference, {
114
+ get refkey() {
115
+ return refkey("test2");
116
+ }
117
+ }), ";"];
118
+ }
119
+ })];
120
+ }
121
+ }));
122
+ assertFileContents(res, {
123
+ "test1.ts": `
124
+ export default function test1() {}
125
+ export function test2() {}
126
+ `,
127
+ "test2.ts": `
128
+ import test1, { test2 } from "./test1.js";
129
+
130
+ const v1 = test1;
131
+ const v2 = test2;
132
+ `
133
+ });
134
+ });
135
+ it("works with default and named imports and name conflicts", () => {
136
+ const res = render(_$createComponent(Output, {
137
+ nameConflictResolver: tsNameConflictResolver,
138
+ get children() {
139
+ return [_$createComponent(ts.SourceFile, {
140
+ path: "test1.ts",
141
+ get children() {
142
+ return [_$createComponent(ts.FunctionDeclaration, {
143
+ "export": true,
144
+ "default": true,
145
+ name: "test1",
146
+ get refkey() {
147
+ return refkey("test1");
148
+ }
149
+ }), _$createIntrinsic("br", {}), _$createComponent(ts.FunctionDeclaration, {
150
+ "export": true,
151
+ name: "test2",
152
+ get refkey() {
153
+ return refkey("test2");
154
+ }
155
+ })];
156
+ }
157
+ }), _$createComponent(ts.SourceFile, {
158
+ path: "test2.ts",
159
+ get children() {
160
+ return [_$createComponent(ts.FunctionDeclaration, {
161
+ "export": true,
162
+ "default": true,
163
+ name: "test1",
164
+ get refkey() {
165
+ return refkey("test3");
166
+ }
167
+ }), _$createIntrinsic("br", {}), _$createComponent(ts.FunctionDeclaration, {
168
+ "export": true,
169
+ name: "test2",
170
+ get refkey() {
171
+ return refkey("test4");
172
+ }
173
+ })];
174
+ }
175
+ }), _$createComponent(ts.SourceFile, {
176
+ path: "test3.ts",
177
+ get children() {
178
+ return _$createComponent(StatementList, {
179
+ get children() {
180
+ return [["const v1 = ", _$createComponent(Reference, {
181
+ get refkey() {
182
+ return refkey("test1");
183
+ }
184
+ })], ["const v1_1 = ", _$createComponent(Reference, {
185
+ get refkey() {
186
+ return refkey("test2");
187
+ }
188
+ })], ["const v2 = ", _$createComponent(Reference, {
189
+ get refkey() {
190
+ return refkey("test3");
191
+ }
192
+ })], ["const v3 = ", _$createComponent(Reference, {
193
+ get refkey() {
194
+ return refkey("test3");
195
+ }
196
+ })], ["const v4 = ", _$createComponent(Reference, {
197
+ get refkey() {
198
+ return refkey("test4");
199
+ }
200
+ })]];
201
+ }
202
+ });
203
+ }
204
+ })];
205
+ }
206
+ }));
207
+ assertFileContents(res, {
208
+ "test1.ts": `
209
+ export default function test1() {}
210
+ export function test2() {}
211
+ `,
212
+ "test2.ts": `
213
+ export default function test1() {}
214
+ export function test2() {}
215
+ `,
216
+ "test3.ts": `
217
+ import test1_1, { test2 as test2_1 } from "./test1.js";
218
+ import test1_2, { test2 as test2_2 } from "./test2.js";
219
+
220
+ const v1 = test1_1;
221
+ const v1_1 = test2_1;
222
+ const v2 = test1_2;
223
+ const v3 = test1_2;
224
+ const v4 = test2_2;
225
+ `
226
+ });
227
+ });
228
+ it("works with default and named imports and name conflicts and references in nested scopes", () => {
229
+ const res = render(_$createComponent(Output, {
230
+ nameConflictResolver: tsNameConflictResolver,
231
+ get children() {
232
+ return [_$createComponent(ts.SourceFile, {
233
+ path: "test1.ts",
234
+ get children() {
235
+ return [_$createComponent(ts.FunctionDeclaration, {
236
+ "export": true,
237
+ "default": true,
238
+ name: "test1",
239
+ get refkey() {
240
+ return refkey("test1");
241
+ }
242
+ }), _$createIntrinsic("hbr", {}), _$createComponent(ts.FunctionDeclaration, {
243
+ "export": true,
244
+ name: "test2",
245
+ get refkey() {
246
+ return refkey("test2");
247
+ }
248
+ })];
249
+ }
250
+ }), _$createComponent(ts.SourceFile, {
251
+ path: "test2.ts",
252
+ get children() {
253
+ return [_$createComponent(ts.FunctionDeclaration, {
254
+ "export": true,
255
+ "default": true,
256
+ name: "test1",
257
+ get refkey() {
258
+ return refkey("test3");
259
+ }
260
+ }), _$createIntrinsic("hbr", {}), _$createComponent(ts.FunctionDeclaration, {
261
+ "export": true,
262
+ name: "test2",
263
+ get refkey() {
264
+ return refkey("test4");
265
+ }
266
+ })];
267
+ }
268
+ }), _$createComponent(ts.SourceFile, {
269
+ path: "test3.ts",
270
+ get children() {
271
+ return [_$createComponent(StatementList, {
272
+ get children() {
273
+ return [["const v1 = ", _$createComponent(Reference, {
274
+ get refkey() {
275
+ return refkey("test1");
276
+ }
277
+ })], ["const v1_1 = ", _$createComponent(Reference, {
278
+ get refkey() {
279
+ return refkey("test2");
280
+ }
281
+ })]];
282
+ }
283
+ }), _$createIntrinsic("hbr", {}), _$createComponent(ts.FunctionDeclaration, {
284
+ name: "foo",
285
+ get children() {
286
+ return _$createComponent(StatementList, {
287
+ get children() {
288
+ return [["const v2 = ", _$createComponent(Reference, {
289
+ get refkey() {
290
+ return refkey("test3");
291
+ }
292
+ })], ["const v3 = ", _$createComponent(Reference, {
293
+ get refkey() {
294
+ return refkey("test3");
295
+ }
296
+ })], ["const v4 = ", _$createComponent(Reference, {
297
+ get refkey() {
298
+ return refkey("test4");
299
+ }
300
+ })]];
301
+ }
302
+ });
303
+ }
304
+ })];
305
+ }
306
+ })];
307
+ }
308
+ }));
309
+ assertFileContents(res, {
310
+ "test1.ts": `
311
+ export default function test1() {}
312
+ export function test2() {}
313
+ `,
314
+ "test2.ts": `
315
+ export default function test1() {}
316
+ export function test2() {}
317
+ `,
318
+ "test3.ts": `
319
+ import test1_1, { test2 as test2_1 } from "./test1.js";
320
+ import test1_2, { test2 as test2_2 } from "./test2.js";
321
+
322
+ const v1 = test1_1;
323
+ const v1_1 = test2_1;
324
+ function foo() {
325
+ const v2 = test1_2;
326
+ const v3 = test1_2;
327
+ const v4 = test2_2;
328
+ }
329
+ `
330
+ });
331
+ });
332
+ it("works with imports from different directories", () => {
333
+ const res = render(_$createComponent(Output, {
334
+ get children() {
335
+ return [_$createComponent(SourceDirectory, {
336
+ path: "src",
337
+ get children() {
338
+ return _$createComponent(ts.SourceFile, {
339
+ path: "test1.ts",
340
+ get children() {
341
+ return [_$createComponent(ts.FunctionDeclaration, {
342
+ "export": true,
343
+ name: "test",
344
+ get refkey() {
345
+ return refkey("test");
346
+ }
347
+ }), _$createIntrinsic("hbr", {}), "const v = ", _$createComponent(Reference, {
348
+ get refkey() {
349
+ return refkey("test2");
350
+ }
351
+ }), ";"];
352
+ }
353
+ });
354
+ }
355
+ }), _$createComponent(ts.SourceFile, {
356
+ path: "test2.ts",
357
+ get children() {
358
+ return ["const v = ", _$createComponent(Reference, {
359
+ get refkey() {
360
+ return refkey("test");
361
+ }
362
+ }), ";", _$createIntrinsic("hbr", {}), _$createComponent(ts.FunctionDeclaration, {
363
+ "export": true,
364
+ name: "test2",
365
+ get refkey() {
366
+ return refkey("test2");
367
+ }
368
+ })];
369
+ }
370
+ })];
371
+ }
372
+ }));
373
+ assertFileContents(res, {
374
+ "src/test1.ts": `
375
+ import { test2 } from "../test2.js";
376
+
377
+ export function test() {}
378
+ const v = test2;
379
+ `,
380
+ "test2.ts": `
381
+ import { test } from "./src/test1.js";
382
+
383
+ const v = test;
384
+ export function test2() {}
385
+ `
386
+ });
387
+ });
388
+ it("handles conflicts with local declarations", () => {
389
+ const res = render(_$createComponent(Output, {
390
+ nameConflictResolver: tsNameConflictResolver,
391
+ get children() {
392
+ return [_$createComponent(SourceDirectory, {
393
+ path: "src",
394
+ get children() {
395
+ return _$createComponent(ts.SourceFile, {
396
+ path: "test1.ts",
397
+ get children() {
398
+ return [_$createComponent(ts.FunctionDeclaration, {
399
+ "export": true,
400
+ name: "test",
401
+ get refkey() {
402
+ return refkey("test");
403
+ }
404
+ }), _$createIntrinsic("hbr", {}), "const v = ", _$createComponent(Reference, {
405
+ get refkey() {
406
+ return refkey("test2");
407
+ }
408
+ }), ";"];
409
+ }
410
+ });
411
+ }
412
+ }), _$createComponent(ts.SourceFile, {
413
+ path: "test2.ts",
414
+ get children() {
415
+ return ["const v = ", _$createComponent(Reference, {
416
+ get refkey() {
417
+ return refkey("test");
418
+ }
419
+ }), ";", _$createIntrinsic("hbr", {}), _$createComponent(ts.FunctionDeclaration, {
420
+ "export": true,
421
+ name: "test"
422
+ })];
423
+ }
424
+ })];
425
+ }
426
+ }));
427
+ assertFileContents(res, {
428
+ "test2.ts": `
429
+ import { test as test_1 } from "./src/test1.js";
430
+
431
+ const v = test_1;
432
+ export function test() {}
433
+ `
434
+ });
435
+ });
436
+ describe("type imports", () => {
437
+ function mkTestFile(name) {
438
+ const TypeA = refkey("TypeA");
439
+ const TypeB = refkey("TypeB");
440
+ const ClassA = refkey("ClassA");
441
+ return {
442
+ component: _$createComponent(ts.SourceFile, {
443
+ path: "test1.ts",
444
+ get children() {
445
+ return [_$createComponent(ts.InterfaceDeclaration, {
446
+ "export": true,
447
+ name: "TypeA",
448
+ refkey: TypeA
449
+ }), _$createComponent(ts.InterfaceDeclaration, {
450
+ "export": true,
451
+ name: "TypeB",
452
+ refkey: TypeB
453
+ }), _$createComponent(ts.ClassDeclaration, {
454
+ "export": true,
455
+ name: "ClassA",
456
+ refkey: ClassA
457
+ })];
458
+ }
459
+ }),
460
+ TypeA,
461
+ TypeB,
462
+ ClassA
463
+ };
464
+ }
465
+ it("adds type keyword only to type imports", () => {
466
+ const {
467
+ component,
468
+ TypeA,
469
+ ClassA
470
+ } = mkTestFile("test1.ts");
471
+ const res = render(_$createComponent(Output, {
472
+ get children() {
473
+ return [component, _$createComponent(ts.SourceFile, {
474
+ path: "test2.ts",
475
+ get children() {
476
+ return _$createComponent(List, {
477
+ get children() {
478
+ return [["type A = ", _$createComponent(Reference, {
479
+ refkey: TypeA,
480
+ type: true
481
+ })], ["class B extends ", _$createComponent(Reference, {
482
+ refkey: ClassA
483
+ }), " ", "{}"]];
484
+ }
485
+ });
486
+ }
487
+ })];
488
+ }
489
+ }));
490
+ assertFileContents(res, {
491
+ "test2.ts": `
492
+ import { type TypeA, ClassA } from "./test1.js";
493
+
494
+ type A = TypeA
495
+ class B extends ClassA {}
496
+ `
497
+ });
498
+ });
499
+ it("add type keyword for whole import if all are types", () => {
500
+ const {
501
+ component,
502
+ TypeA,
503
+ TypeB
504
+ } = mkTestFile("test1.ts");
505
+ const res = render(_$createComponent(Output, {
506
+ get children() {
507
+ return [component, _$createComponent(ts.SourceFile, {
508
+ path: "test2.ts",
509
+ get children() {
510
+ return _$createComponent(List, {
511
+ get children() {
512
+ return [["type A = ", _$createComponent(Reference, {
513
+ refkey: TypeA,
514
+ type: true
515
+ })], ["type B = ", _$createComponent(Reference, {
516
+ refkey: TypeB,
517
+ type: true
518
+ })]];
519
+ }
520
+ });
521
+ }
522
+ })];
523
+ }
524
+ }));
525
+ assertFileContents(res, {
526
+ "test2.ts": `
527
+ import type { TypeA, TypeB } from "./test1.js";
528
+
529
+ type A = TypeA
530
+ type B = TypeB
531
+ `
532
+ });
533
+ });
534
+ it("reference same type multiple times", () => {
535
+ const {
536
+ component,
537
+ TypeA
538
+ } = mkTestFile("test1.ts");
539
+ const res = render(_$createComponent(Output, {
540
+ get children() {
541
+ return [component, _$createComponent(ts.SourceFile, {
542
+ path: "test2.ts",
543
+ get children() {
544
+ return _$createComponent(List, {
545
+ get children() {
546
+ return [["type A = ", _$createComponent(Reference, {
547
+ refkey: TypeA,
548
+ type: true
549
+ })], ["type B = ", _$createComponent(Reference, {
550
+ refkey: TypeA,
551
+ type: true
552
+ })]];
553
+ }
554
+ });
555
+ }
556
+ })];
557
+ }
558
+ }));
559
+ assertFileContents(res, {
560
+ "test2.ts": `
561
+ import type { TypeA } from "./test1.js";
562
+
563
+ type A = TypeA
564
+ type B = TypeA
565
+ `
566
+ });
567
+ });
568
+ it("same reference used as both type and non type doesn't include type", () => {
569
+ const {
570
+ component,
571
+ ClassA
572
+ } = mkTestFile("test1.ts");
573
+ const res = render(_$createComponent(Output, {
574
+ get children() {
575
+ return [component, _$createComponent(ts.SourceFile, {
576
+ path: "test2.ts",
577
+ get children() {
578
+ return _$createComponent(List, {
579
+ get children() {
580
+ return [["type A = ", _$createComponent(Reference, {
581
+ refkey: ClassA,
582
+ type: true
583
+ })], ["class B extends ", _$createComponent(Reference, {
584
+ refkey: ClassA
585
+ }), " ", "{}"]];
586
+ }
587
+ });
588
+ }
589
+ })];
590
+ }
591
+ }));
592
+ assertFileContents(res, {
593
+ "test2.ts": `
594
+ import { ClassA } from "./test1.js";
595
+
596
+ type A = ClassA
597
+ class B extends ClassA {}
598
+ `
599
+ });
600
+ });
601
+ it("infer if a type reference from the typescript context", () => {
602
+ const {
603
+ component,
604
+ TypeA
605
+ } = mkTestFile("test1.ts");
606
+ const res = render(_$createComponent(Output, {
607
+ get children() {
608
+ return [component, _$createComponent(ts.SourceFile, {
609
+ path: "test2.ts",
610
+ get children() {
611
+ return _$createComponent(TypeRefContext.Provider, {
612
+ value: {
613
+ type: true
614
+ },
615
+ get children() {
616
+ return _$createComponent(List, {
617
+ get children() {
618
+ return [["type A = ", _$createComponent(Reference, {
619
+ refkey: TypeA
620
+ })], ["type B = ", _$createComponent(Reference, {
621
+ refkey: TypeA
622
+ })]];
623
+ }
624
+ });
625
+ }
626
+ });
627
+ }
628
+ })];
629
+ }
630
+ }));
631
+ assertFileContents(res, {
632
+ "test2.ts": `
633
+ import type { TypeA } from "./test1.js";
634
+
635
+ type A = TypeA
636
+ type B = TypeA
637
+ `
638
+ });
639
+ });
640
+ });