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
@@ -1,122 +1,152 @@
1
1
  import { Collection } from '../nodes/Collection.js';
2
2
  import { isNode, isPair } from '../nodes/Node.js';
3
3
  import { stringify } from './stringify.js';
4
- import { addComment, stringifyComment } from './stringifyComment.js';
4
+ import { lineComment, indentComment } from './stringifyComment.js';
5
5
 
6
- function stringifyCollection({ comment, flow, items }, ctx, { blockItem, flowChars, itemIndent, onChompKeep, onComment }) {
7
- const { indent, indentStep } = ctx;
8
- const inFlow = flow || ctx.inFlow;
9
- if (inFlow)
10
- itemIndent += indentStep;
11
- ctx = Object.assign({}, ctx, { indent: itemIndent, inFlow, type: null });
12
- let singleLineOutput = true;
6
+ function stringifyCollection(collection, ctx, options) {
7
+ var _a;
8
+ const flow = (_a = ctx.inFlow) !== null && _a !== void 0 ? _a : collection.flow;
9
+ const stringify = flow ? stringifyFlowCollection : stringifyBlockCollection;
10
+ return stringify(collection, ctx, options);
11
+ }
12
+ function stringifyBlockCollection({ comment, items }, ctx, { blockItemPrefix, flowChars, itemIndent, onChompKeep, onComment }) {
13
+ const { indent, options: { commentString } } = ctx;
14
+ const itemCtx = Object.assign({}, ctx, { indent: itemIndent, type: null });
13
15
  let chompKeep = false; // flag for the preceding node's status
14
- const nodes = items.reduce((nodes, item, i) => {
16
+ const lines = [];
17
+ for (let i = 0; i < items.length; ++i) {
18
+ const item = items[i];
15
19
  let comment = null;
16
20
  if (isNode(item)) {
17
21
  if (!chompKeep && item.spaceBefore)
18
- nodes.push({ comment: true, str: '' });
19
- let cb = item.commentBefore;
20
- if (cb && chompKeep)
21
- cb = cb.replace(/^\n+/, '');
22
- if (cb) {
23
- if (/^\n+$/.test(cb))
24
- cb = cb.substring(1);
25
- // This match will always succeed on a non-empty string
26
- for (const line of cb.match(/^.*$/gm)) {
27
- const str = line === ' ' ? '#' : line ? `#${line}` : '';
28
- nodes.push({ comment: true, str });
29
- }
30
- }
31
- if (item.comment) {
22
+ lines.push('');
23
+ addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
24
+ if (item.comment)
32
25
  comment = item.comment;
33
- singleLineOutput = false;
34
- }
35
26
  }
36
27
  else if (isPair(item)) {
37
28
  const ik = isNode(item.key) ? item.key : null;
38
29
  if (ik) {
39
30
  if (!chompKeep && ik.spaceBefore)
40
- nodes.push({ comment: true, str: '' });
41
- let cb = ik.commentBefore;
42
- if (cb && chompKeep)
43
- cb = cb.replace(/^\n+/, '');
44
- if (cb) {
45
- if (/^\n+$/.test(cb))
46
- cb = cb.substring(1);
47
- // This match will always succeed on a non-empty string
48
- for (const line of cb.match(/^.*$/gm)) {
49
- const str = line === ' ' ? '#' : line ? `#${line}` : '';
50
- nodes.push({ comment: true, str });
51
- }
52
- }
53
- if (ik.comment)
54
- singleLineOutput = false;
55
- }
56
- if (inFlow) {
57
- const iv = isNode(item.value) ? item.value : null;
58
- if (iv) {
59
- if (iv.comment)
60
- comment = iv.comment;
61
- if (iv.comment || iv.commentBefore)
62
- singleLineOutput = false;
63
- }
64
- else if (item.value == null && ik && ik.comment) {
65
- comment = ik.comment;
66
- }
31
+ lines.push('');
32
+ addCommentBefore(ctx, lines, ik.commentBefore, chompKeep);
67
33
  }
68
34
  }
69
35
  chompKeep = false;
70
- let str = stringify(item, ctx, () => (comment = null), () => (chompKeep = true));
71
- if (inFlow && i < items.length - 1)
72
- str += ',';
73
- str = addComment(str, itemIndent, comment);
74
- if (chompKeep && (comment || inFlow))
36
+ let str = stringify(item, itemCtx, () => (comment = null), () => (chompKeep = true));
37
+ if (comment)
38
+ str += lineComment(str, itemIndent, commentString(comment));
39
+ if (chompKeep && comment)
75
40
  chompKeep = false;
76
- nodes.push({ comment: false, str });
77
- return nodes;
78
- }, []);
41
+ lines.push(blockItemPrefix + str);
42
+ }
79
43
  let str;
80
- if (nodes.length === 0) {
44
+ if (lines.length === 0) {
81
45
  str = flowChars.start + flowChars.end;
82
46
  }
83
- else if (inFlow) {
84
- const { start, end } = flowChars;
85
- const strings = nodes.map(n => n.str);
86
- let singleLineLength = 2;
87
- for (const node of nodes) {
88
- if (node.comment || node.str.includes('\n')) {
89
- singleLineOutput = false;
90
- break;
47
+ else {
48
+ str = lines[0];
49
+ for (let i = 1; i < lines.length; ++i) {
50
+ const line = lines[i];
51
+ str += line ? `\n${indent}${line}` : '\n';
52
+ }
53
+ }
54
+ if (comment) {
55
+ str += '\n' + indentComment(commentString(comment), indent);
56
+ if (onComment)
57
+ onComment();
58
+ }
59
+ else if (chompKeep && onChompKeep)
60
+ onChompKeep();
61
+ return str;
62
+ }
63
+ function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemIndent, onComment }) {
64
+ const { indent, indentStep, options: { commentString } } = ctx;
65
+ itemIndent += indentStep;
66
+ const itemCtx = Object.assign({}, ctx, {
67
+ indent: itemIndent,
68
+ inFlow: true,
69
+ type: null
70
+ });
71
+ let reqNewline = false;
72
+ let linesAtValue = 0;
73
+ const lines = [];
74
+ for (let i = 0; i < items.length; ++i) {
75
+ const item = items[i];
76
+ let comment = null;
77
+ if (isNode(item)) {
78
+ if (item.spaceBefore)
79
+ lines.push('');
80
+ addCommentBefore(ctx, lines, item.commentBefore, false);
81
+ if (item.comment)
82
+ comment = item.comment;
83
+ }
84
+ else if (isPair(item)) {
85
+ const ik = isNode(item.key) ? item.key : null;
86
+ if (ik) {
87
+ if (ik.spaceBefore)
88
+ lines.push('');
89
+ addCommentBefore(ctx, lines, ik.commentBefore, false);
90
+ if (ik.comment)
91
+ reqNewline = true;
92
+ }
93
+ const iv = isNode(item.value) ? item.value : null;
94
+ if (iv) {
95
+ if (iv.comment)
96
+ comment = iv.comment;
97
+ if (iv.commentBefore)
98
+ reqNewline = true;
99
+ }
100
+ else if (item.value == null && ik && ik.comment) {
101
+ comment = ik.comment;
91
102
  }
92
- singleLineLength += node.str.length + 2;
93
103
  }
94
- if (!singleLineOutput ||
95
- singleLineLength > Collection.maxFlowStringSingleLineLength) {
104
+ if (comment)
105
+ reqNewline = true;
106
+ let str = stringify(item, itemCtx, () => (comment = null));
107
+ if (i < items.length - 1)
108
+ str += ',';
109
+ if (comment)
110
+ str += lineComment(str, itemIndent, commentString(comment));
111
+ if (!reqNewline && (lines.length > linesAtValue || str.includes('\n')))
112
+ reqNewline = true;
113
+ lines.push(str);
114
+ linesAtValue = lines.length;
115
+ }
116
+ let str;
117
+ const { start, end } = flowChars;
118
+ if (lines.length === 0) {
119
+ str = start + end;
120
+ }
121
+ else {
122
+ if (!reqNewline) {
123
+ const len = lines.reduce((sum, line) => sum + line.length + 2, 2);
124
+ reqNewline = len > Collection.maxFlowStringSingleLineLength;
125
+ }
126
+ if (reqNewline) {
96
127
  str = start;
97
- for (const s of strings) {
98
- str += s ? `\n${indentStep}${indent}${s}` : '\n';
99
- }
128
+ for (const line of lines)
129
+ str += line ? `\n${indentStep}${indent}${line}` : '\n';
100
130
  str += `\n${indent}${end}`;
101
131
  }
102
132
  else {
103
- str = `${start} ${strings.join(' ')} ${end}`;
133
+ str = `${start} ${lines.join(' ')} ${end}`;
104
134
  }
105
135
  }
106
- else {
107
- const strings = nodes.map(blockItem);
108
- str = strings.shift() || '';
109
- for (const s of strings)
110
- str += s ? `\n${indent}${s}` : '\n';
111
- }
112
136
  if (comment) {
113
- str += '\n' + stringifyComment(comment, indent);
137
+ str += lineComment(str, commentString(comment), indent);
114
138
  if (onComment)
115
139
  onComment();
116
140
  }
117
- else if (chompKeep && onChompKeep)
118
- onChompKeep();
119
141
  return str;
120
142
  }
143
+ function addCommentBefore({ indent, options: { commentString } }, lines, comment, chompKeep) {
144
+ if (comment && chompKeep)
145
+ comment = comment.replace(/^\n+/, '');
146
+ if (comment) {
147
+ const ic = indentComment(commentString(comment), indent);
148
+ lines.push(ic.trimStart()); // Avoid double indent on first line
149
+ }
150
+ }
121
151
 
122
152
  export { stringifyCollection };
@@ -1,14 +1,20 @@
1
- const stringifyComment = (comment, indent) => /^\n+$/.test(comment)
2
- ? comment.substring(1)
3
- : comment.replace(/^(?!$)(?: $)?/gm, `${indent}#`);
4
- function addComment(str, indent, comment) {
5
- return !comment
6
- ? str
7
- : comment.includes('\n')
8
- ? `${str}\n` + stringifyComment(comment, indent)
9
- : str.endsWith(' ')
10
- ? `${str}#${comment}`
11
- : `${str} #${comment}`;
1
+ /**
2
+ * Stringifies a comment.
3
+ *
4
+ * Empty comment lines are left empty,
5
+ * lines consisting of a single space are replaced by `#`,
6
+ * and all other lines are prefixed with a `#`.
7
+ */
8
+ const stringifyComment = (str) => str.replace(/^(?!$)(?: $)?/gm, '#');
9
+ function indentComment(comment, indent) {
10
+ if (/^\n+$/.test(comment))
11
+ return comment.substring(1);
12
+ return indent ? comment.replace(/^(?! *$)/gm, indent) : comment;
12
13
  }
14
+ const lineComment = (str, indent, comment) => str.endsWith('\n')
15
+ ? indentComment(comment, indent)
16
+ : comment.includes('\n')
17
+ ? '\n' + indentComment(comment, indent)
18
+ : (str.endsWith(' ') ? '' : ' ') + comment;
13
19
 
14
- export { addComment, stringifyComment };
20
+ export { indentComment, lineComment, stringifyComment };
@@ -1,35 +1,40 @@
1
1
  import { isNode } from '../nodes/Node.js';
2
2
  import { createStringifyContext, stringify } from './stringify.js';
3
- import { stringifyComment, addComment } from './stringifyComment.js';
3
+ import { indentComment, lineComment } from './stringifyComment.js';
4
4
 
5
5
  function stringifyDocument(doc, options) {
6
+ var _a;
6
7
  const lines = [];
7
8
  let hasDirectives = options.directives === true;
8
- if (options.directives !== false) {
9
+ if (options.directives !== false && doc.directives) {
9
10
  const dir = doc.directives.toString(doc);
10
11
  if (dir) {
11
12
  lines.push(dir);
12
13
  hasDirectives = true;
13
14
  }
14
- else if (doc.directives.marker)
15
+ else if (doc.directives.docStart)
15
16
  hasDirectives = true;
16
17
  }
17
18
  if (hasDirectives)
18
19
  lines.push('---');
20
+ const ctx = createStringifyContext(doc, options);
21
+ const { commentString } = ctx.options;
19
22
  if (doc.commentBefore) {
20
23
  if (lines.length !== 1)
21
24
  lines.unshift('');
22
- lines.unshift(stringifyComment(doc.commentBefore, ''));
25
+ const cs = commentString(doc.commentBefore);
26
+ lines.unshift(indentComment(cs, ''));
23
27
  }
24
- const ctx = createStringifyContext(doc, options);
25
28
  let chompKeep = false;
26
29
  let contentComment = null;
27
30
  if (doc.contents) {
28
31
  if (isNode(doc.contents)) {
29
32
  if (doc.contents.spaceBefore && hasDirectives)
30
33
  lines.push('');
31
- if (doc.contents.commentBefore)
32
- lines.push(stringifyComment(doc.contents.commentBefore, ''));
34
+ if (doc.contents.commentBefore) {
35
+ const cs = commentString(doc.contents.commentBefore);
36
+ lines.push(indentComment(cs, ''));
37
+ }
33
38
  // top-level block scalars need to be indented if followed by a comment
34
39
  ctx.forceBlockIndent = !!doc.comment;
35
40
  contentComment = doc.contents.comment;
@@ -37,7 +42,7 @@ function stringifyDocument(doc, options) {
37
42
  const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
38
43
  let body = stringify(doc.contents, ctx, () => (contentComment = null), onChompKeep);
39
44
  if (contentComment)
40
- body = addComment(body, '', contentComment);
45
+ body += lineComment(body, '', commentString(contentComment));
41
46
  if ((body[0] === '|' || body[0] === '>') &&
42
47
  lines[lines.length - 1] === '---') {
43
48
  // Top-level block scalars with a preceding doc marker ought to use the
@@ -50,13 +55,30 @@ function stringifyDocument(doc, options) {
50
55
  else {
51
56
  lines.push(stringify(doc.contents, ctx));
52
57
  }
53
- let dc = doc.comment;
54
- if (dc && chompKeep)
55
- dc = dc.replace(/^\n+/, '');
56
- if (dc) {
57
- if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
58
- lines.push('');
59
- lines.push(stringifyComment(dc, ''));
58
+ if ((_a = doc.directives) === null || _a === void 0 ? void 0 : _a.docEnd) {
59
+ if (doc.comment) {
60
+ const cs = commentString(doc.comment);
61
+ if (cs.includes('\n')) {
62
+ lines.push('...');
63
+ lines.push(indentComment(cs, ''));
64
+ }
65
+ else {
66
+ lines.push(`... ${cs}`);
67
+ }
68
+ }
69
+ else {
70
+ lines.push('...');
71
+ }
72
+ }
73
+ else {
74
+ let dc = doc.comment;
75
+ if (dc && chompKeep)
76
+ dc = dc.replace(/^\n+/, '');
77
+ if (dc) {
78
+ if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
79
+ lines.push('');
80
+ lines.push(indentComment(commentString(dc), ''));
81
+ }
60
82
  }
61
83
  return lines.join('\n') + '\n';
62
84
  }
@@ -1,10 +1,10 @@
1
1
  import { isCollection, isNode, isScalar, isSeq } from '../nodes/Node.js';
2
2
  import { Scalar } from '../nodes/Scalar.js';
3
3
  import { stringify } from './stringify.js';
4
- import { addComment, stringifyComment } from './stringifyComment.js';
4
+ import { lineComment, indentComment } from './stringifyComment.js';
5
5
 
6
6
  function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
7
- const { allNullValues, doc, indent, indentStep, options: { indentSeq, simpleKeys } } = ctx;
7
+ const { allNullValues, doc, indent, indentStep, options: { commentString, indentSeq, simpleKeys } } = ctx;
8
8
  let keyComment = (isNode(key) && key.comment) || null;
9
9
  if (simpleKeys) {
10
10
  if (keyComment) {
@@ -39,28 +39,39 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
39
39
  if (allNullValues || value == null) {
40
40
  if (keyCommentDone && onComment)
41
41
  onComment();
42
- return explicitKey ? `? ${str}` : str;
42
+ return str === '' ? '?' : explicitKey ? `? ${str}` : str;
43
43
  }
44
44
  }
45
45
  else if ((allNullValues && !simpleKeys) || (value == null && explicitKey)) {
46
- if (keyCommentDone)
47
- keyComment = null;
48
- if (chompKeep && !keyComment && onChompKeep)
46
+ str = `? ${str}`;
47
+ if (keyComment && !keyCommentDone) {
48
+ str += lineComment(str, ctx.indent, commentString(keyComment));
49
+ }
50
+ else if (chompKeep && onChompKeep)
49
51
  onChompKeep();
50
- return addComment(`? ${str}`, ctx.indent, keyComment);
52
+ return str;
51
53
  }
52
54
  if (keyCommentDone)
53
55
  keyComment = null;
54
- str = explicitKey
55
- ? `? ${addComment(str, ctx.indent, keyComment)}\n${indent}:`
56
- : addComment(`${str}:`, ctx.indent, keyComment);
56
+ if (explicitKey) {
57
+ if (keyComment)
58
+ str += lineComment(str, ctx.indent, commentString(keyComment));
59
+ str = `? ${str}\n${indent}:`;
60
+ }
61
+ else {
62
+ str = `${str}:`;
63
+ if (keyComment)
64
+ str += lineComment(str, ctx.indent, commentString(keyComment));
65
+ }
57
66
  let vcb = '';
58
67
  let valueComment = null;
59
68
  if (isNode(value)) {
60
69
  if (value.spaceBefore)
61
70
  vcb = '\n';
62
- if (value.commentBefore)
63
- vcb += `\n${stringifyComment(value.commentBefore, ctx.indent)}`;
71
+ if (value.commentBefore) {
72
+ const cs = commentString(value.commentBefore);
73
+ vcb += `\n${indentComment(cs, ctx.indent)}`;
74
+ }
64
75
  valueComment = value.comment;
65
76
  }
66
77
  else if (value && typeof value === 'object') {
@@ -85,27 +96,30 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
85
96
  const valueStr = stringify(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true));
86
97
  let ws = ' ';
87
98
  if (vcb || keyComment) {
88
- ws = `${vcb}\n${ctx.indent}`;
99
+ if (valueStr === '' && !ctx.inFlow)
100
+ ws = vcb === '\n' ? '\n\n' : vcb;
101
+ else
102
+ ws = `${vcb}\n${ctx.indent}`;
89
103
  }
90
104
  else if (!explicitKey && isCollection(value)) {
91
105
  const flow = valueStr[0] === '[' || valueStr[0] === '{';
92
106
  if (!flow || valueStr.includes('\n'))
93
107
  ws = `\n${ctx.indent}`;
94
108
  }
95
- else if (valueStr[0] === '\n')
109
+ else if (valueStr === '' || valueStr[0] === '\n')
96
110
  ws = '';
111
+ str += ws + valueStr;
97
112
  if (ctx.inFlow) {
98
113
  if (valueCommentDone && onComment)
99
114
  onComment();
100
- return str + ws + valueStr;
101
115
  }
102
- else {
103
- if (valueCommentDone)
104
- valueComment = null;
105
- if (chompKeep && !valueComment && onChompKeep)
106
- onChompKeep();
107
- return addComment(str + ws + valueStr, ctx.indent, valueComment);
116
+ else if (valueComment && !valueCommentDone) {
117
+ str += lineComment(str, ctx.indent, commentString(valueComment));
118
+ }
119
+ else if (chompKeep && onChompKeep) {
120
+ onChompKeep();
108
121
  }
122
+ return str;
109
123
  }
110
124
 
111
125
  export { stringifyPair };
@@ -118,34 +118,50 @@ function doubleQuotedString(value, ctx) {
118
118
  : foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx));
119
119
  }
120
120
  function singleQuotedString(value, ctx) {
121
- if (ctx.implicitKey) {
122
- if (/\n/.test(value))
123
- return doubleQuotedString(value, ctx);
124
- }
125
- else {
126
- // single quoted string can't have leading or trailing whitespace around newline
127
- if (/[ \t]\n|\n[ \t]/.test(value))
128
- return doubleQuotedString(value, ctx);
129
- }
121
+ if (ctx.options.singleQuote === false ||
122
+ (ctx.implicitKey && value.includes('\n')) ||
123
+ /[ \t]\n|\n[ \t]/.test(value) // single quoted string can't have leading or trailing whitespace around newline
124
+ )
125
+ return doubleQuotedString(value, ctx);
130
126
  const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
131
127
  const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
132
128
  return ctx.implicitKey
133
129
  ? res
134
130
  : foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx));
135
131
  }
132
+ function quotedString(value, ctx) {
133
+ const { singleQuote } = ctx.options;
134
+ let qs;
135
+ if (singleQuote === false)
136
+ qs = doubleQuotedString;
137
+ else {
138
+ const hasDouble = value.includes('"');
139
+ const hasSingle = value.includes("'");
140
+ if (hasDouble && !hasSingle)
141
+ qs = singleQuotedString;
142
+ else if (hasSingle && !hasDouble)
143
+ qs = doubleQuotedString;
144
+ else
145
+ qs = singleQuote ? singleQuotedString : doubleQuotedString;
146
+ }
147
+ return qs(value, ctx);
148
+ }
136
149
  function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
150
+ const { blockQuote, commentString, lineWidth } = ctx.options;
137
151
  // 1. Block can't end in whitespace unless the last line is non-empty.
138
152
  // 2. Strings consisting of only whitespace are best rendered explicitly.
139
- if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
140
- return doubleQuotedString(value, ctx);
153
+ if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
154
+ return quotedString(value, ctx);
141
155
  }
142
156
  const indent = ctx.indent ||
143
157
  (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
144
- const literal = type === Scalar.BLOCK_FOLDED
145
- ? false
146
- : type === Scalar.BLOCK_LITERAL
147
- ? true
148
- : !lineLengthOverLimit(value, ctx.options.lineWidth, indent.length);
158
+ const literal = blockQuote === 'literal'
159
+ ? true
160
+ : blockQuote === 'folded' || type === Scalar.BLOCK_FOLDED
161
+ ? false
162
+ : type === Scalar.BLOCK_LITERAL
163
+ ? true
164
+ : !lineLengthOverLimit(value, lineWidth, indent.length);
149
165
  if (!value)
150
166
  return literal ? '|\n' : '>\n';
151
167
  // determine chomping from whitespace at value end
@@ -196,7 +212,7 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
196
212
  const indentSize = indent ? '2' : '1'; // root is at -1
197
213
  let header = (literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp;
198
214
  if (comment) {
199
- header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ');
215
+ header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
200
216
  if (onComment)
201
217
  onComment();
202
218
  }
@@ -213,44 +229,28 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
213
229
  return `${header}\n${indent}${body}`;
214
230
  }
215
231
  function plainString(item, ctx, onComment, onChompKeep) {
216
- var _a;
217
232
  const { type, value } = item;
218
233
  const { actualString, implicitKey, indent, inFlow } = ctx;
219
234
  if ((implicitKey && /[\n[\]{},]/.test(value)) ||
220
235
  (inFlow && /[[\]{},]/.test(value))) {
221
- return doubleQuotedString(value, ctx);
236
+ return quotedString(value, ctx);
222
237
  }
223
238
  if (!value ||
224
239
  /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
225
- const hasDouble = value.indexOf('"') !== -1;
226
- const hasSingle = value.indexOf("'") !== -1;
227
- let quotedString;
228
- if (hasDouble && !hasSingle) {
229
- quotedString = singleQuotedString;
230
- }
231
- else if (hasSingle && !hasDouble) {
232
- quotedString = doubleQuotedString;
233
- }
234
- else if (ctx.options.singleQuote) {
235
- quotedString = singleQuotedString;
236
- }
237
- else {
238
- quotedString = doubleQuotedString;
239
- }
240
240
  // not allowed:
241
241
  // - empty string, '-' or '?'
242
242
  // - start with an indicator character (except [?:-]) or /[?-] /
243
243
  // - '\n ', ': ' or ' \n' anywhere
244
244
  // - '#' not preceded by a non-space char
245
245
  // - end with ' ' or ':'
246
- return implicitKey || inFlow || value.indexOf('\n') === -1
246
+ return implicitKey || inFlow || !value.includes('\n')
247
247
  ? quotedString(value, ctx)
248
248
  : blockString(item, ctx, onComment, onChompKeep);
249
249
  }
250
250
  if (!implicitKey &&
251
251
  !inFlow &&
252
252
  type !== Scalar.PLAIN &&
253
- value.indexOf('\n') !== -1) {
253
+ value.includes('\n')) {
254
254
  // Where allowed & type not set explicitly, prefer block style for multiline strings
255
255
  return blockString(item, ctx, onComment, onChompKeep);
256
256
  }
@@ -263,12 +263,10 @@ function plainString(item, ctx, onComment, onChompKeep) {
263
263
  // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
264
264
  // and others in v1.1.
265
265
  if (actualString) {
266
- for (const tag of ctx.doc.schema.tags) {
267
- if (tag.default &&
268
- tag.tag !== 'tag:yaml.org,2002:str' &&
269
- ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str)))
270
- return doubleQuotedString(value, ctx);
271
- }
266
+ const test = (tag) => { var _a; return tag.default && tag.tag !== 'tag:yaml.org,2002:str' && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str)); };
267
+ const { compat, tags } = ctx.doc.schema;
268
+ if (tags.some(test) || (compat === null || compat === void 0 ? void 0 : compat.some(test)))
269
+ return quotedString(value, ctx);
272
270
  }
273
271
  return implicitKey
274
272
  ? str
@@ -290,7 +288,7 @@ function stringifyString(item, ctx, onComment, onChompKeep) {
290
288
  case Scalar.BLOCK_FOLDED:
291
289
  case Scalar.BLOCK_LITERAL:
292
290
  return implicitKey || inFlow
293
- ? doubleQuotedString(ss.value, ctx) // blocks are not valid inside flow containers
291
+ ? quotedString(ss.value, ctx) // blocks are not valid inside flow containers
294
292
  : blockString(ss, ctx, onComment, onChompKeep);
295
293
  case Scalar.QUOTE_DOUBLE:
296
294
  return doubleQuotedString(ss.value, ctx);
@@ -1,6 +1,9 @@
1
1
  export { debug, warn } from './log.js';
2
2
  export { findPair } from './nodes/YAMLMap.js';
3
3
  export { toJS } 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.js';
5
8
  export { stringifyNumber } from './stringify/stringifyNumber.js';
6
9
  export { stringifyString } from './stringify/stringifyString.js';