yaml 1.8.3 → 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 (163) 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 +923 -38
  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 -2
  13. package/browser/pair.js +2 -2
  14. package/browser/parse-cst.js +1 -1
  15. package/browser/scalar.js +2 -2
  16. package/browser/schema.js +8 -6
  17. package/browser/seq.js +2 -2
  18. package/browser/types/binary.js +3 -2
  19. package/browser/types/omap.js +3 -2
  20. package/browser/types/pairs.js +3 -2
  21. package/browser/types/set.js +3 -2
  22. package/browser/types/timestamp.js +6 -6
  23. package/browser/types.js +1 -11
  24. package/browser/util.js +1 -15
  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 +21 -43
  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 -14
  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 +372 -0
  37. package/index.js +1 -1
  38. package/map.js +2 -2
  39. package/package.json +32 -45
  40. package/pair.js +2 -2
  41. package/parse-cst.d.ts +187 -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 +407 -0
  52. package/types.js +16 -10
  53. package/types.mjs +15 -18
  54. package/util.d.ts +86 -0
  55. package/util.js +14 -13
  56. package/util.mjs +13 -19
  57. package/browser/dist/Anchors.js +0 -133
  58. package/browser/dist/Document.js +0 -819
  59. package/browser/dist/addComment.js +0 -8
  60. package/browser/dist/constants.js +0 -26
  61. package/browser/dist/cst/Alias.js +0 -42
  62. package/browser/dist/cst/BlankLine.js +0 -56
  63. package/browser/dist/cst/BlockValue.js +0 -247
  64. package/browser/dist/cst/Collection.js +0 -274
  65. package/browser/dist/cst/CollectionItem.js +0 -136
  66. package/browser/dist/cst/Comment.js +0 -40
  67. package/browser/dist/cst/Directive.js +0 -72
  68. package/browser/dist/cst/Document.js +0 -282
  69. package/browser/dist/cst/FlowCollection.js +0 -216
  70. package/browser/dist/cst/Node.js +0 -388
  71. package/browser/dist/cst/ParseContext.js +0 -248
  72. package/browser/dist/cst/PlainValue.js +0 -166
  73. package/browser/dist/cst/QuoteDouble.js +0 -241
  74. package/browser/dist/cst/QuoteSingle.js +0 -116
  75. package/browser/dist/cst/Range.js +0 -67
  76. package/browser/dist/cst/parse.js +0 -48
  77. package/browser/dist/cst/source-utils.js +0 -168
  78. package/browser/dist/errors.js +0 -112
  79. package/browser/dist/foldFlowLines.js +0 -138
  80. package/browser/dist/listTagNames.js +0 -28
  81. package/browser/dist/schema/Alias.js +0 -139
  82. package/browser/dist/schema/Collection.js +0 -267
  83. package/browser/dist/schema/Map.js +0 -178
  84. package/browser/dist/schema/Merge.js +0 -127
  85. package/browser/dist/schema/Node.js +0 -7
  86. package/browser/dist/schema/Pair.js +0 -171
  87. package/browser/dist/schema/Scalar.js +0 -38
  88. package/browser/dist/schema/Seq.js +0 -114
  89. package/browser/dist/schema/index.js +0 -382
  90. package/browser/dist/schema/parseMap.js +0 -312
  91. package/browser/dist/schema/parseSeq.js +0 -168
  92. package/browser/dist/schema/parseUtils.js +0 -110
  93. package/browser/dist/stringify.js +0 -331
  94. package/browser/dist/tags/core.js +0 -126
  95. package/browser/dist/tags/failsafe/index.js +0 -4
  96. package/browser/dist/tags/failsafe/map.js +0 -56
  97. package/browser/dist/tags/failsafe/seq.js +0 -43
  98. package/browser/dist/tags/failsafe/string.js +0 -28
  99. package/browser/dist/tags/index.js +0 -36
  100. package/browser/dist/tags/json.js +0 -76
  101. package/browser/dist/tags/options.js +0 -23
  102. package/browser/dist/tags/yaml-1.1/binary.js +0 -87
  103. package/browser/dist/tags/yaml-1.1/index.js +0 -157
  104. package/browser/dist/tags/yaml-1.1/omap.js +0 -142
  105. package/browser/dist/tags/yaml-1.1/pairs.js +0 -81
  106. package/browser/dist/tags/yaml-1.1/set.js +0 -114
  107. package/browser/dist/tags/yaml-1.1/timestamp.js +0 -97
  108. package/browser/dist/test-events.js +0 -163
  109. package/browser/dist/toJSON.js +0 -20
  110. package/browser/dist/warnings.js +0 -28
  111. package/dist/Anchors.js +0 -125
  112. package/dist/Document.js +0 -710
  113. package/dist/addComment.js +0 -17
  114. package/dist/constants.js +0 -34
  115. package/dist/cst/Alias.js +0 -38
  116. package/dist/cst/BlankLine.js +0 -56
  117. package/dist/cst/BlockValue.js +0 -248
  118. package/dist/cst/Collection.js +0 -271
  119. package/dist/cst/CollectionItem.js +0 -140
  120. package/dist/cst/Comment.js +0 -38
  121. package/dist/cst/Directive.js +0 -64
  122. package/dist/cst/Document.js +0 -281
  123. package/dist/cst/FlowCollection.js +0 -218
  124. package/dist/cst/Node.js +0 -365
  125. package/dist/cst/ParseContext.js +0 -258
  126. package/dist/cst/PlainValue.js +0 -161
  127. package/dist/cst/QuoteDouble.js +0 -237
  128. package/dist/cst/QuoteSingle.js +0 -113
  129. package/dist/cst/Range.js +0 -63
  130. package/dist/cst/parse.js +0 -55
  131. package/dist/cst/source-utils.js +0 -178
  132. package/dist/errors.js +0 -102
  133. package/dist/foldFlowLines.js +0 -146
  134. package/dist/listTagNames.js +0 -38
  135. package/dist/schema/Alias.js +0 -110
  136. package/dist/schema/Collection.js +0 -188
  137. package/dist/schema/Map.js +0 -103
  138. package/dist/schema/Merge.js +0 -80
  139. package/dist/schema/Node.js +0 -10
  140. package/dist/schema/Pair.js +0 -161
  141. package/dist/schema/Scalar.js +0 -31
  142. package/dist/schema/Seq.js +0 -78
  143. package/dist/schema/index.js +0 -315
  144. package/dist/schema/parseMap.js +0 -343
  145. package/dist/schema/parseSeq.js +0 -180
  146. package/dist/schema/parseUtils.js +0 -104
  147. package/dist/stringify.js +0 -358
  148. package/dist/tags/core.js +0 -114
  149. package/dist/tags/failsafe/index.js +0 -17
  150. package/dist/tags/failsafe/map.js +0 -37
  151. package/dist/tags/failsafe/seq.js +0 -34
  152. package/dist/tags/failsafe/string.js +0 -40
  153. package/dist/tags/index.js +0 -62
  154. package/dist/tags/json.js +0 -60
  155. package/dist/tags/options.js +0 -35
  156. package/dist/tags/yaml-1.1/binary.js +0 -97
  157. package/dist/tags/yaml-1.1/index.js +0 -131
  158. package/dist/tags/yaml-1.1/omap.js +0 -105
  159. package/dist/tags/yaml-1.1/pairs.js +0 -80
  160. package/dist/tags/yaml-1.1/set.js +0 -91
  161. package/dist/tags/yaml-1.1/timestamp.js +0 -93
  162. package/dist/toJSON.js +0 -23
  163. package/dist/warnings.js +0 -39
package/dist/Document.js DELETED
@@ -1,710 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _addComment = _interopRequireDefault(require("./addComment"));
9
-
10
- var _Anchors = _interopRequireDefault(require("./Anchors"));
11
-
12
- var _constants = require("./constants");
13
-
14
- var _errors = require("./errors");
15
-
16
- var _listTagNames = _interopRequireDefault(require("./listTagNames"));
17
-
18
- var _schema = _interopRequireDefault(require("./schema"));
19
-
20
- var _Alias = _interopRequireDefault(require("./schema/Alias"));
21
-
22
- var _Collection = _interopRequireWildcard(require("./schema/Collection"));
23
-
24
- var _Node = _interopRequireDefault(require("./schema/Node"));
25
-
26
- var _Scalar = _interopRequireDefault(require("./schema/Scalar"));
27
-
28
- var _toJSON = _interopRequireDefault(require("./toJSON"));
29
-
30
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
31
-
32
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
33
-
34
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
-
36
- 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; }
37
-
38
- const isCollectionItem = node => node && [_constants.Type.MAP_KEY, _constants.Type.MAP_VALUE, _constants.Type.SEQ_ITEM].includes(node.type);
39
-
40
- class Document {
41
- constructor(options) {
42
- this.anchors = new _Anchors.default(options.anchorPrefix);
43
- this.commentBefore = null;
44
- this.comment = null;
45
- this.contents = null;
46
- this.directivesEndMarker = null;
47
- this.errors = [];
48
- this.options = options;
49
- this.schema = null;
50
- this.tagPrefixes = [];
51
- this.version = null;
52
- this.warnings = [];
53
- }
54
-
55
- assertCollectionContents() {
56
- if (this.contents instanceof _Collection.default) return true;
57
- throw new Error('Expected a YAML collection as document contents');
58
- }
59
-
60
- add(value) {
61
- this.assertCollectionContents();
62
- return this.contents.add(value);
63
- }
64
-
65
- addIn(path, value) {
66
- this.assertCollectionContents();
67
- this.contents.addIn(path, value);
68
- }
69
-
70
- delete(key) {
71
- this.assertCollectionContents();
72
- return this.contents.delete(key);
73
- }
74
-
75
- deleteIn(path) {
76
- if ((0, _Collection.isEmptyPath)(path)) {
77
- if (this.contents == null) return false;
78
- this.contents = null;
79
- return true;
80
- }
81
-
82
- this.assertCollectionContents();
83
- return this.contents.deleteIn(path);
84
- }
85
-
86
- getDefaults() {
87
- return Document.defaults[this.version] || Document.defaults[this.options.version] || {};
88
- }
89
-
90
- get(key, keepScalar) {
91
- return this.contents instanceof _Collection.default ? this.contents.get(key, keepScalar) : undefined;
92
- }
93
-
94
- getIn(path, keepScalar) {
95
- if ((0, _Collection.isEmptyPath)(path)) return !keepScalar && this.contents instanceof _Scalar.default ? this.contents.value : this.contents;
96
- return this.contents instanceof _Collection.default ? this.contents.getIn(path, keepScalar) : undefined;
97
- }
98
-
99
- has(key) {
100
- return this.contents instanceof _Collection.default ? this.contents.has(key) : false;
101
- }
102
-
103
- hasIn(path) {
104
- if ((0, _Collection.isEmptyPath)(path)) return this.contents !== undefined;
105
- return this.contents instanceof _Collection.default ? this.contents.hasIn(path) : false;
106
- }
107
-
108
- set(key, value) {
109
- this.assertCollectionContents();
110
- this.contents.set(key, value);
111
- }
112
-
113
- setIn(path, value) {
114
- if ((0, _Collection.isEmptyPath)(path)) this.contents = value;else {
115
- this.assertCollectionContents();
116
- this.contents.setIn(path, value);
117
- }
118
- }
119
-
120
- setSchema(id, customTags) {
121
- if (!id && !customTags && this.schema) return;
122
- if (typeof id === 'number') id = id.toFixed(1);
123
-
124
- if (id === '1.0' || id === '1.1' || id === '1.2') {
125
- if (this.version) this.version = id;else this.options.version = id;
126
- delete this.options.schema;
127
- } else if (id && typeof id === 'string') {
128
- this.options.schema = id;
129
- }
130
-
131
- if (Array.isArray(customTags)) this.options.customTags = customTags;
132
- const opt = Object.assign({}, this.getDefaults(), this.options);
133
- this.schema = new _schema.default(opt);
134
- }
135
-
136
- parse(node, prevDoc) {
137
- if (this.options.keepCstNodes) this.cstNode = node;
138
- if (this.options.keepNodeTypes) this.type = 'DOCUMENT';
139
- const {
140
- directives = [],
141
- contents = [],
142
- directivesEndMarker,
143
- error,
144
- valueRange
145
- } = node;
146
-
147
- if (error) {
148
- if (!error.source) error.source = this;
149
- this.errors.push(error);
150
- }
151
-
152
- this.parseDirectives(directives, prevDoc);
153
- if (directivesEndMarker) this.directivesEndMarker = true;
154
- this.range = valueRange ? [valueRange.start, valueRange.end] : null;
155
- this.setSchema();
156
- this.anchors._cstAliases = [];
157
- this.parseContents(contents);
158
- this.anchors.resolveNodes();
159
-
160
- if (this.options.prettyErrors) {
161
- for (const error of this.errors) if (error instanceof _errors.YAMLError) error.makePretty();
162
-
163
- for (const warn of this.warnings) if (warn instanceof _errors.YAMLError) warn.makePretty();
164
- }
165
-
166
- return this;
167
- }
168
-
169
- parseDirectives(directives, prevDoc) {
170
- const directiveComments = [];
171
- let hasDirectives = false;
172
- directives.forEach(directive => {
173
- const {
174
- comment,
175
- name
176
- } = directive;
177
-
178
- switch (name) {
179
- case 'TAG':
180
- this.resolveTagDirective(directive);
181
- hasDirectives = true;
182
- break;
183
-
184
- case 'YAML':
185
- case 'YAML:1.0':
186
- this.resolveYamlDirective(directive);
187
- hasDirectives = true;
188
- break;
189
-
190
- default:
191
- if (name) {
192
- const msg = `YAML only supports %TAG and %YAML directives, and not %${name}`;
193
- this.warnings.push(new _errors.YAMLWarning(directive, msg));
194
- }
195
-
196
- }
197
-
198
- if (comment) directiveComments.push(comment);
199
- });
200
-
201
- if (prevDoc && !hasDirectives && '1.1' === (this.version || prevDoc.version || this.options.version)) {
202
- const copyTagPrefix = ({
203
- handle,
204
- prefix
205
- }) => ({
206
- handle,
207
- prefix
208
- });
209
-
210
- this.tagPrefixes = prevDoc.tagPrefixes.map(copyTagPrefix);
211
- this.version = prevDoc.version;
212
- }
213
-
214
- this.commentBefore = directiveComments.join('\n') || null;
215
- }
216
-
217
- parseContents(contents) {
218
- const comments = {
219
- before: [],
220
- after: []
221
- };
222
- const contentNodes = [];
223
- let spaceBefore = false;
224
- contents.forEach(node => {
225
- if (node.valueRange) {
226
- if (contentNodes.length === 1) {
227
- const msg = 'Document is not valid YAML (bad indentation?)';
228
- this.errors.push(new _errors.YAMLSyntaxError(node, msg));
229
- }
230
-
231
- const res = this.resolveNode(node);
232
-
233
- if (spaceBefore) {
234
- res.spaceBefore = true;
235
- spaceBefore = false;
236
- }
237
-
238
- contentNodes.push(res);
239
- } else if (node.comment !== null) {
240
- const cc = contentNodes.length === 0 ? comments.before : comments.after;
241
- cc.push(node.comment);
242
- } else if (node.type === _constants.Type.BLANK_LINE) {
243
- spaceBefore = true;
244
-
245
- if (contentNodes.length === 0 && comments.before.length > 0 && !this.commentBefore) {
246
- // space-separated comments at start are parsed as document comments
247
- this.commentBefore = comments.before.join('\n');
248
- comments.before = [];
249
- }
250
- }
251
- });
252
-
253
- switch (contentNodes.length) {
254
- case 0:
255
- this.contents = null;
256
- comments.after = comments.before;
257
- break;
258
-
259
- case 1:
260
- this.contents = contentNodes[0];
261
-
262
- if (this.contents) {
263
- const cb = comments.before.join('\n') || null;
264
-
265
- if (cb) {
266
- const cbNode = this.contents instanceof _Collection.default && this.contents.items[0] ? this.contents.items[0] : this.contents;
267
- cbNode.commentBefore = cbNode.commentBefore ? `${cb}\n${cbNode.commentBefore}` : cb;
268
- }
269
- } else {
270
- comments.after = comments.before.concat(comments.after);
271
- }
272
-
273
- break;
274
-
275
- default:
276
- this.contents = contentNodes;
277
-
278
- if (this.contents[0]) {
279
- this.contents[0].commentBefore = comments.before.join('\n') || null;
280
- } else {
281
- comments.after = comments.before.concat(comments.after);
282
- }
283
-
284
- }
285
-
286
- this.comment = comments.after.join('\n') || null;
287
- }
288
-
289
- resolveTagDirective(directive) {
290
- const [handle, prefix] = directive.parameters;
291
-
292
- if (handle && prefix) {
293
- if (this.tagPrefixes.every(p => p.handle !== handle)) {
294
- this.tagPrefixes.push({
295
- handle,
296
- prefix
297
- });
298
- } else {
299
- const msg = 'The %TAG directive must only be given at most once per handle in the same document.';
300
- this.errors.push(new _errors.YAMLSemanticError(directive, msg));
301
- }
302
- } else {
303
- const msg = 'Insufficient parameters given for %TAG directive';
304
- this.errors.push(new _errors.YAMLSemanticError(directive, msg));
305
- }
306
- }
307
-
308
- resolveYamlDirective(directive) {
309
- let [version] = directive.parameters;
310
- if (directive.name === 'YAML:1.0') version = '1.0';
311
-
312
- if (this.version) {
313
- const msg = 'The %YAML directive must only be given at most once per document.';
314
- this.errors.push(new _errors.YAMLSemanticError(directive, msg));
315
- }
316
-
317
- if (!version) {
318
- const msg = 'Insufficient parameters given for %YAML directive';
319
- this.errors.push(new _errors.YAMLSemanticError(directive, msg));
320
- } else {
321
- if (!Document.defaults[version]) {
322
- const v0 = this.version || this.options.version;
323
- const msg = `Document will be parsed as YAML ${v0} rather than YAML ${version}`;
324
- this.warnings.push(new _errors.YAMLWarning(directive, msg));
325
- }
326
-
327
- this.version = version;
328
- }
329
- }
330
-
331
- resolveTagName(node) {
332
- const {
333
- tag,
334
- type
335
- } = node;
336
- let nonSpecific = false;
337
-
338
- if (tag) {
339
- const {
340
- handle,
341
- suffix,
342
- verbatim
343
- } = tag;
344
-
345
- if (verbatim) {
346
- if (verbatim !== '!' && verbatim !== '!!') return verbatim;
347
- const msg = `Verbatim tags aren't resolved, so ${verbatim} is invalid.`;
348
- this.errors.push(new _errors.YAMLSemanticError(node, msg));
349
- } else if (handle === '!' && !suffix) {
350
- nonSpecific = true;
351
- } else {
352
- let prefix = this.tagPrefixes.find(p => p.handle === handle);
353
-
354
- if (!prefix) {
355
- const dtp = this.getDefaults().tagPrefixes;
356
- if (dtp) prefix = dtp.find(p => p.handle === handle);
357
- }
358
-
359
- if (prefix) {
360
- if (suffix) {
361
- if (handle === '!' && (this.version || this.options.version) === '1.0') {
362
- if (suffix[0] === '^') return suffix;
363
-
364
- if (/[:/]/.test(suffix)) {
365
- // word/foo -> tag:word.yaml.org,2002:foo
366
- const vocab = suffix.match(/^([a-z0-9-]+)\/(.*)/i);
367
- return vocab ? `tag:${vocab[1]}.yaml.org,2002:${vocab[2]}` : `tag:${suffix}`;
368
- }
369
- }
370
-
371
- return prefix.prefix + decodeURIComponent(suffix);
372
- }
373
-
374
- this.errors.push(new _errors.YAMLSemanticError(node, `The ${handle} tag has no suffix.`));
375
- } else {
376
- const msg = `The ${handle} tag handle is non-default and was not declared.`;
377
- this.errors.push(new _errors.YAMLSemanticError(node, msg));
378
- }
379
- }
380
- }
381
-
382
- switch (type) {
383
- case _constants.Type.BLOCK_FOLDED:
384
- case _constants.Type.BLOCK_LITERAL:
385
- case _constants.Type.QUOTE_DOUBLE:
386
- case _constants.Type.QUOTE_SINGLE:
387
- return _schema.default.defaultTags.STR;
388
-
389
- case _constants.Type.FLOW_MAP:
390
- case _constants.Type.MAP:
391
- return _schema.default.defaultTags.MAP;
392
-
393
- case _constants.Type.FLOW_SEQ:
394
- case _constants.Type.SEQ:
395
- return _schema.default.defaultTags.SEQ;
396
-
397
- case _constants.Type.PLAIN:
398
- return nonSpecific ? _schema.default.defaultTags.STR : null;
399
-
400
- default:
401
- return null;
402
- }
403
- }
404
-
405
- resolveNode(node) {
406
- if (!node) return null;
407
- const {
408
- anchors,
409
- errors,
410
- schema
411
- } = this;
412
- let hasAnchor = false;
413
- let hasTag = false;
414
- const comments = {
415
- before: [],
416
- after: []
417
- };
418
- const props = isCollectionItem(node.context.parent) ? node.context.parent.props.concat(node.props) : node.props;
419
-
420
- for (const {
421
- start,
422
- end
423
- } of props) {
424
- switch (node.context.src[start]) {
425
- case _constants.Char.COMMENT:
426
- {
427
- if (!node.commentHasRequiredWhitespace(start)) {
428
- const msg = 'Comments must be separated from other tokens by white space characters';
429
- errors.push(new _errors.YAMLSemanticError(node, msg));
430
- }
431
-
432
- const c = node.context.src.slice(start + 1, end);
433
- const {
434
- header,
435
- valueRange
436
- } = node;
437
-
438
- if (valueRange && (start > valueRange.start || header && start > header.start)) {
439
- comments.after.push(c);
440
- } else {
441
- comments.before.push(c);
442
- }
443
- }
444
- break;
445
-
446
- case _constants.Char.ANCHOR:
447
- if (hasAnchor) {
448
- const msg = 'A node can have at most one anchor';
449
- errors.push(new _errors.YAMLSemanticError(node, msg));
450
- }
451
-
452
- hasAnchor = true;
453
- break;
454
-
455
- case _constants.Char.TAG:
456
- if (hasTag) {
457
- const msg = 'A node can have at most one tag';
458
- errors.push(new _errors.YAMLSemanticError(node, msg));
459
- }
460
-
461
- hasTag = true;
462
- break;
463
- }
464
- }
465
-
466
- if (hasAnchor) {
467
- const name = node.anchor;
468
- const prev = anchors.getNode(name); // At this point, aliases for any preceding node with the same anchor
469
- // name have already been resolved, so it may safely be renamed.
470
-
471
- if (prev) anchors.map[anchors.newName(name)] = prev; // During parsing, we need to store the CST node in anchors.map as
472
- // anchors need to be available during resolution to allow for
473
- // circular references.
474
-
475
- anchors.map[name] = node;
476
- }
477
-
478
- let res;
479
-
480
- if (node.type === _constants.Type.ALIAS) {
481
- if (hasAnchor || hasTag) {
482
- const msg = 'An alias node must not specify any properties';
483
- errors.push(new _errors.YAMLSemanticError(node, msg));
484
- }
485
-
486
- const name = node.rawValue;
487
- const src = anchors.getNode(name);
488
-
489
- if (!src) {
490
- const msg = `Aliased anchor not found: ${name}`;
491
- errors.push(new _errors.YAMLReferenceError(node, msg));
492
- return null;
493
- } // Lazy resolution for circular references
494
-
495
-
496
- res = new _Alias.default(src);
497
-
498
- anchors._cstAliases.push(res);
499
- } else {
500
- const tagName = this.resolveTagName(node);
501
-
502
- if (tagName) {
503
- res = schema.resolveNodeWithFallback(this, node, tagName);
504
- } else {
505
- if (node.type !== _constants.Type.PLAIN) {
506
- const msg = `Failed to resolve ${node.type} node here`;
507
- errors.push(new _errors.YAMLSyntaxError(node, msg));
508
- return null;
509
- }
510
-
511
- try {
512
- res = schema.resolveScalar(node.strValue || '');
513
- } catch (error) {
514
- if (!error.source) error.source = node;
515
- errors.push(error);
516
- return null;
517
- }
518
- }
519
- }
520
-
521
- if (res) {
522
- res.range = [node.range.start, node.range.end];
523
- if (this.options.keepCstNodes) res.cstNode = node;
524
- if (this.options.keepNodeTypes) res.type = node.type;
525
- const cb = comments.before.join('\n');
526
-
527
- if (cb) {
528
- res.commentBefore = res.commentBefore ? `${res.commentBefore}\n${cb}` : cb;
529
- }
530
-
531
- const ca = comments.after.join('\n');
532
- if (ca) res.comment = res.comment ? `${res.comment}\n${ca}` : ca;
533
- }
534
-
535
- return node.resolved = res;
536
- }
537
-
538
- listNonDefaultTags() {
539
- return (0, _listTagNames.default)(this.contents).filter(t => t.indexOf(_schema.default.defaultPrefix) !== 0);
540
- }
541
-
542
- setTagPrefix(handle, prefix) {
543
- if (handle[0] !== '!' || handle[handle.length - 1] !== '!') throw new Error('Handle must start and end with !');
544
-
545
- if (prefix) {
546
- const prev = this.tagPrefixes.find(p => p.handle === handle);
547
- if (prev) prev.prefix = prefix;else this.tagPrefixes.push({
548
- handle,
549
- prefix
550
- });
551
- } else {
552
- this.tagPrefixes = this.tagPrefixes.filter(p => p.handle !== handle);
553
- }
554
- }
555
-
556
- stringifyTag(tag) {
557
- if ((this.version || this.options.version) === '1.0') {
558
- const priv = tag.match(/^tag:private\.yaml\.org,2002:([^:/]+)$/);
559
- if (priv) return '!' + priv[1];
560
- const vocab = tag.match(/^tag:([a-zA-Z0-9-]+)\.yaml\.org,2002:(.*)/);
561
- return vocab ? `!${vocab[1]}/${vocab[2]}` : `!${tag.replace(/^tag:/, '')}`;
562
- } else {
563
- let p = this.tagPrefixes.find(p => tag.indexOf(p.prefix) === 0);
564
-
565
- if (!p) {
566
- const dtp = this.getDefaults().tagPrefixes;
567
- p = dtp && dtp.find(p => tag.indexOf(p.prefix) === 0);
568
- }
569
-
570
- if (!p) return tag[0] === '!' ? tag : `!<${tag}>`;
571
- const suffix = tag.substr(p.prefix.length).replace(/[!,[\]{}]/g, ch => ({
572
- '!': '%21',
573
- ',': '%2C',
574
- '[': '%5B',
575
- ']': '%5D',
576
- '{': '%7B',
577
- '}': '%7D'
578
- })[ch]);
579
- return p.handle + suffix;
580
- }
581
- }
582
-
583
- toJSON(arg) {
584
- const {
585
- keepBlobsInJSON,
586
- mapAsMap,
587
- maxAliasCount
588
- } = this.options;
589
- const keep = keepBlobsInJSON && (typeof arg !== 'string' || !(this.contents instanceof _Scalar.default));
590
- const ctx = {
591
- doc: this,
592
- keep,
593
- mapAsMap: keep && !!mapAsMap,
594
- maxAliasCount
595
- };
596
- const anchorNames = Object.keys(this.anchors.map);
597
- if (anchorNames.length > 0) ctx.anchors = anchorNames.map(name => ({
598
- alias: [],
599
- aliasCount: 0,
600
- count: 1,
601
- node: this.anchors.map[name]
602
- }));
603
- return (0, _toJSON.default)(this.contents, arg, ctx);
604
- }
605
-
606
- toString() {
607
- if (this.errors.length > 0) throw new Error('Document with errors cannot be stringified');
608
- this.setSchema();
609
- const lines = [];
610
- let hasDirectives = false;
611
-
612
- if (this.version) {
613
- let vd = '%YAML 1.2';
614
-
615
- if (this.schema.name === 'yaml-1.1') {
616
- if (this.version === '1.0') vd = '%YAML:1.0';else if (this.version === '1.1') vd = '%YAML 1.1';
617
- }
618
-
619
- lines.push(vd);
620
- hasDirectives = true;
621
- }
622
-
623
- const tagNames = this.listNonDefaultTags();
624
- this.tagPrefixes.forEach(({
625
- handle,
626
- prefix
627
- }) => {
628
- if (tagNames.some(t => t.indexOf(prefix) === 0)) {
629
- lines.push(`%TAG ${handle} ${prefix}`);
630
- hasDirectives = true;
631
- }
632
- });
633
- if (hasDirectives || this.directivesEndMarker) lines.push('---');
634
-
635
- if (this.commentBefore) {
636
- if (hasDirectives || !this.directivesEndMarker) lines.unshift('');
637
- lines.unshift(this.commentBefore.replace(/^/gm, '#'));
638
- }
639
-
640
- const ctx = {
641
- anchors: {},
642
- doc: this,
643
- indent: ''
644
- };
645
- let chompKeep = false;
646
- let contentComment = null;
647
-
648
- if (this.contents) {
649
- if (this.contents instanceof _Node.default) {
650
- if (this.contents.spaceBefore && (hasDirectives || this.directivesEndMarker)) lines.push('');
651
- if (this.contents.commentBefore) lines.push(this.contents.commentBefore.replace(/^/gm, '#')); // top-level block scalars need to be indented if followed by a comment
652
-
653
- ctx.forceBlockIndent = !!this.comment;
654
- contentComment = this.contents.comment;
655
- }
656
-
657
- const onChompKeep = contentComment ? null : () => chompKeep = true;
658
- const body = this.schema.stringify(this.contents, ctx, () => contentComment = null, onChompKeep);
659
- lines.push((0, _addComment.default)(body, '', contentComment));
660
- } else if (this.contents !== undefined) {
661
- lines.push(this.schema.stringify(this.contents, ctx));
662
- }
663
-
664
- if (this.comment) {
665
- if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '') lines.push('');
666
- lines.push(this.comment.replace(/^/gm, '#'));
667
- }
668
-
669
- return lines.join('\n') + '\n';
670
- }
671
-
672
- }
673
-
674
- exports.default = Document;
675
-
676
- _defineProperty(Document, "defaults", {
677
- '1.0': {
678
- schema: 'yaml-1.1',
679
- merge: true,
680
- tagPrefixes: [{
681
- handle: '!',
682
- prefix: _schema.default.defaultPrefix
683
- }, {
684
- handle: '!!',
685
- prefix: 'tag:private.yaml.org,2002:'
686
- }]
687
- },
688
- '1.1': {
689
- schema: 'yaml-1.1',
690
- merge: true,
691
- tagPrefixes: [{
692
- handle: '!',
693
- prefix: '!'
694
- }, {
695
- handle: '!!',
696
- prefix: _schema.default.defaultPrefix
697
- }]
698
- },
699
- '1.2': {
700
- schema: 'core',
701
- merge: false,
702
- tagPrefixes: [{
703
- handle: '!',
704
- prefix: '!'
705
- }, {
706
- handle: '!!',
707
- prefix: _schema.default.defaultPrefix
708
- }]
709
- }
710
- });