resolve-block-seq.js 1.7 KB

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