@scalar/code-highlight 0.4.6 → 0.4.8

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.
@@ -1,736 +0,0 @@
1
- //#region ../../node_modules/.pnpm/unist-util-is@6.0.0/node_modules/unist-util-is/lib/index.js
2
- /**
3
- * Generate an assertion from a test.
4
- *
5
- * Useful if you’re going to test many nodes, for example when creating a
6
- * utility where something else passes a compatible test.
7
- *
8
- * The created function is a bit faster because it expects valid input only:
9
- * a `node`, `index`, and `parent`.
10
- *
11
- * @param {Test} test
12
- * * when nullish, checks if `node` is a `Node`.
13
- * * when `string`, works like passing `(node) => node.type === test`.
14
- * * when `function` checks if function passed the node is true.
15
- * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
16
- * * when `array`, checks if any one of the subtests pass.
17
- * @returns {Check}
18
- * An assertion.
19
- */
20
- var convert = (
21
- /**
22
- * @param {Test} [test]
23
- * @returns {Check}
24
- */
25
- function(test) {
26
- if (test === null || test === void 0) return ok;
27
- if (typeof test === "function") return castFactory(test);
28
- if (typeof test === "object") return Array.isArray(test) ? anyFactory(test) : propsFactory(test);
29
- if (typeof test === "string") return typeFactory(test);
30
- throw new Error("Expected function, string, or object as test");
31
- });
32
- /**
33
- * @param {Array<Props | TestFunction | string>} tests
34
- * @returns {Check}
35
- */
36
- function anyFactory(tests) {
37
- /** @type {Array<Check>} */
38
- const checks = [];
39
- let index = -1;
40
- while (++index < tests.length) checks[index] = convert(tests[index]);
41
- return castFactory(any);
42
- /**
43
- * @this {unknown}
44
- * @type {TestFunction}
45
- */
46
- function any(...parameters) {
47
- let index = -1;
48
- while (++index < checks.length) if (checks[index].apply(this, parameters)) return true;
49
- return false;
50
- }
51
- }
52
- /**
53
- * Turn an object into a test for a node with a certain fields.
54
- *
55
- * @param {Props} check
56
- * @returns {Check}
57
- */
58
- function propsFactory(check) {
59
- const checkAsRecord = check;
60
- return castFactory(all);
61
- /**
62
- * @param {Node} node
63
- * @returns {boolean}
64
- */
65
- function all(node) {
66
- const nodeAsRecord = node;
67
- /** @type {string} */
68
- let key;
69
- for (key in check) if (nodeAsRecord[key] !== checkAsRecord[key]) return false;
70
- return true;
71
- }
72
- }
73
- /**
74
- * Turn a string into a test for a node with a certain type.
75
- *
76
- * @param {string} check
77
- * @returns {Check}
78
- */
79
- function typeFactory(check) {
80
- return castFactory(type);
81
- /**
82
- * @param {Node} node
83
- */
84
- function type(node) {
85
- return node && node.type === check;
86
- }
87
- }
88
- /**
89
- * Turn a custom test into a test for a node that passes that test.
90
- *
91
- * @param {TestFunction} testFunction
92
- * @returns {Check}
93
- */
94
- function castFactory(testFunction) {
95
- return check;
96
- /**
97
- * @this {unknown}
98
- * @type {Check}
99
- */
100
- function check(value, index, parent) {
101
- return Boolean(looksLikeANode(value) && testFunction.call(this, value, typeof index === "number" ? index : void 0, parent || void 0));
102
- }
103
- }
104
- function ok() {
105
- return true;
106
- }
107
- /**
108
- * @param {unknown} value
109
- * @returns {value is Node}
110
- */
111
- function looksLikeANode(value) {
112
- return value !== null && typeof value === "object" && "type" in value;
113
- }
114
- //#endregion
115
- //#region ../../node_modules/.pnpm/unist-util-visit-parents@6.0.2/node_modules/unist-util-visit-parents/lib/color.js
116
- /**
117
- * @param {string} d
118
- * @returns {string}
119
- */
120
- function color(d) {
121
- return d;
122
- }
123
- //#endregion
124
- //#region ../../node_modules/.pnpm/unist-util-visit-parents@6.0.2/node_modules/unist-util-visit-parents/lib/index.js
125
- /**
126
- * @import {Node as UnistNode, Parent as UnistParent} from 'unist'
127
- */
128
- /**
129
- * @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test
130
- * Test from `unist-util-is`.
131
- *
132
- * Note: we have remove and add `undefined`, because otherwise when generating
133
- * automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
134
- * which doesn’t work when publishing on npm.
135
- */
136
- /**
137
- * @typedef {(
138
- * Fn extends (value: any) => value is infer Thing
139
- * ? Thing
140
- * : Fallback
141
- * )} Predicate
142
- * Get the value of a type guard `Fn`.
143
- * @template Fn
144
- * Value; typically function that is a type guard (such as `(x): x is Y`).
145
- * @template Fallback
146
- * Value to yield if `Fn` is not a type guard.
147
- */
148
- /**
149
- * @typedef {(
150
- * Check extends null | undefined // No test.
151
- * ? Value
152
- * : Value extends {type: Check} // String (type) test.
153
- * ? Value
154
- * : Value extends Check // Partial test.
155
- * ? Value
156
- * : Check extends Function // Function test.
157
- * ? Predicate<Check, Value> extends Value
158
- * ? Predicate<Check, Value>
159
- * : never
160
- * : never // Some other test?
161
- * )} MatchesOne
162
- * Check whether a node matches a primitive check in the type system.
163
- * @template Value
164
- * Value; typically unist `Node`.
165
- * @template Check
166
- * Value; typically `unist-util-is`-compatible test, but not arrays.
167
- */
168
- /**
169
- * @typedef {(
170
- * Check extends ReadonlyArray<infer T>
171
- * ? MatchesOne<Value, T>
172
- * : Check extends Array<infer T>
173
- * ? MatchesOne<Value, T>
174
- * : MatchesOne<Value, Check>
175
- * )} Matches
176
- * Check whether a node matches a check in the type system.
177
- * @template Value
178
- * Value; typically unist `Node`.
179
- * @template Check
180
- * Value; typically `unist-util-is`-compatible test.
181
- */
182
- /**
183
- * @typedef {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} Uint
184
- * Number; capped reasonably.
185
- */
186
- /**
187
- * @typedef {I extends 0 ? 1 : I extends 1 ? 2 : I extends 2 ? 3 : I extends 3 ? 4 : I extends 4 ? 5 : I extends 5 ? 6 : I extends 6 ? 7 : I extends 7 ? 8 : I extends 8 ? 9 : 10} Increment
188
- * Increment a number in the type system.
189
- * @template {Uint} [I=0]
190
- * Index.
191
- */
192
- /**
193
- * @typedef {(
194
- * Node extends UnistParent
195
- * ? Node extends {children: Array<infer Children>}
196
- * ? Child extends Children ? Node : never
197
- * : never
198
- * : never
199
- * )} InternalParent
200
- * Collect nodes that can be parents of `Child`.
201
- * @template {UnistNode} Node
202
- * All node types in a tree.
203
- * @template {UnistNode} Child
204
- * Node to search for.
205
- */
206
- /**
207
- * @typedef {InternalParent<InclusiveDescendant<Tree>, Child>} Parent
208
- * Collect nodes in `Tree` that can be parents of `Child`.
209
- * @template {UnistNode} Tree
210
- * All node types in a tree.
211
- * @template {UnistNode} Child
212
- * Node to search for.
213
- */
214
- /**
215
- * @typedef {(
216
- * Depth extends Max
217
- * ? never
218
- * :
219
- * | InternalParent<Node, Child>
220
- * | InternalAncestor<Node, InternalParent<Node, Child>, Max, Increment<Depth>>
221
- * )} InternalAncestor
222
- * Collect nodes in `Tree` that can be ancestors of `Child`.
223
- * @template {UnistNode} Node
224
- * All node types in a tree.
225
- * @template {UnistNode} Child
226
- * Node to search for.
227
- * @template {Uint} [Max=10]
228
- * Max; searches up to this depth.
229
- * @template {Uint} [Depth=0]
230
- * Current depth.
231
- */
232
- /**
233
- * @typedef {InternalAncestor<InclusiveDescendant<Tree>, Child>} Ancestor
234
- * Collect nodes in `Tree` that can be ancestors of `Child`.
235
- * @template {UnistNode} Tree
236
- * All node types in a tree.
237
- * @template {UnistNode} Child
238
- * Node to search for.
239
- */
240
- /**
241
- * @typedef {(
242
- * Tree extends UnistParent
243
- * ? Depth extends Max
244
- * ? Tree
245
- * : Tree | InclusiveDescendant<Tree['children'][number], Max, Increment<Depth>>
246
- * : Tree
247
- * )} InclusiveDescendant
248
- * Collect all (inclusive) descendants of `Tree`.
249
- *
250
- * > 👉 **Note**: for performance reasons, this seems to be the fastest way to
251
- * > recurse without actually running into an infinite loop, which the
252
- * > previous version did.
253
- * >
254
- * > Practically, a max of `2` is typically enough assuming a `Root` is
255
- * > passed, but it doesn’t improve performance.
256
- * > It gets higher with `List > ListItem > Table > TableRow > TableCell`.
257
- * > Using up to `10` doesn’t hurt or help either.
258
- * @template {UnistNode} Tree
259
- * Tree type.
260
- * @template {Uint} [Max=10]
261
- * Max; searches up to this depth.
262
- * @template {Uint} [Depth=0]
263
- * Current depth.
264
- */
265
- /**
266
- * @typedef {'skip' | boolean} Action
267
- * Union of the action types.
268
- *
269
- * @typedef {number} Index
270
- * Move to the sibling at `index` next (after node itself is completely
271
- * traversed).
272
- *
273
- * Useful if mutating the tree, such as removing the node the visitor is
274
- * currently on, or any of its previous siblings.
275
- * Results less than 0 or greater than or equal to `children.length` stop
276
- * traversing the parent.
277
- *
278
- * @typedef {[(Action | null | undefined | void)?, (Index | null | undefined)?]} ActionTuple
279
- * List with one or two values, the first an action, the second an index.
280
- *
281
- * @typedef {Action | ActionTuple | Index | null | undefined | void} VisitorResult
282
- * Any value that can be returned from a visitor.
283
- */
284
- /**
285
- * @callback Visitor
286
- * Handle a node (matching `test`, if given).
287
- *
288
- * Visitors are free to transform `node`.
289
- * They can also transform the parent of node (the last of `ancestors`).
290
- *
291
- * Replacing `node` itself, if `SKIP` is not returned, still causes its
292
- * descendants to be walked (which is a bug).
293
- *
294
- * When adding or removing previous siblings of `node` (or next siblings, in
295
- * case of reverse), the `Visitor` should return a new `Index` to specify the
296
- * sibling to traverse after `node` is traversed.
297
- * Adding or removing next siblings of `node` (or previous siblings, in case
298
- * of reverse) is handled as expected without needing to return a new `Index`.
299
- *
300
- * Removing the children property of an ancestor still results in them being
301
- * traversed.
302
- * @param {Visited} node
303
- * Found node.
304
- * @param {Array<VisitedParents>} ancestors
305
- * Ancestors of `node`.
306
- * @returns {VisitorResult}
307
- * What to do next.
308
- *
309
- * An `Index` is treated as a tuple of `[CONTINUE, Index]`.
310
- * An `Action` is treated as a tuple of `[Action]`.
311
- *
312
- * Passing a tuple back only makes sense if the `Action` is `SKIP`.
313
- * When the `Action` is `EXIT`, that action can be returned.
314
- * When the `Action` is `CONTINUE`, `Index` can be returned.
315
- * @template {UnistNode} [Visited=UnistNode]
316
- * Visited node type.
317
- * @template {UnistParent} [VisitedParents=UnistParent]
318
- * Ancestor type.
319
- */
320
- /**
321
- * @typedef {Visitor<Matches<InclusiveDescendant<Tree>, Check>, Ancestor<Tree, Matches<InclusiveDescendant<Tree>, Check>>>} BuildVisitor
322
- * Build a typed `Visitor` function from a tree and a test.
323
- *
324
- * It will infer which values are passed as `node` and which as `parents`.
325
- * @template {UnistNode} [Tree=UnistNode]
326
- * Tree type.
327
- * @template {Test} [Check=Test]
328
- * Test type.
329
- */
330
- /** @type {Readonly<ActionTuple>} */
331
- var empty = [];
332
- /**
333
- * Do not traverse this node’s children.
334
- */
335
- var SKIP = "skip";
336
- /**
337
- * Visit nodes, with ancestral information.
338
- *
339
- * This algorithm performs *depth-first* *tree traversal* in *preorder*
340
- * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
341
- *
342
- * You can choose for which nodes `visitor` is called by passing a `test`.
343
- * For complex tests, you should test yourself in `visitor`, as it will be
344
- * faster and will have improved type information.
345
- *
346
- * Walking the tree is an intensive task.
347
- * Make use of the return values of the visitor when possible.
348
- * Instead of walking a tree multiple times, walk it once, use `unist-util-is`
349
- * to check if a node matches, and then perform different operations.
350
- *
351
- * You can change the tree.
352
- * See `Visitor` for more info.
353
- *
354
- * @overload
355
- * @param {Tree} tree
356
- * @param {Check} check
357
- * @param {BuildVisitor<Tree, Check>} visitor
358
- * @param {boolean | null | undefined} [reverse]
359
- * @returns {undefined}
360
- *
361
- * @overload
362
- * @param {Tree} tree
363
- * @param {BuildVisitor<Tree>} visitor
364
- * @param {boolean | null | undefined} [reverse]
365
- * @returns {undefined}
366
- *
367
- * @param {UnistNode} tree
368
- * Tree to traverse.
369
- * @param {Visitor | Test} test
370
- * `unist-util-is`-compatible test
371
- * @param {Visitor | boolean | null | undefined} [visitor]
372
- * Handle each node.
373
- * @param {boolean | null | undefined} [reverse]
374
- * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
375
- * @returns {undefined}
376
- * Nothing.
377
- *
378
- * @template {UnistNode} Tree
379
- * Node type.
380
- * @template {Test} Check
381
- * `unist-util-is`-compatible test.
382
- */
383
- function visitParents(tree, test, visitor, reverse) {
384
- /** @type {Test} */
385
- let check;
386
- if (typeof test === "function" && typeof visitor !== "function") {
387
- reverse = visitor;
388
- visitor = test;
389
- } else check = test;
390
- const is = convert(check);
391
- const step = reverse ? -1 : 1;
392
- factory(tree, void 0, [])();
393
- /**
394
- * @param {UnistNode} node
395
- * @param {number | undefined} index
396
- * @param {Array<UnistParent>} parents
397
- */
398
- function factory(node, index, parents) {
399
- const value = node && typeof node === "object" ? node : {};
400
- if (typeof value.type === "string") {
401
- const name = typeof value.tagName === "string" ? value.tagName : typeof value.name === "string" ? value.name : void 0;
402
- Object.defineProperty(visit, "name", { value: "node (" + color(node.type + (name ? "<" + name + ">" : "")) + ")" });
403
- }
404
- return visit;
405
- function visit() {
406
- /** @type {Readonly<ActionTuple>} */
407
- let result = empty;
408
- /** @type {Readonly<ActionTuple>} */
409
- let subresult;
410
- /** @type {number} */
411
- let offset;
412
- /** @type {Array<UnistParent>} */
413
- let grandparents;
414
- if (!test || is(node, index, parents[parents.length - 1] || void 0)) {
415
- result = toResult(visitor(node, parents));
416
- if (result[0] === false) return result;
417
- }
418
- if ("children" in node && node.children) {
419
- const nodeAsParent = node;
420
- if (nodeAsParent.children && result[0] !== "skip") {
421
- offset = (reverse ? nodeAsParent.children.length : -1) + step;
422
- grandparents = parents.concat(nodeAsParent);
423
- while (offset > -1 && offset < nodeAsParent.children.length) {
424
- const child = nodeAsParent.children[offset];
425
- subresult = factory(child, offset, grandparents)();
426
- if (subresult[0] === false) return subresult;
427
- offset = typeof subresult[1] === "number" ? subresult[1] : offset + step;
428
- }
429
- }
430
- }
431
- return result;
432
- }
433
- }
434
- }
435
- /**
436
- * Turn a return value into a clean result.
437
- *
438
- * @param {VisitorResult} value
439
- * Valid return values from visitors.
440
- * @returns {Readonly<ActionTuple>}
441
- * Clean result.
442
- */
443
- function toResult(value) {
444
- if (Array.isArray(value)) return value;
445
- if (typeof value === "number") return [true, value];
446
- return value === null || value === void 0 ? empty : [value];
447
- }
448
- //#endregion
449
- //#region ../../node_modules/.pnpm/unist-util-visit@5.1.0/node_modules/unist-util-visit/lib/index.js
450
- /**
451
- * @import {Node as UnistNode, Parent as UnistParent} from 'unist'
452
- * @import {VisitorResult} from 'unist-util-visit-parents'
453
- */
454
- /**
455
- * @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test
456
- * Test from `unist-util-is`.
457
- *
458
- * Note: we have remove and add `undefined`, because otherwise when generating
459
- * automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
460
- * which doesn’t work when publishing on npm.
461
- */
462
- /**
463
- * @typedef {(
464
- * Fn extends (value: any) => value is infer Thing
465
- * ? Thing
466
- * : Fallback
467
- * )} Predicate
468
- * Get the value of a type guard `Fn`.
469
- * @template Fn
470
- * Value; typically function that is a type guard (such as `(x): x is Y`).
471
- * @template Fallback
472
- * Value to yield if `Fn` is not a type guard.
473
- */
474
- /**
475
- * @typedef {(
476
- * Check extends null | undefined // No test.
477
- * ? Value
478
- * : Value extends {type: Check} // String (type) test.
479
- * ? Value
480
- * : Value extends Check // Partial test.
481
- * ? Value
482
- * : Check extends Function // Function test.
483
- * ? Predicate<Check, Value> extends Value
484
- * ? Predicate<Check, Value>
485
- * : never
486
- * : never // Some other test?
487
- * )} MatchesOne
488
- * Check whether a node matches a primitive check in the type system.
489
- * @template Value
490
- * Value; typically unist `Node`.
491
- * @template Check
492
- * Value; typically `unist-util-is`-compatible test, but not arrays.
493
- */
494
- /**
495
- * @typedef {(
496
- * Check extends ReadonlyArray<any>
497
- * ? MatchesOne<Value, Check[number]>
498
- * : MatchesOne<Value, Check>
499
- * )} Matches
500
- * Check whether a node matches a check in the type system.
501
- * @template Value
502
- * Value; typically unist `Node`.
503
- * @template Check
504
- * Value; typically `unist-util-is`-compatible test.
505
- */
506
- /**
507
- * @typedef {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} Uint
508
- * Number; capped reasonably.
509
- */
510
- /**
511
- * @typedef {I extends 0 ? 1 : I extends 1 ? 2 : I extends 2 ? 3 : I extends 3 ? 4 : I extends 4 ? 5 : I extends 5 ? 6 : I extends 6 ? 7 : I extends 7 ? 8 : I extends 8 ? 9 : 10} Increment
512
- * Increment a number in the type system.
513
- * @template {Uint} [I=0]
514
- * Index.
515
- */
516
- /**
517
- * @typedef {(
518
- * Node extends UnistParent
519
- * ? Node extends {children: Array<infer Children>}
520
- * ? Child extends Children ? Node : never
521
- * : never
522
- * : never
523
- * )} InternalParent
524
- * Collect nodes that can be parents of `Child`.
525
- * @template {UnistNode} Node
526
- * All node types in a tree.
527
- * @template {UnistNode} Child
528
- * Node to search for.
529
- */
530
- /**
531
- * @typedef {InternalParent<InclusiveDescendant<Tree>, Child>} Parent
532
- * Collect nodes in `Tree` that can be parents of `Child`.
533
- * @template {UnistNode} Tree
534
- * All node types in a tree.
535
- * @template {UnistNode} Child
536
- * Node to search for.
537
- */
538
- /**
539
- * @typedef {(
540
- * Depth extends Max
541
- * ? never
542
- * :
543
- * | InternalParent<Node, Child>
544
- * | InternalAncestor<Node, InternalParent<Node, Child>, Max, Increment<Depth>>
545
- * )} InternalAncestor
546
- * Collect nodes in `Tree` that can be ancestors of `Child`.
547
- * @template {UnistNode} Node
548
- * All node types in a tree.
549
- * @template {UnistNode} Child
550
- * Node to search for.
551
- * @template {Uint} [Max=10]
552
- * Max; searches up to this depth.
553
- * @template {Uint} [Depth=0]
554
- * Current depth.
555
- */
556
- /**
557
- * @typedef {(
558
- * Tree extends UnistParent
559
- * ? Depth extends Max
560
- * ? Tree
561
- * : Tree | InclusiveDescendant<Tree['children'][number], Max, Increment<Depth>>
562
- * : Tree
563
- * )} InclusiveDescendant
564
- * Collect all (inclusive) descendants of `Tree`.
565
- *
566
- * > 👉 **Note**: for performance reasons, this seems to be the fastest way to
567
- * > recurse without actually running into an infinite loop, which the
568
- * > previous version did.
569
- * >
570
- * > Practically, a max of `2` is typically enough assuming a `Root` is
571
- * > passed, but it doesn’t improve performance.
572
- * > It gets higher with `List > ListItem > Table > TableRow > TableCell`.
573
- * > Using up to `10` doesn’t hurt or help either.
574
- * @template {UnistNode} Tree
575
- * Tree type.
576
- * @template {Uint} [Max=10]
577
- * Max; searches up to this depth.
578
- * @template {Uint} [Depth=0]
579
- * Current depth.
580
- */
581
- /**
582
- * @callback Visitor
583
- * Handle a node (matching `test`, if given).
584
- *
585
- * Visitors are free to transform `node`.
586
- * They can also transform `parent`.
587
- *
588
- * Replacing `node` itself, if `SKIP` is not returned, still causes its
589
- * descendants to be walked (which is a bug).
590
- *
591
- * When adding or removing previous siblings of `node` (or next siblings, in
592
- * case of reverse), the `Visitor` should return a new `Index` to specify the
593
- * sibling to traverse after `node` is traversed.
594
- * Adding or removing next siblings of `node` (or previous siblings, in case
595
- * of reverse) is handled as expected without needing to return a new `Index`.
596
- *
597
- * Removing the children property of `parent` still results in them being
598
- * traversed.
599
- * @param {Visited} node
600
- * Found node.
601
- * @param {Visited extends UnistNode ? number | undefined : never} index
602
- * Index of `node` in `parent`.
603
- * @param {Ancestor extends UnistParent ? Ancestor | undefined : never} parent
604
- * Parent of `node`.
605
- * @returns {VisitorResult}
606
- * What to do next.
607
- *
608
- * An `Index` is treated as a tuple of `[CONTINUE, Index]`.
609
- * An `Action` is treated as a tuple of `[Action]`.
610
- *
611
- * Passing a tuple back only makes sense if the `Action` is `SKIP`.
612
- * When the `Action` is `EXIT`, that action can be returned.
613
- * When the `Action` is `CONTINUE`, `Index` can be returned.
614
- * @template {UnistNode} [Visited=UnistNode]
615
- * Visited node type.
616
- * @template {UnistParent} [Ancestor=UnistParent]
617
- * Ancestor type.
618
- */
619
- /**
620
- * @typedef {Visitor<Visited, Parent<Ancestor, Visited>>} BuildVisitorFromMatch
621
- * Build a typed `Visitor` function from a node and all possible parents.
622
- *
623
- * It will infer which values are passed as `node` and which as `parent`.
624
- * @template {UnistNode} Visited
625
- * Node type.
626
- * @template {UnistParent} Ancestor
627
- * Parent type.
628
- */
629
- /**
630
- * @typedef {(
631
- * BuildVisitorFromMatch<
632
- * Matches<Descendant, Check>,
633
- * Extract<Descendant, UnistParent>
634
- * >
635
- * )} BuildVisitorFromDescendants
636
- * Build a typed `Visitor` function from a list of descendants and a test.
637
- *
638
- * It will infer which values are passed as `node` and which as `parent`.
639
- * @template {UnistNode} Descendant
640
- * Node type.
641
- * @template {Test} Check
642
- * Test type.
643
- */
644
- /**
645
- * @typedef {(
646
- * BuildVisitorFromDescendants<
647
- * InclusiveDescendant<Tree>,
648
- * Check
649
- * >
650
- * )} BuildVisitor
651
- * Build a typed `Visitor` function from a tree and a test.
652
- *
653
- * It will infer which values are passed as `node` and which as `parent`.
654
- * @template {UnistNode} [Tree=UnistNode]
655
- * Node type.
656
- * @template {Test} [Check=Test]
657
- * Test type.
658
- */
659
- /**
660
- * Visit nodes.
661
- *
662
- * This algorithm performs *depth-first* *tree traversal* in *preorder*
663
- * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
664
- *
665
- * You can choose for which nodes `visitor` is called by passing a `test`.
666
- * For complex tests, you should test yourself in `visitor`, as it will be
667
- * faster and will have improved type information.
668
- *
669
- * Walking the tree is an intensive task.
670
- * Make use of the return values of the visitor when possible.
671
- * Instead of walking a tree multiple times, walk it once, use `unist-util-is`
672
- * to check if a node matches, and then perform different operations.
673
- *
674
- * You can change the tree.
675
- * See `Visitor` for more info.
676
- *
677
- * @overload
678
- * @param {Tree} tree
679
- * @param {Check} check
680
- * @param {BuildVisitor<Tree, Check>} visitor
681
- * @param {boolean | null | undefined} [reverse]
682
- * @returns {undefined}
683
- *
684
- * @overload
685
- * @param {Tree} tree
686
- * @param {BuildVisitor<Tree>} visitor
687
- * @param {boolean | null | undefined} [reverse]
688
- * @returns {undefined}
689
- *
690
- * @param {UnistNode} tree
691
- * Tree to traverse.
692
- * @param {Visitor | Test} testOrVisitor
693
- * `unist-util-is`-compatible test (optional, omit to pass a visitor).
694
- * @param {Visitor | boolean | null | undefined} [visitorOrReverse]
695
- * Handle each node (when test is omitted, pass `reverse`).
696
- * @param {boolean | null | undefined} [maybeReverse=false]
697
- * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
698
- * @returns {undefined}
699
- * Nothing.
700
- *
701
- * @template {UnistNode} Tree
702
- * Node type.
703
- * @template {Test} Check
704
- * `unist-util-is`-compatible test.
705
- */
706
- function visit(tree, testOrVisitor, visitorOrReverse, maybeReverse) {
707
- /** @type {boolean | null | undefined} */
708
- let reverse;
709
- /** @type {Test} */
710
- let test;
711
- /** @type {Visitor} */
712
- let visitor;
713
- if (typeof testOrVisitor === "function" && typeof visitorOrReverse !== "function") {
714
- test = void 0;
715
- visitor = testOrVisitor;
716
- reverse = visitorOrReverse;
717
- } else {
718
- test = testOrVisitor;
719
- visitor = visitorOrReverse;
720
- reverse = maybeReverse;
721
- }
722
- visitParents(tree, test, overload, reverse);
723
- /**
724
- * @param {UnistNode} node
725
- * @param {Array<UnistParent>} parents
726
- */
727
- function overload(node, parents) {
728
- const parent = parents[parents.length - 1];
729
- const index = parent ? parent.children.indexOf(node) : void 0;
730
- return visitor(node, index, parent);
731
- }
732
- }
733
- //#endregion
734
- export { convert as i, SKIP as n, visitParents as r, visit as t };
735
-
736
- //# sourceMappingURL=lib2.js.map