-
-
Notifications
You must be signed in to change notification settings - Fork 724
Expand file tree
/
Copy pathrequire-playground-link.test.js
More file actions
217 lines (180 loc) · 6.52 KB
/
Copy pathrequire-playground-link.test.js
File metadata and controls
217 lines (180 loc) · 6.52 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import outdent from 'outdent';
import {createRuleTester, dedenter, exportType, exportTypeAndOption, fence, jsdoc} from './test-utils.js';
import {generateLinkText, requirePlaygroundLinkRule} from './require-playground-link.js';
const ruleTester = createRuleTester();
const fenceWithLink = (code, lang = '', linkCode = code) =>
outdent`
${fence(code, lang)}
${generateLinkText(linkCode)}
`;
const missingPlaygroundLinkError = getPrefixes => {
// Track the number of times `fence` is called to determine the no. of errors
let callCount = 0;
const trackedFence = (...args) => {
callCount++;
return fence(...args);
};
return {
// Codeblocks in input code don't have playground links
code: exportTypeAndOption(...getPrefixes(trackedFence)),
// Create two error objects for each `fence` call (one for type and one for option)
errors: Array.from({length: callCount * 2}, () => ({messageId: 'missingPlaygroundLink'})),
// Codeblocks in output code have playground links
output: exportTypeAndOption(...getPrefixes(fenceWithLink)),
};
};
const incorrectPlaygroundLinkError = getPrefixes => {
// Track the number of times `fence` is called to determine the no. of errors
let callCount = 0;
const trackedFenceWithIncorrectLink = (code, lang) => {
callCount++;
return fenceWithLink(code, lang, code + '\n// incorrect');
};
return {
// Codeblocks in input code have incorrect playground links
code: exportTypeAndOption(...getPrefixes(trackedFenceWithIncorrectLink)),
// Create two error objects for each `fence` call (one for type and one for option)
errors: Array.from({length: callCount * 2}, () => ({messageId: 'incorrectPlaygroundLink'})),
// Codeblocks in output code have correct playground links
output: exportTypeAndOption(...getPrefixes(fenceWithLink)),
};
};
// Code samples
const code1 = outdent`
type A = string;
//=> string
type B = number;
//=> number
`;
const code2 = outdent`
type T1 = {
foo: string;
bar: number;
};
type T2 = T1[keyof T1];
//=> string | number
`;
ruleTester.run('require-playground-link', requirePlaygroundLinkRule, {
valid: [
// Not exported
dedenter`
${jsdoc(fence(code1))}
type NotExported = string;
`,
dedenter`
type NotExportedOptions = {
${jsdoc(fence(code1))}
p1: string;
}
`,
// Internal (leading underscore)
dedenter`
${jsdoc(fence(code1))}
export type _Internal = string;
`,
dedenter`
export type _InternalOptions = {
${jsdoc(fence(code1))}
p1: string;
}
`,
// Without `Options` suffix
dedenter`
export type NoSuffix = {
${jsdoc(fence(code1))}
p1: string;
}
`,
// No JSDoc
exportTypeAndOption(''),
exportType('type Some = number;'),
exportTypeAndOption('// Not block comment'),
exportTypeAndOption('/* Block comment, but not JSDoc */'),
// No codeblock in JSDoc
exportType(jsdoc('No codeblock here')),
// Valid link
exportTypeAndOption(jsdoc(fenceWithLink(code1))),
// With text before and after
exportTypeAndOption(jsdoc('Some description.', fenceWithLink(code1), '@category Test')),
// With line breaks before and after
exportTypeAndOption(
jsdoc('Some description.\n', 'Note: Some note.\n', fenceWithLink(code1, 'ts'), '\n@category Test'),
),
// With `@example` tag
exportTypeAndOption(jsdoc('@example', fenceWithLink(code1))),
// With language specifiers
exportTypeAndOption(jsdoc(fenceWithLink(code1, 'ts'))),
exportTypeAndOption(jsdoc(fenceWithLink(code1, 'typescript'))),
// Multiple code blocks
exportTypeAndOption(
jsdoc('@example', fenceWithLink(code1, 'ts'), '\nSome text in between.\n', '@example', fenceWithLink(code2)),
),
// Multiple exports and multiple properties
exportTypeAndOption(jsdoc(fenceWithLink(code1)), jsdoc(fenceWithLink(code2))),
],
invalid: [
// Missing link
missingPlaygroundLinkError(fence => [jsdoc(fence(code1))]),
// With text before and after
missingPlaygroundLinkError(fence => [jsdoc('Some description.', fence(code1), '@category Test')]),
// With line breaks before and after
missingPlaygroundLinkError(fence => [
jsdoc('Some description.\n', 'Note: Some note.\n', fence(code1, 'ts'), '\n@category Test'),
]),
// With `@example` tag
missingPlaygroundLinkError(fence => [jsdoc('@example', fence(code1))]),
// With language specifiers
missingPlaygroundLinkError(fence => [jsdoc(fence(code1, 'ts'))]),
missingPlaygroundLinkError(fence => [jsdoc(fence(code1, 'typescript'))]),
// Multiple code blocks
missingPlaygroundLinkError(fence => [
jsdoc('@example', fence(code1, 'ts'), '\nSome text in between.\n', '@example', fence(code2)),
]),
// Multiple exports and multiple properties
missingPlaygroundLinkError(fence => [jsdoc(fence(code1)), jsdoc(fence(code2))]),
// Incorrect existing link
incorrectPlaygroundLinkError(fence => [jsdoc(fence(code1))]),
// Fix indentation
{
code: exportTypeAndOption(jsdoc(fence(code1), '\t' + generateLinkText(code1))),
output: exportTypeAndOption(jsdoc(fenceWithLink(code1))),
errors: [{messageId: 'incorrectPlaygroundLink'}, {messageId: 'incorrectPlaygroundLink'}],
},
{
code: exportTypeAndOption(jsdoc(dedenter`
1. First point
${fence(code1)}
${generateLinkText(code1)}
`)),
output: exportTypeAndOption(jsdoc(dedenter`
1. First point
${fence(code1)}
${generateLinkText(code1)}
`)),
errors: [{messageId: 'incorrectPlaygroundLink'}, {messageId: 'incorrectPlaygroundLink'}],
},
// Empty link
{
code: exportTypeAndOption(jsdoc(fence(code1), '[Playground Link]()')),
output: exportTypeAndOption(jsdoc(fenceWithLink(code1))),
errors: [{messageId: 'incorrectPlaygroundLink'}, {messageId: 'incorrectPlaygroundLink'}],
},
// With text before and after
incorrectPlaygroundLinkError(fence => [jsdoc('Some description.', fence(code1), '@category Test')]),
// With line breaks before and after
incorrectPlaygroundLinkError(fence => [
jsdoc('Some description.\n', 'Note: Some note.\n', fence(code1, 'ts'), '\n@category Test'),
]),
// With `@example` tag
incorrectPlaygroundLinkError(fence => [jsdoc('@example', fence(code1))]),
// With language specifiers
incorrectPlaygroundLinkError(fence => [jsdoc(fence(code1, 'ts'))]),
incorrectPlaygroundLinkError(fence => [jsdoc(fence(code1, 'typescript'))]),
// Multiple code blocks
incorrectPlaygroundLinkError(fence => [
jsdoc('@example', fence(code1, 'ts'), '\nSome text in between.\n', '@example', fence(code2)),
]),
// Multiple exports and multiple properties
incorrectPlaygroundLinkError(fence => [jsdoc(fence(code1)), jsdoc(fence(code2))]),
],
});