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