@alloy-js/typescript 0.6.0 → 0.8.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 (230) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/dist/src/components/ArrayExpression.d.ts.map +1 -1
  3. package/dist/src/components/ArrayExpression.js +9 -3
  4. package/dist/src/components/ArrayExpression.js.map +1 -1
  5. package/dist/src/components/BlockScope.d.ts +13 -0
  6. package/dist/src/components/BlockScope.d.ts.map +1 -0
  7. package/dist/src/components/BlockScope.js +20 -0
  8. package/dist/src/components/BlockScope.js.map +1 -0
  9. package/dist/src/components/CallSignature.d.ts +38 -0
  10. package/dist/src/components/CallSignature.d.ts.map +1 -0
  11. package/dist/src/components/CallSignature.js +26 -0
  12. package/dist/src/components/CallSignature.js.map +1 -0
  13. package/dist/src/components/ClassDeclaration.d.ts +3 -4
  14. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  15. package/dist/src/components/ClassDeclaration.js +99 -41
  16. package/dist/src/components/ClassDeclaration.js.map +1 -1
  17. package/dist/src/components/Declaration.d.ts +21 -4
  18. package/dist/src/components/Declaration.d.ts.map +1 -1
  19. package/dist/src/components/Declaration.js +40 -21
  20. package/dist/src/components/Declaration.js.map +1 -1
  21. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  22. package/dist/src/components/EnumDeclaration.js +19 -6
  23. package/dist/src/components/EnumDeclaration.js.map +1 -1
  24. package/dist/src/components/EnumMember.d.ts +8 -0
  25. package/dist/src/components/EnumMember.d.ts.map +1 -1
  26. package/dist/src/components/EnumMember.js +27 -5
  27. package/dist/src/components/EnumMember.js.map +1 -1
  28. package/dist/src/components/ExportStatement.d.ts.map +1 -1
  29. package/dist/src/components/ExportStatement.js +3 -1
  30. package/dist/src/components/ExportStatement.js.map +1 -1
  31. package/dist/src/components/FunctionCallExpression.js +1 -1
  32. package/dist/src/components/FunctionCallExpression.js.map +1 -1
  33. package/dist/src/components/FunctionDeclaration.d.ts +21 -12
  34. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  35. package/dist/src/components/FunctionDeclaration.js +80 -45
  36. package/dist/src/components/FunctionDeclaration.js.map +1 -1
  37. package/dist/src/components/IfStatement.d.ts +25 -0
  38. package/dist/src/components/IfStatement.d.ts.map +1 -0
  39. package/dist/src/components/IfStatement.js +34 -0
  40. package/dist/src/components/IfStatement.js.map +1 -0
  41. package/dist/src/components/Interface.d.ts +10 -2
  42. package/dist/src/components/Interface.d.ts.map +1 -1
  43. package/dist/src/components/Interface.js +66 -6
  44. package/dist/src/components/Interface.js.map +1 -1
  45. package/dist/src/components/JSDoc.d.ts.map +1 -1
  46. package/dist/src/components/JSDoc.js +1 -5
  47. package/dist/src/components/JSDoc.js.map +1 -1
  48. package/dist/src/components/JSDocParam.d.ts +22 -0
  49. package/dist/src/components/JSDocParam.d.ts.map +1 -0
  50. package/dist/src/components/JSDocParam.js +121 -0
  51. package/dist/src/components/JSDocParam.js.map +1 -0
  52. package/dist/src/components/MemberChainExpression.d.ts +2 -2
  53. package/dist/src/components/MemberChainExpression.d.ts.map +1 -1
  54. package/dist/src/components/MemberChainExpression.js +43 -7
  55. package/dist/src/components/MemberChainExpression.js.map +1 -1
  56. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  57. package/dist/src/components/ObjectExpression.js +9 -2
  58. package/dist/src/components/ObjectExpression.js.map +1 -1
  59. package/dist/src/components/PackageDirectory.d.ts +1 -3
  60. package/dist/src/components/PackageDirectory.d.ts.map +1 -1
  61. package/dist/src/components/PackageDirectory.js +9 -16
  62. package/dist/src/components/PackageDirectory.js.map +1 -1
  63. package/dist/src/components/PackageJson.d.ts +39 -0
  64. package/dist/src/components/PackageJson.d.ts.map +1 -1
  65. package/dist/src/components/PackageJson.js +30 -12
  66. package/dist/src/components/PackageJson.js.map +1 -1
  67. package/dist/src/components/PropertyName.d.ts +19 -0
  68. package/dist/src/components/PropertyName.d.ts.map +1 -0
  69. package/dist/src/components/PropertyName.js +36 -0
  70. package/dist/src/components/PropertyName.js.map +1 -0
  71. package/dist/src/components/SingleLineCommentBlock.d.ts +10 -0
  72. package/dist/src/components/SingleLineCommentBlock.d.ts.map +1 -0
  73. package/dist/src/components/SingleLineCommentBlock.js +20 -0
  74. package/dist/src/components/SingleLineCommentBlock.js.map +1 -0
  75. package/dist/src/components/SourceFile.d.ts +6 -0
  76. package/dist/src/components/SourceFile.d.ts.map +1 -1
  77. package/dist/src/components/SourceFile.js +31 -0
  78. package/dist/src/components/SourceFile.js.map +1 -1
  79. package/dist/src/components/SwitchStatement.d.ts +31 -0
  80. package/dist/src/components/SwitchStatement.d.ts.map +1 -0
  81. package/dist/src/components/SwitchStatement.js +48 -0
  82. package/dist/src/components/SwitchStatement.js.map +1 -0
  83. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  84. package/dist/src/components/TypeDeclaration.js +16 -3
  85. package/dist/src/components/TypeDeclaration.js.map +1 -1
  86. package/dist/src/components/VarDeclaration.d.ts +1 -1
  87. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  88. package/dist/src/components/VarDeclaration.js +20 -6
  89. package/dist/src/components/VarDeclaration.js.map +1 -1
  90. package/dist/src/components/index.d.ts +7 -0
  91. package/dist/src/components/index.d.ts.map +1 -1
  92. package/dist/src/components/index.js +7 -0
  93. package/dist/src/components/index.js.map +1 -1
  94. package/dist/src/components/stc/index.d.ts +35 -112
  95. package/dist/src/components/stc/index.d.ts.map +1 -1
  96. package/dist/src/components/stc/index.js +8 -1
  97. package/dist/src/components/stc/index.js.map +1 -1
  98. package/dist/src/context/index.d.ts +2 -0
  99. package/dist/src/context/index.d.ts.map +1 -0
  100. package/dist/src/context/index.js +1 -0
  101. package/dist/src/context/index.js.map +1 -0
  102. package/dist/src/context/private-scope.d.ts +18 -0
  103. package/dist/src/context/private-scope.d.ts.map +1 -0
  104. package/dist/src/context/private-scope.js +9 -0
  105. package/dist/src/context/private-scope.js.map +1 -0
  106. package/dist/src/index.d.ts +1 -0
  107. package/dist/src/index.d.ts.map +1 -1
  108. package/dist/src/index.js +1 -0
  109. package/dist/src/index.js.map +1 -1
  110. package/dist/src/parameter-descriptor.d.ts +60 -0
  111. package/dist/src/parameter-descriptor.d.ts.map +1 -0
  112. package/dist/src/parameter-descriptor.js +1 -0
  113. package/dist/src/parameter-descriptor.js.map +1 -0
  114. package/dist/src/symbols/reference.d.ts.map +1 -1
  115. package/dist/src/symbols/reference.js +55 -14
  116. package/dist/src/symbols/reference.js.map +1 -1
  117. package/dist/src/symbols/ts-member-scope.d.ts +1 -1
  118. package/dist/src/symbols/ts-member-scope.d.ts.map +1 -1
  119. package/dist/src/symbols/ts-member-scope.js +2 -0
  120. package/dist/src/symbols/ts-member-scope.js.map +1 -1
  121. package/dist/src/symbols/ts-output-symbol.d.ts +7 -3
  122. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  123. package/dist/src/symbols/ts-output-symbol.js +14 -2
  124. package/dist/src/symbols/ts-output-symbol.js.map +1 -1
  125. package/dist/src/symbols/ts-package-scope.d.ts +0 -15
  126. package/dist/src/symbols/ts-package-scope.d.ts.map +1 -1
  127. package/dist/src/symbols/ts-package-scope.js +0 -8
  128. package/dist/src/symbols/ts-package-scope.js.map +1 -1
  129. package/dist/src/utils.d.ts +7 -0
  130. package/dist/src/utils.d.ts.map +1 -1
  131. package/dist/src/utils.js +17 -0
  132. package/dist/src/utils.js.map +1 -1
  133. package/dist/test/block-scope.test.d.ts +2 -0
  134. package/dist/test/block-scope.test.d.ts.map +1 -0
  135. package/dist/test/enum-jsdoc.test.d.ts +2 -0
  136. package/dist/test/enum-jsdoc.test.d.ts.map +1 -0
  137. package/dist/test/function-declaration-jsdoc.test.d.ts +2 -0
  138. package/dist/test/function-declaration-jsdoc.test.d.ts.map +1 -0
  139. package/dist/test/if-statement.test.d.ts +2 -0
  140. package/dist/test/if-statement.test.d.ts.map +1 -0
  141. package/dist/test/interface-jsdoc.test.d.ts +2 -0
  142. package/dist/test/interface-jsdoc.test.d.ts.map +1 -0
  143. package/dist/test/jscomment.test.d.ts +2 -0
  144. package/dist/test/jscomment.test.d.ts.map +1 -0
  145. package/dist/test/jsdoc.test.d.ts.map +1 -1
  146. package/dist/test/jsdocparam.test.d.ts +2 -0
  147. package/dist/test/jsdocparam.test.d.ts.map +1 -0
  148. package/dist/test/name-conflict.test.d.ts +2 -0
  149. package/dist/test/name-conflict.test.d.ts.map +1 -0
  150. package/dist/test/package-json.test.d.ts +2 -0
  151. package/dist/test/package-json.test.d.ts.map +1 -0
  152. package/dist/test/source-file.test.d.ts +2 -0
  153. package/dist/test/source-file.test.d.ts.map +1 -0
  154. package/dist/test/switch-statement.test.d.ts +2 -0
  155. package/dist/test/switch-statement.test.d.ts.map +1 -0
  156. package/dist/test/var-declaration-jsdoc.test.d.ts +2 -0
  157. package/dist/test/var-declaration-jsdoc.test.d.ts.map +1 -0
  158. package/dist/tsconfig.tsbuildinfo +1 -1
  159. package/package.json +3 -3
  160. package/src/components/ArrayExpression.tsx +13 -8
  161. package/src/components/BlockScope.tsx +31 -0
  162. package/src/components/CallSignature.tsx +67 -0
  163. package/src/components/ClassDeclaration.tsx +103 -50
  164. package/src/components/Declaration.tsx +72 -22
  165. package/src/components/EnumDeclaration.tsx +27 -19
  166. package/src/components/EnumMember.tsx +24 -4
  167. package/src/components/ExportStatement.tsx +3 -1
  168. package/src/components/FunctionCallExpression.tsx +1 -1
  169. package/src/components/FunctionDeclaration.tsx +107 -81
  170. package/src/components/IfStatement.tsx +51 -0
  171. package/src/components/Interface.tsx +61 -14
  172. package/src/components/JSDoc.tsx +1 -10
  173. package/src/components/JSDocParam.tsx +115 -0
  174. package/src/components/MemberChainExpression.tsx +37 -5
  175. package/src/components/ObjectExpression.tsx +7 -2
  176. package/src/components/PackageDirectory.tsx +12 -22
  177. package/src/components/PackageJson.tsx +86 -27
  178. package/src/components/PropertyName.tsx +50 -0
  179. package/src/components/SingleLineCommentBlock.tsx +21 -0
  180. package/src/components/SourceFile.tsx +29 -0
  181. package/src/components/SwitchStatement.tsx +78 -0
  182. package/src/components/TypeDeclaration.tsx +11 -4
  183. package/src/components/VarDeclaration.tsx +20 -12
  184. package/src/components/index.ts +7 -0
  185. package/src/components/stc/index.ts +8 -1
  186. package/src/context/index.ts +1 -0
  187. package/src/context/private-scope.ts +23 -0
  188. package/src/index.ts +1 -0
  189. package/src/parameter-descriptor.ts +71 -0
  190. package/src/symbols/reference.ts +77 -22
  191. package/src/symbols/ts-member-scope.ts +6 -2
  192. package/src/symbols/ts-output-symbol.ts +27 -10
  193. package/src/symbols/ts-package-scope.ts +0 -28
  194. package/src/utils.ts +33 -0
  195. package/temp/api.json +5372 -3410
  196. package/test/array.test.tsx +72 -0
  197. package/test/basic.test.tsx +2 -2
  198. package/test/block-scope.test.tsx +29 -0
  199. package/test/class.test.tsx +173 -5
  200. package/test/enum-jsdoc.test.tsx +74 -0
  201. package/test/enum.test.tsx +50 -3
  202. package/test/function-call-expression.test.tsx +5 -2
  203. package/test/function-declaration-jsdoc.test.tsx +89 -0
  204. package/test/function-declaration.test.tsx +13 -9
  205. package/test/if-statement.test.tsx +28 -0
  206. package/test/imports.test.tsx +25 -10
  207. package/test/interface-jsdoc.test.tsx +164 -0
  208. package/test/interface.test.tsx +17 -3
  209. package/test/jscomment.test.tsx +104 -0
  210. package/test/jsdoc.test.tsx +9 -15
  211. package/test/jsdocparam.test.tsx +188 -0
  212. package/test/member-chain.test.tsx +77 -0
  213. package/test/name-conflict.test.tsx +105 -0
  214. package/test/name-policy.test.tsx +1 -1
  215. package/test/package-external.test.tsx +98 -3
  216. package/test/package-json.test.tsx +93 -0
  217. package/test/package.test.tsx +56 -3
  218. package/test/reference.test.tsx +6 -2
  219. package/test/source-file.test.tsx +38 -0
  220. package/test/switch-statement.test.tsx +50 -0
  221. package/test/value-expression.test.tsx +9 -0
  222. package/test/var-declaration-jsdoc.test.tsx +70 -0
  223. package/test/var-declaration.test.tsx +1 -1
  224. package/tsconfig.json +1 -8
  225. package/vitest.config.ts +2 -10
  226. package/dist/src/components/JSDocParagraph.d.ts +0 -10
  227. package/dist/src/components/JSDocParagraph.d.ts.map +0 -1
  228. package/dist/src/components/JSDocParagraph.js +0 -23
  229. package/dist/src/components/JSDocParagraph.js.map +0 -1
  230. package/src/components/JSDocParagraph.tsx +0 -35
@@ -0,0 +1,188 @@
1
+ import { d } from "@alloy-js/core/testing";
2
+ import { expect, it } from "vitest";
3
+ import { JSDocParam } from "../src/components/JSDocParam.jsx";
4
+ import { JSDoc } from "../src/index.js";
5
+
6
+ it("name only", () => {
7
+ const template = (
8
+ <JSDoc>
9
+ <JSDocParam name="somebody" />
10
+ </JSDoc>
11
+ );
12
+
13
+ expect(template).toRenderTo(
14
+ d`
15
+ /**
16
+ * @param somebody
17
+ **/
18
+ `,
19
+ );
20
+ });
21
+
22
+ it("name and type", () => {
23
+ const template = (
24
+ <JSDoc>
25
+ <JSDocParam name="somebody" type="string" />
26
+ </JSDoc>
27
+ );
28
+
29
+ expect(template).toRenderTo(
30
+ d`
31
+ /**
32
+ * @param {string} somebody
33
+ **/
34
+ `,
35
+ );
36
+ });
37
+
38
+ it("name, type and description", () => {
39
+ const template = (
40
+ <JSDoc>
41
+ <JSDocParam name="somebody" type="string">
42
+ Somebody's name.
43
+ </JSDocParam>
44
+ </JSDoc>
45
+ );
46
+
47
+ expect(template).toRenderTo(
48
+ d`
49
+ /**
50
+ * @param {string} somebody Somebody's name.
51
+ **/
52
+ `,
53
+ );
54
+ });
55
+
56
+ it("name, type and description with hyphen", () => {
57
+ const template = (
58
+ <JSDoc>
59
+ <JSDocParam name="somebody" type="string" hyphen>
60
+ Somebody's name.
61
+ </JSDocParam>
62
+ </JSDoc>
63
+ );
64
+
65
+ expect(template).toRenderTo(
66
+ d`
67
+ /**
68
+ * @param {string} somebody - Somebody's name.
69
+ **/
70
+ `,
71
+ );
72
+ });
73
+
74
+ it("name, type, description, hyphen and optional", () => {
75
+ const template = (
76
+ <JSDoc>
77
+ <JSDocParam name="somebody" type="string" optional hyphen>
78
+ Somebody's name.
79
+ </JSDocParam>
80
+ </JSDoc>
81
+ );
82
+
83
+ expect(template).toRenderTo(
84
+ d`
85
+ /**
86
+ * @param {string} [somebody] - Somebody's name.
87
+ **/
88
+ `,
89
+ );
90
+ });
91
+
92
+ it("name, type, description, hyphen and optional with default value", () => {
93
+ const template = (
94
+ <JSDoc>
95
+ <JSDocParam
96
+ name="somebody"
97
+ type="string"
98
+ optional
99
+ defaultValue="John Doe"
100
+ hyphen
101
+ >
102
+ Somebody's name.
103
+ </JSDocParam>
104
+ </JSDoc>
105
+ );
106
+
107
+ expect(template).toRenderTo(
108
+ d`
109
+ /**
110
+ * @param {string} [somebody=John Doe] - Somebody's name.
111
+ **/
112
+ `,
113
+ );
114
+ });
115
+
116
+ it("name, type, description, hyphen and optional with default value with a very lon description", () => {
117
+ const template = (
118
+ <JSDoc>
119
+ <JSDocParam
120
+ name="somebody"
121
+ type="string"
122
+ optional
123
+ defaultValue="John Doe"
124
+ hyphen
125
+ >
126
+ Somebody's name. This can be any string representing a person, whether
127
+ it's a first name, full name, nickname, or even a codename (e.g., "Agent
128
+ X"). It's used primarily for display purposes, logging, or greeting
129
+ messages and is not required to be unique or validated unless specified
130
+ by the caller.
131
+ </JSDocParam>
132
+ <JSDocParam name="somebody2" type="string" hyphen>
133
+ Somebody's name. This can be any string representing a person, whether
134
+ it's a first name, full name, nickname, or even a codename (e.g., "Agent
135
+ X"). It's used primarily for display purposes, logging, or greeting
136
+ messages and is not required to be unique or validated unless specified
137
+ by the caller.
138
+ </JSDocParam>
139
+ </JSDoc>
140
+ );
141
+
142
+ expect(template).toRenderTo(
143
+ d`
144
+ /**
145
+ * @param {string} [somebody=John Doe] - Somebody's name. This can be any string
146
+ * representing a person, whether it's a first name, full name, nickname, or
147
+ * even a codename (e.g., "Agent X"). It's used primarily for display
148
+ * purposes, logging, or greeting messages and is not required to be unique or
149
+ * validated unless specified by the caller.
150
+ *
151
+ * @param {string} somebody2 - Somebody's name. This can be any string
152
+ * representing a person, whether it's a first name, full name, nickname, or
153
+ * even a codename (e.g., "Agent X"). It's used primarily for display
154
+ * purposes, logging, or greeting messages and is not required to be unique or
155
+ * validated unless specified by the caller.
156
+ **/
157
+ `,
158
+ { printWidth: 80 },
159
+ );
160
+ });
161
+
162
+ it("name, type, description, hyphen and optional with default value with a description containing a linebreak", () => {
163
+ const template = (
164
+ <JSDoc>
165
+ <JSDocParam
166
+ name="somebody"
167
+ type="string"
168
+ optional
169
+ defaultValue="John Doe"
170
+ hyphen
171
+ >
172
+ Somebody's name. This is one line
173
+ <hbr />
174
+ This is another line
175
+ </JSDocParam>
176
+ </JSDoc>
177
+ );
178
+
179
+ expect(template).toRenderTo(
180
+ d`
181
+ /**
182
+ * @param {string} [somebody=John Doe] - Somebody's name. This is one line
183
+ * This is another line
184
+ **/
185
+ `,
186
+ { printWidth: 80 },
187
+ );
188
+ });
@@ -45,3 +45,80 @@ it("works with args which break", () => {
45
45
  .array()
46
46
  `);
47
47
  });
48
+
49
+ it("works with args when it doesn't break", () => {
50
+ expect(
51
+ toSourceText(
52
+ <MemberChainExpression>
53
+ <>z</>
54
+ <FunctionCallExpression
55
+ target="object"
56
+ args={[<ValueExpression jsValue={{ a: 1, b: 2 }} />]}
57
+ />
58
+ </MemberChainExpression>,
59
+ ),
60
+ ).toBe(d`
61
+ z.object({
62
+ a: 1,
63
+ b: 2,
64
+ })
65
+ `);
66
+
67
+ expect(
68
+ toSourceText(
69
+ <MemberChainExpression>
70
+ <>z</>
71
+ <>z2</>
72
+ <>z3</>
73
+ <FunctionCallExpression
74
+ target="object"
75
+ args={[<ValueExpression jsValue={{ a: 1, b: 2 }} />]}
76
+ />
77
+ </MemberChainExpression>,
78
+ ),
79
+ ).toBe(d`
80
+ z.z2.z3.object({
81
+ a: 1,
82
+ b: 2,
83
+ })
84
+ `);
85
+ });
86
+
87
+ it("works with nested call member chains", () => {
88
+ expect(
89
+ toSourceText(
90
+ <MemberChainExpression>
91
+ <>z</>
92
+ <FunctionCallExpression target="string" />
93
+ <FunctionCallExpression target="array" />
94
+ <MemberChainExpression>
95
+ <FunctionCallExpression target="min" args={[5]} />
96
+ <FunctionCallExpression target="max" args={[10]} />
97
+ </MemberChainExpression>
98
+ <FunctionCallExpression target="array" />
99
+ </MemberChainExpression>,
100
+ { printWidth: 10 },
101
+ ),
102
+ ).toBe(d`
103
+ z.string()
104
+ .array()
105
+ .min(5)
106
+ .max(10)
107
+ .array()
108
+ `);
109
+ });
110
+
111
+ it("works without function call expressions", () => {
112
+ expect(
113
+ toSourceText(
114
+ <MemberChainExpression>
115
+ <>z</>
116
+ <>string</>
117
+ <>min</>
118
+ </MemberChainExpression>,
119
+ { printWidth: 5 },
120
+ ),
121
+ ).toBe(d`
122
+ z.string.min
123
+ `);
124
+ });
@@ -0,0 +1,105 @@
1
+ import { Output, render } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { camelCase } from "change-case";
4
+ import { expect, it } from "vitest";
5
+ import {
6
+ FunctionDeclaration,
7
+ SourceFile,
8
+ TSOutputSymbol,
9
+ TSSymbolFlags,
10
+ } from "../src/index.js";
11
+ import { ParameterDescriptor } from "../src/parameter-descriptor.js";
12
+ it("handles custom name conflict resolver based on metadata", () => {
13
+ function resolver(name: string, symbols: TSOutputSymbol[]) {
14
+ const goodNamedSymbols = symbols.filter(
15
+ (s) => ~s.tsFlags & TSSymbolFlags.LocalImportSymbol,
16
+ );
17
+ const badNamedSymbols = symbols.filter(
18
+ (s) => s.tsFlags & TSSymbolFlags.LocalImportSymbol,
19
+ );
20
+ let nameCount = 1;
21
+
22
+ // always rename local import symbols (we don't care about them)
23
+ for (const sym of badNamedSymbols) {
24
+ sym.name = name + "_" + nameCount++;
25
+ }
26
+
27
+ // otherwise check the symbol metadata to see how to rename.
28
+
29
+ // group by symbol metadata
30
+ const groupedSymbols = new Map<string | undefined, TSOutputSymbol[]>([
31
+ ["body", []],
32
+ ["query", []],
33
+ ["header", []],
34
+ [undefined, []],
35
+ ]);
36
+ for (const sym of goodNamedSymbols) {
37
+ const sourceLocation = sym.metadata.sourceLocation as
38
+ | "body"
39
+ | "query"
40
+ | "header"
41
+ | undefined;
42
+ groupedSymbols.get(sourceLocation)!.push(sym);
43
+ }
44
+
45
+ for (const [sourceLocation, symbols] of groupedSymbols) {
46
+ let locationCount = 0;
47
+ if (sourceLocation === undefined) {
48
+ for (const sym of symbols) {
49
+ if (locationCount > 0) {
50
+ sym.name = name + locationCount;
51
+ }
52
+
53
+ locationCount++;
54
+ }
55
+ } else if (symbols.length < 0) {
56
+ continue;
57
+ } else if (symbols.length === 1) {
58
+ symbols[0].name = camelCase(name + "_" + sourceLocation);
59
+ } else {
60
+ for (const sym of symbols) {
61
+ sym.name = camelCase(
62
+ name + "_" + sourceLocation + locationCount++,
63
+ {},
64
+ );
65
+ }
66
+ }
67
+ }
68
+ }
69
+
70
+ const testParameters: ParameterDescriptor[] = [
71
+ { name: "foo" },
72
+ { name: "foo" },
73
+ { name: "foo", metadata: { sourceLocation: "body" } },
74
+ { name: "foo", metadata: { sourceLocation: "query" } },
75
+ { name: "foo", metadata: { sourceLocation: "query" } },
76
+ { name: "foo", metadata: { sourceLocation: "header" } },
77
+ { name: "foo", metadata: { sourceLocation: "header" } },
78
+ { name: "foo", metadata: { sourceLocation: "header" } },
79
+ ];
80
+
81
+ const res = render(
82
+ <Output nameConflictResolver={resolver}>
83
+ <SourceFile path="test.ts">
84
+ <FunctionDeclaration
85
+ export
86
+ name="conflicty"
87
+ parameters={testParameters}
88
+ />
89
+ </SourceFile>
90
+ </Output>,
91
+ );
92
+
93
+ expect(res.contents[0].contents).toBe(d`
94
+ export function conflicty(
95
+ foo,
96
+ foo1,
97
+ fooBody,
98
+ fooQuery0,
99
+ fooQuery1,
100
+ fooHeader0,
101
+ fooHeader1,
102
+ fooHeader2,
103
+ ) {}
104
+ `);
105
+ });
@@ -74,7 +74,7 @@ it("appends _ to reserved words", () => {
74
74
  <ts.FunctionDeclaration
75
75
  name="default"
76
76
  refkey={ref1}
77
- parameters={{ await: "any" }}
77
+ parameters={[{ name: "await", type: "any" }]}
78
78
  />
79
79
  <hbr />
80
80
  <StatementList>
@@ -13,13 +13,20 @@ it("imports external packages", () => {
13
13
  version="1.0.0"
14
14
  >
15
15
  <ts.SourceFile path="greetings.ts">
16
- <ts.FunctionDeclaration name="getGreeting">
16
+ <ts.FunctionDeclaration
17
+ name="getGreeting"
18
+ refkey={refkey("getGreeting")}
19
+ >
17
20
  return "Hello world!";
18
21
  </ts.FunctionDeclaration>
19
22
  </ts.SourceFile>
20
23
 
21
24
  <ts.SourceFile path="logGreetings.ts">
22
- <ts.FunctionDeclaration export name="printGreeting">
25
+ <ts.FunctionDeclaration
26
+ export
27
+ name="printGreeting"
28
+ refkey={refkey("printGreeting")}
29
+ >
23
30
  console.log("Hello world!");
24
31
  </ts.FunctionDeclaration>
25
32
  </ts.SourceFile>
@@ -62,7 +69,95 @@ it("imports external packages", () => {
62
69
  "name": "greeting-lib",
63
70
  "version": "1.0.0",
64
71
  "type": "module",
65
- "dependencies": {},
72
+ "devDependencies": {
73
+ "typescript": "^5.5.2"
74
+ },
75
+ "exports": {
76
+ ".": "./dist/index.js"
77
+ }
78
+ }
79
+ `);
80
+
81
+ expect(refFile.contents).toEqual(d`
82
+ import { getGreeting } from "greeting-lib";
83
+
84
+ getGreeting();
85
+ `);
86
+ });
87
+
88
+ it("combines explicit dependencies with referenced dependencies", () => {
89
+ const res = render(
90
+ <Output>
91
+ <ts.PackageDirectory
92
+ name="greeting-lib"
93
+ path="greeting-lib"
94
+ version="1.0.0"
95
+ >
96
+ <ts.SourceFile path="greetings.ts">
97
+ <ts.FunctionDeclaration
98
+ name="getGreeting"
99
+ refkey={refkey("getGreeting")}
100
+ >
101
+ return "Hello world!";
102
+ </ts.FunctionDeclaration>
103
+ </ts.SourceFile>
104
+
105
+ <ts.SourceFile path="logGreetings.ts">
106
+ <ts.FunctionDeclaration
107
+ export
108
+ name="printGreeting"
109
+ refkey={refkey("printGreeting")}
110
+ >
111
+ console.log("Hello world!");
112
+ </ts.FunctionDeclaration>
113
+ </ts.SourceFile>
114
+
115
+ <ts.BarrelFile export="." />
116
+ </ts.PackageDirectory>
117
+
118
+ <ts.PackageDirectory
119
+ name="consumer"
120
+ path="consumer"
121
+ version="1.0.0"
122
+ dependencies={{
123
+ foo: "bar",
124
+ }}
125
+ >
126
+ <ts.SourceFile export="." path="ref.ts">
127
+ <ts.Reference refkey={refkey("getGreeting")} />
128
+ ();
129
+ </ts.SourceFile>
130
+ </ts.PackageDirectory>
131
+ </Output>,
132
+ );
133
+
134
+ const consumerPkgJson = findFile(res, "consumer/package.json")!;
135
+ const greetingPkgJson = findFile(res, "greeting-lib/package.json")!;
136
+ const refFile = findFile(res, "consumer/ref.ts")!;
137
+
138
+ expect(consumerPkgJson.contents).toEqual(d`
139
+ {
140
+ "name": "consumer",
141
+ "version": "1.0.0",
142
+ "type": "module",
143
+ "dependencies": {
144
+ "foo": "bar",
145
+ "greeting-lib": "1.0.0"
146
+ },
147
+ "devDependencies": {
148
+ "typescript": "^5.5.2"
149
+ },
150
+ "exports": {
151
+ ".": "./dist/ref.js"
152
+ }
153
+ }
154
+ `);
155
+
156
+ expect(greetingPkgJson.contents).toEqual(d`
157
+ {
158
+ "name": "greeting-lib",
159
+ "version": "1.0.0",
160
+ "type": "module",
66
161
  "devDependencies": {
67
162
  "typescript": "^5.5.2"
68
163
  },
@@ -0,0 +1,93 @@
1
+ import { Output, render } from "@alloy-js/core";
2
+ import "@alloy-js/core/testing";
3
+ import { it } from "vitest";
4
+ import { PackageJsonFile } from "../src/components/PackageJson.jsx";
5
+ import { assertFileContents } from "./utils.js";
6
+
7
+ it("creates a simple package.json", () => {
8
+ const res = render(
9
+ <Output>
10
+ <PackageJsonFile name="test" version="1.0.0" />
11
+ </Output>,
12
+ );
13
+
14
+ assertFileContents(res, {
15
+ "package.json": `
16
+ {
17
+ "name": "test",
18
+ "version": "1.0.0",
19
+ "type": "module"
20
+ }
21
+ `,
22
+ });
23
+ });
24
+
25
+ it("makes a complex package.json", () => {
26
+ const res = render(
27
+ <Output>
28
+ <PackageJsonFile
29
+ name="test"
30
+ version="1.0.0"
31
+ author="Brian Terlson"
32
+ license="MIT"
33
+ description="A test package!"
34
+ homepage="https://example.com"
35
+ repository="https://github.com/alloy-project/alloy"
36
+ keywords={["test", "package"]}
37
+ type="commonjs"
38
+ scripts={{
39
+ build: "tsc -p .",
40
+ test: "vitest run",
41
+ "test:watch": "vitest -w",
42
+ }}
43
+ dependencies={{
44
+ "@alloy-js/core": "^0.1.0",
45
+ "@alloy-js/typescript": "^0.1.0",
46
+ }}
47
+ devDependencies={{
48
+ typescript: "^5.5.2",
49
+ }}
50
+ exports={{
51
+ ".": "./lib/index.js",
52
+ "./complex": {
53
+ main: "./lib/index.js",
54
+ export: "./lib/index.mjs",
55
+ },
56
+ }}
57
+ />
58
+ </Output>,
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
+ });
@@ -10,7 +10,10 @@ it("exports source files", () => {
10
10
  <Output>
11
11
  <PackageDirectory name="greeting-js" path="." version="1.0.0">
12
12
  <ts.SourceFile path="greeting.ts">
13
- <ts.FunctionDeclaration name="getGreeting">
13
+ <ts.FunctionDeclaration
14
+ name="getGreeting"
15
+ refkey={refkey("getGreeting")}
16
+ >
14
17
  return "Hello world!";
15
18
  </ts.FunctionDeclaration>
16
19
  </ts.SourceFile>
@@ -20,7 +23,11 @@ it("exports source files", () => {
20
23
  {refkey("getGreeting")}()
21
24
  </ts.VarDeclaration>
22
25
  ;<hbr />
23
- <ts.FunctionDeclaration export name="printGreeting">
26
+ <ts.FunctionDeclaration
27
+ export
28
+ name="printGreeting"
29
+ refkey={refkey("printGreeting")}
30
+ >
24
31
  console.log(greeting);
25
32
  </ts.FunctionDeclaration>
26
33
  </ts.SourceFile>
@@ -53,7 +60,6 @@ it("exports source files", () => {
53
60
  "name": "greeting-js",
54
61
  "version": "1.0.0",
55
62
  "type": "module",
56
- "dependencies": {},
57
63
  "devDependencies": {
58
64
  "typescript": "^5.5.2"
59
65
  },
@@ -65,3 +71,50 @@ it("exports source files", () => {
65
71
  `,
66
72
  });
67
73
  });
74
+
75
+ it("combines ref'd exports with explicit exports", () => {
76
+ const res = render(
77
+ <Output>
78
+ <PackageDirectory
79
+ name="greeting-js"
80
+ path="."
81
+ version="1.0.0"
82
+ exports={{
83
+ "./foo": "./foo/test.js",
84
+ }}
85
+ >
86
+ <ts.SourceFile export="." path="index.ts">
87
+ <ts.FunctionDeclaration
88
+ export
89
+ name="printGreeting"
90
+ refkey={refkey("printGreeting")}
91
+ >
92
+ console.log(greeting);
93
+ </ts.FunctionDeclaration>
94
+ </ts.SourceFile>
95
+ </PackageDirectory>
96
+ </Output>,
97
+ );
98
+
99
+ assertFileContents(res, {
100
+ "index.ts": `
101
+ export function printGreeting() {
102
+ console.log(greeting);
103
+ }
104
+ `,
105
+ "package.json": `
106
+ {
107
+ "name": "greeting-js",
108
+ "version": "1.0.0",
109
+ "type": "module",
110
+ "devDependencies": {
111
+ "typescript": "^5.5.2"
112
+ },
113
+ "exports": {
114
+ "./foo": "./foo/test.js",
115
+ ".": "./dist/index.js"
116
+ }
117
+ }
118
+ `,
119
+ });
120
+ });
@@ -9,7 +9,9 @@ it("works with back references", () => {
9
9
  const res = render(
10
10
  <Output>
11
11
  <ts.SourceFile path="test1.ts">
12
- <Declaration name="foo">const foo = 1;</Declaration>
12
+ <Declaration name="foo" refkey={refkey("foo")}>
13
+ const foo = 1;
14
+ </Declaration>
13
15
  </ts.SourceFile>
14
16
 
15
17
  <ts.SourceFile path="test2.ts">
@@ -37,7 +39,9 @@ it("works with forward references", () => {
37
39
  const v = <Reference refkey={refkey("foo")} />;
38
40
  </ts.SourceFile>
39
41
  <ts.SourceFile path="test1.ts">
40
- <Declaration name="foo">const foo = 1;</Declaration>
42
+ <Declaration name="foo" refkey={refkey("foo")}>
43
+ const foo = 1;
44
+ </Declaration>
41
45
  </ts.SourceFile>
42
46
  </Output>,
43
47
  );