1 | import { isNode } from '../nodes/identity.js';
|
---|
2 | import { Scalar } from '../nodes/Scalar.js';
|
---|
3 | import { YAMLMap } from '../nodes/YAMLMap.js';
|
---|
4 | import { YAMLSeq } from '../nodes/YAMLSeq.js';
|
---|
5 | import { resolveBlockMap } from './resolve-block-map.js';
|
---|
6 | import { resolveBlockSeq } from './resolve-block-seq.js';
|
---|
7 | import { resolveFlowCollection } from './resolve-flow-collection.js';
|
---|
8 |
|
---|
9 | function 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 | }
|
---|
26 | function 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 |
|
---|
76 | export { composeCollection };
|
---|