mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-04 18:29:30 +08:00
57 lines
1.7 KiB
Text
57 lines
1.7 KiB
Text
/**
|
|
* @typedef {import('unist').Node} Node
|
|
* @typedef {import('unist').Parent} Parent
|
|
* @typedef {import('unist-util-is').Test} Test
|
|
* @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult
|
|
* @typedef {import('./complex-types').Visitor} Visitor
|
|
*/
|
|
|
|
import {visitParents, CONTINUE, SKIP, EXIT} from 'unist-util-visit-parents'
|
|
|
|
export {CONTINUE, SKIP, EXIT}
|
|
|
|
/**
|
|
* Visit children of tree which pass a test
|
|
*
|
|
* @param tree Abstract syntax tree to walk
|
|
* @param test Test, optional
|
|
* @param visitor Function to run for each node
|
|
* @param reverse Fisit the tree in reverse, defaults to false
|
|
*/
|
|
export const visit =
|
|
/**
|
|
* @type {(
|
|
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: import('./complex-types').BuildVisitor<Tree, Check>, reverse?: boolean) => void) &
|
|
* (<Tree extends Node>(tree: Tree, visitor: import('./complex-types').BuildVisitor<Tree>, reverse?: boolean) => void)
|
|
* )}
|
|
*/
|
|
(
|
|
/**
|
|
* @param {Node} tree
|
|
* @param {Test} test
|
|
* @param {import('./complex-types').Visitor} visitor
|
|
* @param {boolean} [reverse]
|
|
*/
|
|
function (tree, test, visitor, reverse) {
|
|
if (typeof test === 'function' && typeof visitor !== 'function') {
|
|
reverse = visitor
|
|
visitor = test
|
|
test = null
|
|
}
|
|
|
|
visitParents(tree, test, overload, reverse)
|
|
|
|
/**
|
|
* @param {Node} node
|
|
* @param {Array.<Parent>} parents
|
|
*/
|
|
function overload(node, parents) {
|
|
const parent = parents[parents.length - 1]
|
|
return visitor(
|
|
node,
|
|
parent ? parent.children.indexOf(node) : null,
|
|
parent
|
|
)
|
|
}
|
|
}
|
|
)
|