-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Document.js
357 lines (332 loc) · 10.5 KB
/
Document.js
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
import {
Alias,
Collection,
Node,
Pair,
Scalar,
collectionFromPath,
isEmptyPath,
toJS
} from '../ast/index.js'
import { Document as CSTDocument } from '../cst/Document.js'
import { defaultTagPrefix } from '../constants.js'
import { YAMLError } from '../errors.js'
import { defaultOptions, documentOptions } from '../options.js'
import { addComment } from '../stringify/addComment.js'
import { stringify } from '../stringify/stringify.js'
import { Anchors } from './Anchors.js'
import { Schema } from './Schema.js'
import { applyReviver } from './applyReviver.js'
import { createNode } from './createNode.js'
import { listTagNames } from './listTagNames.js'
import { parseContents } from './parseContents.js'
import { parseDirectives } from './parseDirectives.js'
function assertCollection(contents) {
if (contents instanceof Collection) return true
throw new Error('Expected a YAML collection as document contents')
}
export class Document {
static defaults = documentOptions
constructor(value, replacer, options) {
if (
options === undefined &&
replacer &&
typeof replacer === 'object' &&
!Array.isArray(replacer)
) {
options = replacer
replacer = undefined
}
this.options = Object.assign({}, defaultOptions, options)
this.anchors = new Anchors(this.options.anchorPrefix)
this.commentBefore = null
this.comment = null
this.directivesEndMarker = null
this.errors = []
this.schema = null
this.tagPrefixes = []
this.version = null
this.warnings = []
if (value === undefined) {
// note that this.schema is left as null here
this.contents = null
} else if (value instanceof CSTDocument) {
this.parse(value)
} else {
this.contents = this.createNode(value, { replacer })
}
}
add(value) {
assertCollection(this.contents)
return this.contents.add(value)
}
addIn(path, value) {
assertCollection(this.contents)
this.contents.addIn(path, value)
}
createNode(
value,
{ keepUndefined, onTagObj, replacer, tag, wrapScalars } = {}
) {
this.setSchema()
if (typeof replacer === 'function')
value = replacer.call({ '': value }, '', value)
else if (Array.isArray(replacer)) {
const keyToStr = v =>
typeof v === 'number' || v instanceof String || v instanceof Number
const asStr = replacer.filter(keyToStr).map(String)
if (asStr.length > 0) replacer = replacer.concat(asStr)
}
if (typeof keepUndefined !== 'boolean')
keepUndefined = !!this.options.keepUndefined
const aliasNodes = []
const ctx = {
keepUndefined,
onAlias(source) {
const alias = new Alias(source)
aliasNodes.push(alias)
return alias
},
onTagObj,
prevObjects: new Map(),
replacer,
schema: this.schema,
wrapScalars: wrapScalars !== false
}
const node = createNode(value, tag, ctx)
for (const alias of aliasNodes) {
// With circular references, the source node is only resolved after all of
// its child nodes are. This is why anchors are set only after all of the
// nodes have been created.
alias.source = alias.source.node
let name = this.anchors.getName(alias.source)
if (!name) {
name = this.anchors.newName()
this.anchors.map[name] = alias.source
}
}
return node
}
createPair(key, value, options = {}) {
const k = this.createNode(key, options)
const v = this.createNode(value, options)
return new Pair(k, v)
}
delete(key) {
assertCollection(this.contents)
return this.contents.delete(key)
}
deleteIn(path) {
if (isEmptyPath(path)) {
if (this.contents == null) return false
this.contents = null
return true
}
assertCollection(this.contents)
return this.contents.deleteIn(path)
}
getDefaults() {
return (
Document.defaults[this.version] ||
Document.defaults[this.options.version] ||
{}
)
}
get(key, keepScalar) {
return this.contents instanceof Collection
? this.contents.get(key, keepScalar)
: undefined
}
getIn(path, keepScalar) {
if (isEmptyPath(path))
return !keepScalar && this.contents instanceof Scalar
? this.contents.value
: this.contents
return this.contents instanceof Collection
? this.contents.getIn(path, keepScalar)
: undefined
}
has(key) {
return this.contents instanceof Collection ? this.contents.has(key) : false
}
hasIn(path) {
if (isEmptyPath(path)) return this.contents !== undefined
return this.contents instanceof Collection
? this.contents.hasIn(path)
: false
}
set(key, value) {
if (this.contents == null) {
this.setSchema()
this.contents = collectionFromPath(this.schema, [key], value)
} else {
assertCollection(this.contents)
this.contents.set(key, value)
}
}
setIn(path, value) {
if (isEmptyPath(path)) this.contents = value
else if (this.contents == null) {
this.setSchema()
this.contents = collectionFromPath(this.schema, path, value)
} else {
assertCollection(this.contents)
this.contents.setIn(path, value)
}
}
setSchema(id, customTags) {
if (!id && !customTags && this.schema) return
if (typeof id === 'number') id = id.toFixed(1)
if (id === '1.0' || id === '1.1' || id === '1.2') {
if (this.version) this.version = id
else this.options.version = id
delete this.options.schema
} else if (id && typeof id === 'string') {
this.options.schema = id
}
if (Array.isArray(customTags)) this.options.customTags = customTags
const opt = Object.assign({}, this.getDefaults(), this.options)
this.schema = new Schema(opt)
}
parse(node, prevDoc) {
if (this.options.keepCstNodes) this.cstNode = node
if (this.options.keepNodeTypes) this.type = 'DOCUMENT'
const {
directives = [],
contents = [],
directivesEndMarker,
error,
valueRange
} = node
if (error) {
if (!error.source) error.source = this
this.errors.push(error)
}
parseDirectives(this, directives, prevDoc)
if (directivesEndMarker) this.directivesEndMarker = true
this.range = valueRange ? [valueRange.start, valueRange.end] : null
this.setSchema()
this.anchors._cstAliases = []
parseContents(this, contents)
this.anchors.resolveNodes()
if (this.options.prettyErrors) {
for (const error of this.errors)
if (error instanceof YAMLError) error.makePretty()
for (const warn of this.warnings)
if (warn instanceof YAMLError) warn.makePretty()
}
return this
}
setTagPrefix(handle, prefix) {
if (handle[0] !== '!' || handle[handle.length - 1] !== '!')
throw new Error('Handle must start and end with !')
if (prefix) {
const prev = this.tagPrefixes.find(p => p.handle === handle)
if (prev) prev.prefix = prefix
else this.tagPrefixes.push({ handle, prefix })
} else {
this.tagPrefixes = this.tagPrefixes.filter(p => p.handle !== handle)
}
}
toJS({ json, jsonArg, mapAsMap, onAnchor, reviver } = {}) {
const anchorNodes = Object.values(this.anchors.map).map(node => [
node,
{ alias: [], aliasCount: 0, count: 1 }
])
const anchors = anchorNodes.length > 0 ? new Map(anchorNodes) : null
const ctx = {
anchors,
doc: this,
indentStep: ' ',
keep: !json,
mapAsMap:
typeof mapAsMap === 'boolean' ? mapAsMap : !!this.options.mapAsMap,
mapKeyWarned: false,
maxAliasCount: this.options.maxAliasCount,
stringify // Requiring directly in Pair would create circular dependencies
}
const res = toJS(this.contents, jsonArg || '', ctx)
if (typeof onAnchor === 'function' && anchors)
for (const { count, res } of anchors.values()) onAnchor(res, count)
return typeof reviver === 'function'
? applyReviver(reviver, { '': res }, '', res)
: res
}
toJSON(jsonArg, onAnchor) {
return this.toJS({ json: true, jsonArg, mapAsMap: false, onAnchor })
}
toString() {
if (this.errors.length > 0)
throw new Error('Document with errors cannot be stringified')
const indentSize = this.options.indent
if (!Number.isInteger(indentSize) || indentSize <= 0) {
const s = JSON.stringify(indentSize)
throw new Error(`"indent" option must be a positive integer, not ${s}`)
}
this.setSchema()
const lines = []
let hasDirectives = false
if (this.version) {
let vd = '%YAML 1.2'
if (this.schema.name === 'yaml-1.1') {
if (this.version === '1.0') vd = '%YAML:1.0'
else if (this.version === '1.1') vd = '%YAML 1.1'
}
lines.push(vd)
hasDirectives = true
}
const tagNames = listTagNames(this.contents).filter(
t => t.indexOf(defaultTagPrefix) !== 0
)
this.tagPrefixes.forEach(({ handle, prefix }) => {
if (tagNames.some(t => t.indexOf(prefix) === 0)) {
lines.push(`%TAG ${handle} ${prefix}`)
hasDirectives = true
}
})
if (hasDirectives || this.directivesEndMarker) lines.push('---')
if (this.commentBefore) {
if (hasDirectives || !this.directivesEndMarker) lines.unshift('')
lines.unshift(this.commentBefore.replace(/^/gm, '#'))
}
const ctx = {
anchors: Object.create(null),
doc: this,
indent: '',
indentStep: ' '.repeat(indentSize),
stringify // Requiring directly in nodes would create circular dependencies
}
let chompKeep = false
let contentComment = null
if (this.contents) {
if (this.contents instanceof Node) {
if (
this.contents.spaceBefore &&
(hasDirectives || this.directivesEndMarker)
)
lines.push('')
if (this.contents.commentBefore)
lines.push(this.contents.commentBefore.replace(/^/gm, '#'))
// top-level block scalars need to be indented if followed by a comment
ctx.forceBlockIndent = !!this.comment
contentComment = this.contents.comment
}
const onChompKeep = contentComment ? null : () => (chompKeep = true)
const body = stringify(
this.contents,
ctx,
() => (contentComment = null),
onChompKeep
)
lines.push(addComment(body, '', contentComment))
} else {
lines.push(stringify(this.contents, ctx))
}
if (this.comment) {
if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
lines.push('')
lines.push(this.comment.replace(/^/gm, '#'))
}
return lines.join('\n') + '\n'
}
}