@alloy-js/typescript 0.9.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 (203) hide show
  1. package/CHANGELOG.md +22 -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.d.ts +1 -2
  45. package/dist/src/components/MemberExpression.d.ts.map +1 -1
  46. package/dist/src/components/MemberExpression.js +2 -5
  47. package/dist/src/components/ObjectExpression.js +2 -5
  48. package/dist/src/components/PackageDirectory.js +2 -5
  49. package/dist/src/components/PackageJson.js +1 -2
  50. package/dist/src/components/PropertyName.js +1 -2
  51. package/dist/src/components/Reference.js +1 -2
  52. package/dist/src/components/SingleLineCommentBlock.js +2 -4
  53. package/dist/src/components/SourceFile.js +2 -4
  54. package/dist/src/components/SwitchStatement.js +2 -5
  55. package/dist/src/components/TsConfigJson.js +1 -2
  56. package/dist/src/components/TypeDeclaration.js +2 -6
  57. package/dist/src/components/ValueExpression.js +1 -2
  58. package/dist/src/components/VarDeclaration.js +2 -5
  59. package/dist/src/components/index.d.ts +7 -0
  60. package/dist/src/components/index.d.ts.map +1 -1
  61. package/dist/src/components/index.js +7 -1
  62. package/dist/src/components/stc/index.d.ts +8 -0
  63. package/dist/src/components/stc/index.d.ts.map +1 -1
  64. package/dist/src/components/stc/index.js +9 -2
  65. package/dist/src/context/private-scope.js +1 -2
  66. package/dist/src/create-package.js +1 -2
  67. package/dist/src/index.js +1 -2
  68. package/dist/src/name-conflict-resolver.js +1 -2
  69. package/dist/src/name-policy.js +1 -2
  70. package/dist/src/parameter-descriptor.d.ts +27 -17
  71. package/dist/src/parameter-descriptor.d.ts.map +1 -1
  72. package/dist/src/source-directory-data.js +1 -2
  73. package/dist/src/symbols/index.js +1 -2
  74. package/dist/src/symbols/reference.js +2 -3
  75. package/dist/src/symbols/scopes.js +1 -2
  76. package/dist/src/symbols/ts-member-scope.js +1 -2
  77. package/dist/src/symbols/ts-module-scope.js +1 -2
  78. package/dist/src/symbols/ts-output-symbol.js +1 -2
  79. package/dist/src/symbols/ts-package-scope.js +1 -2
  80. package/dist/src/utils.js +1 -2
  81. package/dist/test/array.test.js +105 -0
  82. package/dist/test/arrow-function.test.d.ts +2 -0
  83. package/dist/test/arrow-function.test.d.ts.map +1 -0
  84. package/dist/test/arrow-function.test.js +194 -0
  85. package/dist/test/barrel.test.js +53 -0
  86. package/dist/test/basic.test.js +91 -0
  87. package/dist/test/block-scope.test.js +30 -0
  88. package/dist/test/class.test.js +438 -0
  89. package/dist/test/enum-jsdoc.test.js +88 -0
  90. package/dist/test/enum.test.js +247 -0
  91. package/dist/test/externals.test.js +145 -0
  92. package/dist/test/function-call-expression.test.js +87 -0
  93. package/dist/test/function-declaration-jsdoc.test.js +91 -0
  94. package/dist/test/function-declaration.test.js +228 -0
  95. package/dist/test/function-expression.test.d.ts +2 -0
  96. package/dist/test/function-expression.test.d.ts.map +1 -0
  97. package/dist/test/function-expression.test.js +194 -0
  98. package/dist/test/function-type.test.d.ts +2 -0
  99. package/dist/test/function-type.test.d.ts.map +1 -0
  100. package/dist/test/function-type.test.js +122 -0
  101. package/dist/test/if-statement.test.js +25 -0
  102. package/dist/test/imports.test.js +434 -0
  103. package/dist/test/interface-jsdoc.test.js +178 -0
  104. package/dist/test/interface.test.d.ts.map +1 -1
  105. package/dist/test/interface.test.js +490 -0
  106. package/dist/test/jscomment.test.js +70 -0
  107. package/dist/test/jsdoc.test.js +134 -0
  108. package/dist/test/jsdocparam.test.js +164 -0
  109. package/dist/test/member-chain.test.js +141 -0
  110. package/dist/test/member-expression.test.js +359 -0
  111. package/dist/test/name-conflict.test.js +108 -0
  112. package/dist/test/name-policy.test.js +122 -0
  113. package/dist/test/package-external.test.js +196 -0
  114. package/dist/test/package-json.test.js +93 -0
  115. package/dist/test/package.test.js +136 -0
  116. package/dist/test/reference.test.js +81 -0
  117. package/dist/test/source-file.test.js +36 -0
  118. package/dist/test/switch-statement.test.js +61 -0
  119. package/dist/test/utils.js +45 -0
  120. package/dist/test/value-expression.test.js +30 -0
  121. package/dist/test/values.test.js +351 -0
  122. package/dist/test/var-declaration-jsdoc.test.js +50 -0
  123. package/dist/test/var-declaration.test.js +63 -0
  124. package/dist/test/vitest.d.js +1 -0
  125. package/dist/tsconfig.tsbuildinfo +1 -1
  126. package/package.json +12 -17
  127. package/src/components/ArrowFunction.tsx +87 -0
  128. package/src/components/Declaration.tsx +1 -1
  129. package/src/components/FunctionBase.tsx +271 -0
  130. package/src/components/FunctionDeclaration.tsx +13 -195
  131. package/src/components/FunctionExpression.tsx +86 -0
  132. package/src/components/FunctionType.tsx +88 -0
  133. package/src/components/Interface.tsx +33 -5
  134. package/src/components/InterfaceMethod.tsx +96 -0
  135. package/src/components/MemberDeclaration.tsx +70 -0
  136. package/src/components/MemberExpression.tsx +1 -1
  137. package/src/components/index.ts +11 -0
  138. package/src/components/stc/index.ts +8 -0
  139. package/src/parameter-descriptor.ts +29 -18
  140. package/src/symbols/reference.ts +4 -1
  141. package/temp/api.json +3099 -863
  142. package/test/arrow-function.test.tsx +188 -0
  143. package/test/class.test.tsx +1 -2
  144. package/test/function-call-expression.test.tsx +1 -2
  145. package/test/function-declaration.test.tsx +1 -2
  146. package/test/function-expression.test.tsx +195 -0
  147. package/test/function-type.test.tsx +118 -0
  148. package/test/interface.test.tsx +256 -6
  149. package/tsdoc-metadata.json +1 -1
  150. package/babel.config.cjs +0 -4
  151. package/dist/src/builtins/index.js.map +0 -1
  152. package/dist/src/builtins/node.js.map +0 -1
  153. package/dist/src/components/ArrayExpression.js.map +0 -1
  154. package/dist/src/components/BarrelFile.js.map +0 -1
  155. package/dist/src/components/BlockScope.js.map +0 -1
  156. package/dist/src/components/CallSignature.js.map +0 -1
  157. package/dist/src/components/ClassDeclaration.js.map +0 -1
  158. package/dist/src/components/CommaList.js.map +0 -1
  159. package/dist/src/components/Declaration.js.map +0 -1
  160. package/dist/src/components/EnumDeclaration.js.map +0 -1
  161. package/dist/src/components/EnumMember.js.map +0 -1
  162. package/dist/src/components/ExportStatement.js.map +0 -1
  163. package/dist/src/components/FunctionCallExpression.js.map +0 -1
  164. package/dist/src/components/FunctionDeclaration.js.map +0 -1
  165. package/dist/src/components/IfStatement.js.map +0 -1
  166. package/dist/src/components/ImportStatement.js.map +0 -1
  167. package/dist/src/components/Interface.js.map +0 -1
  168. package/dist/src/components/JSDoc.js.map +0 -1
  169. package/dist/src/components/JSDocComment.js.map +0 -1
  170. package/dist/src/components/JSDocExample.js.map +0 -1
  171. package/dist/src/components/JSDocParam.js.map +0 -1
  172. package/dist/src/components/MemberChainExpression.js.map +0 -1
  173. package/dist/src/components/MemberExpression.js.map +0 -1
  174. package/dist/src/components/ObjectExpression.js.map +0 -1
  175. package/dist/src/components/PackageDirectory.js.map +0 -1
  176. package/dist/src/components/PackageJson.js.map +0 -1
  177. package/dist/src/components/PropertyName.js.map +0 -1
  178. package/dist/src/components/Reference.js.map +0 -1
  179. package/dist/src/components/SingleLineCommentBlock.js.map +0 -1
  180. package/dist/src/components/SourceFile.js.map +0 -1
  181. package/dist/src/components/SwitchStatement.js.map +0 -1
  182. package/dist/src/components/TsConfigJson.js.map +0 -1
  183. package/dist/src/components/TypeDeclaration.js.map +0 -1
  184. package/dist/src/components/ValueExpression.js.map +0 -1
  185. package/dist/src/components/VarDeclaration.js.map +0 -1
  186. package/dist/src/components/index.js.map +0 -1
  187. package/dist/src/components/stc/index.js.map +0 -1
  188. package/dist/src/context/index.js.map +0 -1
  189. package/dist/src/context/private-scope.js.map +0 -1
  190. package/dist/src/create-package.js.map +0 -1
  191. package/dist/src/index.js.map +0 -1
  192. package/dist/src/name-conflict-resolver.js.map +0 -1
  193. package/dist/src/name-policy.js.map +0 -1
  194. package/dist/src/parameter-descriptor.js.map +0 -1
  195. package/dist/src/source-directory-data.js.map +0 -1
  196. package/dist/src/symbols/index.js.map +0 -1
  197. package/dist/src/symbols/reference.js.map +0 -1
  198. package/dist/src/symbols/scopes.js.map +0 -1
  199. package/dist/src/symbols/ts-member-scope.js.map +0 -1
  200. package/dist/src/symbols/ts-module-scope.js.map +0 -1
  201. package/dist/src/symbols/ts-output-symbol.js.map +0 -1
  202. package/dist/src/symbols/ts-package-scope.js.map +0 -1
  203. package/dist/src/utils.js.map +0 -1
@@ -0,0 +1,108 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, render } from "@alloy-js/core";
3
+ import { d } from "@alloy-js/core/testing";
4
+ import { camelCase } from "change-case";
5
+ import { expect, it } from "vitest";
6
+ import { FunctionDeclaration, SourceFile, TSSymbolFlags } from "../src/index.js";
7
+ it("handles custom name conflict resolver based on metadata", () => {
8
+ function resolver(name, symbols) {
9
+ const goodNamedSymbols = symbols.filter(s => ~s.tsFlags & TSSymbolFlags.LocalImportSymbol);
10
+ const badNamedSymbols = symbols.filter(s => s.tsFlags & TSSymbolFlags.LocalImportSymbol);
11
+ let nameCount = 1;
12
+
13
+ // always rename local import symbols (we don't care about them)
14
+ for (const sym of badNamedSymbols) {
15
+ sym.name = name + "_" + nameCount++;
16
+ }
17
+
18
+ // otherwise check the symbol metadata to see how to rename.
19
+
20
+ // group by symbol metadata
21
+ const groupedSymbols = new Map([["body", []], ["query", []], ["header", []], [undefined, []]]);
22
+ for (const sym of goodNamedSymbols) {
23
+ const sourceLocation = sym.metadata.sourceLocation;
24
+ groupedSymbols.get(sourceLocation).push(sym);
25
+ }
26
+ for (const [sourceLocation, symbols] of groupedSymbols) {
27
+ let locationCount = 0;
28
+ if (sourceLocation === undefined) {
29
+ for (const sym of symbols) {
30
+ if (locationCount > 0) {
31
+ sym.name = name + locationCount;
32
+ }
33
+ locationCount++;
34
+ }
35
+ } else if (symbols.length < 0) {
36
+ continue;
37
+ } else if (symbols.length === 1) {
38
+ symbols[0].name = camelCase(name + "_" + sourceLocation);
39
+ } else {
40
+ for (const sym of symbols) {
41
+ sym.name = camelCase(name + "_" + sourceLocation + locationCount++, {});
42
+ }
43
+ }
44
+ }
45
+ }
46
+ const testParameters = [{
47
+ name: "foo"
48
+ }, {
49
+ name: "foo"
50
+ }, {
51
+ name: "foo",
52
+ metadata: {
53
+ sourceLocation: "body"
54
+ }
55
+ }, {
56
+ name: "foo",
57
+ metadata: {
58
+ sourceLocation: "query"
59
+ }
60
+ }, {
61
+ name: "foo",
62
+ metadata: {
63
+ sourceLocation: "query"
64
+ }
65
+ }, {
66
+ name: "foo",
67
+ metadata: {
68
+ sourceLocation: "header"
69
+ }
70
+ }, {
71
+ name: "foo",
72
+ metadata: {
73
+ sourceLocation: "header"
74
+ }
75
+ }, {
76
+ name: "foo",
77
+ metadata: {
78
+ sourceLocation: "header"
79
+ }
80
+ }];
81
+ const res = render(_$createComponent(Output, {
82
+ nameConflictResolver: resolver,
83
+ get children() {
84
+ return _$createComponent(SourceFile, {
85
+ path: "test.ts",
86
+ get children() {
87
+ return _$createComponent(FunctionDeclaration, {
88
+ "export": true,
89
+ name: "conflicty",
90
+ parameters: testParameters
91
+ });
92
+ }
93
+ });
94
+ }
95
+ }));
96
+ expect(res.contents[0].contents).toBe(d`
97
+ export function conflicty(
98
+ foo,
99
+ foo1,
100
+ fooBody,
101
+ fooQuery0,
102
+ fooQuery1,
103
+ fooHeader0,
104
+ fooHeader1,
105
+ fooHeader2,
106
+ ) {}
107
+ `);
108
+ });
@@ -0,0 +1,122 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, refkey, render, StatementList } from "@alloy-js/core";
3
+ import "@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 { createTSNamePolicy } from "../src/name-policy.js";
8
+ it("applies to functions and variables", () => {
9
+ const ref1 = refkey({});
10
+ const ref2 = refkey({});
11
+ const namePolicy = createTSNamePolicy();
12
+ const res = render(_$createComponent(Output, {
13
+ namePolicy: namePolicy,
14
+ get children() {
15
+ return _$createComponent(ts.SourceFile, {
16
+ path: "test.ts",
17
+ get children() {
18
+ return [_$createComponent(ts.FunctionDeclaration, {
19
+ name: "foo-bar",
20
+ refkey: ref1
21
+ }), _$createIntrinsic("hbr", {}), _$createComponent(StatementList, {
22
+ get children() {
23
+ return [_$createComponent(ts.VarDeclaration, {
24
+ name: "one-two",
25
+ refkey: ref2,
26
+ children: "\"hello\""
27
+ }), _$createComponent(ts.Reference, {
28
+ refkey: ref1
29
+ }), _$createComponent(ts.Reference, {
30
+ refkey: ref2
31
+ })];
32
+ }
33
+ })];
34
+ }
35
+ });
36
+ }
37
+ }));
38
+ expect(res.contents[0].contents).toEqual(d`
39
+ function fooBar() {}
40
+ const oneTwo = "hello";
41
+ fooBar;
42
+ oneTwo;
43
+ `);
44
+ });
45
+ it("keeps _ and $ prefix", () => {
46
+ const ref1 = refkey({});
47
+ const ref2 = refkey({});
48
+ const namePolicy = createTSNamePolicy();
49
+ const res = render(_$createComponent(Output, {
50
+ namePolicy: namePolicy,
51
+ get children() {
52
+ return _$createComponent(ts.SourceFile, {
53
+ path: "test.ts",
54
+ get children() {
55
+ return [_$createComponent(ts.FunctionDeclaration, {
56
+ name: "_foo-bar",
57
+ refkey: ref1
58
+ }), _$createIntrinsic("hbr", {}), _$createComponent(StatementList, {
59
+ get children() {
60
+ return [_$createComponent(ts.VarDeclaration, {
61
+ name: "$one-two",
62
+ refkey: ref2,
63
+ children: "\"hello\""
64
+ }), _$createComponent(ts.Reference, {
65
+ refkey: ref1
66
+ }), _$createComponent(ts.Reference, {
67
+ refkey: ref2
68
+ })];
69
+ }
70
+ })];
71
+ }
72
+ });
73
+ }
74
+ }));
75
+ expect(res.contents[0].contents).toEqual(d`
76
+ function _fooBar() {}
77
+ const $oneTwo = "hello";
78
+ _fooBar;
79
+ $oneTwo;
80
+ `);
81
+ });
82
+ it("appends _ to reserved words", () => {
83
+ const ref1 = refkey({});
84
+ const ref2 = refkey({});
85
+ const namePolicy = createTSNamePolicy();
86
+ const res = render(_$createComponent(Output, {
87
+ namePolicy: namePolicy,
88
+ get children() {
89
+ return _$createComponent(ts.SourceFile, {
90
+ path: "test.ts",
91
+ get children() {
92
+ return [_$createComponent(ts.FunctionDeclaration, {
93
+ name: "default",
94
+ refkey: ref1,
95
+ parameters: [{
96
+ name: "await",
97
+ type: "any"
98
+ }]
99
+ }), _$createIntrinsic("hbr", {}), _$createComponent(StatementList, {
100
+ get children() {
101
+ return [_$createComponent(ts.VarDeclaration, {
102
+ name: "super",
103
+ refkey: ref2,
104
+ children: "\"hello\""
105
+ }), _$createComponent(ts.Reference, {
106
+ refkey: ref1
107
+ }), _$createComponent(ts.Reference, {
108
+ refkey: ref2
109
+ })];
110
+ }
111
+ })];
112
+ }
113
+ });
114
+ }
115
+ }));
116
+ expect(res.contents[0].contents).toEqual(d`
117
+ function default_(await_: any) {}
118
+ const super_ = "hello";
119
+ default_;
120
+ super_;
121
+ `);
122
+ });
@@ -0,0 +1,196 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, refkey, render } from "@alloy-js/core";
3
+ import { d } from "@alloy-js/core/testing";
4
+ import { expect, it } from "vitest";
5
+ import * as ts from "../src/index.js";
6
+ import { findFile } from "./utils.js";
7
+ it("imports external packages", () => {
8
+ const res = render(_$createComponent(Output, {
9
+ get children() {
10
+ return [_$createComponent(ts.PackageDirectory, {
11
+ name: "greeting-lib",
12
+ path: "greeting-lib",
13
+ version: "1.0.0",
14
+ get children() {
15
+ return [_$createComponent(ts.SourceFile, {
16
+ path: "greetings.ts",
17
+ get children() {
18
+ return _$createComponent(ts.FunctionDeclaration, {
19
+ name: "getGreeting",
20
+ get refkey() {
21
+ return refkey("getGreeting");
22
+ },
23
+ children: "return \"Hello world!\";"
24
+ });
25
+ }
26
+ }), _$createComponent(ts.SourceFile, {
27
+ path: "logGreetings.ts",
28
+ get children() {
29
+ return _$createComponent(ts.FunctionDeclaration, {
30
+ "export": true,
31
+ name: "printGreeting",
32
+ get refkey() {
33
+ return refkey("printGreeting");
34
+ },
35
+ children: "console.log(\"Hello world!\");"
36
+ });
37
+ }
38
+ }), _$createComponent(ts.BarrelFile, {
39
+ "export": "."
40
+ })];
41
+ }
42
+ }), _$createComponent(ts.PackageDirectory, {
43
+ name: "consumer",
44
+ path: "consumer",
45
+ version: "1.0.0",
46
+ get children() {
47
+ return _$createComponent(ts.SourceFile, {
48
+ "export": ".",
49
+ path: "ref.ts",
50
+ get children() {
51
+ return [_$createComponent(ts.Reference, {
52
+ get refkey() {
53
+ return refkey("getGreeting");
54
+ }
55
+ }), "();"];
56
+ }
57
+ });
58
+ }
59
+ })];
60
+ }
61
+ }));
62
+ const consumerPkgJson = findFile(res, "consumer/package.json");
63
+ const greetingPkgJson = findFile(res, "greeting-lib/package.json");
64
+ const refFile = findFile(res, "consumer/ref.ts");
65
+ expect(consumerPkgJson.contents).toEqual(d`
66
+ {
67
+ "name": "consumer",
68
+ "version": "1.0.0",
69
+ "type": "module",
70
+ "dependencies": {
71
+ "greeting-lib": "1.0.0"
72
+ },
73
+ "devDependencies": {
74
+ "typescript": "^5.5.2"
75
+ },
76
+ "exports": {
77
+ ".": "./dist/ref.js"
78
+ }
79
+ }
80
+ `);
81
+ expect(greetingPkgJson.contents).toEqual(d`
82
+ {
83
+ "name": "greeting-lib",
84
+ "version": "1.0.0",
85
+ "type": "module",
86
+ "devDependencies": {
87
+ "typescript": "^5.5.2"
88
+ },
89
+ "exports": {
90
+ ".": "./dist/index.js"
91
+ }
92
+ }
93
+ `);
94
+ expect(refFile.contents).toEqual(d`
95
+ import { getGreeting } from "greeting-lib";
96
+
97
+ getGreeting();
98
+ `);
99
+ });
100
+ it("combines explicit dependencies with referenced dependencies", () => {
101
+ const res = render(_$createComponent(Output, {
102
+ get children() {
103
+ return [_$createComponent(ts.PackageDirectory, {
104
+ name: "greeting-lib",
105
+ path: "greeting-lib",
106
+ version: "1.0.0",
107
+ get children() {
108
+ return [_$createComponent(ts.SourceFile, {
109
+ path: "greetings.ts",
110
+ get children() {
111
+ return _$createComponent(ts.FunctionDeclaration, {
112
+ name: "getGreeting",
113
+ get refkey() {
114
+ return refkey("getGreeting");
115
+ },
116
+ children: "return \"Hello world!\";"
117
+ });
118
+ }
119
+ }), _$createComponent(ts.SourceFile, {
120
+ path: "logGreetings.ts",
121
+ get children() {
122
+ return _$createComponent(ts.FunctionDeclaration, {
123
+ "export": true,
124
+ name: "printGreeting",
125
+ get refkey() {
126
+ return refkey("printGreeting");
127
+ },
128
+ children: "console.log(\"Hello world!\");"
129
+ });
130
+ }
131
+ }), _$createComponent(ts.BarrelFile, {
132
+ "export": "."
133
+ })];
134
+ }
135
+ }), _$createComponent(ts.PackageDirectory, {
136
+ name: "consumer",
137
+ path: "consumer",
138
+ version: "1.0.0",
139
+ dependencies: {
140
+ foo: "bar"
141
+ },
142
+ get children() {
143
+ return _$createComponent(ts.SourceFile, {
144
+ "export": ".",
145
+ path: "ref.ts",
146
+ get children() {
147
+ return [_$createComponent(ts.Reference, {
148
+ get refkey() {
149
+ return refkey("getGreeting");
150
+ }
151
+ }), "();"];
152
+ }
153
+ });
154
+ }
155
+ })];
156
+ }
157
+ }));
158
+ const consumerPkgJson = findFile(res, "consumer/package.json");
159
+ const greetingPkgJson = findFile(res, "greeting-lib/package.json");
160
+ const refFile = findFile(res, "consumer/ref.ts");
161
+ expect(consumerPkgJson.contents).toEqual(d`
162
+ {
163
+ "name": "consumer",
164
+ "version": "1.0.0",
165
+ "type": "module",
166
+ "dependencies": {
167
+ "foo": "bar",
168
+ "greeting-lib": "1.0.0"
169
+ },
170
+ "devDependencies": {
171
+ "typescript": "^5.5.2"
172
+ },
173
+ "exports": {
174
+ ".": "./dist/ref.js"
175
+ }
176
+ }
177
+ `);
178
+ expect(greetingPkgJson.contents).toEqual(d`
179
+ {
180
+ "name": "greeting-lib",
181
+ "version": "1.0.0",
182
+ "type": "module",
183
+ "devDependencies": {
184
+ "typescript": "^5.5.2"
185
+ },
186
+ "exports": {
187
+ ".": "./dist/index.js"
188
+ }
189
+ }
190
+ `);
191
+ expect(refFile.contents).toEqual(d`
192
+ import { getGreeting } from "greeting-lib";
193
+
194
+ getGreeting();
195
+ `);
196
+ });
@@ -0,0 +1,93 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, render } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { it } from "vitest";
5
+ import { PackageJsonFile } from "../src/components/PackageJson.js";
6
+ import { assertFileContents } from "./utils.js";
7
+ it("creates a simple package.json", () => {
8
+ const res = render(_$createComponent(Output, {
9
+ get children() {
10
+ return _$createComponent(PackageJsonFile, {
11
+ name: "test",
12
+ version: "1.0.0"
13
+ });
14
+ }
15
+ }));
16
+ assertFileContents(res, {
17
+ "package.json": `
18
+ {
19
+ "name": "test",
20
+ "version": "1.0.0",
21
+ "type": "module"
22
+ }
23
+ `
24
+ });
25
+ });
26
+ it("makes a complex package.json", () => {
27
+ const res = render(_$createComponent(Output, {
28
+ get children() {
29
+ return _$createComponent(PackageJsonFile, {
30
+ name: "test",
31
+ version: "1.0.0",
32
+ author: "Brian Terlson",
33
+ license: "MIT",
34
+ description: "A test package!",
35
+ homepage: "https://example.com",
36
+ repository: "https://github.com/alloy-project/alloy",
37
+ keywords: ["test", "package"],
38
+ type: "commonjs",
39
+ scripts: {
40
+ build: "tsc -p .",
41
+ test: "vitest run",
42
+ "test:watch": "vitest -w"
43
+ },
44
+ dependencies: {
45
+ "@alloy-js/core": "^0.1.0",
46
+ "@alloy-js/typescript": "^0.1.0"
47
+ },
48
+ devDependencies: {
49
+ typescript: "^5.5.2"
50
+ },
51
+ exports: {
52
+ ".": "./lib/index.js",
53
+ "./complex": {
54
+ main: "./lib/index.js",
55
+ export: "./lib/index.mjs"
56
+ }
57
+ }
58
+ });
59
+ }
60
+ }));
61
+ assertFileContents(res, {
62
+ "package.json": `
63
+ {
64
+ "name": "test",
65
+ "version": "1.0.0",
66
+ "author": "Brian Terlson",
67
+ "description": "A test package!",
68
+ "license": "MIT",
69
+ "homepage": "https://example.com",
70
+ "type": "commonjs",
71
+ "dependencies": {
72
+ "@alloy-js/core": "^0.1.0",
73
+ "@alloy-js/typescript": "^0.1.0"
74
+ },
75
+ "devDependencies": {
76
+ "typescript": "^5.5.2"
77
+ },
78
+ "scripts": {
79
+ "build": "tsc -p .",
80
+ "test": "vitest run",
81
+ "test:watch": "vitest -w"
82
+ },
83
+ "exports": {
84
+ ".": "./lib/index.js",
85
+ "./complex": {
86
+ "main": "./lib/index.js",
87
+ "export": "./lib/index.mjs"
88
+ }
89
+ }
90
+ }
91
+ `
92
+ });
93
+ });
@@ -0,0 +1,136 @@
1
+ import { createComponent as _$createComponent, memo as _$memo, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, refkey, render } from "@alloy-js/core";
3
+ import "@alloy-js/core/testing";
4
+ import { it } from "vitest";
5
+ import * as ts from "../src/components/index.js";
6
+ import { PackageDirectory } from "../src/components/PackageDirectory.js";
7
+ import { assertFileContents } from "./utils.js";
8
+ it("exports source files", () => {
9
+ const res = render(_$createComponent(Output, {
10
+ get children() {
11
+ return _$createComponent(PackageDirectory, {
12
+ name: "greeting-js",
13
+ path: ".",
14
+ version: "1.0.0",
15
+ get children() {
16
+ return [_$createComponent(ts.SourceFile, {
17
+ path: "greeting.ts",
18
+ get children() {
19
+ return _$createComponent(ts.FunctionDeclaration, {
20
+ name: "getGreeting",
21
+ get refkey() {
22
+ return refkey("getGreeting");
23
+ },
24
+ children: "return \"Hello world!\";"
25
+ });
26
+ }
27
+ }), _$createComponent(ts.SourceFile, {
28
+ "export": true,
29
+ path: "printing.ts",
30
+ get children() {
31
+ return [_$createComponent(ts.VarDeclaration, {
32
+ name: "greeting",
33
+ get children() {
34
+ return [_$memo(() => refkey("getGreeting")), "()"];
35
+ }
36
+ }), ";", _$createIntrinsic("hbr", {}), _$createComponent(ts.FunctionDeclaration, {
37
+ "export": true,
38
+ name: "printGreeting",
39
+ get refkey() {
40
+ return refkey("printGreeting");
41
+ },
42
+ children: "console.log(greeting);"
43
+ })];
44
+ }
45
+ }), _$createComponent(ts.BarrelFile, {
46
+ "export": "."
47
+ })];
48
+ }
49
+ });
50
+ }
51
+ }));
52
+ assertFileContents(res, {
53
+ "greeting.ts": `
54
+ function getGreeting() {
55
+ return "Hello world!";
56
+ }
57
+ `,
58
+ "printing.ts": `
59
+ import { getGreeting } from "./greeting.js";
60
+
61
+ const greeting = getGreeting();
62
+ export function printGreeting() {
63
+ console.log(greeting);
64
+ }
65
+ `,
66
+ "index.ts": `
67
+ export * from "./greeting.js";
68
+ export * from "./printing.js";
69
+ `,
70
+ "package.json": `
71
+ {
72
+ "name": "greeting-js",
73
+ "version": "1.0.0",
74
+ "type": "module",
75
+ "devDependencies": {
76
+ "typescript": "^5.5.2"
77
+ },
78
+ "exports": {
79
+ "./printing.js": "./dist/printing.js",
80
+ ".": "./dist/index.js"
81
+ }
82
+ }
83
+ `
84
+ });
85
+ });
86
+ it("combines ref'd exports with explicit exports", () => {
87
+ const res = render(_$createComponent(Output, {
88
+ get children() {
89
+ return _$createComponent(PackageDirectory, {
90
+ name: "greeting-js",
91
+ path: ".",
92
+ version: "1.0.0",
93
+ exports: {
94
+ "./foo": "./foo/test.js"
95
+ },
96
+ get children() {
97
+ return _$createComponent(ts.SourceFile, {
98
+ "export": ".",
99
+ path: "index.ts",
100
+ get children() {
101
+ return _$createComponent(ts.FunctionDeclaration, {
102
+ "export": true,
103
+ name: "printGreeting",
104
+ get refkey() {
105
+ return refkey("printGreeting");
106
+ },
107
+ children: "console.log(greeting);"
108
+ });
109
+ }
110
+ });
111
+ }
112
+ });
113
+ }
114
+ }));
115
+ assertFileContents(res, {
116
+ "index.ts": `
117
+ export function printGreeting() {
118
+ console.log(greeting);
119
+ }
120
+ `,
121
+ "package.json": `
122
+ {
123
+ "name": "greeting-js",
124
+ "version": "1.0.0",
125
+ "type": "module",
126
+ "devDependencies": {
127
+ "typescript": "^5.5.2"
128
+ },
129
+ "exports": {
130
+ "./foo": "./foo/test.js",
131
+ ".": "./dist/index.js"
132
+ }
133
+ }
134
+ `
135
+ });
136
+ });