UNPKG

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