resolve-block-map.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. 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.hasNewlineAfterProp || utilContainsNewline.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. const keyStart = keyProps.end;
  53. const keyNode = key
  54. ? composeNode(ctx, key, keyProps, onError)
  55. : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
  56. if (ctx.schema.compat)
  57. utilFlowIndentCheck.flowIndentCheck(bm.indent, key, onError);
  58. if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
  59. onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
  60. // value properties
  61. const valueProps = resolveProps.resolveProps(sep ?? [], {
  62. indicator: 'map-value-ind',
  63. next: value,
  64. offset: keyNode.range[2],
  65. onError,
  66. startOnNewline: !key || key.type === 'block-scalar'
  67. });
  68. offset = valueProps.end;
  69. if (valueProps.found) {
  70. if (implicitKey) {
  71. if (value?.type === 'block-map' && !valueProps.hasNewline)
  72. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
  73. if (ctx.options.strict &&
  74. keyProps.start < valueProps.found.offset - 1024)
  75. onError(keyNode.range, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit block mapping key');
  76. }
  77. // value value
  78. const valueNode = value
  79. ? composeNode(ctx, value, valueProps, onError)
  80. : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
  81. if (ctx.schema.compat)
  82. utilFlowIndentCheck.flowIndentCheck(bm.indent, value, onError);
  83. offset = valueNode.range[2];
  84. const pair = new Pair.Pair(keyNode, valueNode);
  85. if (ctx.options.keepSourceTokens)
  86. pair.srcToken = collItem;
  87. map.items.push(pair);
  88. }
  89. else {
  90. // key with no value
  91. if (implicitKey)
  92. onError(keyNode.range, 'MISSING_CHAR', 'Implicit map keys need to be followed by map values');
  93. if (valueProps.comment) {
  94. if (keyNode.comment)
  95. keyNode.comment += '\n' + valueProps.comment;
  96. else
  97. keyNode.comment = valueProps.comment;
  98. }
  99. const pair = new Pair.Pair(keyNode);
  100. if (ctx.options.keepSourceTokens)
  101. pair.srcToken = collItem;
  102. map.items.push(pair);
  103. }
  104. }
  105. if (commentEnd && commentEnd < offset)
  106. onError(commentEnd, 'IMPOSSIBLE', 'Map comment with trailing content');
  107. map.range = [bm.offset, offset, commentEnd ?? offset];
  108. return map;
  109. }
  110. exports.resolveBlockMap = resolveBlockMap;