@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,61 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { List } from "@alloy-js/core";
3
+ import { d } from "@alloy-js/core/testing";
4
+ import { expect, it } from "vitest";
5
+ import { CaseClause, SwitchStatement } from "../src/components/SwitchStatement.js";
6
+ import { toSourceText } from "./utils.js";
7
+ it("handles various cases", () => {
8
+ const text = toSourceText(_$createComponent(SwitchStatement, {
9
+ expression: "x",
10
+ get children() {
11
+ return _$createComponent(List, {
12
+ hardline: true,
13
+ get children() {
14
+ return [_$createComponent(CaseClause, {
15
+ expression: '"test"',
16
+ children: "// expression"
17
+ }), _$createComponent(CaseClause, {
18
+ jsValue: "test",
19
+ children: "// jsValue"
20
+ }), _$createComponent(CaseClause, {
21
+ expression: "'break'",
22
+ "break": true,
23
+ children: "// break"
24
+ }), _$createComponent(CaseClause, {
25
+ expression: "'block'",
26
+ block: true,
27
+ children: "// block"
28
+ }), _$createComponent(CaseClause, {
29
+ expression: "'breakblock'",
30
+ "break": true,
31
+ block: true,
32
+ children: "// block"
33
+ }), _$createComponent(CaseClause, {
34
+ "default": true,
35
+ children: "// default"
36
+ })];
37
+ }
38
+ });
39
+ }
40
+ }));
41
+ expect(text).toBe(d`
42
+ switch (x) {
43
+ case "test":
44
+ // expression
45
+ case "test":
46
+ // jsValue
47
+ case 'break':
48
+ // break
49
+ break;
50
+ case 'block': {
51
+ // block
52
+ }
53
+ case 'breakblock': {
54
+ // block
55
+ break;
56
+ }
57
+ default :
58
+ // default
59
+ }
60
+ `);
61
+ });
@@ -0,0 +1,45 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, render } from "@alloy-js/core";
3
+ import { dedent } from "@alloy-js/core/testing";
4
+ import { expect } from "vitest";
5
+ import * as ts from "../src/index.js";
6
+ export function toSourceText(c, options) {
7
+ const res = render(_$createComponent(Output, {
8
+ get children() {
9
+ return _$createComponent(ts.SourceFile, {
10
+ path: "test.ts",
11
+ children: c
12
+ });
13
+ }
14
+ }), options);
15
+ return res.contents[0].contents;
16
+ }
17
+ export function findFile(res, path) {
18
+ const result = findFileWorker(res, path);
19
+ if (!result) {
20
+ throw new Error("Expected to find file " + path);
21
+ }
22
+ return result;
23
+ function findFileWorker(res, path) {
24
+ for (const item of res.contents) {
25
+ if (item.kind === "file") {
26
+ if (item.path === path) {
27
+ return item;
28
+ }
29
+ continue;
30
+ } else {
31
+ const found = findFileWorker(item, path);
32
+ if (found) {
33
+ return found;
34
+ }
35
+ }
36
+ }
37
+ return null;
38
+ }
39
+ }
40
+ export function assertFileContents(res, expectedFiles) {
41
+ for (const [path, contents] of Object.entries(expectedFiles)) {
42
+ const file = findFile(res, path);
43
+ expect(file.contents).toBe(dedent(contents));
44
+ }
45
+ }
@@ -0,0 +1,30 @@
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 { ValueExpression } from "../src/index.js";
5
+ import { toSourceText } from "./utils.js";
6
+ it.each([[undefined, "undefined"], [null, "null"], [true, "true"], [false, "false"], [42, "42"], [42n, "42n"], ["abc", `"abc"`], ["a\nb\rc\\", `"a\\nb\\rc\\\\"`], [[1, 2, 3], d`
7
+ [1, 2, 3]
8
+ `], [{
9
+ a: 1,
10
+ b: 2,
11
+ c: 3
12
+ }, d`
13
+ {
14
+ a: 1,
15
+ b: 2,
16
+ c: 3,
17
+ }
18
+ `], [{
19
+ "mangled-name": 1,
20
+ "@pagination": 2
21
+ }, d`
22
+ {
23
+ "mangled-name": 1,
24
+ "@pagination": 2,
25
+ }
26
+ `]])("works - %o => %s", (jsValue, expectedSource) => {
27
+ expect(toSourceText(_$createComponent(ValueExpression, {
28
+ jsValue: jsValue
29
+ }))).toBe(expectedSource);
30
+ });
@@ -0,0 +1,351 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { mapJoin, Output, printTree, reactive, refkey, render, renderTree, StatementList } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { describe, expect, it } from "vitest";
5
+ import { d } from "@alloy-js/core/testing";
6
+ import * as ts from "../src/index.js";
7
+ import { assertFileContents } from "./utils.js";
8
+ it("renders an object", () => {
9
+ expect(_$createComponent(ts.ObjectExpression, {})).toRenderTo("{}");
10
+ });
11
+ it("renders an object with properties", () => {
12
+ expect(_$createComponent(ts.ObjectExpression, {
13
+ get children() {
14
+ return [_$createComponent(ts.ObjectProperty, {
15
+ name: "foo",
16
+ value: "1"
17
+ }), ",", _$createIntrinsic("hbr", {}), _$createComponent(ts.ObjectProperty, {
18
+ name: "bar",
19
+ value: "2"
20
+ })];
21
+ }
22
+ })).toRenderTo(`
23
+ {
24
+ foo: 1,
25
+ bar: 2
26
+ }
27
+ `);
28
+ });
29
+ it("renders an object with properties, mapped", () => {
30
+ const propValues = new Map([["foo", "1"], ["bar", "2"]]);
31
+ const props = mapJoin(() => propValues, (name, value) => {
32
+ return _$createComponent(ts.ObjectProperty, {
33
+ name: name,
34
+ value: value
35
+ });
36
+ }, {
37
+ joiner: ",\n"
38
+ });
39
+ expect(_$createComponent(ts.ObjectExpression, {
40
+ children: props
41
+ })).toRenderTo(`
42
+ {
43
+ foo: 1,
44
+ bar: 2
45
+ }
46
+ `);
47
+ });
48
+ it("is reactive", () => {
49
+ const jsValue = reactive(new Map());
50
+ const tree = renderTree(_$createComponent(ts.ObjectExpression, {
51
+ jsValue: jsValue
52
+ }));
53
+ expect(printTree(tree)).toEqual(`{}`);
54
+ jsValue.set("hi", 1);
55
+ expect(printTree(tree)).toEqual(d`
56
+ {
57
+ hi: 1,
58
+ }
59
+ `);
60
+ jsValue.set("bye", 2);
61
+ expect(printTree(tree)).toEqual(d`
62
+ {
63
+ hi: 1,
64
+ bye: 2,
65
+ }
66
+ `);
67
+ });
68
+ it("renders objects with arrays", () => {
69
+ const jsValue = {
70
+ a: [1, 2]
71
+ };
72
+ expect(_$createComponent(ts.ObjectExpression, {
73
+ jsValue: jsValue
74
+ })).toRenderTo(`
75
+ {
76
+ a: [1, 2],
77
+ }
78
+ `);
79
+ });
80
+ it("renders complex objects", () => {
81
+ const jsValue = {
82
+ a: 1,
83
+ b: "hello",
84
+ c: true,
85
+ d: {
86
+ prop: [1, 2, 3]
87
+ }
88
+ };
89
+ expect(_$createComponent(ts.ObjectExpression, {
90
+ jsValue: jsValue
91
+ })).toRenderTo(`
92
+ {
93
+ a: 1,
94
+ b: "hello",
95
+ c: true,
96
+ d: {
97
+ prop: [1, 2, 3],
98
+ },
99
+ }
100
+ `);
101
+ });
102
+ it("renders falsy values", () => {
103
+ const jsValue = {
104
+ a: 0,
105
+ b: "",
106
+ c: false,
107
+ d: null,
108
+ e: undefined
109
+ };
110
+ expect(_$createComponent(ts.ObjectExpression, {
111
+ jsValue: jsValue
112
+ })).toRenderTo(`
113
+ {
114
+ a: 0,
115
+ b: "",
116
+ c: false,
117
+ d: null,
118
+ e: undefined,
119
+ }
120
+ `);
121
+ });
122
+ it("allows embedding things with functions", () => {
123
+ function Foo() {
124
+ return ["a b"];
125
+ }
126
+ const jsValue = {
127
+ a: () => "hello",
128
+ b: () => _$createComponent(Foo, {})
129
+ };
130
+ expect(_$createComponent(ts.ObjectExpression, {
131
+ jsValue: jsValue
132
+ })).toRenderTo(`
133
+ {
134
+ a: hello,
135
+ b: a b,
136
+ }
137
+ `);
138
+ });
139
+ describe("symbols", () => {
140
+ it("can reference members", () => {
141
+ const innerRefkey = refkey();
142
+ const outerRefkey = refkey();
143
+ const decl = _$createComponent(Output, {
144
+ get children() {
145
+ return _$createComponent(ts.SourceFile, {
146
+ path: "foo.ts",
147
+ get children() {
148
+ return _$createComponent(StatementList, {
149
+ get children() {
150
+ return [_$createComponent(ts.VarDeclaration, {
151
+ name: "refme",
152
+ refkey: outerRefkey,
153
+ get children() {
154
+ return _$createComponent(ts.ObjectExpression, {
155
+ get children() {
156
+ return [_$createComponent(ts.ObjectProperty, {
157
+ name: "foo",
158
+ refkey: innerRefkey,
159
+ jsValue: "hello"
160
+ }), ","];
161
+ }
162
+ });
163
+ }
164
+ }), innerRefkey];
165
+ }
166
+ });
167
+ }
168
+ });
169
+ }
170
+ });
171
+ expect(decl).toRenderTo(`
172
+ const refme = {
173
+ foo: "hello",
174
+ };
175
+ refme.foo;
176
+ `);
177
+ });
178
+ it("can reference nested members", () => {
179
+ const varRefkey = refkey();
180
+ const fooRefkey = refkey();
181
+ const barRefkey = refkey();
182
+ const decl = _$createComponent(Output, {
183
+ get children() {
184
+ return _$createComponent(ts.SourceFile, {
185
+ path: "foo.ts",
186
+ get children() {
187
+ return _$createComponent(StatementList, {
188
+ get children() {
189
+ return [_$createComponent(ts.VarDeclaration, {
190
+ name: "refme",
191
+ refkey: varRefkey,
192
+ get children() {
193
+ return _$createComponent(ts.ObjectExpression, {
194
+ get children() {
195
+ return _$createComponent(ts.ObjectProperty, {
196
+ name: "foo",
197
+ refkey: fooRefkey,
198
+ get children() {
199
+ return _$createComponent(ts.ObjectExpression, {
200
+ get children() {
201
+ return _$createComponent(ts.ObjectProperty, {
202
+ name: "bar",
203
+ refkey: barRefkey,
204
+ children: "\"hello\","
205
+ });
206
+ }
207
+ });
208
+ }
209
+ });
210
+ }
211
+ });
212
+ }
213
+ }), barRefkey];
214
+ }
215
+ });
216
+ }
217
+ });
218
+ }
219
+ });
220
+ expect(decl).toRenderTo(`
221
+ const refme = {
222
+ foo: {
223
+ bar: "hello",
224
+ }
225
+ };
226
+ refme.foo.bar;
227
+ `);
228
+ });
229
+ it("can reference nested members in other source files", () => {
230
+ const varRefkey = refkey();
231
+ const fooRefkey = refkey();
232
+ const barRefkey = refkey();
233
+ const decl = _$createComponent(Output, {
234
+ get children() {
235
+ return [_$createComponent(ts.SourceFile, {
236
+ path: "foo.ts",
237
+ get children() {
238
+ return _$createComponent(ts.VarDeclaration, {
239
+ "export": true,
240
+ name: "refme",
241
+ refkey: varRefkey,
242
+ get children() {
243
+ return _$createComponent(ts.ObjectExpression, {
244
+ get children() {
245
+ return _$createComponent(ts.ObjectProperty, {
246
+ name: "foo",
247
+ refkey: fooRefkey,
248
+ get children() {
249
+ return _$createComponent(ts.ObjectExpression, {
250
+ get children() {
251
+ return _$createComponent(ts.ObjectProperty, {
252
+ name: "bar",
253
+ refkey: barRefkey,
254
+ children: "\"hello\","
255
+ });
256
+ }
257
+ });
258
+ }
259
+ });
260
+ }
261
+ });
262
+ }
263
+ });
264
+ }
265
+ }), _$createComponent(ts.SourceFile, {
266
+ path: "bar.ts",
267
+ get children() {
268
+ return ["console.log(", barRefkey, ");"];
269
+ }
270
+ })];
271
+ }
272
+ });
273
+ expect(decl).toRenderTo(`
274
+ export const refme = {
275
+ foo: {
276
+ bar: "hello",
277
+ }
278
+ }import { refme } from "./foo.js";
279
+
280
+ console.log(refme.foo.bar);
281
+ `);
282
+ });
283
+ it("can reference nested members in other packages", () => {
284
+ const varRefkey = refkey();
285
+ const fooRefkey = refkey();
286
+ const barRefkey = refkey();
287
+ const decl = _$createComponent(Output, {
288
+ get children() {
289
+ return [_$createComponent(ts.PackageDirectory, {
290
+ name: "SourcePackage",
291
+ path: "sp",
292
+ version: "1.0.0",
293
+ get children() {
294
+ return _$createComponent(ts.SourceFile, {
295
+ "export": ".",
296
+ path: "foo.ts",
297
+ get children() {
298
+ return _$createComponent(ts.VarDeclaration, {
299
+ "export": true,
300
+ name: "refme",
301
+ refkey: varRefkey,
302
+ get children() {
303
+ return _$createComponent(ts.ObjectExpression, {
304
+ get children() {
305
+ return _$createComponent(ts.ObjectProperty, {
306
+ name: "foo",
307
+ refkey: fooRefkey,
308
+ get children() {
309
+ return _$createComponent(ts.ObjectExpression, {
310
+ get children() {
311
+ return _$createComponent(ts.ObjectProperty, {
312
+ name: "bar",
313
+ refkey: barRefkey,
314
+ children: "\"hello\";"
315
+ });
316
+ }
317
+ });
318
+ }
319
+ });
320
+ }
321
+ });
322
+ }
323
+ });
324
+ }
325
+ });
326
+ }
327
+ }), _$createComponent(ts.PackageDirectory, {
328
+ name: "DepPackage",
329
+ path: "dp",
330
+ version: "1.0.0",
331
+ get children() {
332
+ return _$createComponent(ts.SourceFile, {
333
+ path: "bar.ts",
334
+ get children() {
335
+ return ["console.log(", barRefkey, ");"];
336
+ }
337
+ });
338
+ }
339
+ })];
340
+ }
341
+ });
342
+ const res = render(decl);
343
+ assertFileContents(res, {
344
+ "dp/bar.ts": `
345
+ import { refme } from "SourcePackage";
346
+
347
+ console.log(refme.foo.bar);
348
+ `
349
+ });
350
+ });
351
+ });
@@ -0,0 +1,50 @@
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 * as ts from "../src/index.js";
5
+ import { toSourceText } from "./utils.js";
6
+ it("renders a variable declaration with documentation", () => {
7
+ const res = toSourceText([_$createComponent(ts.VarDeclaration, {
8
+ name: "myVar",
9
+ "const": true,
10
+ doc: "This is a variable documentation",
11
+ children: "123"
12
+ }), ";"]);
13
+ expect(res).toEqual(d`
14
+ /**
15
+ * This is a variable documentation
16
+ */
17
+ const myVar = 123;
18
+ `);
19
+ });
20
+ it("renders a let declaration with documentation", () => {
21
+ const res = toSourceText([_$createComponent(ts.VarDeclaration, {
22
+ "let": true,
23
+ name: "counter",
24
+ doc: "A counter variable that can be changed",
25
+ type: "number",
26
+ children: "0"
27
+ }), ";"]);
28
+ expect(res).toEqual(d`
29
+ /**
30
+ * A counter variable that can be changed
31
+ */
32
+ let counter: number = 0;
33
+ `);
34
+ });
35
+ it("renders an exported variable declaration with documentation", () => {
36
+ const res = toSourceText([_$createComponent(ts.VarDeclaration, {
37
+ "export": true,
38
+ name: "API_URL",
39
+ "const": true,
40
+ doc: "The base URL for API calls",
41
+ type: "string",
42
+ children: "\"https://api.example.com\""
43
+ }), ";"]);
44
+ expect(res).toEqual(d`
45
+ /**
46
+ * The base URL for API calls
47
+ */
48
+ export const API_URL: string = "https://api.example.com";
49
+ `);
50
+ });
@@ -0,0 +1,63 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, refkey, render } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { expect, it } from "vitest";
5
+ import * as ts from "../src/index.js";
6
+ import { assertFileContents } from "./utils.js";
7
+ it("works", () => {
8
+ expect(_$createComponent(Output, {
9
+ get children() {
10
+ return _$createComponent(ts.SourceFile, {
11
+ path: "test.js",
12
+ get children() {
13
+ return [_$createComponent(ts.VarDeclaration, {
14
+ name: "hi",
15
+ initializer: "12"
16
+ }), ";"];
17
+ }
18
+ });
19
+ }
20
+ })).toRenderTo("const hi = 12;");
21
+ });
22
+ it("works end-to-end", () => {
23
+ const TestType = refkey("TestType");
24
+ const res = render(_$createComponent(Output, {
25
+ get children() {
26
+ return [_$createComponent(ts.SourceFile, {
27
+ path: "types.ts",
28
+ get children() {
29
+ return _$createComponent(ts.TypeDeclaration, {
30
+ name: "TestType",
31
+ refkey: TestType,
32
+ children: "\"hello\" | \"goodbye\""
33
+ });
34
+ }
35
+ }), _$createComponent(ts.SourceFile, {
36
+ path: "test.ts",
37
+ get children() {
38
+ return [_$createComponent(ts.VarDeclaration, {
39
+ "export": true,
40
+ "let": true,
41
+ name: "hi",
42
+ get type() {
43
+ return _$createComponent(ts.Reference, {
44
+ refkey: TestType
45
+ });
46
+ },
47
+ children: "\"hello\""
48
+ }), ";"];
49
+ }
50
+ })];
51
+ }
52
+ }));
53
+ assertFileContents(res, {
54
+ "types.ts": `
55
+ type TestType = "hello" | "goodbye";
56
+ `,
57
+ "test.ts": `
58
+ import type { TestType } from "./types.js";
59
+
60
+ export let hi: TestType = "hello";
61
+ `
62
+ });
63
+ });
@@ -0,0 +1 @@
1
+ import "vitest";