Skip to content

Commit 754d038

Browse files
committedJan 24, 2023
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types
1 parent 9f4813b commit 754d038

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed
 

‎index.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ export type {
55
Index,
66
VisitorResult
77
} from 'unist-util-visit-parents'
8-
export {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents'
98
export type {Visitor, BuildVisitor} from './lib/index.js'
10-
export {visit} from './lib/index.js'
9+
export {CONTINUE, EXIT, SKIP, visit} from './lib/index.js'

‎index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
// Note: types exported from `index.d.ts`
2-
export {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents'
3-
export {visit} from './lib/index.js'
2+
export {CONTINUE, EXIT, SKIP, visit} from './lib/index.js'

‎lib/index.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -112,30 +112,48 @@
112112
import {visitParents} from 'unist-util-visit-parents'
113113

114114
/**
115-
* Visit children of tree which pass test.
115+
* Visit nodes.
116+
*
117+
* This algorithm performs *depth-first* *tree traversal* in *preorder*
118+
* (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
119+
*
120+
* You can choose for which nodes `visitor` is called by passing a `test`.
121+
* For complex tests, you should test yourself in `visitor`, as it will be
122+
* faster and will have improved type information.
123+
*
124+
* Walking the tree is an intensive task.
125+
* Make use of the return values of the visitor when possible.
126+
* Instead of walking a tree multiple times, walk it once, use `unist-util-is`
127+
* to check if a node matches, and then perform different operations.
128+
*
129+
* You can change the tree.
130+
* See `Visitor` for more info.
116131
*
117132
* @param tree
118-
* Tree to walk
119-
* @param [test]
133+
* Tree to traverse.
134+
* @param test
120135
* `unist-util-is`-compatible test
121136
* @param visitor
122-
* Function called for nodes that pass `test`.
137+
* Handle each node.
123138
* @param reverse
124-
* Traverse in reverse preorder (NRL) instead of preorder (NLR) (default).
139+
* Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
140+
* @returns
141+
* Nothing.
125142
*/
126143
export const visit =
127144
/**
128145
* @type {(
129-
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean) => void) &
130-
* (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean) => void)
146+
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined) => void) &
147+
* (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined) => void)
131148
* )}
132149
*/
133150
(
134151
/**
135152
* @param {Node} tree
136153
* @param {Test} test
137154
* @param {Visitor} visitor
138-
* @param {boolean} [reverse]
155+
* @param {boolean | null | undefined} [reverse]
156+
* @returns {void}
139157
*/
140158
function (tree, test, visitor, reverse) {
141159
if (typeof test === 'function' && typeof visitor !== 'function') {

0 commit comments

Comments
 (0)
Please sign in to comment.