@alloy-js/typescript 0.11.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 (95) hide show
  1. package/CHANGELOG.md +13 -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 +4 -4
  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 +208 -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 +4 -4
  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 +165 -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
@@ -14,7 +14,7 @@ it("name only", () => {
14
14
  expect(template).toRenderTo(d`
15
15
  /**
16
16
  * @param somebody
17
- **/
17
+ */
18
18
  `);
19
19
  });
20
20
  it("name and type", () => {
@@ -29,7 +29,7 @@ it("name and type", () => {
29
29
  expect(template).toRenderTo(d`
30
30
  /**
31
31
  * @param {string} somebody
32
- **/
32
+ */
33
33
  `);
34
34
  });
35
35
  it("name, type and description", () => {
@@ -45,7 +45,7 @@ it("name, type and description", () => {
45
45
  expect(template).toRenderTo(d`
46
46
  /**
47
47
  * @param {string} somebody Somebody's name.
48
- **/
48
+ */
49
49
  `);
50
50
  });
51
51
  it("name, type and description with hyphen", () => {
@@ -62,7 +62,7 @@ it("name, type and description with hyphen", () => {
62
62
  expect(template).toRenderTo(d`
63
63
  /**
64
64
  * @param {string} somebody - Somebody's name.
65
- **/
65
+ */
66
66
  `);
67
67
  });
68
68
  it("name, type, description, hyphen and optional", () => {
@@ -80,7 +80,7 @@ it("name, type, description, hyphen and optional", () => {
80
80
  expect(template).toRenderTo(d`
81
81
  /**
82
82
  * @param {string} [somebody] - Somebody's name.
83
- **/
83
+ */
84
84
  `);
85
85
  });
86
86
  it("name, type, description, hyphen and optional with default value", () => {
@@ -99,7 +99,7 @@ it("name, type, description, hyphen and optional with default value", () => {
99
99
  expect(template).toRenderTo(d`
100
100
  /**
101
101
  * @param {string} [somebody=John Doe] - Somebody's name.
102
- **/
102
+ */
103
103
  `);
104
104
  });
105
105
  it("name, type, description, hyphen and optional with default value with a very lon description", () => {
@@ -133,7 +133,7 @@ it("name, type, description, hyphen and optional with default value with a very
133
133
  * even a codename (e.g., "Agent X"). It's used primarily for display
134
134
  * purposes, logging, or greeting messages and is not required to be unique or
135
135
  * validated unless specified by the caller.
136
- **/
136
+ */
137
137
  `, {
138
138
  printWidth: 80
139
139
  });
@@ -157,7 +157,7 @@ it("name, type, description, hyphen and optional with default value with a descr
157
157
  /**
158
158
  * @param {string} [somebody=John Doe] - Somebody's name. This is one line
159
159
  * This is another line
160
- **/
160
+ */
161
161
  `, {
162
162
  printWidth: 80
163
163
  });
@@ -1,10 +1,11 @@
1
1
  import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
- import { refkey, StatementList } from "@alloy-js/core";
2
+ import { code, List, refkey, StatementList } from "@alloy-js/core";
3
3
  import { d } from "@alloy-js/core/testing";
4
4
  import { describe, expect, it } from "vitest";
5
5
  import { MemberExpression } from "../src/components/MemberExpression.js";
6
- import { ObjectExpression } from "../src/components/stc/index.js";
6
+ import { InterfaceMember, ObjectExpression } from "../src/components/stc/index.js";
7
7
  import { VarDeclaration } from "../src/components/VarDeclaration.js";
8
+ import { ClassDeclaration, ClassField, FunctionDeclaration, InterfaceDeclaration } from "../src/index.js";
8
9
  import { toSourceText } from "./utils.js";
9
10
  it("renders basic member expression with dot notation", () => {
10
11
  expect(toSourceText(_$createComponent(MemberExpression, {
@@ -165,6 +166,221 @@ describe("with refkeys", () => {
165
166
  test1.test1_2;
166
167
  `);
167
168
  });
169
+ it("handles nullish symbols correctly", () => {
170
+ // Note that this is deliberately producing invalid references (it's just easiest to test with VarDeclaration).
171
+ const rk1 = refkey();
172
+ const rk2 = refkey();
173
+ expect(toSourceText(_$createComponent(StatementList, {
174
+ get children() {
175
+ return [_$createComponent(VarDeclaration, {
176
+ name: "test1",
177
+ refkey: rk1,
178
+ initializer: 1,
179
+ nullish: true
180
+ }), _$createComponent(VarDeclaration, {
181
+ name: "test1",
182
+ refkey: rk2,
183
+ initializer: 2,
184
+ nullish: true
185
+ }), _$createComponent(MemberExpression, {
186
+ get children() {
187
+ return [_$createComponent(MemberExpression.Part, {
188
+ refkey: rk1
189
+ }), _$createComponent(MemberExpression.Part, {
190
+ refkey: rk2
191
+ })];
192
+ }
193
+ })];
194
+ }
195
+ }))).toBe(d`
196
+ const test1 = 1;
197
+ const test1_2 = 2;
198
+ test1?.test1_2;
199
+ `);
200
+ });
201
+ it("handles optional parameters correctly", () => {
202
+ const fooRef = refkey();
203
+ const modelRef = refkey();
204
+ const parameters = [{
205
+ name: "foo",
206
+ optional: true,
207
+ refkey: fooRef,
208
+ type: modelRef
209
+ }];
210
+ const messageRef = refkey();
211
+ const template = _$createComponent(List, {
212
+ hardline: true,
213
+ get children() {
214
+ return [_$createComponent(InterfaceDeclaration, {
215
+ name: "Model",
216
+ refkey: modelRef,
217
+ get children() {
218
+ return _$createComponent(InterfaceMember, {
219
+ name: "bar",
220
+ get refkey() {
221
+ return refkey();
222
+ },
223
+ type: "string"
224
+ });
225
+ }
226
+ }), _$createComponent(FunctionDeclaration, {
227
+ name: "fooFunction",
228
+ parameters: parameters,
229
+ get children() {
230
+ return _$createComponent(StatementList, {
231
+ get children() {
232
+ return [_$createComponent(VarDeclaration, {
233
+ name: "message",
234
+ "const": true,
235
+ refkey: messageRef,
236
+ get initializer() {
237
+ return _$createComponent(MemberExpression, {
238
+ get children() {
239
+ return [_$createComponent(MemberExpression.Part, {
240
+ refkey: fooRef
241
+ }), _$createComponent(MemberExpression.Part, {
242
+ id: "bar"
243
+ })];
244
+ }
245
+ });
246
+ }
247
+ }), ["console.log(", messageRef, ")"]];
248
+ }
249
+ });
250
+ }
251
+ })];
252
+ }
253
+ });
254
+ expect(toSourceText(template)).toBe(d`
255
+ interface Model {
256
+ bar: string
257
+ }
258
+ function fooFunction(foo?: Model) {
259
+ const message = foo?.bar;
260
+ console.log(message);
261
+ }
262
+ `);
263
+ });
264
+ it("handles nullish parameters correctly", () => {
265
+ const fooRef = refkey();
266
+ const modelRef = refkey();
267
+ const parameters = [{
268
+ name: "foo",
269
+ nullish: true,
270
+ refkey: fooRef,
271
+ type: [modelRef, " | null"]
272
+ }];
273
+ const messageRef = refkey();
274
+ const template = _$createComponent(List, {
275
+ hardline: true,
276
+ get children() {
277
+ return [_$createComponent(InterfaceDeclaration, {
278
+ name: "Model",
279
+ refkey: modelRef,
280
+ get children() {
281
+ return _$createComponent(InterfaceMember, {
282
+ name: "bar",
283
+ get refkey() {
284
+ return refkey();
285
+ },
286
+ type: "string"
287
+ });
288
+ }
289
+ }), _$createComponent(FunctionDeclaration, {
290
+ name: "fooFunction",
291
+ parameters: parameters,
292
+ get children() {
293
+ return _$createComponent(StatementList, {
294
+ get children() {
295
+ return [_$createComponent(VarDeclaration, {
296
+ name: "message",
297
+ "const": true,
298
+ refkey: messageRef,
299
+ get initializer() {
300
+ return _$createComponent(MemberExpression, {
301
+ get children() {
302
+ return [_$createComponent(MemberExpression.Part, {
303
+ refkey: fooRef
304
+ }), _$createComponent(MemberExpression.Part, {
305
+ id: "bar"
306
+ })];
307
+ }
308
+ });
309
+ }
310
+ }), ["console.log(", messageRef, ")"]];
311
+ }
312
+ });
313
+ }
314
+ })];
315
+ }
316
+ });
317
+ expect(toSourceText(template)).toBe(d`
318
+ interface Model {
319
+ bar: string
320
+ }
321
+ function fooFunction(foo: Model | null) {
322
+ const message = foo?.bar;
323
+ console.log(message);
324
+ }
325
+ `);
326
+ });
327
+ it("handles nullish class member correctly", () => {
328
+ const classRefkey = refkey();
329
+ const interfaceRefkey = refkey();
330
+ const interfaceMemberRefkey = refkey();
331
+ const classMemberRefkey = refkey();
332
+ const instanceRefkey = refkey();
333
+ expect(toSourceText(_$createComponent(List, {
334
+ hardline: true,
335
+ get children() {
336
+ return [_$createComponent(InterfaceDeclaration, {
337
+ name: "Bar",
338
+ refkey: interfaceRefkey,
339
+ get children() {
340
+ return _$createComponent(InterfaceMember, {
341
+ name: "prop1",
342
+ refkey: interfaceMemberRefkey,
343
+ type: "string"
344
+ });
345
+ }
346
+ }), _$createComponent(ClassDeclaration, {
347
+ name: "Foo",
348
+ refkey: classRefkey,
349
+ get children() {
350
+ return _$createComponent(ClassField, {
351
+ name: "test1",
352
+ refkey: classMemberRefkey,
353
+ nullish: true,
354
+ type: interfaceRefkey
355
+ });
356
+ }
357
+ }), _$createComponent(VarDeclaration, {
358
+ name: "inst",
359
+ refkey: instanceRefkey,
360
+ initializer: code`new ${classRefkey}();`
361
+ }), _$createComponent(MemberExpression, {
362
+ get children() {
363
+ return [_$createComponent(MemberExpression.Part, {
364
+ refkey: instanceRefkey
365
+ }), _$createComponent(MemberExpression.Part, {
366
+ refkey: classMemberRefkey
367
+ }), _$createComponent(MemberExpression.Part, {
368
+ refkey: interfaceMemberRefkey
369
+ })];
370
+ }
371
+ })];
372
+ }
373
+ }))).toBe(d`
374
+ interface Bar {
375
+ prop1: string
376
+ }
377
+ class Foo {
378
+ test1: Bar
379
+ }
380
+ const inst = new Foo();
381
+ inst.test1?.prop1
382
+ `);
383
+ });
168
384
  it("handles late resolved refkeys correctly", () => {
169
385
  const rk1 = refkey();
170
386
  const rk2 = refkey();
@@ -13,7 +13,7 @@ it("renders a variable declaration with documentation", () => {
13
13
  expect(res).toEqual(d`
14
14
  /**
15
15
  * This is a variable documentation
16
- **/
16
+ */
17
17
  const myVar = 123;
18
18
  `);
19
19
  });
@@ -28,7 +28,7 @@ it("renders a let declaration with documentation", () => {
28
28
  expect(res).toEqual(d`
29
29
  /**
30
30
  * A counter variable that can be changed
31
- **/
31
+ */
32
32
  let counter: number = 0;
33
33
  `);
34
34
  });
@@ -44,7 +44,7 @@ it("renders an exported variable declaration with documentation", () => {
44
44
  expect(res).toEqual(d`
45
45
  /**
46
46
  * The base URL for API calls
47
- **/
47
+ */
48
48
  export const API_URL: string = "https://api.example.com";
49
49
  `);
50
50
  });
@@ -55,7 +55,7 @@ it("works end-to-end", () => {
55
55
  type TestType = "hello" | "goodbye";
56
56
  `,
57
57
  "test.ts": `
58
- import { TestType } from "./types.js";
58
+ import type { TestType } from "./types.js";
59
59
 
60
60
  export let hi: TestType = "hello";
61
61
  `