@alloy-js/typescript 0.11.0 → 0.13.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 (95) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/src/components/ClassDeclaration.d.ts +2 -0
  3. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  4. package/dist/src/components/ClassDeclaration.js +14 -2
  5. package/dist/src/components/FunctionBase.d.ts.map +1 -1
  6. package/dist/src/components/FunctionBase.js +13 -3
  7. package/dist/src/components/FunctionType.d.ts +7 -8
  8. package/dist/src/components/FunctionType.d.ts.map +1 -1
  9. package/dist/src/components/FunctionType.js +29 -25
  10. package/dist/src/components/ImportStatement.d.ts.map +1 -1
  11. package/dist/src/components/ImportStatement.js +9 -2
  12. package/dist/src/components/Interface.d.ts +3 -2
  13. package/dist/src/components/Interface.d.ts.map +1 -1
  14. package/dist/src/components/Interface.js +8 -4
  15. package/dist/src/components/JSDocComment.js +1 -1
  16. package/dist/src/components/MemberDeclaration.d.ts +4 -0
  17. package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
  18. package/dist/src/components/MemberDeclaration.js +1 -1
  19. package/dist/src/components/MemberExpression.d.ts.map +1 -1
  20. package/dist/src/components/MemberExpression.js +3 -2
  21. package/dist/src/components/Reference.d.ts +8 -1
  22. package/dist/src/components/Reference.d.ts.map +1 -1
  23. package/dist/src/components/Reference.js +7 -2
  24. package/dist/src/components/TypeDeclaration.d.ts +1 -1
  25. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  26. package/dist/src/components/TypeDeclaration.js +3 -2
  27. package/dist/src/components/TypeRefContext.d.ts +22 -0
  28. package/dist/src/components/TypeRefContext.d.ts.map +1 -0
  29. package/dist/src/components/TypeRefContext.js +41 -0
  30. package/dist/src/components/VarDeclaration.d.ts +1 -0
  31. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  32. package/dist/src/components/VarDeclaration.js +9 -3
  33. package/dist/src/components/index.d.ts +1 -0
  34. package/dist/src/components/index.d.ts.map +1 -1
  35. package/dist/src/components/index.js +1 -0
  36. package/dist/src/components/stc/index.d.ts +1 -0
  37. package/dist/src/components/stc/index.d.ts.map +1 -1
  38. package/dist/src/components/stc/index.js +1 -0
  39. package/dist/src/context/type-ref-context.d.ts +15 -0
  40. package/dist/src/context/type-ref-context.d.ts.map +1 -0
  41. package/dist/src/context/type-ref-context.js +12 -0
  42. package/dist/src/parameter-descriptor.d.ts +6 -0
  43. package/dist/src/parameter-descriptor.d.ts.map +1 -1
  44. package/dist/src/symbols/reference.d.ts +4 -1
  45. package/dist/src/symbols/reference.d.ts.map +1 -1
  46. package/dist/src/symbols/reference.js +11 -5
  47. package/dist/src/symbols/ts-module-scope.d.ts +4 -1
  48. package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
  49. package/dist/src/symbols/ts-module-scope.js +10 -3
  50. package/dist/src/symbols/ts-output-symbol.d.ts +2 -1
  51. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  52. package/dist/src/symbols/ts-output-symbol.js +1 -0
  53. package/dist/test/class.test.js +108 -5
  54. package/dist/test/enum-jsdoc.test.js +6 -6
  55. package/dist/test/function-declaration-jsdoc.test.js +3 -3
  56. package/dist/test/imports.test.d.ts.map +1 -1
  57. package/dist/test/imports.test.js +293 -2
  58. package/dist/test/interface-jsdoc.test.js +9 -9
  59. package/dist/test/jsdoc.test.js +5 -5
  60. package/dist/test/jsdocparam.test.js +8 -8
  61. package/dist/test/member-expression.test.js +218 -2
  62. package/dist/test/var-declaration-jsdoc.test.js +3 -3
  63. package/dist/test/var-declaration.test.js +1 -1
  64. package/dist/tsconfig.tsbuildinfo +1 -1
  65. package/package.json +4 -4
  66. package/src/components/ClassDeclaration.tsx +19 -3
  67. package/src/components/FunctionBase.tsx +14 -3
  68. package/src/components/FunctionType.tsx +45 -40
  69. package/src/components/ImportStatement.tsx +30 -3
  70. package/src/components/Interface.tsx +41 -34
  71. package/src/components/JSDocComment.tsx +1 -1
  72. package/src/components/MemberDeclaration.tsx +7 -1
  73. package/src/components/MemberExpression.tsx +7 -2
  74. package/src/components/Reference.tsx +12 -2
  75. package/src/components/TypeDeclaration.tsx +16 -13
  76. package/src/components/TypeRefContext.tsx +49 -0
  77. package/src/components/VarDeclaration.tsx +6 -2
  78. package/src/components/index.ts +1 -0
  79. package/src/components/stc/index.ts +1 -0
  80. package/src/context/type-ref-context.tsx +19 -0
  81. package/src/parameter-descriptor.ts +7 -0
  82. package/src/symbols/reference.ts +17 -5
  83. package/src/symbols/ts-module-scope.ts +19 -4
  84. package/src/symbols/ts-output-symbol.ts +1 -0
  85. package/temp/api.json +613 -220
  86. package/test/class.test.tsx +75 -5
  87. package/test/enum-jsdoc.test.tsx +6 -6
  88. package/test/function-declaration-jsdoc.test.tsx +3 -3
  89. package/test/imports.test.tsx +215 -1
  90. package/test/interface-jsdoc.test.tsx +9 -9
  91. package/test/jsdoc.test.tsx +5 -5
  92. package/test/jsdocparam.test.tsx +8 -8
  93. package/test/member-expression.test.tsx +171 -2
  94. package/test/var-declaration-jsdoc.test.tsx +3 -3
  95. package/test/var-declaration.test.tsx +1 -1
@@ -1,9 +1,19 @@
1
- import { refkey, StatementList } from "@alloy-js/core";
1
+ import { code, List, refkey, StatementList } from "@alloy-js/core";
2
2
  import { d } from "@alloy-js/core/testing";
3
3
  import { describe, expect, it } from "vitest";
4
4
  import { MemberExpression } from "../src/components/MemberExpression.jsx";
5
- import { ObjectExpression } from "../src/components/stc/index.js";
5
+ import {
6
+ InterfaceMember,
7
+ ObjectExpression,
8
+ } from "../src/components/stc/index.js";
6
9
  import { VarDeclaration } from "../src/components/VarDeclaration.jsx";
10
+ import {
11
+ ClassDeclaration,
12
+ ClassField,
13
+ FunctionDeclaration,
14
+ InterfaceDeclaration,
15
+ ParameterDescriptor,
16
+ } from "../src/index.js";
7
17
  import { toSourceText } from "./utils.js";
8
18
 
9
19
  it("renders basic member expression with dot notation", () => {
@@ -143,6 +153,165 @@ describe("with refkeys", () => {
143
153
  `);
144
154
  });
145
155
 
156
+ it("handles nullish symbols correctly", () => {
157
+ // Note that this is deliberately producing invalid references (it's just easiest to test with VarDeclaration).
158
+ const rk1 = refkey();
159
+ const rk2 = refkey();
160
+ expect(
161
+ toSourceText(
162
+ <StatementList>
163
+ <VarDeclaration name="test1" refkey={rk1} initializer={1} nullish />
164
+ <VarDeclaration name="test1" refkey={rk2} initializer={2} nullish />
165
+ <MemberExpression>
166
+ <MemberExpression.Part refkey={rk1} />
167
+ <MemberExpression.Part refkey={rk2} />
168
+ </MemberExpression>
169
+ </StatementList>,
170
+ ),
171
+ ).toBe(d`
172
+ const test1 = 1;
173
+ const test1_2 = 2;
174
+ test1?.test1_2;
175
+ `);
176
+ });
177
+
178
+ it("handles optional parameters correctly", () => {
179
+ const fooRef = refkey();
180
+ const modelRef = refkey();
181
+ const parameters: ParameterDescriptor[] = [
182
+ { name: "foo", optional: true, refkey: fooRef, type: modelRef },
183
+ ];
184
+ const messageRef = refkey();
185
+ const template = (
186
+ <List hardline>
187
+ <InterfaceDeclaration name="Model" refkey={modelRef}>
188
+ <InterfaceMember name="bar" refkey={refkey()} type="string" />
189
+ </InterfaceDeclaration>
190
+ <FunctionDeclaration name="fooFunction" parameters={parameters}>
191
+ <StatementList>
192
+ <VarDeclaration
193
+ name="message"
194
+ const
195
+ refkey={messageRef}
196
+ initializer={
197
+ <MemberExpression>
198
+ <MemberExpression.Part refkey={fooRef} />
199
+ <MemberExpression.Part id="bar" />
200
+ </MemberExpression>
201
+ }
202
+ />
203
+ <>console.log({messageRef})</>
204
+ </StatementList>
205
+ </FunctionDeclaration>
206
+ </List>
207
+ );
208
+
209
+ expect(toSourceText(template)).toBe(d`
210
+ interface Model {
211
+ bar: string
212
+ }
213
+ function fooFunction(foo?: Model) {
214
+ const message = foo?.bar;
215
+ console.log(message);
216
+ }
217
+ `);
218
+ });
219
+
220
+ it("handles nullish parameters correctly", () => {
221
+ const fooRef = refkey();
222
+ const modelRef = refkey();
223
+ const parameters: ParameterDescriptor[] = [
224
+ {
225
+ name: "foo",
226
+ nullish: true,
227
+ refkey: fooRef,
228
+ type: <>{modelRef} | null</>,
229
+ },
230
+ ];
231
+ const messageRef = refkey();
232
+ const template = (
233
+ <List hardline>
234
+ <InterfaceDeclaration name="Model" refkey={modelRef}>
235
+ <InterfaceMember name="bar" refkey={refkey()} type="string" />
236
+ </InterfaceDeclaration>
237
+ <FunctionDeclaration name="fooFunction" parameters={parameters}>
238
+ <StatementList>
239
+ <VarDeclaration
240
+ name="message"
241
+ const
242
+ refkey={messageRef}
243
+ initializer={
244
+ <MemberExpression>
245
+ <MemberExpression.Part refkey={fooRef} />
246
+ <MemberExpression.Part id="bar" />
247
+ </MemberExpression>
248
+ }
249
+ />
250
+ <>console.log({messageRef})</>
251
+ </StatementList>
252
+ </FunctionDeclaration>
253
+ </List>
254
+ );
255
+
256
+ expect(toSourceText(template)).toBe(d`
257
+ interface Model {
258
+ bar: string
259
+ }
260
+ function fooFunction(foo: Model | null) {
261
+ const message = foo?.bar;
262
+ console.log(message);
263
+ }
264
+ `);
265
+ });
266
+
267
+ it("handles nullish class member correctly", () => {
268
+ const classRefkey = refkey();
269
+ const interfaceRefkey = refkey();
270
+ const interfaceMemberRefkey = refkey();
271
+ const classMemberRefkey = refkey();
272
+ const instanceRefkey = refkey();
273
+ expect(
274
+ toSourceText(
275
+ <List hardline>
276
+ <InterfaceDeclaration name="Bar" refkey={interfaceRefkey}>
277
+ <InterfaceMember
278
+ name="prop1"
279
+ refkey={interfaceMemberRefkey}
280
+ type={"string"}
281
+ />
282
+ </InterfaceDeclaration>
283
+ <ClassDeclaration name="Foo" refkey={classRefkey}>
284
+ <ClassField
285
+ name="test1"
286
+ refkey={classMemberRefkey}
287
+ nullish
288
+ type={interfaceRefkey}
289
+ />
290
+ </ClassDeclaration>
291
+ <VarDeclaration
292
+ name="inst"
293
+ refkey={instanceRefkey}
294
+ initializer={code`new ${classRefkey}();`}
295
+ />
296
+ <MemberExpression>
297
+ <MemberExpression.Part refkey={instanceRefkey} />
298
+ <MemberExpression.Part refkey={classMemberRefkey} />
299
+ <MemberExpression.Part refkey={interfaceMemberRefkey} />
300
+ </MemberExpression>
301
+ </List>,
302
+ ),
303
+ ).toBe(d`
304
+ interface Bar {
305
+ prop1: string
306
+ }
307
+ class Foo {
308
+ test1: Bar
309
+ }
310
+ const inst = new Foo();
311
+ inst.test1?.prop1
312
+ `);
313
+ });
314
+
146
315
  it("handles late resolved refkeys correctly", () => {
147
316
  const rk1 = refkey();
148
317
  const rk2 = refkey();
@@ -19,7 +19,7 @@ it("renders a variable declaration with documentation", () => {
19
19
  expect(res).toEqual(d`
20
20
  /**
21
21
  * This is a variable documentation
22
- **/
22
+ */
23
23
  const myVar = 123;
24
24
  `);
25
25
  });
@@ -41,7 +41,7 @@ it("renders a let declaration with documentation", () => {
41
41
  expect(res).toEqual(d`
42
42
  /**
43
43
  * A counter variable that can be changed
44
- **/
44
+ */
45
45
  let counter: number = 0;
46
46
  `);
47
47
  });
@@ -64,7 +64,7 @@ it("renders an exported variable declaration with documentation", () => {
64
64
  expect(res).toEqual(d`
65
65
  /**
66
66
  * The base URL for API calls
67
- **/
67
+ */
68
68
  export const API_URL: string = "https://api.example.com";
69
69
  `);
70
70
  });
@@ -43,7 +43,7 @@ it("works end-to-end", () => {
43
43
  type TestType = "hello" | "goodbye";
44
44
  `,
45
45
  "test.ts": `
46
- import { TestType } from "./types.js";
46
+ import type { TestType } from "./types.js";
47
47
 
48
48
  export let hi: TestType = "hello";
49
49
  `,