resolve-block-map.js 5.0 KB

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