resolve-block-map.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. startOnNewline: true
  24. });
  25. const implicitKey = !keyProps.found;
  26. if (implicitKey) {
  27. if (key) {
  28. if (key.type === 'block-seq')
  29. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'A block sequence may not be used as an implicit map key');
  30. else if ('indent' in key && key.indent !== bm.indent)
  31. onError(offset, 'BAD_INDENT', startColMsg);
  32. }
  33. if (!keyProps.anchor && !keyProps.tag && !sep) {
  34. commentEnd = keyProps.end;
  35. if (keyProps.comment) {
  36. if (map.comment)
  37. map.comment += '\n' + keyProps.comment;
  38. else
  39. map.comment = keyProps.comment;
  40. }
  41. continue;
  42. }
  43. if (keyProps.hasNewlineAfterProp || containsNewline(key)) {
  44. onError(key ?? start[start.length - 1], 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
  45. }
  46. }
  47. else if (keyProps.found?.indent !== bm.indent) {
  48. onError(offset, 'BAD_INDENT', startColMsg);
  49. }
  50. // key value
  51. const keyStart = keyProps.end;
  52. const keyNode = key
  53. ? composeNode(ctx, key, keyProps, onError)
  54. : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
  55. if (ctx.schema.compat)
  56. flowIndentCheck(bm.indent, key, onError);
  57. if (mapIncludes(ctx, map.items, keyNode))
  58. onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
  59. // value properties
  60. const valueProps = resolveProps(sep ?? [], {
  61. indicator: 'map-value-ind',
  62. next: value,
  63. offset: keyNode.range[2],
  64. onError,
  65. startOnNewline: !key || key.type === 'block-scalar'
  66. });
  67. offset = valueProps.end;
  68. if (valueProps.found) {
  69. if (implicitKey) {
  70. if (value?.type === 'block-map' && !valueProps.hasNewline)
  71. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
  72. if (ctx.options.strict &&
  73. keyProps.start < valueProps.found.offset - 1024)
  74. onError(keyNode.range, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit block mapping key');
  75. }
  76. // value value
  77. const valueNode = value
  78. ? composeNode(ctx, value, valueProps, onError)
  79. : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
  80. if (ctx.schema.compat)
  81. flowIndentCheck(bm.indent, value, onError);
  82. offset = valueNode.range[2];
  83. const pair = new Pair(keyNode, valueNode);
  84. if (ctx.options.keepSourceTokens)
  85. pair.srcToken = collItem;
  86. map.items.push(pair);
  87. }
  88. else {
  89. // key with no value
  90. if (implicitKey)
  91. onError(keyNode.range, 'MISSING_CHAR', 'Implicit map keys need to be followed by map values');
  92. if (valueProps.comment) {
  93. if (keyNode.comment)
  94. keyNode.comment += '\n' + valueProps.comment;
  95. else
  96. keyNode.comment = valueProps.comment;
  97. }
  98. const pair = new Pair(keyNode);
  99. if (ctx.options.keepSourceTokens)
  100. pair.srcToken = collItem;
  101. map.items.push(pair);
  102. }
  103. }
  104. if (commentEnd && commentEnd < offset)
  105. onError(commentEnd, 'IMPOSSIBLE', 'Map comment with trailing content');
  106. map.range = [bm.offset, offset, commentEnd ?? offset];
  107. return map;
  108. }
  109. export { resolveBlockMap };