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
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
- });