resolve-block-seq.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. var YAMLSeq = require('../nodes/YAMLSeq.js');
  3. var resolveProps = require('./resolve-props.js');
  4. var utilFlowIndentCheck = require('./util-flow-indent-check.js');
  5. function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError, tag) {
  6. const NodeClass = tag?.nodeClass ?? YAMLSeq.YAMLSeq;
  7. const seq = new NodeClass(ctx.schema);
  8. if (ctx.atRoot)
  9. ctx.atRoot = false;
  10. let offset = bs.offset;
  11. let commentEnd = null;
  12. for (const { start, value } of bs.items) {
  13. const props = resolveProps.resolveProps(start, {
  14. indicator: 'seq-item-ind',
  15. next: value,
  16. offset,
  17. onError,
  18. startOnNewline: true
  19. });
  20. if (!props.found) {
  21. if (props.anchor || props.tag || value) {
  22. if (value && value.type === 'block-seq')
  23. onError(props.end, 'BAD_INDENT', 'All sequence items must start at the same column');
  24. else
  25. onError(offset, 'MISSING_CHAR', 'Sequence item without - indicator');
  26. }
  27. else {
  28. commentEnd = props.end;
  29. if (props.comment)
  30. seq.comment = props.comment;
  31. continue;
  32. }
  33. }
  34. const node = value
  35. ? composeNode(ctx, value, props, onError)
  36. : composeEmptyNode(ctx, props.end, start, null, props, onError);
  37. if (ctx.schema.compat)
  38. utilFlowIndentCheck.flowIndentCheck(bs.indent, value, onError);
  39. offset = node.range[2];
  40. seq.items.push(node);
  41. }
  42. seq.range = [bs.offset, offset, commentEnd ?? offset];
  43. return seq;
  44. }
  45. exports.resolveBlockSeq = resolveBlockSeq;