-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtransformers.test.mjs
More file actions
136 lines (115 loc) · 7.71 KB
/
Copy pathtransformers.test.mjs
File metadata and controls
136 lines (115 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { strictEqual } from 'node:assert';
import { describe, it } from 'node:test';
import { transformTypeToReferenceLink } from '../transformers.mjs';
describe('transformTypeToReferenceLink', () => {
it('should transform a JavaScript primitive type into a Markdown link', () => {
strictEqual(
transformTypeToReferenceLink('string'),
'[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type)'
);
});
it('should transform a JavaScript global type into a Markdown link', () => {
strictEqual(
transformTypeToReferenceLink('Array'),
'[`<Array>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)'
);
});
it('should transform a type into a Markdown link', () => {
strictEqual(
transformTypeToReferenceLink('SomeOtherType', {
SomeOtherType: 'fromTypeMap',
}),
'[`<SomeOtherType>`](fromTypeMap)'
);
});
it('should transform a basic Generic type into a Markdown link', () => {
strictEqual(
transformTypeToReferenceLink('{Promise<string>}'),
'[`<Promise>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type)>'
);
});
it('should partially transform a Generic type if only one part is known', () => {
strictEqual(
transformTypeToReferenceLink('{CustomType<string>}', {}),
'`<CustomType>`<[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type)>'
);
});
it('should transform a Generic type with an inner union like {Promise<string|boolean>}', () => {
strictEqual(
transformTypeToReferenceLink('{Promise<string|boolean>}', {}),
'[`<Promise>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type) | [`<boolean>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type)>'
);
});
it('should transform multi-parameter generics like {Map<string, number>}', () => {
strictEqual(
transformTypeToReferenceLink('{Map<string, number>}', {}),
'[`<Map>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type), [`<number>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type)>'
);
});
it('should handle outer unions with generics like {Promise<string|number> | Iterable<boolean>}', () => {
strictEqual(
transformTypeToReferenceLink(
'{Promise<string|number> | Iterable<boolean>}',
{}
),
'[`<Promise>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type) | [`<number>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type)> | [`<Iterable>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)<[`<boolean>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type)>'
);
});
it('should transform an intersection type joined with & into linked parts', () => {
strictEqual(
transformTypeToReferenceLink('{string&boolean}', {}),
'[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type) & [`<boolean>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type)'
);
});
it('should handle an intersection with generics like {Map<string, number>&Array<string>}', () => {
strictEqual(
transformTypeToReferenceLink('{Map<string, number>&Array<string>}', {}),
'[`<Map>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type), [`<number>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type)> & [`<Array>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type)>'
);
});
it('should transform a function returning a Generic type', () => {
strictEqual(
transformTypeToReferenceLink('(err: Error) => Promise<boolean>', {}),
'(err: [`<Error>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)) => [`<Promise>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[`<boolean>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type)>'
);
});
it('should respect precedence: Unions (|) are weaker than Intersections (&)', () => {
strictEqual(
transformTypeToReferenceLink('string | number & boolean', {}),
'[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type) | [`<number>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type) & [`<boolean>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type)'
);
});
it('should correctly extract TS prefix operators and link the underlying type', () => {
strictEqual(
transformTypeToReferenceLink('typeof Compiler', {
Compiler: '/api/Compiler',
}),
'typeof [`<Compiler>`](/api/Compiler)'
);
});
it('should not incorrectly strip prefixes if they are part of the type name (word boundary)', () => {
strictEqual(
transformTypeToReferenceLink('typeofSomething', {
typeofSomething: '/api/typeofSomething',
}),
'[`<typeofSomething>`](/api/typeofSomething)'
);
});
it('should handle extreme nested combinations of functions, arrays, generics, unions, and intersections', () => {
const input =
'(str: string[]) => Promise<Map<string, number & string>, Map<string | number>>';
const expected =
'(str: [`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type)[]) => [`<Promise>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[`<Map>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type), [`<number>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type) & [`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type)>, [`<Map>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type) | [`<number>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type)>>';
strictEqual(transformTypeToReferenceLink(input, {}), expected);
});
it('should parse functions with array destructuring in callbacks returning functions with object destructuring', () => {
const input =
'(cb: ([first, second]: string[]) => void) => ({ id, name }: User) => boolean';
const expected =
'(cb: ([first, second]: [`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type)[]) => [`<void>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/void)) => ({ id, name }: [`<User>`](userLink)) => [`<boolean>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type)';
strictEqual(
transformTypeToReferenceLink(input, { User: 'userLink' }),
expected
);
});
});