index.js
   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-b8036b75.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-492ab440.js';
   4  import { S as Schema } from './Schema-e94716c8.js';
   5  import { w as warn } from './warnings-df54cb69.js';
   6  
   7  var defaultOptions = {
   8    anchorPrefix: 'a',
   9    customTags: null,
  10    indent: 2,
  11    indentSeq: true,
  12    keepCstNodes: false,
  13    keepNodeTypes: true,
  14    keepBlobsInJSON: true,
  15    mapAsMap: false,
  16    maxAliasCount: 100,
  17    prettyErrors: false,
  18    // TODO Set true in v2
  19    simpleKeys: false,
  20    version: '1.2'
  21  };
  22  var scalarOptions = {
  23    get binary() {
  24      return binaryOptions;
  25    },
  26  
  27    set binary(opt) {
  28      Object.assign(binaryOptions, opt);
  29    },
  30  
  31    get bool() {
  32      return boolOptions;
  33    },
  34  
  35    set bool(opt) {
  36      Object.assign(boolOptions, opt);
  37    },
  38  
  39    get int() {
  40      return intOptions;
  41    },
  42  
  43    set int(opt) {
  44      Object.assign(intOptions, opt);
  45    },
  46  
  47    get null() {
  48      return nullOptions;
  49    },
  50  
  51    set null(opt) {
  52      Object.assign(nullOptions, opt);
  53    },
  54  
  55    get str() {
  56      return strOptions;
  57    },
  58  
  59    set str(opt) {
  60      Object.assign(strOptions, opt);
  61    }
  62  
  63  };
  64  var documentOptions = {
  65    '1.0': {
  66      schema: 'yaml-1.1',
  67      merge: true,
  68      tagPrefixes: [{
  69        handle: '!',
  70        prefix: 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: defaultTagPrefix
  85      }]
  86    },
  87    1.2: {
  88      schema: 'core',
  89      merge: false,
  90      tagPrefixes: [{
  91        handle: '!',
  92        prefix: '!'
  93      }, {
  94        handle: '!!',
  95        prefix: defaultTagPrefix
  96      }]
  97    }
  98  };
  99  
 100  function stringifyTag(doc, tag) {
 101    if ((doc.version || doc.options.version) === '1.0') {
 102      var priv = tag.match(/^tag:private\.yaml\.org,2002:([^:/]+)$/);
 103      if (priv) return '!' + priv[1];
 104      var vocab = tag.match(/^tag:([a-zA-Z0-9-]+)\.yaml\.org,2002:(.*)/);
 105      return vocab ? "!".concat(vocab[1], "/").concat(vocab[2]) : "!".concat(tag.replace(/^tag:/, ''));
 106    }
 107  
 108    var p = doc.tagPrefixes.find(function (p) {
 109      return tag.indexOf(p.prefix) === 0;
 110    });
 111  
 112    if (!p) {
 113      var dtp = doc.getDefaults().tagPrefixes;
 114      p = dtp && dtp.find(function (p) {
 115        return tag.indexOf(p.prefix) === 0;
 116      });
 117    }
 118  
 119    if (!p) return tag[0] === '!' ? tag : "!<".concat(tag, ">");
 120    var suffix = tag.substr(p.prefix.length).replace(/[!,[\]{}]/g, function (ch) {
 121      return {
 122        '!': '%21',
 123        ',': '%2C',
 124        '[': '%5B',
 125        ']': '%5D',
 126        '{': '%7B',
 127        '}': '%7D'
 128      }[ch];
 129    });
 130    return p.handle + suffix;
 131  }
 132  
 133  function getTagObject(tags, item) {
 134    if (item instanceof Alias) return Alias;
 135  
 136    if (item.tag) {
 137      var match = tags.filter(function (t) {
 138        return t.tag === item.tag;
 139      });
 140      if (match.length > 0) return match.find(function (t) {
 141        return t.format === item.format;
 142      }) || match[0];
 143    }
 144  
 145    var tagObj, obj;
 146  
 147    if (item instanceof Scalar) {
 148      obj = item.value; // TODO: deprecate/remove class check
 149  
 150      var _match = tags.filter(function (t) {
 151        return t.identify && t.identify(obj) || t.class && obj instanceof t.class;
 152      });
 153  
 154      tagObj = _match.find(function (t) {
 155        return t.format === item.format;
 156      }) || _match.find(function (t) {
 157        return !t.format;
 158      });
 159    } else {
 160      obj = item;
 161      tagObj = tags.find(function (t) {
 162        return t.nodeClass && obj instanceof t.nodeClass;
 163      });
 164    }
 165  
 166    if (!tagObj) {
 167      var name = obj && obj.constructor ? obj.constructor.name : _typeof(obj);
 168      throw new Error("Tag not resolved for ".concat(name, " value"));
 169    }
 170  
 171    return tagObj;
 172  } // needs to be called before value stringifier to allow for circular anchor refs
 173  
 174  
 175  function stringifyProps(node, tagObj, _ref) {
 176    var anchors = _ref.anchors,
 177        doc = _ref.doc;
 178    var props = [];
 179    var anchor = doc.anchors.getName(node);
 180  
 181    if (anchor) {
 182      anchors[anchor] = node;
 183      props.push("&".concat(anchor));
 184    }
 185  
 186    if (node.tag) {
 187      props.push(stringifyTag(doc, node.tag));
 188    } else if (!tagObj.default) {
 189      props.push(stringifyTag(doc, tagObj.tag));
 190    }
 191  
 192    return props.join(' ');
 193  }
 194  
 195  function stringify$1(item, ctx, onComment, onChompKeep) {
 196    var _ctx$doc = ctx.doc,
 197        anchors = _ctx$doc.anchors,
 198        schema = _ctx$doc.schema;
 199    var tagObj;
 200  
 201    if (!(item instanceof Node)) {
 202      var createCtx = {
 203        aliasNodes: [],
 204        onTagObj: function onTagObj(o) {
 205          return tagObj = o;
 206        },
 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      }
 230    }
 231  
 232    if (item instanceof Pair) return item.toString(ctx, onComment, onChompKeep);
 233    if (!tagObj) tagObj = getTagObject(schema.tags, item);
 234    var props = stringifyProps(item, tagObj, ctx);
 235    if (props.length > 0) ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1;
 236    var str = typeof tagObj.stringify === 'function' ? tagObj.stringify(item, ctx, onComment, onChompKeep) : item instanceof Scalar ? stringifyString(item, ctx, onComment, onChompKeep) : item.toString(ctx, onComment, onChompKeep);
 237    if (!props) return str;
 238    return item instanceof Scalar || str[0] === '{' || str[0] === '[' ? "".concat(props, " ").concat(str) : "".concat(props, "\n").concat(ctx.indent).concat(str);
 239  }
 240  
 241  var Anchors = /*#__PURE__*/function () {
 242    function Anchors(prefix) {
 243      _classCallCheck(this, Anchors);
 244  
 245      _defineProperty(this, "map", Object.create(null));
 246  
 247      this.prefix = prefix;
 248    }
 249  
 250    _createClass(Anchors, [{
 251      key: "createAlias",
 252      value: function createAlias(node, name) {
 253        this.setAnchor(node, name);
 254        return new Alias(node);
 255      }
 256    }, {
 257      key: "createMergePair",
 258      value: function createMergePair() {
 259        var _this = this;
 260  
 261        var merge = new Merge();
 262  
 263        for (var _len = arguments.length, sources = new Array(_len), _key = 0; _key < _len; _key++) {
 264          sources[_key] = arguments[_key];
 265        }
 266  
 267        merge.value.items = sources.map(function (s) {
 268          if (s instanceof Alias) {
 269            if (s.source instanceof YAMLMap) return s;
 270          } else if (s instanceof YAMLMap) {
 271            return _this.createAlias(s);
 272          }
 273  
 274          throw new Error('Merge sources must be Map nodes or their Aliases');
 275        });
 276        return merge;
 277      }
 278    }, {
 279      key: "getName",
 280      value: function getName(node) {
 281        var map = this.map;
 282        return Object.keys(map).find(function (a) {
 283          return map[a] === node;
 284        });
 285      }
 286    }, {
 287      key: "getNames",
 288      value: function getNames() {
 289        return Object.keys(this.map);
 290      }
 291    }, {
 292      key: "getNode",
 293      value: function getNode(name) {
 294        return this.map[name];
 295      }
 296    }, {
 297      key: "newName",
 298      value: function newName(prefix) {
 299        if (!prefix) prefix = this.prefix;
 300        var names = Object.keys(this.map);
 301  
 302        for (var i = 1; true; ++i) {
 303          var name = "".concat(prefix).concat(i);
 304          if (!names.includes(name)) return name;
 305        }
 306      } // During parsing, map & aliases contain CST nodes
 307  
 308    }, {
 309      key: "resolveNodes",
 310      value: function resolveNodes() {
 311        var map = this.map,
 312            _cstAliases = this._cstAliases;
 313        Object.keys(map).forEach(function (a) {
 314          map[a] = map[a].resolved;
 315        });
 316  
 317        _cstAliases.forEach(function (a) {
 318          a.source = a.source.resolved;
 319        });
 320  
 321        delete this._cstAliases;
 322      }
 323    }, {
 324      key: "setAnchor",
 325      value: function setAnchor(node, name) {
 326        if (node != null && !Anchors.validAnchorNode(node)) {
 327          throw new Error('Anchors may only be set for Scalar, Seq and Map nodes');
 328        }
 329  
 330        if (name && /[\x00-\x19\s,[\]{}]/.test(name)) {
 331          throw new Error('Anchor names must not contain whitespace or control characters');
 332        }
 333  
 334        var map = this.map;
 335        var prev = node && Object.keys(map).find(function (a) {
 336          return map[a] === node;
 337        });
 338  
 339        if (prev) {
 340          if (!name) {
 341            return prev;
 342          } else if (prev !== name) {
 343            delete map[prev];
 344            map[name] = node;
 345          }
 346        } else {
 347          if (!name) {
 348            if (!node) return null;
 349            name = this.newName();
 350          }
 351  
 352          map[name] = node;
 353        }
 354  
 355        return name;
 356      }
 357    }], [{
 358      key: "validAnchorNode",
 359      value: function validAnchorNode(node) {
 360        return node instanceof Scalar || node instanceof YAMLSeq || node instanceof YAMLMap;
 361      }
 362    }]);
 363  
 364    return Anchors;
 365  }();
 366  
 367  var visit = function visit(node, tags) {
 368    if (node && _typeof(node) === 'object') {
 369      var tag = node.tag;
 370  
 371      if (node instanceof Collection) {
 372        if (tag) tags[tag] = true;
 373        node.items.forEach(function (n) {
 374          return visit(n, tags);
 375        });
 376      } else if (node instanceof Pair) {
 377        visit(node.key, tags);
 378        visit(node.value, tags);
 379      } else if (node instanceof Scalar) {
 380        if (tag) tags[tag] = true;
 381      }
 382    }
 383  
 384    return tags;
 385  };
 386  
 387  var listTagNames = function listTagNames(node) {
 388    return Object.keys(visit(node, {}));
 389  };
 390  
 391  function parseContents(doc, contents) {
 392    var comments = {
 393      before: [],
 394      after: []
 395    };
 396    var body = undefined;
 397    var spaceBefore = false;
 398  
 399    var _iterator = _createForOfIteratorHelper(contents),
 400        _step;
 401  
 402    try {
 403      for (_iterator.s(); !(_step = _iterator.n()).done;) {
 404        var node = _step.value;
 405  
 406        if (node.valueRange) {
 407          if (body !== undefined) {
 408            var msg = 'Document contains trailing content not separated by a ... or --- line';
 409            doc.errors.push(new YAMLSyntaxError(node, msg));
 410            break;
 411          }
 412  
 413          var res = resolveNode(doc, node);
 414  
 415          if (spaceBefore) {
 416            res.spaceBefore = true;
 417            spaceBefore = false;
 418          }
 419  
 420          body = res;
 421        } else if (node.comment !== null) {
 422          var cc = body === undefined ? comments.before : comments.after;
 423          cc.push(node.comment);
 424        } else if (node.type === Type.BLANK_LINE) {
 425          spaceBefore = true;
 426  
 427          if (body === undefined && comments.before.length > 0 && !doc.commentBefore) {
 428            // space-separated comments at start are parsed as document comments
 429            doc.commentBefore = comments.before.join('\n');
 430            comments.before = [];
 431          }
 432        }
 433      }
 434    } catch (err) {
 435      _iterator.e(err);
 436    } finally {
 437      _iterator.f();
 438    }
 439  
 440    doc.contents = body || null;
 441  
 442    if (!body) {
 443      doc.comment = comments.before.concat(comments.after).join('\n') || null;
 444    } else {
 445      var cb = comments.before.join('\n');
 446  
 447      if (cb) {
 448        var cbNode = body instanceof Collection && body.items[0] ? body.items[0] : body;
 449        cbNode.commentBefore = cbNode.commentBefore ? "".concat(cb, "\n").concat(cbNode.commentBefore) : cb;
 450      }
 451  
 452      doc.comment = comments.after.join('\n') || null;
 453    }
 454  }
 455  
 456  function resolveTagDirective(_ref, directive) {
 457    var tagPrefixes = _ref.tagPrefixes;
 458  
 459    var _directive$parameters = _slicedToArray(directive.parameters, 2),
 460        handle = _directive$parameters[0],
 461        prefix = _directive$parameters[1];
 462  
 463    if (!handle || !prefix) {
 464      var msg = 'Insufficient parameters given for %TAG directive';
 465      throw new YAMLSemanticError(directive, msg);
 466    }
 467  
 468    if (tagPrefixes.some(function (p) {
 469      return p.handle === handle;
 470    })) {
 471      var _msg = 'The %TAG directive must only be given at most once per handle in the same document.';
 472      throw new YAMLSemanticError(directive, _msg);
 473    }
 474  
 475    return {
 476      handle: handle,
 477      prefix: prefix
 478    };
 479  }
 480  
 481  function resolveYamlDirective(doc, directive) {
 482    var _directive$parameters2 = _slicedToArray(directive.parameters, 1),
 483        version = _directive$parameters2[0];
 484  
 485    if (directive.name === 'YAML:1.0') version = '1.0';
 486  
 487    if (!version) {
 488      var msg = 'Insufficient parameters given for %YAML directive';
 489      throw new YAMLSemanticError(directive, msg);
 490    }
 491  
 492    if (!documentOptions[version]) {
 493      var v0 = doc.version || doc.options.version;
 494  
 495      var _msg2 = "Document will be parsed as YAML ".concat(v0, " rather than YAML ").concat(version);
 496  
 497      doc.warnings.push(new YAMLWarning(directive, _msg2));
 498    }
 499  
 500    return version;
 501  }
 502  
 503  function parseDirectives(doc, directives, prevDoc) {
 504    var directiveComments = [];
 505    var hasDirectives = false;
 506  
 507    var _iterator = _createForOfIteratorHelper(directives),
 508        _step;
 509  
 510    try {
 511      for (_iterator.s(); !(_step = _iterator.n()).done;) {
 512        var directive = _step.value;
 513        var comment = directive.comment,
 514            name = directive.name;
 515  
 516        switch (name) {
 517          case 'TAG':
 518            try {
 519              doc.tagPrefixes.push(resolveTagDirective(doc, directive));
 520            } catch (error) {
 521              doc.errors.push(error);
 522            }
 523  
 524            hasDirectives = true;
 525            break;
 526  
 527          case 'YAML':
 528          case 'YAML:1.0':
 529            if (doc.version) {
 530              var msg = 'The %YAML directive must only be given at most once per document.';
 531              doc.errors.push(new YAMLSemanticError(directive, msg));
 532            }
 533  
 534            try {
 535              doc.version = resolveYamlDirective(doc, directive);
 536            } catch (error) {
 537              doc.errors.push(error);
 538            }
 539  
 540            hasDirectives = true;
 541            break;
 542  
 543          default:
 544            if (name) {
 545              var _msg3 = "YAML only supports %TAG and %YAML directives, and not %".concat(name);
 546  
 547              doc.warnings.push(new YAMLWarning(directive, _msg3));
 548            }
 549  
 550        }
 551  
 552        if (comment) directiveComments.push(comment);
 553      }
 554    } catch (err) {
 555      _iterator.e(err);
 556    } finally {
 557      _iterator.f();
 558    }
 559  
 560    if (prevDoc && !hasDirectives && '1.1' === (doc.version || prevDoc.version || doc.options.version)) {
 561      var copyTagPrefix = function copyTagPrefix(_ref2) {
 562        var handle = _ref2.handle,
 563            prefix = _ref2.prefix;
 564        return {
 565          handle: handle,
 566          prefix: prefix
 567        };
 568      };
 569  
 570      doc.tagPrefixes = prevDoc.tagPrefixes.map(copyTagPrefix);
 571      doc.version = prevDoc.version;
 572    }
 573  
 574    doc.commentBefore = directiveComments.join('\n') || null;
 575  }
 576  
 577  function assertCollection(contents) {
 578    if (contents instanceof Collection) return true;
 579    throw new Error('Expected a YAML collection as document contents');
 580  }
 581  
 582  var Document$1 = /*#__PURE__*/function () {
 583    function Document(options) {
 584      _classCallCheck(this, Document);
 585  
 586      this.anchors = new Anchors(options.anchorPrefix);
 587      this.commentBefore = null;
 588      this.comment = null;
 589      this.contents = null;
 590      this.directivesEndMarker = null;
 591      this.errors = [];
 592      this.options = options;
 593      this.schema = null;
 594      this.tagPrefixes = [];
 595      this.version = null;
 596      this.warnings = [];
 597    }
 598  
 599    _createClass(Document, [{
 600      key: "add",
 601      value: function add(value) {
 602        assertCollection(this.contents);
 603        return this.contents.add(value);
 604      }
 605    }, {
 606      key: "addIn",
 607      value: function addIn(path, value) {
 608        assertCollection(this.contents);
 609        this.contents.addIn(path, value);
 610      }
 611    }, {
 612      key: "delete",
 613      value: function _delete(key) {
 614        assertCollection(this.contents);
 615        return this.contents.delete(key);
 616      }
 617    }, {
 618      key: "deleteIn",
 619      value: function deleteIn(path) {
 620        if (isEmptyPath(path)) {
 621          if (this.contents == null) return false;
 622          this.contents = null;
 623          return true;
 624        }
 625  
 626        assertCollection(this.contents);
 627        return this.contents.deleteIn(path);
 628      }
 629    }, {
 630      key: "getDefaults",
 631      value: function getDefaults() {
 632        return Document.defaults[this.version] || Document.defaults[this.options.version] || {};
 633      }
 634    }, {
 635      key: "get",
 636      value: function get(key, keepScalar) {
 637        return this.contents instanceof Collection ? this.contents.get(key, keepScalar) : undefined;
 638      }
 639    }, {
 640      key: "getIn",
 641      value: function getIn(path, keepScalar) {
 642        if (isEmptyPath(path)) return !keepScalar && this.contents instanceof Scalar ? this.contents.value : this.contents;
 643        return this.contents instanceof Collection ? this.contents.getIn(path, keepScalar) : undefined;
 644      }
 645    }, {
 646      key: "has",
 647      value: function has(key) {
 648        return this.contents instanceof Collection ? this.contents.has(key) : false;
 649      }
 650    }, {
 651      key: "hasIn",
 652      value: function hasIn(path) {
 653        if (isEmptyPath(path)) return this.contents !== undefined;
 654        return this.contents instanceof Collection ? this.contents.hasIn(path) : false;
 655      }
 656    }, {
 657      key: "set",
 658      value: function set(key, value) {
 659        assertCollection(this.contents);
 660        this.contents.set(key, value);
 661      }
 662    }, {
 663      key: "setIn",
 664      value: function setIn(path, value) {
 665        if (isEmptyPath(path)) this.contents = value;else {
 666          assertCollection(this.contents);
 667          this.contents.setIn(path, value);
 668        }
 669      }
 670    }, {
 671      key: "setSchema",
 672      value: function setSchema(id, customTags) {
 673        if (!id && !customTags && this.schema) return;
 674        if (typeof id === 'number') id = id.toFixed(1);
 675  
 676        if (id === '1.0' || id === '1.1' || id === '1.2') {
 677          if (this.version) this.version = id;else this.options.version = id;
 678          delete this.options.schema;
 679        } else if (id && typeof id === 'string') {
 680          this.options.schema = id;
 681        }
 682  
 683        if (Array.isArray(customTags)) this.options.customTags = customTags;
 684        var opt = Object.assign({}, this.getDefaults(), this.options);
 685        this.schema = new Schema(opt);
 686      }
 687    }, {
 688      key: "parse",
 689      value: function parse(node, prevDoc) {
 690        if (this.options.keepCstNodes) this.cstNode = node;
 691        if (this.options.keepNodeTypes) this.type = 'DOCUMENT';
 692        var _node$directives = node.directives,
 693            directives = _node$directives === void 0 ? [] : _node$directives,
 694            _node$contents = node.contents,
 695            contents = _node$contents === void 0 ? [] : _node$contents,
 696            directivesEndMarker = node.directivesEndMarker,
 697            error = node.error,
 698            valueRange = node.valueRange;
 699  
 700        if (error) {
 701          if (!error.source) error.source = this;
 702          this.errors.push(error);
 703        }
 704  
 705        parseDirectives(this, directives, prevDoc);
 706        if (directivesEndMarker) this.directivesEndMarker = true;
 707        this.range = valueRange ? [valueRange.start, valueRange.end] : null;
 708        this.setSchema();
 709        this.anchors._cstAliases = [];
 710        parseContents(this, contents);
 711        this.anchors.resolveNodes();
 712  
 713        if (this.options.prettyErrors) {
 714          var _iterator = _createForOfIteratorHelper(this.errors),
 715              _step;
 716  
 717          try {
 718            for (_iterator.s(); !(_step = _iterator.n()).done;) {
 719              var _error = _step.value;
 720              if (_error instanceof YAMLError) _error.makePretty();
 721            }
 722          } catch (err) {
 723            _iterator.e(err);
 724          } finally {
 725            _iterator.f();
 726          }
 727  
 728          var _iterator2 = _createForOfIteratorHelper(this.warnings),
 729              _step2;
 730  
 731          try {
 732            for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
 733              var warn = _step2.value;
 734              if (warn instanceof YAMLError) warn.makePretty();
 735            }
 736          } catch (err) {
 737            _iterator2.e(err);
 738          } finally {
 739            _iterator2.f();
 740          }
 741        }
 742  
 743        return this;
 744      }
 745    }, {
 746      key: "listNonDefaultTags",
 747      value: function listNonDefaultTags() {
 748        return listTagNames(this.contents).filter(function (t) {
 749          return t.indexOf(Schema.defaultPrefix) !== 0;
 750        });
 751      }
 752    }, {
 753      key: "setTagPrefix",
 754      value: function setTagPrefix(handle, prefix) {
 755        if (handle[0] !== '!' || handle[handle.length - 1] !== '!') throw new Error('Handle must start and end with !');
 756  
 757        if (prefix) {
 758          var prev = this.tagPrefixes.find(function (p) {
 759            return p.handle === handle;
 760          });
 761          if (prev) prev.prefix = prefix;else this.tagPrefixes.push({
 762            handle: handle,
 763            prefix: prefix
 764          });
 765        } else {
 766          this.tagPrefixes = this.tagPrefixes.filter(function (p) {
 767            return p.handle !== handle;
 768          });
 769        }
 770      }
 771    }, {
 772      key: "toJSON",
 773      value: function toJSON$1(arg, onAnchor) {
 774        var _this = this;
 775  
 776        var _this$options = this.options,
 777            keepBlobsInJSON = _this$options.keepBlobsInJSON,
 778            mapAsMap = _this$options.mapAsMap,
 779            maxAliasCount = _this$options.maxAliasCount;
 780        var keep = keepBlobsInJSON && (typeof arg !== 'string' || !(this.contents instanceof Scalar));
 781        var ctx = {
 782          doc: this,
 783          indentStep: '  ',
 784          keep: keep,
 785          mapAsMap: keep && !!mapAsMap,
 786          maxAliasCount: maxAliasCount,
 787          stringify: stringify$1 // Requiring directly in Pair would create circular dependencies
 788  
 789        };
 790        var anchorNames = Object.keys(this.anchors.map);
 791        if (anchorNames.length > 0) ctx.anchors = new Map(anchorNames.map(function (name) {
 792          return [_this.anchors.map[name], {
 793            alias: [],
 794            aliasCount: 0,
 795            count: 1
 796          }];
 797        }));
 798  
 799        var res = toJSON(this.contents, arg, ctx);
 800  
 801        if (typeof onAnchor === 'function' && ctx.anchors) {
 802          var _iterator3 = _createForOfIteratorHelper(ctx.anchors.values()),
 803              _step3;
 804  
 805          try {
 806            for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
 807              var _step3$value = _step3.value,
 808                  count = _step3$value.count,
 809                  _res = _step3$value.res;
 810              onAnchor(_res, count);
 811            }
 812          } catch (err) {
 813            _iterator3.e(err);
 814          } finally {
 815            _iterator3.f();
 816          }
 817        }
 818  
 819        return res;
 820      }
 821    }, {
 822      key: "toString",
 823      value: function toString() {
 824        if (this.errors.length > 0) throw new Error('Document with errors cannot be stringified');
 825        var indentSize = this.options.indent;
 826  
 827        if (!Number.isInteger(indentSize) || indentSize <= 0) {
 828          var s = JSON.stringify(indentSize);
 829          throw new Error("\"indent\" option must be a positive integer, not ".concat(s));
 830        }
 831  
 832        this.setSchema();
 833        var lines = [];
 834        var hasDirectives = false;
 835  
 836        if (this.version) {
 837          var vd = '%YAML 1.2';
 838  
 839          if (this.schema.name === 'yaml-1.1') {
 840            if (this.version === '1.0') vd = '%YAML:1.0';else if (this.version === '1.1') vd = '%YAML 1.1';
 841          }
 842  
 843          lines.push(vd);
 844          hasDirectives = true;
 845        }
 846  
 847        var tagNames = this.listNonDefaultTags();
 848        this.tagPrefixes.forEach(function (_ref) {
 849          var handle = _ref.handle,
 850              prefix = _ref.prefix;
 851  
 852          if (tagNames.some(function (t) {
 853            return t.indexOf(prefix) === 0;
 854          })) {
 855            lines.push("%TAG ".concat(handle, " ").concat(prefix));
 856            hasDirectives = true;
 857          }
 858        });
 859        if (hasDirectives || this.directivesEndMarker) lines.push('---');
 860  
 861        if (this.commentBefore) {
 862          if (hasDirectives || !this.directivesEndMarker) lines.unshift('');
 863          lines.unshift(this.commentBefore.replace(/^/gm, '#'));
 864        }
 865  
 866        var ctx = {
 867          anchors: Object.create(null),
 868          doc: this,
 869          indent: '',
 870          indentStep: ' '.repeat(indentSize),
 871          stringify: stringify$1 // Requiring directly in nodes would create circular dependencies
 872  
 873        };
 874        var chompKeep = false;
 875        var contentComment = null;
 876  
 877        if (this.contents) {
 878          if (this.contents instanceof Node) {
 879            if (this.contents.spaceBefore && (hasDirectives || this.directivesEndMarker)) lines.push('');
 880            if (this.contents.commentBefore) lines.push(this.contents.commentBefore.replace(/^/gm, '#')); // top-level block scalars need to be indented if followed by a comment
 881  
 882            ctx.forceBlockIndent = !!this.comment;
 883            contentComment = this.contents.comment;
 884          }
 885  
 886          var onChompKeep = contentComment ? null : function () {
 887            return chompKeep = true;
 888          };
 889          var body = stringify$1(this.contents, ctx, function () {
 890            return contentComment = null;
 891          }, onChompKeep);
 892          lines.push(addComment(body, '', contentComment));
 893        } else if (this.contents !== undefined) {
 894          lines.push(stringify$1(this.contents, ctx));
 895        }
 896  
 897        if (this.comment) {
 898          if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '') lines.push('');
 899          lines.push(this.comment.replace(/^/gm, '#'));
 900        }
 901  
 902        return lines.join('\n') + '\n';
 903      }
 904    }]);
 905  
 906    return Document;
 907  }();
 908  
 909  _defineProperty(Document$1, "defaults", documentOptions);
 910  
 911  function createNode(value) {
 912    var wrapScalars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
 913    var tag = arguments.length > 2 ? arguments[2] : undefined;
 914  
 915    if (tag === undefined && typeof wrapScalars === 'string') {
 916      tag = wrapScalars;
 917      wrapScalars = true;
 918    }
 919  
 920    var options = Object.assign({}, Document$1.defaults[defaultOptions.version], defaultOptions);
 921    var schema = new Schema(options);
 922    return schema.createNode(value, wrapScalars, tag);
 923  }
 924  
 925  var Document = /*#__PURE__*/function (_YAMLDocument) {
 926    _inherits(Document, _YAMLDocument);
 927  
 928    var _super = _createSuper(Document);
 929  
 930    function Document(options) {
 931      _classCallCheck(this, Document);
 932  
 933      return _super.call(this, Object.assign({}, defaultOptions, options));
 934    }
 935  
 936    return Document;
 937  }(Document$1);
 938  
 939  function parseAllDocuments(src, options) {
 940    var stream = [];
 941    var prev;
 942  
 943    var _iterator = _createForOfIteratorHelper(parse$1(src)),
 944        _step;
 945  
 946    try {
 947      for (_iterator.s(); !(_step = _iterator.n()).done;) {
 948        var cstDoc = _step.value;
 949        var doc = new Document(options);
 950        doc.parse(cstDoc, prev);
 951        stream.push(doc);
 952        prev = doc;
 953      }
 954    } catch (err) {
 955      _iterator.e(err);
 956    } finally {
 957      _iterator.f();
 958    }
 959  
 960    return stream;
 961  }
 962  
 963  function parseDocument(src, options) {
 964    var cst = parse$1(src);
 965    var doc = new Document(options).parse(cst[0]);
 966  
 967    if (cst.length > 1) {
 968      var errMsg = 'Source contains multiple documents; please use YAML.parseAllDocuments()';
 969      doc.errors.unshift(new YAMLSemanticError(cst[1], errMsg));
 970    }
 971  
 972    return doc;
 973  }
 974  
 975  function parse(src, options) {
 976    var doc = parseDocument(src, options);
 977    doc.warnings.forEach(function (warning) {
 978      return warn(warning);
 979    });
 980    if (doc.errors.length > 0) throw doc.errors[0];
 981    return doc.toJSON();
 982  }
 983  
 984  function stringify(value, options) {
 985    var doc = new Document(options);
 986    doc.contents = value;
 987    return String(doc);
 988  }
 989  
 990  var YAML = {
 991    createNode: createNode,
 992    defaultOptions: defaultOptions,
 993    Document: Document,
 994    parse: parse,
 995    parseAllDocuments: parseAllDocuments,
 996    parseCST: parse$1,
 997    parseDocument: parseDocument,
 998    scalarOptions: scalarOptions,
 999    stringify: stringify
1000  };
1001  
1002  export { YAML };