1 | 'use strict';
|
---|
2 |
|
---|
3 | var identity = require('../nodes/identity.js');
|
---|
4 | var Scalar = require('../nodes/Scalar.js');
|
---|
5 | var YAMLMap = require('../nodes/YAMLMap.js');
|
---|
6 | var YAMLSeq = require('../nodes/YAMLSeq.js');
|
---|
7 | var resolveBlockMap = require('./resolve-block-map.js');
|
---|
8 | var resolveBlockSeq = require('./resolve-block-seq.js');
|
---|
9 | var resolveFlowCollection = require('./resolve-flow-collection.js');
|
---|
10 |
|
---|
11 | function 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 | }
|
---|
28 | function composeCollection(CN, ctx, token, tagToken, onError) {
|
---|
29 | const tagName = !tagToken
|
---|
30 | ? null
|
---|
31 | : ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
|
---|
32 | const expType = token.type === 'block-map'
|
---|
33 | ? 'map'
|
---|
34 | : token.type === 'block-seq'
|
---|
35 | ? 'seq'
|
---|
36 | : token.start.source === '{'
|
---|
37 | ? 'map'
|
---|
38 | : 'seq';
|
---|
39 | // shortcut: check if it's a generic YAMLMap or YAMLSeq
|
---|
40 | // before jumping into the custom tag logic.
|
---|
41 | if (!tagToken ||
|
---|
42 | !tagName ||
|
---|
43 | tagName === '!' ||
|
---|
44 | (tagName === YAMLMap.YAMLMap.tagName && expType === 'map') ||
|
---|
45 | (tagName === YAMLSeq.YAMLSeq.tagName && expType === 'seq') ||
|
---|
46 | !expType) {
|
---|
47 | return resolveCollection(CN, ctx, token, onError, tagName);
|
---|
48 | }
|
---|
49 | let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType);
|
---|
50 | if (!tag) {
|
---|
51 | const kt = ctx.schema.knownTags[tagName];
|
---|
52 | if (kt && kt.collection === expType) {
|
---|
53 | ctx.schema.tags.push(Object.assign({}, kt, { default: false }));
|
---|
54 | tag = kt;
|
---|
55 | }
|
---|
56 | else {
|
---|
57 | if (kt?.collection) {
|
---|
58 | onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true);
|
---|
59 | }
|
---|
60 | else {
|
---|
61 | onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
|
---|
62 | }
|
---|
63 | return resolveCollection(CN, ctx, token, onError, tagName);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | const coll = resolveCollection(CN, ctx, token, onError, tagName, tag);
|
---|
67 | const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll;
|
---|
68 | const node = identity.isNode(res)
|
---|
69 | ? res
|
---|
70 | : new Scalar.Scalar(res);
|
---|
71 | node.range = coll.range;
|
---|
72 | node.tag = tagName;
|
---|
73 | if (tag?.format)
|
---|
74 | node.format = tag.format;
|
---|
75 | return node;
|
---|
76 | }
|
---|
77 |
|
---|
78 | exports.composeCollection = composeCollection;
|
---|