yaml 1.9.2 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/README.md +4 -4
  2. package/browser/dist/PlainValue-ff5147c6.js +1277 -0
  3. package/browser/dist/Schema-2bf2c74e.js +679 -0
  4. package/browser/dist/index.js +869 -34
  5. package/browser/dist/legacy-exports.js +3 -0
  6. package/browser/dist/parse-cst.js +1904 -0
  7. package/browser/dist/resolveSeq-04825f30.js +2373 -0
  8. package/browser/dist/types.js +4 -0
  9. package/browser/dist/util.js +2 -0
  10. package/browser/dist/warnings-0e4b70d3.js +499 -0
  11. package/browser/index.js +1 -1
  12. package/browser/map.js +2 -4
  13. package/browser/pair.js +2 -4
  14. package/browser/parse-cst.js +1 -1
  15. package/browser/scalar.js +2 -4
  16. package/browser/schema.js +7 -10
  17. package/browser/seq.js +2 -4
  18. package/browser/types/binary.js +7 -5
  19. package/browser/types/omap.js +3 -4
  20. package/browser/types/pairs.js +3 -4
  21. package/browser/types/set.js +3 -4
  22. package/browser/types/timestamp.js +9 -5
  23. package/browser/types.js +1 -13
  24. package/browser/util.js +1 -18
  25. package/dist/Document-2cf6b08c.js +757 -0
  26. package/dist/PlainValue-ec8e588e.js +876 -0
  27. package/dist/Schema-42e9705c.js +522 -0
  28. package/dist/index.js +20 -84
  29. package/dist/legacy-exports.js +16 -0
  30. package/dist/parse-cst.js +1747 -0
  31. package/dist/resolveSeq-4a68b39b.js +2115 -0
  32. package/dist/test-events.js +11 -9
  33. package/dist/types.js +23 -0
  34. package/dist/util.js +19 -0
  35. package/dist/warnings-39684f17.js +416 -0
  36. package/index.d.ts +22 -8
  37. package/map.js +2 -2
  38. package/package.json +22 -39
  39. package/pair.js +2 -2
  40. package/parse-cst.js +1 -1
  41. package/scalar.js +2 -2
  42. package/schema.js +8 -6
  43. package/seq.js +2 -2
  44. package/types/binary.js +3 -2
  45. package/types/omap.js +3 -2
  46. package/types/pairs.js +3 -2
  47. package/types/set.js +3 -2
  48. package/types/timestamp.js +6 -6
  49. package/types.d.ts +113 -99
  50. package/types.js +16 -11
  51. package/types.mjs +15 -19
  52. package/util.js +14 -14
  53. package/util.mjs +13 -20
  54. package/browser/dist/Anchors.js +0 -130
  55. package/browser/dist/Document.js +0 -824
  56. package/browser/dist/addComment.js +0 -8
  57. package/browser/dist/constants.js +0 -26
  58. package/browser/dist/cst/Alias.js +0 -46
  59. package/browser/dist/cst/BlankLine.js +0 -52
  60. package/browser/dist/cst/BlockValue.js +0 -251
  61. package/browser/dist/cst/Collection.js +0 -278
  62. package/browser/dist/cst/CollectionItem.js +0 -140
  63. package/browser/dist/cst/Comment.js +0 -44
  64. package/browser/dist/cst/Directive.js +0 -76
  65. package/browser/dist/cst/Document.js +0 -286
  66. package/browser/dist/cst/FlowCollection.js +0 -220
  67. package/browser/dist/cst/Node.js +0 -386
  68. package/browser/dist/cst/ParseContext.js +0 -246
  69. package/browser/dist/cst/PlainValue.js +0 -170
  70. package/browser/dist/cst/QuoteDouble.js +0 -245
  71. package/browser/dist/cst/QuoteSingle.js +0 -120
  72. package/browser/dist/cst/Range.js +0 -64
  73. package/browser/dist/cst/parse.js +0 -48
  74. package/browser/dist/cst/source-utils.js +0 -168
  75. package/browser/dist/errors.js +0 -127
  76. package/browser/dist/foldFlowLines.js +0 -138
  77. package/browser/dist/listTagNames.js +0 -28
  78. package/browser/dist/schema/Alias.js +0 -135
  79. package/browser/dist/schema/Collection.js +0 -260
  80. package/browser/dist/schema/Map.js +0 -161
  81. package/browser/dist/schema/Merge.js +0 -118
  82. package/browser/dist/schema/Node.js +0 -4
  83. package/browser/dist/schema/Pair.js +0 -194
  84. package/browser/dist/schema/Scalar.js +0 -42
  85. package/browser/dist/schema/Seq.js +0 -116
  86. package/browser/dist/schema/index.js +0 -368
  87. package/browser/dist/schema/parseMap.js +0 -312
  88. package/browser/dist/schema/parseSeq.js +0 -168
  89. package/browser/dist/schema/parseUtils.js +0 -106
  90. package/browser/dist/stringify.js +0 -332
  91. package/browser/dist/tags/core.js +0 -141
  92. package/browser/dist/tags/failsafe/index.js +0 -4
  93. package/browser/dist/tags/failsafe/map.js +0 -53
  94. package/browser/dist/tags/failsafe/seq.js +0 -39
  95. package/browser/dist/tags/failsafe/string.js +0 -28
  96. package/browser/dist/tags/index.js +0 -36
  97. package/browser/dist/tags/json.js +0 -77
  98. package/browser/dist/tags/options.js +0 -26
  99. package/browser/dist/tags/yaml-1.1/binary.js +0 -87
  100. package/browser/dist/tags/yaml-1.1/index.js +0 -189
  101. package/browser/dist/tags/yaml-1.1/omap.js +0 -137
  102. package/browser/dist/tags/yaml-1.1/pairs.js +0 -78
  103. package/browser/dist/tags/yaml-1.1/set.js +0 -118
  104. package/browser/dist/tags/yaml-1.1/timestamp.js +0 -97
  105. package/browser/dist/test-events.js +0 -163
  106. package/browser/dist/toJSON.js +0 -19
  107. package/browser/dist/warnings.js +0 -39
  108. package/dist/Anchors.js +0 -120
  109. package/dist/Document.js +0 -714
  110. package/dist/addComment.js +0 -14
  111. package/dist/constants.js +0 -31
  112. package/dist/cst/Alias.js +0 -33
  113. package/dist/cst/BlankLine.js +0 -40
  114. package/dist/cst/BlockValue.js +0 -243
  115. package/dist/cst/Collection.js +0 -266
  116. package/dist/cst/CollectionItem.js +0 -135
  117. package/dist/cst/Comment.js +0 -33
  118. package/dist/cst/Directive.js +0 -59
  119. package/dist/cst/Document.js +0 -276
  120. package/dist/cst/FlowCollection.js +0 -213
  121. package/dist/cst/Node.js +0 -360
  122. package/dist/cst/ParseContext.js +0 -253
  123. package/dist/cst/PlainValue.js +0 -156
  124. package/dist/cst/QuoteDouble.js +0 -232
  125. package/dist/cst/QuoteSingle.js +0 -108
  126. package/dist/cst/Range.js +0 -60
  127. package/dist/cst/parse.js +0 -50
  128. package/dist/cst/source-utils.js +0 -175
  129. package/dist/errors.js +0 -97
  130. package/dist/foldFlowLines.js +0 -143
  131. package/dist/listTagNames.js +0 -33
  132. package/dist/schema/Alias.js +0 -105
  133. package/dist/schema/Collection.js +0 -184
  134. package/dist/schema/Map.js +0 -98
  135. package/dist/schema/Merge.js +0 -75
  136. package/dist/schema/Node.js +0 -7
  137. package/dist/schema/Pair.js +0 -174
  138. package/dist/schema/Scalar.js +0 -26
  139. package/dist/schema/Seq.js +0 -73
  140. package/dist/schema/index.js +0 -310
  141. package/dist/schema/parseMap.js +0 -334
  142. package/dist/schema/parseSeq.js +0 -175
  143. package/dist/schema/parseUtils.js +0 -101
  144. package/dist/stringify.js +0 -352
  145. package/dist/tags/core.js +0 -121
  146. package/dist/tags/failsafe/index.js +0 -12
  147. package/dist/tags/failsafe/map.js +0 -32
  148. package/dist/tags/failsafe/seq.js +0 -29
  149. package/dist/tags/failsafe/string.js +0 -37
  150. package/dist/tags/index.js +0 -53
  151. package/dist/tags/json.js +0 -64
  152. package/dist/tags/options.js +0 -36
  153. package/dist/tags/yaml-1.1/binary.js +0 -94
  154. package/dist/tags/yaml-1.1/index.js +0 -162
  155. package/dist/tags/yaml-1.1/omap.js +0 -100
  156. package/dist/tags/yaml-1.1/pairs.js +0 -75
  157. package/dist/tags/yaml-1.1/set.js +0 -82
  158. package/dist/tags/yaml-1.1/timestamp.js +0 -90
  159. package/dist/toJSON.js +0 -21
  160. package/dist/warnings.js +0 -48
@@ -1,310 +0,0 @@
1
- "use strict";
2
-
3
- exports.Schema = void 0;
4
-
5
- var _warnings = require("../warnings");
6
-
7
- var _constants = require("../constants");
8
-
9
- var _errors = require("../errors");
10
-
11
- var _stringify = require("../stringify");
12
-
13
- var _tags = require("../tags");
14
-
15
- var _string = require("../tags/failsafe/string");
16
-
17
- var _Alias = require("./Alias");
18
-
19
- var _Collection = require("./Collection");
20
-
21
- var _Node = require("./Node");
22
-
23
- var _Pair = require("./Pair");
24
-
25
- var _Scalar = require("./Scalar");
26
-
27
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
28
-
29
- const isMap = ({
30
- type
31
- }) => type === _constants.Type.FLOW_MAP || type === _constants.Type.MAP;
32
-
33
- const isSeq = ({
34
- type
35
- }) => type === _constants.Type.FLOW_SEQ || type === _constants.Type.SEQ;
36
-
37
- class Schema {
38
- constructor({
39
- customTags,
40
- merge,
41
- schema,
42
- sortMapEntries,
43
- tags: deprecatedCustomTags
44
- }) {
45
- this.merge = !!merge;
46
- this.name = schema;
47
- this.sortMapEntries = sortMapEntries === true ? (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0 : sortMapEntries || null;
48
- this.tags = _tags.schemas[schema.replace(/\W/g, '')]; // 'yaml-1.1' -> 'yaml11'
49
-
50
- if (!this.tags) {
51
- const keys = Object.keys(_tags.schemas).map(key => JSON.stringify(key)).join(', ');
52
- throw new Error(`Unknown schema "${schema}"; use one of ${keys}`);
53
- }
54
-
55
- if (!customTags && deprecatedCustomTags) {
56
- customTags = deprecatedCustomTags;
57
- (0, _warnings.warnOptionDeprecation)('tags', 'customTags');
58
- }
59
-
60
- if (Array.isArray(customTags)) {
61
- for (const tag of customTags) this.tags = this.tags.concat(tag);
62
- } else if (typeof customTags === 'function') {
63
- this.tags = customTags(this.tags.slice());
64
- }
65
-
66
- for (let i = 0; i < this.tags.length; ++i) {
67
- const tag = this.tags[i];
68
-
69
- if (typeof tag === 'string') {
70
- const tagObj = _tags.tags[tag];
71
-
72
- if (!tagObj) {
73
- const keys = Object.keys(_tags.tags).map(key => JSON.stringify(key)).join(', ');
74
- throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
75
- }
76
-
77
- this.tags[i] = tagObj;
78
- }
79
- }
80
- }
81
-
82
- createNode(value, wrapScalars, tag, ctx) {
83
- if (value instanceof _Node.Node) return value;
84
- let tagObj;
85
-
86
- if (tag) {
87
- if (tag.startsWith('!!')) tag = Schema.defaultPrefix + tag.slice(2);
88
- const match = this.tags.filter(t => t.tag === tag);
89
- tagObj = match.find(t => !t.format) || match[0];
90
- if (!tagObj) throw new Error(`Tag ${tag} not found`);
91
- } else {
92
- // TODO: deprecate/remove class check
93
- tagObj = this.tags.find(t => (t.identify && t.identify(value) || t.class && value instanceof t.class) && !t.format);
94
-
95
- if (!tagObj) {
96
- if (typeof value.toJSON === 'function') value = value.toJSON();
97
- if (typeof value !== 'object') return wrapScalars ? new _Scalar.Scalar(value) : value;
98
- tagObj = value instanceof Map ? _tags.tags.map : value[Symbol.iterator] ? _tags.tags.seq : _tags.tags.map;
99
- }
100
- }
101
-
102
- if (!ctx) ctx = {
103
- wrapScalars
104
- };else ctx.wrapScalars = wrapScalars;
105
-
106
- if (ctx.onTagObj) {
107
- ctx.onTagObj(tagObj);
108
- delete ctx.onTagObj;
109
- }
110
-
111
- const obj = {};
112
-
113
- if (value && typeof value === 'object' && ctx.prevObjects) {
114
- const prev = ctx.prevObjects.get(value);
115
-
116
- if (prev) {
117
- const alias = new _Alias.Alias(prev); // leaves source dirty; must be cleaned by caller
118
-
119
- ctx.aliasNodes.push(alias);
120
- return alias;
121
- }
122
-
123
- obj.value = value;
124
- ctx.prevObjects.set(value, obj);
125
- }
126
-
127
- obj.node = tagObj.createNode ? tagObj.createNode(this, value, ctx) : wrapScalars ? new _Scalar.Scalar(value) : value;
128
- if (tag && obj.node instanceof _Node.Node) obj.node.tag = tag;
129
- return obj.node;
130
- }
131
-
132
- createPair(key, value, ctx) {
133
- const k = this.createNode(key, ctx.wrapScalars, null, ctx);
134
- const v = this.createNode(value, ctx.wrapScalars, null, ctx);
135
- return new _Pair.Pair(k, v);
136
- } // falls back to string on no match
137
-
138
-
139
- resolveScalar(str, tags) {
140
- if (!tags) tags = this.tags;
141
-
142
- for (let i = 0; i < tags.length; ++i) {
143
- const {
144
- format,
145
- test,
146
- resolve
147
- } = tags[i];
148
-
149
- if (test) {
150
- const match = str.match(test);
151
-
152
- if (match) {
153
- let res = resolve.apply(null, match);
154
- if (!(res instanceof _Scalar.Scalar)) res = new _Scalar.Scalar(res);
155
- if (format) res.format = format;
156
- return res;
157
- }
158
- }
159
- }
160
-
161
- if (this.tags.scalarFallback) str = this.tags.scalarFallback(str);
162
- return new _Scalar.Scalar(str);
163
- } // sets node.resolved on success
164
-
165
-
166
- resolveNode(doc, node, tagName) {
167
- const tags = this.tags.filter(({
168
- tag
169
- }) => tag === tagName);
170
- const generic = tags.find(({
171
- test
172
- }) => !test);
173
- if (node.error) doc.errors.push(node.error);
174
-
175
- try {
176
- if (generic) {
177
- let res = generic.resolve(doc, node);
178
- if (!(res instanceof _Collection.Collection)) res = new _Scalar.Scalar(res);
179
- node.resolved = res;
180
- } else {
181
- const str = (0, _string.resolveString)(doc, node);
182
-
183
- if (typeof str === 'string' && tags.length > 0) {
184
- node.resolved = this.resolveScalar(str, tags);
185
- }
186
- }
187
- } catch (error) {
188
- /* istanbul ignore if */
189
- if (!error.source) error.source = node;
190
- doc.errors.push(error);
191
- node.resolved = null;
192
- }
193
-
194
- if (!node.resolved) return null;
195
- if (tagName && node.tag) node.resolved.tag = tagName;
196
- return node.resolved;
197
- }
198
-
199
- resolveNodeWithFallback(doc, node, tagName) {
200
- const res = this.resolveNode(doc, node, tagName);
201
- if (Object.prototype.hasOwnProperty.call(node, 'resolved')) return res;
202
- const fallback = isMap(node) ? Schema.defaultTags.MAP : isSeq(node) ? Schema.defaultTags.SEQ : Schema.defaultTags.STR;
203
- /* istanbul ignore else */
204
-
205
- if (fallback) {
206
- doc.warnings.push(new _errors.YAMLWarning(node, `The tag ${tagName} is unavailable, falling back to ${fallback}`));
207
- const res = this.resolveNode(doc, node, fallback);
208
- res.tag = tagName;
209
- return res;
210
- } else {
211
- doc.errors.push(new _errors.YAMLReferenceError(node, `The tag ${tagName} is unavailable`));
212
- return null;
213
- }
214
- }
215
-
216
- getTagObject(item) {
217
- if (item instanceof _Alias.Alias) return _Alias.Alias;
218
-
219
- if (item.tag) {
220
- const match = this.tags.filter(t => t.tag === item.tag);
221
- if (match.length > 0) return match.find(t => t.format === item.format) || match[0];
222
- }
223
-
224
- let tagObj, obj;
225
-
226
- if (item instanceof _Scalar.Scalar) {
227
- obj = item.value; // TODO: deprecate/remove class check
228
-
229
- const match = this.tags.filter(t => t.identify && t.identify(obj) || t.class && obj instanceof t.class);
230
- tagObj = match.find(t => t.format === item.format) || match.find(t => !t.format);
231
- } else {
232
- obj = item;
233
- tagObj = this.tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
234
- }
235
-
236
- if (!tagObj) {
237
- const name = obj && obj.constructor ? obj.constructor.name : typeof obj;
238
- throw new Error(`Tag not resolved for ${name} value`);
239
- }
240
-
241
- return tagObj;
242
- } // needs to be called before stringifier to allow for circular anchor refs
243
-
244
-
245
- stringifyProps(node, tagObj, {
246
- anchors,
247
- doc
248
- }) {
249
- const props = [];
250
- const anchor = doc.anchors.getName(node);
251
-
252
- if (anchor) {
253
- anchors[anchor] = node;
254
- props.push(`&${anchor}`);
255
- }
256
-
257
- if (node.tag) {
258
- props.push(doc.stringifyTag(node.tag));
259
- } else if (!tagObj.default) {
260
- props.push(doc.stringifyTag(tagObj.tag));
261
- }
262
-
263
- return props.join(' ');
264
- }
265
-
266
- stringify(item, ctx, onComment, onChompKeep) {
267
- let tagObj;
268
-
269
- if (!(item instanceof _Node.Node)) {
270
- const createCtx = {
271
- aliasNodes: [],
272
- onTagObj: o => tagObj = o,
273
- prevObjects: new Map()
274
- };
275
- item = this.createNode(item, true, null, createCtx);
276
- const {
277
- anchors
278
- } = ctx.doc;
279
-
280
- for (const alias of createCtx.aliasNodes) {
281
- alias.source = alias.source.node;
282
- let name = anchors.getName(alias.source);
283
-
284
- if (!name) {
285
- name = anchors.newName();
286
- anchors.map[name] = alias.source;
287
- }
288
- }
289
- }
290
-
291
- ctx.tags = this;
292
- if (item instanceof _Pair.Pair) return item.toString(ctx, onComment, onChompKeep);
293
- if (!tagObj) tagObj = this.getTagObject(item);
294
- const props = this.stringifyProps(item, tagObj, ctx);
295
- if (props.length > 0) ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1;
296
- const str = typeof tagObj.stringify === 'function' ? tagObj.stringify(item, ctx, onComment, onChompKeep) : item instanceof _Collection.Collection ? item.toString(ctx, onComment, onChompKeep) : (0, _stringify.stringifyString)(item, ctx, onComment, onChompKeep);
297
- return props ? item instanceof _Collection.Collection && str[0] !== '{' && str[0] !== '[' ? `${props}\n${ctx.indent}${str}` : `${props} ${str}` : str;
298
- }
299
-
300
- }
301
-
302
- exports.Schema = Schema;
303
-
304
- _defineProperty(Schema, "defaultPrefix", 'tag:yaml.org,2002:');
305
-
306
- _defineProperty(Schema, "defaultTags", {
307
- MAP: 'tag:yaml.org,2002:map',
308
- SEQ: 'tag:yaml.org,2002:seq',
309
- STR: 'tag:yaml.org,2002:str'
310
- });
@@ -1,334 +0,0 @@
1
- "use strict";
2
-
3
- exports.parseMap = parseMap;
4
-
5
- var _constants = require("../constants");
6
-
7
- var _PlainValue = require("../cst/PlainValue");
8
-
9
- var _errors = require("../errors");
10
-
11
- var _Map = require("./Map");
12
-
13
- var _Merge = require("./Merge");
14
-
15
- var _Pair = require("./Pair");
16
-
17
- var _parseUtils = require("./parseUtils");
18
-
19
- var _Alias = require("./Alias");
20
-
21
- var _Collection = require("./Collection");
22
-
23
- function parseMap(doc, cst) {
24
- if (cst.type !== _constants.Type.MAP && cst.type !== _constants.Type.FLOW_MAP) {
25
- const msg = `A ${cst.type} node cannot be resolved as a mapping`;
26
- doc.errors.push(new _errors.YAMLSyntaxError(cst, msg));
27
- return null;
28
- }
29
-
30
- const {
31
- comments,
32
- items
33
- } = cst.type === _constants.Type.FLOW_MAP ? resolveFlowMapItems(doc, cst) : resolveBlockMapItems(doc, cst);
34
- const map = new _Map.YAMLMap();
35
- map.items = items;
36
- (0, _parseUtils.resolveComments)(map, comments);
37
- let hasCollectionKey = false;
38
-
39
- for (let i = 0; i < items.length; ++i) {
40
- const {
41
- key: iKey
42
- } = items[i];
43
- if (iKey instanceof _Collection.Collection) hasCollectionKey = true;
44
-
45
- if (doc.schema.merge && iKey && iKey.value === _Merge.MERGE_KEY) {
46
- items[i] = new _Merge.Merge(items[i]);
47
- const sources = items[i].value.items;
48
- let error = null;
49
- sources.some(node => {
50
- if (node instanceof _Alias.Alias) {
51
- // During parsing, alias sources are CST nodes; to account for
52
- // circular references their resolved values can't be used here.
53
- const {
54
- type
55
- } = node.source;
56
- if (type === _constants.Type.MAP || type === _constants.Type.FLOW_MAP) return false;
57
- return error = 'Merge nodes aliases can only point to maps';
58
- }
59
-
60
- return error = 'Merge nodes can only have Alias nodes as values';
61
- });
62
- if (error) doc.errors.push(new _errors.YAMLSemanticError(cst, error));
63
- } else {
64
- for (let j = i + 1; j < items.length; ++j) {
65
- const {
66
- key: jKey
67
- } = items[j];
68
-
69
- if (iKey === jKey || iKey && jKey && Object.prototype.hasOwnProperty.call(iKey, 'value') && iKey.value === jKey.value) {
70
- const msg = `Map keys must be unique; "${iKey}" is repeated`;
71
- doc.errors.push(new _errors.YAMLSemanticError(cst, msg));
72
- break;
73
- }
74
- }
75
- }
76
- }
77
-
78
- if (hasCollectionKey && !doc.options.mapAsMap) {
79
- const warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
80
- doc.warnings.push(new _errors.YAMLWarning(cst, warn));
81
- }
82
-
83
- cst.resolved = map;
84
- return map;
85
- }
86
-
87
- const valueHasPairComment = ({
88
- context: {
89
- lineStart,
90
- node,
91
- src
92
- },
93
- props
94
- }) => {
95
- if (props.length === 0) return false;
96
- const {
97
- start
98
- } = props[0];
99
- if (node && start > node.valueRange.start) return false;
100
- if (src[start] !== _constants.Char.COMMENT) return false;
101
-
102
- for (let i = lineStart; i < start; ++i) if (src[i] === '\n') return false;
103
-
104
- return true;
105
- };
106
-
107
- function resolvePairComment(item, pair) {
108
- if (!valueHasPairComment(item)) return;
109
- const comment = item.getPropValue(0, _constants.Char.COMMENT, true);
110
- let found = false;
111
- const cb = pair.value.commentBefore;
112
-
113
- if (cb && cb.startsWith(comment)) {
114
- pair.value.commentBefore = cb.substr(comment.length + 1);
115
- found = true;
116
- } else {
117
- const cc = pair.value.comment;
118
-
119
- if (!item.node && cc && cc.startsWith(comment)) {
120
- pair.value.comment = cc.substr(comment.length + 1);
121
- found = true;
122
- }
123
- }
124
-
125
- if (found) pair.comment = comment;
126
- }
127
-
128
- function resolveBlockMapItems(doc, cst) {
129
- const comments = [];
130
- const items = [];
131
- let key = undefined;
132
- let keyStart = null;
133
-
134
- for (let i = 0; i < cst.items.length; ++i) {
135
- const item = cst.items[i];
136
-
137
- switch (item.type) {
138
- case _constants.Type.BLANK_LINE:
139
- comments.push({
140
- afterKey: !!key,
141
- before: items.length
142
- });
143
- break;
144
-
145
- case _constants.Type.COMMENT:
146
- comments.push({
147
- afterKey: !!key,
148
- before: items.length,
149
- comment: item.comment
150
- });
151
- break;
152
-
153
- case _constants.Type.MAP_KEY:
154
- if (key !== undefined) items.push(new _Pair.Pair(key));
155
- if (item.error) doc.errors.push(item.error);
156
- key = doc.resolveNode(item.node);
157
- keyStart = null;
158
- break;
159
-
160
- case _constants.Type.MAP_VALUE:
161
- {
162
- if (key === undefined) key = null;
163
- if (item.error) doc.errors.push(item.error);
164
-
165
- if (!item.context.atLineStart && item.node && item.node.type === _constants.Type.MAP && !item.node.context.atLineStart) {
166
- const msg = 'Nested mappings are not allowed in compact mappings';
167
- doc.errors.push(new _errors.YAMLSemanticError(item.node, msg));
168
- }
169
-
170
- let valueNode = item.node;
171
-
172
- if (!valueNode && item.props.length > 0) {
173
- // Comments on an empty mapping value need to be preserved, so we
174
- // need to construct a minimal empty node here to use instead of the
175
- // missing `item.node`. -- eemeli/yaml#19
176
- valueNode = new _PlainValue.PlainValue(_constants.Type.PLAIN, []);
177
- valueNode.context = {
178
- parent: item,
179
- src: item.context.src
180
- };
181
- const pos = item.range.start + 1;
182
- valueNode.range = {
183
- start: pos,
184
- end: pos
185
- };
186
- valueNode.valueRange = {
187
- start: pos,
188
- end: pos
189
- };
190
-
191
- if (typeof item.range.origStart === 'number') {
192
- const origPos = item.range.origStart + 1;
193
- valueNode.range.origStart = valueNode.range.origEnd = origPos;
194
- valueNode.valueRange.origStart = valueNode.valueRange.origEnd = origPos;
195
- }
196
- }
197
-
198
- const pair = new _Pair.Pair(key, doc.resolveNode(valueNode));
199
- resolvePairComment(item, pair);
200
- items.push(pair);
201
- (0, _parseUtils.checkKeyLength)(doc.errors, cst, i, key, keyStart);
202
- key = undefined;
203
- keyStart = null;
204
- }
205
- break;
206
-
207
- default:
208
- if (key !== undefined) items.push(new _Pair.Pair(key));
209
- key = doc.resolveNode(item);
210
- keyStart = item.range.start;
211
- if (item.error) doc.errors.push(item.error);
212
-
213
- next: for (let j = i + 1;; ++j) {
214
- const nextItem = cst.items[j];
215
-
216
- switch (nextItem && nextItem.type) {
217
- case _constants.Type.BLANK_LINE:
218
- case _constants.Type.COMMENT:
219
- continue next;
220
-
221
- case _constants.Type.MAP_VALUE:
222
- break next;
223
-
224
- default:
225
- doc.errors.push(new _errors.YAMLSemanticError(item, 'Implicit map keys need to be followed by map values'));
226
- break next;
227
- }
228
- }
229
-
230
- if (item.valueRangeContainsNewline) {
231
- const msg = 'Implicit map keys need to be on a single line';
232
- doc.errors.push(new _errors.YAMLSemanticError(item, msg));
233
- }
234
-
235
- }
236
- }
237
-
238
- if (key !== undefined) items.push(new _Pair.Pair(key));
239
- return {
240
- comments,
241
- items
242
- };
243
- }
244
-
245
- function resolveFlowMapItems(doc, cst) {
246
- const comments = [];
247
- const items = [];
248
- let key = undefined;
249
- let keyStart = null;
250
- let explicitKey = false;
251
- let next = '{';
252
-
253
- for (let i = 0; i < cst.items.length; ++i) {
254
- (0, _parseUtils.checkKeyLength)(doc.errors, cst, i, key, keyStart);
255
- const item = cst.items[i];
256
-
257
- if (typeof item.char === 'string') {
258
- const {
259
- char,
260
- offset
261
- } = item;
262
-
263
- if (char === '?' && key === undefined && !explicitKey) {
264
- explicitKey = true;
265
- next = ':';
266
- continue;
267
- }
268
-
269
- if (char === ':') {
270
- if (key === undefined) key = null;
271
-
272
- if (next === ':') {
273
- next = ',';
274
- continue;
275
- }
276
- } else {
277
- if (explicitKey) {
278
- if (key === undefined && char !== ',') key = null;
279
- explicitKey = false;
280
- }
281
-
282
- if (key !== undefined) {
283
- items.push(new _Pair.Pair(key));
284
- key = undefined;
285
- keyStart = null;
286
-
287
- if (char === ',') {
288
- next = ':';
289
- continue;
290
- }
291
- }
292
- }
293
-
294
- if (char === '}') {
295
- if (i === cst.items.length - 1) continue;
296
- } else if (char === next) {
297
- next = ':';
298
- continue;
299
- }
300
-
301
- const msg = `Flow map contains an unexpected ${char}`;
302
- const err = new _errors.YAMLSyntaxError(cst, msg);
303
- err.offset = offset;
304
- doc.errors.push(err);
305
- } else if (item.type === _constants.Type.BLANK_LINE) {
306
- comments.push({
307
- afterKey: !!key,
308
- before: items.length
309
- });
310
- } else if (item.type === _constants.Type.COMMENT) {
311
- comments.push({
312
- afterKey: !!key,
313
- before: items.length,
314
- comment: item.comment
315
- });
316
- } else if (key === undefined) {
317
- if (next === ',') doc.errors.push(new _errors.YAMLSemanticError(item, 'Separator , missing in flow map'));
318
- key = doc.resolveNode(item);
319
- keyStart = explicitKey ? null : item.range.start; // TODO: add error for non-explicit multiline plain key
320
- } else {
321
- if (next !== ',') doc.errors.push(new _errors.YAMLSemanticError(item, 'Indicator : missing in flow map entry'));
322
- items.push(new _Pair.Pair(key, doc.resolveNode(item)));
323
- key = undefined;
324
- explicitKey = false;
325
- }
326
- }
327
-
328
- (0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst);
329
- if (key !== undefined) items.push(new _Pair.Pair(key));
330
- return {
331
- comments,
332
- items
333
- };
334
- }