yaml 2.0.0-7 → 2.0.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 (123) 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 +17 -10
  5. package/browser/dist/compose/composer.js +6 -7
  6. package/browser/dist/compose/resolve-block-map.js +19 -5
  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 +9 -2
  11. package/browser/dist/compose/resolve-props.js +4 -0
  12. package/browser/dist/compose/util-flow-indent-check.js +15 -0
  13. package/browser/dist/doc/Document.js +73 -16
  14. package/browser/dist/doc/anchors.js +2 -2
  15. package/browser/dist/doc/createNode.js +12 -7
  16. package/browser/dist/doc/directives.js +11 -3
  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 +28 -15
  20. package/browser/dist/nodes/Node.js +7 -0
  21. package/browser/dist/nodes/Pair.js +11 -3
  22. package/browser/dist/nodes/Scalar.js +1 -1
  23. package/browser/dist/nodes/YAMLMap.js +6 -5
  24. package/browser/dist/nodes/YAMLSeq.js +2 -2
  25. package/browser/dist/nodes/addPairToJSMap.js +3 -2
  26. package/browser/dist/nodes/toJS.js +3 -1
  27. package/browser/dist/parse/cst-scalar.js +2 -6
  28. package/browser/dist/parse/cst-visit.js +2 -2
  29. package/browser/dist/parse/lexer.js +50 -22
  30. package/browser/dist/parse/parser.js +121 -45
  31. package/browser/dist/public-api.js +4 -5
  32. package/browser/dist/schema/Schema.js +19 -4
  33. package/browser/dist/schema/common/null.js +3 -1
  34. package/browser/dist/schema/core/float.js +4 -1
  35. package/browser/dist/schema/tags.js +17 -13
  36. package/browser/dist/schema/yaml-1.1/float.js +4 -1
  37. package/browser/dist/schema/yaml-1.1/omap.js +1 -1
  38. package/browser/dist/schema/yaml-1.1/pairs.js +2 -1
  39. package/browser/dist/stringify/stringify.js +53 -23
  40. package/browser/dist/stringify/stringifyCollection.js +115 -85
  41. package/browser/dist/stringify/stringifyComment.js +18 -12
  42. package/browser/dist/stringify/stringifyDocument.js +37 -15
  43. package/browser/dist/stringify/stringifyPair.js +35 -21
  44. package/browser/dist/stringify/stringifyString.js +41 -43
  45. package/browser/dist/util.js +3 -0
  46. package/browser/dist/visit.js +150 -62
  47. package/dist/compose/compose-doc.js +3 -2
  48. package/dist/compose/compose-node.d.ts +1 -0
  49. package/dist/compose/compose-node.js +14 -3
  50. package/dist/compose/compose-scalar.js +16 -9
  51. package/dist/compose/composer.d.ts +1 -1
  52. package/dist/compose/composer.js +8 -9
  53. package/dist/compose/resolve-block-map.js +19 -5
  54. package/dist/compose/resolve-block-scalar.js +12 -3
  55. package/dist/compose/resolve-block-seq.js +5 -0
  56. package/dist/compose/resolve-flow-collection.js +18 -6
  57. package/dist/compose/resolve-flow-scalar.js +9 -2
  58. package/dist/compose/resolve-props.d.ts +1 -1
  59. package/dist/compose/resolve-props.js +4 -0
  60. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  61. package/dist/compose/util-flow-indent-check.js +17 -0
  62. package/dist/doc/Document.d.ts +13 -4
  63. package/dist/doc/Document.js +79 -22
  64. package/dist/doc/anchors.d.ts +2 -2
  65. package/dist/doc/anchors.js +2 -2
  66. package/dist/doc/createNode.d.ts +3 -2
  67. package/dist/doc/createNode.js +11 -6
  68. package/dist/doc/directives.d.ts +5 -2
  69. package/dist/doc/directives.js +11 -3
  70. package/dist/index.d.ts +4 -4
  71. package/dist/index.js +1 -2
  72. package/dist/node_modules/tslib/tslib.es6.js +76 -0
  73. package/dist/nodes/Alias.d.ts +7 -3
  74. package/dist/nodes/Collection.d.ts +10 -4
  75. package/dist/nodes/Collection.js +27 -14
  76. package/dist/nodes/Node.d.ts +6 -1
  77. package/dist/nodes/Node.js +7 -0
  78. package/dist/nodes/Pair.d.ts +7 -2
  79. package/dist/nodes/Pair.js +10 -2
  80. package/dist/nodes/Scalar.d.ts +2 -0
  81. package/dist/nodes/Scalar.js +1 -1
  82. package/dist/nodes/YAMLMap.d.ts +5 -1
  83. package/dist/nodes/YAMLMap.js +6 -5
  84. package/dist/nodes/YAMLSeq.d.ts +2 -0
  85. package/dist/nodes/YAMLSeq.js +2 -2
  86. package/dist/nodes/addPairToJSMap.js +2 -1
  87. package/dist/nodes/toJS.js +3 -1
  88. package/dist/options.d.ts +67 -15
  89. package/dist/parse/cst-scalar.d.ts +6 -0
  90. package/dist/parse/cst-scalar.js +2 -6
  91. package/dist/parse/cst-visit.js +2 -2
  92. package/dist/parse/lexer.d.ts +2 -0
  93. package/dist/parse/lexer.js +50 -22
  94. package/dist/parse/parser.js +121 -45
  95. package/dist/public-api.js +4 -5
  96. package/dist/schema/Schema.d.ts +6 -4
  97. package/dist/schema/Schema.js +18 -3
  98. package/dist/schema/common/null.js +3 -1
  99. package/dist/schema/core/float.js +4 -1
  100. package/dist/schema/tags.d.ts +1 -2
  101. package/dist/schema/tags.js +17 -13
  102. package/dist/schema/yaml-1.1/float.js +4 -1
  103. package/dist/schema/yaml-1.1/omap.js +1 -1
  104. package/dist/schema/yaml-1.1/pairs.js +2 -1
  105. package/dist/stringify/stringify.d.ts +5 -3
  106. package/dist/stringify/stringify.js +53 -23
  107. package/dist/stringify/stringifyCollection.d.ts +2 -6
  108. package/dist/stringify/stringifyCollection.js +114 -84
  109. package/dist/stringify/stringifyComment.d.ts +10 -2
  110. package/dist/stringify/stringifyComment.js +19 -12
  111. package/dist/stringify/stringifyDocument.js +36 -14
  112. package/dist/stringify/stringifyPair.js +34 -20
  113. package/dist/stringify/stringifyString.js +41 -43
  114. package/dist/test-events.d.ts +1 -1
  115. package/dist/test-events.js +13 -12
  116. package/dist/util.d.ts +3 -0
  117. package/dist/util.js +6 -0
  118. package/dist/visit.d.ts +49 -10
  119. package/dist/visit.js +149 -60
  120. package/package.json +12 -10
  121. package/util.d.ts +3 -0
  122. package/browser/dist/options.js +0 -17
  123. package/dist/options.js +0 -19
@@ -134,6 +134,8 @@ class Lexer {
134
134
  this.indentNext = 0;
135
135
  /** Indentation level of the current line. */
136
136
  this.indentValue = 0;
137
+ /** Position of the next \n character. */
138
+ this.lineEndPos = null;
137
139
  /** Stores the state of the lexer if reaching the end of incpomplete input */
138
140
  this.next = null;
139
141
  /** A pointer to `buffer`; the current position of the lexer. */
@@ -146,10 +148,13 @@ class Lexer {
146
148
  * @returns A generator of lexical tokens
147
149
  */
148
150
  *lex(source, incomplete = false) {
149
- if (source)
151
+ var _a;
152
+ if (source) {
150
153
  this.buffer = this.buffer ? this.buffer + source : source;
154
+ this.lineEndPos = null;
155
+ }
151
156
  this.atEnd = !incomplete;
152
- let next = this.next || 'stream';
157
+ let next = (_a = this.next) !== null && _a !== void 0 ? _a : 'stream';
153
158
  while (next && (incomplete || this.hasChars(1)))
154
159
  next = yield* this.parseNext(next);
155
160
  }
@@ -190,7 +195,11 @@ class Lexer {
190
195
  return offset;
191
196
  }
192
197
  getLine() {
193
- let end = this.buffer.indexOf('\n', this.pos);
198
+ let end = this.lineEndPos;
199
+ if (typeof end !== 'number' || (end !== -1 && end < this.pos)) {
200
+ end = this.buffer.indexOf('\n', this.pos);
201
+ this.lineEndPos = end;
202
+ }
194
203
  if (end === -1)
195
204
  return this.atEnd ? this.buffer.substring(this.pos) : null;
196
205
  if (this.buffer[end - 1] === '\r')
@@ -203,6 +212,7 @@ class Lexer {
203
212
  setNext(state) {
204
213
  this.buffer = this.buffer.substring(this.pos);
205
214
  this.pos = 0;
215
+ this.lineEndPos = null;
206
216
  this.next = state;
207
217
  return null;
208
218
  }
@@ -348,14 +358,19 @@ class Lexer {
348
358
  let indent = -1;
349
359
  do {
350
360
  nl = yield* this.pushNewline();
351
- sp = yield* this.pushSpaces(true);
352
- if (nl > 0)
361
+ if (nl > 0) {
362
+ sp = yield* this.pushSpaces(false);
353
363
  this.indentValue = indent = sp;
364
+ }
365
+ else {
366
+ sp = 0;
367
+ }
368
+ sp += yield* this.pushSpaces(true);
354
369
  } while (nl + sp > 0);
355
370
  const line = this.getLine();
356
371
  if (line === null)
357
372
  return this.setNext('flow');
358
- if ((indent !== -1 && indent < this.indentNext) ||
373
+ if ((indent !== -1 && indent < this.indentNext && line[0] !== '#') ||
359
374
  (indent === 0 &&
360
375
  (line.startsWith('---') || line.startsWith('...')) &&
361
376
  isEmpty(line[3]))) {
@@ -373,8 +388,11 @@ class Lexer {
373
388
  }
374
389
  }
375
390
  let n = 0;
376
- while (line[n] === ',')
377
- n += (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
391
+ while (line[n] === ',') {
392
+ n += yield* this.pushCount(1);
393
+ n += yield* this.pushSpaces(true);
394
+ this.flowKey = false;
395
+ }
378
396
  n += yield* this.pushIndicators();
379
397
  switch (line[n]) {
380
398
  case undefined:
@@ -404,6 +422,7 @@ class Lexer {
404
422
  case ':': {
405
423
  const next = this.charAt(1);
406
424
  if (this.flowKey || isEmpty(next) || next === ',') {
425
+ this.flowKey = false;
407
426
  yield* this.pushCount(1);
408
427
  yield* this.pushSpaces(true);
409
428
  return 'flow';
@@ -433,17 +452,19 @@ class Lexer {
433
452
  end = this.buffer.indexOf('"', end + 1);
434
453
  }
435
454
  }
436
- let nl = this.buffer.indexOf('\n', this.pos);
437
- if (nl !== -1 && nl < end) {
438
- while (nl !== -1 && nl < end) {
455
+ // Only looking for newlines within the quotes
456
+ const qb = this.buffer.substring(0, end);
457
+ let nl = qb.indexOf('\n', this.pos);
458
+ if (nl !== -1) {
459
+ while (nl !== -1) {
439
460
  const cs = this.continueScalar(nl + 1);
440
461
  if (cs === -1)
441
462
  break;
442
- nl = this.buffer.indexOf('\n', cs);
463
+ nl = qb.indexOf('\n', cs);
443
464
  }
444
- if (nl !== -1 && nl < end) {
465
+ if (nl !== -1) {
445
466
  // this is an error caused by an unexpected unindent
446
- end = nl - 1;
467
+ end = nl - (qb[nl - 1] === '\r' ? 2 : 1);
447
468
  }
448
469
  }
449
470
  if (end === -1) {
@@ -518,9 +539,10 @@ class Lexer {
518
539
  let ch = this.buffer[i];
519
540
  if (ch === '\r')
520
541
  ch = this.buffer[--i];
542
+ const lastChar = i; // Drop the line if last char not more indented
521
543
  while (ch === ' ' || ch === '\t')
522
544
  ch = this.buffer[--i];
523
- if (ch === '\n' && i >= this.pos)
545
+ if (ch === '\n' && i >= this.pos && i + 1 + indent > lastChar)
524
546
  nl = i;
525
547
  else
526
548
  break;
@@ -543,17 +565,18 @@ class Lexer {
543
565
  end = i;
544
566
  }
545
567
  else if (isEmpty(ch)) {
546
- const next = this.buffer[i + 1];
547
- if (next === '#' || (inFlow && invalidFlowScalarChars.includes(next)))
548
- break;
568
+ let next = this.buffer[i + 1];
549
569
  if (ch === '\r') {
550
570
  if (next === '\n') {
551
571
  i += 1;
552
572
  ch = '\n';
573
+ next = this.buffer[i + 1];
553
574
  }
554
575
  else
555
576
  end = i;
556
577
  }
578
+ if (next === '#' || (inFlow && invalidFlowScalarChars.includes(next)))
579
+ break;
557
580
  if (ch === '\n') {
558
581
  const cs = this.continueScalar(i + 1);
559
582
  if (cs === -1)
@@ -602,16 +625,21 @@ class Lexer {
602
625
  return ((yield* this.pushUntil(isNotAnchorChar)) +
603
626
  (yield* this.pushSpaces(true)) +
604
627
  (yield* this.pushIndicators()));
605
- case ':':
606
- case '?': // this is an error outside flow collections
607
628
  case '-': // this is an error
608
- if (isEmpty(this.charAt(1))) {
609
- if (this.flowLevel === 0)
629
+ case '?': // this is an error outside flow collections
630
+ case ':': {
631
+ const inFlow = this.flowLevel > 0;
632
+ const ch1 = this.charAt(1);
633
+ if (isEmpty(ch1) || (inFlow && invalidFlowScalarChars.includes(ch1))) {
634
+ if (!inFlow)
610
635
  this.indentNext = this.indentValue + 1;
636
+ else if (this.flowKey)
637
+ this.flowKey = false;
611
638
  return ((yield* this.pushCount(1)) +
612
639
  (yield* this.pushSpaces(true)) +
613
640
  (yield* this.pushIndicators()));
614
641
  }
642
+ }
615
643
  }
616
644
  return 0;
617
645
  }
@@ -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) {
@@ -508,63 +515,131 @@ class Parser {
508
515
  const atNextItem = !this.onKeyLine &&
509
516
  this.indent === map.indent &&
510
517
  (it.sep || includesNonEmpty(it.start));
518
+ // For empty nodes, assign newline-separated not indented empty tokens to following node
519
+ let start = [];
520
+ if (atNextItem && it.sep && !it.value) {
521
+ const nl = [];
522
+ for (let i = 0; i < it.sep.length; ++i) {
523
+ const st = it.sep[i];
524
+ switch (st.type) {
525
+ case 'newline':
526
+ nl.push(i);
527
+ break;
528
+ case 'space':
529
+ break;
530
+ case 'comment':
531
+ if (st.indent > map.indent)
532
+ nl.length = 0;
533
+ break;
534
+ default:
535
+ nl.length = 0;
536
+ }
537
+ }
538
+ if (nl.length >= 2)
539
+ start = it.sep.splice(nl[1]);
540
+ }
511
541
  switch (this.type) {
512
542
  case 'anchor':
513
543
  case 'tag':
514
544
  if (atNextItem || it.value) {
515
- map.items.push({ start: [this.sourceToken] });
545
+ start.push(this.sourceToken);
546
+ map.items.push({ start });
516
547
  this.onKeyLine = true;
517
548
  }
518
- else if (it.sep)
549
+ else if (it.sep) {
519
550
  it.sep.push(this.sourceToken);
520
- else
551
+ }
552
+ else {
521
553
  it.start.push(this.sourceToken);
554
+ }
522
555
  return;
523
556
  case 'explicit-key-ind':
524
- if (!it.sep && !includesToken(it.start, 'explicit-key-ind'))
557
+ if (!it.sep && !includesToken(it.start, 'explicit-key-ind')) {
525
558
  it.start.push(this.sourceToken);
526
- else if (atNextItem || it.value)
527
- map.items.push({ start: [this.sourceToken] });
528
- else
559
+ }
560
+ else if (atNextItem || it.value) {
561
+ start.push(this.sourceToken);
562
+ map.items.push({ start });
563
+ }
564
+ else {
529
565
  this.stack.push({
530
566
  type: 'block-map',
531
567
  offset: this.offset,
532
568
  indent: this.indent,
533
569
  items: [{ start: [this.sourceToken] }]
534
570
  });
571
+ }
535
572
  this.onKeyLine = true;
536
573
  return;
537
574
  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
- });
575
+ if (includesToken(it.start, 'explicit-key-ind')) {
576
+ if (!it.sep) {
577
+ if (includesToken(it.start, 'newline')) {
578
+ Object.assign(it, { key: null, sep: [this.sourceToken] });
579
+ }
580
+ else {
581
+ const start = getFirstKeyStartProps(it.start);
582
+ this.stack.push({
583
+ type: 'block-map',
584
+ offset: this.offset,
585
+ indent: this.indent,
586
+ items: [{ start, key: null, sep: [this.sourceToken] }]
587
+ });
588
+ }
589
+ }
590
+ else if (it.value) {
591
+ map.items.push({ start: [], key: null, sep: [this.sourceToken] });
592
+ }
593
+ else if (includesToken(it.sep, 'map-value-ind')) {
594
+ this.stack.push({
595
+ type: 'block-map',
596
+ offset: this.offset,
597
+ indent: this.indent,
598
+ items: [{ start, key: null, sep: [this.sourceToken] }]
599
+ });
600
+ }
601
+ else if (isFlowToken(it.key) &&
602
+ !includesToken(it.sep, 'newline')) {
603
+ const start = getFirstKeyStartProps(it.start);
604
+ const key = it.key;
605
+ const sep = it.sep;
606
+ sep.push(this.sourceToken);
607
+ // @ts-expect-error type guard is wrong here
608
+ delete it.key, delete it.sep;
609
+ this.stack.push({
610
+ type: 'block-map',
611
+ offset: this.offset,
612
+ indent: this.indent,
613
+ items: [{ start, key, sep }]
614
+ });
615
+ }
616
+ else if (start.length > 0) {
617
+ // Not actually at next item
618
+ it.sep = it.sep.concat(start, this.sourceToken);
619
+ }
620
+ else {
621
+ it.sep.push(this.sourceToken);
622
+ }
623
+ }
624
+ else {
625
+ if (!it.sep) {
626
+ Object.assign(it, { key: null, sep: [this.sourceToken] });
627
+ }
628
+ else if (it.value || atNextItem) {
629
+ map.items.push({ start, key: null, sep: [this.sourceToken] });
630
+ }
631
+ else if (includesToken(it.sep, 'map-value-ind')) {
632
+ this.stack.push({
633
+ type: 'block-map',
634
+ offset: this.offset,
635
+ indent: this.indent,
636
+ items: [{ start: [], key: null, sep: [this.sourceToken] }]
637
+ });
638
+ }
639
+ else {
640
+ it.sep.push(this.sourceToken);
641
+ }
565
642
  }
566
- else
567
- it.sep.push(this.sourceToken);
568
643
  this.onKeyLine = true;
569
644
  return;
570
645
  case 'alias':
@@ -573,7 +648,7 @@ class Parser {
573
648
  case 'double-quoted-scalar': {
574
649
  const fs = this.flowScalar(this.type);
575
650
  if (atNextItem || it.value) {
576
- map.items.push({ start: [], key: fs, sep: [] });
651
+ map.items.push({ start, key: fs, sep: [] });
577
652
  this.onKeyLine = true;
578
653
  }
579
654
  else if (it.sep) {
@@ -590,8 +665,9 @@ class Parser {
590
665
  if (bv) {
591
666
  if (atNextItem &&
592
667
  bv.type !== 'block-seq' &&
593
- includesToken(it.start, 'explicit-key-ind'))
594
- map.items.push({ start: [] });
668
+ includesToken(it.start, 'explicit-key-ind')) {
669
+ map.items.push({ start });
670
+ }
595
671
  this.stack.push(bv);
596
672
  return;
597
673
  }
@@ -728,7 +804,7 @@ class Parser {
728
804
  else {
729
805
  const parent = this.peek(2);
730
806
  if (parent.type === 'block-map' &&
731
- (this.type === 'map-value-ind' ||
807
+ ((this.type === 'map-value-ind' && parent.indent === fc.indent) ||
732
808
  (this.type === 'newline' &&
733
809
  !parent.items[parent.items.length - 1].sep))) {
734
810
  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,36 @@ 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;
30
+ }
31
+ clone() {
32
+ const copy = Object.create(Schema.prototype, Object.getOwnPropertyDescriptors(this));
33
+ copy.tags = this.tags.slice();
34
+ return copy;
20
35
  }
21
36
  }
22
37
 
@@ -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;