yaml 2.0.0-0 → 2.0.0-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1783 @@
1
+ 'use strict';
2
+
3
+ var _rollupPluginBabelHelpers = require('./_rollupPluginBabelHelpers-eed30217.js');
4
+ var parseCst = require('./parse-3997f544.js');
5
+ var stringifyNumber = require('./stringifyNumber-dea1120c.js');
6
+ var Schema = require('./Schema-807430ba.js');
7
+
8
+ const defaultOptions = {
9
+ anchorPrefix: 'a',
10
+ customTags: null,
11
+ indent: 2,
12
+ indentSeq: true,
13
+ keepCstNodes: false,
14
+ keepNodeTypes: true,
15
+ keepUndefined: false,
16
+ mapAsMap: false,
17
+ maxAliasCount: 100,
18
+ prettyErrors: true,
19
+ simpleKeys: false,
20
+ version: '1.2'
21
+ };
22
+ const scalarOptions = {
23
+ get binary() {
24
+ return stringifyNumber.binaryOptions;
25
+ },
26
+
27
+ set binary(opt) {
28
+ Object.assign(stringifyNumber.binaryOptions, opt);
29
+ },
30
+
31
+ get bool() {
32
+ return stringifyNumber.boolOptions;
33
+ },
34
+
35
+ set bool(opt) {
36
+ Object.assign(stringifyNumber.boolOptions, opt);
37
+ },
38
+
39
+ get int() {
40
+ return stringifyNumber.intOptions;
41
+ },
42
+
43
+ set int(opt) {
44
+ Object.assign(stringifyNumber.intOptions, opt);
45
+ },
46
+
47
+ get null() {
48
+ return stringifyNumber.nullOptions;
49
+ },
50
+
51
+ set null(opt) {
52
+ Object.assign(stringifyNumber.nullOptions, opt);
53
+ },
54
+
55
+ get str() {
56
+ return stringifyNumber.strOptions;
57
+ },
58
+
59
+ set str(opt) {
60
+ Object.assign(stringifyNumber.strOptions, opt);
61
+ }
62
+
63
+ };
64
+ const documentOptions = {
65
+ '1.0': {
66
+ schema: 'yaml-1.1',
67
+ merge: true,
68
+ tagPrefixes: [{
69
+ handle: '!',
70
+ prefix: _rollupPluginBabelHelpers.defaultTagPrefix
71
+ }, {
72
+ handle: '!!',
73
+ prefix: 'tag:private.yaml.org,2002:'
74
+ }]
75
+ },
76
+ '1.1': {
77
+ schema: 'yaml-1.1',
78
+ merge: true,
79
+ tagPrefixes: [{
80
+ handle: '!',
81
+ prefix: '!'
82
+ }, {
83
+ handle: '!!',
84
+ prefix: _rollupPluginBabelHelpers.defaultTagPrefix
85
+ }]
86
+ },
87
+ '1.2': {
88
+ schema: 'core',
89
+ merge: false,
90
+ resolveKnownTags: true,
91
+ tagPrefixes: [{
92
+ handle: '!',
93
+ prefix: '!'
94
+ }, {
95
+ handle: '!!',
96
+ prefix: _rollupPluginBabelHelpers.defaultTagPrefix
97
+ }]
98
+ }
99
+ };
100
+
101
+ function stringifyTag(doc, tag) {
102
+ if ((doc.version || doc.options.version) === '1.0') {
103
+ const priv = tag.match(/^tag:private\.yaml\.org,2002:([^:/]+)$/);
104
+ if (priv) return '!' + priv[1];
105
+ const vocab = tag.match(/^tag:([a-zA-Z0-9-]+)\.yaml\.org,2002:(.*)/);
106
+ return vocab ? `!${vocab[1]}/${vocab[2]}` : `!${tag.replace(/^tag:/, '')}`;
107
+ }
108
+
109
+ let p = doc.tagPrefixes.find(p => tag.indexOf(p.prefix) === 0);
110
+
111
+ if (!p) {
112
+ const dtp = doc.getDefaults().tagPrefixes;
113
+ p = dtp && dtp.find(p => tag.indexOf(p.prefix) === 0);
114
+ }
115
+
116
+ if (!p) return tag[0] === '!' ? tag : `!<${tag}>`;
117
+ const suffix = tag.substr(p.prefix.length).replace(/[!,[\]{}]/g, ch => ({
118
+ '!': '%21',
119
+ ',': '%2C',
120
+ '[': '%5B',
121
+ ']': '%5D',
122
+ '{': '%7B',
123
+ '}': '%7D'
124
+ })[ch]);
125
+ return p.handle + suffix;
126
+ }
127
+
128
+ function getTagObject(tags, item) {
129
+ if (item instanceof stringifyNumber.Alias) return stringifyNumber.Alias;
130
+
131
+ if (item.tag) {
132
+ const match = tags.filter(t => t.tag === item.tag);
133
+ if (match.length > 0) return match.find(t => t.format === item.format) || match[0];
134
+ }
135
+
136
+ let tagObj, obj;
137
+
138
+ if (item instanceof stringifyNumber.Scalar) {
139
+ obj = item.value;
140
+ const match = tags.filter(t => t.identify && t.identify(obj));
141
+ tagObj = match.find(t => t.format === item.format) || match.find(t => !t.format);
142
+ } else {
143
+ obj = item;
144
+ tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
145
+ }
146
+
147
+ if (!tagObj) {
148
+ const name = obj && obj.constructor ? obj.constructor.name : typeof obj;
149
+ throw new Error(`Tag not resolved for ${name} value`);
150
+ }
151
+
152
+ return tagObj;
153
+ } // needs to be called before value stringifier to allow for circular anchor refs
154
+
155
+
156
+ function stringifyProps(node, tagObj, {
157
+ anchors,
158
+ doc
159
+ }) {
160
+ const props = [];
161
+ const anchor = doc.anchors.getName(node);
162
+
163
+ if (anchor) {
164
+ anchors[anchor] = node;
165
+ props.push(`&${anchor}`);
166
+ }
167
+
168
+ if (node.tag) {
169
+ props.push(stringifyTag(doc, node.tag));
170
+ } else if (!tagObj.default) {
171
+ props.push(stringifyTag(doc, tagObj.tag));
172
+ }
173
+
174
+ return props.join(' ');
175
+ }
176
+
177
+ function stringify(item, ctx, onComment, onChompKeep) {
178
+ const {
179
+ schema
180
+ } = ctx.doc;
181
+ let tagObj;
182
+
183
+ if (!(item instanceof stringifyNumber.Node)) {
184
+ item = ctx.doc.createNode(item, {
185
+ onTagObj: o => tagObj = o,
186
+ wrapScalars: true
187
+ });
188
+ }
189
+
190
+ if (item instanceof stringifyNumber.Pair) return item.toString(ctx, onComment, onChompKeep);
191
+ if (!tagObj) tagObj = getTagObject(schema.tags, item);
192
+ const props = stringifyProps(item, tagObj, ctx);
193
+ if (props.length > 0) ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1;
194
+ const str = typeof tagObj.stringify === 'function' ? tagObj.stringify(item, ctx, onComment, onChompKeep) : item instanceof stringifyNumber.Scalar ? stringifyNumber.stringifyString(item, ctx, onComment, onChompKeep) : item.toString(ctx, onComment, onChompKeep);
195
+ if (!props) return str;
196
+ return item instanceof stringifyNumber.Scalar || str[0] === '{' || str[0] === '[' ? `${props} ${str}` : `${props}\n${ctx.indent}${str}`;
197
+ }
198
+
199
+ class Anchors {
200
+ static validAnchorNode(node) {
201
+ return node instanceof stringifyNumber.Scalar || node instanceof stringifyNumber.YAMLSeq || node instanceof stringifyNumber.YAMLMap;
202
+ }
203
+
204
+ constructor(prefix) {
205
+ _rollupPluginBabelHelpers._defineProperty(this, "map", Object.create(null));
206
+
207
+ this.prefix = prefix;
208
+ }
209
+
210
+ createAlias(node, name) {
211
+ this.setAnchor(node, name);
212
+ return new stringifyNumber.Alias(node);
213
+ }
214
+
215
+ createMergePair(...sources) {
216
+ const merge = new Schema.Merge();
217
+ merge.value.items = sources.map(s => {
218
+ if (s instanceof stringifyNumber.Alias) {
219
+ if (s.source instanceof stringifyNumber.YAMLMap) return s;
220
+ } else if (s instanceof stringifyNumber.YAMLMap) {
221
+ return this.createAlias(s);
222
+ }
223
+
224
+ throw new Error('Merge sources must be Map nodes or their Aliases');
225
+ });
226
+ return merge;
227
+ }
228
+
229
+ getName(node) {
230
+ const {
231
+ map
232
+ } = this;
233
+ return Object.keys(map).find(a => map[a] === node);
234
+ }
235
+
236
+ getNames() {
237
+ return Object.keys(this.map);
238
+ }
239
+
240
+ getNode(name) {
241
+ return this.map[name];
242
+ }
243
+
244
+ newName(prefix) {
245
+ if (!prefix) prefix = this.prefix;
246
+ const names = Object.keys(this.map);
247
+
248
+ for (let i = 1; true; ++i) {
249
+ const name = `${prefix}${i}`;
250
+ if (!names.includes(name)) return name;
251
+ }
252
+ } // During parsing, map & aliases contain CST nodes
253
+
254
+
255
+ resolveNodes() {
256
+ const {
257
+ map,
258
+ _cstAliases
259
+ } = this;
260
+ Object.keys(map).forEach(a => {
261
+ map[a] = map[a].resolved;
262
+ });
263
+
264
+ _cstAliases.forEach(a => {
265
+ a.source = a.source.resolved;
266
+ });
267
+
268
+ delete this._cstAliases;
269
+ }
270
+
271
+ setAnchor(node, name) {
272
+ if (node != null && !Anchors.validAnchorNode(node)) {
273
+ throw new Error('Anchors may only be set for Scalar, Seq and Map nodes');
274
+ }
275
+
276
+ if (name && /[\x00-\x19\s,[\]{}]/.test(name)) {
277
+ throw new Error('Anchor names must not contain whitespace or control characters');
278
+ }
279
+
280
+ const {
281
+ map
282
+ } = this;
283
+ const prev = node && Object.keys(map).find(a => map[a] === node);
284
+
285
+ if (prev) {
286
+ if (!name) {
287
+ return prev;
288
+ } else if (prev !== name) {
289
+ delete map[prev];
290
+ map[name] = node;
291
+ }
292
+ } else {
293
+ if (!name) {
294
+ if (!node) return null;
295
+ name = this.newName();
296
+ }
297
+
298
+ map[name] = node;
299
+ }
300
+
301
+ return name;
302
+ }
303
+
304
+ }
305
+
306
+ /**
307
+ * Applies the JSON.parse reviver algorithm as defined in the ECMA-262 spec,
308
+ * in section 24.5.1.1 "Runtime Semantics: InternalizeJSONProperty" of the
309
+ * 2021 edition: https://tc39.es/ecma262/#sec-json.parse
310
+ *
311
+ * Includes extensions for handling Map and Set objects.
312
+ */
313
+ function applyReviver(reviver, obj, key, val) {
314
+ if (val && typeof val === 'object') {
315
+ if (Array.isArray(val)) {
316
+ for (let i = 0, len = val.length; i < len; ++i) {
317
+ const v0 = val[i];
318
+ const v1 = applyReviver(reviver, val, String(i), v0);
319
+ if (v1 === undefined) delete val[i];else if (v1 !== v0) val[i] = v1;
320
+ }
321
+ } else if (val instanceof Map) {
322
+ for (const k of Array.from(val.keys())) {
323
+ const v0 = val.get(k);
324
+ const v1 = applyReviver(reviver, val, k, v0);
325
+ if (v1 === undefined) val.delete(k);else if (v1 !== v0) val.set(k, v1);
326
+ }
327
+ } else if (val instanceof Set) {
328
+ for (const v0 of Array.from(val)) {
329
+ const v1 = applyReviver(reviver, val, v0, v0);
330
+ if (v1 === undefined) val.delete(v0);else if (v1 !== v0) {
331
+ val.delete(v0);
332
+ val.add(v1);
333
+ }
334
+ }
335
+ } else {
336
+ for (const [k, v0] of Object.entries(val)) {
337
+ const v1 = applyReviver(reviver, val, k, v0);
338
+ if (v1 === undefined) delete val[k];else if (v1 !== v0) val[k] = v1;
339
+ }
340
+ }
341
+ }
342
+
343
+ return reviver.call(obj, key, val);
344
+ }
345
+
346
+ const visit = (node, tags) => {
347
+ if (node && typeof node === 'object') {
348
+ const {
349
+ tag
350
+ } = node;
351
+
352
+ if (node instanceof stringifyNumber.Collection) {
353
+ if (tag) tags[tag] = true;
354
+ node.items.forEach(n => visit(n, tags));
355
+ } else if (node instanceof stringifyNumber.Pair) {
356
+ visit(node.key, tags);
357
+ visit(node.value, tags);
358
+ } else if (node instanceof stringifyNumber.Scalar) {
359
+ if (tag) tags[tag] = true;
360
+ }
361
+ }
362
+
363
+ return tags;
364
+ };
365
+
366
+ const listTagNames = node => Object.keys(visit(node, {}));
367
+
368
+ function resolveTagHandle(doc, node) {
369
+ const {
370
+ handle,
371
+ suffix
372
+ } = node.tag;
373
+ let prefix = doc.tagPrefixes.find(p => p.handle === handle);
374
+
375
+ if (!prefix) {
376
+ const dtp = doc.getDefaults().tagPrefixes;
377
+ if (dtp) prefix = dtp.find(p => p.handle === handle);
378
+ if (!prefix) throw new _rollupPluginBabelHelpers.YAMLSemanticError(node, `The ${handle} tag handle is non-default and was not declared.`);
379
+ }
380
+
381
+ if (!suffix) throw new _rollupPluginBabelHelpers.YAMLSemanticError(node, `The ${handle} tag has no suffix.`);
382
+
383
+ if (handle === '!' && (doc.version || doc.options.version) === '1.0') {
384
+ if (suffix[0] === '^') {
385
+ doc.warnings.push(new _rollupPluginBabelHelpers.YAMLWarning(node, 'YAML 1.0 ^ tag expansion is not supported'));
386
+ return suffix;
387
+ }
388
+
389
+ if (/[:/]/.test(suffix)) {
390
+ // word/foo -> tag:word.yaml.org,2002:foo
391
+ const vocab = suffix.match(/^([a-z0-9-]+)\/(.*)/i);
392
+ return vocab ? `tag:${vocab[1]}.yaml.org,2002:${vocab[2]}` : `tag:${suffix}`;
393
+ }
394
+ }
395
+
396
+ return prefix.prefix + decodeURIComponent(suffix);
397
+ }
398
+
399
+ function resolveTagName(doc, node) {
400
+ const {
401
+ tag,
402
+ type
403
+ } = node;
404
+ let nonSpecific = false;
405
+
406
+ if (tag) {
407
+ const {
408
+ handle,
409
+ suffix,
410
+ verbatim
411
+ } = tag;
412
+
413
+ if (verbatim) {
414
+ if (verbatim !== '!' && verbatim !== '!!') return verbatim;
415
+ const msg = `Verbatim tags aren't resolved, so ${verbatim} is invalid.`;
416
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(node, msg));
417
+ } else if (handle === '!' && !suffix) {
418
+ nonSpecific = true;
419
+ } else {
420
+ try {
421
+ return resolveTagHandle(doc, node);
422
+ } catch (error) {
423
+ doc.errors.push(error);
424
+ }
425
+ }
426
+ }
427
+
428
+ switch (type) {
429
+ case _rollupPluginBabelHelpers.Type.BLOCK_FOLDED:
430
+ case _rollupPluginBabelHelpers.Type.BLOCK_LITERAL:
431
+ case _rollupPluginBabelHelpers.Type.QUOTE_DOUBLE:
432
+ case _rollupPluginBabelHelpers.Type.QUOTE_SINGLE:
433
+ return _rollupPluginBabelHelpers.defaultTags.STR;
434
+
435
+ case _rollupPluginBabelHelpers.Type.FLOW_MAP:
436
+ case _rollupPluginBabelHelpers.Type.MAP:
437
+ return _rollupPluginBabelHelpers.defaultTags.MAP;
438
+
439
+ case _rollupPluginBabelHelpers.Type.FLOW_SEQ:
440
+ case _rollupPluginBabelHelpers.Type.SEQ:
441
+ return _rollupPluginBabelHelpers.defaultTags.SEQ;
442
+
443
+ case _rollupPluginBabelHelpers.Type.PLAIN:
444
+ return nonSpecific ? _rollupPluginBabelHelpers.defaultTags.STR : null;
445
+
446
+ default:
447
+ return null;
448
+ }
449
+ }
450
+
451
+ function checkFlowCollectionEnd(errors, cst) {
452
+ let char, name;
453
+
454
+ switch (cst.type) {
455
+ case _rollupPluginBabelHelpers.Type.FLOW_MAP:
456
+ char = '}';
457
+ name = 'flow map';
458
+ break;
459
+
460
+ case _rollupPluginBabelHelpers.Type.FLOW_SEQ:
461
+ char = ']';
462
+ name = 'flow sequence';
463
+ break;
464
+
465
+ default:
466
+ errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(cst, 'Not a flow collection!?'));
467
+ return;
468
+ }
469
+
470
+ let lastItem;
471
+
472
+ for (let i = cst.items.length - 1; i >= 0; --i) {
473
+ const item = cst.items[i];
474
+
475
+ if (!item || item.type !== _rollupPluginBabelHelpers.Type.COMMENT) {
476
+ lastItem = item;
477
+ break;
478
+ }
479
+ }
480
+
481
+ if (lastItem && lastItem.char !== char) {
482
+ const msg = `Expected ${name} to end with ${char}`;
483
+ let err;
484
+
485
+ if (typeof lastItem.offset === 'number') {
486
+ err = new _rollupPluginBabelHelpers.YAMLSemanticError(cst, msg);
487
+ err.offset = lastItem.offset + 1;
488
+ } else {
489
+ err = new _rollupPluginBabelHelpers.YAMLSemanticError(lastItem, msg);
490
+ if (lastItem.range && lastItem.range.end) err.offset = lastItem.range.end - lastItem.range.start;
491
+ }
492
+
493
+ errors.push(err);
494
+ }
495
+ }
496
+ function checkFlowCommentSpace(errors, comment) {
497
+ const prev = comment.context.src[comment.range.start - 1];
498
+
499
+ if (prev !== '\n' && prev !== '\t' && prev !== ' ') {
500
+ const msg = 'Comments must be separated from other tokens by white space characters';
501
+ errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(comment, msg));
502
+ }
503
+ }
504
+ function getLongKeyError(source, key) {
505
+ const sk = String(key);
506
+ const k = sk.substr(0, 8) + '...' + sk.substr(-8);
507
+ return new _rollupPluginBabelHelpers.YAMLSemanticError(source, `The "${k}" key is too long`);
508
+ }
509
+ function resolveComments(collection, comments) {
510
+ for (const {
511
+ afterKey,
512
+ before,
513
+ comment
514
+ } of comments) {
515
+ let item = collection.items[before];
516
+
517
+ if (!item) {
518
+ if (comment !== undefined) {
519
+ if (collection.comment) collection.comment += '\n' + comment;else collection.comment = comment;
520
+ }
521
+ } else {
522
+ if (afterKey && item.value) item = item.value;
523
+
524
+ if (comment === undefined) {
525
+ if (afterKey || !item.commentBefore) item.spaceBefore = true;
526
+ } else {
527
+ if (item.commentBefore) item.commentBefore += '\n' + comment;else item.commentBefore = comment;
528
+ }
529
+ }
530
+ }
531
+ }
532
+
533
+ function resolveMap(doc, cst) {
534
+ const {
535
+ comments,
536
+ items
537
+ } = cst.type === _rollupPluginBabelHelpers.Type.FLOW_MAP ? resolveFlowMapItems(doc, cst) : resolveBlockMapItems(doc, cst);
538
+ const map = new stringifyNumber.YAMLMap(doc.schema);
539
+ map.items = items;
540
+ resolveComments(map, comments);
541
+ let hasCollectionKey = false;
542
+
543
+ for (let i = 0; i < items.length; ++i) {
544
+ const {
545
+ key: iKey
546
+ } = items[i];
547
+ if (iKey instanceof stringifyNumber.Collection) hasCollectionKey = true;
548
+
549
+ if (doc.schema.merge && iKey && iKey.value === Schema.MERGE_KEY) {
550
+ items[i] = new Schema.Merge(items[i]);
551
+ const sources = items[i].value.items;
552
+ let error = null;
553
+ sources.some(node => {
554
+ if (node instanceof stringifyNumber.Alias) {
555
+ // During parsing, alias sources are CST nodes; to account for
556
+ // circular references their resolved values can't be used here.
557
+ const {
558
+ type
559
+ } = node.source;
560
+ if (type === _rollupPluginBabelHelpers.Type.MAP || type === _rollupPluginBabelHelpers.Type.FLOW_MAP) return false;
561
+ return error = 'Merge nodes aliases can only point to maps';
562
+ }
563
+
564
+ return error = 'Merge nodes can only have Alias nodes as values';
565
+ });
566
+ if (error) doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(cst, error));
567
+ } else {
568
+ for (let j = i + 1; j < items.length; ++j) {
569
+ const {
570
+ key: jKey
571
+ } = items[j];
572
+
573
+ if (iKey === jKey || iKey && jKey && Object.prototype.hasOwnProperty.call(iKey, 'value') && iKey.value === jKey.value) {
574
+ const msg = `Map keys must be unique; "${iKey}" is repeated`;
575
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(cst, msg));
576
+ break;
577
+ }
578
+ }
579
+ }
580
+ }
581
+
582
+ if (hasCollectionKey && !doc.options.mapAsMap) {
583
+ const warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
584
+ doc.warnings.push(new _rollupPluginBabelHelpers.YAMLWarning(cst, warn));
585
+ }
586
+
587
+ cst.resolved = map;
588
+ return map;
589
+ }
590
+
591
+ const valueHasPairComment = ({
592
+ context: {
593
+ lineStart,
594
+ node,
595
+ src
596
+ },
597
+ props
598
+ }) => {
599
+ if (props.length === 0) return false;
600
+ const {
601
+ start
602
+ } = props[0];
603
+ if (node && start > node.valueRange.start) return false;
604
+ if (src[start] !== _rollupPluginBabelHelpers.Char.COMMENT) return false;
605
+
606
+ for (let i = lineStart; i < start; ++i) if (src[i] === '\n') return false;
607
+
608
+ return true;
609
+ };
610
+
611
+ function resolvePairComment(item, pair) {
612
+ if (!valueHasPairComment(item)) return;
613
+ const comment = item.getPropValue(0, _rollupPluginBabelHelpers.Char.COMMENT, true);
614
+ let found = false;
615
+ const cb = pair.value.commentBefore;
616
+
617
+ if (cb && cb.startsWith(comment)) {
618
+ pair.value.commentBefore = cb.substr(comment.length + 1);
619
+ found = true;
620
+ } else {
621
+ const cc = pair.value.comment;
622
+
623
+ if (!item.node && cc && cc.startsWith(comment)) {
624
+ pair.value.comment = cc.substr(comment.length + 1);
625
+ found = true;
626
+ }
627
+ }
628
+
629
+ if (found) pair.comment = comment;
630
+ }
631
+
632
+ function resolveBlockMapItems(doc, cst) {
633
+ const comments = [];
634
+ const items = [];
635
+ let key = undefined;
636
+ let keyStart = null;
637
+
638
+ for (let i = 0; i < cst.items.length; ++i) {
639
+ const item = cst.items[i];
640
+
641
+ switch (item.type) {
642
+ case _rollupPluginBabelHelpers.Type.BLANK_LINE:
643
+ comments.push({
644
+ afterKey: !!key,
645
+ before: items.length
646
+ });
647
+ break;
648
+
649
+ case _rollupPluginBabelHelpers.Type.COMMENT:
650
+ comments.push({
651
+ afterKey: !!key,
652
+ before: items.length,
653
+ comment: item.comment
654
+ });
655
+ break;
656
+
657
+ case _rollupPluginBabelHelpers.Type.MAP_KEY:
658
+ if (key !== undefined) items.push(new stringifyNumber.Pair(key));
659
+ if (item.error) doc.errors.push(item.error);
660
+ key = resolveNode(doc, item.node);
661
+ keyStart = null;
662
+ break;
663
+
664
+ case _rollupPluginBabelHelpers.Type.MAP_VALUE:
665
+ {
666
+ if (key === undefined) key = null;
667
+ if (item.error) doc.errors.push(item.error);
668
+
669
+ if (!item.context.atLineStart && item.node && item.node.type === _rollupPluginBabelHelpers.Type.MAP && !item.node.context.atLineStart) {
670
+ const msg = 'Nested mappings are not allowed in compact mappings';
671
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(item.node, msg));
672
+ }
673
+
674
+ let valueNode = item.node;
675
+
676
+ if (!valueNode && item.props.length > 0) {
677
+ // Comments on an empty mapping value need to be preserved, so we
678
+ // need to construct a minimal empty node here to use instead of the
679
+ // missing `item.node`. -- eemeli/yaml#19
680
+ valueNode = new parseCst.PlainValue(_rollupPluginBabelHelpers.Type.PLAIN, []);
681
+ valueNode.context = {
682
+ parent: item,
683
+ src: item.context.src
684
+ };
685
+ const pos = item.range.start + 1;
686
+ valueNode.range = {
687
+ start: pos,
688
+ end: pos
689
+ };
690
+ valueNode.valueRange = {
691
+ start: pos,
692
+ end: pos
693
+ };
694
+
695
+ if (typeof item.range.origStart === 'number') {
696
+ const origPos = item.range.origStart + 1;
697
+ valueNode.range.origStart = valueNode.range.origEnd = origPos;
698
+ valueNode.valueRange.origStart = valueNode.valueRange.origEnd = origPos;
699
+ }
700
+ }
701
+
702
+ const pair = new stringifyNumber.Pair(key, resolveNode(doc, valueNode));
703
+ resolvePairComment(item, pair);
704
+ items.push(pair);
705
+
706
+ if (key && typeof keyStart === 'number') {
707
+ if (item.range.start > keyStart + 1024) doc.errors.push(getLongKeyError(cst, key));
708
+ }
709
+
710
+ key = undefined;
711
+ keyStart = null;
712
+ }
713
+ break;
714
+
715
+ default:
716
+ if (key !== undefined) items.push(new stringifyNumber.Pair(key));
717
+ key = resolveNode(doc, item);
718
+ keyStart = item.range.start;
719
+ if (item.error) doc.errors.push(item.error);
720
+
721
+ next: for (let j = i + 1;; ++j) {
722
+ const nextItem = cst.items[j];
723
+
724
+ switch (nextItem && nextItem.type) {
725
+ case _rollupPluginBabelHelpers.Type.BLANK_LINE:
726
+ case _rollupPluginBabelHelpers.Type.COMMENT:
727
+ continue next;
728
+
729
+ case _rollupPluginBabelHelpers.Type.MAP_VALUE:
730
+ break next;
731
+
732
+ default:
733
+ {
734
+ const msg = 'Implicit map keys need to be followed by map values';
735
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(item, msg));
736
+ break next;
737
+ }
738
+ }
739
+ }
740
+
741
+ if (item.valueRangeContainsNewline) {
742
+ const msg = 'Implicit map keys need to be on a single line';
743
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(item, msg));
744
+ }
745
+
746
+ }
747
+ }
748
+
749
+ if (key !== undefined) items.push(new stringifyNumber.Pair(key));
750
+ return {
751
+ comments,
752
+ items
753
+ };
754
+ }
755
+
756
+ function resolveFlowMapItems(doc, cst) {
757
+ const comments = [];
758
+ const items = [];
759
+ let key = undefined;
760
+ let explicitKey = false;
761
+ let next = '{';
762
+
763
+ for (let i = 0; i < cst.items.length; ++i) {
764
+ const item = cst.items[i];
765
+
766
+ if (typeof item.char === 'string') {
767
+ const {
768
+ char,
769
+ offset
770
+ } = item;
771
+
772
+ if (char === '?' && key === undefined && !explicitKey) {
773
+ explicitKey = true;
774
+ next = ':';
775
+ continue;
776
+ }
777
+
778
+ if (char === ':') {
779
+ if (key === undefined) key = null;
780
+
781
+ if (next === ':') {
782
+ next = ',';
783
+ continue;
784
+ }
785
+ } else {
786
+ if (explicitKey) {
787
+ if (key === undefined && char !== ',') key = null;
788
+ explicitKey = false;
789
+ }
790
+
791
+ if (key !== undefined) {
792
+ items.push(new stringifyNumber.Pair(key));
793
+ key = undefined;
794
+
795
+ if (char === ',') {
796
+ next = ':';
797
+ continue;
798
+ }
799
+ }
800
+ }
801
+
802
+ if (char === '}') {
803
+ if (i === cst.items.length - 1) continue;
804
+ } else if (char === next) {
805
+ next = ':';
806
+ continue;
807
+ }
808
+
809
+ const msg = `Flow map contains an unexpected ${char}`;
810
+ const err = new _rollupPluginBabelHelpers.YAMLSyntaxError(cst, msg);
811
+ err.offset = offset;
812
+ doc.errors.push(err);
813
+ } else if (item.type === _rollupPluginBabelHelpers.Type.BLANK_LINE) {
814
+ comments.push({
815
+ afterKey: !!key,
816
+ before: items.length
817
+ });
818
+ } else if (item.type === _rollupPluginBabelHelpers.Type.COMMENT) {
819
+ checkFlowCommentSpace(doc.errors, item);
820
+ comments.push({
821
+ afterKey: !!key,
822
+ before: items.length,
823
+ comment: item.comment
824
+ });
825
+ } else if (key === undefined) {
826
+ if (next === ',') doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(item, 'Separator , missing in flow map'));
827
+ key = resolveNode(doc, item);
828
+ } else {
829
+ if (next !== ',') doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(item, 'Indicator : missing in flow map entry'));
830
+ items.push(new stringifyNumber.Pair(key, resolveNode(doc, item)));
831
+ key = undefined;
832
+ explicitKey = false;
833
+ }
834
+ }
835
+
836
+ checkFlowCollectionEnd(doc.errors, cst);
837
+ if (key !== undefined) items.push(new stringifyNumber.Pair(key));
838
+ return {
839
+ comments,
840
+ items
841
+ };
842
+ }
843
+
844
+ function resolveSeq(doc, cst) {
845
+ const {
846
+ comments,
847
+ items
848
+ } = cst.type === _rollupPluginBabelHelpers.Type.FLOW_SEQ ? resolveFlowSeqItems(doc, cst) : resolveBlockSeqItems(doc, cst);
849
+ const seq = new stringifyNumber.YAMLSeq(doc.schema);
850
+ seq.items = items;
851
+ resolveComments(seq, comments);
852
+
853
+ if (!doc.options.mapAsMap && items.some(it => it instanceof stringifyNumber.Pair && it.key instanceof stringifyNumber.Collection)) {
854
+ const warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
855
+ doc.warnings.push(new _rollupPluginBabelHelpers.YAMLWarning(cst, warn));
856
+ }
857
+
858
+ cst.resolved = seq;
859
+ return seq;
860
+ }
861
+
862
+ function resolveBlockSeqItems(doc, cst) {
863
+ const comments = [];
864
+ const items = [];
865
+
866
+ for (let i = 0; i < cst.items.length; ++i) {
867
+ const item = cst.items[i];
868
+
869
+ switch (item.type) {
870
+ case _rollupPluginBabelHelpers.Type.BLANK_LINE:
871
+ comments.push({
872
+ before: items.length
873
+ });
874
+ break;
875
+
876
+ case _rollupPluginBabelHelpers.Type.COMMENT:
877
+ comments.push({
878
+ comment: item.comment,
879
+ before: items.length
880
+ });
881
+ break;
882
+
883
+ case _rollupPluginBabelHelpers.Type.SEQ_ITEM:
884
+ if (item.error) doc.errors.push(item.error);
885
+ items.push(resolveNode(doc, item.node));
886
+
887
+ if (item.hasProps) {
888
+ const msg = 'Sequence items cannot have tags or anchors before the - indicator';
889
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(item, msg));
890
+ }
891
+
892
+ break;
893
+
894
+ default:
895
+ if (item.error) doc.errors.push(item.error);
896
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSyntaxError(item, `Unexpected ${item.type} node in sequence`));
897
+ }
898
+ }
899
+
900
+ return {
901
+ comments,
902
+ items
903
+ };
904
+ }
905
+
906
+ function resolveFlowSeqItems(doc, cst) {
907
+ const comments = [];
908
+ const items = [];
909
+ let explicitKey = false;
910
+ let key = undefined;
911
+ let keyStart = null;
912
+ let next = '[';
913
+ let prevItem = null;
914
+
915
+ for (let i = 0; i < cst.items.length; ++i) {
916
+ const item = cst.items[i];
917
+
918
+ if (typeof item.char === 'string') {
919
+ const {
920
+ char,
921
+ offset
922
+ } = item;
923
+
924
+ if (char !== ':' && (explicitKey || key !== undefined)) {
925
+ if (explicitKey && key === undefined) key = next ? items.pop() : null;
926
+ items.push(new stringifyNumber.Pair(key));
927
+ explicitKey = false;
928
+ key = undefined;
929
+ keyStart = null;
930
+ }
931
+
932
+ if (char === next) {
933
+ next = null;
934
+ } else if (!next && char === '?') {
935
+ explicitKey = true;
936
+ } else if (next !== '[' && char === ':' && key === undefined) {
937
+ if (next === ',') {
938
+ key = items.pop();
939
+
940
+ if (key instanceof stringifyNumber.Pair) {
941
+ const msg = 'Chaining flow sequence pairs is invalid';
942
+ const err = new _rollupPluginBabelHelpers.YAMLSemanticError(cst, msg);
943
+ err.offset = offset;
944
+ doc.errors.push(err);
945
+ }
946
+
947
+ if (!explicitKey && typeof keyStart === 'number') {
948
+ const keyEnd = item.range ? item.range.start : item.offset;
949
+ if (keyEnd > keyStart + 1024) doc.errors.push(getLongKeyError(cst, key));
950
+ const {
951
+ src
952
+ } = prevItem.context;
953
+
954
+ for (let i = keyStart; i < keyEnd; ++i) if (src[i] === '\n') {
955
+ const msg = 'Implicit keys of flow sequence pairs need to be on a single line';
956
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(prevItem, msg));
957
+ break;
958
+ }
959
+ }
960
+ } else {
961
+ key = null;
962
+ }
963
+
964
+ keyStart = null;
965
+ explicitKey = false;
966
+ next = null;
967
+ } else if (next === '[' || char !== ']' || i < cst.items.length - 1) {
968
+ const msg = `Flow sequence contains an unexpected ${char}`;
969
+ const err = new _rollupPluginBabelHelpers.YAMLSyntaxError(cst, msg);
970
+ err.offset = offset;
971
+ doc.errors.push(err);
972
+ }
973
+ } else if (item.type === _rollupPluginBabelHelpers.Type.BLANK_LINE) {
974
+ comments.push({
975
+ before: items.length
976
+ });
977
+ } else if (item.type === _rollupPluginBabelHelpers.Type.COMMENT) {
978
+ checkFlowCommentSpace(doc.errors, item);
979
+ comments.push({
980
+ comment: item.comment,
981
+ before: items.length
982
+ });
983
+ } else {
984
+ if (next) {
985
+ const msg = `Expected a ${next} in flow sequence`;
986
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(item, msg));
987
+ }
988
+
989
+ const value = resolveNode(doc, item);
990
+
991
+ if (key === undefined) {
992
+ items.push(value);
993
+ prevItem = item;
994
+ } else {
995
+ items.push(new stringifyNumber.Pair(key, value));
996
+ key = undefined;
997
+ }
998
+
999
+ keyStart = item.range.start;
1000
+ next = ',';
1001
+ }
1002
+ }
1003
+
1004
+ checkFlowCollectionEnd(doc.errors, cst);
1005
+ if (key !== undefined) items.push(new stringifyNumber.Pair(key));
1006
+ return {
1007
+ comments,
1008
+ items
1009
+ };
1010
+ }
1011
+
1012
+ function resolveByTagName({
1013
+ knownTags,
1014
+ tags
1015
+ }, tagName, value, onError) {
1016
+ const matchWithTest = [];
1017
+
1018
+ for (const tag of tags) {
1019
+ if (tag.tag === tagName) {
1020
+ if (tag.test) {
1021
+ if (typeof value === 'string') matchWithTest.push(tag);else onError(`The tag ${tagName} cannot be applied to a collection`);
1022
+ } else {
1023
+ const res = tag.resolve(value, onError);
1024
+ return res instanceof stringifyNumber.Collection ? res : new stringifyNumber.Scalar(res);
1025
+ }
1026
+ }
1027
+ }
1028
+
1029
+ if (matchWithTest.length > 0) return stringifyNumber.resolveScalar(value, matchWithTest);
1030
+ const kt = knownTags[tagName];
1031
+
1032
+ if (kt) {
1033
+ tags.push(Object.assign({}, kt, {
1034
+ default: false,
1035
+ test: undefined
1036
+ }));
1037
+ const res = kt.resolve(value, onError);
1038
+ return res instanceof stringifyNumber.Collection ? res : new stringifyNumber.Scalar(res);
1039
+ }
1040
+
1041
+ return null;
1042
+ }
1043
+
1044
+ function resolveTag(doc, node, tagName) {
1045
+ const {
1046
+ MAP,
1047
+ SEQ,
1048
+ STR
1049
+ } = _rollupPluginBabelHelpers.defaultTags;
1050
+ let value, fallback;
1051
+
1052
+ const onError = message => doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(node, message));
1053
+
1054
+ try {
1055
+ switch (node.type) {
1056
+ case _rollupPluginBabelHelpers.Type.FLOW_MAP:
1057
+ case _rollupPluginBabelHelpers.Type.MAP:
1058
+ value = resolveMap(doc, node);
1059
+ fallback = MAP;
1060
+ if (tagName === SEQ || tagName === STR) onError(`The tag ${tagName} cannot be applied to a mapping`);
1061
+ break;
1062
+
1063
+ case _rollupPluginBabelHelpers.Type.FLOW_SEQ:
1064
+ case _rollupPluginBabelHelpers.Type.SEQ:
1065
+ value = resolveSeq(doc, node);
1066
+ fallback = SEQ;
1067
+ if (tagName === MAP || tagName === STR) onError(`The tag ${tagName} cannot be applied to a sequence`);
1068
+ break;
1069
+
1070
+ default:
1071
+ value = node.strValue || '';
1072
+
1073
+ if (typeof value !== 'string') {
1074
+ value.errors.forEach(error => doc.errors.push(error));
1075
+ value = value.str;
1076
+ }
1077
+
1078
+ if (tagName === MAP || tagName === SEQ) onError(`The tag ${tagName} cannot be applied to a scalar`);
1079
+ fallback = STR;
1080
+ }
1081
+
1082
+ const res = resolveByTagName(doc.schema, tagName, value, onError);
1083
+
1084
+ if (res) {
1085
+ if (tagName && node.tag) res.tag = tagName;
1086
+ return res;
1087
+ }
1088
+ } catch (error) {
1089
+ /* istanbul ignore if */
1090
+ if (!error.source) error.source = node;
1091
+ doc.errors.push(error);
1092
+ return null;
1093
+ }
1094
+
1095
+ try {
1096
+ if (!fallback) throw new Error(`The tag ${tagName} is unavailable`);
1097
+ const msg = `The tag ${tagName} is unavailable, falling back to ${fallback}`;
1098
+ doc.warnings.push(new _rollupPluginBabelHelpers.YAMLWarning(node, msg));
1099
+ const res = resolveByTagName(doc.schema, fallback, value, onError);
1100
+ res.tag = tagName;
1101
+ return res;
1102
+ } catch (error) {
1103
+ const refError = new _rollupPluginBabelHelpers.YAMLReferenceError(node, error.message);
1104
+ refError.stack = error.stack;
1105
+ doc.errors.push(refError);
1106
+ return null;
1107
+ }
1108
+ }
1109
+
1110
+ const isCollectionItem = node => {
1111
+ if (!node) return false;
1112
+ const {
1113
+ type
1114
+ } = node;
1115
+ return type === _rollupPluginBabelHelpers.Type.MAP_KEY || type === _rollupPluginBabelHelpers.Type.MAP_VALUE || type === _rollupPluginBabelHelpers.Type.SEQ_ITEM;
1116
+ };
1117
+
1118
+ function resolveNodeProps(errors, node) {
1119
+ const comments = {
1120
+ before: [],
1121
+ after: []
1122
+ };
1123
+ let hasAnchor = false;
1124
+ let hasTag = false;
1125
+ const props = isCollectionItem(node.context.parent) ? node.context.parent.props.concat(node.props) : node.props;
1126
+
1127
+ for (const {
1128
+ start,
1129
+ end
1130
+ } of props) {
1131
+ switch (node.context.src[start]) {
1132
+ case _rollupPluginBabelHelpers.Char.COMMENT:
1133
+ {
1134
+ if (!node.commentHasRequiredWhitespace(start)) {
1135
+ const msg = 'Comments must be separated from other tokens by white space characters';
1136
+ errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(node, msg));
1137
+ }
1138
+
1139
+ const {
1140
+ header,
1141
+ valueRange
1142
+ } = node;
1143
+ const cc = valueRange && (start > valueRange.start || header && start > header.start) ? comments.after : comments.before;
1144
+ cc.push(node.context.src.slice(start + 1, end));
1145
+ break;
1146
+ }
1147
+ // Actual anchor & tag resolution is handled by schema, here we just complain
1148
+
1149
+ case _rollupPluginBabelHelpers.Char.ANCHOR:
1150
+ if (hasAnchor) {
1151
+ const msg = 'A node can have at most one anchor';
1152
+ errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(node, msg));
1153
+ }
1154
+
1155
+ hasAnchor = true;
1156
+ break;
1157
+
1158
+ case _rollupPluginBabelHelpers.Char.TAG:
1159
+ if (hasTag) {
1160
+ const msg = 'A node can have at most one tag';
1161
+ errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(node, msg));
1162
+ }
1163
+
1164
+ hasTag = true;
1165
+ break;
1166
+ }
1167
+ }
1168
+
1169
+ return {
1170
+ comments,
1171
+ hasAnchor,
1172
+ hasTag
1173
+ };
1174
+ }
1175
+
1176
+ function resolveNodeValue(doc, node) {
1177
+ const {
1178
+ anchors,
1179
+ errors,
1180
+ schema
1181
+ } = doc;
1182
+
1183
+ if (node.type === _rollupPluginBabelHelpers.Type.ALIAS) {
1184
+ const name = node.rawValue;
1185
+ const src = anchors.getNode(name);
1186
+
1187
+ if (!src) {
1188
+ const msg = `Aliased anchor not found: ${name}`;
1189
+ errors.push(new _rollupPluginBabelHelpers.YAMLReferenceError(node, msg));
1190
+ return null;
1191
+ } // Lazy resolution for circular references
1192
+
1193
+
1194
+ const res = new stringifyNumber.Alias(src);
1195
+
1196
+ anchors._cstAliases.push(res);
1197
+
1198
+ return res;
1199
+ }
1200
+
1201
+ const tagName = resolveTagName(doc, node);
1202
+ if (tagName) return resolveTag(doc, node, tagName);
1203
+
1204
+ if (node.type !== _rollupPluginBabelHelpers.Type.PLAIN) {
1205
+ const msg = `Failed to resolve ${node.type} node here`;
1206
+ errors.push(new _rollupPluginBabelHelpers.YAMLSyntaxError(node, msg));
1207
+ return null;
1208
+ }
1209
+
1210
+ try {
1211
+ let str = node.strValue || '';
1212
+
1213
+ if (typeof str !== 'string') {
1214
+ str.errors.forEach(error => doc.errors.push(error));
1215
+ str = str.str;
1216
+ }
1217
+
1218
+ return stringifyNumber.resolveScalar(str, schema.tags);
1219
+ } catch (error) {
1220
+ if (!error.source) error.source = node;
1221
+ errors.push(error);
1222
+ return null;
1223
+ }
1224
+ } // sets node.resolved on success
1225
+
1226
+
1227
+ function resolveNode(doc, node) {
1228
+ if (!node) return null;
1229
+ if (node.error) doc.errors.push(node.error);
1230
+ const {
1231
+ comments,
1232
+ hasAnchor,
1233
+ hasTag
1234
+ } = resolveNodeProps(doc.errors, node);
1235
+
1236
+ if (hasAnchor) {
1237
+ const {
1238
+ anchors
1239
+ } = doc;
1240
+ const name = node.anchor;
1241
+ const prev = anchors.getNode(name); // At this point, aliases for any preceding node with the same anchor
1242
+ // name have already been resolved, so it may safely be renamed.
1243
+
1244
+ if (prev) anchors.map[anchors.newName(name)] = prev; // During parsing, we need to store the CST node in anchors.map as
1245
+ // anchors need to be available during resolution to allow for
1246
+ // circular references.
1247
+
1248
+ anchors.map[name] = node;
1249
+ }
1250
+
1251
+ if (node.type === _rollupPluginBabelHelpers.Type.ALIAS && (hasAnchor || hasTag)) {
1252
+ const msg = 'An alias node must not specify any properties';
1253
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(node, msg));
1254
+ }
1255
+
1256
+ const res = resolveNodeValue(doc, node);
1257
+
1258
+ if (res) {
1259
+ res.range = [node.range.start, node.range.end];
1260
+ if (doc.options.keepCstNodes) res.cstNode = node;
1261
+ if (doc.options.keepNodeTypes) res.type = node.type;
1262
+ const cb = comments.before.join('\n');
1263
+
1264
+ if (cb) {
1265
+ res.commentBefore = res.commentBefore ? `${res.commentBefore}\n${cb}` : cb;
1266
+ }
1267
+
1268
+ const ca = comments.after.join('\n');
1269
+ if (ca) res.comment = res.comment ? `${res.comment}\n${ca}` : ca;
1270
+ }
1271
+
1272
+ return node.resolved = res;
1273
+ }
1274
+
1275
+ function parseContents(doc, contents) {
1276
+ const comments = {
1277
+ before: [],
1278
+ after: []
1279
+ };
1280
+ let body = undefined;
1281
+ let spaceBefore = false;
1282
+
1283
+ for (const node of contents) {
1284
+ if (node.valueRange) {
1285
+ if (body !== undefined) {
1286
+ const msg = 'Document contains trailing content not separated by a ... or --- line';
1287
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSyntaxError(node, msg));
1288
+ break;
1289
+ }
1290
+
1291
+ const res = resolveNode(doc, node);
1292
+
1293
+ if (spaceBefore) {
1294
+ res.spaceBefore = true;
1295
+ spaceBefore = false;
1296
+ }
1297
+
1298
+ body = res;
1299
+ } else if (node.comment !== null) {
1300
+ const cc = body === undefined ? comments.before : comments.after;
1301
+ cc.push(node.comment);
1302
+ } else if (node.type === _rollupPluginBabelHelpers.Type.BLANK_LINE) {
1303
+ spaceBefore = true;
1304
+
1305
+ if (body === undefined && comments.before.length > 0 && !doc.commentBefore) {
1306
+ // space-separated comments at start are parsed as document comments
1307
+ doc.commentBefore = comments.before.join('\n');
1308
+ comments.before = [];
1309
+ }
1310
+ }
1311
+ }
1312
+
1313
+ doc.contents = body || null;
1314
+
1315
+ if (!body) {
1316
+ doc.comment = comments.before.concat(comments.after).join('\n') || null;
1317
+ } else {
1318
+ const cb = comments.before.join('\n');
1319
+
1320
+ if (cb) {
1321
+ const cbNode = body instanceof stringifyNumber.Collection && body.items[0] ? body.items[0] : body;
1322
+ cbNode.commentBefore = cbNode.commentBefore ? `${cb}\n${cbNode.commentBefore}` : cb;
1323
+ }
1324
+
1325
+ doc.comment = comments.after.join('\n') || null;
1326
+ }
1327
+ }
1328
+
1329
+ function resolveTagDirective({
1330
+ tagPrefixes
1331
+ }, directive) {
1332
+ const [handle, prefix] = directive.parameters;
1333
+
1334
+ if (!handle || !prefix) {
1335
+ const msg = 'Insufficient parameters given for %TAG directive';
1336
+ throw new _rollupPluginBabelHelpers.YAMLSemanticError(directive, msg);
1337
+ }
1338
+
1339
+ if (tagPrefixes.some(p => p.handle === handle)) {
1340
+ const msg = 'The %TAG directive must only be given at most once per handle in the same document.';
1341
+ throw new _rollupPluginBabelHelpers.YAMLSemanticError(directive, msg);
1342
+ }
1343
+
1344
+ return {
1345
+ handle,
1346
+ prefix
1347
+ };
1348
+ }
1349
+
1350
+ function resolveYamlDirective(doc, directive) {
1351
+ let [version] = directive.parameters;
1352
+ if (directive.name === 'YAML:1.0') version = '1.0';
1353
+
1354
+ if (!version) {
1355
+ const msg = 'Insufficient parameters given for %YAML directive';
1356
+ throw new _rollupPluginBabelHelpers.YAMLSemanticError(directive, msg);
1357
+ }
1358
+
1359
+ if (!documentOptions[version]) {
1360
+ const v0 = doc.version || doc.options.version;
1361
+ const msg = `Document will be parsed as YAML ${v0} rather than YAML ${version}`;
1362
+ doc.warnings.push(new _rollupPluginBabelHelpers.YAMLWarning(directive, msg));
1363
+ }
1364
+
1365
+ return version;
1366
+ }
1367
+
1368
+ function parseDirectives(doc, directives, prevDoc) {
1369
+ const directiveComments = [];
1370
+ let hasDirectives = false;
1371
+
1372
+ for (const directive of directives) {
1373
+ const {
1374
+ comment,
1375
+ name
1376
+ } = directive;
1377
+
1378
+ switch (name) {
1379
+ case 'TAG':
1380
+ try {
1381
+ doc.tagPrefixes.push(resolveTagDirective(doc, directive));
1382
+ } catch (error) {
1383
+ doc.errors.push(error);
1384
+ }
1385
+
1386
+ hasDirectives = true;
1387
+ break;
1388
+
1389
+ case 'YAML':
1390
+ case 'YAML:1.0':
1391
+ if (doc.version) {
1392
+ const msg = 'The %YAML directive must only be given at most once per document.';
1393
+ doc.errors.push(new _rollupPluginBabelHelpers.YAMLSemanticError(directive, msg));
1394
+ }
1395
+
1396
+ try {
1397
+ doc.version = resolveYamlDirective(doc, directive);
1398
+ } catch (error) {
1399
+ doc.errors.push(error);
1400
+ }
1401
+
1402
+ hasDirectives = true;
1403
+ break;
1404
+
1405
+ default:
1406
+ if (name) {
1407
+ const msg = `YAML only supports %TAG and %YAML directives, and not %${name}`;
1408
+ doc.warnings.push(new _rollupPluginBabelHelpers.YAMLWarning(directive, msg));
1409
+ }
1410
+
1411
+ }
1412
+
1413
+ if (comment) directiveComments.push(comment);
1414
+ }
1415
+
1416
+ if (prevDoc && !hasDirectives && '1.1' === (doc.version || prevDoc.version || doc.options.version)) {
1417
+ const copyTagPrefix = ({
1418
+ handle,
1419
+ prefix
1420
+ }) => ({
1421
+ handle,
1422
+ prefix
1423
+ });
1424
+
1425
+ doc.tagPrefixes = prevDoc.tagPrefixes.map(copyTagPrefix);
1426
+ doc.version = prevDoc.version;
1427
+ }
1428
+
1429
+ doc.commentBefore = directiveComments.join('\n') || null;
1430
+ }
1431
+
1432
+ function assertCollection(contents) {
1433
+ if (contents instanceof stringifyNumber.Collection) return true;
1434
+ throw new Error('Expected a YAML collection as document contents');
1435
+ }
1436
+
1437
+ class Document {
1438
+ constructor(value, replacer, options) {
1439
+ if (options === undefined && replacer && typeof replacer === 'object' && !Array.isArray(replacer)) {
1440
+ options = replacer;
1441
+ replacer = undefined;
1442
+ }
1443
+
1444
+ this.options = Object.assign({}, defaultOptions, options);
1445
+ this.anchors = new Anchors(this.options.anchorPrefix);
1446
+ this.commentBefore = null;
1447
+ this.comment = null;
1448
+ this.directivesEndMarker = null;
1449
+ this.errors = [];
1450
+ this.schema = null;
1451
+ this.tagPrefixes = [];
1452
+ this.version = null;
1453
+ this.warnings = [];
1454
+
1455
+ if (value === undefined) {
1456
+ // note that this.schema is left as null here
1457
+ this.contents = null;
1458
+ } else if (value instanceof parseCst.Document) {
1459
+ this.parse(value);
1460
+ } else {
1461
+ this.contents = this.createNode(value, {
1462
+ replacer
1463
+ });
1464
+ }
1465
+ }
1466
+
1467
+ add(value) {
1468
+ assertCollection(this.contents);
1469
+ return this.contents.add(value);
1470
+ }
1471
+
1472
+ addIn(path, value) {
1473
+ assertCollection(this.contents);
1474
+ this.contents.addIn(path, value);
1475
+ }
1476
+
1477
+ createNode(value, {
1478
+ keepUndefined,
1479
+ onTagObj,
1480
+ replacer,
1481
+ tag,
1482
+ wrapScalars
1483
+ } = {}) {
1484
+ this.setSchema();
1485
+ if (typeof replacer === 'function') value = replacer.call({
1486
+ '': value
1487
+ }, '', value);else if (Array.isArray(replacer)) {
1488
+ const keyToStr = v => typeof v === 'number' || v instanceof String || v instanceof Number;
1489
+
1490
+ const asStr = replacer.filter(keyToStr).map(String);
1491
+ if (asStr.length > 0) replacer = replacer.concat(asStr);
1492
+ }
1493
+ if (typeof keepUndefined !== 'boolean') keepUndefined = !!this.options.keepUndefined;
1494
+ const aliasNodes = [];
1495
+ const ctx = {
1496
+ keepUndefined,
1497
+
1498
+ onAlias(source) {
1499
+ const alias = new stringifyNumber.Alias(source);
1500
+ aliasNodes.push(alias);
1501
+ return alias;
1502
+ },
1503
+
1504
+ onTagObj,
1505
+ prevObjects: new Map(),
1506
+ replacer,
1507
+ schema: this.schema,
1508
+ wrapScalars: wrapScalars !== false
1509
+ };
1510
+ const node = stringifyNumber.createNode(value, tag, ctx);
1511
+
1512
+ for (const alias of aliasNodes) {
1513
+ // With circular references, the source node is only resolved after all of
1514
+ // its child nodes are. This is why anchors are set only after all of the
1515
+ // nodes have been created.
1516
+ alias.source = alias.source.node;
1517
+ let name = this.anchors.getName(alias.source);
1518
+
1519
+ if (!name) {
1520
+ name = this.anchors.newName();
1521
+ this.anchors.map[name] = alias.source;
1522
+ }
1523
+ }
1524
+
1525
+ return node;
1526
+ }
1527
+
1528
+ createPair(key, value, options = {}) {
1529
+ const k = this.createNode(key, options);
1530
+ const v = this.createNode(value, options);
1531
+ return new stringifyNumber.Pair(k, v);
1532
+ }
1533
+
1534
+ delete(key) {
1535
+ assertCollection(this.contents);
1536
+ return this.contents.delete(key);
1537
+ }
1538
+
1539
+ deleteIn(path) {
1540
+ if (stringifyNumber.isEmptyPath(path)) {
1541
+ if (this.contents == null) return false;
1542
+ this.contents = null;
1543
+ return true;
1544
+ }
1545
+
1546
+ assertCollection(this.contents);
1547
+ return this.contents.deleteIn(path);
1548
+ }
1549
+
1550
+ getDefaults() {
1551
+ return Document.defaults[this.version] || Document.defaults[this.options.version] || {};
1552
+ }
1553
+
1554
+ get(key, keepScalar) {
1555
+ return this.contents instanceof stringifyNumber.Collection ? this.contents.get(key, keepScalar) : undefined;
1556
+ }
1557
+
1558
+ getIn(path, keepScalar) {
1559
+ if (stringifyNumber.isEmptyPath(path)) return !keepScalar && this.contents instanceof stringifyNumber.Scalar ? this.contents.value : this.contents;
1560
+ return this.contents instanceof stringifyNumber.Collection ? this.contents.getIn(path, keepScalar) : undefined;
1561
+ }
1562
+
1563
+ has(key) {
1564
+ return this.contents instanceof stringifyNumber.Collection ? this.contents.has(key) : false;
1565
+ }
1566
+
1567
+ hasIn(path) {
1568
+ if (stringifyNumber.isEmptyPath(path)) return this.contents !== undefined;
1569
+ return this.contents instanceof stringifyNumber.Collection ? this.contents.hasIn(path) : false;
1570
+ }
1571
+
1572
+ set(key, value) {
1573
+ if (this.contents == null) {
1574
+ this.setSchema();
1575
+ this.contents = stringifyNumber.collectionFromPath(this.schema, [key], value);
1576
+ } else {
1577
+ assertCollection(this.contents);
1578
+ this.contents.set(key, value);
1579
+ }
1580
+ }
1581
+
1582
+ setIn(path, value) {
1583
+ if (stringifyNumber.isEmptyPath(path)) this.contents = value;else if (this.contents == null) {
1584
+ this.setSchema();
1585
+ this.contents = stringifyNumber.collectionFromPath(this.schema, path, value);
1586
+ } else {
1587
+ assertCollection(this.contents);
1588
+ this.contents.setIn(path, value);
1589
+ }
1590
+ }
1591
+
1592
+ setSchema(id, customTags) {
1593
+ if (!id && !customTags && this.schema) return;
1594
+ if (typeof id === 'number') id = id.toFixed(1);
1595
+
1596
+ if (id === '1.0' || id === '1.1' || id === '1.2') {
1597
+ if (this.version) this.version = id;else this.options.version = id;
1598
+ delete this.options.schema;
1599
+ } else if (id && typeof id === 'string') {
1600
+ this.options.schema = id;
1601
+ }
1602
+
1603
+ if (Array.isArray(customTags)) this.options.customTags = customTags;
1604
+ const opt = Object.assign({}, this.getDefaults(), this.options);
1605
+ this.schema = new Schema.Schema(opt);
1606
+ }
1607
+
1608
+ parse(node, prevDoc) {
1609
+ if (this.options.keepCstNodes) this.cstNode = node;
1610
+ if (this.options.keepNodeTypes) this.type = 'DOCUMENT';
1611
+ const {
1612
+ directives = [],
1613
+ contents = [],
1614
+ directivesEndMarker,
1615
+ error,
1616
+ valueRange
1617
+ } = node;
1618
+
1619
+ if (error) {
1620
+ if (!error.source) error.source = this;
1621
+ this.errors.push(error);
1622
+ }
1623
+
1624
+ parseDirectives(this, directives, prevDoc);
1625
+ if (directivesEndMarker) this.directivesEndMarker = true;
1626
+ this.range = valueRange ? [valueRange.start, valueRange.end] : null;
1627
+ this.setSchema();
1628
+ this.anchors._cstAliases = [];
1629
+ parseContents(this, contents);
1630
+ this.anchors.resolveNodes();
1631
+
1632
+ if (this.options.prettyErrors) {
1633
+ for (const error of this.errors) if (error instanceof _rollupPluginBabelHelpers.YAMLError) error.makePretty();
1634
+
1635
+ for (const warn of this.warnings) if (warn instanceof _rollupPluginBabelHelpers.YAMLError) warn.makePretty();
1636
+ }
1637
+
1638
+ return this;
1639
+ }
1640
+
1641
+ listNonDefaultTags() {
1642
+ return listTagNames(this.contents).filter(t => t.indexOf(_rollupPluginBabelHelpers.defaultTagPrefix) !== 0);
1643
+ }
1644
+
1645
+ setTagPrefix(handle, prefix) {
1646
+ if (handle[0] !== '!' || handle[handle.length - 1] !== '!') throw new Error('Handle must start and end with !');
1647
+
1648
+ if (prefix) {
1649
+ const prev = this.tagPrefixes.find(p => p.handle === handle);
1650
+ if (prev) prev.prefix = prefix;else this.tagPrefixes.push({
1651
+ handle,
1652
+ prefix
1653
+ });
1654
+ } else {
1655
+ this.tagPrefixes = this.tagPrefixes.filter(p => p.handle !== handle);
1656
+ }
1657
+ }
1658
+
1659
+ toJS({
1660
+ json,
1661
+ jsonArg,
1662
+ mapAsMap,
1663
+ onAnchor,
1664
+ reviver
1665
+ } = {}) {
1666
+ const anchorNodes = Object.values(this.anchors.map).map(node => [node, {
1667
+ alias: [],
1668
+ aliasCount: 0,
1669
+ count: 1
1670
+ }]);
1671
+ const anchors = anchorNodes.length > 0 ? new Map(anchorNodes) : null;
1672
+ const ctx = {
1673
+ anchors,
1674
+ doc: this,
1675
+ indentStep: ' ',
1676
+ keep: !json,
1677
+ mapAsMap: typeof mapAsMap === 'boolean' ? mapAsMap : !!this.options.mapAsMap,
1678
+ maxAliasCount: this.options.maxAliasCount,
1679
+ stringify // Requiring directly in Pair would create circular dependencies
1680
+
1681
+ };
1682
+ const res = stringifyNumber.toJS(this.contents, jsonArg || '', ctx);
1683
+ if (typeof onAnchor === 'function' && anchors) for (const {
1684
+ count,
1685
+ res
1686
+ } of anchors.values()) onAnchor(res, count);
1687
+ return typeof reviver === 'function' ? applyReviver(reviver, {
1688
+ '': res
1689
+ }, '', res) : res;
1690
+ }
1691
+
1692
+ toJSON(jsonArg, onAnchor) {
1693
+ return this.toJS({
1694
+ json: true,
1695
+ jsonArg,
1696
+ mapAsMap: false,
1697
+ onAnchor
1698
+ });
1699
+ }
1700
+
1701
+ toString() {
1702
+ if (this.errors.length > 0) throw new Error('Document with errors cannot be stringified');
1703
+ const indentSize = this.options.indent;
1704
+
1705
+ if (!Number.isInteger(indentSize) || indentSize <= 0) {
1706
+ const s = JSON.stringify(indentSize);
1707
+ throw new Error(`"indent" option must be a positive integer, not ${s}`);
1708
+ }
1709
+
1710
+ this.setSchema();
1711
+ const lines = [];
1712
+ let hasDirectives = false;
1713
+
1714
+ if (this.version) {
1715
+ let vd = '%YAML 1.2';
1716
+
1717
+ if (this.schema.name === 'yaml-1.1') {
1718
+ if (this.version === '1.0') vd = '%YAML:1.0';else if (this.version === '1.1') vd = '%YAML 1.1';
1719
+ }
1720
+
1721
+ lines.push(vd);
1722
+ hasDirectives = true;
1723
+ }
1724
+
1725
+ const tagNames = this.listNonDefaultTags();
1726
+ this.tagPrefixes.forEach(({
1727
+ handle,
1728
+ prefix
1729
+ }) => {
1730
+ if (tagNames.some(t => t.indexOf(prefix) === 0)) {
1731
+ lines.push(`%TAG ${handle} ${prefix}`);
1732
+ hasDirectives = true;
1733
+ }
1734
+ });
1735
+ if (hasDirectives || this.directivesEndMarker) lines.push('---');
1736
+
1737
+ if (this.commentBefore) {
1738
+ if (hasDirectives || !this.directivesEndMarker) lines.unshift('');
1739
+ lines.unshift(this.commentBefore.replace(/^/gm, '#'));
1740
+ }
1741
+
1742
+ const ctx = {
1743
+ anchors: Object.create(null),
1744
+ doc: this,
1745
+ indent: '',
1746
+ indentStep: ' '.repeat(indentSize),
1747
+ stringify // Requiring directly in nodes would create circular dependencies
1748
+
1749
+ };
1750
+ let chompKeep = false;
1751
+ let contentComment = null;
1752
+
1753
+ if (this.contents) {
1754
+ if (this.contents instanceof stringifyNumber.Node) {
1755
+ if (this.contents.spaceBefore && (hasDirectives || this.directivesEndMarker)) lines.push('');
1756
+ if (this.contents.commentBefore) lines.push(this.contents.commentBefore.replace(/^/gm, '#')); // top-level block scalars need to be indented if followed by a comment
1757
+
1758
+ ctx.forceBlockIndent = !!this.comment;
1759
+ contentComment = this.contents.comment;
1760
+ }
1761
+
1762
+ const onChompKeep = contentComment ? null : () => chompKeep = true;
1763
+ const body = stringify(this.contents, ctx, () => contentComment = null, onChompKeep);
1764
+ lines.push(stringifyNumber.addComment(body, '', contentComment));
1765
+ } else {
1766
+ lines.push(stringify(this.contents, ctx));
1767
+ }
1768
+
1769
+ if (this.comment) {
1770
+ if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '') lines.push('');
1771
+ lines.push(this.comment.replace(/^/gm, '#'));
1772
+ }
1773
+
1774
+ return lines.join('\n') + '\n';
1775
+ }
1776
+
1777
+ }
1778
+
1779
+ _rollupPluginBabelHelpers._defineProperty(Document, "defaults", documentOptions);
1780
+
1781
+ exports.Document = Document;
1782
+ exports.defaultOptions = defaultOptions;
1783
+ exports.scalarOptions = scalarOptions;