[d24f17c] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var Alias = require('../nodes/Alias.js');
|
---|
| 4 | var composeCollection = require('./compose-collection.js');
|
---|
| 5 | var composeScalar = require('./compose-scalar.js');
|
---|
| 6 | var resolveEnd = require('./resolve-end.js');
|
---|
| 7 | var utilEmptyScalarPosition = require('./util-empty-scalar-position.js');
|
---|
| 8 |
|
---|
| 9 | const CN = { composeNode, composeEmptyNode };
|
---|
| 10 | function composeNode(ctx, token, props, onError) {
|
---|
| 11 | const { spaceBefore, comment, anchor, tag } = props;
|
---|
| 12 | let node;
|
---|
| 13 | let isSrcToken = true;
|
---|
| 14 | switch (token.type) {
|
---|
| 15 | case 'alias':
|
---|
| 16 | node = composeAlias(ctx, token, onError);
|
---|
| 17 | if (anchor || tag)
|
---|
| 18 | onError(token, 'ALIAS_PROPS', 'An alias node must not specify any properties');
|
---|
| 19 | break;
|
---|
| 20 | case 'scalar':
|
---|
| 21 | case 'single-quoted-scalar':
|
---|
| 22 | case 'double-quoted-scalar':
|
---|
| 23 | case 'block-scalar':
|
---|
| 24 | node = composeScalar.composeScalar(ctx, token, tag, onError);
|
---|
| 25 | if (anchor)
|
---|
| 26 | node.anchor = anchor.source.substring(1);
|
---|
| 27 | break;
|
---|
| 28 | case 'block-map':
|
---|
| 29 | case 'block-seq':
|
---|
| 30 | case 'flow-collection':
|
---|
| 31 | node = composeCollection.composeCollection(CN, ctx, token, tag, onError);
|
---|
| 32 | if (anchor)
|
---|
| 33 | node.anchor = anchor.source.substring(1);
|
---|
| 34 | break;
|
---|
| 35 | default: {
|
---|
| 36 | const message = token.type === 'error'
|
---|
| 37 | ? token.message
|
---|
| 38 | : `Unsupported token (type: ${token.type})`;
|
---|
| 39 | onError(token, 'UNEXPECTED_TOKEN', message);
|
---|
| 40 | node = composeEmptyNode(ctx, token.offset, undefined, null, props, onError);
|
---|
| 41 | isSrcToken = false;
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | if (anchor && node.anchor === '')
|
---|
| 45 | onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
|
---|
| 46 | if (spaceBefore)
|
---|
| 47 | node.spaceBefore = true;
|
---|
| 48 | if (comment) {
|
---|
| 49 | if (token.type === 'scalar' && token.source === '')
|
---|
| 50 | node.comment = comment;
|
---|
| 51 | else
|
---|
| 52 | node.commentBefore = comment;
|
---|
| 53 | }
|
---|
| 54 | // @ts-expect-error Type checking misses meaning of isSrcToken
|
---|
| 55 | if (ctx.options.keepSourceTokens && isSrcToken)
|
---|
| 56 | node.srcToken = token;
|
---|
| 57 | return node;
|
---|
| 58 | }
|
---|
| 59 | function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anchor, tag, end }, onError) {
|
---|
| 60 | const token = {
|
---|
| 61 | type: 'scalar',
|
---|
| 62 | offset: utilEmptyScalarPosition.emptyScalarPosition(offset, before, pos),
|
---|
| 63 | indent: -1,
|
---|
| 64 | source: ''
|
---|
| 65 | };
|
---|
| 66 | const node = composeScalar.composeScalar(ctx, token, tag, onError);
|
---|
| 67 | if (anchor) {
|
---|
| 68 | node.anchor = anchor.source.substring(1);
|
---|
| 69 | if (node.anchor === '')
|
---|
| 70 | onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
|
---|
| 71 | }
|
---|
| 72 | if (spaceBefore)
|
---|
| 73 | node.spaceBefore = true;
|
---|
| 74 | if (comment) {
|
---|
| 75 | node.comment = comment;
|
---|
| 76 | node.range[2] = end;
|
---|
| 77 | }
|
---|
| 78 | return node;
|
---|
| 79 | }
|
---|
| 80 | function composeAlias({ options }, { offset, source, end }, onError) {
|
---|
| 81 | const alias = new Alias.Alias(source.substring(1));
|
---|
| 82 | if (alias.source === '')
|
---|
| 83 | onError(offset, 'BAD_ALIAS', 'Alias cannot be an empty string');
|
---|
| 84 | if (alias.source.endsWith(':'))
|
---|
| 85 | onError(offset + source.length - 1, 'BAD_ALIAS', 'Alias ending in : is ambiguous', true);
|
---|
| 86 | const valueEnd = offset + source.length;
|
---|
| 87 | const re = resolveEnd.resolveEnd(end, valueEnd, options.strict, onError);
|
---|
| 88 | alias.range = [offset, valueEnd, re.offset];
|
---|
| 89 | if (re.comment)
|
---|
| 90 | alias.comment = re.comment;
|
---|
| 91 | return alias;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | exports.composeEmptyNode = composeEmptyNode;
|
---|
| 95 | exports.composeNode = composeNode;
|
---|