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
@@ -1,13 +1,12 @@
1
1
  import { anchorIsValid } from '../doc/anchors.js';
2
2
  import { isPair, isAlias, isNode, isScalar, isCollection } from '../nodes/Node.js';
3
+ import { stringifyComment } from './stringifyComment.js';
3
4
  import { stringifyString } from './stringifyString.js';
4
5
 
5
- const createStringifyContext = (doc, options) => ({
6
- anchors: new Set(),
7
- doc,
8
- indent: '',
9
- indentStep: typeof options.indent === 'number' ? ' '.repeat(options.indent) : ' ',
10
- options: Object.assign({
6
+ function createStringifyContext(doc, options) {
7
+ const opt = Object.assign({
8
+ blockQuote: true,
9
+ commentString: stringifyComment,
11
10
  defaultKeyType: null,
12
11
  defaultStringType: 'PLAIN',
13
12
  directives: null,
@@ -19,57 +18,88 @@ const createStringifyContext = (doc, options) => ({
19
18
  minContentWidth: 20,
20
19
  nullStr: 'null',
21
20
  simpleKeys: false,
22
- singleQuote: false,
21
+ singleQuote: null,
23
22
  trueStr: 'true',
24
23
  verifyAliasOrder: true
25
- }, options)
26
- });
24
+ }, doc.schema.toStringOptions, options);
25
+ let inFlow;
26
+ switch (opt.collectionStyle) {
27
+ case 'block':
28
+ inFlow = false;
29
+ break;
30
+ case 'flow':
31
+ inFlow = true;
32
+ break;
33
+ default:
34
+ inFlow = null;
35
+ }
36
+ return {
37
+ anchors: new Set(),
38
+ doc,
39
+ indent: '',
40
+ indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ',
41
+ inFlow,
42
+ options: opt
43
+ };
44
+ }
27
45
  function getTagObject(tags, item) {
46
+ var _a, _b, _c, _d;
28
47
  if (item.tag) {
29
48
  const match = tags.filter(t => t.tag === item.tag);
30
49
  if (match.length > 0)
31
- return match.find(t => t.format === item.format) || match[0];
50
+ return (_a = match.find(t => t.format === item.format)) !== null && _a !== void 0 ? _a : match[0];
32
51
  }
33
52
  let tagObj = undefined;
34
53
  let obj;
35
54
  if (isScalar(item)) {
36
55
  obj = item.value;
37
- const match = tags.filter(t => t.identify && t.identify(obj));
56
+ const match = tags.filter(t => { var _a; return (_a = t.identify) === null || _a === void 0 ? void 0 : _a.call(t, obj); });
38
57
  tagObj =
39
- match.find(t => t.format === item.format) || match.find(t => !t.format);
58
+ (_b = match.find(t => t.format === item.format)) !== null && _b !== void 0 ? _b : match.find(t => !t.format);
40
59
  }
41
60
  else {
42
61
  obj = item;
43
62
  tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
44
63
  }
45
64
  if (!tagObj) {
46
- // @ts-ignore
47
- const name = obj && obj.constructor ? obj.constructor.name : typeof obj;
65
+ const name = (_d = (_c = obj === null || obj === void 0 ? void 0 : obj.constructor) === null || _c === void 0 ? void 0 : _c.name) !== null && _d !== void 0 ? _d : typeof obj;
48
66
  throw new Error(`Tag not resolved for ${name} value`);
49
67
  }
50
68
  return tagObj;
51
69
  }
52
70
  // needs to be called before value stringifier to allow for circular anchor refs
53
71
  function stringifyProps(node, tagObj, { anchors, doc }) {
72
+ if (!doc.directives)
73
+ return '';
54
74
  const props = [];
55
75
  const anchor = (isScalar(node) || isCollection(node)) && node.anchor;
56
76
  if (anchor && anchorIsValid(anchor)) {
57
77
  anchors.add(anchor);
58
78
  props.push(`&${anchor}`);
59
79
  }
60
- if (node.tag) {
61
- props.push(doc.directives.tagString(node.tag));
62
- }
63
- else if (!tagObj.default) {
64
- props.push(doc.directives.tagString(tagObj.tag));
65
- }
80
+ const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
81
+ if (tag)
82
+ props.push(doc.directives.tagString(tag));
66
83
  return props.join(' ');
67
84
  }
68
85
  function stringify(item, ctx, onComment, onChompKeep) {
86
+ var _a, _b;
69
87
  if (isPair(item))
70
88
  return item.toString(ctx, onComment, onChompKeep);
71
- if (isAlias(item))
72
- return item.toString(ctx);
89
+ if (isAlias(item)) {
90
+ if (ctx.doc.directives)
91
+ return item.toString(ctx);
92
+ if ((_a = ctx.resolvedAliases) === null || _a === void 0 ? void 0 : _a.has(item)) {
93
+ throw new TypeError(`Cannot stringify circular structure without alias nodes`);
94
+ }
95
+ else {
96
+ if (ctx.resolvedAliases)
97
+ ctx.resolvedAliases.add(item);
98
+ else
99
+ ctx.resolvedAliases = new Set([item]);
100
+ item = item.resolve(ctx.doc);
101
+ }
102
+ }
73
103
  let tagObj = undefined;
74
104
  const node = isNode(item)
75
105
  ? item
@@ -78,7 +108,7 @@ function stringify(item, ctx, onComment, onChompKeep) {
78
108
  tagObj = getTagObject(ctx.doc.schema.tags, node);
79
109
  const props = stringifyProps(node, tagObj, ctx);
80
110
  if (props.length > 0)
81
- ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1;
111
+ ctx.indentAtStart = ((_b = ctx.indentAtStart) !== null && _b !== void 0 ? _b : 0) + props.length + 1;
82
112
  const str = typeof tagObj.stringify === 'function'
83
113
  ? tagObj.stringify(node, ctx, onComment, onChompKeep)
84
114
  : isScalar(node)
@@ -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 };