unist-util-assert
Version:
unist utility to assert nodes
101 lines (100 loc) • 2.73 kB
TypeScript
/**
* Assert that `tree` is a valid unist node.
*
* If `tree` is a parent, all children will be asserted too.
*
* @param {unknown} [tree]
* Thing to assert.
* @param {Parent | null | undefined} [parent]
* Optional, valid parent.
* @returns {asserts tree is Node}
* Nothing.
* @throws {AssertionError}
* When `tree` (or its descendants) is not a node.
*/
export function assert(
tree?: unknown,
parent?: Parent | null | undefined
): asserts tree is import('unist').Node
/**
* Assert that `tree` is a valid unist parent.
*
* All children will be asserted too.
*
* @param {unknown} [tree]
* Thing to assert.
* @param {Parent | null | undefined} [parent]
* Optional, valid parent.
* @returns {asserts tree is Parent}
* Nothing.
* @throws {AssertionError}
* When `tree` is not a parent or its descendants are not nodes.
*/
export function parent(
tree?: unknown,
parent?: Parent | null | undefined
): asserts tree is import('unist').Parent
/**
* Assert that `node` is a valid unist literal.
*
* @param {unknown} [node]
* Thing to assert.
* @param {Parent | null | undefined} [parent]
* Optional, valid parent.
* @returns {asserts node is Literal}
* Nothing.
* @throws {AssertionError}
* When `node` is not a literal.
*/
export function literal(
node?: unknown,
parent?: Parent | null | undefined
): asserts node is import('unist').Literal
/**
* Assert that `node` is a valid void node.
*
* @param {unknown} [node]
* Thing to assert.
* @param {Parent | null | undefined} [parent]
* Optional, valid parent.
* @returns {asserts node is _Void}
* Nothing.
* @throws {AssertionError}
* When `node` is not a node, a parent, or a literal.
*/
export function _void(
node?: unknown,
parent?: Parent | null | undefined
): asserts node is _Void
/**
* Wrapper that adds the current node (and parent, if available) to error
* messages.
*
* @template {Node} T
* Node type.
* @param {CustomAssertion<T>} fn
* Custom assertion.
* @returns {CustomAssertion<T>}
* Assertion.
*/
export function wrap<T extends import('unist').Node>(
fn: CustomAssertion<T>
): CustomAssertion<T>
export type AssertionError = import('node:assert').AssertionError
export type Literal = import('unist').Literal
export type Node = import('unist').Node
export type Parent = import('unist').Parent
export type Point = import('unist').Point
export type Position = import('unist').Position
export type CustomAssertion<T> = (
node?: any,
parent?: Parent | null | undefined
) => asserts node is T
export type _Void = Node & {
children: never
value: never
}
export type SeenErrorFields = {
__unist__?: true
}
export type SeenError = Error & SeenErrorFields