/
options.ts
318 lines (279 loc) · 7.94 KB
/
options.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
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
import type { LogLevelId } from './constants.js'
import type { Reviver } from './doc/applyReviver.js'
import type { Directives } from './doc/directives.js'
import type { Replacer } from './doc/Document.js'
import type { SchemaName } from './doc/Schema.js'
import type { Pair } from './nodes/Pair.js'
import type { Scalar } from './nodes/Scalar.js'
import type { LineCounter } from './parse/line-counter.js'
import type { CollectionTag, ScalarTag, TagValue } from './tags/types.js'
export type ParseOptions = {
/**
* Whether integers should be parsed into BigInt rather than number values.
*
* Default: `false`
*
* https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/BigInt
*/
intAsBigInt?: boolean
/**
* If set, newlines will be tracked, to allow for `lineCounter.linePos(offset)`
* to provide the `{ line, col }` positions within the input.
*/
lineCounter?: LineCounter
/**
* Include line/col position & node type directly in parse errors.
*
* Default: `true`
*/
prettyErrors?: boolean
/**
* Detect and report errors that are required by the YAML 1.2 spec,
* but are caused by unambiguous content.
*
* Default: `true`
*/
strict?: boolean
}
export type DocumentOptions = {
/**
* Default prefix for anchors.
*
* Default: `'a'`, resulting in anchors `a1`, `a2`, etc.
*/
anchorPrefix?: string
/**
* Used internally by Composer. If set and includes an explicit version,
* that overrides the `version` option.
*/
directives?: Directives
/**
* Keep `undefined` object values when creating mappings and return a Scalar
* node when calling `YAML.stringify(undefined)`, rather than `undefined`.
*
* Default: `false`
*/
keepUndefined?: boolean
/**
* Control the logging level during parsing
*
* Default: `'warn'`
*/
logLevel?: LogLevelId
/**
* The YAML version used by documents without a `%YAML` directive.
*
* Default: `"1.2"`
*/
version?: '1.1' | '1.2'
}
export type SchemaOptions = {
/**
* Array of additional tags to include in the schema, or a function that may
* modify the schema's base tag array.
*/
customTags?: TagValue[] | ((tags: TagValue[]) => TagValue[]) | null
/**
* Enable support for `<<` merge keys.
*
* Default: `false` for YAML 1.2, `true` for earlier versions
*/
merge?: boolean
/**
* When using the `'core'` schema, support parsing values with these
* explicit YAML 1.1 tags:
*
* `!!binary`, `!!omap`, `!!pairs`, `!!set`, `!!timestamp`.
*
* Default `true`
*/
resolveKnownTags?: boolean
/**
* The base schema to use.
*
* Default: `"core"` for YAML 1.2, `"yaml-1.1"` for earlier versions
*/
schema?: SchemaName
/**
* When adding to or stringifying a map, sort the entries.
* If `true`, sort by comparing key values with `<`.
*
* Default: `false`
*/
sortMapEntries?: boolean | ((a: Pair, b: Pair) => number)
}
export type CreateNodeOptions = {
/** Force the top-level collection node to use flow style. */
flow?: boolean
/**
* Keep `undefined` object values when creating mappings, rather than
* discarding them.
*
* Default: `false`
*/
keepUndefined?: boolean | null
onTagObj?: (tagObj: ScalarTag | CollectionTag) => void
/**
* Filter or modify values while creating a node.
*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
*/
replacer?: Replacer
/**
* Specify the collection type, e.g. `"!!omap"`. Note that this requires the
* corresponding tag to be available in this document's schema.
*/
tag?: string
}
export type ToJSOptions = {
/**
* 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
/**
* If defined, called with the resolved `value` and reference `count` for
* each anchor in the document.
*/
onAnchor?: (value: unknown, count: number) => void
/**
* Optional function that may filter or modify the output JS value
*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#using_the_reviver_parameter
*/
reviver?: Reviver
}
export type ToStringOptions = {
/**
* The default type of string literal used to stringify implicit key values.
* Output may use other types if required to fully represent the value.
*
* If `null`, the value of `defaultStringType` is used.
*
* Default: `null`
*/
defaultKeyType?: Scalar.Type | null
/**
* The default type of string literal used to stringify values in general.
* Output may use other types if required to fully represent the value.
*
* Default: `'PLAIN'`
*/
defaultStringType?: Scalar.Type
/**
* Include directives in the output.
*
* - If `true`, at least the document-start marker `---` is always included.
* This does not force the `%YAML` directive to be included. To do that,
* set `doc.directives.yaml.explicit = true`.
* - If `false`, no directives or marker is ever included. If using the `%TAG`
* directive, you are expected to include it manually in the stream before
* its use.
* - If `null`, directives and marker may be included if required.
*
* Default: `null`
*/
directives?: boolean | null
/**
* Restrict double-quoted strings to use JSON-compatible syntax.
*
* Default: `false`
*/
doubleQuotedAsJSON?: boolean
/**
* Minimum length for double-quoted strings to use multiple lines to
* represent the value. Ignored if `doubleQuotedAsJSON` is set.
*
* Default: `40`
*/
doubleQuotedMinMultiLineLength?: number
/**
* String representation for `false`.
* With the core schema, use `'false'`, `'False'`, or `'FALSE'`.
*
* Default: `'false'`
*/
falseStr?: string
/**
* The number of spaces to use when indenting code.
*
* Default: `2`
*/
indent?: number
/**
* Whether block sequences should be indented.
*
* Default: `true`
*/
indentSeq?: boolean
/**
* Maximum line width (set to `0` to disable folding).
*
* This is a soft limit, as only double-quoted semantics allow for inserting
* a line break in the middle of a word, as well as being influenced by the
* `minContentWidth` option.
*
* Default: `80`
*/
lineWidth?: number
/**
* Minimum line width for highly-indented content (set to `0` to disable).
*
* Default: `20`
*/
minContentWidth?: number
/**
* String representation for `null`.
* With the core schema, use `'null'`, `'Null'`, `'NULL'`, `'~'`, or an empty
* string `''`.
*
* Default: `'null'`
*/
nullStr?: string
/**
* Require keys to be scalars and to use implicit rather than explicit notation.
*
* Default: `false`
*/
simpleKeys?: boolean
/**
* Prefer 'single quote' rather than "double quote" where applicable.
*
* Default: `false`
*/
singleQuote?: boolean
/**
* String representation for `true`.
* With the core schema, use `'true'`, `'True'`, or `'TRUE'`.
*
* Default: `'true'`
*/
trueStr?: string
}
export type Options = ParseOptions & DocumentOptions & SchemaOptions
/**
* `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: Required<
Omit<ParseOptions, 'lineCounter'> & Omit<DocumentOptions, 'directives'>
> = {
anchorPrefix: 'a',
intAsBigInt: false,
keepUndefined: false,
logLevel: 'warn',
prettyErrors: true,
strict: true,
version: '1.2'
}