resolveSeq-492ab440.js 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419
  1. import { c as _classCallCheck, j as _inherits, k as _createSuper, b as _createClass, e as _defineProperty, p as _assertThisInitialized, a as _typeof, q as _toArray, T as Type, _ as _createForOfIteratorHelper, l as _get, m as _getPrototypeOf, o as YAMLReferenceError, r as _possibleConstructorReturn, h as _slicedToArray, g as YAMLSemanticError, n as defaultTags, f as YAMLWarning, C as Char, Y as YAMLSyntaxError, P as PlainValue } from './PlainValue-b8036b75.js';
  2. function addCommentBefore(str, indent, comment) {
  3. if (!comment) return str;
  4. var cc = comment.replace(/[\s\S]^/gm, "$&".concat(indent, "#"));
  5. return "#".concat(cc, "\n").concat(indent).concat(str);
  6. }
  7. function addComment(str, indent, comment) {
  8. return !comment ? str : comment.indexOf('\n') === -1 ? "".concat(str, " #").concat(comment) : "".concat(str, "\n") + comment.replace(/^/gm, "".concat(indent || '', "#"));
  9. }
  10. var Node = function Node() {
  11. _classCallCheck(this, Node);
  12. };
  13. function toJSON(value, arg, ctx) {
  14. if (Array.isArray(value)) return value.map(function (v, i) {
  15. return toJSON(v, String(i), ctx);
  16. });
  17. if (value && typeof value.toJSON === 'function') {
  18. var anchor = ctx && ctx.anchors && ctx.anchors.get(value);
  19. if (anchor) ctx.onCreate = function (res) {
  20. anchor.res = res;
  21. delete ctx.onCreate;
  22. };
  23. var res = value.toJSON(arg, ctx);
  24. if (anchor && ctx.onCreate) ctx.onCreate(res);
  25. return res;
  26. }
  27. if ((!ctx || !ctx.keep) && typeof value === 'bigint') return Number(value);
  28. return value;
  29. }
  30. var Scalar = /*#__PURE__*/function (_Node) {
  31. _inherits(Scalar, _Node);
  32. var _super = _createSuper(Scalar);
  33. function Scalar(value) {
  34. var _this;
  35. _classCallCheck(this, Scalar);
  36. _this = _super.call(this);
  37. _this.value = value;
  38. return _this;
  39. }
  40. _createClass(Scalar, [{
  41. key: "toJSON",
  42. value: function toJSON$1(arg, ctx) {
  43. return ctx && ctx.keep ? this.value : toJSON(this.value, arg, ctx);
  44. }
  45. }, {
  46. key: "toString",
  47. value: function toString() {
  48. return String(this.value);
  49. }
  50. }]);
  51. return Scalar;
  52. }(Node);
  53. function collectionFromPath(schema, path, value) {
  54. var v = value;
  55. for (var i = path.length - 1; i >= 0; --i) {
  56. var k = path[i];
  57. if (Number.isInteger(k) && k >= 0) {
  58. var a = [];
  59. a[k] = v;
  60. v = a;
  61. } else {
  62. var o = {};
  63. Object.defineProperty(o, k, {
  64. value: v,
  65. writable: true,
  66. enumerable: true,
  67. configurable: true
  68. });
  69. v = o;
  70. }
  71. }
  72. return schema.createNode(v, false);
  73. } // null, undefined, or an empty non-string iterable (e.g. [])
  74. var isEmptyPath = function isEmptyPath(path) {
  75. return path == null || _typeof(path) === 'object' && path[Symbol.iterator]().next().done;
  76. };
  77. var Collection = /*#__PURE__*/function (_Node) {
  78. _inherits(Collection, _Node);
  79. var _super = _createSuper(Collection);
  80. function Collection(schema) {
  81. var _this;
  82. _classCallCheck(this, Collection);
  83. _this = _super.call(this);
  84. _defineProperty(_assertThisInitialized(_this), "items", []);
  85. _this.schema = schema;
  86. return _this;
  87. }
  88. _createClass(Collection, [{
  89. key: "addIn",
  90. value: function addIn(path, value) {
  91. if (isEmptyPath(path)) this.add(value);else {
  92. var _path = _toArray(path),
  93. key = _path[0],
  94. rest = _path.slice(1);
  95. var node = this.get(key, true);
  96. if (node instanceof Collection) node.addIn(rest, value);else if (node === undefined && this.schema) this.set(key, collectionFromPath(this.schema, rest, value));else throw new Error("Expected YAML collection at ".concat(key, ". Remaining path: ").concat(rest));
  97. }
  98. }
  99. }, {
  100. key: "deleteIn",
  101. value: function deleteIn(_ref) {
  102. var _ref2 = _toArray(_ref),
  103. key = _ref2[0],
  104. rest = _ref2.slice(1);
  105. if (rest.length === 0) return this.delete(key);
  106. var node = this.get(key, true);
  107. if (node instanceof Collection) return node.deleteIn(rest);else throw new Error("Expected YAML collection at ".concat(key, ". Remaining path: ").concat(rest));
  108. }
  109. }, {
  110. key: "getIn",
  111. value: function getIn(_ref3, keepScalar) {
  112. var _ref4 = _toArray(_ref3),
  113. key = _ref4[0],
  114. rest = _ref4.slice(1);
  115. var node = this.get(key, true);
  116. if (rest.length === 0) return !keepScalar && node instanceof Scalar ? node.value : node;else return node instanceof Collection ? node.getIn(rest, keepScalar) : undefined;
  117. }
  118. }, {
  119. key: "hasAllNullValues",
  120. value: function hasAllNullValues() {
  121. return this.items.every(function (node) {
  122. if (!node || node.type !== 'PAIR') return false;
  123. var n = node.value;
  124. return n == null || n instanceof Scalar && n.value == null && !n.commentBefore && !n.comment && !n.tag;
  125. });
  126. }
  127. }, {
  128. key: "hasIn",
  129. value: function hasIn(_ref5) {
  130. var _ref6 = _toArray(_ref5),
  131. key = _ref6[0],
  132. rest = _ref6.slice(1);
  133. if (rest.length === 0) return this.has(key);
  134. var node = this.get(key, true);
  135. return node instanceof Collection ? node.hasIn(rest) : false;
  136. }
  137. }, {
  138. key: "setIn",
  139. value: function setIn(_ref7, value) {
  140. var _ref8 = _toArray(_ref7),
  141. key = _ref8[0],
  142. rest = _ref8.slice(1);
  143. if (rest.length === 0) {
  144. this.set(key, value);
  145. } else {
  146. var node = this.get(key, true);
  147. if (node instanceof Collection) node.setIn(rest, value);else if (node === undefined && this.schema) this.set(key, collectionFromPath(this.schema, rest, value));else throw new Error("Expected YAML collection at ".concat(key, ". Remaining path: ").concat(rest));
  148. }
  149. } // overridden in implementations
  150. /* istanbul ignore next */
  151. }, {
  152. key: "toJSON",
  153. value: function toJSON() {
  154. return null;
  155. }
  156. }, {
  157. key: "toString",
  158. value: function toString(ctx, _ref9, onComment, onChompKeep) {
  159. var _this2 = this;
  160. var blockItem = _ref9.blockItem,
  161. flowChars = _ref9.flowChars,
  162. isMap = _ref9.isMap,
  163. itemIndent = _ref9.itemIndent;
  164. var _ctx = ctx,
  165. indent = _ctx.indent,
  166. indentStep = _ctx.indentStep,
  167. stringify = _ctx.stringify;
  168. var inFlow = this.type === Type.FLOW_MAP || this.type === Type.FLOW_SEQ || ctx.inFlow;
  169. if (inFlow) itemIndent += indentStep;
  170. var allNullValues = isMap && this.hasAllNullValues();
  171. ctx = Object.assign({}, ctx, {
  172. allNullValues: allNullValues,
  173. indent: itemIndent,
  174. inFlow: inFlow,
  175. type: null
  176. });
  177. var chompKeep = false;
  178. var hasItemWithNewLine = false;
  179. var nodes = this.items.reduce(function (nodes, item, i) {
  180. var comment;
  181. if (item) {
  182. if (!chompKeep && item.spaceBefore) nodes.push({
  183. type: 'comment',
  184. str: ''
  185. });
  186. if (item.commentBefore) item.commentBefore.match(/^.*$/gm).forEach(function (line) {
  187. nodes.push({
  188. type: 'comment',
  189. str: "#".concat(line)
  190. });
  191. });
  192. if (item.comment) comment = item.comment;
  193. if (inFlow && (!chompKeep && item.spaceBefore || item.commentBefore || item.comment || item.key && (item.key.commentBefore || item.key.comment) || item.value && (item.value.commentBefore || item.value.comment))) hasItemWithNewLine = true;
  194. }
  195. chompKeep = false;
  196. var str = stringify(item, ctx, function () {
  197. return comment = null;
  198. }, function () {
  199. return chompKeep = true;
  200. });
  201. if (inFlow && !hasItemWithNewLine && str.includes('\n')) hasItemWithNewLine = true;
  202. if (inFlow && i < _this2.items.length - 1) str += ',';
  203. str = addComment(str, itemIndent, comment);
  204. if (chompKeep && (comment || inFlow)) chompKeep = false;
  205. nodes.push({
  206. type: 'item',
  207. str: str
  208. });
  209. return nodes;
  210. }, []);
  211. var str;
  212. if (nodes.length === 0) {
  213. str = flowChars.start + flowChars.end;
  214. } else if (inFlow) {
  215. var start = flowChars.start,
  216. end = flowChars.end;
  217. var strings = nodes.map(function (n) {
  218. return n.str;
  219. });
  220. if (hasItemWithNewLine || strings.reduce(function (sum, str) {
  221. return sum + str.length + 2;
  222. }, 2) > Collection.maxFlowStringSingleLineLength) {
  223. str = start;
  224. var _iterator = _createForOfIteratorHelper(strings),
  225. _step;
  226. try {
  227. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  228. var s = _step.value;
  229. str += s ? "\n".concat(indentStep).concat(indent).concat(s) : '\n';
  230. }
  231. } catch (err) {
  232. _iterator.e(err);
  233. } finally {
  234. _iterator.f();
  235. }
  236. str += "\n".concat(indent).concat(end);
  237. } else {
  238. str = "".concat(start, " ").concat(strings.join(' '), " ").concat(end);
  239. }
  240. } else {
  241. var _strings = nodes.map(blockItem);
  242. str = _strings.shift();
  243. var _iterator2 = _createForOfIteratorHelper(_strings),
  244. _step2;
  245. try {
  246. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  247. var _s = _step2.value;
  248. str += _s ? "\n".concat(indent).concat(_s) : '\n';
  249. }
  250. } catch (err) {
  251. _iterator2.e(err);
  252. } finally {
  253. _iterator2.f();
  254. }
  255. }
  256. if (this.comment) {
  257. str += '\n' + this.comment.replace(/^/gm, "".concat(indent, "#"));
  258. if (onComment) onComment();
  259. } else if (chompKeep && onChompKeep) onChompKeep();
  260. return str;
  261. }
  262. }]);
  263. return Collection;
  264. }(Node);
  265. _defineProperty(Collection, "maxFlowStringSingleLineLength", 60);
  266. function asItemIndex(key) {
  267. var idx = key instanceof Scalar ? key.value : key;
  268. if (idx && typeof idx === 'string') idx = Number(idx);
  269. return Number.isInteger(idx) && idx >= 0 ? idx : null;
  270. }
  271. var YAMLSeq = /*#__PURE__*/function (_Collection) {
  272. _inherits(YAMLSeq, _Collection);
  273. var _super = _createSuper(YAMLSeq);
  274. function YAMLSeq() {
  275. _classCallCheck(this, YAMLSeq);
  276. return _super.apply(this, arguments);
  277. }
  278. _createClass(YAMLSeq, [{
  279. key: "add",
  280. value: function add(value) {
  281. this.items.push(value);
  282. }
  283. }, {
  284. key: "delete",
  285. value: function _delete(key) {
  286. var idx = asItemIndex(key);
  287. if (typeof idx !== 'number') return false;
  288. var del = this.items.splice(idx, 1);
  289. return del.length > 0;
  290. }
  291. }, {
  292. key: "get",
  293. value: function get(key, keepScalar) {
  294. var idx = asItemIndex(key);
  295. if (typeof idx !== 'number') return undefined;
  296. var it = this.items[idx];
  297. return !keepScalar && it instanceof Scalar ? it.value : it;
  298. }
  299. }, {
  300. key: "has",
  301. value: function has(key) {
  302. var idx = asItemIndex(key);
  303. return typeof idx === 'number' && idx < this.items.length;
  304. }
  305. }, {
  306. key: "set",
  307. value: function set(key, value) {
  308. var idx = asItemIndex(key);
  309. if (typeof idx !== 'number') throw new Error("Expected a valid index, not ".concat(key, "."));
  310. this.items[idx] = value;
  311. }
  312. }, {
  313. key: "toJSON",
  314. value: function toJSON$1(_, ctx) {
  315. var seq = [];
  316. if (ctx && ctx.onCreate) ctx.onCreate(seq);
  317. var i = 0;
  318. var _iterator = _createForOfIteratorHelper(this.items),
  319. _step;
  320. try {
  321. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  322. var item = _step.value;
  323. seq.push(toJSON(item, String(i++), ctx));
  324. }
  325. } catch (err) {
  326. _iterator.e(err);
  327. } finally {
  328. _iterator.f();
  329. }
  330. return seq;
  331. }
  332. }, {
  333. key: "toString",
  334. value: function toString(ctx, onComment, onChompKeep) {
  335. if (!ctx) return JSON.stringify(this);
  336. return _get(_getPrototypeOf(YAMLSeq.prototype), "toString", this).call(this, ctx, {
  337. blockItem: function blockItem(n) {
  338. return n.type === 'comment' ? n.str : "- ".concat(n.str);
  339. },
  340. flowChars: {
  341. start: '[',
  342. end: ']'
  343. },
  344. isMap: false,
  345. itemIndent: (ctx.indent || '') + ' '
  346. }, onComment, onChompKeep);
  347. }
  348. }]);
  349. return YAMLSeq;
  350. }(Collection);
  351. var stringifyKey = function stringifyKey(key, jsKey, ctx) {
  352. if (jsKey === null) return '';
  353. if (_typeof(jsKey) !== 'object') return String(jsKey);
  354. if (key instanceof Node && ctx && ctx.doc) return key.toString({
  355. anchors: Object.create(null),
  356. doc: ctx.doc,
  357. indent: '',
  358. indentStep: ctx.indentStep,
  359. inFlow: true,
  360. inStringifyKey: true,
  361. stringify: ctx.stringify
  362. });
  363. return JSON.stringify(jsKey);
  364. };
  365. var Pair = /*#__PURE__*/function (_Node) {
  366. _inherits(Pair, _Node);
  367. var _super = _createSuper(Pair);
  368. function Pair(key) {
  369. var _this;
  370. var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  371. _classCallCheck(this, Pair);
  372. _this = _super.call(this);
  373. _this.key = key;
  374. _this.value = value;
  375. _this.type = Pair.Type.PAIR;
  376. return _this;
  377. }
  378. _createClass(Pair, [{
  379. key: "commentBefore",
  380. get: function get() {
  381. return this.key instanceof Node ? this.key.commentBefore : undefined;
  382. },
  383. set: function set(cb) {
  384. if (this.key == null) this.key = new Scalar(null);
  385. if (this.key instanceof Node) this.key.commentBefore = cb;else {
  386. var msg = 'Pair.commentBefore is an alias for Pair.key.commentBefore. To set it, the key must be a Node.';
  387. throw new Error(msg);
  388. }
  389. }
  390. }, {
  391. key: "addToJSMap",
  392. value: function addToJSMap(ctx, map) {
  393. var key = toJSON(this.key, '', ctx);
  394. if (map instanceof Map) {
  395. var value = toJSON(this.value, key, ctx);
  396. map.set(key, value);
  397. } else if (map instanceof Set) {
  398. map.add(key);
  399. } else {
  400. var stringKey = stringifyKey(this.key, key, ctx);
  401. var _value = toJSON(this.value, stringKey, ctx);
  402. if (stringKey in map) Object.defineProperty(map, stringKey, {
  403. value: _value,
  404. writable: true,
  405. enumerable: true,
  406. configurable: true
  407. });else map[stringKey] = _value;
  408. }
  409. return map;
  410. }
  411. }, {
  412. key: "toJSON",
  413. value: function toJSON(_, ctx) {
  414. var pair = ctx && ctx.mapAsMap ? new Map() : {};
  415. return this.addToJSMap(ctx, pair);
  416. }
  417. }, {
  418. key: "toString",
  419. value: function toString(ctx, onComment, onChompKeep) {
  420. if (!ctx || !ctx.doc) return JSON.stringify(this);
  421. var _ctx$doc$options = ctx.doc.options,
  422. indentSize = _ctx$doc$options.indent,
  423. indentSeq = _ctx$doc$options.indentSeq,
  424. simpleKeys = _ctx$doc$options.simpleKeys;
  425. var key = this.key,
  426. value = this.value;
  427. var keyComment = key instanceof Node && key.comment;
  428. if (simpleKeys) {
  429. if (keyComment) {
  430. throw new Error('With simple keys, key nodes cannot have comments');
  431. }
  432. if (key instanceof Collection) {
  433. var msg = 'With simple keys, collection cannot be used as a key value';
  434. throw new Error(msg);
  435. }
  436. }
  437. var explicitKey = !simpleKeys && (!key || keyComment || (key instanceof Node ? key instanceof Collection || key.type === Type.BLOCK_FOLDED || key.type === Type.BLOCK_LITERAL : _typeof(key) === 'object'));
  438. var _ctx = ctx,
  439. doc = _ctx.doc,
  440. indent = _ctx.indent,
  441. indentStep = _ctx.indentStep,
  442. stringify = _ctx.stringify;
  443. ctx = Object.assign({}, ctx, {
  444. implicitKey: !explicitKey,
  445. indent: indent + indentStep
  446. });
  447. var chompKeep = false;
  448. var str = stringify(key, ctx, function () {
  449. return keyComment = null;
  450. }, function () {
  451. return chompKeep = true;
  452. });
  453. str = addComment(str, ctx.indent, keyComment);
  454. if (!explicitKey && str.length > 1024) {
  455. if (simpleKeys) throw new Error('With simple keys, single line scalar must not span more than 1024 characters');
  456. explicitKey = true;
  457. }
  458. if (ctx.allNullValues && !simpleKeys) {
  459. if (this.comment) {
  460. str = addComment(str, ctx.indent, this.comment);
  461. if (onComment) onComment();
  462. } else if (chompKeep && !keyComment && onChompKeep) onChompKeep();
  463. return ctx.inFlow && !explicitKey ? str : "? ".concat(str);
  464. }
  465. str = explicitKey ? "? ".concat(str, "\n").concat(indent, ":") : "".concat(str, ":");
  466. if (this.comment) {
  467. // expected (but not strictly required) to be a single-line comment
  468. str = addComment(str, ctx.indent, this.comment);
  469. if (onComment) onComment();
  470. }
  471. var vcb = '';
  472. var valueComment = null;
  473. if (value instanceof Node) {
  474. if (value.spaceBefore) vcb = '\n';
  475. if (value.commentBefore) {
  476. var cs = value.commentBefore.replace(/^/gm, "".concat(ctx.indent, "#"));
  477. vcb += "\n".concat(cs);
  478. }
  479. valueComment = value.comment;
  480. } else if (value && _typeof(value) === 'object') {
  481. value = doc.schema.createNode(value, true);
  482. }
  483. ctx.implicitKey = false;
  484. if (!explicitKey && !this.comment && value instanceof Scalar) ctx.indentAtStart = str.length + 1;
  485. chompKeep = false;
  486. if (!indentSeq && indentSize >= 2 && !ctx.inFlow && !explicitKey && value instanceof YAMLSeq && value.type !== Type.FLOW_SEQ && !value.tag && !doc.anchors.getName(value)) {
  487. // If indentSeq === false, consider '- ' as part of indentation where possible
  488. ctx.indent = ctx.indent.substr(2);
  489. }
  490. var valueStr = stringify(value, ctx, function () {
  491. return valueComment = null;
  492. }, function () {
  493. return chompKeep = true;
  494. });
  495. var ws = ' ';
  496. if (vcb || this.comment) {
  497. ws = "".concat(vcb, "\n").concat(ctx.indent);
  498. } else if (!explicitKey && value instanceof Collection) {
  499. var flow = valueStr[0] === '[' || valueStr[0] === '{';
  500. if (!flow || valueStr.includes('\n')) ws = "\n".concat(ctx.indent);
  501. } else if (valueStr[0] === '\n') ws = '';
  502. if (chompKeep && !valueComment && onChompKeep) onChompKeep();
  503. return addComment(str + ws + valueStr, ctx.indent, valueComment);
  504. }
  505. }]);
  506. return Pair;
  507. }(Node);
  508. _defineProperty(Pair, "Type", {
  509. PAIR: 'PAIR',
  510. MERGE_PAIR: 'MERGE_PAIR'
  511. });
  512. var getAliasCount = function getAliasCount(node, anchors) {
  513. if (node instanceof Alias) {
  514. var anchor = anchors.get(node.source);
  515. return anchor.count * anchor.aliasCount;
  516. } else if (node instanceof Collection) {
  517. var count = 0;
  518. var _iterator = _createForOfIteratorHelper(node.items),
  519. _step;
  520. try {
  521. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  522. var item = _step.value;
  523. var c = getAliasCount(item, anchors);
  524. if (c > count) count = c;
  525. }
  526. } catch (err) {
  527. _iterator.e(err);
  528. } finally {
  529. _iterator.f();
  530. }
  531. return count;
  532. } else if (node instanceof Pair) {
  533. var kc = getAliasCount(node.key, anchors);
  534. var vc = getAliasCount(node.value, anchors);
  535. return Math.max(kc, vc);
  536. }
  537. return 1;
  538. };
  539. var Alias = /*#__PURE__*/function (_Node) {
  540. _inherits(Alias, _Node);
  541. var _super = _createSuper(Alias);
  542. function Alias(source) {
  543. var _this;
  544. _classCallCheck(this, Alias);
  545. _this = _super.call(this);
  546. _this.source = source;
  547. _this.type = Type.ALIAS;
  548. return _this;
  549. }
  550. _createClass(Alias, [{
  551. key: "tag",
  552. set: function set(t) {
  553. throw new Error('Alias nodes cannot have tags');
  554. }
  555. }, {
  556. key: "toJSON",
  557. value: function toJSON$1(arg, ctx) {
  558. if (!ctx) return toJSON(this.source, arg, ctx);
  559. var anchors = ctx.anchors,
  560. maxAliasCount = ctx.maxAliasCount;
  561. var anchor = anchors.get(this.source);
  562. /* istanbul ignore if */
  563. if (!anchor || anchor.res === undefined) {
  564. var msg = 'This should not happen: Alias anchor was not resolved?';
  565. if (this.cstNode) throw new YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg);
  566. }
  567. if (maxAliasCount >= 0) {
  568. anchor.count += 1;
  569. if (anchor.aliasCount === 0) anchor.aliasCount = getAliasCount(this.source, anchors);
  570. if (anchor.count * anchor.aliasCount > maxAliasCount) {
  571. var _msg = 'Excessive alias count indicates a resource exhaustion attack';
  572. if (this.cstNode) throw new YAMLReferenceError(this.cstNode, _msg);else throw new ReferenceError(_msg);
  573. }
  574. }
  575. return anchor.res;
  576. } // Only called when stringifying an alias mapping key while constructing
  577. // Object output.
  578. }, {
  579. key: "toString",
  580. value: function toString(ctx) {
  581. return Alias.stringify(this, ctx);
  582. }
  583. }], [{
  584. key: "stringify",
  585. value: function stringify(_ref, _ref2) {
  586. var range = _ref.range,
  587. source = _ref.source;
  588. var anchors = _ref2.anchors,
  589. doc = _ref2.doc,
  590. implicitKey = _ref2.implicitKey,
  591. inStringifyKey = _ref2.inStringifyKey;
  592. var anchor = Object.keys(anchors).find(function (a) {
  593. return anchors[a] === source;
  594. });
  595. if (!anchor && inStringifyKey) anchor = doc.anchors.getName(source) || doc.anchors.newName();
  596. if (anchor) return "*".concat(anchor).concat(implicitKey ? ' ' : '');
  597. var msg = doc.anchors.getName(source) ? 'Alias node must be after source node' : 'Source node not found for alias node';
  598. throw new Error("".concat(msg, " [").concat(range, "]"));
  599. }
  600. }]);
  601. return Alias;
  602. }(Node);
  603. _defineProperty(Alias, "default", true);
  604. function findPair(items, key) {
  605. var k = key instanceof Scalar ? key.value : key;
  606. var _iterator = _createForOfIteratorHelper(items),
  607. _step;
  608. try {
  609. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  610. var it = _step.value;
  611. if (it instanceof Pair) {
  612. if (it.key === key || it.key === k) return it;
  613. if (it.key && it.key.value === k) return it;
  614. }
  615. }
  616. } catch (err) {
  617. _iterator.e(err);
  618. } finally {
  619. _iterator.f();
  620. }
  621. return undefined;
  622. }
  623. var YAMLMap = /*#__PURE__*/function (_Collection) {
  624. _inherits(YAMLMap, _Collection);
  625. var _super = _createSuper(YAMLMap);
  626. function YAMLMap() {
  627. _classCallCheck(this, YAMLMap);
  628. return _super.apply(this, arguments);
  629. }
  630. _createClass(YAMLMap, [{
  631. key: "add",
  632. value: function add(pair, overwrite) {
  633. if (!pair) pair = new Pair(pair);else if (!(pair instanceof Pair)) pair = new Pair(pair.key || pair, pair.value);
  634. var prev = findPair(this.items, pair.key);
  635. var sortEntries = this.schema && this.schema.sortMapEntries;
  636. if (prev) {
  637. if (overwrite) prev.value = pair.value;else throw new Error("Key ".concat(pair.key, " already set"));
  638. } else if (sortEntries) {
  639. var i = this.items.findIndex(function (item) {
  640. return sortEntries(pair, item) < 0;
  641. });
  642. if (i === -1) this.items.push(pair);else this.items.splice(i, 0, pair);
  643. } else {
  644. this.items.push(pair);
  645. }
  646. }
  647. }, {
  648. key: "delete",
  649. value: function _delete(key) {
  650. var it = findPair(this.items, key);
  651. if (!it) return false;
  652. var del = this.items.splice(this.items.indexOf(it), 1);
  653. return del.length > 0;
  654. }
  655. }, {
  656. key: "get",
  657. value: function get(key, keepScalar) {
  658. var it = findPair(this.items, key);
  659. var node = it && it.value;
  660. return !keepScalar && node instanceof Scalar ? node.value : node;
  661. }
  662. }, {
  663. key: "has",
  664. value: function has(key) {
  665. return !!findPair(this.items, key);
  666. }
  667. }, {
  668. key: "set",
  669. value: function set(key, value) {
  670. this.add(new Pair(key, value), true);
  671. }
  672. /**
  673. * @param {*} arg ignored
  674. * @param {*} ctx Conversion context, originally set in Document#toJSON()
  675. * @param {Class} Type If set, forces the returned collection type
  676. * @returns {*} Instance of Type, Map, or Object
  677. */
  678. }, {
  679. key: "toJSON",
  680. value: function toJSON(_, ctx, Type) {
  681. var map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {};
  682. if (ctx && ctx.onCreate) ctx.onCreate(map);
  683. var _iterator2 = _createForOfIteratorHelper(this.items),
  684. _step2;
  685. try {
  686. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  687. var item = _step2.value;
  688. item.addToJSMap(ctx, map);
  689. }
  690. } catch (err) {
  691. _iterator2.e(err);
  692. } finally {
  693. _iterator2.f();
  694. }
  695. return map;
  696. }
  697. }, {
  698. key: "toString",
  699. value: function toString(ctx, onComment, onChompKeep) {
  700. if (!ctx) return JSON.stringify(this);
  701. var _iterator3 = _createForOfIteratorHelper(this.items),
  702. _step3;
  703. try {
  704. for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
  705. var item = _step3.value;
  706. if (!(item instanceof Pair)) throw new Error("Map items must all be pairs; found ".concat(JSON.stringify(item), " instead"));
  707. }
  708. } catch (err) {
  709. _iterator3.e(err);
  710. } finally {
  711. _iterator3.f();
  712. }
  713. return _get(_getPrototypeOf(YAMLMap.prototype), "toString", this).call(this, ctx, {
  714. blockItem: function blockItem(n) {
  715. return n.str;
  716. },
  717. flowChars: {
  718. start: '{',
  719. end: '}'
  720. },
  721. isMap: true,
  722. itemIndent: ctx.indent || ''
  723. }, onComment, onChompKeep);
  724. }
  725. }]);
  726. return YAMLMap;
  727. }(Collection);
  728. var MERGE_KEY = '<<';
  729. var Merge = /*#__PURE__*/function (_Pair) {
  730. _inherits(Merge, _Pair);
  731. var _super = _createSuper(Merge);
  732. function Merge(pair) {
  733. var _this;
  734. _classCallCheck(this, Merge);
  735. if (pair instanceof Pair) {
  736. var seq = pair.value;
  737. if (!(seq instanceof YAMLSeq)) {
  738. seq = new YAMLSeq();
  739. seq.items.push(pair.value);
  740. seq.range = pair.value.range;
  741. }
  742. _this = _super.call(this, pair.key, seq);
  743. _this.range = pair.range;
  744. } else {
  745. _this = _super.call(this, new Scalar(MERGE_KEY), new YAMLSeq());
  746. }
  747. _this.type = Pair.Type.MERGE_PAIR;
  748. return _possibleConstructorReturn(_this);
  749. } // If the value associated with a merge key is a single mapping node, each of
  750. // its key/value pairs is inserted into the current mapping, unless the key
  751. // already exists in it. If the value associated with the merge key is a
  752. // sequence, then this sequence is expected to contain mapping nodes and each
  753. // of these nodes is merged in turn according to its order in the sequence.
  754. // Keys in mapping nodes earlier in the sequence override keys specified in
  755. // later mapping nodes. -- http://yaml.org/type/merge.html
  756. _createClass(Merge, [{
  757. key: "addToJSMap",
  758. value: function addToJSMap(ctx, map) {
  759. var _iterator = _createForOfIteratorHelper(this.value.items),
  760. _step;
  761. try {
  762. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  763. var source = _step.value.source;
  764. if (!(source instanceof YAMLMap)) throw new Error('Merge sources must be maps');
  765. var srcMap = source.toJSON(null, ctx, Map);
  766. var _iterator2 = _createForOfIteratorHelper(srcMap),
  767. _step2;
  768. try {
  769. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  770. var _step2$value = _slicedToArray(_step2.value, 2),
  771. key = _step2$value[0],
  772. value = _step2$value[1];
  773. if (map instanceof Map) {
  774. if (!map.has(key)) map.set(key, value);
  775. } else if (map instanceof Set) {
  776. map.add(key);
  777. } else if (!Object.prototype.hasOwnProperty.call(map, key)) {
  778. Object.defineProperty(map, key, {
  779. value: value,
  780. writable: true,
  781. enumerable: true,
  782. configurable: true
  783. });
  784. }
  785. }
  786. } catch (err) {
  787. _iterator2.e(err);
  788. } finally {
  789. _iterator2.f();
  790. }
  791. }
  792. } catch (err) {
  793. _iterator.e(err);
  794. } finally {
  795. _iterator.f();
  796. }
  797. return map;
  798. }
  799. }, {
  800. key: "toString",
  801. value: function toString(ctx, onComment) {
  802. var seq = this.value;
  803. if (seq.items.length > 1) return _get(_getPrototypeOf(Merge.prototype), "toString", this).call(this, ctx, onComment);
  804. this.value = seq.items[0];
  805. var str = _get(_getPrototypeOf(Merge.prototype), "toString", this).call(this, ctx, onComment);
  806. this.value = seq;
  807. return str;
  808. }
  809. }]);
  810. return Merge;
  811. }(Pair);
  812. var binaryOptions = {
  813. defaultType: Type.BLOCK_LITERAL,
  814. lineWidth: 76
  815. };
  816. var boolOptions = {
  817. trueStr: 'true',
  818. falseStr: 'false'
  819. };
  820. var intOptions = {
  821. asBigInt: false
  822. };
  823. var nullOptions = {
  824. nullStr: 'null'
  825. };
  826. var strOptions = {
  827. defaultType: Type.PLAIN,
  828. doubleQuoted: {
  829. jsonEncoding: false,
  830. minMultiLineLength: 40
  831. },
  832. fold: {
  833. lineWidth: 80,
  834. minContentWidth: 20
  835. }
  836. };
  837. function resolveScalar(str, tags, scalarFallback) {
  838. var _iterator = _createForOfIteratorHelper(tags),
  839. _step;
  840. try {
  841. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  842. var _step$value = _step.value,
  843. format = _step$value.format,
  844. test = _step$value.test,
  845. resolve = _step$value.resolve;
  846. if (test) {
  847. var match = str.match(test);
  848. if (match) {
  849. var res = resolve.apply(null, match);
  850. if (!(res instanceof Scalar)) res = new Scalar(res);
  851. if (format) res.format = format;
  852. return res;
  853. }
  854. }
  855. }
  856. } catch (err) {
  857. _iterator.e(err);
  858. } finally {
  859. _iterator.f();
  860. }
  861. if (scalarFallback) str = scalarFallback(str);
  862. return new Scalar(str);
  863. }
  864. var FOLD_FLOW = 'flow';
  865. var FOLD_BLOCK = 'block';
  866. var FOLD_QUOTED = 'quoted'; // presumes i+1 is at the start of a line
  867. // returns index of last newline in more-indented block
  868. var consumeMoreIndentedLines = function consumeMoreIndentedLines(text, i) {
  869. var ch = text[i + 1];
  870. while (ch === ' ' || ch === '\t') {
  871. do {
  872. ch = text[i += 1];
  873. } while (ch && ch !== '\n');
  874. ch = text[i + 1];
  875. }
  876. return i;
  877. };
  878. /**
  879. * Tries to keep input at up to `lineWidth` characters, splitting only on spaces
  880. * not followed by newlines or spaces unless `mode` is `'quoted'`. Lines are
  881. * terminated with `\n` and started with `indent`.
  882. *
  883. * @param {string} text
  884. * @param {string} indent
  885. * @param {string} [mode='flow'] `'block'` prevents more-indented lines
  886. * from being folded; `'quoted'` allows for `\` escapes, including escaped
  887. * newlines
  888. * @param {Object} options
  889. * @param {number} [options.indentAtStart] Accounts for leading contents on
  890. * the first line, defaulting to `indent.length`
  891. * @param {number} [options.lineWidth=80]
  892. * @param {number} [options.minContentWidth=20] Allow highly indented lines to
  893. * stretch the line width or indent content from the start
  894. * @param {function} options.onFold Called once if the text is folded
  895. * @param {function} options.onFold Called once if any line of text exceeds
  896. * lineWidth characters
  897. */
  898. function foldFlowLines(text, indent, mode, _ref) {
  899. var indentAtStart = _ref.indentAtStart,
  900. _ref$lineWidth = _ref.lineWidth,
  901. lineWidth = _ref$lineWidth === void 0 ? 80 : _ref$lineWidth,
  902. _ref$minContentWidth = _ref.minContentWidth,
  903. minContentWidth = _ref$minContentWidth === void 0 ? 20 : _ref$minContentWidth,
  904. onFold = _ref.onFold,
  905. onOverflow = _ref.onOverflow;
  906. if (!lineWidth || lineWidth < 0) return text;
  907. var endStep = Math.max(1 + minContentWidth, 1 + lineWidth - indent.length);
  908. if (text.length <= endStep) return text;
  909. var folds = [];
  910. var escapedFolds = {};
  911. var end = lineWidth - indent.length;
  912. if (typeof indentAtStart === 'number') {
  913. if (indentAtStart > lineWidth - Math.max(2, minContentWidth)) folds.push(0);else end = lineWidth - indentAtStart;
  914. }
  915. var split = undefined;
  916. var prev = undefined;
  917. var overflow = false;
  918. var i = -1;
  919. var escStart = -1;
  920. var escEnd = -1;
  921. if (mode === FOLD_BLOCK) {
  922. i = consumeMoreIndentedLines(text, i);
  923. if (i !== -1) end = i + endStep;
  924. }
  925. for (var ch; ch = text[i += 1];) {
  926. if (mode === FOLD_QUOTED && ch === '\\') {
  927. escStart = i;
  928. switch (text[i + 1]) {
  929. case 'x':
  930. i += 3;
  931. break;
  932. case 'u':
  933. i += 5;
  934. break;
  935. case 'U':
  936. i += 9;
  937. break;
  938. default:
  939. i += 1;
  940. }
  941. escEnd = i;
  942. }
  943. if (ch === '\n') {
  944. if (mode === FOLD_BLOCK) i = consumeMoreIndentedLines(text, i);
  945. end = i + endStep;
  946. split = undefined;
  947. } else {
  948. if (ch === ' ' && prev && prev !== ' ' && prev !== '\n' && prev !== '\t') {
  949. // space surrounded by non-space can be replaced with newline + indent
  950. var next = text[i + 1];
  951. if (next && next !== ' ' && next !== '\n' && next !== '\t') split = i;
  952. }
  953. if (i >= end) {
  954. if (split) {
  955. folds.push(split);
  956. end = split + endStep;
  957. split = undefined;
  958. } else if (mode === FOLD_QUOTED) {
  959. // white-space collected at end may stretch past lineWidth
  960. while (prev === ' ' || prev === '\t') {
  961. prev = ch;
  962. ch = text[i += 1];
  963. overflow = true;
  964. } // Account for newline escape, but don't break preceding escape
  965. var j = i > escEnd + 1 ? i - 2 : escStart - 1; // Bail out if lineWidth & minContentWidth are shorter than an escape string
  966. if (escapedFolds[j]) return text;
  967. folds.push(j);
  968. escapedFolds[j] = true;
  969. end = j + endStep;
  970. split = undefined;
  971. } else {
  972. overflow = true;
  973. }
  974. }
  975. }
  976. prev = ch;
  977. }
  978. if (overflow && onOverflow) onOverflow();
  979. if (folds.length === 0) return text;
  980. if (onFold) onFold();
  981. var res = text.slice(0, folds[0]);
  982. for (var _i = 0; _i < folds.length; ++_i) {
  983. var fold = folds[_i];
  984. var _end = folds[_i + 1] || text.length;
  985. if (fold === 0) res = "\n".concat(indent).concat(text.slice(0, _end));else {
  986. if (mode === FOLD_QUOTED && escapedFolds[fold]) res += "".concat(text[fold], "\\");
  987. res += "\n".concat(indent).concat(text.slice(fold + 1, _end));
  988. }
  989. }
  990. return res;
  991. }
  992. var getFoldOptions = function getFoldOptions(_ref) {
  993. var indentAtStart = _ref.indentAtStart;
  994. return indentAtStart ? Object.assign({
  995. indentAtStart: indentAtStart
  996. }, strOptions.fold) : strOptions.fold;
  997. }; // Also checks for lines starting with %, as parsing the output as YAML 1.1 will
  998. // presume that's starting a new document.
  999. var containsDocumentMarker = function containsDocumentMarker(str) {
  1000. return /^(%|---|\.\.\.)/m.test(str);
  1001. };
  1002. function lineLengthOverLimit(str, lineWidth, indentLength) {
  1003. if (!lineWidth || lineWidth < 0) return false;
  1004. var limit = lineWidth - indentLength;
  1005. var strLen = str.length;
  1006. if (strLen <= limit) return false;
  1007. for (var i = 0, start = 0; i < strLen; ++i) {
  1008. if (str[i] === '\n') {
  1009. if (i - start > limit) return true;
  1010. start = i + 1;
  1011. if (strLen - start <= limit) return false;
  1012. }
  1013. }
  1014. return true;
  1015. }
  1016. function doubleQuotedString(value, ctx) {
  1017. var implicitKey = ctx.implicitKey;
  1018. var _strOptions$doubleQuo = strOptions.doubleQuoted,
  1019. jsonEncoding = _strOptions$doubleQuo.jsonEncoding,
  1020. minMultiLineLength = _strOptions$doubleQuo.minMultiLineLength;
  1021. var json = JSON.stringify(value);
  1022. if (jsonEncoding) return json;
  1023. var indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
  1024. var str = '';
  1025. var start = 0;
  1026. for (var i = 0, ch = json[i]; ch; ch = json[++i]) {
  1027. if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') {
  1028. // space before newline needs to be escaped to not be folded
  1029. str += json.slice(start, i) + '\\ ';
  1030. i += 1;
  1031. start = i;
  1032. ch = '\\';
  1033. }
  1034. if (ch === '\\') switch (json[i + 1]) {
  1035. case 'u':
  1036. {
  1037. str += json.slice(start, i);
  1038. var code = json.substr(i + 2, 4);
  1039. switch (code) {
  1040. case '0000':
  1041. str += '\\0';
  1042. break;
  1043. case '0007':
  1044. str += '\\a';
  1045. break;
  1046. case '000b':
  1047. str += '\\v';
  1048. break;
  1049. case '001b':
  1050. str += '\\e';
  1051. break;
  1052. case '0085':
  1053. str += '\\N';
  1054. break;
  1055. case '00a0':
  1056. str += '\\_';
  1057. break;
  1058. case '2028':
  1059. str += '\\L';
  1060. break;
  1061. case '2029':
  1062. str += '\\P';
  1063. break;
  1064. default:
  1065. if (code.substr(0, 2) === '00') str += '\\x' + code.substr(2);else str += json.substr(i, 6);
  1066. }
  1067. i += 5;
  1068. start = i + 1;
  1069. }
  1070. break;
  1071. case 'n':
  1072. if (implicitKey || json[i + 2] === '"' || json.length < minMultiLineLength) {
  1073. i += 1;
  1074. } else {
  1075. // folding will eat first newline
  1076. str += json.slice(start, i) + '\n\n';
  1077. while (json[i + 2] === '\\' && json[i + 3] === 'n' && json[i + 4] !== '"') {
  1078. str += '\n';
  1079. i += 2;
  1080. }
  1081. str += indent; // space after newline needs to be escaped to not be folded
  1082. if (json[i + 2] === ' ') str += '\\';
  1083. i += 1;
  1084. start = i + 1;
  1085. }
  1086. break;
  1087. default:
  1088. i += 1;
  1089. }
  1090. }
  1091. str = start ? str + json.slice(start) : json;
  1092. return implicitKey ? str : foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx));
  1093. }
  1094. function singleQuotedString(value, ctx) {
  1095. if (ctx.implicitKey) {
  1096. if (/\n/.test(value)) return doubleQuotedString(value, ctx);
  1097. } else {
  1098. // single quoted string can't have leading or trailing whitespace around newline
  1099. if (/[ \t]\n|\n[ \t]/.test(value)) return doubleQuotedString(value, ctx);
  1100. }
  1101. var indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
  1102. var res = "'" + value.replace(/'/g, "''").replace(/\n+/g, "$&\n".concat(indent)) + "'";
  1103. return ctx.implicitKey ? res : foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx));
  1104. }
  1105. function blockString(_ref2, ctx, onComment, onChompKeep) {
  1106. var comment = _ref2.comment,
  1107. type = _ref2.type,
  1108. value = _ref2.value;
  1109. // 1. Block can't end in whitespace unless the last line is non-empty.
  1110. // 2. Strings consisting of only whitespace are best rendered explicitly.
  1111. if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
  1112. return doubleQuotedString(value, ctx);
  1113. }
  1114. var indent = ctx.indent || (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
  1115. var indentSize = indent ? '2' : '1'; // root is at -1
  1116. var literal = type === Type.BLOCK_FOLDED ? false : type === Type.BLOCK_LITERAL ? true : !lineLengthOverLimit(value, strOptions.fold.lineWidth, indent.length);
  1117. var header = literal ? '|' : '>';
  1118. if (!value) return header + '\n';
  1119. var wsStart = '';
  1120. var wsEnd = '';
  1121. value = value.replace(/[\n\t ]*$/, function (ws) {
  1122. var n = ws.indexOf('\n');
  1123. if (n === -1) {
  1124. header += '-'; // strip
  1125. } else if (value === ws || n !== ws.length - 1) {
  1126. header += '+'; // keep
  1127. if (onChompKeep) onChompKeep();
  1128. }
  1129. wsEnd = ws.replace(/\n$/, '');
  1130. return '';
  1131. }).replace(/^[\n ]*/, function (ws) {
  1132. if (ws.indexOf(' ') !== -1) header += indentSize;
  1133. var m = ws.match(/ +$/);
  1134. if (m) {
  1135. wsStart = ws.slice(0, -m[0].length);
  1136. return m[0];
  1137. } else {
  1138. wsStart = ws;
  1139. return '';
  1140. }
  1141. });
  1142. if (wsEnd) wsEnd = wsEnd.replace(/\n+(?!\n|$)/g, "$&".concat(indent));
  1143. if (wsStart) wsStart = wsStart.replace(/\n+/g, "$&".concat(indent));
  1144. if (comment) {
  1145. header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ');
  1146. if (onComment) onComment();
  1147. }
  1148. if (!value) return "".concat(header).concat(indentSize, "\n").concat(indent).concat(wsEnd);
  1149. if (literal) {
  1150. value = value.replace(/\n+/g, "$&".concat(indent));
  1151. return "".concat(header, "\n").concat(indent).concat(wsStart).concat(value).concat(wsEnd);
  1152. }
  1153. value = value.replace(/\n+/g, '\n$&').replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
  1154. // ^ ind.line ^ empty ^ capture next empty lines only at end of indent
  1155. .replace(/\n+/g, "$&".concat(indent));
  1156. var body = foldFlowLines("".concat(wsStart).concat(value).concat(wsEnd), indent, FOLD_BLOCK, strOptions.fold);
  1157. return "".concat(header, "\n").concat(indent).concat(body);
  1158. }
  1159. function plainString(item, ctx, onComment, onChompKeep) {
  1160. var comment = item.comment,
  1161. type = item.type,
  1162. value = item.value;
  1163. var actualString = ctx.actualString,
  1164. implicitKey = ctx.implicitKey,
  1165. indent = ctx.indent,
  1166. inFlow = ctx.inFlow;
  1167. if (implicitKey && /[\n[\]{},]/.test(value) || inFlow && /[[\]{},]/.test(value)) {
  1168. return doubleQuotedString(value, ctx);
  1169. }
  1170. if (!value || /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
  1171. // not allowed:
  1172. // - empty string, '-' or '?'
  1173. // - start with an indicator character (except [?:-]) or /[?-] /
  1174. // - '\n ', ': ' or ' \n' anywhere
  1175. // - '#' not preceded by a non-space char
  1176. // - end with ' ' or ':'
  1177. return implicitKey || inFlow || value.indexOf('\n') === -1 ? value.indexOf('"') !== -1 && value.indexOf("'") === -1 ? singleQuotedString(value, ctx) : doubleQuotedString(value, ctx) : blockString(item, ctx, onComment, onChompKeep);
  1178. }
  1179. if (!implicitKey && !inFlow && type !== Type.PLAIN && value.indexOf('\n') !== -1) {
  1180. // Where allowed & type not set explicitly, prefer block style for multiline strings
  1181. return blockString(item, ctx, onComment, onChompKeep);
  1182. }
  1183. if (indent === '' && containsDocumentMarker(value)) {
  1184. ctx.forceBlockIndent = true;
  1185. return blockString(item, ctx, onComment, onChompKeep);
  1186. }
  1187. var str = value.replace(/\n+/g, "$&\n".concat(indent)); // Verify that output will be parsed as a string, as e.g. plain numbers and
  1188. // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
  1189. // and others in v1.1.
  1190. if (actualString) {
  1191. var tags = ctx.doc.schema.tags;
  1192. var resolved = resolveScalar(str, tags, tags.scalarFallback).value;
  1193. if (typeof resolved !== 'string') return doubleQuotedString(value, ctx);
  1194. }
  1195. var body = implicitKey ? str : foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx));
  1196. if (comment && !inFlow && (body.indexOf('\n') !== -1 || comment.indexOf('\n') !== -1)) {
  1197. if (onComment) onComment();
  1198. return addCommentBefore(body, indent, comment);
  1199. }
  1200. return body;
  1201. }
  1202. function stringifyString(item, ctx, onComment, onChompKeep) {
  1203. var defaultType = strOptions.defaultType;
  1204. var implicitKey = ctx.implicitKey,
  1205. inFlow = ctx.inFlow;
  1206. var _item = item,
  1207. type = _item.type,
  1208. value = _item.value;
  1209. if (typeof value !== 'string') {
  1210. value = String(value);
  1211. item = Object.assign({}, item, {
  1212. value: value
  1213. });
  1214. }
  1215. var _stringify = function _stringify(_type) {
  1216. switch (_type) {
  1217. case Type.BLOCK_FOLDED:
  1218. case Type.BLOCK_LITERAL:
  1219. return blockString(item, ctx, onComment, onChompKeep);
  1220. case Type.QUOTE_DOUBLE:
  1221. return doubleQuotedString(value, ctx);
  1222. case Type.QUOTE_SINGLE:
  1223. return singleQuotedString(value, ctx);
  1224. case Type.PLAIN:
  1225. return plainString(item, ctx, onComment, onChompKeep);
  1226. default:
  1227. return null;
  1228. }
  1229. };
  1230. if (type !== Type.QUOTE_DOUBLE && /[\x00-\x08\x0b-\x1f\x7f-\x9f]/.test(value)) {
  1231. // force double quotes on control characters
  1232. type = Type.QUOTE_DOUBLE;
  1233. } else if ((implicitKey || inFlow) && (type === Type.BLOCK_FOLDED || type === Type.BLOCK_LITERAL)) {
  1234. // should not happen; blocks are not valid inside flow containers
  1235. type = Type.QUOTE_DOUBLE;
  1236. }
  1237. var res = _stringify(type);
  1238. if (res === null) {
  1239. res = _stringify(defaultType);
  1240. if (res === null) throw new Error("Unsupported default string type ".concat(defaultType));
  1241. }
  1242. return res;
  1243. }
  1244. function stringifyNumber(_ref) {
  1245. var format = _ref.format,
  1246. minFractionDigits = _ref.minFractionDigits,
  1247. tag = _ref.tag,
  1248. value = _ref.value;
  1249. if (typeof value === 'bigint') return String(value);
  1250. if (!isFinite(value)) return isNaN(value) ? '.nan' : value < 0 ? '-.inf' : '.inf';
  1251. var n = JSON.stringify(value);
  1252. if (!format && minFractionDigits && (!tag || tag === 'tag:yaml.org,2002:float') && /^\d/.test(n)) {
  1253. var i = n.indexOf('.');
  1254. if (i < 0) {
  1255. i = n.length;
  1256. n += '.';
  1257. }
  1258. var d = minFractionDigits - (n.length - i - 1);
  1259. while (d-- > 0) {
  1260. n += '0';
  1261. }
  1262. }
  1263. return n;
  1264. }
  1265. function checkFlowCollectionEnd(errors, cst) {
  1266. var char, name;
  1267. switch (cst.type) {
  1268. case Type.FLOW_MAP:
  1269. char = '}';
  1270. name = 'flow map';
  1271. break;
  1272. case Type.FLOW_SEQ:
  1273. char = ']';
  1274. name = 'flow sequence';
  1275. break;
  1276. default:
  1277. errors.push(new YAMLSemanticError(cst, 'Not a flow collection!?'));
  1278. return;
  1279. }
  1280. var lastItem;
  1281. for (var i = cst.items.length - 1; i >= 0; --i) {
  1282. var item = cst.items[i];
  1283. if (!item || item.type !== Type.COMMENT) {
  1284. lastItem = item;
  1285. break;
  1286. }
  1287. }
  1288. if (lastItem && lastItem.char !== char) {
  1289. var msg = "Expected ".concat(name, " to end with ").concat(char);
  1290. var err;
  1291. if (typeof lastItem.offset === 'number') {
  1292. err = new YAMLSemanticError(cst, msg);
  1293. err.offset = lastItem.offset + 1;
  1294. } else {
  1295. err = new YAMLSemanticError(lastItem, msg);
  1296. if (lastItem.range && lastItem.range.end) err.offset = lastItem.range.end - lastItem.range.start;
  1297. }
  1298. errors.push(err);
  1299. }
  1300. }
  1301. function checkFlowCommentSpace(errors, comment) {
  1302. var prev = comment.context.src[comment.range.start - 1];
  1303. if (prev !== '\n' && prev !== '\t' && prev !== ' ') {
  1304. var msg = 'Comments must be separated from other tokens by white space characters';
  1305. errors.push(new YAMLSemanticError(comment, msg));
  1306. }
  1307. }
  1308. function getLongKeyError(source, key) {
  1309. var sk = String(key);
  1310. var k = sk.substr(0, 8) + '...' + sk.substr(-8);
  1311. return new YAMLSemanticError(source, "The \"".concat(k, "\" key is too long"));
  1312. }
  1313. function resolveComments(collection, comments) {
  1314. var _iterator = _createForOfIteratorHelper(comments),
  1315. _step;
  1316. try {
  1317. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  1318. var _step$value = _step.value,
  1319. afterKey = _step$value.afterKey,
  1320. before = _step$value.before,
  1321. comment = _step$value.comment;
  1322. var item = collection.items[before];
  1323. if (!item) {
  1324. if (comment !== undefined) {
  1325. if (collection.comment) collection.comment += '\n' + comment;else collection.comment = comment;
  1326. }
  1327. } else {
  1328. if (afterKey && item.value) item = item.value;
  1329. if (comment === undefined) {
  1330. if (afterKey || !item.commentBefore) item.spaceBefore = true;
  1331. } else {
  1332. if (item.commentBefore) item.commentBefore += '\n' + comment;else item.commentBefore = comment;
  1333. }
  1334. }
  1335. }
  1336. } catch (err) {
  1337. _iterator.e(err);
  1338. } finally {
  1339. _iterator.f();
  1340. }
  1341. }
  1342. // on error, will return { str: string, errors: Error[] }
  1343. function resolveString(doc, node) {
  1344. var res = node.strValue;
  1345. if (!res) return '';
  1346. if (typeof res === 'string') return res;
  1347. res.errors.forEach(function (error) {
  1348. if (!error.source) error.source = node;
  1349. doc.errors.push(error);
  1350. });
  1351. return res.str;
  1352. }
  1353. function resolveTagHandle(doc, node) {
  1354. var _node$tag = node.tag,
  1355. handle = _node$tag.handle,
  1356. suffix = _node$tag.suffix;
  1357. var prefix = doc.tagPrefixes.find(function (p) {
  1358. return p.handle === handle;
  1359. });
  1360. if (!prefix) {
  1361. var dtp = doc.getDefaults().tagPrefixes;
  1362. if (dtp) prefix = dtp.find(function (p) {
  1363. return p.handle === handle;
  1364. });
  1365. if (!prefix) throw new YAMLSemanticError(node, "The ".concat(handle, " tag handle is non-default and was not declared."));
  1366. }
  1367. if (!suffix) throw new YAMLSemanticError(node, "The ".concat(handle, " tag has no suffix."));
  1368. if (handle === '!' && (doc.version || doc.options.version) === '1.0') {
  1369. if (suffix[0] === '^') {
  1370. doc.warnings.push(new YAMLWarning(node, 'YAML 1.0 ^ tag expansion is not supported'));
  1371. return suffix;
  1372. }
  1373. if (/[:/]/.test(suffix)) {
  1374. // word/foo -> tag:word.yaml.org,2002:foo
  1375. var vocab = suffix.match(/^([a-z0-9-]+)\/(.*)/i);
  1376. return vocab ? "tag:".concat(vocab[1], ".yaml.org,2002:").concat(vocab[2]) : "tag:".concat(suffix);
  1377. }
  1378. }
  1379. return prefix.prefix + decodeURIComponent(suffix);
  1380. }
  1381. function resolveTagName(doc, node) {
  1382. var tag = node.tag,
  1383. type = node.type;
  1384. var nonSpecific = false;
  1385. if (tag) {
  1386. var handle = tag.handle,
  1387. suffix = tag.suffix,
  1388. verbatim = tag.verbatim;
  1389. if (verbatim) {
  1390. if (verbatim !== '!' && verbatim !== '!!') return verbatim;
  1391. var msg = "Verbatim tags aren't resolved, so ".concat(verbatim, " is invalid.");
  1392. doc.errors.push(new YAMLSemanticError(node, msg));
  1393. } else if (handle === '!' && !suffix) {
  1394. nonSpecific = true;
  1395. } else {
  1396. try {
  1397. return resolveTagHandle(doc, node);
  1398. } catch (error) {
  1399. doc.errors.push(error);
  1400. }
  1401. }
  1402. }
  1403. switch (type) {
  1404. case Type.BLOCK_FOLDED:
  1405. case Type.BLOCK_LITERAL:
  1406. case Type.QUOTE_DOUBLE:
  1407. case Type.QUOTE_SINGLE:
  1408. return defaultTags.STR;
  1409. case Type.FLOW_MAP:
  1410. case Type.MAP:
  1411. return defaultTags.MAP;
  1412. case Type.FLOW_SEQ:
  1413. case Type.SEQ:
  1414. return defaultTags.SEQ;
  1415. case Type.PLAIN:
  1416. return nonSpecific ? defaultTags.STR : null;
  1417. default:
  1418. return null;
  1419. }
  1420. }
  1421. function resolveByTagName(doc, node, tagName) {
  1422. var tags = doc.schema.tags;
  1423. var matchWithTest = [];
  1424. var _iterator = _createForOfIteratorHelper(tags),
  1425. _step;
  1426. try {
  1427. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  1428. var tag = _step.value;
  1429. if (tag.tag === tagName) {
  1430. if (tag.test) matchWithTest.push(tag);else {
  1431. var res = tag.resolve(doc, node);
  1432. return res instanceof Collection ? res : new Scalar(res);
  1433. }
  1434. }
  1435. }
  1436. } catch (err) {
  1437. _iterator.e(err);
  1438. } finally {
  1439. _iterator.f();
  1440. }
  1441. var str = resolveString(doc, node);
  1442. if (typeof str === 'string' && matchWithTest.length > 0) return resolveScalar(str, matchWithTest, tags.scalarFallback);
  1443. return null;
  1444. }
  1445. function getFallbackTagName(_ref) {
  1446. var type = _ref.type;
  1447. switch (type) {
  1448. case Type.FLOW_MAP:
  1449. case Type.MAP:
  1450. return defaultTags.MAP;
  1451. case Type.FLOW_SEQ:
  1452. case Type.SEQ:
  1453. return defaultTags.SEQ;
  1454. default:
  1455. return defaultTags.STR;
  1456. }
  1457. }
  1458. function resolveTag(doc, node, tagName) {
  1459. try {
  1460. var res = resolveByTagName(doc, node, tagName);
  1461. if (res) {
  1462. if (tagName && node.tag) res.tag = tagName;
  1463. return res;
  1464. }
  1465. } catch (error) {
  1466. /* istanbul ignore if */
  1467. if (!error.source) error.source = node;
  1468. doc.errors.push(error);
  1469. return null;
  1470. }
  1471. try {
  1472. var fallback = getFallbackTagName(node);
  1473. if (!fallback) throw new Error("The tag ".concat(tagName, " is unavailable"));
  1474. var msg = "The tag ".concat(tagName, " is unavailable, falling back to ").concat(fallback);
  1475. doc.warnings.push(new YAMLWarning(node, msg));
  1476. var _res = resolveByTagName(doc, node, fallback);
  1477. _res.tag = tagName;
  1478. return _res;
  1479. } catch (error) {
  1480. var refError = new YAMLReferenceError(node, error.message);
  1481. refError.stack = error.stack;
  1482. doc.errors.push(refError);
  1483. return null;
  1484. }
  1485. }
  1486. var isCollectionItem = function isCollectionItem(node) {
  1487. if (!node) return false;
  1488. var type = node.type;
  1489. return type === Type.MAP_KEY || type === Type.MAP_VALUE || type === Type.SEQ_ITEM;
  1490. };
  1491. function resolveNodeProps(errors, node) {
  1492. var comments = {
  1493. before: [],
  1494. after: []
  1495. };
  1496. var hasAnchor = false;
  1497. var hasTag = false;
  1498. var props = isCollectionItem(node.context.parent) ? node.context.parent.props.concat(node.props) : node.props;
  1499. var _iterator = _createForOfIteratorHelper(props),
  1500. _step;
  1501. try {
  1502. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  1503. var _step$value = _step.value,
  1504. start = _step$value.start,
  1505. end = _step$value.end;
  1506. switch (node.context.src[start]) {
  1507. case Char.COMMENT:
  1508. {
  1509. if (!node.commentHasRequiredWhitespace(start)) {
  1510. var msg = 'Comments must be separated from other tokens by white space characters';
  1511. errors.push(new YAMLSemanticError(node, msg));
  1512. }
  1513. var header = node.header,
  1514. valueRange = node.valueRange;
  1515. var cc = valueRange && (start > valueRange.start || header && start > header.start) ? comments.after : comments.before;
  1516. cc.push(node.context.src.slice(start + 1, end));
  1517. break;
  1518. }
  1519. // Actual anchor & tag resolution is handled by schema, here we just complain
  1520. case Char.ANCHOR:
  1521. if (hasAnchor) {
  1522. var _msg = 'A node can have at most one anchor';
  1523. errors.push(new YAMLSemanticError(node, _msg));
  1524. }
  1525. hasAnchor = true;
  1526. break;
  1527. case Char.TAG:
  1528. if (hasTag) {
  1529. var _msg2 = 'A node can have at most one tag';
  1530. errors.push(new YAMLSemanticError(node, _msg2));
  1531. }
  1532. hasTag = true;
  1533. break;
  1534. }
  1535. }
  1536. } catch (err) {
  1537. _iterator.e(err);
  1538. } finally {
  1539. _iterator.f();
  1540. }
  1541. return {
  1542. comments: comments,
  1543. hasAnchor: hasAnchor,
  1544. hasTag: hasTag
  1545. };
  1546. }
  1547. function resolveNodeValue(doc, node) {
  1548. var anchors = doc.anchors,
  1549. errors = doc.errors,
  1550. schema = doc.schema;
  1551. if (node.type === Type.ALIAS) {
  1552. var name = node.rawValue;
  1553. var src = anchors.getNode(name);
  1554. if (!src) {
  1555. var msg = "Aliased anchor not found: ".concat(name);
  1556. errors.push(new YAMLReferenceError(node, msg));
  1557. return null;
  1558. } // Lazy resolution for circular references
  1559. var res = new Alias(src);
  1560. anchors._cstAliases.push(res);
  1561. return res;
  1562. }
  1563. var tagName = resolveTagName(doc, node);
  1564. if (tagName) return resolveTag(doc, node, tagName);
  1565. if (node.type !== Type.PLAIN) {
  1566. var _msg3 = "Failed to resolve ".concat(node.type, " node here");
  1567. errors.push(new YAMLSyntaxError(node, _msg3));
  1568. return null;
  1569. }
  1570. try {
  1571. var str = resolveString(doc, node);
  1572. return resolveScalar(str, schema.tags, schema.tags.scalarFallback);
  1573. } catch (error) {
  1574. if (!error.source) error.source = node;
  1575. errors.push(error);
  1576. return null;
  1577. }
  1578. } // sets node.resolved on success
  1579. function resolveNode(doc, node) {
  1580. if (!node) return null;
  1581. if (node.error) doc.errors.push(node.error);
  1582. var _resolveNodeProps = resolveNodeProps(doc.errors, node),
  1583. comments = _resolveNodeProps.comments,
  1584. hasAnchor = _resolveNodeProps.hasAnchor,
  1585. hasTag = _resolveNodeProps.hasTag;
  1586. if (hasAnchor) {
  1587. var anchors = doc.anchors;
  1588. var name = node.anchor;
  1589. var prev = anchors.getNode(name); // At this point, aliases for any preceding node with the same anchor
  1590. // name have already been resolved, so it may safely be renamed.
  1591. if (prev) anchors.map[anchors.newName(name)] = prev; // During parsing, we need to store the CST node in anchors.map as
  1592. // anchors need to be available during resolution to allow for
  1593. // circular references.
  1594. anchors.map[name] = node;
  1595. }
  1596. if (node.type === Type.ALIAS && (hasAnchor || hasTag)) {
  1597. var msg = 'An alias node must not specify any properties';
  1598. doc.errors.push(new YAMLSemanticError(node, msg));
  1599. }
  1600. var res = resolveNodeValue(doc, node);
  1601. if (res) {
  1602. res.range = [node.range.start, node.range.end];
  1603. if (doc.options.keepCstNodes) res.cstNode = node;
  1604. if (doc.options.keepNodeTypes) res.type = node.type;
  1605. var cb = comments.before.join('\n');
  1606. if (cb) {
  1607. res.commentBefore = res.commentBefore ? "".concat(res.commentBefore, "\n").concat(cb) : cb;
  1608. }
  1609. var ca = comments.after.join('\n');
  1610. if (ca) res.comment = res.comment ? "".concat(res.comment, "\n").concat(ca) : ca;
  1611. }
  1612. return node.resolved = res;
  1613. }
  1614. function resolveMap(doc, cst) {
  1615. if (cst.type !== Type.MAP && cst.type !== Type.FLOW_MAP) {
  1616. var msg = "A ".concat(cst.type, " node cannot be resolved as a mapping");
  1617. doc.errors.push(new YAMLSyntaxError(cst, msg));
  1618. return null;
  1619. }
  1620. var _ref = cst.type === Type.FLOW_MAP ? resolveFlowMapItems(doc, cst) : resolveBlockMapItems(doc, cst),
  1621. comments = _ref.comments,
  1622. items = _ref.items;
  1623. var map = new YAMLMap();
  1624. map.items = items;
  1625. resolveComments(map, comments);
  1626. var hasCollectionKey = false;
  1627. for (var i = 0; i < items.length; ++i) {
  1628. var iKey = items[i].key;
  1629. if (iKey instanceof Collection) hasCollectionKey = true;
  1630. if (doc.schema.merge && iKey && iKey.value === MERGE_KEY) {
  1631. items[i] = new Merge(items[i]);
  1632. var sources = items[i].value.items;
  1633. var error = null;
  1634. sources.some(function (node) {
  1635. if (node instanceof Alias) {
  1636. // During parsing, alias sources are CST nodes; to account for
  1637. // circular references their resolved values can't be used here.
  1638. var type = node.source.type;
  1639. if (type === Type.MAP || type === Type.FLOW_MAP) return false;
  1640. return error = 'Merge nodes aliases can only point to maps';
  1641. }
  1642. return error = 'Merge nodes can only have Alias nodes as values';
  1643. });
  1644. if (error) doc.errors.push(new YAMLSemanticError(cst, error));
  1645. } else {
  1646. for (var j = i + 1; j < items.length; ++j) {
  1647. var jKey = items[j].key;
  1648. if (iKey === jKey || iKey && jKey && Object.prototype.hasOwnProperty.call(iKey, 'value') && iKey.value === jKey.value) {
  1649. var _msg = "Map keys must be unique; \"".concat(iKey, "\" is repeated");
  1650. doc.errors.push(new YAMLSemanticError(cst, _msg));
  1651. break;
  1652. }
  1653. }
  1654. }
  1655. }
  1656. if (hasCollectionKey && !doc.options.mapAsMap) {
  1657. var warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
  1658. doc.warnings.push(new YAMLWarning(cst, warn));
  1659. }
  1660. cst.resolved = map;
  1661. return map;
  1662. }
  1663. var valueHasPairComment = function valueHasPairComment(_ref2) {
  1664. var _ref2$context = _ref2.context,
  1665. lineStart = _ref2$context.lineStart,
  1666. node = _ref2$context.node,
  1667. src = _ref2$context.src,
  1668. props = _ref2.props;
  1669. if (props.length === 0) return false;
  1670. var start = props[0].start;
  1671. if (node && start > node.valueRange.start) return false;
  1672. if (src[start] !== Char.COMMENT) return false;
  1673. for (var i = lineStart; i < start; ++i) {
  1674. if (src[i] === '\n') return false;
  1675. }
  1676. return true;
  1677. };
  1678. function resolvePairComment(item, pair) {
  1679. if (!valueHasPairComment(item)) return;
  1680. var comment = item.getPropValue(0, Char.COMMENT, true);
  1681. var found = false;
  1682. var cb = pair.value.commentBefore;
  1683. if (cb && cb.startsWith(comment)) {
  1684. pair.value.commentBefore = cb.substr(comment.length + 1);
  1685. found = true;
  1686. } else {
  1687. var cc = pair.value.comment;
  1688. if (!item.node && cc && cc.startsWith(comment)) {
  1689. pair.value.comment = cc.substr(comment.length + 1);
  1690. found = true;
  1691. }
  1692. }
  1693. if (found) pair.comment = comment;
  1694. }
  1695. function resolveBlockMapItems(doc, cst) {
  1696. var comments = [];
  1697. var items = [];
  1698. var key = undefined;
  1699. var keyStart = null;
  1700. for (var i = 0; i < cst.items.length; ++i) {
  1701. var item = cst.items[i];
  1702. switch (item.type) {
  1703. case Type.BLANK_LINE:
  1704. comments.push({
  1705. afterKey: !!key,
  1706. before: items.length
  1707. });
  1708. break;
  1709. case Type.COMMENT:
  1710. comments.push({
  1711. afterKey: !!key,
  1712. before: items.length,
  1713. comment: item.comment
  1714. });
  1715. break;
  1716. case Type.MAP_KEY:
  1717. if (key !== undefined) items.push(new Pair(key));
  1718. if (item.error) doc.errors.push(item.error);
  1719. key = resolveNode(doc, item.node);
  1720. keyStart = null;
  1721. break;
  1722. case Type.MAP_VALUE:
  1723. {
  1724. if (key === undefined) key = null;
  1725. if (item.error) doc.errors.push(item.error);
  1726. if (!item.context.atLineStart && item.node && item.node.type === Type.MAP && !item.node.context.atLineStart) {
  1727. var msg = 'Nested mappings are not allowed in compact mappings';
  1728. doc.errors.push(new YAMLSemanticError(item.node, msg));
  1729. }
  1730. var valueNode = item.node;
  1731. if (!valueNode && item.props.length > 0) {
  1732. // Comments on an empty mapping value need to be preserved, so we
  1733. // need to construct a minimal empty node here to use instead of the
  1734. // missing `item.node`. -- eemeli/yaml#19
  1735. valueNode = new PlainValue(Type.PLAIN, []);
  1736. valueNode.context = {
  1737. parent: item,
  1738. src: item.context.src
  1739. };
  1740. var pos = item.range.start + 1;
  1741. valueNode.range = {
  1742. start: pos,
  1743. end: pos
  1744. };
  1745. valueNode.valueRange = {
  1746. start: pos,
  1747. end: pos
  1748. };
  1749. if (typeof item.range.origStart === 'number') {
  1750. var origPos = item.range.origStart + 1;
  1751. valueNode.range.origStart = valueNode.range.origEnd = origPos;
  1752. valueNode.valueRange.origStart = valueNode.valueRange.origEnd = origPos;
  1753. }
  1754. }
  1755. var pair = new Pair(key, resolveNode(doc, valueNode));
  1756. resolvePairComment(item, pair);
  1757. items.push(pair);
  1758. if (key && typeof keyStart === 'number') {
  1759. if (item.range.start > keyStart + 1024) doc.errors.push(getLongKeyError(cst, key));
  1760. }
  1761. key = undefined;
  1762. keyStart = null;
  1763. }
  1764. break;
  1765. default:
  1766. if (key !== undefined) items.push(new Pair(key));
  1767. key = resolveNode(doc, item);
  1768. keyStart = item.range.start;
  1769. if (item.error) doc.errors.push(item.error);
  1770. next: for (var j = i + 1;; ++j) {
  1771. var nextItem = cst.items[j];
  1772. switch (nextItem && nextItem.type) {
  1773. case Type.BLANK_LINE:
  1774. case Type.COMMENT:
  1775. continue next;
  1776. case Type.MAP_VALUE:
  1777. break next;
  1778. default:
  1779. {
  1780. var _msg2 = 'Implicit map keys need to be followed by map values';
  1781. doc.errors.push(new YAMLSemanticError(item, _msg2));
  1782. break next;
  1783. }
  1784. }
  1785. }
  1786. if (item.valueRangeContainsNewline) {
  1787. var _msg3 = 'Implicit map keys need to be on a single line';
  1788. doc.errors.push(new YAMLSemanticError(item, _msg3));
  1789. }
  1790. }
  1791. }
  1792. if (key !== undefined) items.push(new Pair(key));
  1793. return {
  1794. comments: comments,
  1795. items: items
  1796. };
  1797. }
  1798. function resolveFlowMapItems(doc, cst) {
  1799. var comments = [];
  1800. var items = [];
  1801. var key = undefined;
  1802. var explicitKey = false;
  1803. var next = '{';
  1804. for (var i = 0; i < cst.items.length; ++i) {
  1805. var item = cst.items[i];
  1806. if (typeof item.char === 'string') {
  1807. var char = item.char,
  1808. offset = item.offset;
  1809. if (char === '?' && key === undefined && !explicitKey) {
  1810. explicitKey = true;
  1811. next = ':';
  1812. continue;
  1813. }
  1814. if (char === ':') {
  1815. if (key === undefined) key = null;
  1816. if (next === ':') {
  1817. next = ',';
  1818. continue;
  1819. }
  1820. } else {
  1821. if (explicitKey) {
  1822. if (key === undefined && char !== ',') key = null;
  1823. explicitKey = false;
  1824. }
  1825. if (key !== undefined) {
  1826. items.push(new Pair(key));
  1827. key = undefined;
  1828. if (char === ',') {
  1829. next = ':';
  1830. continue;
  1831. }
  1832. }
  1833. }
  1834. if (char === '}') {
  1835. if (i === cst.items.length - 1) continue;
  1836. } else if (char === next) {
  1837. next = ':';
  1838. continue;
  1839. }
  1840. var msg = "Flow map contains an unexpected ".concat(char);
  1841. var err = new YAMLSyntaxError(cst, msg);
  1842. err.offset = offset;
  1843. doc.errors.push(err);
  1844. } else if (item.type === Type.BLANK_LINE) {
  1845. comments.push({
  1846. afterKey: !!key,
  1847. before: items.length
  1848. });
  1849. } else if (item.type === Type.COMMENT) {
  1850. checkFlowCommentSpace(doc.errors, item);
  1851. comments.push({
  1852. afterKey: !!key,
  1853. before: items.length,
  1854. comment: item.comment
  1855. });
  1856. } else if (key === undefined) {
  1857. if (next === ',') doc.errors.push(new YAMLSemanticError(item, 'Separator , missing in flow map'));
  1858. key = resolveNode(doc, item);
  1859. } else {
  1860. if (next !== ',') doc.errors.push(new YAMLSemanticError(item, 'Indicator : missing in flow map entry'));
  1861. items.push(new Pair(key, resolveNode(doc, item)));
  1862. key = undefined;
  1863. explicitKey = false;
  1864. }
  1865. }
  1866. checkFlowCollectionEnd(doc.errors, cst);
  1867. if (key !== undefined) items.push(new Pair(key));
  1868. return {
  1869. comments: comments,
  1870. items: items
  1871. };
  1872. }
  1873. function resolveSeq(doc, cst) {
  1874. if (cst.type !== Type.SEQ && cst.type !== Type.FLOW_SEQ) {
  1875. var msg = "A ".concat(cst.type, " node cannot be resolved as a sequence");
  1876. doc.errors.push(new YAMLSyntaxError(cst, msg));
  1877. return null;
  1878. }
  1879. var _ref = cst.type === Type.FLOW_SEQ ? resolveFlowSeqItems(doc, cst) : resolveBlockSeqItems(doc, cst),
  1880. comments = _ref.comments,
  1881. items = _ref.items;
  1882. var seq = new YAMLSeq();
  1883. seq.items = items;
  1884. resolveComments(seq, comments);
  1885. if (!doc.options.mapAsMap && items.some(function (it) {
  1886. return it instanceof Pair && it.key instanceof Collection;
  1887. })) {
  1888. var warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
  1889. doc.warnings.push(new YAMLWarning(cst, warn));
  1890. }
  1891. cst.resolved = seq;
  1892. return seq;
  1893. }
  1894. function resolveBlockSeqItems(doc, cst) {
  1895. var comments = [];
  1896. var items = [];
  1897. for (var i = 0; i < cst.items.length; ++i) {
  1898. var item = cst.items[i];
  1899. switch (item.type) {
  1900. case Type.BLANK_LINE:
  1901. comments.push({
  1902. before: items.length
  1903. });
  1904. break;
  1905. case Type.COMMENT:
  1906. comments.push({
  1907. comment: item.comment,
  1908. before: items.length
  1909. });
  1910. break;
  1911. case Type.SEQ_ITEM:
  1912. if (item.error) doc.errors.push(item.error);
  1913. items.push(resolveNode(doc, item.node));
  1914. if (item.hasProps) {
  1915. var msg = 'Sequence items cannot have tags or anchors before the - indicator';
  1916. doc.errors.push(new YAMLSemanticError(item, msg));
  1917. }
  1918. break;
  1919. default:
  1920. if (item.error) doc.errors.push(item.error);
  1921. doc.errors.push(new YAMLSyntaxError(item, "Unexpected ".concat(item.type, " node in sequence")));
  1922. }
  1923. }
  1924. return {
  1925. comments: comments,
  1926. items: items
  1927. };
  1928. }
  1929. function resolveFlowSeqItems(doc, cst) {
  1930. var comments = [];
  1931. var items = [];
  1932. var explicitKey = false;
  1933. var key = undefined;
  1934. var keyStart = null;
  1935. var next = '[';
  1936. var prevItem = null;
  1937. for (var i = 0; i < cst.items.length; ++i) {
  1938. var item = cst.items[i];
  1939. if (typeof item.char === 'string') {
  1940. var char = item.char,
  1941. offset = item.offset;
  1942. if (char !== ':' && (explicitKey || key !== undefined)) {
  1943. if (explicitKey && key === undefined) key = next ? items.pop() : null;
  1944. items.push(new Pair(key));
  1945. explicitKey = false;
  1946. key = undefined;
  1947. keyStart = null;
  1948. }
  1949. if (char === next) {
  1950. next = null;
  1951. } else if (!next && char === '?') {
  1952. explicitKey = true;
  1953. } else if (next !== '[' && char === ':' && key === undefined) {
  1954. if (next === ',') {
  1955. key = items.pop();
  1956. if (key instanceof Pair) {
  1957. var msg = 'Chaining flow sequence pairs is invalid';
  1958. var err = new YAMLSemanticError(cst, msg);
  1959. err.offset = offset;
  1960. doc.errors.push(err);
  1961. }
  1962. if (!explicitKey && typeof keyStart === 'number') {
  1963. var keyEnd = item.range ? item.range.start : item.offset;
  1964. if (keyEnd > keyStart + 1024) doc.errors.push(getLongKeyError(cst, key));
  1965. var src = prevItem.context.src;
  1966. for (var _i = keyStart; _i < keyEnd; ++_i) {
  1967. if (src[_i] === '\n') {
  1968. var _msg = 'Implicit keys of flow sequence pairs need to be on a single line';
  1969. doc.errors.push(new YAMLSemanticError(prevItem, _msg));
  1970. break;
  1971. }
  1972. }
  1973. }
  1974. } else {
  1975. key = null;
  1976. }
  1977. keyStart = null;
  1978. explicitKey = false;
  1979. next = null;
  1980. } else if (next === '[' || char !== ']' || i < cst.items.length - 1) {
  1981. var _msg2 = "Flow sequence contains an unexpected ".concat(char);
  1982. var _err = new YAMLSyntaxError(cst, _msg2);
  1983. _err.offset = offset;
  1984. doc.errors.push(_err);
  1985. }
  1986. } else if (item.type === Type.BLANK_LINE) {
  1987. comments.push({
  1988. before: items.length
  1989. });
  1990. } else if (item.type === Type.COMMENT) {
  1991. checkFlowCommentSpace(doc.errors, item);
  1992. comments.push({
  1993. comment: item.comment,
  1994. before: items.length
  1995. });
  1996. } else {
  1997. if (next) {
  1998. var _msg3 = "Expected a ".concat(next, " in flow sequence");
  1999. doc.errors.push(new YAMLSemanticError(item, _msg3));
  2000. }
  2001. var value = resolveNode(doc, item);
  2002. if (key === undefined) {
  2003. items.push(value);
  2004. prevItem = item;
  2005. } else {
  2006. items.push(new Pair(key, value));
  2007. key = undefined;
  2008. }
  2009. keyStart = item.range.start;
  2010. next = ',';
  2011. }
  2012. }
  2013. checkFlowCollectionEnd(doc.errors, cst);
  2014. if (key !== undefined) items.push(new Pair(key));
  2015. return {
  2016. comments: comments,
  2017. items: items
  2018. };
  2019. }
  2020. export { Alias as A, Collection as C, Merge as M, Node as N, Pair as P, Scalar as S, YAMLSeq as Y, boolOptions as a, binaryOptions as b, stringifyString as c, YAMLMap as d, isEmptyPath as e, addComment as f, resolveMap as g, resolveSeq as h, intOptions as i, resolveString as j, stringifyNumber as k, findPair as l, nullOptions as n, resolveNode as r, strOptions as s, toJSON as t };