index.d.ts
10.5 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { CST } from './parse-cst'
import {
AST,
Alias,
Collection,
Merge,
Node,
Scalar,
Schema,
YAMLMap,
YAMLSeq
} from './types'
import { Type, YAMLError, YAMLWarning } from './util'
export { AST, CST }
export { default as parseCST } from './parse-cst'
/**
* `yaml` defines document-specific options in three places: as an argument of
* parse, create and stringify calls, in the values of `YAML.defaultOptions`,
* and in the version-dependent `YAML.Document.defaults` object. Values set in
* `YAML.defaultOptions` override version-dependent defaults, and argument
* options override both.
*/
export const defaultOptions: Options
export interface Options extends Schema.Options {
/**
* Default prefix for anchors.
*
* Default: `'a'`, resulting in anchors `a1`, `a2`, etc.
*/
anchorPrefix?: string
/**
* The number of spaces to use when indenting code.
*
* Default: `2`
*/
indent?: number
/**
* Whether block sequences should be indented.
*
* Default: `true`
*/
indentSeq?: boolean
/**
* Allow non-JSON JavaScript objects to remain in the `toJSON` output.
* Relevant with the YAML 1.1 `!!timestamp` and `!!binary` tags as well as BigInts.
*
* Default: `true`
*/
keepBlobsInJSON?: boolean
/**
* Include references in the AST to each node's corresponding CST node.
*
* Default: `false`
*/
keepCstNodes?: boolean
/**
* Store the original node type when parsing documents.
*
* Default: `true`
*/
keepNodeTypes?: boolean
/**
* When outputting JS, use Map rather than Object to represent mappings.
*
* Default: `false`
*/
mapAsMap?: boolean
/**
* Prevent exponential entity expansion attacks by limiting data aliasing count;
* set to `-1` to disable checks; `0` disallows all alias nodes.
*
* Default: `100`
*/
maxAliasCount?: number
/**
* Include line position & node type directly in errors; drop their verbose source and context.
*
* Default: `false`
*/
prettyErrors?: boolean
/**
* When stringifying, require keys to be scalars and to use implicit rather than explicit notation.
*
* Default: `false`
*/
simpleKeys?: boolean
/**
* The YAML version used by documents without a `%YAML` directive.
*
* Default: `"1.2"`
*/
version?: '1.0' | '1.1' | '1.2'
}
/**
* Some customization options are availabe to control the parsing and
* stringification of scalars. Note that these values are used by all documents.
*/
export const scalarOptions: {
binary: scalarOptions.Binary
bool: scalarOptions.Bool
int: scalarOptions.Int
null: scalarOptions.Null
str: scalarOptions.Str
}
export namespace scalarOptions {
interface Binary {
/**
* The type of string literal used to stringify `!!binary` values.
*
* Default: `'BLOCK_LITERAL'`
*/
defaultType: Scalar.Type
/**
* Maximum line width for `!!binary`.
*
* Default: `76`
*/
lineWidth: number
}
interface Bool {
/**
* String representation for `true`. With the core schema, use `'true' | 'True' | 'TRUE'`.
*
* Default: `'true'`
*/
trueStr: string
/**
* String representation for `false`. With the core schema, use `'false' | 'False' | 'FALSE'`.
*
* Default: `'false'`
*/
falseStr: string
}
interface Int {
/**
* Whether integers should be parsed into BigInt values.
*
* Default: `false`
*/
asBigInt: boolean
}
interface Null {
/**
* String representation for `null`. With the core schema, use `'null' | 'Null' | 'NULL' | '~' | ''`.
*
* Default: `'null'`
*/
nullStr: string
}
interface Str {
/**
* The default type of string literal used to stringify values
*
* Default: `'PLAIN'`
*/
defaultType: Scalar.Type
doubleQuoted: {
/**
* Whether to restrict double-quoted strings to use JSON-compatible syntax.
*
* Default: `false`
*/
jsonEncoding: boolean
/**
* Minimum length to use multiple lines to represent the value.
*
* Default: `40`
*/
minMultiLineLength: number
}
fold: {
/**
* Maximum line width (set to `0` to disable folding).
*
* Default: `80`
*/
lineWidth: number
/**
* Minimum width for highly-indented content.
*
* Default: `20`
*/
minContentWidth: number
}
}
}
export class Document extends Collection {
cstNode?: CST.Document
constructor(options?: Options)
tag: never
directivesEndMarker?: boolean
type: Type.DOCUMENT
/**
* Anchors associated with the document's nodes;
* also provides alias & merge node creators.
*/
anchors: Document.Anchors
/** The document contents. */
contents: any
/** Errors encountered during parsing. */
errors: YAMLError[]
/**
* The schema used with the document. Use `setSchema()` to change or
* initialise.
*/
schema?: Schema
/**
* Array of prefixes; each will have a string `handle` that
* starts and ends with `!` and a string `prefix` that the handle will be replaced by.
*/
tagPrefixes: Document.TagPrefix[]
/**
* The parsed version of the source document;
* if true-ish, stringified output will include a `%YAML` directive.
*/
version?: string
/** Warnings encountered during parsing. */
warnings: YAMLWarning[]
/**
* List the tags used in the document that are not in the default
* `tag:yaml.org,2002:` namespace.
*/
listNonDefaultTags(): string[]
/** Parse a CST into this document */
parse(cst: CST.Document): this
/**
* When a document is created with `new YAML.Document()`, the schema object is
* not set as it may be influenced by parsed directives; call this with no
* arguments to set it manually, or with arguments to change the schema used
* by the document.
**/
setSchema(
id?: Options['version'] | Schema.Name,
customTags?: (Schema.TagId | Schema.Tag)[]
): void
/** Set `handle` as a shorthand string for the `prefix` tag namespace. */
setTagPrefix(handle: string, prefix: string): void
/**
* A plain JavaScript representation of the document `contents`.
*
* @param arg Used by `JSON.stringify` to indicate the array index or property
* name. If its value is a `string` and the document `contents` has a scalar
* value, the `keepBlobsInJSON` option has no effect.
* @param onAnchor If defined, called with the resolved `value` and reference
* `count` for each anchor in the document.
* */
toJSON(arg?: string, onAnchor?: (value: any, count: number) => void): any
/** A YAML representation of the document. */
toString(): string
}
export namespace Document {
interface Parsed extends Document {
contents: Scalar | YAMLMap | YAMLSeq | null
/** The schema used with the document. */
schema: Schema
}
interface Anchors {
/**
* Create a new `Alias` node, adding the required anchor for `node`.
* If `name` is empty, a new anchor name will be generated.
*/
createAlias(node: Node, name?: string): Alias
/**
* Create a new `Merge` node with the given source nodes.
* Non-`Alias` sources will be automatically wrapped.
*/
createMergePair(...nodes: Node[]): Merge
/** The anchor name associated with `node`, if set. */
getName(node: Node): undefined | string
/** List of all defined anchor names. */
getNames(): string[]
/** The node associated with the anchor `name`, if set. */
getNode(name: string): undefined | Node
/**
* Find an available anchor name with the given `prefix` and a
* numerical suffix.
*/
newName(prefix: string): string
/**
* Associate an anchor with `node`. If `name` is empty, a new name will be generated.
* To remove an anchor, use `setAnchor(null, name)`.
*/
setAnchor(node: Node | null, name?: string): void | string
}
interface TagPrefix {
handle: string
prefix: string
}
}
/**
* Recursively turns objects into collections. Generic objects as well as `Map`
* and its descendants become mappings, while arrays and other iterable objects
* result in sequences.
*
* The primary purpose of this function is to enable attaching comments or other
* metadata to a value, or to otherwise exert more fine-grained control over the
* stringified output. To that end, you'll need to assign its return value to
* the `contents` of a Document (or somewhere within said contents), as the
* document's schema is required for YAML string output.
*
* @param wrapScalars If undefined or `true`, also wraps plain values in
* `Scalar` objects; if `false` and `value` is not an object, it will be
* returned directly.
* @param tag Use to specify the collection type, e.g. `"!!omap"`. Note that
* this requires the corresponding tag to be available based on the default
* options. To use a specific document's schema, use `doc.schema.createNode`.
*/
export function createNode(
value: any,
wrapScalars?: true,
tag?: string
): YAMLMap | YAMLSeq | Scalar
/**
* YAML.createNode recursively turns objects into Map and arrays to Seq collections.
* Its primary use is to enable attaching comments or other metadata to a value,
* or to otherwise exert more fine-grained control over the stringified output.
*
* Doesn't wrap plain values in Scalar objects.
*/
export function createNode(
value: any,
wrapScalars: false,
tag?: string
): YAMLMap | YAMLSeq | string | number | boolean | null
/**
* Parse an input string into a single YAML.Document.
*/
export function parseDocument(str: string, options?: Options): Document.Parsed
/**
* Parse the input as a stream of YAML documents.
*
* Documents should be separated from each other by `...` or `---` marker lines.
*/
export function parseAllDocuments(
str: string,
options?: Options
): Document.Parsed[]
/**
* Parse an input string into JavaScript.
*
* Only supports input consisting of a single YAML document; for multi-document
* support you should use `YAML.parseAllDocuments`. May throw on error, and may
* log warnings using `console.warn`.
*
* @param str A string with YAML formatting.
* @returns The value will match the type of the root value of the parsed YAML
* document, so Maps become objects, Sequences arrays, and scalars result in
* nulls, booleans, numbers and strings.
*/
export function parse(str: string, options?: Options): any
/**
* @returns Will always include \n as the last character, as is expected of YAML documents.
*/
export function stringify(value: any, options?: Options): string