暂无描述

resolve-block-map.js 4.6KB

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