index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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. 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$1(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. function Anchors(prefix) {
  208. _classCallCheck(this, Anchors);
  209. _defineProperty(this, "map", Object.create(null));
  210. this.prefix = prefix;
  211. }
  212. _createClass(Anchors, [{
  213. key: "createAlias",
  214. value: function createAlias(node, name) {
  215. this.setAnchor(node, name);
  216. return new Alias(node);
  217. }
  218. }, {
  219. key: "createMergePair",
  220. value: function createMergePair() {
  221. var _this = this;
  222. var merge = new Merge();
  223. for (var _len = arguments.length, sources = new Array(_len), _key = 0; _key < _len; _key++) {
  224. sources[_key] = arguments[_key];
  225. }
  226. merge.value.items = sources.map(function (s) {
  227. if (s instanceof Alias) {
  228. if (s.source instanceof YAMLMap) return s;
  229. } else if (s instanceof YAMLMap) {
  230. return _this.createAlias(s);
  231. }
  232. throw new Error('Merge sources must be Map nodes or their Aliases');
  233. });
  234. return merge;
  235. }
  236. }, {
  237. key: "getName",
  238. value: function getName(node) {
  239. var map = this.map;
  240. return Object.keys(map).find(function (a) {
  241. return map[a] === node;
  242. });
  243. }
  244. }, {
  245. key: "getNames",
  246. value: function getNames() {
  247. return Object.keys(this.map);
  248. }
  249. }, {
  250. key: "getNode",
  251. value: function getNode(name) {
  252. return this.map[name];
  253. }
  254. }, {
  255. key: "newName",
  256. value: function newName(prefix) {
  257. if (!prefix) prefix = this.prefix;
  258. var names = Object.keys(this.map);
  259. for (var i = 1; true; ++i) {
  260. var name = "".concat(prefix).concat(i);
  261. if (!names.includes(name)) return name;
  262. }
  263. } // During parsing, map & aliases contain CST nodes
  264. }, {
  265. key: "resolveNodes",
  266. value: function resolveNodes() {
  267. var map = this.map,
  268. _cstAliases = this._cstAliases;
  269. Object.keys(map).forEach(function (a) {
  270. map[a] = map[a].resolved;
  271. });
  272. _cstAliases.forEach(function (a) {
  273. a.source = a.source.resolved;
  274. });
  275. delete this._cstAliases;
  276. }
  277. }, {
  278. key: "setAnchor",
  279. value: function setAnchor(node, name) {
  280. if (node != null && !Anchors.validAnchorNode(node)) {
  281. throw new Error('Anchors may only be set for Scalar, Seq and Map nodes');
  282. }
  283. if (name && /[\x00-\x19\s,[\]{}]/.test(name)) {
  284. throw new Error('Anchor names must not contain whitespace or control characters');
  285. }
  286. var map = this.map;
  287. var prev = node && Object.keys(map).find(function (a) {
  288. return map[a] === node;
  289. });
  290. if (prev) {
  291. if (!name) {
  292. return prev;
  293. } else if (prev !== name) {
  294. delete map[prev];
  295. map[name] = node;
  296. }
  297. } else {
  298. if (!name) {
  299. if (!node) return null;
  300. name = this.newName();
  301. }
  302. map[name] = node;
  303. }
  304. return name;
  305. }
  306. }], [{
  307. key: "validAnchorNode",
  308. value: function validAnchorNode(node) {
  309. return node instanceof Scalar || node instanceof YAMLSeq || node instanceof YAMLMap;
  310. }
  311. }]);
  312. return Anchors;
  313. }();
  314. var visit = function visit(node, tags) {
  315. if (node && _typeof(node) === 'object') {
  316. var tag = node.tag;
  317. if (node instanceof Collection) {
  318. if (tag) tags[tag] = true;
  319. node.items.forEach(function (n) {
  320. return visit(n, tags);
  321. });
  322. } else if (node instanceof Pair) {
  323. visit(node.key, tags);
  324. visit(node.value, tags);
  325. } else if (node instanceof Scalar) {
  326. if (tag) tags[tag] = true;
  327. }
  328. }
  329. return tags;
  330. };
  331. var listTagNames = function listTagNames(node) {
  332. return Object.keys(visit(node, {}));
  333. };
  334. function parseContents(doc, contents) {
  335. var comments = {
  336. before: [],
  337. after: []
  338. };
  339. var body = undefined;
  340. var spaceBefore = false;
  341. var _iterator = _createForOfIteratorHelper(contents),
  342. _step;
  343. try {
  344. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  345. var node = _step.value;
  346. if (node.valueRange) {
  347. if (body !== undefined) {
  348. var msg = 'Document contains trailing content not separated by a ... or --- line';
  349. doc.errors.push(new YAMLSyntaxError(node, msg));
  350. break;
  351. }
  352. var res = resolveNode(doc, node);
  353. if (spaceBefore) {
  354. res.spaceBefore = true;
  355. spaceBefore = false;
  356. }
  357. body = res;
  358. } else if (node.comment !== null) {
  359. var cc = body === undefined ? comments.before : comments.after;
  360. cc.push(node.comment);
  361. } else if (node.type === Type.BLANK_LINE) {
  362. spaceBefore = true;
  363. if (body === undefined && comments.before.length > 0 && !doc.commentBefore) {
  364. // space-separated comments at start are parsed as document comments
  365. doc.commentBefore = comments.before.join('\n');
  366. comments.before = [];
  367. }
  368. }
  369. }
  370. } catch (err) {
  371. _iterator.e(err);
  372. } finally {
  373. _iterator.f();
  374. }
  375. doc.contents = body || null;
  376. if (!body) {
  377. doc.comment = comments.before.concat(comments.after).join('\n') || null;
  378. } else {
  379. var cb = comments.before.join('\n');
  380. if (cb) {
  381. var cbNode = body instanceof Collection && body.items[0] ? body.items[0] : body;
  382. cbNode.commentBefore = cbNode.commentBefore ? "".concat(cb, "\n").concat(cbNode.commentBefore) : cb;
  383. }
  384. doc.comment = comments.after.join('\n') || null;
  385. }
  386. }
  387. function resolveTagDirective(_ref, directive) {
  388. var tagPrefixes = _ref.tagPrefixes;
  389. var _directive$parameters = _slicedToArray(directive.parameters, 2),
  390. handle = _directive$parameters[0],
  391. prefix = _directive$parameters[1];
  392. if (!handle || !prefix) {
  393. var msg = 'Insufficient parameters given for %TAG directive';
  394. throw new YAMLSemanticError(directive, msg);
  395. }
  396. if (tagPrefixes.some(function (p) {
  397. return p.handle === handle;
  398. })) {
  399. var _msg = 'The %TAG directive must only be given at most once per handle in the same document.';
  400. throw new YAMLSemanticError(directive, _msg);
  401. }
  402. return {
  403. handle: handle,
  404. prefix: prefix
  405. };
  406. }
  407. function resolveYamlDirective(doc, directive) {
  408. var _directive$parameters2 = _slicedToArray(directive.parameters, 1),
  409. version = _directive$parameters2[0];
  410. if (directive.name === 'YAML:1.0') version = '1.0';
  411. if (!version) {
  412. var msg = 'Insufficient parameters given for %YAML directive';
  413. throw new YAMLSemanticError(directive, msg);
  414. }
  415. if (!documentOptions[version]) {
  416. var v0 = doc.version || doc.options.version;
  417. var _msg2 = "Document will be parsed as YAML ".concat(v0, " rather than YAML ").concat(version);
  418. doc.warnings.push(new YAMLWarning(directive, _msg2));
  419. }
  420. return version;
  421. }
  422. function parseDirectives(doc, directives, prevDoc) {
  423. var directiveComments = [];
  424. var hasDirectives = false;
  425. var _iterator = _createForOfIteratorHelper(directives),
  426. _step;
  427. try {
  428. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  429. var directive = _step.value;
  430. var comment = directive.comment,
  431. name = directive.name;
  432. switch (name) {
  433. case 'TAG':
  434. try {
  435. doc.tagPrefixes.push(resolveTagDirective(doc, directive));
  436. } catch (error) {
  437. doc.errors.push(error);
  438. }
  439. hasDirectives = true;
  440. break;
  441. case 'YAML':
  442. case 'YAML:1.0':
  443. if (doc.version) {
  444. var msg = 'The %YAML directive must only be given at most once per document.';
  445. doc.errors.push(new YAMLSemanticError(directive, msg));
  446. }
  447. try {
  448. doc.version = resolveYamlDirective(doc, directive);
  449. } catch (error) {
  450. doc.errors.push(error);
  451. }
  452. hasDirectives = true;
  453. break;
  454. default:
  455. if (name) {
  456. var _msg3 = "YAML only supports %TAG and %YAML directives, and not %".concat(name);
  457. doc.warnings.push(new YAMLWarning(directive, _msg3));
  458. }
  459. }
  460. if (comment) directiveComments.push(comment);
  461. }
  462. } catch (err) {
  463. _iterator.e(err);
  464. } finally {
  465. _iterator.f();
  466. }
  467. if (prevDoc && !hasDirectives && '1.1' === (doc.version || prevDoc.version || doc.options.version)) {
  468. var copyTagPrefix = function copyTagPrefix(_ref2) {
  469. var handle = _ref2.handle,
  470. prefix = _ref2.prefix;
  471. return {
  472. handle: handle,
  473. prefix: prefix
  474. };
  475. };
  476. doc.tagPrefixes = prevDoc.tagPrefixes.map(copyTagPrefix);
  477. doc.version = prevDoc.version;
  478. }
  479. doc.commentBefore = directiveComments.join('\n') || null;
  480. }
  481. function assertCollection(contents) {
  482. if (contents instanceof Collection) return true;
  483. throw new Error('Expected a YAML collection as document contents');
  484. }
  485. var Document$1 = /*#__PURE__*/function () {
  486. function Document(options) {
  487. _classCallCheck(this, Document);
  488. this.anchors = new Anchors(options.anchorPrefix);
  489. this.commentBefore = null;
  490. this.comment = null;
  491. this.contents = null;
  492. this.directivesEndMarker = null;
  493. this.errors = [];
  494. this.options = options;
  495. this.schema = null;
  496. this.tagPrefixes = [];
  497. this.version = null;
  498. this.warnings = [];
  499. }
  500. _createClass(Document, [{
  501. key: "add",
  502. value: function add(value) {
  503. assertCollection(this.contents);
  504. return this.contents.add(value);
  505. }
  506. }, {
  507. key: "addIn",
  508. value: function addIn(path, value) {
  509. assertCollection(this.contents);
  510. this.contents.addIn(path, value);
  511. }
  512. }, {
  513. key: "delete",
  514. value: function _delete(key) {
  515. assertCollection(this.contents);
  516. return this.contents.delete(key);
  517. }
  518. }, {
  519. key: "deleteIn",
  520. value: function deleteIn(path) {
  521. if (isEmptyPath(path)) {
  522. if (this.contents == null) return false;
  523. this.contents = null;
  524. return true;
  525. }
  526. assertCollection(this.contents);
  527. return this.contents.deleteIn(path);
  528. }
  529. }, {
  530. key: "getDefaults",
  531. value: function getDefaults() {
  532. return Document.defaults[this.version] || Document.defaults[this.options.version] || {};
  533. }
  534. }, {
  535. key: "get",
  536. value: function get(key, keepScalar) {
  537. return this.contents instanceof Collection ? this.contents.get(key, keepScalar) : undefined;
  538. }
  539. }, {
  540. key: "getIn",
  541. value: function getIn(path, keepScalar) {
  542. if (isEmptyPath(path)) return !keepScalar && this.contents instanceof Scalar ? this.contents.value : this.contents;
  543. return this.contents instanceof Collection ? this.contents.getIn(path, keepScalar) : undefined;
  544. }
  545. }, {
  546. key: "has",
  547. value: function has(key) {
  548. return this.contents instanceof Collection ? this.contents.has(key) : false;
  549. }
  550. }, {
  551. key: "hasIn",
  552. value: function hasIn(path) {
  553. if (isEmptyPath(path)) return this.contents !== undefined;
  554. return this.contents instanceof Collection ? this.contents.hasIn(path) : false;
  555. }
  556. }, {
  557. key: "set",
  558. value: function set(key, value) {
  559. assertCollection(this.contents);
  560. this.contents.set(key, value);
  561. }
  562. }, {
  563. key: "setIn",
  564. value: function setIn(path, value) {
  565. if (isEmptyPath(path)) this.contents = value;else {
  566. assertCollection(this.contents);
  567. this.contents.setIn(path, value);
  568. }
  569. }
  570. }, {
  571. key: "setSchema",
  572. value: function setSchema(id, customTags) {
  573. if (!id && !customTags && this.schema) return;
  574. if (typeof id === 'number') id = id.toFixed(1);
  575. if (id === '1.0' || id === '1.1' || id === '1.2') {
  576. if (this.version) this.version = id;else this.options.version = id;
  577. delete this.options.schema;
  578. } else if (id && typeof id === 'string') {
  579. this.options.schema = id;
  580. }
  581. if (Array.isArray(customTags)) this.options.customTags = customTags;
  582. var opt = Object.assign({}, this.getDefaults(), this.options);
  583. this.schema = new Schema(opt);
  584. }
  585. }, {
  586. key: "parse",
  587. value: function parse(node, prevDoc) {
  588. if (this.options.keepCstNodes) this.cstNode = node;
  589. if (this.options.keepNodeTypes) this.type = 'DOCUMENT';
  590. var _node$directives = node.directives,
  591. directives = _node$directives === void 0 ? [] : _node$directives,
  592. _node$contents = node.contents,
  593. contents = _node$contents === void 0 ? [] : _node$contents,
  594. directivesEndMarker = node.directivesEndMarker,
  595. error = node.error,
  596. valueRange = node.valueRange;
  597. if (error) {
  598. if (!error.source) error.source = this;
  599. this.errors.push(error);
  600. }
  601. parseDirectives(this, directives, prevDoc);
  602. if (directivesEndMarker) this.directivesEndMarker = true;
  603. this.range = valueRange ? [valueRange.start, valueRange.end] : null;
  604. this.setSchema();
  605. this.anchors._cstAliases = [];
  606. parseContents(this, contents);
  607. this.anchors.resolveNodes();
  608. if (this.options.prettyErrors) {
  609. var _iterator = _createForOfIteratorHelper(this.errors),
  610. _step;
  611. try {
  612. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  613. var _error = _step.value;
  614. if (_error instanceof YAMLError) _error.makePretty();
  615. }
  616. } catch (err) {
  617. _iterator.e(err);
  618. } finally {
  619. _iterator.f();
  620. }
  621. var _iterator2 = _createForOfIteratorHelper(this.warnings),
  622. _step2;
  623. try {
  624. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  625. var warn = _step2.value;
  626. if (warn instanceof YAMLError) warn.makePretty();
  627. }
  628. } catch (err) {
  629. _iterator2.e(err);
  630. } finally {
  631. _iterator2.f();
  632. }
  633. }
  634. return this;
  635. }
  636. }, {
  637. key: "listNonDefaultTags",
  638. value: function listNonDefaultTags() {
  639. return listTagNames(this.contents).filter(function (t) {
  640. return t.indexOf(Schema.defaultPrefix) !== 0;
  641. });
  642. }
  643. }, {
  644. key: "setTagPrefix",
  645. value: function setTagPrefix(handle, prefix) {
  646. if (handle[0] !== '!' || handle[handle.length - 1] !== '!') throw new Error('Handle must start and end with !');
  647. if (prefix) {
  648. var prev = this.tagPrefixes.find(function (p) {
  649. return p.handle === handle;
  650. });
  651. if (prev) prev.prefix = prefix;else this.tagPrefixes.push({
  652. handle: handle,
  653. prefix: prefix
  654. });
  655. } else {
  656. this.tagPrefixes = this.tagPrefixes.filter(function (p) {
  657. return p.handle !== handle;
  658. });
  659. }
  660. }
  661. }, {
  662. key: "toJSON",
  663. value: function toJSON$1(arg, onAnchor) {
  664. var _this = this;
  665. var _this$options = this.options,
  666. keepBlobsInJSON = _this$options.keepBlobsInJSON,
  667. mapAsMap = _this$options.mapAsMap,
  668. maxAliasCount = _this$options.maxAliasCount;
  669. var keep = keepBlobsInJSON && (typeof arg !== 'string' || !(this.contents instanceof Scalar));
  670. var ctx = {
  671. doc: this,
  672. indentStep: ' ',
  673. keep: keep,
  674. mapAsMap: keep && !!mapAsMap,
  675. maxAliasCount: maxAliasCount,
  676. stringify: stringify$1 // Requiring directly in Pair would create circular dependencies
  677. };
  678. var anchorNames = Object.keys(this.anchors.map);
  679. if (anchorNames.length > 0) ctx.anchors = new Map(anchorNames.map(function (name) {
  680. return [_this.anchors.map[name], {
  681. alias: [],
  682. aliasCount: 0,
  683. count: 1
  684. }];
  685. }));
  686. var res = toJSON(this.contents, arg, ctx);
  687. if (typeof onAnchor === 'function' && ctx.anchors) {
  688. var _iterator3 = _createForOfIteratorHelper(ctx.anchors.values()),
  689. _step3;
  690. try {
  691. for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
  692. var _step3$value = _step3.value,
  693. count = _step3$value.count,
  694. _res = _step3$value.res;
  695. onAnchor(_res, count);
  696. }
  697. } catch (err) {
  698. _iterator3.e(err);
  699. } finally {
  700. _iterator3.f();
  701. }
  702. }
  703. return res;
  704. }
  705. }, {
  706. key: "toString",
  707. value: function toString() {
  708. if (this.errors.length > 0) throw new Error('Document with errors cannot be stringified');
  709. var indentSize = this.options.indent;
  710. if (!Number.isInteger(indentSize) || indentSize <= 0) {
  711. var s = JSON.stringify(indentSize);
  712. throw new Error("\"indent\" option must be a positive integer, not ".concat(s));
  713. }
  714. this.setSchema();
  715. var lines = [];
  716. var hasDirectives = false;
  717. if (this.version) {
  718. var vd = '%YAML 1.2';
  719. if (this.schema.name === 'yaml-1.1') {
  720. if (this.version === '1.0') vd = '%YAML:1.0';else if (this.version === '1.1') vd = '%YAML 1.1';
  721. }
  722. lines.push(vd);
  723. hasDirectives = true;
  724. }
  725. var tagNames = this.listNonDefaultTags();
  726. this.tagPrefixes.forEach(function (_ref) {
  727. var handle = _ref.handle,
  728. prefix = _ref.prefix;
  729. if (tagNames.some(function (t) {
  730. return t.indexOf(prefix) === 0;
  731. })) {
  732. lines.push("%TAG ".concat(handle, " ").concat(prefix));
  733. hasDirectives = true;
  734. }
  735. });
  736. if (hasDirectives || this.directivesEndMarker) lines.push('---');
  737. if (this.commentBefore) {
  738. if (hasDirectives || !this.directivesEndMarker) lines.unshift('');
  739. lines.unshift(this.commentBefore.replace(/^/gm, '#'));
  740. }
  741. var ctx = {
  742. anchors: Object.create(null),
  743. doc: this,
  744. indent: '',
  745. indentStep: ' '.repeat(indentSize),
  746. stringify: stringify$1 // Requiring directly in nodes would create circular dependencies
  747. };
  748. var chompKeep = false;
  749. var contentComment = null;
  750. if (this.contents) {
  751. if (this.contents instanceof Node) {
  752. if (this.contents.spaceBefore && (hasDirectives || this.directivesEndMarker)) lines.push('');
  753. if (this.contents.commentBefore) lines.push(this.contents.commentBefore.replace(/^/gm, '#')); // top-level block scalars need to be indented if followed by a comment
  754. ctx.forceBlockIndent = !!this.comment;
  755. contentComment = this.contents.comment;
  756. }
  757. var onChompKeep = contentComment ? null : function () {
  758. return chompKeep = true;
  759. };
  760. var body = stringify$1(this.contents, ctx, function () {
  761. return contentComment = null;
  762. }, onChompKeep);
  763. lines.push(addComment(body, '', contentComment));
  764. } else if (this.contents !== undefined) {
  765. lines.push(stringify$1(this.contents, ctx));
  766. }
  767. if (this.comment) {
  768. if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '') lines.push('');
  769. lines.push(this.comment.replace(/^/gm, '#'));
  770. }
  771. return lines.join('\n') + '\n';
  772. }
  773. }]);
  774. return Document;
  775. }();
  776. _defineProperty(Document$1, "defaults", documentOptions);
  777. function createNode(value) {
  778. var wrapScalars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  779. var tag = arguments.length > 2 ? arguments[2] : undefined;
  780. if (tag === undefined && typeof wrapScalars === 'string') {
  781. tag = wrapScalars;
  782. wrapScalars = true;
  783. }
  784. var options = Object.assign({}, Document$1.defaults[defaultOptions.version], defaultOptions);
  785. var schema = new Schema(options);
  786. return schema.createNode(value, wrapScalars, tag);
  787. }
  788. var Document = /*#__PURE__*/function (_YAMLDocument) {
  789. _inherits(Document, _YAMLDocument);
  790. var _super = _createSuper(Document);
  791. function Document(options) {
  792. _classCallCheck(this, Document);
  793. return _super.call(this, Object.assign({}, defaultOptions, options));
  794. }
  795. return Document;
  796. }(Document$1);
  797. function parseAllDocuments(src, options) {
  798. var stream = [];
  799. var prev;
  800. var _iterator = _createForOfIteratorHelper(parse$1(src)),
  801. _step;
  802. try {
  803. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  804. var cstDoc = _step.value;
  805. var doc = new Document(options);
  806. doc.parse(cstDoc, prev);
  807. stream.push(doc);
  808. prev = doc;
  809. }
  810. } catch (err) {
  811. _iterator.e(err);
  812. } finally {
  813. _iterator.f();
  814. }
  815. return stream;
  816. }
  817. function parseDocument(src, options) {
  818. var cst = parse$1(src);
  819. var doc = new Document(options).parse(cst[0]);
  820. if (cst.length > 1) {
  821. var errMsg = 'Source contains multiple documents; please use YAML.parseAllDocuments()';
  822. doc.errors.unshift(new YAMLSemanticError(cst[1], errMsg));
  823. }
  824. return doc;
  825. }
  826. function parse(src, options) {
  827. var doc = parseDocument(src, options);
  828. doc.warnings.forEach(function (warning) {
  829. return warn(warning);
  830. });
  831. if (doc.errors.length > 0) throw doc.errors[0];
  832. return doc.toJSON();
  833. }
  834. function stringify(value, options) {
  835. var doc = new Document(options);
  836. doc.contents = value;
  837. return String(doc);
  838. }
  839. var YAML = {
  840. createNode: createNode,
  841. defaultOptions: defaultOptions,
  842. Document: Document,
  843. parse: parse,
  844. parseAllDocuments: parseAllDocuments,
  845. parseCST: parse$1,
  846. parseDocument: parseDocument,
  847. scalarOptions: scalarOptions,
  848. stringify: stringify
  849. };
  850. export { YAML };