@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.
- package/CHANGELOG.md +13 -0
- package/dist/src/components/ClassDeclaration.d.ts +2 -0
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.js +14 -2
- package/dist/src/components/FunctionBase.d.ts.map +1 -1
- package/dist/src/components/FunctionBase.js +13 -3
- package/dist/src/components/FunctionType.d.ts +7 -8
- package/dist/src/components/FunctionType.d.ts.map +1 -1
- package/dist/src/components/FunctionType.js +29 -25
- package/dist/src/components/ImportStatement.d.ts.map +1 -1
- package/dist/src/components/ImportStatement.js +9 -2
- package/dist/src/components/Interface.d.ts +3 -2
- package/dist/src/components/Interface.d.ts.map +1 -1
- package/dist/src/components/Interface.js +8 -4
- package/dist/src/components/JSDocComment.js +1 -1
- package/dist/src/components/MemberDeclaration.d.ts +4 -0
- package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
- package/dist/src/components/MemberDeclaration.js +1 -1
- package/dist/src/components/MemberExpression.d.ts.map +1 -1
- package/dist/src/components/MemberExpression.js +3 -2
- package/dist/src/components/Reference.d.ts +8 -1
- package/dist/src/components/Reference.d.ts.map +1 -1
- package/dist/src/components/Reference.js +7 -2
- package/dist/src/components/TypeDeclaration.d.ts +1 -1
- package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
- package/dist/src/components/TypeDeclaration.js +3 -2
- package/dist/src/components/TypeRefContext.d.ts +22 -0
- package/dist/src/components/TypeRefContext.d.ts.map +1 -0
- package/dist/src/components/TypeRefContext.js +41 -0
- package/dist/src/components/VarDeclaration.d.ts +1 -0
- package/dist/src/components/VarDeclaration.d.ts.map +1 -1
- package/dist/src/components/VarDeclaration.js +9 -3
- package/dist/src/components/index.d.ts +1 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +1 -0
- package/dist/src/components/stc/index.d.ts +1 -0
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +1 -0
- package/dist/src/context/type-ref-context.d.ts +15 -0
- package/dist/src/context/type-ref-context.d.ts.map +1 -0
- package/dist/src/context/type-ref-context.js +12 -0
- package/dist/src/parameter-descriptor.d.ts +6 -0
- package/dist/src/parameter-descriptor.d.ts.map +1 -1
- package/dist/src/symbols/reference.d.ts +4 -1
- package/dist/src/symbols/reference.d.ts.map +1 -1
- package/dist/src/symbols/reference.js +11 -5
- package/dist/src/symbols/ts-module-scope.d.ts +4 -1
- package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
- package/dist/src/symbols/ts-module-scope.js +10 -3
- package/dist/src/symbols/ts-output-symbol.d.ts +2 -1
- package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
- package/dist/src/symbols/ts-output-symbol.js +1 -0
- package/dist/test/class.test.js +4 -4
- package/dist/test/enum-jsdoc.test.js +6 -6
- package/dist/test/function-declaration-jsdoc.test.js +3 -3
- package/dist/test/imports.test.d.ts.map +1 -1
- package/dist/test/imports.test.js +208 -2
- package/dist/test/interface-jsdoc.test.js +9 -9
- package/dist/test/jsdoc.test.js +5 -5
- package/dist/test/jsdocparam.test.js +8 -8
- package/dist/test/member-expression.test.js +218 -2
- package/dist/test/var-declaration-jsdoc.test.js +3 -3
- package/dist/test/var-declaration.test.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/src/components/ClassDeclaration.tsx +19 -3
- package/src/components/FunctionBase.tsx +14 -3
- package/src/components/FunctionType.tsx +45 -40
- package/src/components/ImportStatement.tsx +30 -3
- package/src/components/Interface.tsx +41 -34
- package/src/components/JSDocComment.tsx +1 -1
- package/src/components/MemberDeclaration.tsx +7 -1
- package/src/components/MemberExpression.tsx +7 -2
- package/src/components/Reference.tsx +12 -2
- package/src/components/TypeDeclaration.tsx +16 -13
- package/src/components/TypeRefContext.tsx +49 -0
- package/src/components/VarDeclaration.tsx +6 -2
- package/src/components/index.ts +1 -0
- package/src/components/stc/index.ts +1 -0
- package/src/context/type-ref-context.tsx +19 -0
- package/src/parameter-descriptor.ts +7 -0
- package/src/symbols/reference.ts +17 -5
- package/src/symbols/ts-module-scope.ts +19 -4
- package/src/symbols/ts-output-symbol.ts +1 -0
- package/temp/api.json +613 -220
- package/test/class.test.tsx +4 -4
- package/test/enum-jsdoc.test.tsx +6 -6
- package/test/function-declaration-jsdoc.test.tsx +3 -3
- package/test/imports.test.tsx +165 -1
- package/test/interface-jsdoc.test.tsx +9 -9
- package/test/jsdoc.test.tsx +5 -5
- package/test/jsdocparam.test.tsx +8 -8
- package/test/member-expression.test.tsx +171 -2
- package/test/var-declaration-jsdoc.test.tsx +3 -3
- package/test/var-declaration.test.tsx +1 -1
package/test/class.test.tsx
CHANGED
|
@@ -309,15 +309,15 @@ it("renders a class with docs for the class and its members", () => {
|
|
|
309
309
|
expect(res).toEqual(d`
|
|
310
310
|
/**
|
|
311
311
|
* This is a class documentation
|
|
312
|
-
|
|
312
|
+
*/
|
|
313
313
|
class Foo {
|
|
314
314
|
/**
|
|
315
315
|
* This is a field documentation
|
|
316
|
-
|
|
316
|
+
*/
|
|
317
317
|
bar = 123;
|
|
318
318
|
/**
|
|
319
319
|
* This is a method documentation
|
|
320
|
-
|
|
320
|
+
*/
|
|
321
321
|
baz() {
|
|
322
322
|
return 123;
|
|
323
323
|
}
|
|
@@ -353,7 +353,7 @@ it("renders a method with parameter docs", () => {
|
|
|
353
353
|
* @param {string} b - Line 1 for b.
|
|
354
354
|
* This is a long description that
|
|
355
355
|
* should continue in the next line.
|
|
356
|
-
|
|
356
|
+
*/
|
|
357
357
|
bar(a: number, b: string): void {}
|
|
358
358
|
}
|
|
359
359
|
`);
|
package/test/enum-jsdoc.test.tsx
CHANGED
|
@@ -19,7 +19,7 @@ it("renders an enum declaration with documentation", () => {
|
|
|
19
19
|
expect(res).toEqual(d`
|
|
20
20
|
/**
|
|
21
21
|
* Color enumeration for the application
|
|
22
|
-
|
|
22
|
+
*/
|
|
23
23
|
enum Color {
|
|
24
24
|
Red,
|
|
25
25
|
Green,
|
|
@@ -49,25 +49,25 @@ it("renders an enum declaration with documented members", () => {
|
|
|
49
49
|
expect(res).toEqual(d`
|
|
50
50
|
/**
|
|
51
51
|
* Represents cardinal directions. Used for navigation purposes
|
|
52
|
-
|
|
52
|
+
*/
|
|
53
53
|
enum Direction {
|
|
54
54
|
/**
|
|
55
55
|
* Bound to the north
|
|
56
|
-
|
|
56
|
+
*/
|
|
57
57
|
North = 0,
|
|
58
58
|
/**
|
|
59
59
|
* Bound to the East
|
|
60
60
|
*
|
|
61
61
|
* A second doc paragraph
|
|
62
|
-
|
|
62
|
+
*/
|
|
63
63
|
East = 1,
|
|
64
64
|
/**
|
|
65
65
|
* Opposite to North
|
|
66
|
-
|
|
66
|
+
*/
|
|
67
67
|
South = 2,
|
|
68
68
|
/**
|
|
69
69
|
* Going west!
|
|
70
|
-
|
|
70
|
+
*/
|
|
71
71
|
West = 3,
|
|
72
72
|
}
|
|
73
73
|
`);
|
|
@@ -18,7 +18,7 @@ it("renders a function declaration with documentation", () => {
|
|
|
18
18
|
* A function that greets a person
|
|
19
19
|
*
|
|
20
20
|
* @param {string} name
|
|
21
|
-
|
|
21
|
+
*/
|
|
22
22
|
function greet(name: string): string {}
|
|
23
23
|
`);
|
|
24
24
|
});
|
|
@@ -42,7 +42,7 @@ it("With documented parameters", () => {
|
|
|
42
42
|
*
|
|
43
43
|
* @param {number} price
|
|
44
44
|
* @param {number} [taxRate]
|
|
45
|
-
|
|
45
|
+
*/
|
|
46
46
|
export function calculateTotal(price: number, taxRate?: number): number {}
|
|
47
47
|
`);
|
|
48
48
|
});
|
|
@@ -84,6 +84,6 @@ it("renders a function with a reference", () => {
|
|
|
84
84
|
* Creates a new user
|
|
85
85
|
*
|
|
86
86
|
* @param {User} user - The user object to create
|
|
87
|
-
|
|
87
|
+
*/
|
|
88
88
|
function createUser(user: User): void {}`);
|
|
89
89
|
});
|
package/test/imports.test.tsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
List,
|
|
2
3
|
Output,
|
|
3
4
|
SourceDirectory,
|
|
4
5
|
StatementList,
|
|
@@ -6,9 +7,10 @@ import {
|
|
|
6
7
|
render,
|
|
7
8
|
} from "@alloy-js/core";
|
|
8
9
|
import "@alloy-js/core/testing";
|
|
9
|
-
import { it } from "vitest";
|
|
10
|
+
import { describe, it } from "vitest";
|
|
10
11
|
import * as ts from "../src/components/index.js";
|
|
11
12
|
import { Reference } from "../src/components/Reference.js";
|
|
13
|
+
import { TypeRefContext } from "../src/context/type-ref-context.js";
|
|
12
14
|
import { tsNameConflictResolver } from "../src/name-conflict-resolver.js";
|
|
13
15
|
import { assertFileContents } from "./utils.js";
|
|
14
16
|
|
|
@@ -308,3 +310,165 @@ it("handles conflicts with local declarations", () => {
|
|
|
308
310
|
`,
|
|
309
311
|
});
|
|
310
312
|
});
|
|
313
|
+
|
|
314
|
+
describe("type imports", () => {
|
|
315
|
+
function mkTestFile(name: string) {
|
|
316
|
+
const TypeA = refkey("TypeA");
|
|
317
|
+
const TypeB = refkey("TypeB");
|
|
318
|
+
const ClassA = refkey("ClassA");
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
component: (
|
|
322
|
+
<ts.SourceFile path="test1.ts">
|
|
323
|
+
<ts.InterfaceDeclaration export name="TypeA" refkey={TypeA} />
|
|
324
|
+
<ts.InterfaceDeclaration export name="TypeB" refkey={TypeB} />
|
|
325
|
+
<ts.ClassDeclaration export name="ClassA" refkey={ClassA} />
|
|
326
|
+
</ts.SourceFile>
|
|
327
|
+
),
|
|
328
|
+
TypeA,
|
|
329
|
+
TypeB,
|
|
330
|
+
ClassA,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
it("adds type keyword only to type imports", () => {
|
|
335
|
+
const { component, TypeA, ClassA } = mkTestFile("test1.ts");
|
|
336
|
+
const res = render(
|
|
337
|
+
<Output>
|
|
338
|
+
{component}
|
|
339
|
+
<ts.SourceFile path="test2.ts">
|
|
340
|
+
<List>
|
|
341
|
+
<>
|
|
342
|
+
type A = <Reference refkey={TypeA} type />
|
|
343
|
+
</>
|
|
344
|
+
<>
|
|
345
|
+
class B extends <Reference refkey={ClassA} /> {"{}"}
|
|
346
|
+
</>
|
|
347
|
+
</List>
|
|
348
|
+
</ts.SourceFile>
|
|
349
|
+
</Output>,
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
assertFileContents(res, {
|
|
353
|
+
"test2.ts": `
|
|
354
|
+
import { type TypeA, ClassA } from "./test1.js";
|
|
355
|
+
|
|
356
|
+
type A = TypeA
|
|
357
|
+
class B extends ClassA {}
|
|
358
|
+
`,
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it("add type keyword for whole import if all are types", () => {
|
|
363
|
+
const { component, TypeA, TypeB } = mkTestFile("test1.ts");
|
|
364
|
+
const res = render(
|
|
365
|
+
<Output>
|
|
366
|
+
{component}
|
|
367
|
+
<ts.SourceFile path="test2.ts">
|
|
368
|
+
<List>
|
|
369
|
+
<>
|
|
370
|
+
type A = <Reference refkey={TypeA} type />
|
|
371
|
+
</>
|
|
372
|
+
<>
|
|
373
|
+
type B = <Reference refkey={TypeB} type />
|
|
374
|
+
</>
|
|
375
|
+
</List>
|
|
376
|
+
</ts.SourceFile>
|
|
377
|
+
</Output>,
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
assertFileContents(res, {
|
|
381
|
+
"test2.ts": `
|
|
382
|
+
import type { TypeA, TypeB } from "./test1.js";
|
|
383
|
+
|
|
384
|
+
type A = TypeA
|
|
385
|
+
type B = TypeB
|
|
386
|
+
`,
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
it("reference same type multiple times", () => {
|
|
390
|
+
const { component, TypeA } = mkTestFile("test1.ts");
|
|
391
|
+
const res = render(
|
|
392
|
+
<Output>
|
|
393
|
+
{component}
|
|
394
|
+
<ts.SourceFile path="test2.ts">
|
|
395
|
+
<List>
|
|
396
|
+
<>
|
|
397
|
+
type A = <Reference refkey={TypeA} type />
|
|
398
|
+
</>
|
|
399
|
+
<>
|
|
400
|
+
type B = <Reference refkey={TypeA} type />
|
|
401
|
+
</>
|
|
402
|
+
</List>
|
|
403
|
+
</ts.SourceFile>
|
|
404
|
+
</Output>,
|
|
405
|
+
);
|
|
406
|
+
|
|
407
|
+
assertFileContents(res, {
|
|
408
|
+
"test2.ts": `
|
|
409
|
+
import type { TypeA } from "./test1.js";
|
|
410
|
+
|
|
411
|
+
type A = TypeA
|
|
412
|
+
type B = TypeA
|
|
413
|
+
`,
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it("same reference used as both type and non type doesn't include type", () => {
|
|
418
|
+
const { component, ClassA } = mkTestFile("test1.ts");
|
|
419
|
+
const res = render(
|
|
420
|
+
<Output>
|
|
421
|
+
{component}
|
|
422
|
+
<ts.SourceFile path="test2.ts">
|
|
423
|
+
<List>
|
|
424
|
+
<>
|
|
425
|
+
type A = <Reference refkey={ClassA} type />
|
|
426
|
+
</>
|
|
427
|
+
<>
|
|
428
|
+
class B extends <Reference refkey={ClassA} /> {"{}"}
|
|
429
|
+
</>
|
|
430
|
+
</List>
|
|
431
|
+
</ts.SourceFile>
|
|
432
|
+
</Output>,
|
|
433
|
+
);
|
|
434
|
+
|
|
435
|
+
assertFileContents(res, {
|
|
436
|
+
"test2.ts": `
|
|
437
|
+
import { ClassA } from "./test1.js";
|
|
438
|
+
|
|
439
|
+
type A = ClassA
|
|
440
|
+
class B extends ClassA {}
|
|
441
|
+
`,
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("infer if a type reference from the typescript context", () => {
|
|
446
|
+
const { component, TypeA } = mkTestFile("test1.ts");
|
|
447
|
+
const res = render(
|
|
448
|
+
<Output>
|
|
449
|
+
{component}
|
|
450
|
+
<ts.SourceFile path="test2.ts">
|
|
451
|
+
<TypeRefContext.Provider value={{ type: true }}>
|
|
452
|
+
<List>
|
|
453
|
+
<>
|
|
454
|
+
type A = <Reference refkey={TypeA} />
|
|
455
|
+
</>
|
|
456
|
+
<>
|
|
457
|
+
type B = <Reference refkey={TypeA} />
|
|
458
|
+
</>
|
|
459
|
+
</List>
|
|
460
|
+
</TypeRefContext.Provider>
|
|
461
|
+
</ts.SourceFile>
|
|
462
|
+
</Output>,
|
|
463
|
+
);
|
|
464
|
+
|
|
465
|
+
assertFileContents(res, {
|
|
466
|
+
"test2.ts": `
|
|
467
|
+
import type { TypeA } from "./test1.js";
|
|
468
|
+
|
|
469
|
+
type A = TypeA
|
|
470
|
+
type B = TypeA
|
|
471
|
+
`,
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
});
|
|
@@ -19,7 +19,7 @@ it("renders an interface declaration with documentation", () => {
|
|
|
19
19
|
expect(res).toEqual(d`
|
|
20
20
|
/**
|
|
21
21
|
* Interface representing a person
|
|
22
|
-
|
|
22
|
+
*/
|
|
23
23
|
interface Person {
|
|
24
24
|
name: string;
|
|
25
25
|
age: number;
|
|
@@ -42,7 +42,7 @@ it("renders an interface declaration with docs from a string array with single e
|
|
|
42
42
|
expect(res).toEqual(d`
|
|
43
43
|
/**
|
|
44
44
|
* Interface representing a person
|
|
45
|
-
|
|
45
|
+
*/
|
|
46
46
|
interface Person {
|
|
47
47
|
name: string;
|
|
48
48
|
age: number;
|
|
@@ -70,7 +70,7 @@ it("renders an interface declaration with docs from a string array", () => {
|
|
|
70
70
|
* Interface representing a person
|
|
71
71
|
*
|
|
72
72
|
* This should be another paragraph
|
|
73
|
-
|
|
73
|
+
*/
|
|
74
74
|
interface Person {
|
|
75
75
|
name: string;
|
|
76
76
|
age: number;
|
|
@@ -103,15 +103,15 @@ it("renders an interface declaration with documented properties", () => {
|
|
|
103
103
|
expect(res).toEqual(d`
|
|
104
104
|
/**
|
|
105
105
|
* Standard API response format
|
|
106
|
-
|
|
106
|
+
*/
|
|
107
107
|
export interface APIResponse {
|
|
108
108
|
/**
|
|
109
109
|
* Wether or not the request completed successfully
|
|
110
|
-
|
|
110
|
+
*/
|
|
111
111
|
success: boolean;
|
|
112
112
|
/**
|
|
113
113
|
* the response payload
|
|
114
|
-
|
|
114
|
+
*/
|
|
115
115
|
data: unknown;
|
|
116
116
|
error?: string;
|
|
117
117
|
}
|
|
@@ -146,17 +146,17 @@ it("renders an interface declaration with array documented properties ", () => {
|
|
|
146
146
|
expect(res).toEqual(d`
|
|
147
147
|
/**
|
|
148
148
|
* Standard API response format
|
|
149
|
-
|
|
149
|
+
*/
|
|
150
150
|
export interface APIResponse {
|
|
151
151
|
/**
|
|
152
152
|
* Wether or not the request completed successfully
|
|
153
153
|
*
|
|
154
154
|
* This is another paragraph
|
|
155
|
-
|
|
155
|
+
*/
|
|
156
156
|
success: boolean;
|
|
157
157
|
/**
|
|
158
158
|
* the response payload
|
|
159
|
-
|
|
159
|
+
*/
|
|
160
160
|
data: unknown;
|
|
161
161
|
error?: string;
|
|
162
162
|
}
|
package/test/jsdoc.test.tsx
CHANGED
|
@@ -24,7 +24,7 @@ describe("JSDoc", () => {
|
|
|
24
24
|
* Hello!
|
|
25
25
|
*
|
|
26
26
|
* This is another line
|
|
27
|
-
|
|
27
|
+
*/
|
|
28
28
|
`,
|
|
29
29
|
{ printWidth: 40 },
|
|
30
30
|
);
|
|
@@ -59,7 +59,7 @@ describe("Prose", () => {
|
|
|
59
59
|
* This is another paragraph. There
|
|
60
60
|
* should be a couple breaks in front of
|
|
61
61
|
* it.
|
|
62
|
-
|
|
62
|
+
*/
|
|
63
63
|
`,
|
|
64
64
|
{ printWidth: 40 },
|
|
65
65
|
);
|
|
@@ -83,7 +83,7 @@ describe("JSDocExample", () => {
|
|
|
83
83
|
*
|
|
84
84
|
* @example
|
|
85
85
|
* console.log("Hello world!")
|
|
86
|
-
|
|
86
|
+
*/
|
|
87
87
|
`,
|
|
88
88
|
{ printWidth: 40 },
|
|
89
89
|
);
|
|
@@ -109,7 +109,7 @@ describe("JSDocExample", () => {
|
|
|
109
109
|
* \`\`\`ts
|
|
110
110
|
* console.log("Hello world!")
|
|
111
111
|
* \`\`\`
|
|
112
|
-
|
|
112
|
+
*/
|
|
113
113
|
`,
|
|
114
114
|
{ printWidth: 40 },
|
|
115
115
|
);
|
|
@@ -140,7 +140,7 @@ describe("JSDocExample", () => {
|
|
|
140
140
|
* @example
|
|
141
141
|
* // make a function call.
|
|
142
142
|
* console.log(arg1, arg2);
|
|
143
|
-
|
|
143
|
+
*/
|
|
144
144
|
`,
|
|
145
145
|
{ printWidth: 40 },
|
|
146
146
|
);
|
package/test/jsdocparam.test.tsx
CHANGED
|
@@ -14,7 +14,7 @@ it("name only", () => {
|
|
|
14
14
|
d`
|
|
15
15
|
/**
|
|
16
16
|
* @param somebody
|
|
17
|
-
|
|
17
|
+
*/
|
|
18
18
|
`,
|
|
19
19
|
);
|
|
20
20
|
});
|
|
@@ -30,7 +30,7 @@ it("name and type", () => {
|
|
|
30
30
|
d`
|
|
31
31
|
/**
|
|
32
32
|
* @param {string} somebody
|
|
33
|
-
|
|
33
|
+
*/
|
|
34
34
|
`,
|
|
35
35
|
);
|
|
36
36
|
});
|
|
@@ -48,7 +48,7 @@ it("name, type and description", () => {
|
|
|
48
48
|
d`
|
|
49
49
|
/**
|
|
50
50
|
* @param {string} somebody Somebody's name.
|
|
51
|
-
|
|
51
|
+
*/
|
|
52
52
|
`,
|
|
53
53
|
);
|
|
54
54
|
});
|
|
@@ -66,7 +66,7 @@ it("name, type and description with hyphen", () => {
|
|
|
66
66
|
d`
|
|
67
67
|
/**
|
|
68
68
|
* @param {string} somebody - Somebody's name.
|
|
69
|
-
|
|
69
|
+
*/
|
|
70
70
|
`,
|
|
71
71
|
);
|
|
72
72
|
});
|
|
@@ -84,7 +84,7 @@ it("name, type, description, hyphen and optional", () => {
|
|
|
84
84
|
d`
|
|
85
85
|
/**
|
|
86
86
|
* @param {string} [somebody] - Somebody's name.
|
|
87
|
-
|
|
87
|
+
*/
|
|
88
88
|
`,
|
|
89
89
|
);
|
|
90
90
|
});
|
|
@@ -108,7 +108,7 @@ it("name, type, description, hyphen and optional with default value", () => {
|
|
|
108
108
|
d`
|
|
109
109
|
/**
|
|
110
110
|
* @param {string} [somebody=John Doe] - Somebody's name.
|
|
111
|
-
|
|
111
|
+
*/
|
|
112
112
|
`,
|
|
113
113
|
);
|
|
114
114
|
});
|
|
@@ -153,7 +153,7 @@ it("name, type, description, hyphen and optional with default value with a very
|
|
|
153
153
|
* even a codename (e.g., "Agent X"). It's used primarily for display
|
|
154
154
|
* purposes, logging, or greeting messages and is not required to be unique or
|
|
155
155
|
* validated unless specified by the caller.
|
|
156
|
-
|
|
156
|
+
*/
|
|
157
157
|
`,
|
|
158
158
|
{ printWidth: 80 },
|
|
159
159
|
);
|
|
@@ -181,7 +181,7 @@ it("name, type, description, hyphen and optional with default value with a descr
|
|
|
181
181
|
/**
|
|
182
182
|
* @param {string} [somebody=John Doe] - Somebody's name. This is one line
|
|
183
183
|
* This is another line
|
|
184
|
-
|
|
184
|
+
*/
|
|
185
185
|
`,
|
|
186
186
|
{ printWidth: 80 },
|
|
187
187
|
);
|
|
@@ -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 {
|
|
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
|
});
|