index.js 27 KB

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