@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,10 @@
1
1
  import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
- import { Output, SourceDirectory, StatementList, refkey, render } from "@alloy-js/core";
2
+ import { List, Output, SourceDirectory, StatementList, refkey, render } from "@alloy-js/core";
3
3
  import "@alloy-js/core/testing";
4
- import { it } from "vitest";
4
+ import { describe, it } from "vitest";
5
5
  import * as ts from "../src/components/index.js";
6
6
  import { Reference } from "../src/components/Reference.js";
7
+ import { TypeRefContext } from "../src/context/type-ref-context.js";
7
8
  import { tsNameConflictResolver } from "../src/name-conflict-resolver.js";
8
9
  import { assertFileContents } from "./utils.js";
9
10
  it("works with default imports", () => {
@@ -224,6 +225,91 @@ it("works with default and named imports and name conflicts", () => {
224
225
  `
225
226
  });
226
227
  });
228
+ it("works with importing the same name many times from different files with the default name conflict resolver", () => {
229
+ const rk1 = refkey();
230
+ const rk1i = refkey();
231
+ const rk2 = refkey();
232
+ const rk2i = refkey();
233
+ const rk3 = refkey();
234
+ const rk3i = refkey();
235
+ const res = render(_$createComponent(Output, {
236
+ get children() {
237
+ return [_$createComponent(ts.SourceFile, {
238
+ path: "test1.ts",
239
+ get children() {
240
+ return [_$createComponent(ts.VarDeclaration, {
241
+ "export": true,
242
+ name: "conflict",
243
+ refkey: rk1,
244
+ children: "\"hi\""
245
+ }), _$createComponent(ts.InterfaceDeclaration, {
246
+ "export": true,
247
+ name: "MyInterface",
248
+ refkey: rk1i
249
+ })];
250
+ }
251
+ }), _$createComponent(ts.SourceFile, {
252
+ path: "test2.ts",
253
+ get children() {
254
+ return [_$createComponent(ts.VarDeclaration, {
255
+ name: "conflict",
256
+ refkey: rk2,
257
+ children: "\"hi\""
258
+ }), _$createComponent(ts.InterfaceDeclaration, {
259
+ "export": true,
260
+ name: "MyInterface",
261
+ refkey: rk2i
262
+ })];
263
+ }
264
+ }), _$createComponent(ts.SourceFile, {
265
+ path: "test3.ts",
266
+ get children() {
267
+ return [_$createComponent(ts.VarDeclaration, {
268
+ name: "conflict",
269
+ refkey: rk3,
270
+ children: "\"hi\""
271
+ }), _$createComponent(ts.InterfaceDeclaration, {
272
+ "export": true,
273
+ name: "MyInterface",
274
+ refkey: rk3i
275
+ })];
276
+ }
277
+ }), _$createComponent(ts.SourceFile, {
278
+ path: "test-import.ts",
279
+ get children() {
280
+ return _$createComponent(StatementList, {
281
+ get children() {
282
+ return [_$createComponent(ts.VarDeclaration, {
283
+ name: "one",
284
+ type: rk1i,
285
+ initializer: rk1
286
+ }), _$createComponent(ts.VarDeclaration, {
287
+ name: "two",
288
+ type: rk2i,
289
+ initializer: rk2
290
+ }), _$createComponent(ts.VarDeclaration, {
291
+ name: "three",
292
+ type: rk3i,
293
+ initializer: rk3
294
+ })];
295
+ }
296
+ });
297
+ }
298
+ })];
299
+ }
300
+ }));
301
+ assertFileContents(res, {
302
+ "test-import.ts": `
303
+ import { type MyInterface, conflict } from "./test1.js";
304
+ import { type MyInterface as MyInterface_2, conflict as conflict_2 } from "./test2.js";
305
+ import { type MyInterface as MyInterface_3, conflict as conflict_3 } from "./test3.js";
306
+
307
+ const one: MyInterface = conflict;
308
+ const two: MyInterface_2 = conflict_2;
309
+ const three: MyInterface_3 = conflict_3;
310
+ `
311
+ });
312
+ });
227
313
  it("works with default and named imports and name conflicts and references in nested scopes", () => {
228
314
  const res = render(_$createComponent(Output, {
229
315
  nameConflictResolver: tsNameConflictResolver,
@@ -431,4 +517,209 @@ it("handles conflicts with local declarations", () => {
431
517
  export function test() {}
432
518
  `
433
519
  });
520
+ });
521
+ describe("type imports", () => {
522
+ function mkTestFile(name) {
523
+ const TypeA = refkey("TypeA");
524
+ const TypeB = refkey("TypeB");
525
+ const ClassA = refkey("ClassA");
526
+ return {
527
+ component: _$createComponent(ts.SourceFile, {
528
+ path: "test1.ts",
529
+ get children() {
530
+ return [_$createComponent(ts.InterfaceDeclaration, {
531
+ "export": true,
532
+ name: "TypeA",
533
+ refkey: TypeA
534
+ }), _$createComponent(ts.InterfaceDeclaration, {
535
+ "export": true,
536
+ name: "TypeB",
537
+ refkey: TypeB
538
+ }), _$createComponent(ts.ClassDeclaration, {
539
+ "export": true,
540
+ name: "ClassA",
541
+ refkey: ClassA
542
+ })];
543
+ }
544
+ }),
545
+ TypeA,
546
+ TypeB,
547
+ ClassA
548
+ };
549
+ }
550
+ it("adds type keyword only to type imports", () => {
551
+ const {
552
+ component,
553
+ TypeA,
554
+ ClassA
555
+ } = mkTestFile("test1.ts");
556
+ const res = render(_$createComponent(Output, {
557
+ get children() {
558
+ return [component, _$createComponent(ts.SourceFile, {
559
+ path: "test2.ts",
560
+ get children() {
561
+ return _$createComponent(List, {
562
+ get children() {
563
+ return [["type A = ", _$createComponent(Reference, {
564
+ refkey: TypeA,
565
+ type: true
566
+ })], ["class B extends ", _$createComponent(Reference, {
567
+ refkey: ClassA
568
+ }), " ", "{}"]];
569
+ }
570
+ });
571
+ }
572
+ })];
573
+ }
574
+ }));
575
+ assertFileContents(res, {
576
+ "test2.ts": `
577
+ import { type TypeA, ClassA } from "./test1.js";
578
+
579
+ type A = TypeA
580
+ class B extends ClassA {}
581
+ `
582
+ });
583
+ });
584
+ it("add type keyword for whole import if all are types", () => {
585
+ const {
586
+ component,
587
+ TypeA,
588
+ TypeB
589
+ } = mkTestFile("test1.ts");
590
+ const res = render(_$createComponent(Output, {
591
+ get children() {
592
+ return [component, _$createComponent(ts.SourceFile, {
593
+ path: "test2.ts",
594
+ get children() {
595
+ return _$createComponent(List, {
596
+ get children() {
597
+ return [["type A = ", _$createComponent(Reference, {
598
+ refkey: TypeA,
599
+ type: true
600
+ })], ["type B = ", _$createComponent(Reference, {
601
+ refkey: TypeB,
602
+ type: true
603
+ })]];
604
+ }
605
+ });
606
+ }
607
+ })];
608
+ }
609
+ }));
610
+ assertFileContents(res, {
611
+ "test2.ts": `
612
+ import type { TypeA, TypeB } from "./test1.js";
613
+
614
+ type A = TypeA
615
+ type B = TypeB
616
+ `
617
+ });
618
+ });
619
+ it("reference same type multiple times", () => {
620
+ const {
621
+ component,
622
+ TypeA
623
+ } = mkTestFile("test1.ts");
624
+ const res = render(_$createComponent(Output, {
625
+ get children() {
626
+ return [component, _$createComponent(ts.SourceFile, {
627
+ path: "test2.ts",
628
+ get children() {
629
+ return _$createComponent(List, {
630
+ get children() {
631
+ return [["type A = ", _$createComponent(Reference, {
632
+ refkey: TypeA,
633
+ type: true
634
+ })], ["type B = ", _$createComponent(Reference, {
635
+ refkey: TypeA,
636
+ type: true
637
+ })]];
638
+ }
639
+ });
640
+ }
641
+ })];
642
+ }
643
+ }));
644
+ assertFileContents(res, {
645
+ "test2.ts": `
646
+ import type { TypeA } from "./test1.js";
647
+
648
+ type A = TypeA
649
+ type B = TypeA
650
+ `
651
+ });
652
+ });
653
+ it("same reference used as both type and non type doesn't include type", () => {
654
+ const {
655
+ component,
656
+ ClassA
657
+ } = mkTestFile("test1.ts");
658
+ const res = render(_$createComponent(Output, {
659
+ get children() {
660
+ return [component, _$createComponent(ts.SourceFile, {
661
+ path: "test2.ts",
662
+ get children() {
663
+ return _$createComponent(List, {
664
+ get children() {
665
+ return [["type A = ", _$createComponent(Reference, {
666
+ refkey: ClassA,
667
+ type: true
668
+ })], ["class B extends ", _$createComponent(Reference, {
669
+ refkey: ClassA
670
+ }), " ", "{}"]];
671
+ }
672
+ });
673
+ }
674
+ })];
675
+ }
676
+ }));
677
+ assertFileContents(res, {
678
+ "test2.ts": `
679
+ import { ClassA } from "./test1.js";
680
+
681
+ type A = ClassA
682
+ class B extends ClassA {}
683
+ `
684
+ });
685
+ });
686
+ it("infer if a type reference from the typescript context", () => {
687
+ const {
688
+ component,
689
+ TypeA
690
+ } = mkTestFile("test1.ts");
691
+ const res = render(_$createComponent(Output, {
692
+ get children() {
693
+ return [component, _$createComponent(ts.SourceFile, {
694
+ path: "test2.ts",
695
+ get children() {
696
+ return _$createComponent(TypeRefContext.Provider, {
697
+ value: {
698
+ type: true
699
+ },
700
+ get children() {
701
+ return _$createComponent(List, {
702
+ get children() {
703
+ return [["type A = ", _$createComponent(Reference, {
704
+ refkey: TypeA
705
+ })], ["type B = ", _$createComponent(Reference, {
706
+ refkey: TypeA
707
+ })]];
708
+ }
709
+ });
710
+ }
711
+ });
712
+ }
713
+ })];
714
+ }
715
+ }));
716
+ assertFileContents(res, {
717
+ "test2.ts": `
718
+ import type { TypeA } from "./test1.js";
719
+
720
+ type A = TypeA
721
+ type B = TypeA
722
+ `
723
+ });
724
+ });
434
725
  });
@@ -25,7 +25,7 @@ it("renders an interface declaration with documentation", () => {
25
25
  expect(res).toEqual(d`
26
26
  /**
27
27
  * Interface representing a person
28
- **/
28
+ */
29
29
  interface Person {
30
30
  name: string;
31
31
  age: number;
@@ -53,7 +53,7 @@ it("renders an interface declaration with docs from a string array with single e
53
53
  expect(res).toEqual(d`
54
54
  /**
55
55
  * Interface representing a person
56
- **/
56
+ */
57
57
  interface Person {
58
58
  name: string;
59
59
  age: number;
@@ -83,7 +83,7 @@ it("renders an interface declaration with docs from a string array", () => {
83
83
  * Interface representing a person
84
84
  *
85
85
  * This should be another paragraph
86
- **/
86
+ */
87
87
  interface Person {
88
88
  name: string;
89
89
  age: number;
@@ -118,15 +118,15 @@ it("renders an interface declaration with documented properties", () => {
118
118
  expect(res).toEqual(d`
119
119
  /**
120
120
  * Standard API response format
121
- **/
121
+ */
122
122
  export interface APIResponse {
123
123
  /**
124
124
  * Wether or not the request completed successfully
125
- **/
125
+ */
126
126
  success: boolean;
127
127
  /**
128
128
  * the response payload
129
- **/
129
+ */
130
130
  data: unknown;
131
131
  error?: string;
132
132
  }
@@ -160,17 +160,17 @@ it("renders an interface declaration with array documented properties ", () => {
160
160
  expect(res).toEqual(d`
161
161
  /**
162
162
  * Standard API response format
163
- **/
163
+ */
164
164
  export interface APIResponse {
165
165
  /**
166
166
  * Wether or not the request completed successfully
167
167
  *
168
168
  * This is another paragraph
169
- **/
169
+ */
170
170
  success: boolean;
171
171
  /**
172
172
  * the response payload
173
- **/
173
+ */
174
174
  data: unknown;
175
175
  error?: string;
176
176
  }
@@ -19,7 +19,7 @@ describe("JSDoc", () => {
19
19
  * Hello!
20
20
  *
21
21
  * This is another line
22
- **/
22
+ */
23
23
  `, {
24
24
  printWidth: 40
25
25
  });
@@ -47,7 +47,7 @@ describe("Prose", () => {
47
47
  * This is another paragraph. There
48
48
  * should be a couple breaks in front of
49
49
  * it.
50
- **/
50
+ */
51
51
  `, {
52
52
  printWidth: 40
53
53
  });
@@ -71,7 +71,7 @@ describe("JSDocExample", () => {
71
71
  *
72
72
  * @example
73
73
  * console.log("Hello world!")
74
- **/
74
+ */
75
75
  `, {
76
76
  printWidth: 40
77
77
  });
@@ -97,7 +97,7 @@ describe("JSDocExample", () => {
97
97
  * \`\`\`ts
98
98
  * console.log("Hello world!")
99
99
  * \`\`\`
100
- **/
100
+ */
101
101
  `, {
102
102
  printWidth: 40
103
103
  });
@@ -126,7 +126,7 @@ describe("JSDocExample", () => {
126
126
  * @example
127
127
  * // make a function call.
128
128
  * console.log(arg1, arg2);
129
- **/
129
+ */
130
130
  `, {
131
131
  printWidth: 40
132
132
  });
@@ -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
  });