source: node_modules/yaml/browser/dist/compose/compose-collection.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[d24f17c]1import { isNode } from '../nodes/identity.js';
2import { Scalar } from '../nodes/Scalar.js';
3import { YAMLMap } from '../nodes/YAMLMap.js';
4import { YAMLSeq } from '../nodes/YAMLSeq.js';
5import { resolveBlockMap } from './resolve-block-map.js';
6import { resolveBlockSeq } from './resolve-block-seq.js';
7import { resolveFlowCollection } from './resolve-flow-collection.js';
8
9function resolveCollection(CN, ctx, token, onError, tagName, tag) {
10 const coll = token.type === 'block-map'
11 ? resolveBlockMap(CN, ctx, token, onError, tag)
12 : token.type === 'block-seq'
13 ? resolveBlockSeq(CN, ctx, token, onError, tag)
14 : resolveFlowCollection(CN, ctx, token, onError, tag);
15 const Coll = coll.constructor;
16 // If we got a tagName matching the class, or the tag name is '!',
17 // then use the tagName from the node class used to create it.
18 if (tagName === '!' || tagName === Coll.tagName) {
19 coll.tag = Coll.tagName;
20 return coll;
21 }
22 if (tagName)
23 coll.tag = tagName;
24 return coll;
25}
26function composeCollection(CN, ctx, token, tagToken, onError) {
27 const tagName = !tagToken
28 ? null
29 : ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
30 const expType = token.type === 'block-map'
31 ? 'map'
32 : token.type === 'block-seq'
33 ? 'seq'
34 : token.start.source === '{'
35 ? 'map'
36 : 'seq';
37 // shortcut: check if it's a generic YAMLMap or YAMLSeq
38 // before jumping into the custom tag logic.
39 if (!tagToken ||
40 !tagName ||
41 tagName === '!' ||
42 (tagName === YAMLMap.tagName && expType === 'map') ||
43 (tagName === YAMLSeq.tagName && expType === 'seq') ||
44 !expType) {
45 return resolveCollection(CN, ctx, token, onError, tagName);
46 }
47 let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType);
48 if (!tag) {
49 const kt = ctx.schema.knownTags[tagName];
50 if (kt && kt.collection === expType) {
51 ctx.schema.tags.push(Object.assign({}, kt, { default: false }));
52 tag = kt;
53 }
54 else {
55 if (kt?.collection) {
56 onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true);
57 }
58 else {
59 onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
60 }
61 return resolveCollection(CN, ctx, token, onError, tagName);
62 }
63 }
64 const coll = resolveCollection(CN, ctx, token, onError, tagName, tag);
65 const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll;
66 const node = isNode(res)
67 ? res
68 : new Scalar(res);
69 node.range = coll.range;
70 node.tag = tagName;
71 if (tag?.format)
72 node.format = tag.format;
73 return node;
74}
75
76export { composeCollection };
Note: See TracBrowser for help on using the repository browser.