resolve-block-map.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Pair } from '../nodes/Pair.js';
  2. import { YAMLMap } from '../nodes/YAMLMap.js';
  3. import { resolveProps } from './resolve-props.js';
  4. import { containsNewline } from './util-contains-newline.js';
  5. import { flowIndentCheck } from './util-flow-indent-check.js';
  6. import { mapIncludes } from './util-map-includes.js';
  7. const startColMsg = 'All mapping items must start at the same column';
  8. function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, tag) {
  9. const NodeClass = tag?.nodeClass ?? YAMLMap;
  10. const map = new NodeClass(ctx.schema);
  11. if (ctx.atRoot)
  12. ctx.atRoot = false;
  13. let offset = bm.offset;
  14. let commentEnd = null;
  15. for (const collItem of bm.items) {
  16. const { start, key, sep, value } = collItem;
  17. // key properties
  18. const keyProps = resolveProps(start, {
  19. indicator: 'explicit-key-ind',
  20. next: key ?? sep?.[0],
  21. offset,
  22. onError,
  23. parentIndent: bm.indent,
  24. startOnNewline: true
  25. });
  26. const implicitKey = !keyProps.found;
  27. if (implicitKey) {
  28. if (key) {
  29. if (key.type === 'block-seq')
  30. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'A block sequence may not be used as an implicit map key');
  31. else if ('indent' in key && key.indent !== bm.indent)
  32. onError(offset, 'BAD_INDENT', startColMsg);
  33. }
  34. if (!keyProps.anchor && !keyProps.tag && !sep) {
  35. commentEnd = keyProps.end;
  36. if (keyProps.comment) {
  37. if (map.comment)
  38. map.comment += '\n' + keyProps.comment;
  39. else
  40. map.comment = keyProps.comment;
  41. }
  42. continue;
  43. }
  44. if (keyProps.newlineAfterProp || containsNewline(key)) {
  45. onError(key ?? start[start.length - 1], 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
  46. }
  47. }
  48. else if (keyProps.found?.indent !== bm.indent) {
  49. onError(offset, 'BAD_INDENT', startColMsg);
  50. }
  51. // key value
  52. ctx.atKey = true;
  53. const keyStart = keyProps.end;
  54. const keyNode = key
  55. ? composeNode(ctx, key, keyProps, onError)
  56. : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
  57. if (ctx.schema.compat)
  58. flowIndentCheck(bm.indent, key, onError);
  59. ctx.atKey = false;
  60. if (mapIncludes(ctx, map.items, keyNode))
  61. onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
  62. // value properties
  63. const valueProps = resolveProps(sep ?? [], {
  64. indicator: 'map-value-ind',
  65. next: value,
  66. offset: keyNode.range[2],
  67. onError,
  68. parentIndent: bm.indent,
  69. startOnNewline: !key || key.type === 'block-scalar'
  70. });
  71. offset = valueProps.end;
  72. if (valueProps.found) {
  73. if (implicitKey) {
  74. if (value?.type === 'block-map' && !valueProps.hasNewline)
  75. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
  76. if (ctx.options.strict &&
  77. keyProps.start < valueProps.found.offset - 1024)
  78. onError(keyNode.range, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit block mapping key');
  79. }
  80. // value value
  81. const valueNode = value
  82. ? composeNode(ctx, value, valueProps, onError)
  83. : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
  84. if (ctx.schema.compat)
  85. flowIndentCheck(bm.indent, value, onError);
  86. offset = valueNode.range[2];
  87. const pair = new Pair(keyNode, valueNode);
  88. if (ctx.options.keepSourceTokens)
  89. pair.srcToken = collItem;
  90. map.items.push(pair);
  91. }
  92. else {
  93. // key with no value
  94. if (implicitKey)
  95. onError(keyNode.range, 'MISSING_CHAR', 'Implicit map keys need to be followed by map values');
  96. if (valueProps.comment) {
  97. if (keyNode.comment)
  98. keyNode.comment += '\n' + valueProps.comment;
  99. else
  100. keyNode.comment = valueProps.comment;
  101. }
  102. const pair = new Pair(keyNode);
  103. if (ctx.options.keepSourceTokens)
  104. pair.srcToken = collItem;
  105. map.items.push(pair);
  106. }
  107. }
  108. if (commentEnd && commentEnd < offset)
  109. onError(commentEnd, 'IMPOSSIBLE', 'Map comment with trailing content');
  110. map.range = [bm.offset, offset, commentEnd ?? offset];
  111. return map;
  112. }
  113. export { resolveBlockMap };