1 | 'use strict';
|
---|
2 |
|
---|
3 | var Pair = require('../nodes/Pair.js');
|
---|
4 | var YAMLMap = require('../nodes/YAMLMap.js');
|
---|
5 | var resolveProps = require('./resolve-props.js');
|
---|
6 | var utilContainsNewline = require('./util-contains-newline.js');
|
---|
7 | var utilFlowIndentCheck = require('./util-flow-indent-check.js');
|
---|
8 | var utilMapIncludes = require('./util-map-includes.js');
|
---|
9 |
|
---|
10 | const startColMsg = 'All mapping items must start at the same column';
|
---|
11 | function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, tag) {
|
---|
12 | const NodeClass = tag?.nodeClass ?? YAMLMap.YAMLMap;
|
---|
13 | const map = new NodeClass(ctx.schema);
|
---|
14 | if (ctx.atRoot)
|
---|
15 | ctx.atRoot = false;
|
---|
16 | let offset = bm.offset;
|
---|
17 | let commentEnd = null;
|
---|
18 | for (const collItem of bm.items) {
|
---|
19 | const { start, key, sep, value } = collItem;
|
---|
20 | // key properties
|
---|
21 | const keyProps = resolveProps.resolveProps(start, {
|
---|
22 | indicator: 'explicit-key-ind',
|
---|
23 | next: key ?? sep?.[0],
|
---|
24 | offset,
|
---|
25 | onError,
|
---|
26 | startOnNewline: true
|
---|
27 | });
|
---|
28 | const implicitKey = !keyProps.found;
|
---|
29 | if (implicitKey) {
|
---|
30 | if (key) {
|
---|
31 | if (key.type === 'block-seq')
|
---|
32 | onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'A block sequence may not be used as an implicit map key');
|
---|
33 | else if ('indent' in key && key.indent !== bm.indent)
|
---|
34 | onError(offset, 'BAD_INDENT', startColMsg);
|
---|
35 | }
|
---|
36 | if (!keyProps.anchor && !keyProps.tag && !sep) {
|
---|
37 | commentEnd = keyProps.end;
|
---|
38 | if (keyProps.comment) {
|
---|
39 | if (map.comment)
|
---|
40 | map.comment += '\n' + keyProps.comment;
|
---|
41 | else
|
---|
42 | map.comment = keyProps.comment;
|
---|
43 | }
|
---|
44 | continue;
|
---|
45 | }
|
---|
46 | if (keyProps.hasNewlineAfterProp || utilContainsNewline.containsNewline(key)) {
|
---|
47 | onError(key ?? start[start.length - 1], 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
|
---|
48 | }
|
---|
49 | }
|
---|
50 | else if (keyProps.found?.indent !== bm.indent) {
|
---|
51 | onError(offset, 'BAD_INDENT', startColMsg);
|
---|
52 | }
|
---|
53 | // key value
|
---|
54 | const keyStart = keyProps.end;
|
---|
55 | const keyNode = key
|
---|
56 | ? composeNode(ctx, key, keyProps, onError)
|
---|
57 | : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
|
---|
58 | if (ctx.schema.compat)
|
---|
59 | utilFlowIndentCheck.flowIndentCheck(bm.indent, key, onError);
|
---|
60 | if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
|
---|
61 | onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
|
---|
62 | // value properties
|
---|
63 | const valueProps = resolveProps.resolveProps(sep ?? [], {
|
---|
64 | indicator: 'map-value-ind',
|
---|
65 | next: value,
|
---|
66 | offset: keyNode.range[2],
|
---|
67 | onError,
|
---|
68 | startOnNewline: !key || key.type === 'block-scalar'
|
---|
69 | });
|
---|
70 | offset = valueProps.end;
|
---|
71 | if (valueProps.found) {
|
---|
72 | if (implicitKey) {
|
---|
73 | if (value?.type === 'block-map' && !valueProps.hasNewline)
|
---|
74 | onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
|
---|
75 | if (ctx.options.strict &&
|
---|
76 | keyProps.start < valueProps.found.offset - 1024)
|
---|
77 | onError(keyNode.range, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit block mapping key');
|
---|
78 | }
|
---|
79 | // value value
|
---|
80 | const valueNode = value
|
---|
81 | ? composeNode(ctx, value, valueProps, onError)
|
---|
82 | : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
|
---|
83 | if (ctx.schema.compat)
|
---|
84 | utilFlowIndentCheck.flowIndentCheck(bm.indent, value, onError);
|
---|
85 | offset = valueNode.range[2];
|
---|
86 | const pair = new Pair.Pair(keyNode, valueNode);
|
---|
87 | if (ctx.options.keepSourceTokens)
|
---|
88 | pair.srcToken = collItem;
|
---|
89 | map.items.push(pair);
|
---|
90 | }
|
---|
91 | else {
|
---|
92 | // key with no value
|
---|
93 | if (implicitKey)
|
---|
94 | onError(keyNode.range, 'MISSING_CHAR', 'Implicit map keys need to be followed by map values');
|
---|
95 | if (valueProps.comment) {
|
---|
96 | if (keyNode.comment)
|
---|
97 | keyNode.comment += '\n' + valueProps.comment;
|
---|
98 | else
|
---|
99 | keyNode.comment = valueProps.comment;
|
---|
100 | }
|
---|
101 | const pair = new Pair.Pair(keyNode);
|
---|
102 | if (ctx.options.keepSourceTokens)
|
---|
103 | pair.srcToken = collItem;
|
---|
104 | map.items.push(pair);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | if (commentEnd && commentEnd < offset)
|
---|
108 | onError(commentEnd, 'IMPOSSIBLE', 'Map comment with trailing content');
|
---|
109 | map.range = [bm.offset, offset, commentEnd ?? offset];
|
---|
110 | return map;
|
---|
111 | }
|
---|
112 |
|
---|
113 | exports.resolveBlockMap = resolveBlockMap;
|
---|