UNPKG

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