@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
@@ -28,5 +28,4 @@ export function createTSPackageScope(binder, parent, name, version, path, builti
28
28
  return null;
29
29
  }
30
30
  });
31
- }
32
- //# sourceMappingURL=ts-package-scope.js.map
31
+ }
package/dist/src/utils.js CHANGED
@@ -20,5 +20,4 @@ export function getCallSignatureProps(props, defaults) {
20
20
  const identifierRegex = /^(?:[\p{ID_Start}$_])(?:[\p{ID_Continue}$\u200C\u200D])*$/u;
21
21
  export function isValidJSIdentifier(str) {
22
22
  return identifierRegex.test(str);
23
- }
24
- //# sourceMappingURL=utils.js.map
23
+ }
@@ -0,0 +1,105 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import { ArrayExpression } from "../src/index.js";
5
+ import { toSourceText } from "./utils.js";
6
+ it("works", () => {
7
+ expect(toSourceText(_$createComponent(ArrayExpression, {
8
+ jsValue: [1, 2, 3]
9
+ }))).toBe(d`
10
+ [1, 2, 3]
11
+ `);
12
+ expect(toSourceText(_$createComponent(ArrayExpression, {
13
+ get jsValue() {
14
+ return [1, 2, 3, "foo".repeat(10)];
15
+ }
16
+ }), {
17
+ printWidth: 20
18
+ })).toBe(d`
19
+ [
20
+ 1,
21
+ 2,
22
+ 3,
23
+ "foofoofoofoofoofoofoofoofoofoo"
24
+ ]
25
+ `);
26
+ });
27
+ it("accepts children", () => {
28
+ expect(toSourceText(_$createComponent(ArrayExpression, {
29
+ jsValue: [1, 2, 3],
30
+ get children() {
31
+ return _$createComponent(ArrayExpression, {
32
+ jsValue: [4, 5, 6]
33
+ });
34
+ }
35
+ }))).toBe(d`
36
+ [1, 2, 3, [4, 5, 6]]
37
+ `);
38
+ });
39
+ it("accepts only children", () => {
40
+ expect(toSourceText(_$createComponent(ArrayExpression, {
41
+ get children() {
42
+ return _$createComponent(ArrayExpression, {
43
+ jsValue: [4, 5, 6]
44
+ });
45
+ }
46
+ }))).toBe(d`
47
+ [[4, 5, 6]]
48
+ `);
49
+ });
50
+ it("renders a falsy jsValue", () => {
51
+ expect(toSourceText(_$createComponent(ArrayExpression, {
52
+ jsValue: [undefined]
53
+ }))).toBe(d`
54
+ [undefined]
55
+ `);
56
+ });
57
+ it("renders a falsy jsValue with no leading comma", () => {
58
+ expect(toSourceText(_$createComponent(ArrayExpression, {
59
+ jsValue: [null]
60
+ }))).toBe(d`
61
+ [null]
62
+ `);
63
+ });
64
+ it("renders a falsy jsValue and children", () => {
65
+ expect(toSourceText(_$createComponent(ArrayExpression, {
66
+ jsValue: [false],
67
+ children: "\"Hello\""
68
+ }))).toBe(d`
69
+ [false, "Hello"]
70
+ `);
71
+ });
72
+ it("renders a falsy jsValue but not invisible children", () => {
73
+ expect(toSourceText(_$createComponent(ArrayExpression, {
74
+ jsValue: [false],
75
+ get children() {
76
+ return [null, undefined];
77
+ }
78
+ }))).toBe(d`
79
+ [false]
80
+ `);
81
+ });
82
+ it("renders a falsy jsValue and visible children", () => {
83
+ expect(toSourceText(_$createComponent(ArrayExpression, {
84
+ jsValue: [false],
85
+ children: "\"Hello\""
86
+ }))).toBe(d`
87
+ [false, "Hello"]
88
+ `);
89
+ });
90
+ it("renders a falsy jsValue and visible falsy children", () => {
91
+ expect(toSourceText(_$createComponent(ArrayExpression, {
92
+ jsValue: [false],
93
+ children: "false"
94
+ }))).toBe(d`
95
+ [false, false]
96
+ `);
97
+ });
98
+ it("renders a falsy jsValue but not invisible falsy children", () => {
99
+ expect(toSourceText(_$createComponent(ArrayExpression, {
100
+ jsValue: [false],
101
+ children: false
102
+ }))).toBe(d`
103
+ [false]
104
+ `);
105
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=arrow-function.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arrow-function.test.d.ts","sourceRoot":"","sources":["../../test/arrow-function.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,194 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { refkey, StatementList } from "@alloy-js/core";
3
+ import { d } from "@alloy-js/core/testing";
4
+ import { describe, expect, it } from "vitest";
5
+ import { ArrowFunction } from "../src/components/ArrowFunction.js";
6
+ import { VarDeclaration } from "../src/index.js";
7
+ import { toSourceText } from "./utils.js";
8
+ it("create basic function", () => {
9
+ expect(toSourceText(_$createComponent(ArrowFunction, {}))).toBe(d`
10
+ () => {}
11
+ `);
12
+ });
13
+ it("can be an async function", () => {
14
+ expect(toSourceText(_$createComponent(ArrowFunction, {
15
+ async: true
16
+ }))).toBe(d`
17
+ async () => {}
18
+ `);
19
+ });
20
+ it("can be an async with returnType", () => {
21
+ expect(toSourceText(_$createComponent(ArrowFunction, {
22
+ async: true,
23
+ returnType: "Foo"
24
+ }))).toBe(d`
25
+ async (): Promise<Foo> => {}
26
+ `);
27
+ });
28
+ it("can be an async with returnType element", () => {
29
+ function Foo(_props) {
30
+ return ["Foo"];
31
+ }
32
+ expect(toSourceText(_$createComponent(ArrowFunction, {
33
+ async: true,
34
+ get returnType() {
35
+ return _$createComponent(Foo, {});
36
+ }
37
+ }))).toBe(d`
38
+ async (): Promise<Foo> => {}
39
+ `);
40
+ });
41
+ it("supports parameters by element", () => {
42
+ const decl = _$createComponent(ArrowFunction, {
43
+ get children() {
44
+ return ["return a + b;", _$createComponent(ArrowFunction.Parameters, {
45
+ children: "a, b"
46
+ })];
47
+ }
48
+ });
49
+ expect(toSourceText(decl)).toBe(d`
50
+ (a, b) => {
51
+ return a + b;
52
+ }
53
+ `);
54
+ });
55
+ it("supports type parameters by descriptor object", () => {
56
+ const decl = _$createComponent(ArrowFunction, {
57
+ typeParameters: [{
58
+ name: "a",
59
+ extends: "any"
60
+ }, {
61
+ name: "b",
62
+ extends: "any"
63
+ }]
64
+ });
65
+ expect(toSourceText(decl)).toBe(d`
66
+ <a extends any, b extends any>() => {}
67
+ `);
68
+ });
69
+ it("supports type parameters by descriptor array", () => {
70
+ const decl = _$createComponent(ArrowFunction, {
71
+ typeParameters: ["a", "b"]
72
+ });
73
+ expect(toSourceText(decl)).toBe(d`
74
+ <a, b>() => {}
75
+ `);
76
+ });
77
+ it("supports type parameters by element", () => {
78
+ const decl = _$createComponent(ArrowFunction, {
79
+ get children() {
80
+ return _$createComponent(ArrowFunction.TypeParameters, {
81
+ children: "a, b"
82
+ });
83
+ }
84
+ });
85
+ expect(toSourceText(decl)).toBe(d`
86
+ <a, b>() => {}
87
+ `);
88
+ });
89
+ describe("symbols", () => {
90
+ it("creates a nested scope", () => {
91
+ const innerRefkey = refkey();
92
+ const outerRefkey = refkey();
93
+ const decl = _$createComponent(StatementList, {
94
+ get children() {
95
+ return [_$createComponent(ArrowFunction, {
96
+ get children() {
97
+ return _$createComponent(StatementList, {
98
+ get children() {
99
+ return [innerRefkey, _$createComponent(VarDeclaration, {
100
+ name: "refme",
101
+ refkey: innerRefkey,
102
+ children: "1"
103
+ })];
104
+ }
105
+ });
106
+ }
107
+ }), _$createComponent(VarDeclaration, {
108
+ name: "refme",
109
+ refkey: outerRefkey,
110
+ children: "2"
111
+ }), outerRefkey];
112
+ }
113
+ });
114
+ expect(toSourceText(decl)).toBe(d`
115
+ () => {
116
+ refme;
117
+ const refme = 1;
118
+ };
119
+ const refme = 2;
120
+ refme;
121
+ `);
122
+ });
123
+ it("throws an error when trying to access a symbol in a nested scope", () => {
124
+ const innerRefkey = refkey();
125
+ const decl = [_$createComponent(ArrowFunction, {
126
+ get children() {
127
+ return _$createComponent(VarDeclaration, {
128
+ name: "refme",
129
+ refkey: innerRefkey,
130
+ children: "1"
131
+ });
132
+ }
133
+ }), ";", innerRefkey];
134
+ expect(() => toSourceText(decl)).toThrow(/Cannot reference a symbol/);
135
+ });
136
+ it("creates symbols for parameters", () => {
137
+ const rk = refkey();
138
+ const decl = [_$createComponent(ArrowFunction, {
139
+ parameters: [{
140
+ name: "sym",
141
+ type: "any",
142
+ refkey: rk
143
+ }],
144
+ get children() {
145
+ return _$createComponent(ArrowFunction, {
146
+ children: rk
147
+ });
148
+ }
149
+ })];
150
+ expect(toSourceText(decl)).toBe(d`
151
+ (sym: any) => {
152
+ () => {
153
+ sym
154
+ }
155
+ }
156
+ `);
157
+ });
158
+ it("creates symbols for parameters and addresses conflicts", () => {
159
+ const decl = [_$createComponent(ArrowFunction, {
160
+ parameters: [{
161
+ name: "conflict",
162
+ type: "any"
163
+ }],
164
+ get children() {
165
+ return [_$createComponent(VarDeclaration, {
166
+ name: "conflict",
167
+ children: "1"
168
+ }), ";"];
169
+ }
170
+ })];
171
+ expect(toSourceText(decl)).toBe(d`
172
+ (conflict: any) => {
173
+ const conflict_2 = 1;
174
+ }
175
+ `);
176
+ });
177
+ it("create optional parameters", () => {
178
+ const paramDesc = {
179
+ name: "foo",
180
+ refkey: refkey(),
181
+ type: "any",
182
+ optional: true
183
+ };
184
+ const decl = [_$createComponent(ArrowFunction, {
185
+ parameters: [paramDesc],
186
+ children: "console.log(foo);"
187
+ })];
188
+ expect(toSourceText(decl)).toBe(d`
189
+ (foo?: any) => {
190
+ console.log(foo);
191
+ }
192
+ `);
193
+ });
194
+ });
@@ -0,0 +1,53 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, render, SourceDirectory, SourceFile } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { d } from "@alloy-js/core/testing";
5
+ import { expect, it } from "vitest";
6
+ import * as ts from "../src/components/index.js";
7
+ it("exports everything from source files within it", () => {
8
+ const res = render(_$createComponent(Output, {
9
+ get children() {
10
+ return [_$createComponent(ts.SourceFile, {
11
+ path: "test1.ts"
12
+ }), _$createComponent(ts.SourceFile, {
13
+ path: "test2.ts"
14
+ }), _$createComponent(SourceDirectory, {
15
+ path: "components",
16
+ get children() {
17
+ return [_$createComponent(ts.SourceFile, {
18
+ path: "c1.ts"
19
+ }), _$createComponent(ts.SourceFile, {
20
+ path: "c2.ts"
21
+ }), _$createComponent(ts.BarrelFile, {})];
22
+ }
23
+ }), _$createComponent(ts.BarrelFile, {})];
24
+ }
25
+ }));
26
+ expect(res.contents[2].contents[2].contents).toBe(d`
27
+ export * from "./c1.js";
28
+ export * from "./c2.js";
29
+ `);
30
+ expect(res.contents[3].contents).toBe(d`
31
+ export * from "./test1.js";
32
+ export * from "./test2.js";
33
+ export * from "./components/index.js";
34
+ `);
35
+ });
36
+ it("ignores non-TS files", () => {
37
+ const res = render(_$createComponent(Output, {
38
+ get children() {
39
+ return [_$createComponent(SourceFile, {
40
+ path: "test.txt",
41
+ filetype: "text"
42
+ }), _$createComponent(ts.SourceFile, {
43
+ path: "test1.ts"
44
+ }), _$createComponent(ts.SourceFile, {
45
+ path: "test2.ts"
46
+ }), _$createComponent(ts.BarrelFile, {})];
47
+ }
48
+ }));
49
+ expect(res.contents[3].contents).toBe(d`
50
+ export * from "./test1.js";
51
+ export * from "./test2.js";
52
+ `);
53
+ });
@@ -0,0 +1,91 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic, memo as _$memo } from "@alloy-js/core/jsx-runtime";
2
+ import { List, Output, refkey, render, SourceFile } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { d } from "@alloy-js/core/testing";
5
+ import { expect, it } from "vitest";
6
+ import * as ts from "../src/components/index.js";
7
+ import { Reference } from "../src/components/Reference.js";
8
+ import { findFile } from "./utils.js";
9
+ it("works", () => {
10
+ const fnSpec = {
11
+ greeting: "Hello",
12
+ farewell: "Goodbye"
13
+ };
14
+ const greetKey = refkey(fnSpec, "greet");
15
+ const farewellKey = refkey(fnSpec, "farewell");
16
+ const res = render(_$createComponent(Output, {
17
+ get children() {
18
+ return [_$createComponent(SourceFile, {
19
+ path: "readme.md",
20
+ filetype: "markdown",
21
+ children: "This is a sample output project."
22
+ }), _$createComponent(ts.SourceFile, {
23
+ path: "index.ts",
24
+ children: "console.log(\"Hello world!\");"
25
+ }), _$createComponent(ts.SourceFile, {
26
+ path: "test2.ts",
27
+ get children() {
28
+ return ["console.log(", _$createComponent(Reference, {
29
+ refkey: greetKey
30
+ }), "(\"world\"));", _$createIntrinsic("hbr", {}), "console.log(", _$createComponent(Reference, {
31
+ refkey: farewellKey
32
+ }), "(\"world\"));"];
33
+ }
34
+ }), _$createComponent(ts.SourceFile, {
35
+ path: "test1.ts",
36
+ get children() {
37
+ return _$createComponent(List, {
38
+ hardline: true,
39
+ get children() {
40
+ return [_$createComponent(ts.FunctionDeclaration, {
41
+ get name() {
42
+ return "say" + fnSpec.greeting;
43
+ },
44
+ refkey: greetKey,
45
+ parameters: [{
46
+ name: "str",
47
+ type: "string"
48
+ }],
49
+ get children() {
50
+ return ["return \"", _$memo(() => fnSpec.greeting), " \" + str;"];
51
+ }
52
+ }), _$createComponent(ts.FunctionDeclaration, {
53
+ get name() {
54
+ return "say" + fnSpec.farewell;
55
+ },
56
+ refkey: farewellKey,
57
+ parameters: [{
58
+ name: "str",
59
+ type: "string"
60
+ }],
61
+ get children() {
62
+ return ["return \"", _$memo(() => fnSpec.farewell), " \" + str;"];
63
+ }
64
+ })];
65
+ }
66
+ });
67
+ }
68
+ })];
69
+ }
70
+ }));
71
+ expect(findFile(res, "readme.md").contents).toEqual(d`
72
+ This is a sample output project.
73
+ `);
74
+ expect(findFile(res, "index.ts").contents).toEqual(d`
75
+ console.log("Hello world!");
76
+ `);
77
+ expect(findFile(res, "test1.ts").contents).toEqual(d`
78
+ function sayHello(str: string) {
79
+ return "Hello " + str;
80
+ }
81
+ function sayGoodbye(str: string) {
82
+ return "Goodbye " + str;
83
+ }
84
+ `);
85
+ expect(findFile(res, "test2.ts").contents).toEqual(d`
86
+ import { sayHello, sayGoodbye } from "./test1.js";
87
+
88
+ console.log(sayHello("world"));
89
+ console.log(sayGoodbye("world"));
90
+ `);
91
+ });
@@ -0,0 +1,30 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import { BlockScope } from "../src/components/BlockScope.js";
5
+ import { VarDeclaration } from "../src/index.js";
6
+ import { toSourceText } from "./utils.js";
7
+ it("creates a scope", () => {
8
+ const text = toSourceText([_$createComponent(VarDeclaration, {
9
+ name: "x",
10
+ initializer: "hi"
11
+ }), ";", _$createIntrinsic("hbr", {}), _$createComponent(BlockScope, {
12
+ name: "foo",
13
+ get children() {
14
+ return [_$createComponent(VarDeclaration, {
15
+ name: "x",
16
+ initializer: "hello"
17
+ }), ";"];
18
+ }
19
+ })]);
20
+ expect(text).toBe(d`
21
+ const x = hi;
22
+ {
23
+ const x = hello;
24
+ }
25
+ `);
26
+ });
27
+ it("renders an empty block properly", () => {
28
+ const text = toSourceText(_$createComponent(BlockScope, {}));
29
+ expect(text).toBe("{}");
30
+ });