UNPKG

3.62 kBJavaScriptView Raw
1'use strict';
2
3var identity = require('../nodes/identity.js');
4var Scalar = require('../nodes/Scalar.js');
5var YAMLMap = require('../nodes/YAMLMap.js');
6var YAMLSeq = require('../nodes/YAMLSeq.js');
7var resolveBlockMap = require('./resolve-block-map.js');
8var resolveBlockSeq = require('./resolve-block-seq.js');
9var resolveFlowCollection = require('./resolve-flow-collection.js');
10
11function resolveCollection(CN, ctx, token, onError, tagName, tag) {
12 const coll = token.type === 'block-map'
13 ? resolveBlockMap.resolveBlockMap(CN, ctx, token, onError, tag)
14 : token.type === 'block-seq'
15 ? resolveBlockSeq.resolveBlockSeq(CN, ctx, token, onError, tag)
16 : resolveFlowCollection.resolveFlowCollection(CN, ctx, token, onError, tag);
17 const Coll = coll.constructor;
18 // If we got a tagName matching the class, or the tag name is '!',
19 // then use the tagName from the node class used to create it.
20 if (tagName === '!' || tagName === Coll.tagName) {
21 coll.tag = Coll.tagName;
22 return coll;
23 }
24 if (tagName)
25 coll.tag = tagName;
26 return coll;
27}
28function composeCollection(CN, ctx, token, props, onError) {
29 const tagToken = props.tag;
30 const tagName = !tagToken
31 ? null
32 : ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
33 if (token.type === 'block-seq') {
34 const { anchor, newlineAfterProp: nl } = props;
35 const lastProp = anchor && tagToken
36 ? anchor.offset > tagToken.offset
37 ? anchor
38 : tagToken
39 : (anchor ?? tagToken);
40 if (lastProp && (!nl || nl.offset < lastProp.offset)) {
41 const message = 'Missing newline after block sequence props';
42 onError(lastProp, 'MISSING_CHAR', message);
43 }
44 }
45 const expType = token.type === 'block-map'
46 ? 'map'
47 : token.type === 'block-seq'
48 ? 'seq'
49 : token.start.source === '{'
50 ? 'map'
51 : 'seq';
52 // shortcut: check if it's a generic YAMLMap or YAMLSeq
53 // before jumping into the custom tag logic.
54 if (!tagToken ||
55 !tagName ||
56 tagName === '!' ||
57 (tagName === YAMLMap.YAMLMap.tagName && expType === 'map') ||
58 (tagName === YAMLSeq.YAMLSeq.tagName && expType === 'seq')) {
59 return resolveCollection(CN, ctx, token, onError, tagName);
60 }
61 let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType);
62 if (!tag) {
63 const kt = ctx.schema.knownTags[tagName];
64 if (kt && kt.collection === expType) {
65 ctx.schema.tags.push(Object.assign({}, kt, { default: false }));
66 tag = kt;
67 }
68 else {
69 if (kt?.collection) {
70 onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true);
71 }
72 else {
73 onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
74 }
75 return resolveCollection(CN, ctx, token, onError, tagName);
76 }
77 }
78 const coll = resolveCollection(CN, ctx, token, onError, tagName, tag);
79 const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll;
80 const node = identity.isNode(res)
81 ? res
82 : new Scalar.Scalar(res);
83 node.range = coll.range;
84 node.tag = tagName;
85 if (tag?.format)
86 node.format = tag.format;
87 return node;
88}
89
90exports.composeCollection = composeCollection;