resolve-flow-collection.js 8.4 KB

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