Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
*   Add more docs to JSDoc
*   Add support for `null` in input of API types
  • Loading branch information
wooorm committed Jan 24, 2023
1 parent 9f4813b commit 754d038
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
3 changes: 1 addition & 2 deletions index.d.ts
Expand Up @@ -5,6 +5,5 @@ export type {
Index,
VisitorResult
} from 'unist-util-visit-parents'
export {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents'
export type {Visitor, BuildVisitor} from './lib/index.js'
export {visit} from './lib/index.js'
export {CONTINUE, EXIT, SKIP, visit} from './lib/index.js'
3 changes: 1 addition & 2 deletions index.js
@@ -1,3 +1,2 @@
// Note: types exported from `index.d.ts`
export {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents'
export {visit} from './lib/index.js'
export {CONTINUE, EXIT, SKIP, visit} from './lib/index.js'
34 changes: 26 additions & 8 deletions lib/index.js
Expand Up @@ -112,30 +112,48 @@
import {visitParents} from 'unist-util-visit-parents'

/**
* Visit children of tree which pass test.
* Visit nodes.
*
* This algorithm performs *depth-first* *tree traversal* in *preorder*
* (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
*
* You can choose for which nodes `visitor` is called by passing a `test`.
* For complex tests, you should test yourself in `visitor`, as it will be
* faster and will have improved type information.
*
* Walking the tree is an intensive task.
* Make use of the return values of the visitor when possible.
* Instead of walking a tree multiple times, walk it once, use `unist-util-is`
* to check if a node matches, and then perform different operations.
*
* You can change the tree.
* See `Visitor` for more info.
*
* @param tree
* Tree to walk
* @param [test]
* Tree to traverse.
* @param test
* `unist-util-is`-compatible test
* @param visitor
* Function called for nodes that pass `test`.
* Handle each node.
* @param reverse
* Traverse in reverse preorder (NRL) instead of preorder (NLR) (default).
* Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
* @returns
* Nothing.
*/
export const visit =
/**
* @type {(
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean) => void) &
* (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean) => void)
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined) => void) &
* (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined) => void)
* )}
*/
(
/**
* @param {Node} tree
* @param {Test} test
* @param {Visitor} visitor
* @param {boolean} [reverse]
* @param {boolean | null | undefined} [reverse]
* @returns {void}
*/
function (tree, test, visitor, reverse) {
if (typeof test === 'function' && typeof visitor !== 'function') {
Expand Down

0 comments on commit 754d038

Please sign in to comment.