resolve-block-seq.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. if (ctx.atKey)
  11. ctx.atKey = false;
  12. let offset = bs.offset;
  13. let commentEnd = null;
  14. for (const { start, value } of bs.items) {
  15. const props = resolveProps.resolveProps(start, {
  16. indicator: 'seq-item-ind',
  17. next: value,
  18. offset,
  19. onError,
  20. parentIndent: bs.indent,
  21. startOnNewline: true
  22. });
  23. if (!props.found) {
  24. if (props.anchor || props.tag || value) {
  25. if (value && value.type === 'block-seq')
  26. onError(props.end, 'BAD_INDENT', 'All sequence items must start at the same column');
  27. else
  28. onError(offset, 'MISSING_CHAR', 'Sequence item without - indicator');
  29. }
  30. else {
  31. commentEnd = props.end;
  32. if (props.comment)
  33. seq.comment = props.comment;
  34. continue;
  35. }
  36. }
  37. const node = value
  38. ? composeNode(ctx, value, props, onError)
  39. : composeEmptyNode(ctx, props.end, start, null, props, onError);
  40. if (ctx.schema.compat)
  41. utilFlowIndentCheck.flowIndentCheck(bs.indent, value, onError);
  42. offset = node.range[2];
  43. seq.items.push(node);
  44. }
  45. seq.range = [bs.offset, offset, commentEnd ?? offset];
  46. return seq;
  47. }
  48. exports.resolveBlockSeq = resolveBlockSeq;