UNPKG

3.7 kBTypeScriptView Raw
1import type { CreateNodeContext } from '../doc/createNode.js';
2import type { Node } from '../nodes/Node.js';
3import type { Scalar } from '../nodes/Scalar.js';
4import type { YAMLMap } from '../nodes/YAMLMap.js';
5import type { YAMLSeq } from '../nodes/YAMLSeq.js';
6import type { ParseOptions } from '../options.js';
7import type { StringifyContext } from '../stringify/stringify.js';
8import type { Schema } from './Schema.js';
9interface TagBase {
10 /**
11 * An optional factory function, used e.g. by collections when wrapping JS objects as AST nodes.
12 */
13 createNode?: (schema: Schema, value: unknown, ctx: CreateNodeContext) => Node;
14 /**
15 * If `true`, together with `test` allows for values to be stringified without
16 * an explicit tag. For most cases, it's unlikely that you'll actually want to
17 * use this, even if you first think you do.
18 */
19 default?: boolean;
20 /**
21 * If a tag has multiple forms that should be parsed and/or stringified
22 * differently, use `format` to identify them.
23 */
24 format?: string;
25 /**
26 * Used by `YAML.createNode` to detect your data type, e.g. using `typeof` or
27 * `instanceof`.
28 */
29 identify?: (value: unknown) => boolean;
30 /**
31 * The identifier for your data type, with which its stringified form will be
32 * prefixed. Should either be a !-prefixed local `!tag`, or a fully qualified
33 * `tag:domain,date:foo`.
34 */
35 tag: string;
36}
37export interface ScalarTag extends TagBase {
38 collection?: never;
39 nodeClass?: never;
40 /**
41 * Turns a value into an AST node.
42 * If returning a non-`Node` value, the output will be wrapped as a `Scalar`.
43 */
44 resolve(value: string, onError: (message: string) => void, options: ParseOptions): unknown;
45 /**
46 * Optional function stringifying a Scalar node. If your data includes a
47 * suitable `.toString()` method, you can probably leave this undefined and
48 * use the default stringifier.
49 *
50 * @param item The node being stringified.
51 * @param ctx Contains the stringifying context variables.
52 * @param onComment Callback to signal that the stringifier includes the
53 * item's comment in its output.
54 * @param onChompKeep Callback to signal that the output uses a block scalar
55 * type with the `+` chomping indicator.
56 */
57 stringify?: (item: Scalar, ctx: StringifyContext, onComment?: () => void, onChompKeep?: () => void) => string;
58 /**
59 * Together with `default` allows for values to be stringified without an
60 * explicit tag and detected using a regular expression. For most cases, it's
61 * unlikely that you'll actually want to use these, even if you first think
62 * you do.
63 */
64 test?: RegExp;
65}
66export interface CollectionTag extends TagBase {
67 stringify?: never;
68 test?: never;
69 /** The source collection type supported by this tag. */
70 collection: 'map' | 'seq';
71 /**
72 * The `Node` child class that implements this tag.
73 * If set, used to select this tag when stringifying.
74 *
75 * If the class provides a static `from` method, then that
76 * will be used if the tag object doesn't have a `createNode` method.
77 */
78 nodeClass?: {
79 new (schema?: Schema): Node;
80 from?: (schema: Schema, obj: unknown, ctx: CreateNodeContext) => Node;
81 };
82 /**
83 * Turns a value into an AST node.
84 * If returning a non-`Node` value, the output will be wrapped as a `Scalar`.
85 *
86 * Note: this is required if nodeClass is not provided.
87 */
88 resolve?: (value: YAMLMap.Parsed | YAMLSeq.Parsed, onError: (message: string) => void, options: ParseOptions) => unknown;
89}
90export {};