1+ /**
2+ * @typedef {import('unist').Node } Node
3+ * @typedef {import('unist').Parent } Parent
4+ * @typedef {import('mdast').Heading } Heading
5+ *
6+ * @typedef {(value: string, node: Heading) => boolean } TestFunction
7+ * @typedef {string|RegExp|TestFunction } Test
8+ *
9+ * @typedef Options
10+ * @property {Test } test
11+ * @property {boolean } [ignoreFinalDefinitions=false]
12+ *
13+ * @typedef ZoneInfo
14+ * @property {number } start
15+ * @property {number } end
16+ * @property {Parent|null } parent
17+ *
18+ * @callback Handler
19+ * @param {Heading|undefined } start
20+ * @param {Array.<Node> } between
21+ * @param {Node|undefined } end
22+ * @param {ZoneInfo } info
23+ */
24+
125import { toString } from 'mdast-util-to-string'
226
3- // Search `node` with `options` and invoke `callback`.
27+ /**
28+ * Search `node` with `options` and invoke `callback`.
29+ *
30+ * @param {Node } node
31+ * @param {Test|Options } options
32+ * @param {Handler } handler
33+ */
434// eslint-disable-next-line complexity
5- export function headingRange ( node , options , callback ) {
35+ export function headingRange ( node , options , handler ) {
636 var test = options
7- var children = node . children
37+ /** @type {Array.<Node> } */
38+ // @ts -ignore looks like children.
39+ var children = node . children || [ ]
840 var index = - 1
41+ /** @type {boolean } */
942 var ignoreFinalDefinitions
43+ /** @type {number } */
1044 var depth
45+ /** @type {number } */
1146 var start
47+ /** @type {number } */
1248 var end
49+ /** @type {Array.<Node> } */
1350 var nodes
51+ /** @type {Array.<Node> } */
1452 var result
53+ /** @type {Node } */
1554 var child
1655
1756 // Object, not regex.
@@ -48,7 +87,9 @@ export function headingRange(node, options, callback) {
4887 break
4988 }
5089
90+ // @ts -ignore looks like a heading.
5191 if ( ! depth && test ( toString ( child ) , child ) ) {
92+ // @ts -ignore looks like a heading.
5293 depth = child . depth
5394 start = index
5495 // Assume no end heading is found.
@@ -68,7 +109,8 @@ export function headingRange(node, options, callback) {
68109 }
69110 }
70111
71- nodes = callback (
112+ nodes = handler (
113+ // @ts -ignore `start` points to a heading.
72114 children [ start ] ,
73115 children . slice ( start + 1 , end ) ,
74116 children [ end ] ,
@@ -94,11 +136,19 @@ export function headingRange(node, options, callback) {
94136 }
95137}
96138
97- // Wrap an expression into an assertion function.
139+ /**
140+ * Wrap an expression into an assertion function.
141+ * @param {RegExp } expression
142+ * @returns {(value: string) => boolean }
143+ */
98144function wrapExpression ( expression ) {
99145 return assertion
100146
101- // Assert `value` matches the bound `expression`.
147+ /**
148+ * Assert `value` matches the bound `expression`.
149+ * @param {string } value
150+ * @returns {boolean }
151+ */
102152 function assertion ( value ) {
103153 return expression . test ( value )
104154 }
0 commit comments