UNPKG

5.06 kBTypeScriptView Raw
1import type { Document } from './doc/Document.js';
2import type { Alias } from './nodes/Alias.js';
3import { Node } from './nodes/Node.js';
4import type { Pair } from './nodes/Pair.js';
5import type { Scalar } from './nodes/Scalar.js';
6import type { YAMLMap } from './nodes/YAMLMap.js';
7import type { YAMLSeq } from './nodes/YAMLSeq.js';
8export type visitorFn<T> = (key: number | 'key' | 'value' | null, node: T, path: readonly (Document | Node | Pair)[]) => void | symbol | number | Node | Pair;
9export type visitor = visitorFn<unknown> | {
10 Alias?: visitorFn<Alias>;
11 Collection?: visitorFn<YAMLMap | YAMLSeq>;
12 Map?: visitorFn<YAMLMap>;
13 Node?: visitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
14 Pair?: visitorFn<Pair>;
15 Scalar?: visitorFn<Scalar>;
16 Seq?: visitorFn<YAMLSeq>;
17 Value?: visitorFn<Scalar | YAMLMap | YAMLSeq>;
18};
19export type asyncVisitorFn<T> = (key: number | 'key' | 'value' | null, node: T, path: readonly (Document | Node | Pair)[]) => void | symbol | number | Node | Pair | Promise<void | symbol | number | Node | Pair>;
20export type asyncVisitor = asyncVisitorFn<unknown> | {
21 Alias?: asyncVisitorFn<Alias>;
22 Collection?: asyncVisitorFn<YAMLMap | YAMLSeq>;
23 Map?: asyncVisitorFn<YAMLMap>;
24 Node?: asyncVisitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
25 Pair?: asyncVisitorFn<Pair>;
26 Scalar?: asyncVisitorFn<Scalar>;
27 Seq?: asyncVisitorFn<YAMLSeq>;
28 Value?: asyncVisitorFn<Scalar | YAMLMap | YAMLSeq>;
29};
30/**
31 * Apply a visitor to an AST node or document.
32 *
33 * Walks through the tree (depth-first) starting from `node`, calling a
34 * `visitor` function with three arguments:
35 * - `key`: For sequence values and map `Pair`, the node's index in the
36 * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
37 * `null` for the root node.
38 * - `node`: The current node.
39 * - `path`: The ancestry of the current node.
40 *
41 * The return value of the visitor may be used to control the traversal:
42 * - `undefined` (default): Do nothing and continue
43 * - `visit.SKIP`: Do not visit the children of this node, continue with next
44 * sibling
45 * - `visit.BREAK`: Terminate traversal completely
46 * - `visit.REMOVE`: Remove the current node, then continue with the next one
47 * - `Node`: Replace the current node, then continue by visiting it
48 * - `number`: While iterating the items of a sequence or map, set the index
49 * of the next step. This is useful especially if the index of the current
50 * node has changed.
51 *
52 * If `visitor` is a single function, it will be called with all values
53 * encountered in the tree, including e.g. `null` values. Alternatively,
54 * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
55 * `Alias` and `Scalar` node. To define the same visitor function for more than
56 * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
57 * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
58 * specific defined one will be used for each node.
59 */
60export declare function visit(node: Node | Document | null, visitor: visitor): void;
61export declare namespace visit {
62 var BREAK: symbol;
63 var SKIP: symbol;
64 var REMOVE: symbol;
65}
66/**
67 * Apply an async visitor to an AST node or document.
68 *
69 * Walks through the tree (depth-first) starting from `node`, calling a
70 * `visitor` function with three arguments:
71 * - `key`: For sequence values and map `Pair`, the node's index in the
72 * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
73 * `null` for the root node.
74 * - `node`: The current node.
75 * - `path`: The ancestry of the current node.
76 *
77 * The return value of the visitor may be used to control the traversal:
78 * - `Promise`: Must resolve to one of the following values
79 * - `undefined` (default): Do nothing and continue
80 * - `visit.SKIP`: Do not visit the children of this node, continue with next
81 * sibling
82 * - `visit.BREAK`: Terminate traversal completely
83 * - `visit.REMOVE`: Remove the current node, then continue with the next one
84 * - `Node`: Replace the current node, then continue by visiting it
85 * - `number`: While iterating the items of a sequence or map, set the index
86 * of the next step. This is useful especially if the index of the current
87 * node has changed.
88 *
89 * If `visitor` is a single function, it will be called with all values
90 * encountered in the tree, including e.g. `null` values. Alternatively,
91 * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
92 * `Alias` and `Scalar` node. To define the same visitor function for more than
93 * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
94 * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
95 * specific defined one will be used for each node.
96 */
97export declare function visitAsync(node: Node | Document | null, visitor: asyncVisitor): Promise<void>;
98export declare namespace visitAsync {
99 var BREAK: symbol;
100 var SKIP: symbol;
101 var REMOVE: symbol;
102}