yaml 2.0.0-5 → 2.0.0-9

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 (94) hide show
  1. package/browser/dist/compose/compose-doc.js +1 -1
  2. package/browser/dist/compose/compose-node.js +10 -1
  3. package/browser/dist/compose/compose-scalar.js +2 -1
  4. package/browser/dist/compose/composer.js +3 -2
  5. package/browser/dist/compose/resolve-block-map.js +15 -5
  6. package/browser/dist/compose/resolve-block-scalar.js +2 -2
  7. package/browser/dist/compose/resolve-block-seq.js +1 -1
  8. package/browser/dist/compose/resolve-end.js +2 -2
  9. package/browser/dist/compose/resolve-flow-collection.js +14 -6
  10. package/browser/dist/compose/resolve-flow-scalar.js +19 -7
  11. package/browser/dist/compose/resolve-props.js +30 -7
  12. package/browser/dist/compose/util-map-includes.js +17 -0
  13. package/browser/dist/doc/Document.js +26 -2
  14. package/browser/dist/doc/createNode.js +11 -5
  15. package/browser/dist/doc/directives.js +5 -0
  16. package/browser/dist/node_modules/tslib/tslib.es6.js +164 -0
  17. package/browser/dist/nodes/Collection.js +28 -15
  18. package/browser/dist/nodes/Node.js +7 -0
  19. package/browser/dist/nodes/Pair.js +9 -1
  20. package/browser/dist/nodes/addPairToJSMap.js +4 -3
  21. package/browser/dist/options.js +2 -0
  22. package/browser/dist/parse/cst-scalar.js +2 -6
  23. package/browser/dist/parse/lexer.js +64 -31
  24. package/browser/dist/parse/parser.js +53 -52
  25. package/browser/dist/schema/Schema.js +5 -0
  26. package/browser/dist/schema/tags.js +9 -5
  27. package/browser/dist/stringify/stringify.js +2 -1
  28. package/browser/dist/stringify/stringifyCollection.js +23 -9
  29. package/browser/dist/stringify/stringifyComment.js +14 -0
  30. package/browser/dist/stringify/stringifyDocument.js +8 -5
  31. package/browser/dist/stringify/stringifyPair.js +5 -7
  32. package/browser/dist/stringify/stringifyString.js +35 -34
  33. package/browser/dist/util.js +3 -0
  34. package/dist/compose/compose-doc.js +1 -1
  35. package/dist/compose/compose-node.js +10 -1
  36. package/dist/compose/compose-scalar.js +2 -1
  37. package/dist/compose/composer.js +3 -2
  38. package/dist/compose/resolve-block-map.d.ts +2 -1
  39. package/dist/compose/resolve-block-map.js +15 -5
  40. package/dist/compose/resolve-block-scalar.js +2 -2
  41. package/dist/compose/resolve-block-seq.js +1 -1
  42. package/dist/compose/resolve-end.js +2 -2
  43. package/dist/compose/resolve-flow-collection.js +14 -6
  44. package/dist/compose/resolve-flow-scalar.js +19 -7
  45. package/dist/compose/resolve-props.d.ts +3 -4
  46. package/dist/compose/resolve-props.js +30 -7
  47. package/dist/compose/util-map-includes.d.ts +4 -0
  48. package/dist/compose/util-map-includes.js +19 -0
  49. package/dist/doc/Document.d.ts +10 -2
  50. package/dist/doc/Document.js +25 -1
  51. package/dist/doc/createNode.d.ts +2 -1
  52. package/dist/doc/createNode.js +10 -4
  53. package/dist/doc/directives.d.ts +1 -0
  54. package/dist/doc/directives.js +5 -0
  55. package/dist/errors.d.ts +1 -1
  56. package/dist/index.d.ts +2 -2
  57. package/dist/node_modules/tslib/tslib.es6.js +76 -0
  58. package/dist/nodes/Alias.d.ts +7 -3
  59. package/dist/nodes/Collection.d.ts +10 -4
  60. package/dist/nodes/Collection.js +27 -14
  61. package/dist/nodes/Node.d.ts +10 -3
  62. package/dist/nodes/Node.js +7 -0
  63. package/dist/nodes/Pair.d.ts +7 -2
  64. package/dist/nodes/Pair.js +8 -0
  65. package/dist/nodes/Scalar.d.ts +2 -0
  66. package/dist/nodes/YAMLMap.d.ts +2 -0
  67. package/dist/nodes/YAMLSeq.d.ts +2 -0
  68. package/dist/nodes/addPairToJSMap.js +3 -2
  69. package/dist/options.d.ts +40 -3
  70. package/dist/options.js +2 -0
  71. package/dist/parse/cst-scalar.d.ts +6 -0
  72. package/dist/parse/cst-scalar.js +2 -6
  73. package/dist/parse/lexer.d.ts +3 -1
  74. package/dist/parse/lexer.js +64 -31
  75. package/dist/parse/parser.d.ts +1 -0
  76. package/dist/parse/parser.js +53 -52
  77. package/dist/schema/Schema.d.ts +1 -0
  78. package/dist/schema/Schema.js +5 -0
  79. package/dist/schema/tags.js +9 -5
  80. package/dist/stringify/stringify.js +2 -1
  81. package/dist/stringify/stringifyCollection.d.ts +1 -1
  82. package/dist/stringify/stringifyCollection.js +24 -10
  83. package/dist/stringify/stringifyComment.d.ts +2 -0
  84. package/dist/stringify/{addComment.js → stringifyComment.js} +5 -1
  85. package/dist/stringify/stringifyDocument.js +9 -6
  86. package/dist/stringify/stringifyPair.js +9 -11
  87. package/dist/stringify/stringifyString.js +35 -34
  88. package/dist/test-events.d.ts +1 -1
  89. package/dist/util.d.ts +3 -0
  90. package/dist/util.js +6 -0
  91. package/package.json +9 -8
  92. package/util.d.ts +3 -0
  93. package/browser/dist/stringify/addComment.js +0 -11
  94. package/dist/stringify/addComment.d.ts +0 -2
@@ -22,38 +22,6 @@ function includesNonEmpty(list) {
22
22
  }
23
23
  return false;
24
24
  }
25
- function atFirstEmptyLineAfterComments(start) {
26
- let hasComment = false;
27
- for (let i = 0; i < start.length; ++i) {
28
- switch (start[i].type) {
29
- case 'space':
30
- break;
31
- case 'comment':
32
- hasComment = true;
33
- break;
34
- case 'newline':
35
- if (!hasComment)
36
- return false;
37
- break;
38
- default:
39
- return false;
40
- }
41
- }
42
- if (hasComment) {
43
- for (let i = start.length - 1; i >= 0; --i) {
44
- switch (start[i].type) {
45
- /* istanbul ignore next */
46
- case 'space':
47
- break;
48
- case 'newline':
49
- return true;
50
- default:
51
- return false;
52
- }
53
- }
54
- }
55
- return false;
56
- }
57
25
  function isFlowToken(token) {
58
26
  switch (token === null || token === void 0 ? void 0 : token.type) {
59
27
  case 'alias':
@@ -506,24 +474,38 @@ class Parser {
506
474
  switch (this.type) {
507
475
  case 'newline':
508
476
  this.onKeyLine = false;
509
- if (!it.sep && atFirstEmptyLineAfterComments(it.start)) {
510
- const prev = map.items[map.items.length - 2];
511
- const end = (_a = prev === null || prev === void 0 ? void 0 : prev.value) === null || _a === void 0 ? void 0 : _a.end;
512
- if (Array.isArray(end)) {
513
- Array.prototype.push.apply(end, it.start);
514
- it.start = [this.sourceToken];
515
- return;
516
- }
477
+ if (it.value) {
478
+ const end = 'end' in it.value ? it.value.end : undefined;
479
+ const last = Array.isArray(end) ? end[end.length - 1] : undefined;
480
+ if ((last === null || last === void 0 ? void 0 : last.type) === 'comment')
481
+ end === null || end === void 0 ? void 0 : end.push(this.sourceToken);
482
+ else
483
+ map.items.push({ start: [this.sourceToken] });
517
484
  }
518
- // fallthrough
485
+ else if (it.sep)
486
+ it.sep.push(this.sourceToken);
487
+ else
488
+ it.start.push(this.sourceToken);
489
+ return;
519
490
  case 'space':
520
491
  case 'comment':
521
492
  if (it.value)
522
493
  map.items.push({ start: [this.sourceToken] });
523
494
  else if (it.sep)
524
495
  it.sep.push(this.sourceToken);
525
- else
496
+ else {
497
+ if (this.atIndentedComment(it.start, map.indent)) {
498
+ const prev = map.items[map.items.length - 2];
499
+ const end = (_a = prev === null || prev === void 0 ? void 0 : prev.value) === null || _a === void 0 ? void 0 : _a.end;
500
+ if (Array.isArray(end)) {
501
+ Array.prototype.push.apply(end, it.start);
502
+ end.push(this.sourceToken);
503
+ map.items.pop();
504
+ return;
505
+ }
506
+ }
526
507
  it.start.push(this.sourceToken);
508
+ }
527
509
  return;
528
510
  }
529
511
  if (this.indent >= map.indent) {
@@ -628,22 +610,34 @@ class Parser {
628
610
  const it = seq.items[seq.items.length - 1];
629
611
  switch (this.type) {
630
612
  case 'newline':
631
- if (!it.value && atFirstEmptyLineAfterComments(it.start)) {
632
- const prev = seq.items[seq.items.length - 2];
633
- const end = (_a = prev === null || prev === void 0 ? void 0 : prev.value) === null || _a === void 0 ? void 0 : _a.end;
634
- if (Array.isArray(end)) {
635
- Array.prototype.push.apply(end, it.start);
636
- it.start = [this.sourceToken];
637
- return;
638
- }
613
+ if (it.value) {
614
+ const end = 'end' in it.value ? it.value.end : undefined;
615
+ const last = Array.isArray(end) ? end[end.length - 1] : undefined;
616
+ if ((last === null || last === void 0 ? void 0 : last.type) === 'comment')
617
+ end === null || end === void 0 ? void 0 : end.push(this.sourceToken);
618
+ else
619
+ seq.items.push({ start: [this.sourceToken] });
639
620
  }
640
- // fallthrough
621
+ else
622
+ it.start.push(this.sourceToken);
623
+ return;
641
624
  case 'space':
642
625
  case 'comment':
643
626
  if (it.value)
644
627
  seq.items.push({ start: [this.sourceToken] });
645
- else
628
+ else {
629
+ if (this.atIndentedComment(it.start, seq.indent)) {
630
+ const prev = seq.items[seq.items.length - 2];
631
+ const end = (_a = prev === null || prev === void 0 ? void 0 : prev.value) === null || _a === void 0 ? void 0 : _a.end;
632
+ if (Array.isArray(end)) {
633
+ Array.prototype.push.apply(end, it.start);
634
+ end.push(this.sourceToken);
635
+ seq.items.pop();
636
+ return;
637
+ }
638
+ }
646
639
  it.start.push(this.sourceToken);
640
+ }
647
641
  return;
648
642
  case 'anchor':
649
643
  case 'tag':
@@ -838,6 +832,13 @@ class Parser {
838
832
  }
839
833
  return null;
840
834
  }
835
+ atIndentedComment(start, indent) {
836
+ if (this.type !== 'comment')
837
+ return false;
838
+ if (this.indent <= indent)
839
+ return false;
840
+ return start.every(st => st.type === 'newline' || st.type === 'space');
841
+ }
841
842
  *documentEnd(docEnd) {
842
843
  if (this.type !== 'doc-mode') {
843
844
  if (docEnd.end)
@@ -13,4 +13,5 @@ export declare class Schema {
13
13
  [SCALAR]: ScalarTag;
14
14
  [SEQ]: CollectionTag;
15
15
  constructor({ customTags, merge, resolveKnownTags, schema, sortMapEntries }: SchemaOptions);
16
+ clone(): Schema;
16
17
  }
@@ -20,6 +20,11 @@ class Schema {
20
20
  this.sortMapEntries =
21
21
  sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null;
22
22
  }
23
+ clone() {
24
+ const copy = Object.create(Schema.prototype, Object.getOwnPropertyDescriptors(this));
25
+ copy.tags = this.tags.slice();
26
+ return copy;
27
+ }
23
28
  }
24
29
 
25
30
  exports.Schema = Schema;
@@ -52,11 +52,15 @@ const coreKnownTags = {
52
52
  function getTags(customTags, schemaName) {
53
53
  let tags = schemas[schemaName];
54
54
  if (!tags) {
55
- const keys = Object.keys(schemas)
56
- .filter(key => key !== 'yaml11')
57
- .map(key => JSON.stringify(key))
58
- .join(', ');
59
- throw new Error(`Unknown schema "${schemaName}"; use one of ${keys}`);
55
+ if (Array.isArray(customTags))
56
+ tags = [];
57
+ else {
58
+ const keys = Object.keys(schemas)
59
+ .filter(key => key !== 'yaml11')
60
+ .map(key => JSON.stringify(key))
61
+ .join(', ');
62
+ throw new Error(`Unknown schema "${schemaName}"; use one of ${keys} or define customTags array`);
63
+ }
60
64
  }
61
65
  if (Array.isArray(customTags)) {
62
66
  for (const tag of customTags)
@@ -10,6 +10,7 @@ const createStringifyContext = (doc, options) => ({
10
10
  indent: '',
11
11
  indentStep: typeof options.indent === 'number' ? ' '.repeat(options.indent) : ' ',
12
12
  options: Object.assign({
13
+ blockQuote: true,
13
14
  defaultKeyType: null,
14
15
  defaultStringType: 'PLAIN',
15
16
  directives: null,
@@ -21,7 +22,7 @@ const createStringifyContext = (doc, options) => ({
21
22
  minContentWidth: 20,
22
23
  nullStr: 'null',
23
24
  simpleKeys: false,
24
- singleQuote: false,
25
+ singleQuote: null,
25
26
  trueStr: 'true',
26
27
  verifyAliasOrder: true
27
28
  }, options)
@@ -1,5 +1,5 @@
1
1
  import { Collection } from '../nodes/Collection.js';
2
- import { StringifyContext } from '../stringify/stringify.js';
2
+ import { StringifyContext } from './stringify.js';
3
3
  declare type StringifyNode = {
4
4
  comment: boolean;
5
5
  str: string;
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
3
  var Collection = require('../nodes/Collection.js');
4
- var addComment = require('./addComment.js');
5
- var stringify = require('./stringify.js');
6
4
  var Node = require('../nodes/Node.js');
5
+ var stringify = require('./stringify.js');
6
+ var stringifyComment = require('./stringifyComment.js');
7
7
 
8
8
  function stringifyCollection({ comment, flow, items }, ctx, { blockItem, flowChars, itemIndent, onChompKeep, onComment }) {
9
9
  const { indent, indentStep } = ctx;
@@ -18,10 +18,17 @@ function stringifyCollection({ comment, flow, items }, ctx, { blockItem, flowCha
18
18
  if (Node.isNode(item)) {
19
19
  if (!chompKeep && item.spaceBefore)
20
20
  nodes.push({ comment: true, str: '' });
21
- if (item.commentBefore) {
21
+ let cb = item.commentBefore;
22
+ if (cb && chompKeep)
23
+ cb = cb.replace(/^\n+/, '');
24
+ if (cb) {
25
+ if (/^\n+$/.test(cb))
26
+ cb = cb.substring(1);
22
27
  // This match will always succeed on a non-empty string
23
- for (const line of item.commentBefore.match(/^.*$/gm))
24
- nodes.push({ comment: true, str: `#${line}` });
28
+ for (const line of cb.match(/^.*$/gm)) {
29
+ const str = line === ' ' ? '#' : line ? `#${line}` : '';
30
+ nodes.push({ comment: true, str });
31
+ }
25
32
  }
26
33
  if (item.comment) {
27
34
  comment = item.comment;
@@ -33,10 +40,17 @@ function stringifyCollection({ comment, flow, items }, ctx, { blockItem, flowCha
33
40
  if (ik) {
34
41
  if (!chompKeep && ik.spaceBefore)
35
42
  nodes.push({ comment: true, str: '' });
36
- if (ik.commentBefore) {
43
+ let cb = ik.commentBefore;
44
+ if (cb && chompKeep)
45
+ cb = cb.replace(/^\n+/, '');
46
+ if (cb) {
47
+ if (/^\n+$/.test(cb))
48
+ cb = cb.substring(1);
37
49
  // This match will always succeed on a non-empty string
38
- for (const line of ik.commentBefore.match(/^.*$/gm))
39
- nodes.push({ comment: true, str: `#${line}` });
50
+ for (const line of cb.match(/^.*$/gm)) {
51
+ const str = line === ' ' ? '#' : line ? `#${line}` : '';
52
+ nodes.push({ comment: true, str });
53
+ }
40
54
  }
41
55
  if (ik.comment)
42
56
  singleLineOutput = false;
@@ -58,7 +72,7 @@ function stringifyCollection({ comment, flow, items }, ctx, { blockItem, flowCha
58
72
  let str = stringify.stringify(item, ctx, () => (comment = null), () => (chompKeep = true));
59
73
  if (inFlow && i < items.length - 1)
60
74
  str += ',';
61
- str = addComment.addComment(str, itemIndent, comment);
75
+ str = stringifyComment.addComment(str, itemIndent, comment);
62
76
  if (chompKeep && (comment || inFlow))
63
77
  chompKeep = false;
64
78
  nodes.push({ comment: false, str });
@@ -98,7 +112,7 @@ function stringifyCollection({ comment, flow, items }, ctx, { blockItem, flowCha
98
112
  str += s ? `\n${indent}${s}` : '\n';
99
113
  }
100
114
  if (comment) {
101
- str += '\n' + comment.replace(/^/gm, `${indent}#`);
115
+ str += '\n' + stringifyComment.stringifyComment(comment, indent);
102
116
  if (onComment)
103
117
  onComment();
104
118
  }
@@ -0,0 +1,2 @@
1
+ export declare const stringifyComment: (comment: string, indent: string) => string;
2
+ export declare function addComment(str: string, indent: string, comment?: string | null): string;
@@ -1,13 +1,17 @@
1
1
  'use strict';
2
2
 
3
+ const stringifyComment = (comment, indent) => /^\n+$/.test(comment)
4
+ ? comment.substring(1)
5
+ : comment.replace(/^(?!$)(?: $)?/gm, `${indent}#`);
3
6
  function addComment(str, indent, comment) {
4
7
  return !comment
5
8
  ? str
6
9
  : comment.includes('\n')
7
- ? `${str}\n` + comment.replace(/^/gm, `${indent || ''}#`)
10
+ ? `${str}\n` + stringifyComment(comment, indent)
8
11
  : str.endsWith(' ')
9
12
  ? `${str}#${comment}`
10
13
  : `${str} #${comment}`;
11
14
  }
12
15
 
13
16
  exports.addComment = addComment;
17
+ exports.stringifyComment = stringifyComment;
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var Node = require('../nodes/Node.js');
4
- var addComment = require('./addComment.js');
5
4
  var stringify = require('./stringify.js');
5
+ var stringifyComment = require('./stringifyComment.js');
6
6
 
7
7
  function stringifyDocument(doc, options) {
8
8
  const lines = [];
@@ -21,7 +21,7 @@ function stringifyDocument(doc, options) {
21
21
  if (doc.commentBefore) {
22
22
  if (lines.length !== 1)
23
23
  lines.unshift('');
24
- lines.unshift(doc.commentBefore.replace(/^/gm, '#'));
24
+ lines.unshift(stringifyComment.stringifyComment(doc.commentBefore, ''));
25
25
  }
26
26
  const ctx = stringify.createStringifyContext(doc, options);
27
27
  let chompKeep = false;
@@ -31,7 +31,7 @@ function stringifyDocument(doc, options) {
31
31
  if (doc.contents.spaceBefore && hasDirectives)
32
32
  lines.push('');
33
33
  if (doc.contents.commentBefore)
34
- lines.push(doc.contents.commentBefore.replace(/^/gm, '#'));
34
+ lines.push(stringifyComment.stringifyComment(doc.contents.commentBefore, ''));
35
35
  // top-level block scalars need to be indented if followed by a comment
36
36
  ctx.forceBlockIndent = !!doc.comment;
37
37
  contentComment = doc.contents.comment;
@@ -39,7 +39,7 @@ function stringifyDocument(doc, options) {
39
39
  const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
40
40
  let body = stringify.stringify(doc.contents, ctx, () => (contentComment = null), onChompKeep);
41
41
  if (contentComment)
42
- body = addComment.addComment(body, '', contentComment);
42
+ body = stringifyComment.addComment(body, '', contentComment);
43
43
  if ((body[0] === '|' || body[0] === '>') &&
44
44
  lines[lines.length - 1] === '---') {
45
45
  // Top-level block scalars with a preceding doc marker ought to use the
@@ -52,10 +52,13 @@ function stringifyDocument(doc, options) {
52
52
  else {
53
53
  lines.push(stringify.stringify(doc.contents, ctx));
54
54
  }
55
- if (doc.comment) {
55
+ let dc = doc.comment;
56
+ if (dc && chompKeep)
57
+ dc = dc.replace(/^\n+/, '');
58
+ if (dc) {
56
59
  if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
57
60
  lines.push('');
58
- lines.push(doc.comment.replace(/^/gm, '#'));
61
+ lines.push(stringifyComment.stringifyComment(dc, ''));
59
62
  }
60
63
  return lines.join('\n') + '\n';
61
64
  }
@@ -2,8 +2,8 @@
2
2
 
3
3
  var Node = require('../nodes/Node.js');
4
4
  var Scalar = require('../nodes/Scalar.js');
5
- var addComment = require('./addComment.js');
6
5
  var stringify = require('./stringify.js');
6
+ var stringifyComment = require('./stringifyComment.js');
7
7
 
8
8
  function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
9
9
  const { allNullValues, doc, indent, indentStep, options: { indentSeq, simpleKeys } } = ctx;
@@ -49,22 +49,20 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
49
49
  keyComment = null;
50
50
  if (chompKeep && !keyComment && onChompKeep)
51
51
  onChompKeep();
52
- return addComment.addComment(`? ${str}`, ctx.indent, keyComment);
52
+ return stringifyComment.addComment(`? ${str}`, ctx.indent, keyComment);
53
53
  }
54
54
  if (keyCommentDone)
55
55
  keyComment = null;
56
56
  str = explicitKey
57
- ? `? ${addComment.addComment(str, ctx.indent, keyComment)}\n${indent}:`
58
- : addComment.addComment(`${str}:`, ctx.indent, keyComment);
57
+ ? `? ${stringifyComment.addComment(str, ctx.indent, keyComment)}\n${indent}:`
58
+ : stringifyComment.addComment(`${str}:`, ctx.indent, keyComment);
59
59
  let vcb = '';
60
60
  let valueComment = null;
61
61
  if (Node.isNode(value)) {
62
62
  if (value.spaceBefore)
63
63
  vcb = '\n';
64
- if (value.commentBefore) {
65
- const cs = value.commentBefore.replace(/^/gm, `${ctx.indent}#`);
66
- vcb += `\n${cs}`;
67
- }
64
+ if (value.commentBefore)
65
+ vcb += `\n${stringifyComment.stringifyComment(value.commentBefore, ctx.indent)}`;
68
66
  valueComment = value.comment;
69
67
  }
70
68
  else if (value && typeof value === 'object') {
@@ -89,14 +87,14 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
89
87
  const valueStr = stringify.stringify(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true));
90
88
  let ws = ' ';
91
89
  if (vcb || keyComment) {
92
- ws = `${vcb}\n${ctx.indent}`;
90
+ ws = valueStr === '' && !ctx.inFlow ? vcb : `${vcb}\n${ctx.indent}`;
93
91
  }
94
92
  else if (!explicitKey && Node.isCollection(value)) {
95
93
  const flow = valueStr[0] === '[' || valueStr[0] === '{';
96
94
  if (!flow || valueStr.includes('\n'))
97
95
  ws = `\n${ctx.indent}`;
98
96
  }
99
- else if (valueStr[0] === '\n')
97
+ else if (valueStr === '' || valueStr[0] === '\n')
100
98
  ws = '';
101
99
  if (ctx.inFlow) {
102
100
  if (valueCommentDone && onComment)
@@ -108,7 +106,7 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
108
106
  valueComment = null;
109
107
  if (chompKeep && !valueComment && onChompKeep)
110
108
  onChompKeep();
111
- return addComment.addComment(str + ws + valueStr, ctx.indent, valueComment);
109
+ return stringifyComment.addComment(str + ws + valueStr, ctx.indent, valueComment);
112
110
  }
113
111
  }
114
112
 
@@ -120,34 +120,50 @@ function doubleQuotedString(value, ctx) {
120
120
  : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_QUOTED, getFoldOptions(ctx));
121
121
  }
122
122
  function singleQuotedString(value, ctx) {
123
- if (ctx.implicitKey) {
124
- if (/\n/.test(value))
125
- return doubleQuotedString(value, ctx);
126
- }
127
- else {
128
- // single quoted string can't have leading or trailing whitespace around newline
129
- if (/[ \t]\n|\n[ \t]/.test(value))
130
- return doubleQuotedString(value, ctx);
131
- }
123
+ if (ctx.options.singleQuote === false ||
124
+ (ctx.implicitKey && value.includes('\n')) ||
125
+ /[ \t]\n|\n[ \t]/.test(value) // single quoted string can't have leading or trailing whitespace around newline
126
+ )
127
+ return doubleQuotedString(value, ctx);
132
128
  const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
133
129
  const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
134
130
  return ctx.implicitKey
135
131
  ? res
136
132
  : foldFlowLines.foldFlowLines(res, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
137
133
  }
134
+ function quotedString(value, ctx) {
135
+ const { singleQuote } = ctx.options;
136
+ let qs;
137
+ if (singleQuote === false)
138
+ qs = doubleQuotedString;
139
+ else {
140
+ const hasDouble = value.includes('"');
141
+ const hasSingle = value.includes("'");
142
+ if (hasDouble && !hasSingle)
143
+ qs = singleQuotedString;
144
+ else if (hasSingle && !hasDouble)
145
+ qs = doubleQuotedString;
146
+ else
147
+ qs = singleQuote ? singleQuotedString : doubleQuotedString;
148
+ }
149
+ return qs(value, ctx);
150
+ }
138
151
  function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
152
+ const { lineWidth, blockQuote } = ctx.options;
139
153
  // 1. Block can't end in whitespace unless the last line is non-empty.
140
154
  // 2. Strings consisting of only whitespace are best rendered explicitly.
141
- if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
142
- return doubleQuotedString(value, ctx);
155
+ if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
156
+ return quotedString(value, ctx);
143
157
  }
144
158
  const indent = ctx.indent ||
145
159
  (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
146
- const literal = type === Scalar.Scalar.BLOCK_FOLDED
147
- ? false
148
- : type === Scalar.Scalar.BLOCK_LITERAL
149
- ? true
150
- : !lineLengthOverLimit(value, ctx.options.lineWidth, indent.length);
160
+ const literal = blockQuote === 'literal'
161
+ ? true
162
+ : blockQuote === 'folded' || type === Scalar.Scalar.BLOCK_FOLDED
163
+ ? false
164
+ : type === Scalar.Scalar.BLOCK_LITERAL
165
+ ? true
166
+ : !lineLengthOverLimit(value, lineWidth, indent.length);
151
167
  if (!value)
152
168
  return literal ? '|\n' : '>\n';
153
169
  // determine chomping from whitespace at value end
@@ -220,25 +236,10 @@ function plainString(item, ctx, onComment, onChompKeep) {
220
236
  const { actualString, implicitKey, indent, inFlow } = ctx;
221
237
  if ((implicitKey && /[\n[\]{},]/.test(value)) ||
222
238
  (inFlow && /[[\]{},]/.test(value))) {
223
- return doubleQuotedString(value, ctx);
239
+ return quotedString(value, ctx);
224
240
  }
225
241
  if (!value ||
226
242
  /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
227
- const hasDouble = value.indexOf('"') !== -1;
228
- const hasSingle = value.indexOf("'") !== -1;
229
- let quotedString;
230
- if (hasDouble && !hasSingle) {
231
- quotedString = singleQuotedString;
232
- }
233
- else if (hasSingle && !hasDouble) {
234
- quotedString = doubleQuotedString;
235
- }
236
- else if (ctx.options.singleQuote) {
237
- quotedString = singleQuotedString;
238
- }
239
- else {
240
- quotedString = doubleQuotedString;
241
- }
242
243
  // not allowed:
243
244
  // - empty string, '-' or '?'
244
245
  // - start with an indicator character (except [?:-]) or /[?-] /
@@ -269,7 +270,7 @@ function plainString(item, ctx, onComment, onChompKeep) {
269
270
  if (tag.default &&
270
271
  tag.tag !== 'tag:yaml.org,2002:str' &&
271
272
  ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str)))
272
- return doubleQuotedString(value, ctx);
273
+ return quotedString(value, ctx);
273
274
  }
274
275
  }
275
276
  return implicitKey
@@ -292,7 +293,7 @@ function stringifyString(item, ctx, onComment, onChompKeep) {
292
293
  case Scalar.Scalar.BLOCK_FOLDED:
293
294
  case Scalar.Scalar.BLOCK_LITERAL:
294
295
  return implicitKey || inFlow
295
- ? doubleQuotedString(ss.value, ctx) // blocks are not valid inside flow containers
296
+ ? quotedString(ss.value, ctx) // blocks are not valid inside flow containers
296
297
  : blockString(ss, ctx, onComment, onChompKeep);
297
298
  case Scalar.Scalar.QUOTE_DOUBLE:
298
299
  return doubleQuotedString(ss.value, ctx);
@@ -1,4 +1,4 @@
1
1
  export declare function testEvents(src: string): {
2
2
  events: string[];
3
- error: any;
3
+ error: unknown;
4
4
  };
package/dist/util.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  export { debug, LogLevelId, warn } from './log.js';
2
2
  export { findPair } from './nodes/YAMLMap.js';
3
3
  export { toJS, ToJSContext } from './nodes/toJS.js';
4
+ export { map as mapTag } from './schema/common/map.js';
5
+ export { seq as seqTag } from './schema/common/seq.js';
6
+ export { string as stringTag } from './schema/common/string.js';
4
7
  export { foldFlowLines } from './stringify/foldFlowLines';
5
8
  export { stringifyNumber } from './stringify/stringifyNumber.js';
6
9
  export { stringifyString } from './stringify/stringifyString.js';
package/dist/util.js CHANGED
@@ -3,6 +3,9 @@
3
3
  var log = require('./log.js');
4
4
  var YAMLMap = require('./nodes/YAMLMap.js');
5
5
  var toJS = require('./nodes/toJS.js');
6
+ var map = require('./schema/common/map.js');
7
+ var seq = require('./schema/common/seq.js');
8
+ var string = require('./schema/common/string.js');
6
9
  var foldFlowLines = require('./stringify/foldFlowLines.js');
7
10
  var stringifyNumber = require('./stringify/stringifyNumber.js');
8
11
  var stringifyString = require('./stringify/stringifyString.js');
@@ -13,6 +16,9 @@ exports.debug = log.debug;
13
16
  exports.warn = log.warn;
14
17
  exports.findPair = YAMLMap.findPair;
15
18
  exports.toJS = toJS.toJS;
19
+ exports.mapTag = map.map;
20
+ exports.seqTag = seq.seq;
21
+ exports.stringTag = string.string;
16
22
  exports.foldFlowLines = foldFlowLines.foldFlowLines;
17
23
  exports.stringifyNumber = stringifyNumber.stringifyNumber;
18
24
  exports.stringifyString = stringifyString.stringifyString;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml",
3
- "version": "2.0.0-5",
3
+ "version": "2.0.0-9",
4
4
  "license": "ISC",
5
5
  "author": "Eemeli Aro <eemeli@gmail.com>",
6
6
  "repository": "github:eemeli/yaml",
@@ -14,6 +14,7 @@
14
14
  "files": [
15
15
  "browser/",
16
16
  "dist/",
17
+ "util.d.ts",
17
18
  "util.js"
18
19
  ],
19
20
  "type": "commonjs",
@@ -68,25 +69,25 @@
68
69
  "@babel/plugin-transform-typescript": "^7.12.17",
69
70
  "@babel/preset-env": "^7.12.11",
70
71
  "@rollup/plugin-babel": "^5.2.3",
71
- "@rollup/plugin-replace": "^2.3.4",
72
+ "@rollup/plugin-replace": "^3.0.0",
72
73
  "@rollup/plugin-typescript": "^8.1.1",
73
- "@types/jest": "^26.0.20",
74
- "@types/node": "^14.14.37",
74
+ "@types/jest": "^27.0.1",
75
+ "@types/node": "^16.9.1",
75
76
  "@typescript-eslint/eslint-plugin": "^4.15.2",
76
77
  "@typescript-eslint/parser": "^4.15.2",
77
- "babel-jest": "^26.6.3",
78
+ "babel-jest": "^27.0.1",
78
79
  "cross-env": "^7.0.3",
79
80
  "eslint": "^7.20.0",
80
81
  "eslint-config-prettier": "^8.1.0",
81
82
  "fast-check": "^2.12.0",
82
- "jest": "^26.6.3",
83
+ "jest": "^27.0.1",
83
84
  "jest-ts-webcompat-resolver": "^1.0.0",
84
85
  "prettier": "^2.2.1",
85
86
  "rollup": "^2.38.2",
86
87
  "tslib": "^2.1.0",
87
- "typescript": "^4.1.3"
88
+ "typescript": "^4.3.5"
88
89
  },
89
90
  "engines": {
90
- "node": ">= 10"
91
+ "node": ">= 12"
91
92
  }
92
93
  }
package/util.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ // Workaround for incomplete exports support in TypeScript
2
+ // https://github.com/microsoft/TypeScript/issues/33079
3
+ export * from './dist/util.js'
@@ -1,11 +0,0 @@
1
- function addComment(str, indent, comment) {
2
- return !comment
3
- ? str
4
- : comment.includes('\n')
5
- ? `${str}\n` + comment.replace(/^/gm, `${indent || ''}#`)
6
- : str.endsWith(' ')
7
- ? `${str}#${comment}`
8
- : `${str} #${comment}`;
9
- }
10
-
11
- export { addComment };
@@ -1,2 +0,0 @@
1
- export declare function addCommentBefore(str: string, indent: string, comment?: string | null): string;
2
- export declare function addComment(str: string, indent: string, comment?: string | null): string;