yaml 2.0.0-10 → 2.0.0-5

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 (112) hide show
  1. package/browser/dist/compose/compose-doc.js +1 -2
  2. package/browser/dist/compose/compose-node.js +1 -10
  3. package/browser/dist/compose/compose-scalar.js +10 -17
  4. package/browser/dist/compose/composer.js +2 -3
  5. package/browser/dist/compose/resolve-block-map.js +5 -22
  6. package/browser/dist/compose/resolve-block-scalar.js +2 -2
  7. package/browser/dist/compose/resolve-block-seq.js +1 -6
  8. package/browser/dist/compose/resolve-end.js +2 -2
  9. package/browser/dist/compose/resolve-flow-collection.js +8 -24
  10. package/browser/dist/compose/resolve-flow-scalar.js +7 -19
  11. package/browser/dist/compose/resolve-props.js +7 -32
  12. package/browser/dist/doc/Document.js +12 -58
  13. package/browser/dist/doc/createNode.js +5 -11
  14. package/browser/dist/doc/directives.js +0 -5
  15. package/browser/dist/nodes/Collection.js +15 -28
  16. package/browser/dist/nodes/Node.js +0 -7
  17. package/browser/dist/nodes/Pair.js +1 -9
  18. package/browser/dist/nodes/YAMLMap.js +1 -1
  19. package/browser/dist/nodes/YAMLSeq.js +1 -1
  20. package/browser/dist/nodes/addPairToJSMap.js +3 -4
  21. package/browser/dist/options.js +0 -2
  22. package/browser/dist/parse/cst-scalar.js +6 -2
  23. package/browser/dist/parse/lexer.js +31 -67
  24. package/browser/dist/parse/parser.js +55 -62
  25. package/browser/dist/public-api.js +4 -2
  26. package/browser/dist/schema/Schema.js +3 -14
  27. package/browser/dist/schema/core/float.js +1 -4
  28. package/browser/dist/schema/tags.js +13 -17
  29. package/browser/dist/schema/yaml-1.1/float.js +1 -4
  30. package/browser/dist/stringify/addComment.js +11 -0
  31. package/browser/dist/stringify/stringify.js +17 -47
  32. package/browser/dist/stringify/stringifyCollection.js +72 -116
  33. package/browser/dist/stringify/stringifyDocument.js +9 -16
  34. package/browser/dist/stringify/stringifyPair.js +20 -29
  35. package/browser/dist/stringify/stringifyString.js +41 -39
  36. package/browser/dist/util.js +0 -3
  37. package/dist/compose/compose-doc.js +1 -2
  38. package/dist/compose/compose-node.d.ts +0 -1
  39. package/dist/compose/compose-node.js +1 -10
  40. package/dist/compose/compose-scalar.js +9 -16
  41. package/dist/compose/composer.js +2 -3
  42. package/dist/compose/resolve-block-map.d.ts +1 -2
  43. package/dist/compose/resolve-block-map.js +5 -22
  44. package/dist/compose/resolve-block-scalar.js +2 -2
  45. package/dist/compose/resolve-block-seq.js +1 -6
  46. package/dist/compose/resolve-end.js +2 -2
  47. package/dist/compose/resolve-flow-collection.js +8 -24
  48. package/dist/compose/resolve-flow-scalar.js +7 -19
  49. package/dist/compose/resolve-props.d.ts +4 -3
  50. package/dist/compose/resolve-props.js +7 -32
  51. package/dist/doc/Document.d.ts +5 -16
  52. package/dist/doc/Document.js +11 -57
  53. package/dist/doc/createNode.d.ts +1 -2
  54. package/dist/doc/createNode.js +4 -10
  55. package/dist/doc/directives.d.ts +0 -1
  56. package/dist/doc/directives.js +0 -5
  57. package/dist/errors.d.ts +1 -1
  58. package/dist/index.d.ts +2 -2
  59. package/dist/nodes/Alias.d.ts +3 -7
  60. package/dist/nodes/Collection.d.ts +4 -10
  61. package/dist/nodes/Collection.js +14 -27
  62. package/dist/nodes/Node.d.ts +3 -10
  63. package/dist/nodes/Node.js +0 -7
  64. package/dist/nodes/Pair.d.ts +2 -7
  65. package/dist/nodes/Pair.js +0 -8
  66. package/dist/nodes/Scalar.d.ts +0 -2
  67. package/dist/nodes/YAMLMap.d.ts +0 -2
  68. package/dist/nodes/YAMLMap.js +1 -1
  69. package/dist/nodes/YAMLSeq.d.ts +0 -2
  70. package/dist/nodes/YAMLSeq.js +1 -1
  71. package/dist/nodes/addPairToJSMap.js +2 -3
  72. package/dist/options.d.ts +6 -81
  73. package/dist/options.js +0 -2
  74. package/dist/parse/cst-scalar.d.ts +0 -6
  75. package/dist/parse/cst-scalar.js +6 -2
  76. package/dist/parse/lexer.d.ts +1 -3
  77. package/dist/parse/lexer.js +31 -67
  78. package/dist/parse/parser.d.ts +0 -1
  79. package/dist/parse/parser.js +55 -62
  80. package/dist/public-api.js +4 -2
  81. package/dist/schema/Schema.d.ts +4 -6
  82. package/dist/schema/Schema.js +2 -13
  83. package/dist/schema/core/float.js +1 -4
  84. package/dist/schema/tags.d.ts +2 -1
  85. package/dist/schema/tags.js +13 -17
  86. package/dist/schema/yaml-1.1/float.js +1 -4
  87. package/dist/stringify/addComment.d.ts +2 -0
  88. package/dist/stringify/addComment.js +13 -0
  89. package/dist/stringify/stringify.d.ts +3 -5
  90. package/dist/stringify/stringify.js +17 -47
  91. package/dist/stringify/stringifyCollection.d.ts +7 -3
  92. package/dist/stringify/stringifyCollection.js +72 -116
  93. package/dist/stringify/stringifyDocument.js +9 -16
  94. package/dist/stringify/stringifyPair.js +20 -29
  95. package/dist/stringify/stringifyString.js +41 -39
  96. package/dist/test-events.d.ts +1 -1
  97. package/dist/test-events.js +2 -4
  98. package/dist/util.d.ts +0 -3
  99. package/dist/util.js +0 -6
  100. package/package.json +12 -14
  101. package/browser/dist/compose/util-flow-indent-check.js +0 -15
  102. package/browser/dist/compose/util-map-includes.js +0 -17
  103. package/browser/dist/node_modules/tslib/tslib.es6.js +0 -164
  104. package/browser/dist/stringify/stringifyComment.js +0 -18
  105. package/dist/compose/util-flow-indent-check.d.ts +0 -3
  106. package/dist/compose/util-flow-indent-check.js +0 -17
  107. package/dist/compose/util-map-includes.d.ts +0 -4
  108. package/dist/compose/util-map-includes.js +0 -19
  109. package/dist/node_modules/tslib/tslib.es6.js +0 -76
  110. package/dist/stringify/stringifyComment.d.ts +0 -10
  111. package/dist/stringify/stringifyComment.js +0 -22
  112. package/util.d.ts +0 -3
@@ -20,10 +20,7 @@ const floatExp = {
20
20
  format: 'EXP',
21
21
  test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
22
22
  resolve: str => parseFloat(str),
23
- stringify(node) {
24
- const num = Number(node.value);
25
- return isFinite(num) ? num.toExponential() : stringifyNumber(node);
26
- }
23
+ stringify: ({ value }) => Number(value).toExponential()
27
24
  };
28
25
  const float = {
29
26
  identify: value => typeof value === 'number',
@@ -14,13 +14,13 @@ import { schema as schema$2 } from './yaml-1.1/schema.js';
14
14
  import { set } from './yaml-1.1/set.js';
15
15
  import { floatTime, intTime, timestamp } from './yaml-1.1/timestamp.js';
16
16
 
17
- const schemas = new Map([
18
- ['core', schema],
19
- ['failsafe', [map, seq, string]],
20
- ['json', schema$1],
21
- ['yaml11', schema$2],
22
- ['yaml-1.1', schema$2]
23
- ]);
17
+ const schemas = {
18
+ core: schema,
19
+ failsafe: [map, seq, string],
20
+ json: schema$1,
21
+ yaml11: schema$2,
22
+ 'yaml-1.1': schema$2
23
+ };
24
24
  const tagsByName = {
25
25
  binary,
26
26
  bool: boolTag,
@@ -48,17 +48,13 @@ const coreKnownTags = {
48
48
  'tag:yaml.org,2002:timestamp': timestamp
49
49
  };
50
50
  function getTags(customTags, schemaName) {
51
- let tags = schemas.get(schemaName);
51
+ let tags = schemas[schemaName];
52
52
  if (!tags) {
53
- if (Array.isArray(customTags))
54
- tags = [];
55
- else {
56
- const keys = Array.from(schemas.keys())
57
- .filter(key => key !== 'yaml11')
58
- .map(key => JSON.stringify(key))
59
- .join(', ');
60
- throw new Error(`Unknown schema "${schemaName}"; use one of ${keys} or define customTags array`);
61
- }
53
+ const keys = Object.keys(schemas)
54
+ .filter(key => key !== 'yaml11')
55
+ .map(key => JSON.stringify(key))
56
+ .join(', ');
57
+ throw new Error(`Unknown schema "${schemaName}"; use one of ${keys}`);
62
58
  }
63
59
  if (Array.isArray(customTags)) {
64
60
  for (const tag of customTags)
@@ -20,10 +20,7 @@ const floatExp = {
20
20
  format: 'EXP',
21
21
  test: /^[-+]?(?:[0-9][0-9_]*)?(?:\.[0-9_]*)?[eE][-+]?[0-9]+$/,
22
22
  resolve: (str) => parseFloat(str.replace(/_/g, '')),
23
- stringify(node) {
24
- const num = Number(node.value);
25
- return isFinite(num) ? num.toExponential() : stringifyNumber(node);
26
- }
23
+ stringify: ({ value }) => Number(value).toExponential()
27
24
  };
28
25
  const float = {
29
26
  identify: value => typeof value === 'number',
@@ -0,0 +1,11 @@
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,12 +1,13 @@
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';
4
3
  import { stringifyString } from './stringifyString.js';
5
4
 
6
- function createStringifyContext(doc, options) {
7
- const opt = Object.assign({
8
- blockQuote: true,
9
- commentString: stringifyComment,
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({
10
11
  defaultKeyType: null,
11
12
  defaultStringType: 'PLAIN',
12
13
  directives: null,
@@ -18,30 +19,11 @@ function createStringifyContext(doc, options) {
18
19
  minContentWidth: 20,
19
20
  nullStr: 'null',
20
21
  simpleKeys: false,
21
- singleQuote: null,
22
+ singleQuote: false,
22
23
  trueStr: 'true',
23
24
  verifyAliasOrder: true
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
- }
25
+ }, options)
26
+ });
45
27
  function getTagObject(tags, item) {
46
28
  if (item.tag) {
47
29
  const match = tags.filter(t => t.tag === item.tag);
@@ -69,37 +51,25 @@ function getTagObject(tags, item) {
69
51
  }
70
52
  // needs to be called before value stringifier to allow for circular anchor refs
71
53
  function stringifyProps(node, tagObj, { anchors, doc }) {
72
- if (!doc.directives)
73
- return '';
74
54
  const props = [];
75
55
  const anchor = (isScalar(node) || isCollection(node)) && node.anchor;
76
56
  if (anchor && anchorIsValid(anchor)) {
77
57
  anchors.add(anchor);
78
58
  props.push(`&${anchor}`);
79
59
  }
80
- const tag = node.tag || (tagObj.default ? null : tagObj.tag);
81
- if (tag)
82
- props.push(doc.directives.tagString(tag));
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
+ }
83
66
  return props.join(' ');
84
67
  }
85
68
  function stringify(item, ctx, onComment, onChompKeep) {
86
- var _a;
87
69
  if (isPair(item))
88
70
  return item.toString(ctx, onComment, onChompKeep);
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
- }
71
+ if (isAlias(item))
72
+ return item.toString(ctx);
103
73
  let tagObj = undefined;
104
74
  const node = isNode(item)
105
75
  ? item
@@ -1,152 +1,108 @@
1
1
  import { Collection } from '../nodes/Collection.js';
2
- import { isNode, isPair } from '../nodes/Node.js';
2
+ import { addComment } from './addComment.js';
3
3
  import { stringify } from './stringify.js';
4
- import { lineComment, indentComment } from './stringifyComment.js';
4
+ import { isNode, isPair } from '../nodes/Node.js';
5
5
 
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 });
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;
15
13
  let chompKeep = false; // flag for the preceding node's status
16
- const lines = [];
17
- for (let i = 0; i < items.length; ++i) {
18
- const item = items[i];
14
+ const nodes = items.reduce((nodes, item, i) => {
19
15
  let comment = null;
20
16
  if (isNode(item)) {
21
17
  if (!chompKeep && item.spaceBefore)
22
- lines.push('');
23
- addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
24
- if (item.comment)
18
+ nodes.push({ comment: true, str: '' });
19
+ if (item.commentBefore) {
20
+ // This match will always succeed on a non-empty string
21
+ for (const line of item.commentBefore.match(/^.*$/gm))
22
+ nodes.push({ comment: true, str: `#${line}` });
23
+ }
24
+ if (item.comment) {
25
25
  comment = item.comment;
26
+ singleLineOutput = false;
27
+ }
26
28
  }
27
29
  else if (isPair(item)) {
28
30
  const ik = isNode(item.key) ? item.key : null;
29
31
  if (ik) {
30
32
  if (!chompKeep && ik.spaceBefore)
31
- lines.push('');
32
- addCommentBefore(ctx, lines, ik.commentBefore, chompKeep);
33
+ nodes.push({ comment: true, str: '' });
34
+ if (ik.commentBefore) {
35
+ // This match will always succeed on a non-empty string
36
+ for (const line of ik.commentBefore.match(/^.*$/gm))
37
+ nodes.push({ comment: true, str: `#${line}` });
38
+ }
39
+ if (ik.comment)
40
+ singleLineOutput = false;
41
+ }
42
+ if (inFlow) {
43
+ const iv = isNode(item.value) ? item.value : null;
44
+ if (iv) {
45
+ if (iv.comment)
46
+ comment = iv.comment;
47
+ if (iv.comment || iv.commentBefore)
48
+ singleLineOutput = false;
49
+ }
50
+ else if (item.value == null && ik && ik.comment) {
51
+ comment = ik.comment;
52
+ }
33
53
  }
34
54
  }
35
55
  chompKeep = false;
36
- let str = stringify(item, itemCtx, () => (comment = null), () => (chompKeep = true));
37
- if (comment)
38
- str += lineComment(str, itemIndent, commentString(comment));
39
- if (chompKeep && comment)
56
+ let str = stringify(item, ctx, () => (comment = null), () => (chompKeep = true));
57
+ if (inFlow && i < items.length - 1)
58
+ str += ',';
59
+ str = addComment(str, itemIndent, comment);
60
+ if (chompKeep && (comment || inFlow))
40
61
  chompKeep = false;
41
- lines.push(blockItemPrefix + str);
42
- }
62
+ nodes.push({ comment: false, str });
63
+ return nodes;
64
+ }, []);
43
65
  let str;
44
- if (lines.length === 0) {
66
+ if (nodes.length === 0) {
45
67
  str = flowChars.start + flowChars.end;
46
68
  }
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;
69
+ else if (inFlow) {
70
+ const { start, end } = flowChars;
71
+ const strings = nodes.map(n => n.str);
72
+ let singleLineLength = 2;
73
+ for (const node of nodes) {
74
+ if (node.comment || node.str.includes('\n')) {
75
+ singleLineOutput = false;
76
+ break;
92
77
  }
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;
102
- }
103
- }
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;
78
+ singleLineLength += node.str.length + 2;
125
79
  }
126
- if (reqNewline) {
80
+ if (!singleLineOutput ||
81
+ singleLineLength > Collection.maxFlowStringSingleLineLength) {
127
82
  str = start;
128
- for (const line of lines)
129
- str += line ? `\n${indentStep}${indent}${line}` : '\n';
83
+ for (const s of strings) {
84
+ str += s ? `\n${indentStep}${indent}${s}` : '\n';
85
+ }
130
86
  str += `\n${indent}${end}`;
131
87
  }
132
88
  else {
133
- str = `${start} ${lines.join(' ')} ${end}`;
89
+ str = `${start} ${strings.join(' ')} ${end}`;
134
90
  }
135
91
  }
92
+ else {
93
+ const strings = nodes.map(blockItem);
94
+ str = strings.shift() || '';
95
+ for (const s of strings)
96
+ str += s ? `\n${indent}${s}` : '\n';
97
+ }
136
98
  if (comment) {
137
- str += lineComment(str, commentString(comment), indent);
99
+ str += '\n' + comment.replace(/^/gm, `${indent}#`);
138
100
  if (onComment)
139
101
  onComment();
140
102
  }
103
+ else if (chompKeep && onChompKeep)
104
+ onChompKeep();
141
105
  return str;
142
106
  }
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
- }
151
107
 
152
108
  export { stringifyCollection };
@@ -1,11 +1,11 @@
1
1
  import { isNode } from '../nodes/Node.js';
2
+ import { addComment } from './addComment.js';
2
3
  import { createStringifyContext, stringify } from './stringify.js';
3
- import { indentComment, lineComment } from './stringifyComment.js';
4
4
 
5
5
  function stringifyDocument(doc, options) {
6
6
  const lines = [];
7
7
  let hasDirectives = options.directives === true;
8
- if (options.directives !== false && doc.directives) {
8
+ if (options.directives !== false) {
9
9
  const dir = doc.directives.toString(doc);
10
10
  if (dir) {
11
11
  lines.push(dir);
@@ -16,24 +16,20 @@ function stringifyDocument(doc, options) {
16
16
  }
17
17
  if (hasDirectives)
18
18
  lines.push('---');
19
- const ctx = createStringifyContext(doc, options);
20
- const { commentString } = ctx.options;
21
19
  if (doc.commentBefore) {
22
20
  if (lines.length !== 1)
23
21
  lines.unshift('');
24
- const cs = commentString(doc.commentBefore);
25
- lines.unshift(indentComment(cs, ''));
22
+ lines.unshift(doc.commentBefore.replace(/^/gm, '#'));
26
23
  }
24
+ const ctx = createStringifyContext(doc, options);
27
25
  let chompKeep = false;
28
26
  let contentComment = null;
29
27
  if (doc.contents) {
30
28
  if (isNode(doc.contents)) {
31
29
  if (doc.contents.spaceBefore && hasDirectives)
32
30
  lines.push('');
33
- if (doc.contents.commentBefore) {
34
- const cs = commentString(doc.contents.commentBefore);
35
- lines.push(indentComment(cs, ''));
36
- }
31
+ if (doc.contents.commentBefore)
32
+ lines.push(doc.contents.commentBefore.replace(/^/gm, '#'));
37
33
  // top-level block scalars need to be indented if followed by a comment
38
34
  ctx.forceBlockIndent = !!doc.comment;
39
35
  contentComment = doc.contents.comment;
@@ -41,7 +37,7 @@ function stringifyDocument(doc, options) {
41
37
  const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
42
38
  let body = stringify(doc.contents, ctx, () => (contentComment = null), onChompKeep);
43
39
  if (contentComment)
44
- body += lineComment(body, '', commentString(contentComment));
40
+ body = addComment(body, '', contentComment);
45
41
  if ((body[0] === '|' || body[0] === '>') &&
46
42
  lines[lines.length - 1] === '---') {
47
43
  // Top-level block scalars with a preceding doc marker ought to use the
@@ -54,13 +50,10 @@ function stringifyDocument(doc, options) {
54
50
  else {
55
51
  lines.push(stringify(doc.contents, ctx));
56
52
  }
57
- let dc = doc.comment;
58
- if (dc && chompKeep)
59
- dc = dc.replace(/^\n+/, '');
60
- if (dc) {
53
+ if (doc.comment) {
61
54
  if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
62
55
  lines.push('');
63
- lines.push(indentComment(commentString(dc), ''));
56
+ lines.push(doc.comment.replace(/^/gm, '#'));
64
57
  }
65
58
  return lines.join('\n') + '\n';
66
59
  }
@@ -1,10 +1,10 @@
1
1
  import { isCollection, isNode, isScalar, isSeq } from '../nodes/Node.js';
2
2
  import { Scalar } from '../nodes/Scalar.js';
3
+ import { addComment } from './addComment.js';
3
4
  import { stringify } from './stringify.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: { commentString, indentSeq, simpleKeys } } = ctx;
7
+ const { allNullValues, doc, indent, indentStep, options: { indentSeq, simpleKeys } } = ctx;
8
8
  let keyComment = (isNode(key) && key.comment) || null;
9
9
  if (simpleKeys) {
10
10
  if (keyComment) {
@@ -43,34 +43,25 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
43
43
  }
44
44
  }
45
45
  else if ((allNullValues && !simpleKeys) || (value == null && explicitKey)) {
46
- str = `? ${str}`;
47
- if (keyComment && !keyCommentDone) {
48
- str += lineComment(str, ctx.indent, commentString(keyComment));
49
- }
50
- else if (chompKeep && onChompKeep)
46
+ if (keyCommentDone)
47
+ keyComment = null;
48
+ if (chompKeep && !keyComment && onChompKeep)
51
49
  onChompKeep();
52
- return str;
50
+ return addComment(`? ${str}`, ctx.indent, keyComment);
53
51
  }
54
52
  if (keyCommentDone)
55
53
  keyComment = null;
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
- }
54
+ str = explicitKey
55
+ ? `? ${addComment(str, ctx.indent, keyComment)}\n${indent}:`
56
+ : addComment(`${str}:`, ctx.indent, keyComment);
66
57
  let vcb = '';
67
58
  let valueComment = null;
68
59
  if (isNode(value)) {
69
60
  if (value.spaceBefore)
70
61
  vcb = '\n';
71
62
  if (value.commentBefore) {
72
- const cs = commentString(value.commentBefore);
73
- vcb += `\n${indentComment(cs, ctx.indent)}`;
63
+ const cs = value.commentBefore.replace(/^/gm, `${ctx.indent}#`);
64
+ vcb += `\n${cs}`;
74
65
  }
75
66
  valueComment = value.comment;
76
67
  }
@@ -96,27 +87,27 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
96
87
  const valueStr = stringify(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true));
97
88
  let ws = ' ';
98
89
  if (vcb || keyComment) {
99
- ws = valueStr === '' && !ctx.inFlow ? vcb : `${vcb}\n${ctx.indent}`;
90
+ ws = `${vcb}\n${ctx.indent}`;
100
91
  }
101
92
  else if (!explicitKey && isCollection(value)) {
102
93
  const flow = valueStr[0] === '[' || valueStr[0] === '{';
103
94
  if (!flow || valueStr.includes('\n'))
104
95
  ws = `\n${ctx.indent}`;
105
96
  }
106
- else if (valueStr === '' || valueStr[0] === '\n')
97
+ else if (valueStr[0] === '\n')
107
98
  ws = '';
108
- str += ws + valueStr;
109
99
  if (ctx.inFlow) {
110
100
  if (valueCommentDone && onComment)
111
101
  onComment();
102
+ return str + ws + valueStr;
112
103
  }
113
- else if (valueComment && !valueCommentDone) {
114
- str += lineComment(str, ctx.indent, commentString(valueComment));
115
- }
116
- else if (chompKeep && onChompKeep) {
117
- onChompKeep();
104
+ else {
105
+ if (valueCommentDone)
106
+ valueComment = null;
107
+ if (chompKeep && !valueComment && onChompKeep)
108
+ onChompKeep();
109
+ return addComment(str + ws + valueStr, ctx.indent, valueComment);
118
110
  }
119
- return str;
120
111
  }
121
112
 
122
113
  export { stringifyPair };