Skip to content

Commit d9ddc7f

Browse files
committed
Change types to use mdast types
1 parent c4a7d78 commit d9ddc7f

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @typedef {import('unist').Node} Node
32
* @typedef {import('unist').Parent} Parent
3+
* @typedef {import('mdast').Root|import('mdast').Content} Node
44
* @typedef {import('mdast').Heading} Heading
55
*
66
* @typedef {(value: string, node: Heading) => boolean} TestFunction
@@ -35,8 +35,7 @@ import {toString} from 'mdast-util-to-string'
3535
export function headingRange(node, options, handler) {
3636
let test = options
3737
/** @type {Array.<Node>} */
38-
// @ts-ignore looks like children.
39-
const children = node.children || []
38+
const children = 'children' in node ? node.children : []
4039
/** @type {boolean|undefined} */
4140
let ignoreFinalDefinitions
4241

@@ -77,15 +76,12 @@ export function headingRange(node, options, handler) {
7776
const child = children[index]
7877

7978
if (child.type === 'heading') {
80-
// @ts-expect-error: looks like a heading.
8179
if (depth && child.depth <= depth) {
8280
end = index
8381
break
8482
}
8583

86-
// @ts-ignore looks like a heading.
8784
if (!depth && test(toString(child), child)) {
88-
// @ts-ignore looks like a heading.
8985
depth = child.depth
9086
start = index
9187
// Assume no end heading is found.
@@ -107,7 +103,7 @@ export function headingRange(node, options, handler) {
107103

108104
/** @type {Array.<Node>} */
109105
const nodes = handler(
110-
// @ts-ignore `start` points to a heading.
106+
// @ts-expect-error `start` points to a heading.
111107
children[start],
112108
children.slice(start + 1, end),
113109
children[end],

test.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @typedef {import('tape').Test} Test
3-
* @typedef {import('unist').Node} Node
43
* @typedef {import('mdast').Root} Root
4+
* @typedef {Root|import('mdast').Content} Node
55
* @typedef {import('./index.js').Test | import('./index.js').Options} Options
66
*/
77

@@ -141,7 +141,8 @@ test('mdast-util-heading-range()', (t) => {
141141
remark()
142142
.use(() => {
143143
return function (node) {
144-
headingRange(node, 'foo', () => {
144+
const root = /** @type {Root} */ (node)
145+
headingRange(root, 'foo', () => {
145146
return null
146147
})
147148
}
@@ -159,7 +160,8 @@ test('mdast-util-heading-range()', (t) => {
159160
remark()
160161
.use(() => {
161162
return function (node) {
162-
headingRange(node, 'foo', () => {
163+
const root = /** @type {Root} */ (node)
164+
headingRange(root, 'foo', () => {
163165
return []
164166
})
165167
}
@@ -177,7 +179,8 @@ test('mdast-util-heading-range()', (t) => {
177179
remark()
178180
.use(() => {
179181
return function (node) {
180-
headingRange(node, 'foo', (start, _, end) => {
182+
const root = /** @type {Root} */ (node)
183+
headingRange(root, 'foo', (start, _, end) => {
181184
return [start, {type: 'thematicBreak'}, end]
182185
})
183186
}
@@ -198,7 +201,8 @@ test('mdast-util-heading-range()', (t) => {
198201
remark()
199202
.use(() => {
200203
return function (node) {
201-
headingRange(node, 'foo', (start, nodes, end) => {
204+
const root = /** @type {Root} */ (node)
205+
headingRange(root, 'foo', (start, nodes, end) => {
202206
t.equal(nodes.length, 3)
203207
return [start, ...nodes, end]
204208
})
@@ -327,7 +331,8 @@ function process(t, value, options) {
327331
return remark()
328332
.use(() => {
329333
return function (node) {
330-
headingRange(node, options, (start, _, end, scope) => {
334+
const root = /** @type {Root} */ (node)
335+
headingRange(root, options, (start, _, end, scope) => {
331336
t.equal(typeof scope.start, 'number')
332337
t.assert(typeof scope.end === 'number' || scope.end === null)
333338
t.equal(scope.parent && scope.parent.type, 'root')

0 commit comments

Comments
 (0)