yaml 1.9.1 → 1.10.2

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