PlainValue-b8036b75.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. function _typeof(obj) {
  2. "@babel/helpers - typeof";
  3. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  4. _typeof = function (obj) {
  5. return typeof obj;
  6. };
  7. } else {
  8. _typeof = function (obj) {
  9. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  10. };
  11. }
  12. return _typeof(obj);
  13. }
  14. function _classCallCheck(instance, Constructor) {
  15. if (!(instance instanceof Constructor)) {
  16. throw new TypeError("Cannot call a class as a function");
  17. }
  18. }
  19. function _defineProperties(target, props) {
  20. for (var i = 0; i < props.length; i++) {
  21. var descriptor = props[i];
  22. descriptor.enumerable = descriptor.enumerable || false;
  23. descriptor.configurable = true;
  24. if ("value" in descriptor) descriptor.writable = true;
  25. Object.defineProperty(target, descriptor.key, descriptor);
  26. }
  27. }
  28. function _createClass(Constructor, protoProps, staticProps) {
  29. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  30. if (staticProps) _defineProperties(Constructor, staticProps);
  31. return Constructor;
  32. }
  33. function _defineProperty(obj, key, value) {
  34. if (key in obj) {
  35. Object.defineProperty(obj, key, {
  36. value: value,
  37. enumerable: true,
  38. configurable: true,
  39. writable: true
  40. });
  41. } else {
  42. obj[key] = value;
  43. }
  44. return obj;
  45. }
  46. function _inherits(subClass, superClass) {
  47. if (typeof superClass !== "function" && superClass !== null) {
  48. throw new TypeError("Super expression must either be null or a function");
  49. }
  50. subClass.prototype = Object.create(superClass && superClass.prototype, {
  51. constructor: {
  52. value: subClass,
  53. writable: true,
  54. configurable: true
  55. }
  56. });
  57. if (superClass) _setPrototypeOf(subClass, superClass);
  58. }
  59. function _getPrototypeOf(o) {
  60. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
  61. return o.__proto__ || Object.getPrototypeOf(o);
  62. };
  63. return _getPrototypeOf(o);
  64. }
  65. function _setPrototypeOf(o, p) {
  66. _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
  67. o.__proto__ = p;
  68. return o;
  69. };
  70. return _setPrototypeOf(o, p);
  71. }
  72. function _isNativeReflectConstruct() {
  73. if (typeof Reflect === "undefined" || !Reflect.construct) return false;
  74. if (Reflect.construct.sham) return false;
  75. if (typeof Proxy === "function") return true;
  76. try {
  77. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  78. return true;
  79. } catch (e) {
  80. return false;
  81. }
  82. }
  83. function _construct(Parent, args, Class) {
  84. if (_isNativeReflectConstruct()) {
  85. _construct = Reflect.construct;
  86. } else {
  87. _construct = function _construct(Parent, args, Class) {
  88. var a = [null];
  89. a.push.apply(a, args);
  90. var Constructor = Function.bind.apply(Parent, a);
  91. var instance = new Constructor();
  92. if (Class) _setPrototypeOf(instance, Class.prototype);
  93. return instance;
  94. };
  95. }
  96. return _construct.apply(null, arguments);
  97. }
  98. function _isNativeFunction(fn) {
  99. return Function.toString.call(fn).indexOf("[native code]") !== -1;
  100. }
  101. function _wrapNativeSuper(Class) {
  102. var _cache = typeof Map === "function" ? new Map() : undefined;
  103. _wrapNativeSuper = function _wrapNativeSuper(Class) {
  104. if (Class === null || !_isNativeFunction(Class)) return Class;
  105. if (typeof Class !== "function") {
  106. throw new TypeError("Super expression must either be null or a function");
  107. }
  108. if (typeof _cache !== "undefined") {
  109. if (_cache.has(Class)) return _cache.get(Class);
  110. _cache.set(Class, Wrapper);
  111. }
  112. function Wrapper() {
  113. return _construct(Class, arguments, _getPrototypeOf(this).constructor);
  114. }
  115. Wrapper.prototype = Object.create(Class.prototype, {
  116. constructor: {
  117. value: Wrapper,
  118. enumerable: false,
  119. writable: true,
  120. configurable: true
  121. }
  122. });
  123. return _setPrototypeOf(Wrapper, Class);
  124. };
  125. return _wrapNativeSuper(Class);
  126. }
  127. function _assertThisInitialized(self) {
  128. if (self === void 0) {
  129. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  130. }
  131. return self;
  132. }
  133. function _possibleConstructorReturn(self, call) {
  134. if (call && (typeof call === "object" || typeof call === "function")) {
  135. return call;
  136. }
  137. return _assertThisInitialized(self);
  138. }
  139. function _createSuper(Derived) {
  140. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  141. return function _createSuperInternal() {
  142. var Super = _getPrototypeOf(Derived),
  143. result;
  144. if (hasNativeReflectConstruct) {
  145. var NewTarget = _getPrototypeOf(this).constructor;
  146. result = Reflect.construct(Super, arguments, NewTarget);
  147. } else {
  148. result = Super.apply(this, arguments);
  149. }
  150. return _possibleConstructorReturn(this, result);
  151. };
  152. }
  153. function _superPropBase(object, property) {
  154. while (!Object.prototype.hasOwnProperty.call(object, property)) {
  155. object = _getPrototypeOf(object);
  156. if (object === null) break;
  157. }
  158. return object;
  159. }
  160. function _get(target, property, receiver) {
  161. if (typeof Reflect !== "undefined" && Reflect.get) {
  162. _get = Reflect.get;
  163. } else {
  164. _get = function _get(target, property, receiver) {
  165. var base = _superPropBase(target, property);
  166. if (!base) return;
  167. var desc = Object.getOwnPropertyDescriptor(base, property);
  168. if (desc.get) {
  169. return desc.get.call(receiver);
  170. }
  171. return desc.value;
  172. };
  173. }
  174. return _get(target, property, receiver || target);
  175. }
  176. function _slicedToArray(arr, i) {
  177. return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
  178. }
  179. function _toArray(arr) {
  180. return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest();
  181. }
  182. function _arrayWithHoles(arr) {
  183. if (Array.isArray(arr)) return arr;
  184. }
  185. function _iterableToArray(iter) {
  186. if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
  187. }
  188. function _iterableToArrayLimit(arr, i) {
  189. if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
  190. var _arr = [];
  191. var _n = true;
  192. var _d = false;
  193. var _e = undefined;
  194. try {
  195. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  196. _arr.push(_s.value);
  197. if (i && _arr.length === i) break;
  198. }
  199. } catch (err) {
  200. _d = true;
  201. _e = err;
  202. } finally {
  203. try {
  204. if (!_n && _i["return"] != null) _i["return"]();
  205. } finally {
  206. if (_d) throw _e;
  207. }
  208. }
  209. return _arr;
  210. }
  211. function _unsupportedIterableToArray(o, minLen) {
  212. if (!o) return;
  213. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  214. var n = Object.prototype.toString.call(o).slice(8, -1);
  215. if (n === "Object" && o.constructor) n = o.constructor.name;
  216. if (n === "Map" || n === "Set") return Array.from(o);
  217. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  218. }
  219. function _arrayLikeToArray(arr, len) {
  220. if (len == null || len > arr.length) len = arr.length;
  221. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  222. return arr2;
  223. }
  224. function _nonIterableRest() {
  225. throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  226. }
  227. function _createForOfIteratorHelper(o, allowArrayLike) {
  228. var it;
  229. if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
  230. if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
  231. if (it) o = it;
  232. var i = 0;
  233. var F = function () {};
  234. return {
  235. s: F,
  236. n: function () {
  237. if (i >= o.length) return {
  238. done: true
  239. };
  240. return {
  241. done: false,
  242. value: o[i++]
  243. };
  244. },
  245. e: function (e) {
  246. throw e;
  247. },
  248. f: F
  249. };
  250. }
  251. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  252. }
  253. var normalCompletion = true,
  254. didErr = false,
  255. err;
  256. return {
  257. s: function () {
  258. it = o[Symbol.iterator]();
  259. },
  260. n: function () {
  261. var step = it.next();
  262. normalCompletion = step.done;
  263. return step;
  264. },
  265. e: function (e) {
  266. didErr = true;
  267. err = e;
  268. },
  269. f: function () {
  270. try {
  271. if (!normalCompletion && it.return != null) it.return();
  272. } finally {
  273. if (didErr) throw err;
  274. }
  275. }
  276. };
  277. }
  278. var Char = {
  279. ANCHOR: '&',
  280. COMMENT: '#',
  281. TAG: '!',
  282. DIRECTIVES_END: '-',
  283. DOCUMENT_END: '.'
  284. };
  285. var Type = {
  286. ALIAS: 'ALIAS',
  287. BLANK_LINE: 'BLANK_LINE',
  288. BLOCK_FOLDED: 'BLOCK_FOLDED',
  289. BLOCK_LITERAL: 'BLOCK_LITERAL',
  290. COMMENT: 'COMMENT',
  291. DIRECTIVE: 'DIRECTIVE',
  292. DOCUMENT: 'DOCUMENT',
  293. FLOW_MAP: 'FLOW_MAP',
  294. FLOW_SEQ: 'FLOW_SEQ',
  295. MAP: 'MAP',
  296. MAP_KEY: 'MAP_KEY',
  297. MAP_VALUE: 'MAP_VALUE',
  298. PLAIN: 'PLAIN',
  299. QUOTE_DOUBLE: 'QUOTE_DOUBLE',
  300. QUOTE_SINGLE: 'QUOTE_SINGLE',
  301. SEQ: 'SEQ',
  302. SEQ_ITEM: 'SEQ_ITEM'
  303. };
  304. var defaultTagPrefix = 'tag:yaml.org,2002:';
  305. var defaultTags = {
  306. MAP: 'tag:yaml.org,2002:map',
  307. SEQ: 'tag:yaml.org,2002:seq',
  308. STR: 'tag:yaml.org,2002:str'
  309. };
  310. function findLineStarts(src) {
  311. var ls = [0];
  312. var offset = src.indexOf('\n');
  313. while (offset !== -1) {
  314. offset += 1;
  315. ls.push(offset);
  316. offset = src.indexOf('\n', offset);
  317. }
  318. return ls;
  319. }
  320. function getSrcInfo(cst) {
  321. var lineStarts, src;
  322. if (typeof cst === 'string') {
  323. lineStarts = findLineStarts(cst);
  324. src = cst;
  325. } else {
  326. if (Array.isArray(cst)) cst = cst[0];
  327. if (cst && cst.context) {
  328. if (!cst.lineStarts) cst.lineStarts = findLineStarts(cst.context.src);
  329. lineStarts = cst.lineStarts;
  330. src = cst.context.src;
  331. }
  332. }
  333. return {
  334. lineStarts: lineStarts,
  335. src: src
  336. };
  337. }
  338. /**
  339. * @typedef {Object} LinePos - One-indexed position in the source
  340. * @property {number} line
  341. * @property {number} col
  342. */
  343. /**
  344. * Determine the line/col position matching a character offset.
  345. *
  346. * Accepts a source string or a CST document as the second parameter. With
  347. * the latter, starting indices for lines are cached in the document as
  348. * `lineStarts: number[]`.
  349. *
  350. * Returns a one-indexed `{ line, col }` location if found, or
  351. * `undefined` otherwise.
  352. *
  353. * @param {number} offset
  354. * @param {string|Document|Document[]} cst
  355. * @returns {?LinePos}
  356. */
  357. function getLinePos(offset, cst) {
  358. if (typeof offset !== 'number' || offset < 0) return null;
  359. var _getSrcInfo = getSrcInfo(cst),
  360. lineStarts = _getSrcInfo.lineStarts,
  361. src = _getSrcInfo.src;
  362. if (!lineStarts || !src || offset > src.length) return null;
  363. for (var i = 0; i < lineStarts.length; ++i) {
  364. var start = lineStarts[i];
  365. if (offset < start) {
  366. return {
  367. line: i,
  368. col: offset - lineStarts[i - 1] + 1
  369. };
  370. }
  371. if (offset === start) return {
  372. line: i + 1,
  373. col: 1
  374. };
  375. }
  376. var line = lineStarts.length;
  377. return {
  378. line: line,
  379. col: offset - lineStarts[line - 1] + 1
  380. };
  381. }
  382. /**
  383. * Get a specified line from the source.
  384. *
  385. * Accepts a source string or a CST document as the second parameter. With
  386. * the latter, starting indices for lines are cached in the document as
  387. * `lineStarts: number[]`.
  388. *
  389. * Returns the line as a string if found, or `null` otherwise.
  390. *
  391. * @param {number} line One-indexed line number
  392. * @param {string|Document|Document[]} cst
  393. * @returns {?string}
  394. */
  395. function getLine(line, cst) {
  396. var _getSrcInfo2 = getSrcInfo(cst),
  397. lineStarts = _getSrcInfo2.lineStarts,
  398. src = _getSrcInfo2.src;
  399. if (!lineStarts || !(line >= 1) || line > lineStarts.length) return null;
  400. var start = lineStarts[line - 1];
  401. var end = lineStarts[line]; // undefined for last line; that's ok for slice()
  402. while (end && end > start && src[end - 1] === '\n') {
  403. --end;
  404. }
  405. return src.slice(start, end);
  406. }
  407. /**
  408. * Pretty-print the starting line from the source indicated by the range `pos`
  409. *
  410. * Trims output to `maxWidth` chars while keeping the starting column visible,
  411. * using `…` at either end to indicate dropped characters.
  412. *
  413. * Returns a two-line string (or `null`) with `\n` as separator; the second line
  414. * will hold appropriately indented `^` marks indicating the column range.
  415. *
  416. * @param {Object} pos
  417. * @param {LinePos} pos.start
  418. * @param {LinePos} [pos.end]
  419. * @param {string|Document|Document[]*} cst
  420. * @param {number} [maxWidth=80]
  421. * @returns {?string}
  422. */
  423. function getPrettyContext(_ref, cst) {
  424. var start = _ref.start,
  425. end = _ref.end;
  426. var maxWidth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 80;
  427. var src = getLine(start.line, cst);
  428. if (!src) return null;
  429. var col = start.col;
  430. if (src.length > maxWidth) {
  431. if (col <= maxWidth - 10) {
  432. src = src.substr(0, maxWidth - 1) + '…';
  433. } else {
  434. var halfWidth = Math.round(maxWidth / 2);
  435. if (src.length > col + halfWidth) src = src.substr(0, col + halfWidth - 1) + '…';
  436. col -= src.length - maxWidth;
  437. src = '…' + src.substr(1 - maxWidth);
  438. }
  439. }
  440. var errLen = 1;
  441. var errEnd = '';
  442. if (end) {
  443. if (end.line === start.line && col + (end.col - start.col) <= maxWidth + 1) {
  444. errLen = end.col - start.col;
  445. } else {
  446. errLen = Math.min(src.length + 1, maxWidth) - col;
  447. errEnd = '…';
  448. }
  449. }
  450. var offset = col > 1 ? ' '.repeat(col - 1) : '';
  451. var err = '^'.repeat(errLen);
  452. return "".concat(src, "\n").concat(offset).concat(err).concat(errEnd);
  453. }
  454. var Range = /*#__PURE__*/function () {
  455. function Range(start, end) {
  456. _classCallCheck(this, Range);
  457. this.start = start;
  458. this.end = end || start;
  459. }
  460. _createClass(Range, [{
  461. key: "isEmpty",
  462. value: function isEmpty() {
  463. return typeof this.start !== 'number' || !this.end || this.end <= this.start;
  464. }
  465. /**
  466. * Set `origStart` and `origEnd` to point to the original source range for
  467. * this node, which may differ due to dropped CR characters.
  468. *
  469. * @param {number[]} cr - Positions of dropped CR characters
  470. * @param {number} offset - Starting index of `cr` from the last call
  471. * @returns {number} - The next offset, matching the one found for `origStart`
  472. */
  473. }, {
  474. key: "setOrigRange",
  475. value: function setOrigRange(cr, offset) {
  476. var start = this.start,
  477. end = this.end;
  478. if (cr.length === 0 || end <= cr[0]) {
  479. this.origStart = start;
  480. this.origEnd = end;
  481. return offset;
  482. }
  483. var i = offset;
  484. while (i < cr.length) {
  485. if (cr[i] > start) break;else ++i;
  486. }
  487. this.origStart = start + i;
  488. var nextOffset = i;
  489. while (i < cr.length) {
  490. // if end was at \n, it should now be at \r
  491. if (cr[i] >= end) break;else ++i;
  492. }
  493. this.origEnd = end + i;
  494. return nextOffset;
  495. }
  496. }], [{
  497. key: "copy",
  498. value: function copy(orig) {
  499. return new Range(orig.start, orig.end);
  500. }
  501. }]);
  502. return Range;
  503. }();
  504. /** Root class of all nodes */
  505. var Node = /*#__PURE__*/function () {
  506. function Node(type, props, context) {
  507. _classCallCheck(this, Node);
  508. Object.defineProperty(this, 'context', {
  509. value: context || null,
  510. writable: true
  511. });
  512. this.error = null;
  513. this.range = null;
  514. this.valueRange = null;
  515. this.props = props || [];
  516. this.type = type;
  517. this.value = null;
  518. }
  519. _createClass(Node, [{
  520. key: "getPropValue",
  521. value: function getPropValue(idx, key, skipKey) {
  522. if (!this.context) return null;
  523. var src = this.context.src;
  524. var prop = this.props[idx];
  525. return prop && src[prop.start] === key ? src.slice(prop.start + (skipKey ? 1 : 0), prop.end) : null;
  526. }
  527. }, {
  528. key: "anchor",
  529. get: function get() {
  530. for (var i = 0; i < this.props.length; ++i) {
  531. var anchor = this.getPropValue(i, Char.ANCHOR, true);
  532. if (anchor != null) return anchor;
  533. }
  534. return null;
  535. }
  536. }, {
  537. key: "comment",
  538. get: function get() {
  539. var comments = [];
  540. for (var i = 0; i < this.props.length; ++i) {
  541. var comment = this.getPropValue(i, Char.COMMENT, true);
  542. if (comment != null) comments.push(comment);
  543. }
  544. return comments.length > 0 ? comments.join('\n') : null;
  545. }
  546. }, {
  547. key: "commentHasRequiredWhitespace",
  548. value: function commentHasRequiredWhitespace(start) {
  549. var src = this.context.src;
  550. if (this.header && start === this.header.end) return false;
  551. if (!this.valueRange) return false;
  552. var end = this.valueRange.end;
  553. return start !== end || Node.atBlank(src, end - 1);
  554. }
  555. }, {
  556. key: "hasComment",
  557. get: function get() {
  558. if (this.context) {
  559. var src = this.context.src;
  560. for (var i = 0; i < this.props.length; ++i) {
  561. if (src[this.props[i].start] === Char.COMMENT) return true;
  562. }
  563. }
  564. return false;
  565. }
  566. }, {
  567. key: "hasProps",
  568. get: function get() {
  569. if (this.context) {
  570. var src = this.context.src;
  571. for (var i = 0; i < this.props.length; ++i) {
  572. if (src[this.props[i].start] !== Char.COMMENT) return true;
  573. }
  574. }
  575. return false;
  576. }
  577. }, {
  578. key: "includesTrailingLines",
  579. get: function get() {
  580. return false;
  581. }
  582. }, {
  583. key: "jsonLike",
  584. get: function get() {
  585. var jsonLikeTypes = [Type.FLOW_MAP, Type.FLOW_SEQ, Type.QUOTE_DOUBLE, Type.QUOTE_SINGLE];
  586. return jsonLikeTypes.indexOf(this.type) !== -1;
  587. }
  588. }, {
  589. key: "rangeAsLinePos",
  590. get: function get() {
  591. if (!this.range || !this.context) return undefined;
  592. var start = getLinePos(this.range.start, this.context.root);
  593. if (!start) return undefined;
  594. var end = getLinePos(this.range.end, this.context.root);
  595. return {
  596. start: start,
  597. end: end
  598. };
  599. }
  600. }, {
  601. key: "rawValue",
  602. get: function get() {
  603. if (!this.valueRange || !this.context) return null;
  604. var _this$valueRange = this.valueRange,
  605. start = _this$valueRange.start,
  606. end = _this$valueRange.end;
  607. return this.context.src.slice(start, end);
  608. }
  609. }, {
  610. key: "tag",
  611. get: function get() {
  612. for (var i = 0; i < this.props.length; ++i) {
  613. var tag = this.getPropValue(i, Char.TAG, false);
  614. if (tag != null) {
  615. if (tag[1] === '<') {
  616. return {
  617. verbatim: tag.slice(2, -1)
  618. };
  619. } else {
  620. // eslint-disable-next-line no-unused-vars
  621. var _tag$match = tag.match(/^(.*!)([^!]*)$/),
  622. _tag$match2 = _slicedToArray(_tag$match, 3);
  623. _tag$match2[0];
  624. var handle = _tag$match2[1],
  625. suffix = _tag$match2[2];
  626. return {
  627. handle: handle,
  628. suffix: suffix
  629. };
  630. }
  631. }
  632. }
  633. return null;
  634. }
  635. }, {
  636. key: "valueRangeContainsNewline",
  637. get: function get() {
  638. if (!this.valueRange || !this.context) return false;
  639. var _this$valueRange2 = this.valueRange,
  640. start = _this$valueRange2.start,
  641. end = _this$valueRange2.end;
  642. var src = this.context.src;
  643. for (var i = start; i < end; ++i) {
  644. if (src[i] === '\n') return true;
  645. }
  646. return false;
  647. }
  648. }, {
  649. key: "parseComment",
  650. value: function parseComment(start) {
  651. var src = this.context.src;
  652. if (src[start] === Char.COMMENT) {
  653. var end = Node.endOfLine(src, start + 1);
  654. var commentRange = new Range(start, end);
  655. this.props.push(commentRange);
  656. return end;
  657. }
  658. return start;
  659. }
  660. /**
  661. * Populates the `origStart` and `origEnd` values of all ranges for this
  662. * node. Extended by child classes to handle descendant nodes.
  663. *
  664. * @param {number[]} cr - Positions of dropped CR characters
  665. * @param {number} offset - Starting index of `cr` from the last call
  666. * @returns {number} - The next offset, matching the one found for `origStart`
  667. */
  668. }, {
  669. key: "setOrigRanges",
  670. value: function setOrigRanges(cr, offset) {
  671. if (this.range) offset = this.range.setOrigRange(cr, offset);
  672. if (this.valueRange) this.valueRange.setOrigRange(cr, offset);
  673. this.props.forEach(function (prop) {
  674. return prop.setOrigRange(cr, offset);
  675. });
  676. return offset;
  677. }
  678. }, {
  679. key: "toString",
  680. value: function toString() {
  681. var src = this.context.src,
  682. range = this.range,
  683. value = this.value;
  684. if (value != null) return value;
  685. var str = src.slice(range.start, range.end);
  686. return Node.addStringTerminator(src, range.end, str);
  687. }
  688. }], [{
  689. key: "addStringTerminator",
  690. value: function addStringTerminator(src, offset, str) {
  691. if (str[str.length - 1] === '\n') return str;
  692. var next = Node.endOfWhiteSpace(src, offset);
  693. return next >= src.length || src[next] === '\n' ? str + '\n' : str;
  694. } // ^(---|...)
  695. }, {
  696. key: "atDocumentBoundary",
  697. value: function atDocumentBoundary(src, offset, sep) {
  698. var ch0 = src[offset];
  699. if (!ch0) return true;
  700. var prev = src[offset - 1];
  701. if (prev && prev !== '\n') return false;
  702. if (sep) {
  703. if (ch0 !== sep) return false;
  704. } else {
  705. if (ch0 !== Char.DIRECTIVES_END && ch0 !== Char.DOCUMENT_END) return false;
  706. }
  707. var ch1 = src[offset + 1];
  708. var ch2 = src[offset + 2];
  709. if (ch1 !== ch0 || ch2 !== ch0) return false;
  710. var ch3 = src[offset + 3];
  711. return !ch3 || ch3 === '\n' || ch3 === '\t' || ch3 === ' ';
  712. }
  713. }, {
  714. key: "endOfIdentifier",
  715. value: function endOfIdentifier(src, offset) {
  716. var ch = src[offset];
  717. var isVerbatim = ch === '<';
  718. var notOk = isVerbatim ? ['\n', '\t', ' ', '>'] : ['\n', '\t', ' ', '[', ']', '{', '}', ','];
  719. while (ch && notOk.indexOf(ch) === -1) {
  720. ch = src[offset += 1];
  721. }
  722. if (isVerbatim && ch === '>') offset += 1;
  723. return offset;
  724. }
  725. }, {
  726. key: "endOfIndent",
  727. value: function endOfIndent(src, offset) {
  728. var ch = src[offset];
  729. while (ch === ' ') {
  730. ch = src[offset += 1];
  731. }
  732. return offset;
  733. }
  734. }, {
  735. key: "endOfLine",
  736. value: function endOfLine(src, offset) {
  737. var ch = src[offset];
  738. while (ch && ch !== '\n') {
  739. ch = src[offset += 1];
  740. }
  741. return offset;
  742. }
  743. }, {
  744. key: "endOfWhiteSpace",
  745. value: function endOfWhiteSpace(src, offset) {
  746. var ch = src[offset];
  747. while (ch === '\t' || ch === ' ') {
  748. ch = src[offset += 1];
  749. }
  750. return offset;
  751. }
  752. }, {
  753. key: "startOfLine",
  754. value: function startOfLine(src, offset) {
  755. var ch = src[offset - 1];
  756. if (ch === '\n') return offset;
  757. while (ch && ch !== '\n') {
  758. ch = src[offset -= 1];
  759. }
  760. return offset + 1;
  761. }
  762. /**
  763. * End of indentation, or null if the line's indent level is not more
  764. * than `indent`
  765. *
  766. * @param {string} src
  767. * @param {number} indent
  768. * @param {number} lineStart
  769. * @returns {?number}
  770. */
  771. }, {
  772. key: "endOfBlockIndent",
  773. value: function endOfBlockIndent(src, indent, lineStart) {
  774. var inEnd = Node.endOfIndent(src, lineStart);
  775. if (inEnd > lineStart + indent) {
  776. return inEnd;
  777. } else {
  778. var wsEnd = Node.endOfWhiteSpace(src, inEnd);
  779. var ch = src[wsEnd];
  780. if (!ch || ch === '\n') return wsEnd;
  781. }
  782. return null;
  783. }
  784. }, {
  785. key: "atBlank",
  786. value: function atBlank(src, offset, endAsBlank) {
  787. var ch = src[offset];
  788. return ch === '\n' || ch === '\t' || ch === ' ' || endAsBlank && !ch;
  789. }
  790. }, {
  791. key: "nextNodeIsIndented",
  792. value: function nextNodeIsIndented(ch, indentDiff, indicatorAsIndent) {
  793. if (!ch || indentDiff < 0) return false;
  794. if (indentDiff > 0) return true;
  795. return indicatorAsIndent && ch === '-';
  796. } // should be at line or string end, or at next non-whitespace char
  797. }, {
  798. key: "normalizeOffset",
  799. value: function normalizeOffset(src, offset) {
  800. var ch = src[offset];
  801. return !ch ? offset : ch !== '\n' && src[offset - 1] === '\n' ? offset - 1 : Node.endOfWhiteSpace(src, offset);
  802. } // fold single newline into space, multiple newlines to N - 1 newlines
  803. // presumes src[offset] === '\n'
  804. }, {
  805. key: "foldNewline",
  806. value: function foldNewline(src, offset, indent) {
  807. var inCount = 0;
  808. var error = false;
  809. var fold = '';
  810. var ch = src[offset + 1];
  811. while (ch === ' ' || ch === '\t' || ch === '\n') {
  812. switch (ch) {
  813. case '\n':
  814. inCount = 0;
  815. offset += 1;
  816. fold += '\n';
  817. break;
  818. case '\t':
  819. if (inCount <= indent) error = true;
  820. offset = Node.endOfWhiteSpace(src, offset + 2) - 1;
  821. break;
  822. case ' ':
  823. inCount += 1;
  824. offset += 1;
  825. break;
  826. }
  827. ch = src[offset + 1];
  828. }
  829. if (!fold) fold = ' ';
  830. if (ch && inCount <= indent) error = true;
  831. return {
  832. fold: fold,
  833. offset: offset,
  834. error: error
  835. };
  836. }
  837. }]);
  838. return Node;
  839. }();
  840. var YAMLError = /*#__PURE__*/function (_Error) {
  841. _inherits(YAMLError, _Error);
  842. var _super = _createSuper(YAMLError);
  843. function YAMLError(name, source, message) {
  844. var _this;
  845. _classCallCheck(this, YAMLError);
  846. if (!message || !(source instanceof Node)) throw new Error("Invalid arguments for new ".concat(name));
  847. _this = _super.call(this);
  848. _this.name = name;
  849. _this.message = message;
  850. _this.source = source;
  851. return _this;
  852. }
  853. _createClass(YAMLError, [{
  854. key: "makePretty",
  855. value: function makePretty() {
  856. if (!this.source) return;
  857. this.nodeType = this.source.type;
  858. var cst = this.source.context && this.source.context.root;
  859. if (typeof this.offset === 'number') {
  860. this.range = new Range(this.offset, this.offset + 1);
  861. var start = cst && getLinePos(this.offset, cst);
  862. if (start) {
  863. var end = {
  864. line: start.line,
  865. col: start.col + 1
  866. };
  867. this.linePos = {
  868. start: start,
  869. end: end
  870. };
  871. }
  872. delete this.offset;
  873. } else {
  874. this.range = this.source.range;
  875. this.linePos = this.source.rangeAsLinePos;
  876. }
  877. if (this.linePos) {
  878. var _this$linePos$start = this.linePos.start,
  879. line = _this$linePos$start.line,
  880. col = _this$linePos$start.col;
  881. this.message += " at line ".concat(line, ", column ").concat(col);
  882. var ctx = cst && getPrettyContext(this.linePos, cst);
  883. if (ctx) this.message += ":\n\n".concat(ctx, "\n");
  884. }
  885. delete this.source;
  886. }
  887. }]);
  888. return YAMLError;
  889. }( /*#__PURE__*/_wrapNativeSuper(Error));
  890. var YAMLReferenceError = /*#__PURE__*/function (_YAMLError) {
  891. _inherits(YAMLReferenceError, _YAMLError);
  892. var _super2 = _createSuper(YAMLReferenceError);
  893. function YAMLReferenceError(source, message) {
  894. _classCallCheck(this, YAMLReferenceError);
  895. return _super2.call(this, 'YAMLReferenceError', source, message);
  896. }
  897. return YAMLReferenceError;
  898. }(YAMLError);
  899. var YAMLSemanticError = /*#__PURE__*/function (_YAMLError2) {
  900. _inherits(YAMLSemanticError, _YAMLError2);
  901. var _super3 = _createSuper(YAMLSemanticError);
  902. function YAMLSemanticError(source, message) {
  903. _classCallCheck(this, YAMLSemanticError);
  904. return _super3.call(this, 'YAMLSemanticError', source, message);
  905. }
  906. return YAMLSemanticError;
  907. }(YAMLError);
  908. var YAMLSyntaxError = /*#__PURE__*/function (_YAMLError3) {
  909. _inherits(YAMLSyntaxError, _YAMLError3);
  910. var _super4 = _createSuper(YAMLSyntaxError);
  911. function YAMLSyntaxError(source, message) {
  912. _classCallCheck(this, YAMLSyntaxError);
  913. return _super4.call(this, 'YAMLSyntaxError', source, message);
  914. }
  915. return YAMLSyntaxError;
  916. }(YAMLError);
  917. var YAMLWarning = /*#__PURE__*/function (_YAMLError4) {
  918. _inherits(YAMLWarning, _YAMLError4);
  919. var _super5 = _createSuper(YAMLWarning);
  920. function YAMLWarning(source, message) {
  921. _classCallCheck(this, YAMLWarning);
  922. return _super5.call(this, 'YAMLWarning', source, message);
  923. }
  924. return YAMLWarning;
  925. }(YAMLError);
  926. var PlainValue = /*#__PURE__*/function (_Node) {
  927. _inherits(PlainValue, _Node);
  928. var _super = _createSuper(PlainValue);
  929. function PlainValue() {
  930. _classCallCheck(this, PlainValue);
  931. return _super.apply(this, arguments);
  932. }
  933. _createClass(PlainValue, [{
  934. key: "strValue",
  935. get: function get() {
  936. if (!this.valueRange || !this.context) return null;
  937. var _this$valueRange = this.valueRange,
  938. start = _this$valueRange.start,
  939. end = _this$valueRange.end;
  940. var src = this.context.src;
  941. var ch = src[end - 1];
  942. while (start < end && (ch === '\n' || ch === '\t' || ch === ' ')) {
  943. ch = src[--end - 1];
  944. }
  945. var str = '';
  946. for (var i = start; i < end; ++i) {
  947. var _ch = src[i];
  948. if (_ch === '\n') {
  949. var _Node$foldNewline = Node.foldNewline(src, i, -1),
  950. fold = _Node$foldNewline.fold,
  951. offset = _Node$foldNewline.offset;
  952. str += fold;
  953. i = offset;
  954. } else if (_ch === ' ' || _ch === '\t') {
  955. // trim trailing whitespace
  956. var wsStart = i;
  957. var next = src[i + 1];
  958. while (i < end && (next === ' ' || next === '\t')) {
  959. i += 1;
  960. next = src[i + 1];
  961. }
  962. if (next !== '\n') str += i > wsStart ? src.slice(wsStart, i + 1) : _ch;
  963. } else {
  964. str += _ch;
  965. }
  966. }
  967. var ch0 = src[start];
  968. switch (ch0) {
  969. case '\t':
  970. {
  971. var msg = 'Plain value cannot start with a tab character';
  972. var errors = [new YAMLSemanticError(this, msg)];
  973. return {
  974. errors: errors,
  975. str: str
  976. };
  977. }
  978. case '@':
  979. case '`':
  980. {
  981. var _msg = "Plain value cannot start with reserved character ".concat(ch0);
  982. var _errors = [new YAMLSemanticError(this, _msg)];
  983. return {
  984. errors: _errors,
  985. str: str
  986. };
  987. }
  988. default:
  989. return str;
  990. }
  991. }
  992. }, {
  993. key: "parseBlockValue",
  994. value: function parseBlockValue(start) {
  995. var _this$context = this.context,
  996. indent = _this$context.indent,
  997. inFlow = _this$context.inFlow,
  998. src = _this$context.src;
  999. var offset = start;
  1000. var valueEnd = start;
  1001. for (var ch = src[offset]; ch === '\n'; ch = src[offset]) {
  1002. if (Node.atDocumentBoundary(src, offset + 1)) break;
  1003. var end = Node.endOfBlockIndent(src, indent, offset + 1);
  1004. if (end === null || src[end] === '#') break;
  1005. if (src[end] === '\n') {
  1006. offset = end;
  1007. } else {
  1008. valueEnd = PlainValue.endOfLine(src, end, inFlow);
  1009. offset = valueEnd;
  1010. }
  1011. }
  1012. if (this.valueRange.isEmpty()) this.valueRange.start = start;
  1013. this.valueRange.end = valueEnd;
  1014. return valueEnd;
  1015. }
  1016. /**
  1017. * Parses a plain value from the source
  1018. *
  1019. * Accepted forms are:
  1020. * ```
  1021. * #comment
  1022. *
  1023. * first line
  1024. *
  1025. * first line #comment
  1026. *
  1027. * first line
  1028. * block
  1029. * lines
  1030. *
  1031. * #comment
  1032. * block
  1033. * lines
  1034. * ```
  1035. * where block lines are empty or have an indent level greater than `indent`.
  1036. *
  1037. * @param {ParseContext} context
  1038. * @param {number} start - Index of first character
  1039. * @returns {number} - Index of the character after this scalar, may be `\n`
  1040. */
  1041. }, {
  1042. key: "parse",
  1043. value: function parse(context, start) {
  1044. this.context = context;
  1045. var inFlow = context.inFlow,
  1046. src = context.src;
  1047. var offset = start;
  1048. var ch = src[offset];
  1049. if (ch && ch !== '#' && ch !== '\n') {
  1050. offset = PlainValue.endOfLine(src, start, inFlow);
  1051. }
  1052. this.valueRange = new Range(start, offset);
  1053. offset = Node.endOfWhiteSpace(src, offset);
  1054. offset = this.parseComment(offset);
  1055. if (!this.hasComment || this.valueRange.isEmpty()) {
  1056. offset = this.parseBlockValue(offset);
  1057. }
  1058. return offset;
  1059. }
  1060. }], [{
  1061. key: "endOfLine",
  1062. value: function endOfLine(src, start, inFlow) {
  1063. var ch = src[start];
  1064. var offset = start;
  1065. while (ch && ch !== '\n') {
  1066. if (inFlow && (ch === '[' || ch === ']' || ch === '{' || ch === '}' || ch === ',')) break;
  1067. var next = src[offset + 1];
  1068. if (ch === ':' && (!next || next === '\n' || next === '\t' || next === ' ' || inFlow && next === ',')) break;
  1069. if ((ch === ' ' || ch === '\t') && next === '#') break;
  1070. offset += 1;
  1071. ch = next;
  1072. }
  1073. return offset;
  1074. }
  1075. }]);
  1076. return PlainValue;
  1077. }(Node);
  1078. export { Char as C, Node as N, PlainValue as P, Range as R, Type as T, YAMLSyntaxError as Y, _createForOfIteratorHelper as _, _typeof as a, _createClass as b, _classCallCheck as c, defaultTagPrefix as d, _defineProperty as e, YAMLWarning as f, YAMLSemanticError as g, _slicedToArray as h, YAMLError as i, _inherits as j, _createSuper as k, _get as l, _getPrototypeOf as m, defaultTags as n, YAMLReferenceError as o, _assertThisInitialized as p, _toArray as q, _possibleConstructorReturn as r };