-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
index.test-d.ts
598 lines (517 loc) · 16.1 KB
/
index.test-d.ts
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import type {Root as HastRoot} from 'hast'
import type {Root as MdastRoot} from 'mdast'
import {expectType} from 'tsd'
import type {Node as UnistNode} from 'unist'
import type {VFile} from 'vfile'
import type {Plugin, Processor, TransformCallback} from './index.js'
import {unified} from './index.js'
expectType<Processor>(unified())
expectType<Processor>(unified().freeze())
type ReactNode = {
kind: string
}
type ExampleOptionalOptions = {
example?: string | null | undefined
}
type ExampleRequiredOptions = {
example: string
}
const hastRoot: HastRoot = {
type: 'root',
children: [{type: 'element', tagName: 'p', properties: {}, children: []}]
}
const mdastRoot: MdastRoot = {
type: 'root',
children: [{type: 'paragraph', children: []}]
}
// # Explicitly typed plugins
// ## Plugin w/o options
const pluginWithoutOptions: Plugin = function () {
// Empty.
}
unified().use(pluginWithoutOptions)
// @ts-expect-error: plugin does not expect options.
unified().use(pluginWithoutOptions, {})
// @ts-expect-error: plugin does not expect `string` as options.
unified().use(pluginWithoutOptions, '')
// @ts-expect-error: plugin does not expect anything.
unified().use(pluginWithoutOptions, undefined)
// ## Plugin w/ optional options
const pluginWithOptionalOptions: Plugin<
[(ExampleOptionalOptions | null | undefined)?]
> = function (options) {
expectType<ExampleOptionalOptions | null | undefined>(options)
}
unified().use(pluginWithOptionalOptions)
unified().use(pluginWithOptionalOptions, {})
unified().use(pluginWithOptionalOptions, {example: null})
unified().use(pluginWithOptionalOptions, {example: undefined})
unified().use(pluginWithOptionalOptions, {example: 'asd'})
// @ts-expect-error: plugin does not accept `whatever`.
unified().use(pluginWithOptionalOptions, {whatever: 1})
// ## Plugin w/ required options
const pluginWithOptions: Plugin<[ExampleRequiredOptions]> = function (options) {
expectType<ExampleRequiredOptions>(options)
}
// @ts-expect-error: plugin requires options.
unified().use(pluginWithOptions)
// @ts-expect-error: plugin requires particular option.
unified().use(pluginWithOptions, {})
unified().use(pluginWithOptions, {example: ''})
// ## Plugin w/ several arguments
const pluginWithSeveralArguments: Plugin<[ExampleRequiredOptions, number]> =
function (options, value) {
expectType<ExampleRequiredOptions>(options)
expectType<number>(value)
}
// @ts-expect-error: plugin requires options.
unified().use(pluginWithSeveralArguments)
// @ts-expect-error: plugin requires particular option.
unified().use(pluginWithSeveralArguments, {})
// @ts-expect-error: plugin requires more arguments.
unified().use(pluginWithSeveralArguments, {example: ''})
unified().use(pluginWithSeveralArguments, {example: ''}, 1)
// # Implicitly typed plugins.
// ## Plugin without options.
function pluginWithoutOptionsImplicit() {
// Empty.
}
unified().use(pluginWithoutOptionsImplicit)
unified().use(
pluginWithoutOptionsImplicit,
// @ts-expect-error: plugin does not accept options.
{}
)
// ## Plugin w/ optional options
function pluginWithOptionalOptionsImplicit(
options?: ExampleOptionalOptions | null | undefined
) {
expectType<ExampleOptionalOptions | null | undefined>(options)
}
unified().use(pluginWithOptionalOptionsImplicit)
unified().use(pluginWithOptionalOptionsImplicit, {})
unified().use(pluginWithOptionalOptionsImplicit, {example: null})
unified().use(pluginWithOptionalOptionsImplicit, {example: undefined})
unified().use(pluginWithOptionalOptionsImplicit, {example: 'asd'})
unified().use(
pluginWithOptionalOptionsImplicit,
// @ts-expect-error: plugin does not accept `whatever`.
{whatever: 1}
)
// ## Plugin w/ required options
function pluginWithOptionsImplicit(options: ExampleRequiredOptions) {
expectType<ExampleRequiredOptions>(options)
}
// @ts-expect-error: plugin requires options.
unified().use(pluginWithOptionsImplicit)
unified().use(
pluginWithOptionsImplicit,
// @ts-expect-error: plugin requires particular option.
{}
)
unified().use(pluginWithOptionsImplicit, {example: ''})
// ## Plugin w/ several arguments
function pluginWithSeveralArgumentsImplicit(
options: ExampleRequiredOptions,
value: number
) {
expectType<ExampleRequiredOptions>(options)
expectType<number>(value)
}
// @ts-expect-error: plugin requires options.
unified().use(pluginWithSeveralArgumentsImplicit)
unified().use(
pluginWithSeveralArgumentsImplicit,
// @ts-expect-error: plugin requires particular option.
{}
)
unified().use(
pluginWithSeveralArgumentsImplicit,
// @ts-expect-error: plugin requires more arguments.
{example: ''}
)
unified().use(pluginWithSeveralArgumentsImplicit, {example: ''}, 1)
// # Different ways of passing options
unified()
.use(pluginWithOptions, {example: ''})
.use([[pluginWithOptions, {example: ''}]])
.use({
plugins: [[pluginWithOptions, {example: ''}]]
})
// # Turning plugins on/off w/ booleans
unified()
.use(pluginWithoutOptions, true)
.use(pluginWithoutOptions, false)
.use([
[pluginWithoutOptions, true],
[pluginWithoutOptions, false]
])
.use({
plugins: [
[pluginWithoutOptions, true],
[pluginWithoutOptions, false]
]
})
// # Plugin defining parser/compiler
unified().use(function () {
// Function.
this.parser = function (document, file) {
expectType<string>(document)
expectType<VFile>(file)
return {type: ''}
}
// Function.
this.compiler = function (tree, file) {
expectType<UnistNode>(tree)
expectType<VFile>(file)
return ''
}
})
// # Plugins w/ transformer
unified()
// Sync w/ nothing (baseline).
.use(function () {
return function (tree, file) {
expectType<UnistNode>(tree)
expectType<VFile>(file)
}
})
// Sync yielding tree.
.use(function () {
return function () {
return {type: 'x'}
}
})
// Sync yielding explicit `undefined`.
.use(function () {
return function () {
return undefined
}
})
// Sync yielding implicit `void` (because TS).
.use(function () {
return function () {
// Empty.
}
})
// Sync yielding error.
.use(function () {
return function (x) {
return new Error('x')
}
})
// Sync throwing error.
.use(function () {
return function (x) {
// Note: TS doesn’t like the `never` if we remove this useless condition.
if (x) {
throw new Error('x')
}
}
})
// Sync calling `next` w/ tree.
.use(function () {
return function (_1, _2, next) {
expectType<TransformCallback>(next)
next(undefined, {type: 'x'})
}
})
// Sync calling `next` w/ error.
.use(function () {
return function (_1, _2, next) {
next(new Error('x'))
}
})
// Async calling `next`.
.use(function () {
return function (_1, _2, next) {
setImmediate(function () {
next()
})
}
})
// Async calling `next` w/ tree.
.use(function () {
return function (_1, _2, next) {
setImmediate(function () {
next(undefined, {type: 'x'})
})
}
})
// Async calling `next` w/ error.
.use(function () {
return function (_1, _2, next) {
setImmediate(function () {
next(new Error('x'))
})
}
})
// Resolving nothing (baseline).
.use(function () {
return async function (tree, file) {
expectType<UnistNode>(tree)
expectType<VFile>(file)
}
})
// Resolving tree.
.use(function () {
return async function () {
return {type: 'x'}
}
})
// Resolving explicit `undefined`.
.use(function () {
return async function () {
return undefined
}
})
// Resolving implicit `void` (because TS).
.use(function () {
return async function () {
// Empty.
}
})
// Rejecting error.
.use(function () {
return async function (x) {
if (x) {
throw new Error('x')
}
}
})
// # Plugins bound to a certain node
// Parse plugins.
const remarkParse: Plugin<[], string, MdastRoot> = function () {
// Empty.
}
const processorWithRemarkParse = unified().use(remarkParse)
expectType<Processor<MdastRoot>>(processorWithRemarkParse)
expectType<MdastRoot>(processorWithRemarkParse.parse(''))
expectType<UnistNode>(processorWithRemarkParse.runSync(mdastRoot))
expectType<UnistNode>(processorWithRemarkParse.runSync(hastRoot))
expectType<Uint8Array | string>(processorWithRemarkParse.stringify(mdastRoot))
processorWithRemarkParse.stringify(hastRoot)
expectType<VFile>(processorWithRemarkParse.processSync(''))
// Inspect/transform plugin (explicit).
const remarkLint: Plugin<[], MdastRoot> = function () {
// Empty.
}
const processorWithRemarkLint = unified().use(remarkLint)
expectType<Processor<undefined, MdastRoot, MdastRoot>>(processorWithRemarkLint)
expectType<UnistNode>(processorWithRemarkLint.parse(''))
expectType<MdastRoot>(processorWithRemarkLint.runSync(mdastRoot))
// @ts-expect-error: not the correct node type.
processorWithRemarkLint.runSync(hastRoot)
expectType<Uint8Array | string>(processorWithRemarkLint.stringify(mdastRoot))
expectType<Uint8Array | string>(processorWithRemarkLint.stringify(hastRoot))
expectType<VFile>(processorWithRemarkLint.processSync(''))
// Inspect/transform plugin (implicit).
function remarkLintImplicit() {
return function (tree: MdastRoot) {
expectType<MdastRoot>(tree)
return mdastRoot
}
}
const processorWithRemarkLintImplicit = unified().use(remarkLintImplicit)
expectType<Processor<undefined, MdastRoot, MdastRoot>>(
processorWithRemarkLintImplicit
)
expectType<UnistNode>(processorWithRemarkLintImplicit.parse(''))
expectType<MdastRoot>(processorWithRemarkLintImplicit.runSync(mdastRoot))
// @ts-expect-error: not the correct node type.
processorWithRemarkLintImplicit.runSync(hastRoot)
expectType<Uint8Array | string>(
processorWithRemarkLintImplicit.stringify(mdastRoot)
)
expectType<Uint8Array | string>(
processorWithRemarkLintImplicit.stringify(hastRoot)
)
expectType<VFile>(processorWithRemarkLintImplicit.processSync(''))
// Mutate plugin (explicit).
const remarkRehype: Plugin<[], MdastRoot, HastRoot> = function () {
// Empty.
}
const processorWithRemarkRehype = unified().use(remarkRehype)
expectType<Processor<undefined, MdastRoot, HastRoot>>(processorWithRemarkRehype)
expectType<UnistNode>(processorWithRemarkRehype.parse(''))
expectType<HastRoot>(processorWithRemarkRehype.runSync(mdastRoot))
// @ts-expect-error: not the correct node type.
processorWithRemarkRehype.runSync(hastRoot)
expectType<Uint8Array | string>(processorWithRemarkRehype.stringify(hastRoot))
expectType<Uint8Array | string>(processorWithRemarkRehype.stringify(mdastRoot))
expectType<VFile>(processorWithRemarkRehype.processSync(''))
// Mutate plugin (implicit).
function remarkRehypeImplicit() {
return function (tree: MdastRoot) {
expectType<MdastRoot>(tree)
return hastRoot
}
}
const processorWithRemarkRehypeImplicit = unified().use(remarkRehypeImplicit)
expectType<Processor<undefined, MdastRoot, HastRoot>>(
processorWithRemarkRehypeImplicit
)
expectType<UnistNode>(processorWithRemarkRehypeImplicit.parse(''))
expectType<HastRoot>(processorWithRemarkRehypeImplicit.runSync(mdastRoot))
// @ts-expect-error: not the correct node type.
processorWithRemarkRehypeImplicit.runSync(hastRoot)
expectType<Uint8Array | string>(
processorWithRemarkRehypeImplicit.stringify(hastRoot)
)
expectType<Uint8Array | string>(
processorWithRemarkRehypeImplicit.stringify(mdastRoot)
)
expectType<VFile>(processorWithRemarkRehypeImplicit.processSync(''))
// Compile plugin.
const rehypeStringify: Plugin<[], HastRoot, string> = function () {
// Empty.
}
const processorWithRehypeStringify = unified().use(rehypeStringify)
expectType<Processor<undefined, undefined, undefined, HastRoot, string>>(
processorWithRehypeStringify
)
expectType<UnistNode>(processorWithRehypeStringify.parse(''))
expectType<UnistNode>(processorWithRehypeStringify.runSync(mdastRoot))
expectType<UnistNode>(processorWithRehypeStringify.runSync(hastRoot))
expectType<string>(processorWithRehypeStringify.stringify(hastRoot))
// @ts-expect-error: not the correct node type.
processorWithRehypeStringify.stringify(mdastRoot)
expectType<VFile>(processorWithRehypeStringify.processSync(''))
// Compile plugin (to an `Uint8Array`).
const rehypeStringifyUint8Array: Plugin<[], HastRoot, Uint8Array> =
function () {
// Empty.
}
const processorWithRehypeStringifyUint8Array = unified().use(
rehypeStringifyUint8Array
)
expectType<Processor<undefined, undefined, undefined, HastRoot, Uint8Array>>(
processorWithRehypeStringifyUint8Array
)
expectType<UnistNode>(processorWithRehypeStringifyUint8Array.parse(''))
expectType<UnistNode>(processorWithRehypeStringifyUint8Array.runSync(mdastRoot))
expectType<UnistNode>(processorWithRehypeStringifyUint8Array.runSync(hastRoot))
expectType<Uint8Array>(
processorWithRehypeStringifyUint8Array.stringify(hastRoot)
)
// @ts-expect-error: not the correct node type.
processorWithRehypeStringifyUint8Array.stringify(mdastRoot)
expectType<VFile>(processorWithRehypeStringifyUint8Array.processSync(''))
/**
* Register our custom compile result.
*/
declare module './index.js' {
interface CompileResultMap {
ReactNode: ReactNode
}
}
// Compile plugin (to a non-node).
const rehypeReact: Plugin<[], HastRoot, ReactNode> = function () {
// Empty.
}
const processorWithRehypeReact = unified().use(rehypeReact)
expectType<Processor<undefined, undefined, undefined, HastRoot, ReactNode>>(
processorWithRehypeReact
)
expectType<UnistNode>(processorWithRehypeReact.parse(''))
expectType<UnistNode>(processorWithRehypeReact.runSync(mdastRoot))
expectType<UnistNode>(processorWithRehypeReact.runSync(hastRoot))
expectType<ReactNode>(processorWithRehypeReact.stringify(hastRoot))
// @ts-expect-error: not the correct node type.
processorWithRehypeReact.stringify(mdastRoot)
expectType<VFile & {result: ReactNode}>(
processorWithRehypeReact.processSync('')
)
// All together.
const processorWithAll = unified()
.use(remarkParse)
.use(remarkLint)
.use(remarkLintImplicit)
.use(remarkRehype)
.use(rehypeStringify)
expectType<Processor<MdastRoot, MdastRoot, HastRoot, HastRoot, string>>(
processorWithAll
)
expectType<MdastRoot>(processorWithAll.parse(''))
expectType<HastRoot>(processorWithAll.runSync(mdastRoot))
// @ts-expect-error: not the correct node type.
processorWithAll.runSync(hastRoot)
expectType<string>(processorWithAll.stringify(hastRoot))
// @ts-expect-error: not the correct node type.
processorWithAll.stringify(mdastRoot)
expectType<VFile>(processorWithAll.processSync(''))
// Doesn’t matter how you apply, compiler, transformers, parser is also fine.
expectType<Processor<MdastRoot, MdastRoot, HastRoot, HastRoot, string>>(
unified()
.use(rehypeStringify)
.use(remarkLint)
.use(remarkLintImplicit)
.use(remarkRehype)
.use(remarkParse)
)
// # Different ways to use plugins
expectType<Processor>(unified().use([remarkParse]))
expectType<Processor>(
unified().use([
remarkParse,
remarkLint,
remarkLintImplicit,
remarkRehype,
rehypeStringify
])
)
expectType<Processor>(
unified().use({
plugins: [remarkParse]
})
)
expectType<Processor>(
unified().use({
plugins: [
remarkParse,
remarkLint,
remarkLintImplicit,
remarkRehype,
rehypeStringify
]
})
)
/**
* Register our setting.
*/
declare module './index.js' {
interface Settings {
something?: string | undefined
}
}
expectType<Processor>(
unified().use({
plugins: [
remarkParse,
remarkLint,
remarkLintImplicit,
remarkRehype,
rehypeStringify
],
settings: {something: 'stuff'}
})
)
// # Using multiple parsers/compilers
const rehypeParse: Plugin<[], string, HastRoot> = function () {
// Empty.
}
const remarkStringify: Plugin<[], MdastRoot, string> = function () {
// Empty.
}
expectType<HastRoot>(unified().use(remarkParse).use(rehypeParse).parse(''))
expectType<string>(
unified().use(remarkStringify).use(rehypeStringify).stringify(hastRoot)
)
// # Using mismatched inspect/transform plugins
const rehypeClassNames: Plugin<[], HastRoot> = function () {
// Empty.
}
// We currently only *use* types, we don’t crash if they are nonsensical.
expectType<Processor<undefined, MdastRoot, HastRoot>>(
unified().use(remarkLint).use(rehypeClassNames)
)