yaml 2.0.0-8 → 2.0.1

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 (120) hide show
  1. package/README.md +1 -1
  2. package/browser/dist/compose/compose-doc.js +3 -2
  3. package/browser/dist/compose/compose-node.js +14 -3
  4. package/browser/dist/compose/compose-scalar.js +16 -10
  5. package/browser/dist/compose/composer.js +6 -7
  6. package/browser/dist/compose/resolve-block-map.js +24 -9
  7. package/browser/dist/compose/resolve-block-scalar.js +12 -3
  8. package/browser/dist/compose/resolve-block-seq.js +5 -0
  9. package/browser/dist/compose/resolve-flow-collection.js +18 -6
  10. package/browser/dist/compose/resolve-flow-scalar.js +2 -1
  11. package/browser/dist/compose/resolve-props.js +8 -0
  12. package/browser/dist/compose/util-flow-indent-check.js +15 -0
  13. package/browser/dist/doc/Document.js +49 -16
  14. package/browser/dist/doc/anchors.js +2 -2
  15. package/browser/dist/doc/createNode.js +7 -4
  16. package/browser/dist/doc/directives.js +7 -4
  17. package/browser/dist/index.js +1 -2
  18. package/browser/dist/node_modules/tslib/tslib.es6.js +164 -0
  19. package/browser/dist/nodes/Collection.js +8 -4
  20. package/browser/dist/nodes/Pair.js +2 -2
  21. package/browser/dist/nodes/Scalar.js +1 -1
  22. package/browser/dist/nodes/YAMLMap.js +6 -5
  23. package/browser/dist/nodes/YAMLSeq.js +2 -2
  24. package/browser/dist/nodes/addPairToJSMap.js +3 -2
  25. package/browser/dist/nodes/toJS.js +3 -1
  26. package/browser/dist/parse/cst-scalar.js +2 -6
  27. package/browser/dist/parse/cst-visit.js +2 -2
  28. package/browser/dist/parse/lexer.js +27 -11
  29. package/browser/dist/parse/parser.js +135 -57
  30. package/browser/dist/public-api.js +4 -5
  31. package/browser/dist/schema/Schema.js +14 -4
  32. package/browser/dist/schema/common/null.js +3 -1
  33. package/browser/dist/schema/core/float.js +4 -1
  34. package/browser/dist/schema/tags.js +17 -13
  35. package/browser/dist/schema/yaml-1.1/float.js +4 -1
  36. package/browser/dist/schema/yaml-1.1/omap.js +1 -1
  37. package/browser/dist/schema/yaml-1.1/pairs.js +2 -1
  38. package/browser/dist/stringify/stringify.js +53 -23
  39. package/browser/dist/stringify/stringifyCollection.js +115 -85
  40. package/browser/dist/stringify/stringifyComment.js +18 -12
  41. package/browser/dist/stringify/stringifyDocument.js +37 -15
  42. package/browser/dist/stringify/stringifyPair.js +35 -21
  43. package/browser/dist/stringify/stringifyString.js +41 -43
  44. package/browser/dist/util.js +3 -0
  45. package/browser/dist/visit.js +150 -62
  46. package/dist/compose/compose-doc.js +3 -2
  47. package/dist/compose/compose-node.d.ts +1 -0
  48. package/dist/compose/compose-node.js +14 -3
  49. package/dist/compose/compose-scalar.js +15 -9
  50. package/dist/compose/composer.d.ts +1 -1
  51. package/dist/compose/composer.js +8 -9
  52. package/dist/compose/resolve-block-map.js +24 -9
  53. package/dist/compose/resolve-block-scalar.js +12 -3
  54. package/dist/compose/resolve-block-seq.js +5 -0
  55. package/dist/compose/resolve-flow-collection.js +18 -6
  56. package/dist/compose/resolve-flow-scalar.js +2 -1
  57. package/dist/compose/resolve-props.d.ts +2 -1
  58. package/dist/compose/resolve-props.js +8 -0
  59. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  60. package/dist/compose/util-flow-indent-check.js +17 -0
  61. package/dist/doc/Document.d.ts +7 -4
  62. package/dist/doc/Document.js +56 -23
  63. package/dist/doc/anchors.d.ts +2 -2
  64. package/dist/doc/anchors.js +2 -2
  65. package/dist/doc/createNode.d.ts +1 -1
  66. package/dist/doc/createNode.js +7 -4
  67. package/dist/doc/directives.d.ts +4 -2
  68. package/dist/doc/directives.js +7 -4
  69. package/dist/index.d.ts +4 -4
  70. package/dist/index.js +1 -2
  71. package/dist/node_modules/tslib/tslib.es6.js +76 -0
  72. package/dist/nodes/Alias.d.ts +7 -3
  73. package/dist/nodes/Collection.d.ts +4 -4
  74. package/dist/nodes/Collection.js +8 -4
  75. package/dist/nodes/Node.d.ts +3 -0
  76. package/dist/nodes/Pair.d.ts +3 -0
  77. package/dist/nodes/Pair.js +2 -2
  78. package/dist/nodes/Scalar.d.ts +2 -0
  79. package/dist/nodes/Scalar.js +1 -1
  80. package/dist/nodes/YAMLMap.d.ts +5 -1
  81. package/dist/nodes/YAMLMap.js +6 -5
  82. package/dist/nodes/YAMLSeq.d.ts +2 -0
  83. package/dist/nodes/YAMLSeq.js +2 -2
  84. package/dist/nodes/addPairToJSMap.js +2 -1
  85. package/dist/nodes/toJS.js +3 -1
  86. package/dist/options.d.ts +60 -15
  87. package/dist/parse/cst-scalar.d.ts +6 -0
  88. package/dist/parse/cst-scalar.js +2 -6
  89. package/dist/parse/cst-visit.js +2 -2
  90. package/dist/parse/lexer.js +27 -11
  91. package/dist/parse/parser.js +135 -57
  92. package/dist/public-api.js +4 -5
  93. package/dist/schema/Schema.d.ts +5 -4
  94. package/dist/schema/Schema.js +13 -3
  95. package/dist/schema/common/null.js +3 -1
  96. package/dist/schema/core/float.js +4 -1
  97. package/dist/schema/tags.d.ts +1 -2
  98. package/dist/schema/tags.js +17 -13
  99. package/dist/schema/yaml-1.1/float.js +4 -1
  100. package/dist/schema/yaml-1.1/omap.js +1 -1
  101. package/dist/schema/yaml-1.1/pairs.js +2 -1
  102. package/dist/stringify/stringify.d.ts +5 -3
  103. package/dist/stringify/stringify.js +53 -23
  104. package/dist/stringify/stringifyCollection.d.ts +2 -6
  105. package/dist/stringify/stringifyCollection.js +114 -84
  106. package/dist/stringify/stringifyComment.d.ts +10 -2
  107. package/dist/stringify/stringifyComment.js +19 -12
  108. package/dist/stringify/stringifyDocument.js +36 -14
  109. package/dist/stringify/stringifyPair.js +34 -20
  110. package/dist/stringify/stringifyString.js +41 -43
  111. package/dist/test-events.d.ts +1 -1
  112. package/dist/test-events.js +13 -12
  113. package/dist/util.d.ts +3 -0
  114. package/dist/util.js +6 -0
  115. package/dist/visit.d.ts +49 -10
  116. package/dist/visit.js +149 -60
  117. package/package.json +10 -8
  118. package/util.d.ts +3 -0
  119. package/browser/dist/options.js +0 -17
  120. package/dist/options.js +0 -19
@@ -7,7 +7,7 @@ function includesToken(list, type) {
7
7
  return true;
8
8
  return false;
9
9
  }
10
- function includesNonEmpty(list) {
10
+ function findNonEmptyIndex(list) {
11
11
  for (let i = 0; i < list.length; ++i) {
12
12
  switch (list[i].type) {
13
13
  case 'space':
@@ -15,10 +15,10 @@ function includesNonEmpty(list) {
15
15
  case 'newline':
16
16
  break;
17
17
  default:
18
- return true;
18
+ return i;
19
19
  }
20
20
  }
21
- return false;
21
+ return -1;
22
22
  }
23
23
  function isFlowToken(token) {
24
24
  switch (token === null || token === void 0 ? void 0 : token.type) {
@@ -33,12 +33,13 @@ function isFlowToken(token) {
33
33
  }
34
34
  }
35
35
  function getPrevProps(parent) {
36
+ var _a;
36
37
  switch (parent.type) {
37
38
  case 'document':
38
39
  return parent.start;
39
40
  case 'block-map': {
40
41
  const it = parent.items[parent.items.length - 1];
41
- return it.sep || it.start;
42
+ return (_a = it.sep) !== null && _a !== void 0 ? _a : it.start;
42
43
  }
43
44
  case 'block-seq':
44
45
  return parent.items[parent.items.length - 1].start;
@@ -203,6 +204,7 @@ class Parser {
203
204
  this.indent += source.length;
204
205
  break;
205
206
  case 'doc-mode':
207
+ case 'flow-error-end':
206
208
  return;
207
209
  default:
208
210
  this.atNewLine = false;
@@ -264,7 +266,7 @@ class Parser {
264
266
  return this.stack[this.stack.length - n];
265
267
  }
266
268
  *pop(error) {
267
- const token = error || this.stack.pop();
269
+ const token = error !== null && error !== void 0 ? error : this.stack.pop();
268
270
  /* istanbul ignore if should not happen */
269
271
  if (!token) {
270
272
  const message = 'Tried to pop an empty stack';
@@ -275,9 +277,14 @@ class Parser {
275
277
  }
276
278
  else {
277
279
  const top = this.peek(1);
278
- // For these, parent indent is needed instead of own
279
- if (token.type === 'block-scalar' || token.type === 'flow-collection')
280
- token.indent = 'indent' in top ? top.indent : -1;
280
+ if (token.type === 'block-scalar') {
281
+ // Block scalars use their parent rather than header indent
282
+ token.indent = 'indent' in top ? top.indent : 0;
283
+ }
284
+ else if (token.type === 'flow-collection' && top.type === 'document') {
285
+ // Ignore all indent for top-level flow collections
286
+ token.indent = 0;
287
+ }
281
288
  if (token.type === 'flow-collection')
282
289
  fixFlowSeqItems(token);
283
290
  switch (top.type) {
@@ -336,7 +343,7 @@ class Parser {
336
343
  !last.sep &&
337
344
  !last.value &&
338
345
  last.start.length > 0 &&
339
- !includesNonEmpty(last.start) &&
346
+ findNonEmptyIndex(last.start) === -1 &&
340
347
  (token.indent === 0 ||
341
348
  last.start.every(st => st.type !== 'comment' || st.indent < token.indent))) {
342
349
  if (top.type === 'document')
@@ -384,7 +391,7 @@ class Parser {
384
391
  return yield* this.lineEnd(doc);
385
392
  switch (this.type) {
386
393
  case 'doc-start': {
387
- if (includesNonEmpty(doc.start)) {
394
+ if (findNonEmptyIndex(doc.start) !== -1) {
388
395
  yield* this.pop();
389
396
  yield* this.step();
390
397
  }
@@ -478,17 +485,21 @@ class Parser {
478
485
  else
479
486
  map.items.push({ start: [this.sourceToken] });
480
487
  }
481
- else if (it.sep)
488
+ else if (it.sep) {
482
489
  it.sep.push(this.sourceToken);
483
- else
490
+ }
491
+ else {
484
492
  it.start.push(this.sourceToken);
493
+ }
485
494
  return;
486
495
  case 'space':
487
496
  case 'comment':
488
- if (it.value)
497
+ if (it.value) {
489
498
  map.items.push({ start: [this.sourceToken] });
490
- else if (it.sep)
499
+ }
500
+ else if (it.sep) {
491
501
  it.sep.push(this.sourceToken);
502
+ }
492
503
  else {
493
504
  if (this.atIndentedComment(it.start, map.indent)) {
494
505
  const prev = map.items[map.items.length - 2];
@@ -505,66 +516,132 @@ class Parser {
505
516
  return;
506
517
  }
507
518
  if (this.indent >= map.indent) {
508
- const atNextItem = !this.onKeyLine &&
509
- this.indent === map.indent &&
510
- (it.sep || includesNonEmpty(it.start));
519
+ const atNextItem = !this.onKeyLine && this.indent === map.indent && it.sep;
520
+ // For empty nodes, assign newline-separated not indented empty tokens to following node
521
+ let start = [];
522
+ if (atNextItem && it.sep && !it.value) {
523
+ const nl = [];
524
+ for (let i = 0; i < it.sep.length; ++i) {
525
+ const st = it.sep[i];
526
+ switch (st.type) {
527
+ case 'newline':
528
+ nl.push(i);
529
+ break;
530
+ case 'space':
531
+ break;
532
+ case 'comment':
533
+ if (st.indent > map.indent)
534
+ nl.length = 0;
535
+ break;
536
+ default:
537
+ nl.length = 0;
538
+ }
539
+ }
540
+ if (nl.length >= 2)
541
+ start = it.sep.splice(nl[1]);
542
+ }
511
543
  switch (this.type) {
512
544
  case 'anchor':
513
545
  case 'tag':
514
546
  if (atNextItem || it.value) {
515
- map.items.push({ start: [this.sourceToken] });
547
+ start.push(this.sourceToken);
548
+ map.items.push({ start });
516
549
  this.onKeyLine = true;
517
550
  }
518
- else if (it.sep)
551
+ else if (it.sep) {
519
552
  it.sep.push(this.sourceToken);
520
- else
553
+ }
554
+ else {
521
555
  it.start.push(this.sourceToken);
556
+ }
522
557
  return;
523
558
  case 'explicit-key-ind':
524
- if (!it.sep && !includesToken(it.start, 'explicit-key-ind'))
559
+ if (!it.sep && !includesToken(it.start, 'explicit-key-ind')) {
525
560
  it.start.push(this.sourceToken);
526
- else if (atNextItem || it.value)
527
- map.items.push({ start: [this.sourceToken] });
528
- else
561
+ }
562
+ else if (atNextItem || it.value) {
563
+ start.push(this.sourceToken);
564
+ map.items.push({ start });
565
+ }
566
+ else {
529
567
  this.stack.push({
530
568
  type: 'block-map',
531
569
  offset: this.offset,
532
570
  indent: this.indent,
533
571
  items: [{ start: [this.sourceToken] }]
534
572
  });
573
+ }
535
574
  this.onKeyLine = true;
536
575
  return;
537
576
  case 'map-value-ind':
538
- if (!it.sep)
539
- Object.assign(it, { key: null, sep: [this.sourceToken] });
540
- else if (it.value ||
541
- (atNextItem && !includesToken(it.start, 'explicit-key-ind')))
542
- map.items.push({ start: [], key: null, sep: [this.sourceToken] });
543
- else if (includesToken(it.sep, 'map-value-ind'))
544
- this.stack.push({
545
- type: 'block-map',
546
- offset: this.offset,
547
- indent: this.indent,
548
- items: [{ start: [], key: null, sep: [this.sourceToken] }]
549
- });
550
- else if (includesToken(it.start, 'explicit-key-ind') &&
551
- isFlowToken(it.key) &&
552
- !includesToken(it.sep, 'newline')) {
553
- const start = getFirstKeyStartProps(it.start);
554
- const key = it.key;
555
- const sep = it.sep;
556
- sep.push(this.sourceToken);
557
- // @ts-ignore type guard is wrong here
558
- delete it.key, delete it.sep;
559
- this.stack.push({
560
- type: 'block-map',
561
- offset: this.offset,
562
- indent: this.indent,
563
- items: [{ start, key, sep }]
564
- });
577
+ if (includesToken(it.start, 'explicit-key-ind')) {
578
+ if (!it.sep) {
579
+ if (includesToken(it.start, 'newline')) {
580
+ Object.assign(it, { key: null, sep: [this.sourceToken] });
581
+ }
582
+ else {
583
+ const start = getFirstKeyStartProps(it.start);
584
+ this.stack.push({
585
+ type: 'block-map',
586
+ offset: this.offset,
587
+ indent: this.indent,
588
+ items: [{ start, key: null, sep: [this.sourceToken] }]
589
+ });
590
+ }
591
+ }
592
+ else if (it.value) {
593
+ map.items.push({ start: [], key: null, sep: [this.sourceToken] });
594
+ }
595
+ else if (includesToken(it.sep, 'map-value-ind')) {
596
+ this.stack.push({
597
+ type: 'block-map',
598
+ offset: this.offset,
599
+ indent: this.indent,
600
+ items: [{ start, key: null, sep: [this.sourceToken] }]
601
+ });
602
+ }
603
+ else if (isFlowToken(it.key) &&
604
+ !includesToken(it.sep, 'newline')) {
605
+ const start = getFirstKeyStartProps(it.start);
606
+ const key = it.key;
607
+ const sep = it.sep;
608
+ sep.push(this.sourceToken);
609
+ // @ts-expect-error type guard is wrong here
610
+ delete it.key, delete it.sep;
611
+ this.stack.push({
612
+ type: 'block-map',
613
+ offset: this.offset,
614
+ indent: this.indent,
615
+ items: [{ start, key, sep }]
616
+ });
617
+ }
618
+ else if (start.length > 0) {
619
+ // Not actually at next item
620
+ it.sep = it.sep.concat(start, this.sourceToken);
621
+ }
622
+ else {
623
+ it.sep.push(this.sourceToken);
624
+ }
625
+ }
626
+ else {
627
+ if (!it.sep) {
628
+ Object.assign(it, { key: null, sep: [this.sourceToken] });
629
+ }
630
+ else if (it.value || atNextItem) {
631
+ map.items.push({ start, key: null, sep: [this.sourceToken] });
632
+ }
633
+ else if (includesToken(it.sep, 'map-value-ind')) {
634
+ this.stack.push({
635
+ type: 'block-map',
636
+ offset: this.offset,
637
+ indent: this.indent,
638
+ items: [{ start: [], key: null, sep: [this.sourceToken] }]
639
+ });
640
+ }
641
+ else {
642
+ it.sep.push(this.sourceToken);
643
+ }
565
644
  }
566
- else
567
- it.sep.push(this.sourceToken);
568
645
  this.onKeyLine = true;
569
646
  return;
570
647
  case 'alias':
@@ -573,7 +650,7 @@ class Parser {
573
650
  case 'double-quoted-scalar': {
574
651
  const fs = this.flowScalar(this.type);
575
652
  if (atNextItem || it.value) {
576
- map.items.push({ start: [], key: fs, sep: [] });
653
+ map.items.push({ start, key: fs, sep: [] });
577
654
  this.onKeyLine = true;
578
655
  }
579
656
  else if (it.sep) {
@@ -590,8 +667,9 @@ class Parser {
590
667
  if (bv) {
591
668
  if (atNextItem &&
592
669
  bv.type !== 'block-seq' &&
593
- includesToken(it.start, 'explicit-key-ind'))
594
- map.items.push({ start: [] });
670
+ includesToken(it.start, 'explicit-key-ind')) {
671
+ map.items.push({ start });
672
+ }
595
673
  this.stack.push(bv);
596
674
  return;
597
675
  }
@@ -728,7 +806,7 @@ class Parser {
728
806
  else {
729
807
  const parent = this.peek(2);
730
808
  if (parent.type === 'block-map' &&
731
- (this.type === 'map-value-ind' ||
809
+ ((this.type === 'map-value-ind' && parent.indent === fc.indent) ||
732
810
  (this.type === 'newline' &&
733
811
  !parent.items[parent.items.length - 1].sep))) {
734
812
  yield* this.pop();
@@ -6,10 +6,8 @@ import { LineCounter } from './parse/line-counter.js';
6
6
  import { Parser } from './parse/parser.js';
7
7
 
8
8
  function parseOptions(options) {
9
- const prettyErrors = !options || options.prettyErrors !== false;
10
- const lineCounter = (options && options.lineCounter) ||
11
- (prettyErrors && new LineCounter()) ||
12
- null;
9
+ const prettyErrors = options.prettyErrors !== false;
10
+ const lineCounter = options.lineCounter || (prettyErrors && new LineCounter()) || null;
13
11
  return { lineCounter, prettyErrors };
14
12
  }
15
13
  /**
@@ -77,6 +75,7 @@ function parse(src, reviver, options) {
77
75
  return doc.toJS(Object.assign({ reviver: _reviver }, options));
78
76
  }
79
77
  function stringify(value, replacer, options) {
78
+ var _a;
80
79
  let _replacer = null;
81
80
  if (typeof replacer === 'function' || Array.isArray(replacer)) {
82
81
  _replacer = replacer;
@@ -91,7 +90,7 @@ function stringify(value, replacer, options) {
91
90
  options = indent < 1 ? undefined : indent > 8 ? { indent: 8 } : { indent };
92
91
  }
93
92
  if (value === undefined) {
94
- const { keepUndefined } = options || replacer || {};
93
+ const { keepUndefined } = (_a = options !== null && options !== void 0 ? options : replacer) !== null && _a !== void 0 ? _a : {};
95
94
  if (!keepUndefined)
96
95
  return undefined;
97
96
  }
@@ -2,21 +2,31 @@ import { MAP, SCALAR, SEQ } from '../nodes/Node.js';
2
2
  import { map } from './common/map.js';
3
3
  import { seq } from './common/seq.js';
4
4
  import { string } from './common/string.js';
5
- import { coreKnownTags, getTags } from './tags.js';
5
+ import { getTags, coreKnownTags } from './tags.js';
6
6
 
7
7
  const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
8
8
  class Schema {
9
- constructor({ customTags, merge, resolveKnownTags, schema, sortMapEntries }) {
9
+ constructor({ compat, customTags, merge, resolveKnownTags, schema, sortMapEntries, toStringDefaults }) {
10
+ this.compat = Array.isArray(compat)
11
+ ? getTags(compat, 'compat')
12
+ : compat
13
+ ? getTags(null, compat)
14
+ : null;
10
15
  this.merge = !!merge;
11
- this.name = schema || 'core';
16
+ this.name = (typeof schema === 'string' && schema) || 'core';
12
17
  this.knownTags = resolveKnownTags ? coreKnownTags : {};
13
18
  this.tags = getTags(customTags, this.name);
19
+ this.toStringOptions = toStringDefaults !== null && toStringDefaults !== void 0 ? toStringDefaults : null;
14
20
  Object.defineProperty(this, MAP, { value: map });
15
21
  Object.defineProperty(this, SCALAR, { value: string });
16
22
  Object.defineProperty(this, SEQ, { value: seq });
17
23
  // Used by createMap()
18
24
  this.sortMapEntries =
19
- sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null;
25
+ typeof sortMapEntries === 'function'
26
+ ? sortMapEntries
27
+ : sortMapEntries === true
28
+ ? sortMapEntriesByKey
29
+ : null;
20
30
  }
21
31
  clone() {
22
32
  const copy = Object.create(Schema.prototype, Object.getOwnPropertyDescriptors(this));
@@ -7,7 +7,9 @@ const nullTag = {
7
7
  tag: 'tag:yaml.org,2002:null',
8
8
  test: /^(?:~|[Nn]ull|NULL)?$/,
9
9
  resolve: () => new Scalar(null),
10
- stringify: ({ source }, ctx) => source && nullTag.test.test(source) ? source : ctx.options.nullStr
10
+ stringify: ({ source }, ctx) => typeof source === 'string' && nullTag.test.test(source)
11
+ ? source
12
+ : ctx.options.nullStr
11
13
  };
12
14
 
13
15
  export { nullTag };
@@ -20,7 +20,10 @@ const floatExp = {
20
20
  format: 'EXP',
21
21
  test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
22
22
  resolve: str => parseFloat(str),
23
- stringify: ({ value }) => Number(value).toExponential()
23
+ stringify(node) {
24
+ const num = Number(node.value);
25
+ return isFinite(num) ? num.toExponential() : stringifyNumber(node);
26
+ }
24
27
  };
25
28
  const float = {
26
29
  identify: value => typeof value === 'number',
@@ -14,13 +14,13 @@ import { schema as schema$2 } from './yaml-1.1/schema.js';
14
14
  import { set } from './yaml-1.1/set.js';
15
15
  import { floatTime, intTime, timestamp } from './yaml-1.1/timestamp.js';
16
16
 
17
- const schemas = {
18
- core: schema,
19
- failsafe: [map, seq, string],
20
- json: schema$1,
21
- yaml11: schema$2,
22
- 'yaml-1.1': schema$2
23
- };
17
+ const schemas = new Map([
18
+ ['core', schema],
19
+ ['failsafe', [map, seq, string]],
20
+ ['json', schema$1],
21
+ ['yaml11', schema$2],
22
+ ['yaml-1.1', schema$2]
23
+ ]);
24
24
  const tagsByName = {
25
25
  binary,
26
26
  bool: boolTag,
@@ -48,13 +48,17 @@ const coreKnownTags = {
48
48
  'tag:yaml.org,2002:timestamp': timestamp
49
49
  };
50
50
  function getTags(customTags, schemaName) {
51
- let tags = schemas[schemaName];
51
+ let tags = schemas.get(schemaName);
52
52
  if (!tags) {
53
- const keys = Object.keys(schemas)
54
- .filter(key => key !== 'yaml11')
55
- .map(key => JSON.stringify(key))
56
- .join(', ');
57
- throw new Error(`Unknown schema "${schemaName}"; use one of ${keys}`);
53
+ if (Array.isArray(customTags))
54
+ tags = [];
55
+ else {
56
+ const keys = Array.from(schemas.keys())
57
+ .filter(key => key !== 'yaml11')
58
+ .map(key => JSON.stringify(key))
59
+ .join(', ');
60
+ throw new Error(`Unknown schema "${schemaName}"; use one of ${keys} or define customTags array`);
61
+ }
58
62
  }
59
63
  if (Array.isArray(customTags)) {
60
64
  for (const tag of customTags)
@@ -20,7 +20,10 @@ const floatExp = {
20
20
  format: 'EXP',
21
21
  test: /^[-+]?(?:[0-9][0-9_]*)?(?:\.[0-9_]*)?[eE][-+]?[0-9]+$/,
22
22
  resolve: (str) => parseFloat(str.replace(/_/g, '')),
23
- stringify: ({ value }) => Number(value).toExponential()
23
+ stringify(node) {
24
+ const num = Number(node.value);
25
+ return isFinite(num) ? num.toExponential() : stringifyNumber(node);
26
+ }
24
27
  };
25
28
  const float = {
26
29
  identify: value => typeof value === 'number',
@@ -22,7 +22,7 @@ class YAMLOMap extends YAMLSeq {
22
22
  if (!ctx)
23
23
  return super.toJSON(_);
24
24
  const map = new Map();
25
- if (ctx && ctx.onCreate)
25
+ if (ctx === null || ctx === void 0 ? void 0 : ctx.onCreate)
26
26
  ctx.onCreate(map);
27
27
  for (const pair of this.items) {
28
28
  let key, value;
@@ -4,6 +4,7 @@ import { Scalar } from '../../nodes/Scalar.js';
4
4
  import { YAMLSeq } from '../../nodes/YAMLSeq.js';
5
5
 
6
6
  function resolvePairs(seq, onError) {
7
+ var _a;
7
8
  if (isSeq(seq)) {
8
9
  for (let i = 0; i < seq.items.length; ++i) {
9
10
  let item = seq.items[i];
@@ -18,7 +19,7 @@ function resolvePairs(seq, onError) {
18
19
  ? `${item.commentBefore}\n${pair.key.commentBefore}`
19
20
  : item.commentBefore;
20
21
  if (item.comment) {
21
- const cn = pair.value || pair.key;
22
+ const cn = (_a = pair.value) !== null && _a !== void 0 ? _a : pair.key;
22
23
  cn.comment = cn.comment
23
24
  ? `${item.comment}\n${cn.comment}`
24
25
  : item.comment;
@@ -1,13 +1,12 @@
1
1
  import { anchorIsValid } from '../doc/anchors.js';
2
2
  import { isPair, isAlias, isNode, isScalar, isCollection } from '../nodes/Node.js';
3
+ import { stringifyComment } from './stringifyComment.js';
3
4
  import { stringifyString } from './stringifyString.js';
4
5
 
5
- const createStringifyContext = (doc, options) => ({
6
- anchors: new Set(),
7
- doc,
8
- indent: '',
9
- indentStep: typeof options.indent === 'number' ? ' '.repeat(options.indent) : ' ',
10
- options: Object.assign({
6
+ function createStringifyContext(doc, options) {
7
+ const opt = Object.assign({
8
+ blockQuote: true,
9
+ commentString: stringifyComment,
11
10
  defaultKeyType: null,
12
11
  defaultStringType: 'PLAIN',
13
12
  directives: null,
@@ -19,57 +18,88 @@ const createStringifyContext = (doc, options) => ({
19
18
  minContentWidth: 20,
20
19
  nullStr: 'null',
21
20
  simpleKeys: false,
22
- singleQuote: false,
21
+ singleQuote: null,
23
22
  trueStr: 'true',
24
23
  verifyAliasOrder: true
25
- }, options)
26
- });
24
+ }, doc.schema.toStringOptions, options);
25
+ let inFlow;
26
+ switch (opt.collectionStyle) {
27
+ case 'block':
28
+ inFlow = false;
29
+ break;
30
+ case 'flow':
31
+ inFlow = true;
32
+ break;
33
+ default:
34
+ inFlow = null;
35
+ }
36
+ return {
37
+ anchors: new Set(),
38
+ doc,
39
+ indent: '',
40
+ indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ',
41
+ inFlow,
42
+ options: opt
43
+ };
44
+ }
27
45
  function getTagObject(tags, item) {
46
+ var _a, _b, _c, _d;
28
47
  if (item.tag) {
29
48
  const match = tags.filter(t => t.tag === item.tag);
30
49
  if (match.length > 0)
31
- return match.find(t => t.format === item.format) || match[0];
50
+ return (_a = match.find(t => t.format === item.format)) !== null && _a !== void 0 ? _a : match[0];
32
51
  }
33
52
  let tagObj = undefined;
34
53
  let obj;
35
54
  if (isScalar(item)) {
36
55
  obj = item.value;
37
- const match = tags.filter(t => t.identify && t.identify(obj));
56
+ const match = tags.filter(t => { var _a; return (_a = t.identify) === null || _a === void 0 ? void 0 : _a.call(t, obj); });
38
57
  tagObj =
39
- match.find(t => t.format === item.format) || match.find(t => !t.format);
58
+ (_b = match.find(t => t.format === item.format)) !== null && _b !== void 0 ? _b : match.find(t => !t.format);
40
59
  }
41
60
  else {
42
61
  obj = item;
43
62
  tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
44
63
  }
45
64
  if (!tagObj) {
46
- // @ts-ignore
47
- const name = obj && obj.constructor ? obj.constructor.name : typeof obj;
65
+ const name = (_d = (_c = obj === null || obj === void 0 ? void 0 : obj.constructor) === null || _c === void 0 ? void 0 : _c.name) !== null && _d !== void 0 ? _d : typeof obj;
48
66
  throw new Error(`Tag not resolved for ${name} value`);
49
67
  }
50
68
  return tagObj;
51
69
  }
52
70
  // needs to be called before value stringifier to allow for circular anchor refs
53
71
  function stringifyProps(node, tagObj, { anchors, doc }) {
72
+ if (!doc.directives)
73
+ return '';
54
74
  const props = [];
55
75
  const anchor = (isScalar(node) || isCollection(node)) && node.anchor;
56
76
  if (anchor && anchorIsValid(anchor)) {
57
77
  anchors.add(anchor);
58
78
  props.push(`&${anchor}`);
59
79
  }
60
- if (node.tag) {
61
- props.push(doc.directives.tagString(node.tag));
62
- }
63
- else if (!tagObj.default) {
64
- props.push(doc.directives.tagString(tagObj.tag));
65
- }
80
+ const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
81
+ if (tag)
82
+ props.push(doc.directives.tagString(tag));
66
83
  return props.join(' ');
67
84
  }
68
85
  function stringify(item, ctx, onComment, onChompKeep) {
86
+ var _a, _b;
69
87
  if (isPair(item))
70
88
  return item.toString(ctx, onComment, onChompKeep);
71
- if (isAlias(item))
72
- return item.toString(ctx);
89
+ if (isAlias(item)) {
90
+ if (ctx.doc.directives)
91
+ return item.toString(ctx);
92
+ if ((_a = ctx.resolvedAliases) === null || _a === void 0 ? void 0 : _a.has(item)) {
93
+ throw new TypeError(`Cannot stringify circular structure without alias nodes`);
94
+ }
95
+ else {
96
+ if (ctx.resolvedAliases)
97
+ ctx.resolvedAliases.add(item);
98
+ else
99
+ ctx.resolvedAliases = new Set([item]);
100
+ item = item.resolve(ctx.doc);
101
+ }
102
+ }
73
103
  let tagObj = undefined;
74
104
  const node = isNode(item)
75
105
  ? item
@@ -78,7 +108,7 @@ function stringify(item, ctx, onComment, onChompKeep) {
78
108
  tagObj = getTagObject(ctx.doc.schema.tags, node);
79
109
  const props = stringifyProps(node, tagObj, ctx);
80
110
  if (props.length > 0)
81
- ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1;
111
+ ctx.indentAtStart = ((_b = ctx.indentAtStart) !== null && _b !== void 0 ? _b : 0) + props.length + 1;
82
112
  const str = typeof tagObj.stringify === 'function'
83
113
  ? tagObj.stringify(node, ctx, onComment, onChompKeep)
84
114
  : isScalar(node)