@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,188 @@
1
+ import { Props, refkey, StatementList } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { describe, expect, it } from "vitest";
4
+ import { ArrowFunction } from "../src/components/ArrowFunction.jsx";
5
+ import { VarDeclaration } from "../src/index.js";
6
+ import { ParameterDescriptor } from "../src/parameter-descriptor.js";
7
+ import { toSourceText } from "./utils.jsx";
8
+
9
+ it("create basic function", () => {
10
+ expect(toSourceText(<ArrowFunction />)).toBe(d`
11
+ () => {}
12
+ `);
13
+ });
14
+
15
+ it("can be an async function", () => {
16
+ expect(toSourceText(<ArrowFunction async />)).toBe(d`
17
+ async () => {}
18
+ `);
19
+ });
20
+
21
+ it("can be an async with returnType", () => {
22
+ expect(toSourceText(<ArrowFunction async returnType="Foo" />)).toBe(d`
23
+ async (): Promise<Foo> => {}
24
+ `);
25
+ });
26
+
27
+ it("can be an async with returnType element", () => {
28
+ function Foo(_props?: Props) {
29
+ return <>Foo</>;
30
+ }
31
+ expect(toSourceText(<ArrowFunction async returnType={<Foo />} />)).toBe(d`
32
+ async (): Promise<Foo> => {}
33
+ `);
34
+ });
35
+
36
+ it("supports parameters by element", () => {
37
+ const decl = (
38
+ <ArrowFunction>
39
+ return a + b;
40
+ <ArrowFunction.Parameters>a, b</ArrowFunction.Parameters>
41
+ </ArrowFunction>
42
+ );
43
+
44
+ expect(toSourceText(decl)).toBe(d`
45
+ (a, b) => {
46
+ return a + b;
47
+ }
48
+ `);
49
+ });
50
+
51
+ it("supports type parameters by descriptor object", () => {
52
+ const decl = (
53
+ <ArrowFunction
54
+ typeParameters={[
55
+ { name: "a", extends: "any" },
56
+ { name: "b", extends: "any" },
57
+ ]}
58
+ ></ArrowFunction>
59
+ );
60
+
61
+ expect(toSourceText(decl)).toBe(d`
62
+ <a extends any, b extends any>() => {}
63
+ `);
64
+ });
65
+
66
+ it("supports type parameters by descriptor array", () => {
67
+ const decl = <ArrowFunction typeParameters={["a", "b"]}></ArrowFunction>;
68
+
69
+ expect(toSourceText(decl)).toBe(d`
70
+ <a, b>() => {}
71
+ `);
72
+ });
73
+
74
+ it("supports type parameters by element", () => {
75
+ const decl = (
76
+ <ArrowFunction>
77
+ <ArrowFunction.TypeParameters>a, b</ArrowFunction.TypeParameters>
78
+ </ArrowFunction>
79
+ );
80
+
81
+ expect(toSourceText(decl)).toBe(d`
82
+ <a, b>() => {}
83
+ `);
84
+ });
85
+
86
+ describe("symbols", () => {
87
+ it("creates a nested scope", () => {
88
+ const innerRefkey = refkey();
89
+ const outerRefkey = refkey();
90
+ const decl = (
91
+ <StatementList>
92
+ <ArrowFunction>
93
+ <StatementList>
94
+ {innerRefkey}
95
+ <VarDeclaration name="refme" refkey={innerRefkey}>
96
+ 1
97
+ </VarDeclaration>
98
+ </StatementList>
99
+ </ArrowFunction>
100
+ <VarDeclaration name="refme" refkey={outerRefkey}>
101
+ 2
102
+ </VarDeclaration>
103
+ {outerRefkey}
104
+ </StatementList>
105
+ );
106
+ expect(toSourceText(decl)).toBe(d`
107
+ () => {
108
+ refme;
109
+ const refme = 1;
110
+ };
111
+ const refme = 2;
112
+ refme;
113
+ `);
114
+ });
115
+
116
+ it("throws an error when trying to access a symbol in a nested scope", () => {
117
+ const innerRefkey = refkey();
118
+ const decl = (
119
+ <>
120
+ <ArrowFunction>
121
+ <VarDeclaration name="refme" refkey={innerRefkey}>
122
+ 1
123
+ </VarDeclaration>
124
+ </ArrowFunction>
125
+ ;{innerRefkey}
126
+ </>
127
+ );
128
+ expect(() => toSourceText(decl)).toThrow(/Cannot reference a symbol/);
129
+ });
130
+
131
+ it("creates symbols for parameters", () => {
132
+ const rk = refkey();
133
+
134
+ const decl = (
135
+ <>
136
+ <ArrowFunction parameters={[{ name: "sym", type: "any", refkey: rk }]}>
137
+ <ArrowFunction>{rk}</ArrowFunction>
138
+ </ArrowFunction>
139
+ </>
140
+ );
141
+
142
+ expect(toSourceText(decl)).toBe(d`
143
+ (sym: any) => {
144
+ () => {
145
+ sym
146
+ }
147
+ }
148
+ `);
149
+ });
150
+
151
+ it("creates symbols for parameters and addresses conflicts", () => {
152
+ const decl = (
153
+ <>
154
+ <ArrowFunction parameters={[{ name: "conflict", type: "any" }]}>
155
+ <VarDeclaration name="conflict">1</VarDeclaration>;
156
+ </ArrowFunction>
157
+ </>
158
+ );
159
+
160
+ expect(toSourceText(decl)).toBe(d`
161
+ (conflict: any) => {
162
+ const conflict_2 = 1;
163
+ }
164
+ `);
165
+ });
166
+
167
+ it("create optional parameters", () => {
168
+ const paramDesc: ParameterDescriptor = {
169
+ name: "foo",
170
+ refkey: refkey(),
171
+ type: "any",
172
+ optional: true,
173
+ };
174
+ const decl = (
175
+ <>
176
+ <ArrowFunction parameters={[paramDesc]}>
177
+ console.log(foo);
178
+ </ArrowFunction>
179
+ </>
180
+ );
181
+
182
+ expect(toSourceText(decl)).toBe(d`
183
+ (foo?: any) => {
184
+ console.log(foo);
185
+ }
186
+ `);
187
+ });
188
+ });
@@ -1,8 +1,7 @@
1
- import { List, refkey } from "@alloy-js/core";
1
+ import { List, refkey, StatementList } from "@alloy-js/core";
2
2
  import "@alloy-js/core/testing";
3
3
  import { d } from "@alloy-js/core/testing";
4
4
  import { describe, expect, it } from "vitest";
5
- import { StatementList } from "../../core/src/components/StatementList.jsx";
6
5
  import * as ts from "../src/components/index.js";
7
6
  import { toSourceText } from "./utils.js";
8
7
 
@@ -310,15 +309,15 @@ it("renders a class with docs for the class and its members", () => {
310
309
  expect(res).toEqual(d`
311
310
  /**
312
311
  * This is a class documentation
313
- **/
312
+ */
314
313
  class Foo {
315
314
  /**
316
315
  * This is a field documentation
317
- **/
316
+ */
318
317
  bar = 123;
319
318
  /**
320
319
  * This is a method documentation
321
- **/
320
+ */
322
321
  baz() {
323
322
  return 123;
324
323
  }
@@ -354,7 +353,7 @@ it("renders a method with parameter docs", () => {
354
353
  * @param {string} b - Line 1 for b.
355
354
  * This is a long description that
356
355
  * should continue in the next line.
357
- **/
356
+ */
358
357
  bar(a: number, b: string): void {}
359
358
  }
360
359
  `);
@@ -19,7 +19,7 @@ it("renders an enum declaration with documentation", () => {
19
19
  expect(res).toEqual(d`
20
20
  /**
21
21
  * Color enumeration for the application
22
- **/
22
+ */
23
23
  enum Color {
24
24
  Red,
25
25
  Green,
@@ -49,25 +49,25 @@ it("renders an enum declaration with documented members", () => {
49
49
  expect(res).toEqual(d`
50
50
  /**
51
51
  * Represents cardinal directions. Used for navigation purposes
52
- **/
52
+ */
53
53
  enum Direction {
54
54
  /**
55
55
  * Bound to the north
56
- **/
56
+ */
57
57
  North = 0,
58
58
  /**
59
59
  * Bound to the East
60
60
  *
61
61
  * A second doc paragraph
62
- **/
62
+ */
63
63
  East = 1,
64
64
  /**
65
65
  * Opposite to North
66
- **/
66
+ */
67
67
  South = 2,
68
68
  /**
69
69
  * Going west!
70
- **/
70
+ */
71
71
  West = 3,
72
72
  }
73
73
  `);
@@ -1,6 +1,5 @@
1
- import { Output, refkey, render } from "@alloy-js/core";
1
+ import { Output, refkey, render, StatementList } from "@alloy-js/core";
2
2
  import { it } from "vitest";
3
- import { StatementList } from "../../core/src/components/StatementList.jsx";
4
3
  import {
5
4
  FunctionCallExpression,
6
5
  FunctionDeclaration,
@@ -18,7 +18,7 @@ it("renders a function declaration with documentation", () => {
18
18
  * A function that greets a person
19
19
  *
20
20
  * @param {string} name
21
- **/
21
+ */
22
22
  function greet(name: string): string {}
23
23
  `);
24
24
  });
@@ -42,7 +42,7 @@ it("With documented parameters", () => {
42
42
  *
43
43
  * @param {number} price
44
44
  * @param {number} [taxRate]
45
- **/
45
+ */
46
46
  export function calculateTotal(price: number, taxRate?: number): number {}
47
47
  `);
48
48
  });
@@ -84,6 +84,6 @@ it("renders a function with a reference", () => {
84
84
  * Creates a new user
85
85
  *
86
86
  * @param {User} user - The user object to create
87
- **/
87
+ */
88
88
  function createUser(user: User): void {}`);
89
89
  });
@@ -1,7 +1,6 @@
1
- import { Props, refkey } from "@alloy-js/core";
1
+ import { Props, refkey, StatementList } from "@alloy-js/core";
2
2
  import { d } from "@alloy-js/core/testing";
3
3
  import { describe, expect, it } from "vitest";
4
- import { StatementList } from "../../core/src/components/StatementList.jsx";
5
4
  import { FunctionDeclaration, VarDeclaration } from "../src/index.js";
6
5
  import { ParameterDescriptor } from "../src/parameter-descriptor.js";
7
6
  import { toSourceText } from "./utils.js";
@@ -0,0 +1,195 @@
1
+ import { Props, refkey, StatementList } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { describe, expect, it } from "vitest";
4
+ import { FunctionExpression } from "../src/components/FunctionExpression.jsx";
5
+ import { VarDeclaration } from "../src/index.js";
6
+ import { ParameterDescriptor } from "../src/parameter-descriptor.js";
7
+ import { toSourceText } from "./utils.jsx";
8
+
9
+ it("create basic function", () => {
10
+ expect(toSourceText(<FunctionExpression />)).toBe(d`
11
+ function () {}
12
+ `);
13
+ });
14
+
15
+ it("can be an async function", () => {
16
+ expect(toSourceText(<FunctionExpression async />)).toBe(d`
17
+ async function () {}
18
+ `);
19
+ });
20
+
21
+ it("can be an async function with returnType", () => {
22
+ expect(toSourceText(<FunctionExpression async returnType="Foo" />)).toBe(d`
23
+ async function (): Promise<Foo> {}
24
+ `);
25
+ });
26
+
27
+ it("can be an async function with returnType element", () => {
28
+ function Foo(_props?: Props) {
29
+ return <>Foo</>;
30
+ }
31
+ expect(toSourceText(<FunctionExpression async returnType={<Foo />} />))
32
+ .toBe(d`
33
+ async function (): Promise<Foo> {}
34
+ `);
35
+ });
36
+
37
+ it("supports parameters by element", () => {
38
+ const decl = (
39
+ <FunctionExpression>
40
+ return a + b;
41
+ <FunctionExpression.Parameters>a, b</FunctionExpression.Parameters>
42
+ </FunctionExpression>
43
+ );
44
+
45
+ expect(toSourceText(decl)).toBe(d`
46
+ function (a, b) {
47
+ return a + b;
48
+ }
49
+ `);
50
+ });
51
+
52
+ it("supports type parameters by descriptor object", () => {
53
+ const decl = (
54
+ <FunctionExpression
55
+ typeParameters={[
56
+ { name: "a", extends: "any" },
57
+ { name: "b", extends: "any" },
58
+ ]}
59
+ ></FunctionExpression>
60
+ );
61
+
62
+ expect(toSourceText(decl)).toBe(d`
63
+ function <a extends any, b extends any>() {}
64
+ `);
65
+ });
66
+
67
+ it("supports type parameters by descriptor array", () => {
68
+ const decl = (
69
+ <FunctionExpression typeParameters={["a", "b"]}></FunctionExpression>
70
+ );
71
+
72
+ expect(toSourceText(decl)).toBe(d`
73
+ function <a, b>() {}
74
+ `);
75
+ });
76
+
77
+ it("supports type parameters by element", () => {
78
+ const decl = (
79
+ <FunctionExpression>
80
+ <FunctionExpression.TypeParameters>
81
+ a, b
82
+ </FunctionExpression.TypeParameters>
83
+ </FunctionExpression>
84
+ );
85
+
86
+ expect(toSourceText(decl)).toBe(d`
87
+ function <a, b>() {}
88
+ `);
89
+ });
90
+
91
+ describe("symbols", () => {
92
+ it("creates a nested scope", () => {
93
+ const innerRefkey = refkey();
94
+ const outerRefkey = refkey();
95
+ const decl = (
96
+ <StatementList>
97
+ <FunctionExpression>
98
+ <StatementList>
99
+ {innerRefkey}
100
+ <VarDeclaration name="refme" refkey={innerRefkey}>
101
+ 1
102
+ </VarDeclaration>
103
+ </StatementList>
104
+ </FunctionExpression>
105
+ <VarDeclaration name="refme" refkey={outerRefkey}>
106
+ 2
107
+ </VarDeclaration>
108
+ {outerRefkey}
109
+ </StatementList>
110
+ );
111
+ expect(toSourceText(decl)).toBe(d`
112
+ function () {
113
+ refme;
114
+ const refme = 1;
115
+ };
116
+ const refme = 2;
117
+ refme;
118
+ `);
119
+ });
120
+
121
+ it("throws an error when trying to access a symbol in a nested function scope", () => {
122
+ const innerRefkey = refkey();
123
+ const decl = (
124
+ <>
125
+ <FunctionExpression>
126
+ <VarDeclaration name="refme" refkey={innerRefkey}>
127
+ 1
128
+ </VarDeclaration>
129
+ </FunctionExpression>
130
+ ;{innerRefkey}
131
+ </>
132
+ );
133
+ expect(() => toSourceText(decl)).toThrow(/Cannot reference a symbol/);
134
+ });
135
+
136
+ it("creates symbols for parameters", () => {
137
+ const rk = refkey();
138
+
139
+ const decl = (
140
+ <>
141
+ <FunctionExpression
142
+ parameters={[{ name: "sym", type: "any", refkey: rk }]}
143
+ >
144
+ <FunctionExpression>{rk}</FunctionExpression>
145
+ </FunctionExpression>
146
+ </>
147
+ );
148
+
149
+ expect(toSourceText(decl)).toBe(d`
150
+ function (sym: any) {
151
+ function () {
152
+ sym
153
+ }
154
+ }
155
+ `);
156
+ });
157
+
158
+ it("creates symbols for parameters and addresses conflicts", () => {
159
+ const decl = (
160
+ <>
161
+ <FunctionExpression parameters={[{ name: "conflict", type: "any" }]}>
162
+ <VarDeclaration name="conflict">1</VarDeclaration>;
163
+ </FunctionExpression>
164
+ </>
165
+ );
166
+
167
+ expect(toSourceText(decl)).toBe(d`
168
+ function (conflict: any) {
169
+ const conflict_2 = 1;
170
+ }
171
+ `);
172
+ });
173
+
174
+ it("create optional parameters", () => {
175
+ const paramDesc: ParameterDescriptor = {
176
+ name: "foo",
177
+ refkey: refkey(),
178
+ type: "any",
179
+ optional: true,
180
+ };
181
+ const decl = (
182
+ <>
183
+ <FunctionExpression parameters={[paramDesc]}>
184
+ console.log(foo);
185
+ </FunctionExpression>
186
+ </>
187
+ );
188
+
189
+ expect(toSourceText(decl)).toBe(d`
190
+ function (foo?: any) {
191
+ console.log(foo);
192
+ }
193
+ `);
194
+ });
195
+ });
@@ -0,0 +1,118 @@
1
+ import { Props, refkey } from "@alloy-js/core";
2
+ import "@alloy-js/core/testing";
3
+ import { d } from "@alloy-js/core/testing";
4
+ import { describe, expect, it } from "vitest";
5
+ import { FunctionType } from "../src/components/FunctionType.jsx";
6
+ import * as ts from "../src/components/index.js";
7
+ import { ParameterDescriptor } from "../src/components/index.js";
8
+ import { toSourceText } from "./utils.jsx";
9
+
10
+ it("render basic", () => {
11
+ expect(toSourceText(<FunctionType />)).toBe(d`
12
+ () => void
13
+ `);
14
+ });
15
+
16
+ it("render in interface", () => {
17
+ expect(
18
+ toSourceText(
19
+ <ts.InterfaceDeclaration name="Foo">
20
+ <ts.InterfaceMember name="foo" type={<FunctionType />} />
21
+ </ts.InterfaceDeclaration>,
22
+ ),
23
+ ).toBe(d`
24
+ interface Foo {
25
+ foo: () => void
26
+ }
27
+ `);
28
+ });
29
+
30
+ describe("marking it as async", () => {
31
+ it("no return type change to Promise<void>", () => {
32
+ expect(toSourceText(<FunctionType async />)).toBe(d`
33
+ () => Promise<void>
34
+ `);
35
+ });
36
+
37
+ it("explicit returnType change to Promise<T>", () => {
38
+ expect(toSourceText(<FunctionType async returnType="Foo" />)).toBe(d`
39
+ () => Promise<Foo>
40
+ `);
41
+ });
42
+
43
+ it("component returnType change to Promise<T>", () => {
44
+ function Foo(_props?: Props) {
45
+ return <>Foo</>;
46
+ }
47
+ expect(toSourceText(<FunctionType async returnType={<Foo />} />)).toBe(d`
48
+ () => Promise<Foo>
49
+ `);
50
+ });
51
+ });
52
+
53
+ it("supports parameters by element", () => {
54
+ const decl = (
55
+ <FunctionType>
56
+ <FunctionType.Parameters>a, b</FunctionType.Parameters>
57
+ </FunctionType>
58
+ );
59
+
60
+ expect(toSourceText(decl)).toBe(d`
61
+ (a, b) => void
62
+ `);
63
+ });
64
+
65
+ it("supports type parameters by descriptor object", () => {
66
+ const decl = (
67
+ <FunctionType
68
+ typeParameters={[
69
+ { name: "a", extends: "any" },
70
+ { name: "b", extends: "any" },
71
+ ]}
72
+ ></FunctionType>
73
+ );
74
+
75
+ expect(toSourceText(decl)).toBe(d`
76
+ <a extends any, b extends any>() => void
77
+ `);
78
+ });
79
+
80
+ it("supports type parameters by descriptor array", () => {
81
+ const decl = <FunctionType typeParameters={["a", "b"]}></FunctionType>;
82
+
83
+ expect(toSourceText(decl)).toBe(d`
84
+ <a, b>() => void
85
+ `);
86
+ });
87
+
88
+ it("supports type parameters by element", () => {
89
+ const decl = (
90
+ <FunctionType>
91
+ <FunctionType.TypeParameters>a, b</FunctionType.TypeParameters>
92
+ </FunctionType>
93
+ );
94
+
95
+ expect(toSourceText(decl)).toBe(d`
96
+ <a, b>() => void
97
+ `);
98
+ });
99
+
100
+ describe("symbols", () => {
101
+ it("create optional parameters", () => {
102
+ const paramDesc: ParameterDescriptor = {
103
+ name: "foo",
104
+ refkey: refkey(),
105
+ type: "any",
106
+ optional: true,
107
+ };
108
+ const decl = (
109
+ <>
110
+ <FunctionType parameters={[paramDesc]}></FunctionType>
111
+ </>
112
+ );
113
+
114
+ expect(toSourceText(decl)).toBe(d`
115
+ (foo?: any) => void
116
+ `);
117
+ });
118
+ });