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