Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CollectionItem.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _constants = require("../constants");
  7. var _errors = require("../errors");
  8. var _BlankLine = _interopRequireDefault(require("./BlankLine"));
  9. var _Node = _interopRequireDefault(require("./Node"));
  10. var _Range = _interopRequireDefault(require("./Range"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. class CollectionItem extends _Node.default {
  13. constructor(type, props) {
  14. super(type, props);
  15. this.node = null;
  16. }
  17. get includesTrailingLines() {
  18. return !!this.node && this.node.includesTrailingLines;
  19. }
  20. /**
  21. * @param {ParseContext} context
  22. * @param {number} start - Index of first character
  23. * @returns {number} - Index of the character after this
  24. */
  25. parse(context, start) {
  26. this.context = context;
  27. const {
  28. parseNode,
  29. src
  30. } = context;
  31. let {
  32. atLineStart,
  33. lineStart
  34. } = context;
  35. if (!atLineStart && this.type === _constants.Type.SEQ_ITEM) this.error = new _errors.YAMLSemanticError(this, 'Sequence items must not have preceding content on the same line');
  36. const indent = atLineStart ? start - lineStart : context.indent;
  37. let offset = _Node.default.endOfWhiteSpace(src, start + 1);
  38. let ch = src[offset];
  39. const inlineComment = ch === '#';
  40. const comments = [];
  41. let blankLine = null;
  42. while (ch === '\n' || ch === '#') {
  43. if (ch === '#') {
  44. const end = _Node.default.endOfLine(src, offset + 1);
  45. comments.push(new _Range.default(offset, end));
  46. offset = end;
  47. } else {
  48. atLineStart = true;
  49. lineStart = offset + 1;
  50. const wsEnd = _Node.default.endOfWhiteSpace(src, lineStart);
  51. if (src[wsEnd] === '\n' && comments.length === 0) {
  52. blankLine = new _BlankLine.default();
  53. lineStart = blankLine.parse({
  54. src
  55. }, lineStart);
  56. }
  57. offset = _Node.default.endOfIndent(src, lineStart);
  58. }
  59. ch = src[offset];
  60. }
  61. if (_Node.default.nextNodeIsIndented(ch, offset - (lineStart + indent), this.type !== _constants.Type.SEQ_ITEM)) {
  62. this.node = parseNode({
  63. atLineStart,
  64. inCollection: false,
  65. indent,
  66. lineStart,
  67. parent: this
  68. }, offset);
  69. } else if (ch && lineStart > start + 1) {
  70. offset = lineStart - 1;
  71. }
  72. if (this.node) {
  73. if (blankLine) {
  74. // Only blank lines preceding non-empty nodes are captured. Note that
  75. // this means that collection item range start indices do not always
  76. // increase monotonically. -- eemeli/yaml#126
  77. const items = context.parent.items || context.parent.contents;
  78. if (items) items.push(blankLine);
  79. }
  80. if (comments.length) Array.prototype.push.apply(this.props, comments);
  81. offset = this.node.range.end;
  82. } else {
  83. if (inlineComment) {
  84. const c = comments[0];
  85. this.props.push(c);
  86. offset = c.end;
  87. } else {
  88. offset = _Node.default.endOfLine(src, start + 1);
  89. }
  90. }
  91. const end = this.node ? this.node.valueRange.end : offset;
  92. this.valueRange = new _Range.default(start, end);
  93. return offset;
  94. }
  95. setOrigRanges(cr, offset) {
  96. offset = super.setOrigRanges(cr, offset);
  97. return this.node ? this.node.setOrigRanges(cr, offset) : offset;
  98. }
  99. toString() {
  100. const {
  101. context: {
  102. src
  103. },
  104. node,
  105. range,
  106. value
  107. } = this;
  108. if (value != null) return value;
  109. const str = node ? src.slice(range.start, node.range.start) + String(node) : src.slice(range.start, range.end);
  110. return _Node.default.addStringTerminator(src, range.end, str);
  111. }
  112. }
  113. exports.default = CollectionItem;