yaml 1.10.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.
Files changed (55) hide show
  1. package/README.md +10 -11
  2. package/browser/dist/Schema-ea978338.js +1211 -0
  3. package/browser/dist/{PlainValue-ff5147c6.js → errors-2634d01a.js} +8 -186
  4. package/browser/dist/index.js +1174 -121
  5. package/browser/dist/parse-cst.js +2 -1904
  6. package/browser/dist/parse-d1ba890f.js +2084 -0
  7. package/browser/dist/{resolveSeq-04825f30.js → stringifyNumber-d8af95b1.js} +197 -1072
  8. package/browser/dist/types.js +3 -4
  9. package/browser/dist/util.js +2 -2
  10. package/browser/index.js +1 -1
  11. package/dist/Document-f89a2614.js +1783 -0
  12. package/dist/Schema-807430ba.js +934 -0
  13. package/dist/{PlainValue-ec8e588e.js → _rollupPluginBabelHelpers-eed30217.js} +0 -167
  14. package/dist/index.js +58 -44
  15. package/dist/parse-3997f544.js +1915 -0
  16. package/dist/parse-cst.js +3 -1742
  17. package/dist/stringifyNumber-dea1120c.js +1286 -0
  18. package/dist/test-events.js +6 -7
  19. package/dist/types.js +16 -17
  20. package/dist/util.js +12 -14
  21. package/index.d.ts +103 -20
  22. package/index.js +1 -1
  23. package/package.json +16 -26
  24. package/types.d.ts +19 -47
  25. package/util.d.ts +2 -12
  26. package/util.js +0 -2
  27. package/util.mjs +0 -3
  28. package/browser/dist/Schema-2bf2c74e.js +0 -679
  29. package/browser/dist/legacy-exports.js +0 -3
  30. package/browser/dist/warnings-0e4b70d3.js +0 -499
  31. package/browser/map.js +0 -2
  32. package/browser/pair.js +0 -2
  33. package/browser/scalar.js +0 -2
  34. package/browser/schema.js +0 -9
  35. package/browser/seq.js +0 -2
  36. package/browser/types/binary.js +0 -8
  37. package/browser/types/omap.js +0 -3
  38. package/browser/types/pairs.js +0 -3
  39. package/browser/types/set.js +0 -3
  40. package/browser/types/timestamp.js +0 -10
  41. package/dist/Document-2cf6b08c.js +0 -757
  42. package/dist/Schema-42e9705c.js +0 -522
  43. package/dist/legacy-exports.js +0 -16
  44. package/dist/resolveSeq-4a68b39b.js +0 -2115
  45. package/dist/warnings-39684f17.js +0 -416
  46. package/map.js +0 -2
  47. package/pair.js +0 -2
  48. package/scalar.js +0 -2
  49. package/schema.js +0 -9
  50. package/seq.js +0 -2
  51. package/types/binary.js +0 -8
  52. package/types/omap.js +0 -3
  53. package/types/pairs.js +0 -3
  54. package/types/set.js +0 -3
  55. package/types/timestamp.js +0 -10
@@ -1,8 +1,8 @@
1
- import { d as defaultTagPrefix, _ as _createForOfIteratorHelper, a as _typeof, b as _createClass, c as _classCallCheck, e as _defineProperty, Y as YAMLSyntaxError, T as Type, f as YAMLWarning, g as YAMLSemanticError, h as _slicedToArray, i as YAMLError, j as _inherits, k as _createSuper } from './PlainValue-ff5147c6.js';
2
- import { parse as parse$1 } from './parse-cst.js';
3
- import { b as binaryOptions, a as boolOptions, i as intOptions, n as nullOptions, s as strOptions, N as Node, P as Pair, S as Scalar, c as stringifyString, A as Alias, Y as YAMLSeq, d as YAMLMap, M as Merge, C as Collection, r as resolveNode, e as isEmptyPath, t as toJSON, f as addComment } from './resolveSeq-04825f30.js';
4
- import { S as Schema } from './Schema-2bf2c74e.js';
5
- import { w as warn } from './warnings-0e4b70d3.js';
1
+ import { d as defaultTagPrefix, _ as _typeof, a as _createClass, b as _classCallCheck, c as _defineProperty, e as _slicedToArray, Y as YAMLSemanticError, T as Type, f as defaultTags, g as YAMLWarning, h as _createForOfIteratorHelper, i as YAMLSyntaxError, C as Char, j as YAMLReferenceError, k as YAMLError } from './errors-2634d01a.js';
2
+ import { P as PlainValue, D as Document$1, p as parse$1 } from './parse-d1ba890f.js';
3
+ export { p as parseCST } from './parse-d1ba890f.js';
4
+ import { b as binaryOptions, a as boolOptions, i as intOptions, n as nullOptions, s as strOptions, N as Node, P as Pair, S as Scalar, c as stringifyString, A as Alias, Y as YAMLSeq, d as YAMLMap, C as Collection, r as resolveScalar, e as createNode, f as isEmptyPath, g as collectionFromPath, t as toJS, h as addComment } from './stringifyNumber-d8af95b1.js';
5
+ import { M as Merge, a as MERGE_KEY, S as Schema } from './Schema-ea978338.js';
6
6
 
7
7
  var defaultOptions = {
8
8
  anchorPrefix: 'a',
@@ -11,11 +11,10 @@ var defaultOptions = {
11
11
  indentSeq: true,
12
12
  keepCstNodes: false,
13
13
  keepNodeTypes: true,
14
- keepBlobsInJSON: true,
14
+ keepUndefined: false,
15
15
  mapAsMap: false,
16
16
  maxAliasCount: 100,
17
- prettyErrors: false,
18
- // TODO Set true in v2
17
+ prettyErrors: true,
19
18
  simpleKeys: false,
20
19
  version: '1.2'
21
20
  };
@@ -87,6 +86,7 @@ var documentOptions = {
87
86
  '1.2': {
88
87
  schema: 'core',
89
88
  merge: false,
89
+ resolveKnownTags: true,
90
90
  tagPrefixes: [{
91
91
  handle: '!',
92
92
  prefix: '!'
@@ -145,10 +145,10 @@ function getTagObject(tags, item) {
145
145
  var tagObj, obj;
146
146
 
147
147
  if (item instanceof Scalar) {
148
- obj = item.value; // TODO: deprecate/remove class check
148
+ obj = item.value;
149
149
 
150
150
  var _match = tags.filter(function (t) {
151
- return t.identify && t.identify(obj) || t.class && obj instanceof t.class;
151
+ return t.identify && t.identify(obj);
152
152
  });
153
153
 
154
154
  tagObj = _match.find(function (t) {
@@ -193,40 +193,16 @@ function stringifyProps(node, tagObj, _ref) {
193
193
  }
194
194
 
195
195
  function stringify(item, ctx, onComment, onChompKeep) {
196
- var _ctx$doc = ctx.doc,
197
- anchors = _ctx$doc.anchors,
198
- schema = _ctx$doc.schema;
196
+ var schema = ctx.doc.schema;
199
197
  var tagObj;
200
198
 
201
199
  if (!(item instanceof Node)) {
202
- var createCtx = {
203
- aliasNodes: [],
200
+ item = ctx.doc.createNode(item, {
204
201
  onTagObj: function onTagObj(o) {
205
202
  return tagObj = o;
206
203
  },
207
- prevObjects: new Map()
208
- };
209
- item = schema.createNode(item, true, null, createCtx);
210
-
211
- var _iterator = _createForOfIteratorHelper(createCtx.aliasNodes),
212
- _step;
213
-
214
- try {
215
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
216
- var alias = _step.value;
217
- alias.source = alias.source.node;
218
- var name = anchors.getName(alias.source);
219
-
220
- if (!name) {
221
- name = anchors.newName();
222
- anchors.map[name] = alias.source;
223
- }
224
- }
225
- } catch (err) {
226
- _iterator.e(err);
227
- } finally {
228
- _iterator.f();
229
- }
204
+ wrapScalars: true
205
+ });
230
206
  }
231
207
 
232
208
  if (item instanceof Pair) return item.toString(ctx, onComment, onChompKeep);
@@ -249,7 +225,7 @@ var Anchors = /*#__PURE__*/function () {
249
225
  function Anchors(prefix) {
250
226
  _classCallCheck(this, Anchors);
251
227
 
252
- _defineProperty(this, "map", {});
228
+ _defineProperty(this, "map", Object.create(null));
253
229
 
254
230
  this.prefix = prefix;
255
231
  }
@@ -366,6 +342,58 @@ var Anchors = /*#__PURE__*/function () {
366
342
  return Anchors;
367
343
  }();
368
344
 
345
+ /**
346
+ * Applies the JSON.parse reviver algorithm as defined in the ECMA-262 spec,
347
+ * in section 24.5.1.1 "Runtime Semantics: InternalizeJSONProperty" of the
348
+ * 2021 edition: https://tc39.es/ecma262/#sec-json.parse
349
+ *
350
+ * Includes extensions for handling Map and Set objects.
351
+ */
352
+ function applyReviver(reviver, obj, key, val) {
353
+ if (val && _typeof(val) === 'object') {
354
+ if (Array.isArray(val)) {
355
+ for (var i = 0, len = val.length; i < len; ++i) {
356
+ var v0 = val[i];
357
+ var v1 = applyReviver(reviver, val, String(i), v0);
358
+ if (v1 === undefined) delete val[i];else if (v1 !== v0) val[i] = v1;
359
+ }
360
+ } else if (val instanceof Map) {
361
+ for (var _i = 0, _Array$from = Array.from(val.keys()); _i < _Array$from.length; _i++) {
362
+ var k = _Array$from[_i];
363
+
364
+ var _v = val.get(k);
365
+
366
+ var _v2 = applyReviver(reviver, val, k, _v);
367
+
368
+ if (_v2 === undefined) val.delete(k);else if (_v2 !== _v) val.set(k, _v2);
369
+ }
370
+ } else if (val instanceof Set) {
371
+ for (var _i2 = 0, _Array$from2 = Array.from(val); _i2 < _Array$from2.length; _i2++) {
372
+ var _v3 = _Array$from2[_i2];
373
+
374
+ var _v4 = applyReviver(reviver, val, _v3, _v3);
375
+
376
+ if (_v4 === undefined) val.delete(_v3);else if (_v4 !== _v3) {
377
+ val.delete(_v3);
378
+ val.add(_v4);
379
+ }
380
+ }
381
+ } else {
382
+ for (var _i3 = 0, _Object$entries = Object.entries(val); _i3 < _Object$entries.length; _i3++) {
383
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i3], 2),
384
+ _k = _Object$entries$_i[0],
385
+ _v5 = _Object$entries$_i[1];
386
+
387
+ var _v6 = applyReviver(reviver, val, _k, _v5);
388
+
389
+ if (_v6 === undefined) delete val[_k];else if (_v6 !== _v5) val[_k] = _v6;
390
+ }
391
+ }
392
+ }
393
+
394
+ return reviver.call(obj, key, val);
395
+ }
396
+
369
397
  var visit = function visit(node, tags) {
370
398
  if (node && _typeof(node) === 'object') {
371
399
  var tag = node.tag;
@@ -390,6 +418,934 @@ var listTagNames = function listTagNames(node) {
390
418
  return Object.keys(visit(node, {}));
391
419
  };
392
420
 
421
+ function resolveTagHandle(doc, node) {
422
+ var _node$tag = node.tag,
423
+ handle = _node$tag.handle,
424
+ suffix = _node$tag.suffix;
425
+ var prefix = doc.tagPrefixes.find(function (p) {
426
+ return p.handle === handle;
427
+ });
428
+
429
+ if (!prefix) {
430
+ var dtp = doc.getDefaults().tagPrefixes;
431
+ if (dtp) prefix = dtp.find(function (p) {
432
+ return p.handle === handle;
433
+ });
434
+ if (!prefix) throw new YAMLSemanticError(node, "The ".concat(handle, " tag handle is non-default and was not declared."));
435
+ }
436
+
437
+ if (!suffix) throw new YAMLSemanticError(node, "The ".concat(handle, " tag has no suffix."));
438
+
439
+ if (handle === '!' && (doc.version || doc.options.version) === '1.0') {
440
+ if (suffix[0] === '^') {
441
+ doc.warnings.push(new YAMLWarning(node, 'YAML 1.0 ^ tag expansion is not supported'));
442
+ return suffix;
443
+ }
444
+
445
+ if (/[:/]/.test(suffix)) {
446
+ // word/foo -> tag:word.yaml.org,2002:foo
447
+ var vocab = suffix.match(/^([a-z0-9-]+)\/(.*)/i);
448
+ return vocab ? "tag:".concat(vocab[1], ".yaml.org,2002:").concat(vocab[2]) : "tag:".concat(suffix);
449
+ }
450
+ }
451
+
452
+ return prefix.prefix + decodeURIComponent(suffix);
453
+ }
454
+
455
+ function resolveTagName(doc, node) {
456
+ var tag = node.tag,
457
+ type = node.type;
458
+ var nonSpecific = false;
459
+
460
+ if (tag) {
461
+ var handle = tag.handle,
462
+ suffix = tag.suffix,
463
+ verbatim = tag.verbatim;
464
+
465
+ if (verbatim) {
466
+ if (verbatim !== '!' && verbatim !== '!!') return verbatim;
467
+ var msg = "Verbatim tags aren't resolved, so ".concat(verbatim, " is invalid.");
468
+ doc.errors.push(new YAMLSemanticError(node, msg));
469
+ } else if (handle === '!' && !suffix) {
470
+ nonSpecific = true;
471
+ } else {
472
+ try {
473
+ return resolveTagHandle(doc, node);
474
+ } catch (error) {
475
+ doc.errors.push(error);
476
+ }
477
+ }
478
+ }
479
+
480
+ switch (type) {
481
+ case Type.BLOCK_FOLDED:
482
+ case Type.BLOCK_LITERAL:
483
+ case Type.QUOTE_DOUBLE:
484
+ case Type.QUOTE_SINGLE:
485
+ return defaultTags.STR;
486
+
487
+ case Type.FLOW_MAP:
488
+ case Type.MAP:
489
+ return defaultTags.MAP;
490
+
491
+ case Type.FLOW_SEQ:
492
+ case Type.SEQ:
493
+ return defaultTags.SEQ;
494
+
495
+ case Type.PLAIN:
496
+ return nonSpecific ? defaultTags.STR : null;
497
+
498
+ default:
499
+ return null;
500
+ }
501
+ }
502
+
503
+ function checkFlowCollectionEnd(errors, cst) {
504
+ var char, name;
505
+
506
+ switch (cst.type) {
507
+ case Type.FLOW_MAP:
508
+ char = '}';
509
+ name = 'flow map';
510
+ break;
511
+
512
+ case Type.FLOW_SEQ:
513
+ char = ']';
514
+ name = 'flow sequence';
515
+ break;
516
+
517
+ default:
518
+ errors.push(new YAMLSemanticError(cst, 'Not a flow collection!?'));
519
+ return;
520
+ }
521
+
522
+ var lastItem;
523
+
524
+ for (var i = cst.items.length - 1; i >= 0; --i) {
525
+ var item = cst.items[i];
526
+
527
+ if (!item || item.type !== Type.COMMENT) {
528
+ lastItem = item;
529
+ break;
530
+ }
531
+ }
532
+
533
+ if (lastItem && lastItem.char !== char) {
534
+ var msg = "Expected ".concat(name, " to end with ").concat(char);
535
+ var err;
536
+
537
+ if (typeof lastItem.offset === 'number') {
538
+ err = new YAMLSemanticError(cst, msg);
539
+ err.offset = lastItem.offset + 1;
540
+ } else {
541
+ err = new YAMLSemanticError(lastItem, msg);
542
+ if (lastItem.range && lastItem.range.end) err.offset = lastItem.range.end - lastItem.range.start;
543
+ }
544
+
545
+ errors.push(err);
546
+ }
547
+ }
548
+ function checkFlowCommentSpace(errors, comment) {
549
+ var prev = comment.context.src[comment.range.start - 1];
550
+
551
+ if (prev !== '\n' && prev !== '\t' && prev !== ' ') {
552
+ var msg = 'Comments must be separated from other tokens by white space characters';
553
+ errors.push(new YAMLSemanticError(comment, msg));
554
+ }
555
+ }
556
+ function getLongKeyError(source, key) {
557
+ var sk = String(key);
558
+ var k = sk.substr(0, 8) + '...' + sk.substr(-8);
559
+ return new YAMLSemanticError(source, "The \"".concat(k, "\" key is too long"));
560
+ }
561
+ function resolveComments(collection, comments) {
562
+ var _iterator = _createForOfIteratorHelper(comments),
563
+ _step;
564
+
565
+ try {
566
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
567
+ var _step$value = _step.value,
568
+ afterKey = _step$value.afterKey,
569
+ before = _step$value.before,
570
+ comment = _step$value.comment;
571
+ var item = collection.items[before];
572
+
573
+ if (!item) {
574
+ if (comment !== undefined) {
575
+ if (collection.comment) collection.comment += '\n' + comment;else collection.comment = comment;
576
+ }
577
+ } else {
578
+ if (afterKey && item.value) item = item.value;
579
+
580
+ if (comment === undefined) {
581
+ if (afterKey || !item.commentBefore) item.spaceBefore = true;
582
+ } else {
583
+ if (item.commentBefore) item.commentBefore += '\n' + comment;else item.commentBefore = comment;
584
+ }
585
+ }
586
+ }
587
+ } catch (err) {
588
+ _iterator.e(err);
589
+ } finally {
590
+ _iterator.f();
591
+ }
592
+ }
593
+
594
+ function resolveMap(doc, cst) {
595
+ var _ref = cst.type === Type.FLOW_MAP ? resolveFlowMapItems(doc, cst) : resolveBlockMapItems(doc, cst),
596
+ comments = _ref.comments,
597
+ items = _ref.items;
598
+
599
+ var map = new YAMLMap(doc.schema);
600
+ map.items = items;
601
+ resolveComments(map, comments);
602
+ var hasCollectionKey = false;
603
+
604
+ for (var i = 0; i < items.length; ++i) {
605
+ var iKey = items[i].key;
606
+ if (iKey instanceof Collection) hasCollectionKey = true;
607
+
608
+ if (doc.schema.merge && iKey && iKey.value === MERGE_KEY) {
609
+ items[i] = new Merge(items[i]);
610
+ var sources = items[i].value.items;
611
+ var error = null;
612
+ sources.some(function (node) {
613
+ if (node instanceof Alias) {
614
+ // During parsing, alias sources are CST nodes; to account for
615
+ // circular references their resolved values can't be used here.
616
+ var type = node.source.type;
617
+ if (type === Type.MAP || type === Type.FLOW_MAP) return false;
618
+ return error = 'Merge nodes aliases can only point to maps';
619
+ }
620
+
621
+ return error = 'Merge nodes can only have Alias nodes as values';
622
+ });
623
+ if (error) doc.errors.push(new YAMLSemanticError(cst, error));
624
+ } else {
625
+ for (var j = i + 1; j < items.length; ++j) {
626
+ var jKey = items[j].key;
627
+
628
+ if (iKey === jKey || iKey && jKey && Object.prototype.hasOwnProperty.call(iKey, 'value') && iKey.value === jKey.value) {
629
+ var msg = "Map keys must be unique; \"".concat(iKey, "\" is repeated");
630
+ doc.errors.push(new YAMLSemanticError(cst, msg));
631
+ break;
632
+ }
633
+ }
634
+ }
635
+ }
636
+
637
+ if (hasCollectionKey && !doc.options.mapAsMap) {
638
+ var warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
639
+ doc.warnings.push(new YAMLWarning(cst, warn));
640
+ }
641
+
642
+ cst.resolved = map;
643
+ return map;
644
+ }
645
+
646
+ var valueHasPairComment = function valueHasPairComment(_ref2) {
647
+ var _ref2$context = _ref2.context,
648
+ lineStart = _ref2$context.lineStart,
649
+ node = _ref2$context.node,
650
+ src = _ref2$context.src,
651
+ props = _ref2.props;
652
+ if (props.length === 0) return false;
653
+ var start = props[0].start;
654
+ if (node && start > node.valueRange.start) return false;
655
+ if (src[start] !== Char.COMMENT) return false;
656
+
657
+ for (var i = lineStart; i < start; ++i) {
658
+ if (src[i] === '\n') return false;
659
+ }
660
+
661
+ return true;
662
+ };
663
+
664
+ function resolvePairComment(item, pair) {
665
+ if (!valueHasPairComment(item)) return;
666
+ var comment = item.getPropValue(0, Char.COMMENT, true);
667
+ var found = false;
668
+ var cb = pair.value.commentBefore;
669
+
670
+ if (cb && cb.startsWith(comment)) {
671
+ pair.value.commentBefore = cb.substr(comment.length + 1);
672
+ found = true;
673
+ } else {
674
+ var cc = pair.value.comment;
675
+
676
+ if (!item.node && cc && cc.startsWith(comment)) {
677
+ pair.value.comment = cc.substr(comment.length + 1);
678
+ found = true;
679
+ }
680
+ }
681
+
682
+ if (found) pair.comment = comment;
683
+ }
684
+
685
+ function resolveBlockMapItems(doc, cst) {
686
+ var comments = [];
687
+ var items = [];
688
+ var key = undefined;
689
+ var keyStart = null;
690
+
691
+ for (var i = 0; i < cst.items.length; ++i) {
692
+ var item = cst.items[i];
693
+
694
+ switch (item.type) {
695
+ case Type.BLANK_LINE:
696
+ comments.push({
697
+ afterKey: !!key,
698
+ before: items.length
699
+ });
700
+ break;
701
+
702
+ case Type.COMMENT:
703
+ comments.push({
704
+ afterKey: !!key,
705
+ before: items.length,
706
+ comment: item.comment
707
+ });
708
+ break;
709
+
710
+ case Type.MAP_KEY:
711
+ if (key !== undefined) items.push(new Pair(key));
712
+ if (item.error) doc.errors.push(item.error);
713
+ key = resolveNode(doc, item.node);
714
+ keyStart = null;
715
+ break;
716
+
717
+ case Type.MAP_VALUE:
718
+ {
719
+ if (key === undefined) key = null;
720
+ if (item.error) doc.errors.push(item.error);
721
+
722
+ if (!item.context.atLineStart && item.node && item.node.type === Type.MAP && !item.node.context.atLineStart) {
723
+ var msg = 'Nested mappings are not allowed in compact mappings';
724
+ doc.errors.push(new YAMLSemanticError(item.node, msg));
725
+ }
726
+
727
+ var valueNode = item.node;
728
+
729
+ if (!valueNode && item.props.length > 0) {
730
+ // Comments on an empty mapping value need to be preserved, so we
731
+ // need to construct a minimal empty node here to use instead of the
732
+ // missing `item.node`. -- eemeli/yaml#19
733
+ valueNode = new PlainValue(Type.PLAIN, []);
734
+ valueNode.context = {
735
+ parent: item,
736
+ src: item.context.src
737
+ };
738
+ var pos = item.range.start + 1;
739
+ valueNode.range = {
740
+ start: pos,
741
+ end: pos
742
+ };
743
+ valueNode.valueRange = {
744
+ start: pos,
745
+ end: pos
746
+ };
747
+
748
+ if (typeof item.range.origStart === 'number') {
749
+ var origPos = item.range.origStart + 1;
750
+ valueNode.range.origStart = valueNode.range.origEnd = origPos;
751
+ valueNode.valueRange.origStart = valueNode.valueRange.origEnd = origPos;
752
+ }
753
+ }
754
+
755
+ var pair = new Pair(key, resolveNode(doc, valueNode));
756
+ resolvePairComment(item, pair);
757
+ items.push(pair);
758
+
759
+ if (key && typeof keyStart === 'number') {
760
+ if (item.range.start > keyStart + 1024) doc.errors.push(getLongKeyError(cst, key));
761
+ }
762
+
763
+ key = undefined;
764
+ keyStart = null;
765
+ }
766
+ break;
767
+
768
+ default:
769
+ if (key !== undefined) items.push(new Pair(key));
770
+ key = resolveNode(doc, item);
771
+ keyStart = item.range.start;
772
+ if (item.error) doc.errors.push(item.error);
773
+
774
+ next: for (var j = i + 1;; ++j) {
775
+ var nextItem = cst.items[j];
776
+
777
+ switch (nextItem && nextItem.type) {
778
+ case Type.BLANK_LINE:
779
+ case Type.COMMENT:
780
+ continue next;
781
+
782
+ case Type.MAP_VALUE:
783
+ break next;
784
+
785
+ default:
786
+ {
787
+ var _msg = 'Implicit map keys need to be followed by map values';
788
+ doc.errors.push(new YAMLSemanticError(item, _msg));
789
+ break next;
790
+ }
791
+ }
792
+ }
793
+
794
+ if (item.valueRangeContainsNewline) {
795
+ var _msg2 = 'Implicit map keys need to be on a single line';
796
+ doc.errors.push(new YAMLSemanticError(item, _msg2));
797
+ }
798
+
799
+ }
800
+ }
801
+
802
+ if (key !== undefined) items.push(new Pair(key));
803
+ return {
804
+ comments: comments,
805
+ items: items
806
+ };
807
+ }
808
+
809
+ function resolveFlowMapItems(doc, cst) {
810
+ var comments = [];
811
+ var items = [];
812
+ var key = undefined;
813
+ var explicitKey = false;
814
+ var next = '{';
815
+
816
+ for (var i = 0; i < cst.items.length; ++i) {
817
+ var item = cst.items[i];
818
+
819
+ if (typeof item.char === 'string') {
820
+ var char = item.char,
821
+ offset = item.offset;
822
+
823
+ if (char === '?' && key === undefined && !explicitKey) {
824
+ explicitKey = true;
825
+ next = ':';
826
+ continue;
827
+ }
828
+
829
+ if (char === ':') {
830
+ if (key === undefined) key = null;
831
+
832
+ if (next === ':') {
833
+ next = ',';
834
+ continue;
835
+ }
836
+ } else {
837
+ if (explicitKey) {
838
+ if (key === undefined && char !== ',') key = null;
839
+ explicitKey = false;
840
+ }
841
+
842
+ if (key !== undefined) {
843
+ items.push(new Pair(key));
844
+ key = undefined;
845
+
846
+ if (char === ',') {
847
+ next = ':';
848
+ continue;
849
+ }
850
+ }
851
+ }
852
+
853
+ if (char === '}') {
854
+ if (i === cst.items.length - 1) continue;
855
+ } else if (char === next) {
856
+ next = ':';
857
+ continue;
858
+ }
859
+
860
+ var msg = "Flow map contains an unexpected ".concat(char);
861
+ var err = new YAMLSyntaxError(cst, msg);
862
+ err.offset = offset;
863
+ doc.errors.push(err);
864
+ } else if (item.type === Type.BLANK_LINE) {
865
+ comments.push({
866
+ afterKey: !!key,
867
+ before: items.length
868
+ });
869
+ } else if (item.type === Type.COMMENT) {
870
+ checkFlowCommentSpace(doc.errors, item);
871
+ comments.push({
872
+ afterKey: !!key,
873
+ before: items.length,
874
+ comment: item.comment
875
+ });
876
+ } else if (key === undefined) {
877
+ if (next === ',') doc.errors.push(new YAMLSemanticError(item, 'Separator , missing in flow map'));
878
+ key = resolveNode(doc, item);
879
+ } else {
880
+ if (next !== ',') doc.errors.push(new YAMLSemanticError(item, 'Indicator : missing in flow map entry'));
881
+ items.push(new Pair(key, resolveNode(doc, item)));
882
+ key = undefined;
883
+ explicitKey = false;
884
+ }
885
+ }
886
+
887
+ checkFlowCollectionEnd(doc.errors, cst);
888
+ if (key !== undefined) items.push(new Pair(key));
889
+ return {
890
+ comments: comments,
891
+ items: items
892
+ };
893
+ }
894
+
895
+ function resolveSeq(doc, cst) {
896
+ var _ref = cst.type === Type.FLOW_SEQ ? resolveFlowSeqItems(doc, cst) : resolveBlockSeqItems(doc, cst),
897
+ comments = _ref.comments,
898
+ items = _ref.items;
899
+
900
+ var seq = new YAMLSeq(doc.schema);
901
+ seq.items = items;
902
+ resolveComments(seq, comments);
903
+
904
+ if (!doc.options.mapAsMap && items.some(function (it) {
905
+ return it instanceof Pair && it.key instanceof Collection;
906
+ })) {
907
+ var warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
908
+ doc.warnings.push(new YAMLWarning(cst, warn));
909
+ }
910
+
911
+ cst.resolved = seq;
912
+ return seq;
913
+ }
914
+
915
+ function resolveBlockSeqItems(doc, cst) {
916
+ var comments = [];
917
+ var items = [];
918
+
919
+ for (var i = 0; i < cst.items.length; ++i) {
920
+ var item = cst.items[i];
921
+
922
+ switch (item.type) {
923
+ case Type.BLANK_LINE:
924
+ comments.push({
925
+ before: items.length
926
+ });
927
+ break;
928
+
929
+ case Type.COMMENT:
930
+ comments.push({
931
+ comment: item.comment,
932
+ before: items.length
933
+ });
934
+ break;
935
+
936
+ case Type.SEQ_ITEM:
937
+ if (item.error) doc.errors.push(item.error);
938
+ items.push(resolveNode(doc, item.node));
939
+
940
+ if (item.hasProps) {
941
+ var msg = 'Sequence items cannot have tags or anchors before the - indicator';
942
+ doc.errors.push(new YAMLSemanticError(item, msg));
943
+ }
944
+
945
+ break;
946
+
947
+ default:
948
+ if (item.error) doc.errors.push(item.error);
949
+ doc.errors.push(new YAMLSyntaxError(item, "Unexpected ".concat(item.type, " node in sequence")));
950
+ }
951
+ }
952
+
953
+ return {
954
+ comments: comments,
955
+ items: items
956
+ };
957
+ }
958
+
959
+ function resolveFlowSeqItems(doc, cst) {
960
+ var comments = [];
961
+ var items = [];
962
+ var explicitKey = false;
963
+ var key = undefined;
964
+ var keyStart = null;
965
+ var next = '[';
966
+ var prevItem = null;
967
+
968
+ for (var i = 0; i < cst.items.length; ++i) {
969
+ var item = cst.items[i];
970
+
971
+ if (typeof item.char === 'string') {
972
+ var char = item.char,
973
+ offset = item.offset;
974
+
975
+ if (char !== ':' && (explicitKey || key !== undefined)) {
976
+ if (explicitKey && key === undefined) key = next ? items.pop() : null;
977
+ items.push(new Pair(key));
978
+ explicitKey = false;
979
+ key = undefined;
980
+ keyStart = null;
981
+ }
982
+
983
+ if (char === next) {
984
+ next = null;
985
+ } else if (!next && char === '?') {
986
+ explicitKey = true;
987
+ } else if (next !== '[' && char === ':' && key === undefined) {
988
+ if (next === ',') {
989
+ key = items.pop();
990
+
991
+ if (key instanceof Pair) {
992
+ var msg = 'Chaining flow sequence pairs is invalid';
993
+ var err = new YAMLSemanticError(cst, msg);
994
+ err.offset = offset;
995
+ doc.errors.push(err);
996
+ }
997
+
998
+ if (!explicitKey && typeof keyStart === 'number') {
999
+ var keyEnd = item.range ? item.range.start : item.offset;
1000
+ if (keyEnd > keyStart + 1024) doc.errors.push(getLongKeyError(cst, key));
1001
+ var src = prevItem.context.src;
1002
+
1003
+ for (var _i = keyStart; _i < keyEnd; ++_i) {
1004
+ if (src[_i] === '\n') {
1005
+ var _msg = 'Implicit keys of flow sequence pairs need to be on a single line';
1006
+ doc.errors.push(new YAMLSemanticError(prevItem, _msg));
1007
+ break;
1008
+ }
1009
+ }
1010
+ }
1011
+ } else {
1012
+ key = null;
1013
+ }
1014
+
1015
+ keyStart = null;
1016
+ explicitKey = false;
1017
+ next = null;
1018
+ } else if (next === '[' || char !== ']' || i < cst.items.length - 1) {
1019
+ var _msg2 = "Flow sequence contains an unexpected ".concat(char);
1020
+
1021
+ var _err = new YAMLSyntaxError(cst, _msg2);
1022
+
1023
+ _err.offset = offset;
1024
+ doc.errors.push(_err);
1025
+ }
1026
+ } else if (item.type === Type.BLANK_LINE) {
1027
+ comments.push({
1028
+ before: items.length
1029
+ });
1030
+ } else if (item.type === Type.COMMENT) {
1031
+ checkFlowCommentSpace(doc.errors, item);
1032
+ comments.push({
1033
+ comment: item.comment,
1034
+ before: items.length
1035
+ });
1036
+ } else {
1037
+ if (next) {
1038
+ var _msg3 = "Expected a ".concat(next, " in flow sequence");
1039
+
1040
+ doc.errors.push(new YAMLSemanticError(item, _msg3));
1041
+ }
1042
+
1043
+ var value = resolveNode(doc, item);
1044
+
1045
+ if (key === undefined) {
1046
+ items.push(value);
1047
+ prevItem = item;
1048
+ } else {
1049
+ items.push(new Pair(key, value));
1050
+ key = undefined;
1051
+ }
1052
+
1053
+ keyStart = item.range.start;
1054
+ next = ',';
1055
+ }
1056
+ }
1057
+
1058
+ checkFlowCollectionEnd(doc.errors, cst);
1059
+ if (key !== undefined) items.push(new Pair(key));
1060
+ return {
1061
+ comments: comments,
1062
+ items: items
1063
+ };
1064
+ }
1065
+
1066
+ function resolveByTagName(_ref, tagName, value, onError) {
1067
+ var knownTags = _ref.knownTags,
1068
+ tags = _ref.tags;
1069
+ var matchWithTest = [];
1070
+
1071
+ var _iterator = _createForOfIteratorHelper(tags),
1072
+ _step;
1073
+
1074
+ try {
1075
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1076
+ var tag = _step.value;
1077
+
1078
+ if (tag.tag === tagName) {
1079
+ if (tag.test) {
1080
+ if (typeof value === 'string') matchWithTest.push(tag);else onError("The tag ".concat(tagName, " cannot be applied to a collection"));
1081
+ } else {
1082
+ var _res = tag.resolve(value, onError);
1083
+
1084
+ return _res instanceof Collection ? _res : new Scalar(_res);
1085
+ }
1086
+ }
1087
+ }
1088
+ } catch (err) {
1089
+ _iterator.e(err);
1090
+ } finally {
1091
+ _iterator.f();
1092
+ }
1093
+
1094
+ if (matchWithTest.length > 0) return resolveScalar(value, matchWithTest);
1095
+ var kt = knownTags[tagName];
1096
+
1097
+ if (kt) {
1098
+ tags.push(Object.assign({}, kt, {
1099
+ default: false,
1100
+ test: undefined
1101
+ }));
1102
+ var res = kt.resolve(value, onError);
1103
+ return res instanceof Collection ? res : new Scalar(res);
1104
+ }
1105
+
1106
+ return null;
1107
+ }
1108
+
1109
+ function resolveTag(doc, node, tagName) {
1110
+ var MAP = defaultTags.MAP,
1111
+ SEQ = defaultTags.SEQ,
1112
+ STR = defaultTags.STR;
1113
+ var value, fallback;
1114
+
1115
+ var onError = function onError(message) {
1116
+ return doc.errors.push(new YAMLSemanticError(node, message));
1117
+ };
1118
+
1119
+ try {
1120
+ switch (node.type) {
1121
+ case Type.FLOW_MAP:
1122
+ case Type.MAP:
1123
+ value = resolveMap(doc, node);
1124
+ fallback = MAP;
1125
+ if (tagName === SEQ || tagName === STR) onError("The tag ".concat(tagName, " cannot be applied to a mapping"));
1126
+ break;
1127
+
1128
+ case Type.FLOW_SEQ:
1129
+ case Type.SEQ:
1130
+ value = resolveSeq(doc, node);
1131
+ fallback = SEQ;
1132
+ if (tagName === MAP || tagName === STR) onError("The tag ".concat(tagName, " cannot be applied to a sequence"));
1133
+ break;
1134
+
1135
+ default:
1136
+ value = node.strValue || '';
1137
+
1138
+ if (typeof value !== 'string') {
1139
+ value.errors.forEach(function (error) {
1140
+ return doc.errors.push(error);
1141
+ });
1142
+ value = value.str;
1143
+ }
1144
+
1145
+ if (tagName === MAP || tagName === SEQ) onError("The tag ".concat(tagName, " cannot be applied to a scalar"));
1146
+ fallback = STR;
1147
+ }
1148
+
1149
+ var res = resolveByTagName(doc.schema, tagName, value, onError);
1150
+
1151
+ if (res) {
1152
+ if (tagName && node.tag) res.tag = tagName;
1153
+ return res;
1154
+ }
1155
+ } catch (error) {
1156
+ /* istanbul ignore if */
1157
+ if (!error.source) error.source = node;
1158
+ doc.errors.push(error);
1159
+ return null;
1160
+ }
1161
+
1162
+ try {
1163
+ if (!fallback) throw new Error("The tag ".concat(tagName, " is unavailable"));
1164
+ var msg = "The tag ".concat(tagName, " is unavailable, falling back to ").concat(fallback);
1165
+ doc.warnings.push(new YAMLWarning(node, msg));
1166
+
1167
+ var _res2 = resolveByTagName(doc.schema, fallback, value, onError);
1168
+
1169
+ _res2.tag = tagName;
1170
+ return _res2;
1171
+ } catch (error) {
1172
+ var refError = new YAMLReferenceError(node, error.message);
1173
+ refError.stack = error.stack;
1174
+ doc.errors.push(refError);
1175
+ return null;
1176
+ }
1177
+ }
1178
+
1179
+ var isCollectionItem = function isCollectionItem(node) {
1180
+ if (!node) return false;
1181
+ var type = node.type;
1182
+ return type === Type.MAP_KEY || type === Type.MAP_VALUE || type === Type.SEQ_ITEM;
1183
+ };
1184
+
1185
+ function resolveNodeProps(errors, node) {
1186
+ var comments = {
1187
+ before: [],
1188
+ after: []
1189
+ };
1190
+ var hasAnchor = false;
1191
+ var hasTag = false;
1192
+ var props = isCollectionItem(node.context.parent) ? node.context.parent.props.concat(node.props) : node.props;
1193
+
1194
+ var _iterator = _createForOfIteratorHelper(props),
1195
+ _step;
1196
+
1197
+ try {
1198
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1199
+ var _step$value = _step.value,
1200
+ start = _step$value.start,
1201
+ end = _step$value.end;
1202
+
1203
+ switch (node.context.src[start]) {
1204
+ case Char.COMMENT:
1205
+ {
1206
+ if (!node.commentHasRequiredWhitespace(start)) {
1207
+ var msg = 'Comments must be separated from other tokens by white space characters';
1208
+ errors.push(new YAMLSemanticError(node, msg));
1209
+ }
1210
+
1211
+ var header = node.header,
1212
+ valueRange = node.valueRange;
1213
+ var cc = valueRange && (start > valueRange.start || header && start > header.start) ? comments.after : comments.before;
1214
+ cc.push(node.context.src.slice(start + 1, end));
1215
+ break;
1216
+ }
1217
+ // Actual anchor & tag resolution is handled by schema, here we just complain
1218
+
1219
+ case Char.ANCHOR:
1220
+ if (hasAnchor) {
1221
+ var _msg = 'A node can have at most one anchor';
1222
+ errors.push(new YAMLSemanticError(node, _msg));
1223
+ }
1224
+
1225
+ hasAnchor = true;
1226
+ break;
1227
+
1228
+ case Char.TAG:
1229
+ if (hasTag) {
1230
+ var _msg2 = 'A node can have at most one tag';
1231
+ errors.push(new YAMLSemanticError(node, _msg2));
1232
+ }
1233
+
1234
+ hasTag = true;
1235
+ break;
1236
+ }
1237
+ }
1238
+ } catch (err) {
1239
+ _iterator.e(err);
1240
+ } finally {
1241
+ _iterator.f();
1242
+ }
1243
+
1244
+ return {
1245
+ comments: comments,
1246
+ hasAnchor: hasAnchor,
1247
+ hasTag: hasTag
1248
+ };
1249
+ }
1250
+
1251
+ function resolveNodeValue(doc, node) {
1252
+ var anchors = doc.anchors,
1253
+ errors = doc.errors,
1254
+ schema = doc.schema;
1255
+
1256
+ if (node.type === Type.ALIAS) {
1257
+ var name = node.rawValue;
1258
+ var src = anchors.getNode(name);
1259
+
1260
+ if (!src) {
1261
+ var msg = "Aliased anchor not found: ".concat(name);
1262
+ errors.push(new YAMLReferenceError(node, msg));
1263
+ return null;
1264
+ } // Lazy resolution for circular references
1265
+
1266
+
1267
+ var res = new Alias(src);
1268
+
1269
+ anchors._cstAliases.push(res);
1270
+
1271
+ return res;
1272
+ }
1273
+
1274
+ var tagName = resolveTagName(doc, node);
1275
+ if (tagName) return resolveTag(doc, node, tagName);
1276
+
1277
+ if (node.type !== Type.PLAIN) {
1278
+ var _msg3 = "Failed to resolve ".concat(node.type, " node here");
1279
+
1280
+ errors.push(new YAMLSyntaxError(node, _msg3));
1281
+ return null;
1282
+ }
1283
+
1284
+ try {
1285
+ var str = node.strValue || '';
1286
+
1287
+ if (typeof str !== 'string') {
1288
+ str.errors.forEach(function (error) {
1289
+ return doc.errors.push(error);
1290
+ });
1291
+ str = str.str;
1292
+ }
1293
+
1294
+ return resolveScalar(str, schema.tags);
1295
+ } catch (error) {
1296
+ if (!error.source) error.source = node;
1297
+ errors.push(error);
1298
+ return null;
1299
+ }
1300
+ } // sets node.resolved on success
1301
+
1302
+
1303
+ function resolveNode(doc, node) {
1304
+ if (!node) return null;
1305
+ if (node.error) doc.errors.push(node.error);
1306
+
1307
+ var _resolveNodeProps = resolveNodeProps(doc.errors, node),
1308
+ comments = _resolveNodeProps.comments,
1309
+ hasAnchor = _resolveNodeProps.hasAnchor,
1310
+ hasTag = _resolveNodeProps.hasTag;
1311
+
1312
+ if (hasAnchor) {
1313
+ var anchors = doc.anchors;
1314
+ var name = node.anchor;
1315
+ var prev = anchors.getNode(name); // At this point, aliases for any preceding node with the same anchor
1316
+ // name have already been resolved, so it may safely be renamed.
1317
+
1318
+ if (prev) anchors.map[anchors.newName(name)] = prev; // During parsing, we need to store the CST node in anchors.map as
1319
+ // anchors need to be available during resolution to allow for
1320
+ // circular references.
1321
+
1322
+ anchors.map[name] = node;
1323
+ }
1324
+
1325
+ if (node.type === Type.ALIAS && (hasAnchor || hasTag)) {
1326
+ var msg = 'An alias node must not specify any properties';
1327
+ doc.errors.push(new YAMLSemanticError(node, msg));
1328
+ }
1329
+
1330
+ var res = resolveNodeValue(doc, node);
1331
+
1332
+ if (res) {
1333
+ res.range = [node.range.start, node.range.end];
1334
+ if (doc.options.keepCstNodes) res.cstNode = node;
1335
+ if (doc.options.keepNodeTypes) res.type = node.type;
1336
+ var cb = comments.before.join('\n');
1337
+
1338
+ if (cb) {
1339
+ res.commentBefore = res.commentBefore ? "".concat(res.commentBefore, "\n").concat(cb) : cb;
1340
+ }
1341
+
1342
+ var ca = comments.after.join('\n');
1343
+ if (ca) res.comment = res.comment ? "".concat(res.comment, "\n").concat(ca) : ca;
1344
+ }
1345
+
1346
+ return node.resolved = res;
1347
+ }
1348
+
393
1349
  function parseContents(doc, contents) {
394
1350
  var comments = {
395
1351
  before: [],
@@ -582,20 +1538,35 @@ function assertCollection(contents) {
582
1538
  }
583
1539
 
584
1540
  var Document = /*#__PURE__*/function () {
585
- function Document(options) {
1541
+ function Document(value, replacer, options) {
586
1542
  _classCallCheck(this, Document);
587
1543
 
588
- this.anchors = new Anchors(options.anchorPrefix);
1544
+ if (options === undefined && replacer && _typeof(replacer) === 'object' && !Array.isArray(replacer)) {
1545
+ options = replacer;
1546
+ replacer = undefined;
1547
+ }
1548
+
1549
+ this.options = Object.assign({}, defaultOptions, options);
1550
+ this.anchors = new Anchors(this.options.anchorPrefix);
589
1551
  this.commentBefore = null;
590
1552
  this.comment = null;
591
- this.contents = null;
592
1553
  this.directivesEndMarker = null;
593
1554
  this.errors = [];
594
- this.options = options;
595
1555
  this.schema = null;
596
1556
  this.tagPrefixes = [];
597
1557
  this.version = null;
598
1558
  this.warnings = [];
1559
+
1560
+ if (value === undefined) {
1561
+ // note that this.schema is left as null here
1562
+ this.contents = null;
1563
+ } else if (value instanceof Document$1) {
1564
+ this.parse(value);
1565
+ } else {
1566
+ this.contents = this.createNode(value, {
1567
+ replacer: replacer
1568
+ });
1569
+ }
599
1570
  }
600
1571
 
601
1572
  _createClass(Document, [{
@@ -610,6 +1581,69 @@ var Document = /*#__PURE__*/function () {
610
1581
  assertCollection(this.contents);
611
1582
  this.contents.addIn(path, value);
612
1583
  }
1584
+ }, {
1585
+ key: "createNode",
1586
+ value: function createNode$1(value) {
1587
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
1588
+ keepUndefined = _ref.keepUndefined,
1589
+ onTagObj = _ref.onTagObj,
1590
+ replacer = _ref.replacer,
1591
+ tag = _ref.tag,
1592
+ wrapScalars = _ref.wrapScalars;
1593
+
1594
+ this.setSchema();
1595
+ if (typeof replacer === 'function') value = replacer.call({
1596
+ '': value
1597
+ }, '', value);else if (Array.isArray(replacer)) {
1598
+ var keyToStr = function keyToStr(v) {
1599
+ return typeof v === 'number' || v instanceof String || v instanceof Number;
1600
+ };
1601
+
1602
+ var asStr = replacer.filter(keyToStr).map(String);
1603
+ if (asStr.length > 0) replacer = replacer.concat(asStr);
1604
+ }
1605
+ if (typeof keepUndefined !== 'boolean') keepUndefined = !!this.options.keepUndefined;
1606
+ var aliasNodes = [];
1607
+ var ctx = {
1608
+ keepUndefined: keepUndefined,
1609
+ onAlias: function onAlias(source) {
1610
+ var alias = new Alias(source);
1611
+ aliasNodes.push(alias);
1612
+ return alias;
1613
+ },
1614
+ onTagObj: onTagObj,
1615
+ prevObjects: new Map(),
1616
+ replacer: replacer,
1617
+ schema: this.schema,
1618
+ wrapScalars: wrapScalars !== false
1619
+ };
1620
+
1621
+ var node = createNode(value, tag, ctx);
1622
+
1623
+ for (var _i = 0, _aliasNodes = aliasNodes; _i < _aliasNodes.length; _i++) {
1624
+ var alias = _aliasNodes[_i];
1625
+ // With circular references, the source node is only resolved after all of
1626
+ // its child nodes are. This is why anchors are set only after all of the
1627
+ // nodes have been created.
1628
+ alias.source = alias.source.node;
1629
+ var name = this.anchors.getName(alias.source);
1630
+
1631
+ if (!name) {
1632
+ name = this.anchors.newName();
1633
+ this.anchors.map[name] = alias.source;
1634
+ }
1635
+ }
1636
+
1637
+ return node;
1638
+ }
1639
+ }, {
1640
+ key: "createPair",
1641
+ value: function createPair(key, value) {
1642
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1643
+ var k = this.createNode(key, options);
1644
+ var v = this.createNode(value, options);
1645
+ return new Pair(k, v);
1646
+ }
613
1647
  }, {
614
1648
  key: "delete",
615
1649
  value: function _delete(key) {
@@ -658,13 +1692,21 @@ var Document = /*#__PURE__*/function () {
658
1692
  }, {
659
1693
  key: "set",
660
1694
  value: function set(key, value) {
661
- assertCollection(this.contents);
662
- this.contents.set(key, value);
1695
+ if (this.contents == null) {
1696
+ this.setSchema();
1697
+ this.contents = collectionFromPath(this.schema, [key], value);
1698
+ } else {
1699
+ assertCollection(this.contents);
1700
+ this.contents.set(key, value);
1701
+ }
663
1702
  }
664
1703
  }, {
665
1704
  key: "setIn",
666
1705
  value: function setIn(path, value) {
667
- if (isEmptyPath(path)) this.contents = value;else {
1706
+ if (isEmptyPath(path)) this.contents = value;else if (this.contents == null) {
1707
+ this.setSchema();
1708
+ this.contents = collectionFromPath(this.schema, path, value);
1709
+ } else {
668
1710
  assertCollection(this.contents);
669
1711
  this.contents.setIn(path, value);
670
1712
  }
@@ -748,7 +1790,7 @@ var Document = /*#__PURE__*/function () {
748
1790
  key: "listNonDefaultTags",
749
1791
  value: function listNonDefaultTags() {
750
1792
  return listTagNames(this.contents).filter(function (t) {
751
- return t.indexOf(Schema.defaultPrefix) !== 0;
1793
+ return t.indexOf(defaultTagPrefix) !== 0;
752
1794
  });
753
1795
  }
754
1796
  }, {
@@ -771,37 +1813,38 @@ var Document = /*#__PURE__*/function () {
771
1813
  }
772
1814
  }
773
1815
  }, {
774
- key: "toJSON",
775
- value: function toJSON$1(arg, onAnchor) {
776
- var _this = this;
777
-
778
- var _this$options = this.options,
779
- keepBlobsInJSON = _this$options.keepBlobsInJSON,
780
- mapAsMap = _this$options.mapAsMap,
781
- maxAliasCount = _this$options.maxAliasCount;
782
- var keep = keepBlobsInJSON && (typeof arg !== 'string' || !(this.contents instanceof Scalar));
1816
+ key: "toJS",
1817
+ value: function toJS$1() {
1818
+ var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
1819
+ json = _ref2.json,
1820
+ jsonArg = _ref2.jsonArg,
1821
+ mapAsMap = _ref2.mapAsMap,
1822
+ onAnchor = _ref2.onAnchor,
1823
+ reviver = _ref2.reviver;
1824
+
1825
+ var anchorNodes = Object.values(this.anchors.map).map(function (node) {
1826
+ return [node, {
1827
+ alias: [],
1828
+ aliasCount: 0,
1829
+ count: 1
1830
+ }];
1831
+ });
1832
+ var anchors = anchorNodes.length > 0 ? new Map(anchorNodes) : null;
783
1833
  var ctx = {
1834
+ anchors: anchors,
784
1835
  doc: this,
785
1836
  indentStep: ' ',
786
- keep: keep,
787
- mapAsMap: keep && !!mapAsMap,
788
- maxAliasCount: maxAliasCount,
1837
+ keep: !json,
1838
+ mapAsMap: typeof mapAsMap === 'boolean' ? mapAsMap : !!this.options.mapAsMap,
1839
+ maxAliasCount: this.options.maxAliasCount,
789
1840
  stringify: stringify // Requiring directly in Pair would create circular dependencies
790
1841
 
791
1842
  };
792
- var anchorNames = Object.keys(this.anchors.map);
793
- if (anchorNames.length > 0) ctx.anchors = new Map(anchorNames.map(function (name) {
794
- return [_this.anchors.map[name], {
795
- alias: [],
796
- aliasCount: 0,
797
- count: 1
798
- }];
799
- }));
800
1843
 
801
- var res = toJSON(this.contents, arg, ctx);
1844
+ var res = toJS(this.contents, jsonArg || '', ctx);
802
1845
 
803
- if (typeof onAnchor === 'function' && ctx.anchors) {
804
- var _iterator3 = _createForOfIteratorHelper(ctx.anchors.values()),
1846
+ if (typeof onAnchor === 'function' && anchors) {
1847
+ var _iterator3 = _createForOfIteratorHelper(anchors.values()),
805
1848
  _step3;
806
1849
 
807
1850
  try {
@@ -818,7 +1861,19 @@ var Document = /*#__PURE__*/function () {
818
1861
  }
819
1862
  }
820
1863
 
821
- return res;
1864
+ return typeof reviver === 'function' ? applyReviver(reviver, {
1865
+ '': res
1866
+ }, '', res) : res;
1867
+ }
1868
+ }, {
1869
+ key: "toJSON",
1870
+ value: function toJSON(jsonArg, onAnchor) {
1871
+ return this.toJS({
1872
+ json: true,
1873
+ jsonArg: jsonArg,
1874
+ mapAsMap: false,
1875
+ onAnchor: onAnchor
1876
+ });
822
1877
  }
823
1878
  }, {
824
1879
  key: "toString",
@@ -847,9 +1902,9 @@ var Document = /*#__PURE__*/function () {
847
1902
  }
848
1903
 
849
1904
  var tagNames = this.listNonDefaultTags();
850
- this.tagPrefixes.forEach(function (_ref) {
851
- var handle = _ref.handle,
852
- prefix = _ref.prefix;
1905
+ this.tagPrefixes.forEach(function (_ref3) {
1906
+ var handle = _ref3.handle,
1907
+ prefix = _ref3.prefix;
853
1908
 
854
1909
  if (tagNames.some(function (t) {
855
1910
  return t.indexOf(prefix) === 0;
@@ -866,7 +1921,7 @@ var Document = /*#__PURE__*/function () {
866
1921
  }
867
1922
 
868
1923
  var ctx = {
869
- anchors: {},
1924
+ anchors: Object.create(null),
870
1925
  doc: this,
871
1926
  indent: '',
872
1927
  indentStep: ' '.repeat(indentSize),
@@ -892,7 +1947,7 @@ var Document = /*#__PURE__*/function () {
892
1947
  return contentComment = null;
893
1948
  }, onChompKeep);
894
1949
  lines.push(addComment(body, '', contentComment));
895
- } else if (this.contents !== undefined) {
1950
+ } else {
896
1951
  lines.push(stringify(this.contents, ctx));
897
1952
  }
898
1953
 
@@ -910,33 +1965,23 @@ var Document = /*#__PURE__*/function () {
910
1965
 
911
1966
  _defineProperty(Document, "defaults", documentOptions);
912
1967
 
913
- function createNode(value) {
914
- var wrapScalars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
915
- var tag = arguments.length > 2 ? arguments[2] : undefined;
916
-
917
- if (tag === undefined && typeof wrapScalars === 'string') {
918
- tag = wrapScalars;
919
- wrapScalars = true;
920
- }
921
-
922
- var options = Object.assign({}, Document.defaults[defaultOptions.version], defaultOptions);
923
- var schema = new Schema(options);
924
- return schema.createNode(value, wrapScalars, tag);
925
- }
926
-
927
- var Document$1 = /*#__PURE__*/function (_YAMLDocument) {
928
- _inherits(Document, _YAMLDocument);
1968
+ /* global console, process, YAML_SILENCE_WARNINGS */
1969
+ function warn(warning, type) {
1970
+ if (typeof YAML_SILENCE_WARNINGS !== 'undefined' && YAML_SILENCE_WARNINGS) return;
929
1971
 
930
- var _super = _createSuper(Document);
1972
+ if (typeof process !== 'undefined') {
1973
+ if (process.env.YAML_SILENCE_WARNINGS) return; // This will throw in Jest if `warning` is an Error instance due to
1974
+ // https://github.com/facebook/jest/issues/2549
931
1975
 
932
- function Document(options) {
933
- _classCallCheck(this, Document);
1976
+ if (process.emitWarning) {
1977
+ process.emitWarning(warning, type);
1978
+ return;
1979
+ }
1980
+ } // eslint-disable-next-line no-console
934
1981
 
935
- return _super.call(this, Object.assign({}, defaultOptions, options));
936
- }
937
1982
 
938
- return Document;
939
- }(Document);
1983
+ console.warn(type ? "".concat(type, ": ").concat(warning) : warning);
1984
+ }
940
1985
 
941
1986
  function parseAllDocuments(src, options) {
942
1987
  var stream = [];
@@ -948,7 +1993,7 @@ function parseAllDocuments(src, options) {
948
1993
  try {
949
1994
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
950
1995
  var cstDoc = _step.value;
951
- var doc = new Document$1(options);
1996
+ var doc = new Document(undefined, null, options);
952
1997
  doc.parse(cstDoc, prev);
953
1998
  stream.push(doc);
954
1999
  prev = doc;
@@ -961,10 +2006,9 @@ function parseAllDocuments(src, options) {
961
2006
 
962
2007
  return stream;
963
2008
  }
964
-
965
2009
  function parseDocument(src, options) {
966
2010
  var cst = parse$1(src);
967
- var doc = new Document$1(options).parse(cst[0]);
2011
+ var doc = new Document(cst[0], null, options);
968
2012
 
969
2013
  if (cst.length > 1) {
970
2014
  var errMsg = 'Source contains multiple documents; please use YAML.parseAllDocuments()';
@@ -973,32 +2017,41 @@ function parseDocument(src, options) {
973
2017
 
974
2018
  return doc;
975
2019
  }
2020
+ function parse(src, reviver, options) {
2021
+ if (options === undefined && reviver && _typeof(reviver) === 'object') {
2022
+ options = reviver;
2023
+ reviver = undefined;
2024
+ }
976
2025
 
977
- function parse(src, options) {
978
2026
  var doc = parseDocument(src, options);
979
2027
  doc.warnings.forEach(function (warning) {
980
2028
  return warn(warning);
981
2029
  });
982
2030
  if (doc.errors.length > 0) throw doc.errors[0];
983
- return doc.toJSON();
2031
+ return doc.toJS({
2032
+ reviver: reviver
2033
+ });
984
2034
  }
2035
+ function stringify$1(value, replacer, options) {
2036
+ if (typeof options === 'string') options = options.length;
2037
+
2038
+ if (typeof options === 'number') {
2039
+ var indent = Math.round(options);
2040
+ options = indent < 1 ? undefined : indent > 8 ? {
2041
+ indent: 8
2042
+ } : {
2043
+ indent: indent
2044
+ };
2045
+ }
985
2046
 
986
- function stringify$1(value, options) {
987
- var doc = new Document$1(options);
988
- doc.contents = value;
989
- return String(doc);
990
- }
2047
+ if (value === undefined) {
2048
+ var _ref = options || replacer || {},
2049
+ keepUndefined = _ref.keepUndefined;
991
2050
 
992
- var YAML = {
993
- createNode: createNode,
994
- defaultOptions: defaultOptions,
995
- Document: Document$1,
996
- parse: parse,
997
- parseAllDocuments: parseAllDocuments,
998
- parseCST: parse$1,
999
- parseDocument: parseDocument,
1000
- scalarOptions: scalarOptions,
1001
- stringify: stringify$1
1002
- };
2051
+ if (!keepUndefined) return undefined;
2052
+ }
2053
+
2054
+ return new Document(value, replacer, options).toString();
2055
+ }
1003
2056
 
1004
- export { YAML };
2057
+ export { Document, defaultOptions, parse, parseAllDocuments, parseDocument, scalarOptions, stringify$1 as stringify };