Нет описания

compose-collection.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { isNode, isMap } from '../nodes/Node.js';
  2. import { Scalar } from '../nodes/Scalar.js';
  3. import { resolveBlockMap } from './resolve-block-map.js';
  4. import { resolveBlockSeq } from './resolve-block-seq.js';
  5. import { resolveFlowCollection } from './resolve-flow-collection.js';
  6. function composeCollection(CN, ctx, token, tagToken, onError) {
  7. let coll;
  8. switch (token.type) {
  9. case 'block-map': {
  10. coll = resolveBlockMap(CN, ctx, token, onError);
  11. break;
  12. }
  13. case 'block-seq': {
  14. coll = resolveBlockSeq(CN, ctx, token, onError);
  15. break;
  16. }
  17. case 'flow-collection': {
  18. coll = resolveFlowCollection(CN, ctx, token, onError);
  19. break;
  20. }
  21. }
  22. if (!tagToken)
  23. return coll;
  24. const tagName = ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
  25. if (!tagName)
  26. return coll;
  27. // Cast needed due to: https://github.com/Microsoft/TypeScript/issues/3841
  28. const Coll = coll.constructor;
  29. if (tagName === '!' || tagName === Coll.tagName) {
  30. coll.tag = Coll.tagName;
  31. return coll;
  32. }
  33. const expType = isMap(coll) ? 'map' : 'seq';
  34. let tag = ctx.schema.tags.find(t => t.collection === expType && t.tag === tagName);
  35. if (!tag) {
  36. const kt = ctx.schema.knownTags[tagName];
  37. if (kt && kt.collection === expType) {
  38. ctx.schema.tags.push(Object.assign({}, kt, { default: false }));
  39. tag = kt;
  40. }
  41. else {
  42. onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
  43. coll.tag = tagName;
  44. return coll;
  45. }
  46. }
  47. const res = tag.resolve(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options);
  48. const node = isNode(res)
  49. ? res
  50. : new Scalar(res);
  51. node.range = coll.range;
  52. node.tag = tagName;
  53. if (tag?.format)
  54. node.format = tag.format;
  55. return node;
  56. }
  57. export { composeCollection };