import { XastNode } from "./types"; export type VisitorContext = { /** * Whether the node is being processed in math mode. * * This happens when the node is a director or indirect child * of a math environment (e.g. `$abc$`), but not when an environment * re-establishes text mode (e.g. `$\text{abc}$`) */ inMathMode?: boolean; /** * Whether the node has any ancestor that is processed in math mode. */ hasMathModeAncestor?: boolean; }; type GetGuard = T extends (x: any) => x is infer R ? R : never; /** * Gets the type that a type-guard function is guarding. If * the guard type cannot be determined, the input type is returned. */ type GuardTypeOf boolean> = GetGuard extends never ? T extends (x: infer A) => any ? A : never : GetGuard; /** * Extracts the guard type from the `test` function provided in a * `VisitOptions` argument. */ type GuardFromOptions = Opts extends { test: infer R; } ? R extends (x: any) => boolean ? Extract> : PossibleTypes : PossibleTypes; /** * Narrow the type `T` based on the `VisitOptions` supplied. If `{includeArrays: false}` * is specified in the `VisitOptions`, then arrays are excluded from `T`. */ type NarrowArraysBasedOnOptions = Opts extends { includeArrays: infer A; } ? A extends true ? T : Exclude : Exclude; /** * Get the type of the parameter to the `Visitor` function based on the * `VisitOptions` that are supplied. */ type VisitorTypeFromOptions = NarrowArraysBasedOnOptions, Opts>; /** * Continue traversing as normal */ export declare const CONTINUE: unique symbol; /** * Do not traverse this node’s children */ export declare const SKIP: unique symbol; /** * Stop traversing immediately */ export declare const EXIT: unique symbol; type Action = typeof CONTINUE | typeof SKIP | typeof EXIT; type Index = number; type ActionTuple = [Action] | [typeof SKIP, Index] | [typeof CONTINUE, Index]; /** * A visitor takes a `node`, `key`, `index`, and ... * * @param key - The key of the parent that we were accessed through. */ type Visitor = (node: T, info: VisitInfo) => null | undefined | Action | Index | ActionTuple | void; type Visitors = { enter?: Visitor; leave?: Visitor; }; type VisitOptions = { startingContext?: VisitorContext; /** * Type guard for types that are passed to the `visitor` function. */ test?: (node: XastNode | XastNode[], info: VisitInfo) => boolean; /** * Whether arrays will be sent to the `visitor` function. If falsy, * only nodes will be past to `visitor`. */ includeArrays?: boolean; }; export type VisitInfo = { /** * If the element was accessed via an attribute, the attribute key is specified. */ readonly key: string | undefined; /** * If the element was accessed in an array, the index is specified. */ readonly index: number | undefined; /** * A list of ancestor nodes, `[parent, grandparent, great-grandparent, ...]` */ readonly parents: XastNode[]; /** * If the element was accessed in an array, the array that it is part of. */ readonly containingArray: XastNode[] | undefined; /** * The LaTeX context of the current match. */ readonly context: VisitorContext; }; /** * Visit children of tree which pass a test. This is an enhanced version of unified's visit utility. * * @param tree Abstract syntax tree to walk * @param [visitor] Function to run for each node */ export declare function visit(tree: XastNode | XastNode[], visitor: Visitor> | Visitors>, options?: Opts): void; export {}; //# sourceMappingURL=visit.d.ts.map