Skip to content

Commit

Permalink
Change to pass undefined, not null
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 6, 2023
1 parent 07a2fbe commit 12c9ee9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
18 changes: 10 additions & 8 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ visit(sampleTree)
// ## No test
visit(sampleTree, function (node, index, parent) {
expectType<Nodes>(node)
expectType<number | null>(index)
expectType<Parents | null>(parent)
expectType<number | undefined>(index)
expectType<Parents | undefined>(parent)
})

visit(implicitTree, function (node, index, parent) {
// Objects are too loose.
expectAssignable<Node>(node)
expectNotType<Node>(node)
expectType<number | null>(index)
expectType<number | undefined>(index)
expectType<never>(parent)
})

Expand All @@ -63,8 +63,10 @@ visit(implicitTree, function (node, index, parent) {
// Knows it’s a heading and its parents.
visit(sampleTree, 'heading', function (node, index, parent) {
expectType<Heading>(node)
expectType<number | null>(index)
expectType<Blockquote | FootnoteDefinition | ListItem | Root | null>(parent)
expectType<number | undefined>(index)
expectType<
Blockquote | FootnoteDefinition | ListItem | Root | undefined
>(parent)
})

// Not in tree.
Expand All @@ -83,8 +85,8 @@ visit(implicitTree, 'heading', function (node, index, parent) {

visit(sampleTree, 'tableCell', function (node, index, parent) {
expectType<TableCell>(node)
expectType<number | null>(index)
expectType<Root | TableRow | null>(parent)
expectType<number | undefined>(index)
expectType<Root | TableRow | undefined>(parent)
})

// ## Props test
Expand Down Expand Up @@ -203,7 +205,7 @@ visit(sampleTree, 'tableCell', function (node) {
| LinkReference
| Strong
| TableCell
| null
| undefined
>(parent)
})
})
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
* traversed.
* @param {Visited} node
* Found node.
* @param {Visited extends UnistNode ? number | null : never} index
* @param {Visited extends UnistNode ? number | undefined : never} index
* Index of `node` in `parent`.
* @param {Ancestor extends UnistNode ? Ancestor | null : never} parent
* @param {Ancestor extends UnistNode ? Ancestor | undefined : never} parent
* Parent of `node`.
* @returns {VisitorResult}
* What to do next.
Expand Down Expand Up @@ -187,7 +187,7 @@ export function visit(tree, testOrVisitor, visitorOrReverse, maybeReverse) {
*/
function overload(node, parents) {
const parent = parents[parents.length - 1]
const index = parent ? parent.children.indexOf(node) : null
const index = parent ? parent.children.indexOf(node) : undefined
return visitor(node, index, parent)
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ traversed.

* `node` ([`Node`][node])
— found node
* `index` (`number` or `null`)
* `index` (`number` or `undefined`)
— index of `node` in `parent`
* `parent` ([`Node`][node] or `null`)
* `parent` ([`Node`][node] or `undefined`)
— parent of `node`

###### Returns
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ test('visit', async function (t) {

assert.equal(n, 3, 'should visit all passing nodes')

// To do: when `unist-util-is` updates, we can drop `null`.
/**
* @param {Node} _
* @param {number | null | undefined} index
Expand Down

0 comments on commit 12c9ee9

Please sign in to comment.