resolve-block-seq.js 1.6 KB

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