yaml 2.0.0-9 → 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 (102) 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 +13 -4
  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 +9 -2
  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 +14 -5
  10. package/browser/dist/compose/resolve-flow-scalar.js +2 -1
  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 +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/nodes/Pair.js +2 -2
  19. package/browser/dist/nodes/Scalar.js +1 -1
  20. package/browser/dist/nodes/YAMLMap.js +6 -5
  21. package/browser/dist/nodes/YAMLSeq.js +2 -2
  22. package/browser/dist/nodes/addPairToJSMap.js +1 -1
  23. package/browser/dist/nodes/toJS.js +3 -1
  24. package/browser/dist/parse/cst-visit.js +2 -2
  25. package/browser/dist/parse/lexer.js +21 -8
  26. package/browser/dist/parse/parser.js +121 -45
  27. package/browser/dist/public-api.js +4 -5
  28. package/browser/dist/schema/Schema.js +14 -4
  29. package/browser/dist/schema/common/null.js +3 -1
  30. package/browser/dist/schema/core/float.js +4 -1
  31. package/browser/dist/schema/tags.js +9 -9
  32. package/browser/dist/schema/yaml-1.1/float.js +4 -1
  33. package/browser/dist/schema/yaml-1.1/omap.js +1 -1
  34. package/browser/dist/schema/yaml-1.1/pairs.js +2 -1
  35. package/browser/dist/stringify/stringify.js +51 -22
  36. package/browser/dist/stringify/stringifyCollection.js +115 -85
  37. package/browser/dist/stringify/stringifyComment.js +18 -12
  38. package/browser/dist/stringify/stringifyDocument.js +37 -15
  39. package/browser/dist/stringify/stringifyPair.js +34 -20
  40. package/browser/dist/stringify/stringifyString.js +8 -11
  41. package/browser/dist/visit.js +150 -62
  42. package/dist/compose/compose-doc.js +3 -2
  43. package/dist/compose/compose-node.d.ts +1 -0
  44. package/dist/compose/compose-node.js +13 -4
  45. package/dist/compose/compose-scalar.js +15 -9
  46. package/dist/compose/composer.d.ts +1 -1
  47. package/dist/compose/composer.js +8 -9
  48. package/dist/compose/resolve-block-map.js +9 -2
  49. package/dist/compose/resolve-block-scalar.js +12 -3
  50. package/dist/compose/resolve-block-seq.js +5 -0
  51. package/dist/compose/resolve-flow-collection.js +14 -5
  52. package/dist/compose/resolve-flow-scalar.js +2 -1
  53. package/dist/compose/resolve-props.d.ts +1 -1
  54. package/dist/compose/resolve-props.js +4 -0
  55. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  56. package/dist/compose/util-flow-indent-check.js +17 -0
  57. package/dist/doc/Document.d.ts +7 -4
  58. package/dist/doc/Document.js +56 -23
  59. package/dist/doc/anchors.d.ts +2 -2
  60. package/dist/doc/anchors.js +2 -2
  61. package/dist/doc/createNode.d.ts +1 -1
  62. package/dist/doc/createNode.js +7 -4
  63. package/dist/doc/directives.d.ts +4 -2
  64. package/dist/doc/directives.js +7 -4
  65. package/dist/index.d.ts +2 -2
  66. package/dist/index.js +1 -2
  67. package/dist/nodes/Pair.js +2 -2
  68. package/dist/nodes/Scalar.js +1 -1
  69. package/dist/nodes/YAMLMap.d.ts +3 -1
  70. package/dist/nodes/YAMLMap.js +6 -5
  71. package/dist/nodes/YAMLSeq.js +2 -2
  72. package/dist/nodes/addPairToJSMap.js +1 -1
  73. package/dist/nodes/toJS.js +3 -1
  74. package/dist/options.d.ts +42 -12
  75. package/dist/parse/cst-visit.js +2 -2
  76. package/dist/parse/lexer.js +21 -8
  77. package/dist/parse/parser.js +121 -45
  78. package/dist/public-api.js +4 -5
  79. package/dist/schema/Schema.d.ts +5 -4
  80. package/dist/schema/Schema.js +13 -3
  81. package/dist/schema/common/null.js +3 -1
  82. package/dist/schema/core/float.js +4 -1
  83. package/dist/schema/tags.d.ts +1 -2
  84. package/dist/schema/tags.js +9 -9
  85. package/dist/schema/yaml-1.1/float.js +4 -1
  86. package/dist/schema/yaml-1.1/omap.js +1 -1
  87. package/dist/schema/yaml-1.1/pairs.js +2 -1
  88. package/dist/stringify/stringify.d.ts +5 -3
  89. package/dist/stringify/stringify.js +51 -22
  90. package/dist/stringify/stringifyCollection.d.ts +2 -6
  91. package/dist/stringify/stringifyCollection.js +114 -84
  92. package/dist/stringify/stringifyComment.d.ts +10 -2
  93. package/dist/stringify/stringifyComment.js +19 -12
  94. package/dist/stringify/stringifyDocument.js +36 -14
  95. package/dist/stringify/stringifyPair.js +33 -19
  96. package/dist/stringify/stringifyString.js +8 -11
  97. package/dist/test-events.js +13 -12
  98. package/dist/visit.d.ts +49 -10
  99. package/dist/visit.js +149 -60
  100. package/package.json +9 -8
  101. package/browser/dist/options.js +0 -18
  102. package/dist/options.js +0 -20
@@ -1,14 +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({
11
8
  blockQuote: true,
9
+ commentString: stringifyComment,
12
10
  defaultKeyType: null,
13
11
  defaultStringType: 'PLAIN',
14
12
  directives: null,
@@ -23,54 +21,85 @@ const createStringifyContext = (doc, options) => ({
23
21
  singleQuote: null,
24
22
  trueStr: 'true',
25
23
  verifyAliasOrder: true
26
- }, options)
27
- });
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
+ }
28
45
  function getTagObject(tags, item) {
46
+ var _a, _b, _c, _d;
29
47
  if (item.tag) {
30
48
  const match = tags.filter(t => t.tag === item.tag);
31
49
  if (match.length > 0)
32
- 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];
33
51
  }
34
52
  let tagObj = undefined;
35
53
  let obj;
36
54
  if (isScalar(item)) {
37
55
  obj = item.value;
38
- 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); });
39
57
  tagObj =
40
- 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);
41
59
  }
42
60
  else {
43
61
  obj = item;
44
62
  tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
45
63
  }
46
64
  if (!tagObj) {
47
- // @ts-ignore
48
- 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;
49
66
  throw new Error(`Tag not resolved for ${name} value`);
50
67
  }
51
68
  return tagObj;
52
69
  }
53
70
  // needs to be called before value stringifier to allow for circular anchor refs
54
71
  function stringifyProps(node, tagObj, { anchors, doc }) {
72
+ if (!doc.directives)
73
+ return '';
55
74
  const props = [];
56
75
  const anchor = (isScalar(node) || isCollection(node)) && node.anchor;
57
76
  if (anchor && anchorIsValid(anchor)) {
58
77
  anchors.add(anchor);
59
78
  props.push(`&${anchor}`);
60
79
  }
61
- if (node.tag) {
62
- props.push(doc.directives.tagString(node.tag));
63
- }
64
- else if (!tagObj.default) {
65
- props.push(doc.directives.tagString(tagObj.tag));
66
- }
80
+ const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
81
+ if (tag)
82
+ props.push(doc.directives.tagString(tag));
67
83
  return props.join(' ');
68
84
  }
69
85
  function stringify(item, ctx, onComment, onChompKeep) {
86
+ var _a, _b;
70
87
  if (isPair(item))
71
88
  return item.toString(ctx, onComment, onChompKeep);
72
- if (isAlias(item))
73
- 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
+ }
74
103
  let tagObj = undefined;
75
104
  const node = isNode(item)
76
105
  ? item
@@ -79,7 +108,7 @@ function stringify(item, ctx, onComment, onChompKeep) {
79
108
  tagObj = getTagObject(ctx.doc.schema.tags, node);
80
109
  const props = stringifyProps(node, tagObj, ctx);
81
110
  if (props.length > 0)
82
- ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1;
111
+ ctx.indentAtStart = ((_b = ctx.indentAtStart) !== null && _b !== void 0 ? _b : 0) + props.length + 1;
83
112
  const str = typeof tagObj.stringify === 'function'
84
113
  ? tagObj.stringify(node, ctx, onComment, onChompKeep)
85
114
  : isScalar(node)
@@ -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,7 +96,10 @@ 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 = valueStr === '' && !ctx.inFlow ? vcb : `${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] === '{';
@@ -94,18 +108,18 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
94
108
  }
95
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 };
@@ -147,7 +147,7 @@ function quotedString(value, ctx) {
147
147
  return qs(value, ctx);
148
148
  }
149
149
  function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
150
- const { lineWidth, blockQuote } = ctx.options;
150
+ const { blockQuote, commentString, lineWidth } = ctx.options;
151
151
  // 1. Block can't end in whitespace unless the last line is non-empty.
152
152
  // 2. Strings consisting of only whitespace are best rendered explicitly.
153
153
  if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
@@ -212,7 +212,7 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
212
212
  const indentSize = indent ? '2' : '1'; // root is at -1
213
213
  let header = (literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp;
214
214
  if (comment) {
215
- header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ');
215
+ header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
216
216
  if (onComment)
217
217
  onComment();
218
218
  }
@@ -229,7 +229,6 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
229
229
  return `${header}\n${indent}${body}`;
230
230
  }
231
231
  function plainString(item, ctx, onComment, onChompKeep) {
232
- var _a;
233
232
  const { type, value } = item;
234
233
  const { actualString, implicitKey, indent, inFlow } = ctx;
235
234
  if ((implicitKey && /[\n[\]{},]/.test(value)) ||
@@ -244,14 +243,14 @@ function plainString(item, ctx, onComment, onChompKeep) {
244
243
  // - '\n ', ': ' or ' \n' anywhere
245
244
  // - '#' not preceded by a non-space char
246
245
  // - end with ' ' or ':'
247
- return implicitKey || inFlow || value.indexOf('\n') === -1
246
+ return implicitKey || inFlow || !value.includes('\n')
248
247
  ? quotedString(value, ctx)
249
248
  : blockString(item, ctx, onComment, onChompKeep);
250
249
  }
251
250
  if (!implicitKey &&
252
251
  !inFlow &&
253
252
  type !== Scalar.PLAIN &&
254
- value.indexOf('\n') !== -1) {
253
+ value.includes('\n')) {
255
254
  // Where allowed & type not set explicitly, prefer block style for multiline strings
256
255
  return blockString(item, ctx, onComment, onChompKeep);
257
256
  }
@@ -264,12 +263,10 @@ function plainString(item, ctx, onComment, onChompKeep) {
264
263
  // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
265
264
  // and others in v1.1.
266
265
  if (actualString) {
267
- for (const tag of ctx.doc.schema.tags) {
268
- if (tag.default &&
269
- tag.tag !== 'tag:yaml.org,2002:str' &&
270
- ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str)))
271
- return quotedString(value, ctx);
272
- }
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);
273
270
  }
274
271
  return implicitKey
275
272
  ? str