resolve-flow-collection.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. 'use strict';
  2. var identity = require('../nodes/identity.js');
  3. var Pair = require('../nodes/Pair.js');
  4. var YAMLMap = require('../nodes/YAMLMap.js');
  5. var YAMLSeq = require('../nodes/YAMLSeq.js');
  6. var resolveEnd = require('./resolve-end.js');
  7. var resolveProps = require('./resolve-props.js');
  8. var utilContainsNewline = require('./util-contains-newline.js');
  9. var utilMapIncludes = require('./util-map-includes.js');
  10. const blockMsg = 'Block collections are not allowed within flow collections';
  11. const isBlock = (token) => token && (token.type === 'block-map' || token.type === 'block-seq');
  12. function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError, tag) {
  13. const isMap = fc.start.source === '{';
  14. const fcName = isMap ? 'flow map' : 'flow sequence';
  15. const NodeClass = (tag?.nodeClass ?? (isMap ? YAMLMap.YAMLMap : YAMLSeq.YAMLSeq));
  16. const coll = new NodeClass(ctx.schema);
  17. coll.flow = true;
  18. const atRoot = ctx.atRoot;
  19. if (atRoot)
  20. ctx.atRoot = false;
  21. if (ctx.atKey)
  22. ctx.atKey = false;
  23. let offset = fc.offset + fc.start.source.length;
  24. for (let i = 0; i < fc.items.length; ++i) {
  25. const collItem = fc.items[i];
  26. const { start, key, sep, value } = collItem;
  27. const props = resolveProps.resolveProps(start, {
  28. flow: fcName,
  29. indicator: 'explicit-key-ind',
  30. next: key ?? sep?.[0],
  31. offset,
  32. onError,
  33. parentIndent: fc.indent,
  34. startOnNewline: false
  35. });
  36. if (!props.found) {
  37. if (!props.anchor && !props.tag && !sep && !value) {
  38. if (i === 0 && props.comma)
  39. onError(props.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${fcName}`);
  40. else if (i < fc.items.length - 1)
  41. onError(props.start, 'UNEXPECTED_TOKEN', `Unexpected empty item in ${fcName}`);
  42. if (props.comment) {
  43. if (coll.comment)
  44. coll.comment += '\n' + props.comment;
  45. else
  46. coll.comment = props.comment;
  47. }
  48. offset = props.end;
  49. continue;
  50. }
  51. if (!isMap && ctx.options.strict && utilContainsNewline.containsNewline(key))
  52. onError(key, // checked by containsNewline()
  53. 'MULTILINE_IMPLICIT_KEY', 'Implicit keys of flow sequence pairs need to be on a single line');
  54. }
  55. if (i === 0) {
  56. if (props.comma)
  57. onError(props.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${fcName}`);
  58. }
  59. else {
  60. if (!props.comma)
  61. onError(props.start, 'MISSING_CHAR', `Missing , between ${fcName} items`);
  62. if (props.comment) {
  63. let prevItemComment = '';
  64. loop: for (const st of start) {
  65. switch (st.type) {
  66. case 'comma':
  67. case 'space':
  68. break;
  69. case 'comment':
  70. prevItemComment = st.source.substring(1);
  71. break loop;
  72. default:
  73. break loop;
  74. }
  75. }
  76. if (prevItemComment) {
  77. let prev = coll.items[coll.items.length - 1];
  78. if (identity.isPair(prev))
  79. prev = prev.value ?? prev.key;
  80. if (prev.comment)
  81. prev.comment += '\n' + prevItemComment;
  82. else
  83. prev.comment = prevItemComment;
  84. props.comment = props.comment.substring(prevItemComment.length + 1);
  85. }
  86. }
  87. }
  88. if (!isMap && !sep && !props.found) {
  89. // item is a value in a seq
  90. // → key & sep are empty, start does not include ? or :
  91. const valueNode = value
  92. ? composeNode(ctx, value, props, onError)
  93. : composeEmptyNode(ctx, props.end, sep, null, props, onError);
  94. coll.items.push(valueNode);
  95. offset = valueNode.range[2];
  96. if (isBlock(value))
  97. onError(valueNode.range, 'BLOCK_IN_FLOW', blockMsg);
  98. }
  99. else {
  100. // item is a key+value pair
  101. // key value
  102. ctx.atKey = true;
  103. const keyStart = props.end;
  104. const keyNode = key
  105. ? composeNode(ctx, key, props, onError)
  106. : composeEmptyNode(ctx, keyStart, start, null, props, onError);
  107. if (isBlock(key))
  108. onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
  109. ctx.atKey = false;
  110. // value properties
  111. const valueProps = resolveProps.resolveProps(sep ?? [], {
  112. flow: fcName,
  113. indicator: 'map-value-ind',
  114. next: value,
  115. offset: keyNode.range[2],
  116. onError,
  117. parentIndent: fc.indent,
  118. startOnNewline: false
  119. });
  120. if (valueProps.found) {
  121. if (!isMap && !props.found && ctx.options.strict) {
  122. if (sep)
  123. for (const st of sep) {
  124. if (st === valueProps.found)
  125. break;
  126. if (st.type === 'newline') {
  127. onError(st, 'MULTILINE_IMPLICIT_KEY', 'Implicit keys of flow sequence pairs need to be on a single line');
  128. break;
  129. }
  130. }
  131. if (props.start < valueProps.found.offset - 1024)
  132. onError(valueProps.found, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit flow sequence key');
  133. }
  134. }
  135. else if (value) {
  136. if ('source' in value && value.source && value.source[0] === ':')
  137. onError(value, 'MISSING_CHAR', `Missing space after : in ${fcName}`);
  138. else
  139. onError(valueProps.start, 'MISSING_CHAR', `Missing , or : between ${fcName} items`);
  140. }
  141. // value value
  142. const valueNode = value
  143. ? composeNode(ctx, value, valueProps, onError)
  144. : valueProps.found
  145. ? composeEmptyNode(ctx, valueProps.end, sep, null, valueProps, onError)
  146. : null;
  147. if (valueNode) {
  148. if (isBlock(value))
  149. onError(valueNode.range, 'BLOCK_IN_FLOW', blockMsg);
  150. }
  151. else if (valueProps.comment) {
  152. if (keyNode.comment)
  153. keyNode.comment += '\n' + valueProps.comment;
  154. else
  155. keyNode.comment = valueProps.comment;
  156. }
  157. const pair = new Pair.Pair(keyNode, valueNode);
  158. if (ctx.options.keepSourceTokens)
  159. pair.srcToken = collItem;
  160. if (isMap) {
  161. const map = coll;
  162. if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
  163. onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
  164. map.items.push(pair);
  165. }
  166. else {
  167. const map = new YAMLMap.YAMLMap(ctx.schema);
  168. map.flow = true;
  169. map.items.push(pair);
  170. const endRange = (valueNode ?? keyNode).range;
  171. map.range = [keyNode.range[0], endRange[1], endRange[2]];
  172. coll.items.push(map);
  173. }
  174. offset = valueNode ? valueNode.range[2] : valueProps.end;
  175. }
  176. }
  177. const expectedEnd = isMap ? '}' : ']';
  178. const [ce, ...ee] = fc.end;
  179. let cePos = offset;
  180. if (ce && ce.source === expectedEnd)
  181. cePos = ce.offset + ce.source.length;
  182. else {
  183. const name = fcName[0].toUpperCase() + fcName.substring(1);
  184. const msg = atRoot
  185. ? `${name} must end with a ${expectedEnd}`
  186. : `${name} in block collection must be sufficiently indented and end with a ${expectedEnd}`;
  187. onError(offset, atRoot ? 'MISSING_CHAR' : 'BAD_INDENT', msg);
  188. if (ce && ce.source.length !== 1)
  189. ee.unshift(ce);
  190. }
  191. if (ee.length > 0) {
  192. const end = resolveEnd.resolveEnd(ee, cePos, ctx.options.strict, onError);
  193. if (end.comment) {
  194. if (coll.comment)
  195. coll.comment += '\n' + end.comment;
  196. else
  197. coll.comment = end.comment;
  198. }
  199. coll.range = [fc.offset, cePos, end.offset];
  200. }
  201. else {
  202. coll.range = [fc.offset, cePos, cePos];
  203. }
  204. return coll;
  205. }
  206. exports.resolveFlowCollection = resolveFlowCollection;