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
@@ -2,14 +2,13 @@
2
2
 
3
3
  var anchors = require('../doc/anchors.js');
4
4
  var Node = require('../nodes/Node.js');
5
+ var stringifyComment = require('./stringifyComment.js');
5
6
  var stringifyString = require('./stringifyString.js');
6
7
 
7
- const createStringifyContext = (doc, options) => ({
8
- anchors: new Set(),
9
- doc,
10
- indent: '',
11
- indentStep: typeof options.indent === 'number' ? ' '.repeat(options.indent) : ' ',
12
- options: Object.assign({
8
+ function createStringifyContext(doc, options) {
9
+ const opt = Object.assign({
10
+ blockQuote: true,
11
+ commentString: stringifyComment.stringifyComment,
13
12
  defaultKeyType: null,
14
13
  defaultStringType: 'PLAIN',
15
14
  directives: null,
@@ -21,57 +20,88 @@ const createStringifyContext = (doc, options) => ({
21
20
  minContentWidth: 20,
22
21
  nullStr: 'null',
23
22
  simpleKeys: false,
24
- singleQuote: false,
23
+ singleQuote: null,
25
24
  trueStr: 'true',
26
25
  verifyAliasOrder: true
27
- }, options)
28
- });
26
+ }, doc.schema.toStringOptions, options);
27
+ let inFlow;
28
+ switch (opt.collectionStyle) {
29
+ case 'block':
30
+ inFlow = false;
31
+ break;
32
+ case 'flow':
33
+ inFlow = true;
34
+ break;
35
+ default:
36
+ inFlow = null;
37
+ }
38
+ return {
39
+ anchors: new Set(),
40
+ doc,
41
+ indent: '',
42
+ indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ',
43
+ inFlow,
44
+ options: opt
45
+ };
46
+ }
29
47
  function getTagObject(tags, item) {
48
+ var _a, _b, _c, _d;
30
49
  if (item.tag) {
31
50
  const match = tags.filter(t => t.tag === item.tag);
32
51
  if (match.length > 0)
33
- return match.find(t => t.format === item.format) || match[0];
52
+ return (_a = match.find(t => t.format === item.format)) !== null && _a !== void 0 ? _a : match[0];
34
53
  }
35
54
  let tagObj = undefined;
36
55
  let obj;
37
56
  if (Node.isScalar(item)) {
38
57
  obj = item.value;
39
- const match = tags.filter(t => t.identify && t.identify(obj));
58
+ const match = tags.filter(t => { var _a; return (_a = t.identify) === null || _a === void 0 ? void 0 : _a.call(t, obj); });
40
59
  tagObj =
41
- match.find(t => t.format === item.format) || match.find(t => !t.format);
60
+ (_b = match.find(t => t.format === item.format)) !== null && _b !== void 0 ? _b : match.find(t => !t.format);
42
61
  }
43
62
  else {
44
63
  obj = item;
45
64
  tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
46
65
  }
47
66
  if (!tagObj) {
48
- // @ts-ignore
49
- const name = obj && obj.constructor ? obj.constructor.name : typeof obj;
67
+ 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;
50
68
  throw new Error(`Tag not resolved for ${name} value`);
51
69
  }
52
70
  return tagObj;
53
71
  }
54
72
  // needs to be called before value stringifier to allow for circular anchor refs
55
73
  function stringifyProps(node, tagObj, { anchors: anchors$1, doc }) {
74
+ if (!doc.directives)
75
+ return '';
56
76
  const props = [];
57
77
  const anchor = (Node.isScalar(node) || Node.isCollection(node)) && node.anchor;
58
78
  if (anchor && anchors.anchorIsValid(anchor)) {
59
79
  anchors$1.add(anchor);
60
80
  props.push(`&${anchor}`);
61
81
  }
62
- if (node.tag) {
63
- props.push(doc.directives.tagString(node.tag));
64
- }
65
- else if (!tagObj.default) {
66
- props.push(doc.directives.tagString(tagObj.tag));
67
- }
82
+ const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
83
+ if (tag)
84
+ props.push(doc.directives.tagString(tag));
68
85
  return props.join(' ');
69
86
  }
70
87
  function stringify(item, ctx, onComment, onChompKeep) {
88
+ var _a, _b;
71
89
  if (Node.isPair(item))
72
90
  return item.toString(ctx, onComment, onChompKeep);
73
- if (Node.isAlias(item))
74
- return item.toString(ctx);
91
+ if (Node.isAlias(item)) {
92
+ if (ctx.doc.directives)
93
+ return item.toString(ctx);
94
+ if ((_a = ctx.resolvedAliases) === null || _a === void 0 ? void 0 : _a.has(item)) {
95
+ throw new TypeError(`Cannot stringify circular structure without alias nodes`);
96
+ }
97
+ else {
98
+ if (ctx.resolvedAliases)
99
+ ctx.resolvedAliases.add(item);
100
+ else
101
+ ctx.resolvedAliases = new Set([item]);
102
+ item = item.resolve(ctx.doc);
103
+ }
104
+ }
75
105
  let tagObj = undefined;
76
106
  const node = Node.isNode(item)
77
107
  ? item
@@ -80,7 +110,7 @@ function stringify(item, ctx, onComment, onChompKeep) {
80
110
  tagObj = getTagObject(ctx.doc.schema.tags, node);
81
111
  const props = stringifyProps(node, tagObj, ctx);
82
112
  if (props.length > 0)
83
- ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1;
113
+ ctx.indentAtStart = ((_b = ctx.indentAtStart) !== null && _b !== void 0 ? _b : 0) + props.length + 1;
84
114
  const str = typeof tagObj.stringify === 'function'
85
115
  ? tagObj.stringify(node, ctx, onComment, onChompKeep)
86
116
  : Node.isScalar(node)
@@ -1,11 +1,7 @@
1
1
  import { Collection } from '../nodes/Collection.js';
2
2
  import { StringifyContext } from './stringify.js';
3
- declare type StringifyNode = {
4
- comment: boolean;
5
- str: string;
6
- };
7
3
  interface StringifyCollectionOptions {
8
- blockItem(node: StringifyNode): string;
4
+ blockItemPrefix: string;
9
5
  flowChars: {
10
6
  start: '{';
11
7
  end: '}';
@@ -17,5 +13,5 @@ interface StringifyCollectionOptions {
17
13
  onChompKeep?: () => void;
18
14
  onComment?: () => void;
19
15
  }
20
- export declare function stringifyCollection({ comment, flow, items }: Readonly<Collection>, ctx: StringifyContext, { blockItem, flowChars, itemIndent, onChompKeep, onComment }: StringifyCollectionOptions): string;
16
+ export declare function stringifyCollection(collection: Readonly<Collection>, ctx: StringifyContext, options: StringifyCollectionOptions): string;
21
17
  export {};
@@ -5,120 +5,150 @@ var Node = require('../nodes/Node.js');
5
5
  var stringify = require('./stringify.js');
6
6
  var stringifyComment = require('./stringifyComment.js');
7
7
 
8
- function stringifyCollection({ comment, flow, items }, ctx, { blockItem, flowChars, itemIndent, onChompKeep, onComment }) {
9
- const { indent, indentStep } = ctx;
10
- const inFlow = flow || ctx.inFlow;
11
- if (inFlow)
12
- itemIndent += indentStep;
13
- ctx = Object.assign({}, ctx, { indent: itemIndent, inFlow, type: null });
14
- let singleLineOutput = true;
8
+ function stringifyCollection(collection, ctx, options) {
9
+ var _a;
10
+ const flow = (_a = ctx.inFlow) !== null && _a !== void 0 ? _a : collection.flow;
11
+ const stringify = flow ? stringifyFlowCollection : stringifyBlockCollection;
12
+ return stringify(collection, ctx, options);
13
+ }
14
+ function stringifyBlockCollection({ comment, items }, ctx, { blockItemPrefix, flowChars, itemIndent, onChompKeep, onComment }) {
15
+ const { indent, options: { commentString } } = ctx;
16
+ const itemCtx = Object.assign({}, ctx, { indent: itemIndent, type: null });
15
17
  let chompKeep = false; // flag for the preceding node's status
16
- const nodes = items.reduce((nodes, item, i) => {
18
+ const lines = [];
19
+ for (let i = 0; i < items.length; ++i) {
20
+ const item = items[i];
17
21
  let comment = null;
18
22
  if (Node.isNode(item)) {
19
23
  if (!chompKeep && item.spaceBefore)
20
- nodes.push({ comment: true, str: '' });
21
- let cb = item.commentBefore;
22
- if (cb && chompKeep)
23
- cb = cb.replace(/^\n+/, '');
24
- if (cb) {
25
- if (/^\n+$/.test(cb))
26
- cb = cb.substring(1);
27
- // This match will always succeed on a non-empty string
28
- for (const line of cb.match(/^.*$/gm)) {
29
- const str = line === ' ' ? '#' : line ? `#${line}` : '';
30
- nodes.push({ comment: true, str });
31
- }
32
- }
33
- if (item.comment) {
24
+ lines.push('');
25
+ addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
26
+ if (item.comment)
34
27
  comment = item.comment;
35
- singleLineOutput = false;
36
- }
37
28
  }
38
29
  else if (Node.isPair(item)) {
39
30
  const ik = Node.isNode(item.key) ? item.key : null;
40
31
  if (ik) {
41
32
  if (!chompKeep && ik.spaceBefore)
42
- nodes.push({ comment: true, str: '' });
43
- let cb = ik.commentBefore;
44
- if (cb && chompKeep)
45
- cb = cb.replace(/^\n+/, '');
46
- if (cb) {
47
- if (/^\n+$/.test(cb))
48
- cb = cb.substring(1);
49
- // This match will always succeed on a non-empty string
50
- for (const line of cb.match(/^.*$/gm)) {
51
- const str = line === ' ' ? '#' : line ? `#${line}` : '';
52
- nodes.push({ comment: true, str });
53
- }
54
- }
55
- if (ik.comment)
56
- singleLineOutput = false;
57
- }
58
- if (inFlow) {
59
- const iv = Node.isNode(item.value) ? item.value : null;
60
- if (iv) {
61
- if (iv.comment)
62
- comment = iv.comment;
63
- if (iv.comment || iv.commentBefore)
64
- singleLineOutput = false;
65
- }
66
- else if (item.value == null && ik && ik.comment) {
67
- comment = ik.comment;
68
- }
33
+ lines.push('');
34
+ addCommentBefore(ctx, lines, ik.commentBefore, chompKeep);
69
35
  }
70
36
  }
71
37
  chompKeep = false;
72
- let str = stringify.stringify(item, ctx, () => (comment = null), () => (chompKeep = true));
73
- if (inFlow && i < items.length - 1)
74
- str += ',';
75
- str = stringifyComment.addComment(str, itemIndent, comment);
76
- if (chompKeep && (comment || inFlow))
38
+ let str = stringify.stringify(item, itemCtx, () => (comment = null), () => (chompKeep = true));
39
+ if (comment)
40
+ str += stringifyComment.lineComment(str, itemIndent, commentString(comment));
41
+ if (chompKeep && comment)
77
42
  chompKeep = false;
78
- nodes.push({ comment: false, str });
79
- return nodes;
80
- }, []);
43
+ lines.push(blockItemPrefix + str);
44
+ }
81
45
  let str;
82
- if (nodes.length === 0) {
46
+ if (lines.length === 0) {
83
47
  str = flowChars.start + flowChars.end;
84
48
  }
85
- else if (inFlow) {
86
- const { start, end } = flowChars;
87
- const strings = nodes.map(n => n.str);
88
- let singleLineLength = 2;
89
- for (const node of nodes) {
90
- if (node.comment || node.str.includes('\n')) {
91
- singleLineOutput = false;
92
- break;
49
+ else {
50
+ str = lines[0];
51
+ for (let i = 1; i < lines.length; ++i) {
52
+ const line = lines[i];
53
+ str += line ? `\n${indent}${line}` : '\n';
54
+ }
55
+ }
56
+ if (comment) {
57
+ str += '\n' + stringifyComment.indentComment(commentString(comment), indent);
58
+ if (onComment)
59
+ onComment();
60
+ }
61
+ else if (chompKeep && onChompKeep)
62
+ onChompKeep();
63
+ return str;
64
+ }
65
+ function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemIndent, onComment }) {
66
+ const { indent, indentStep, options: { commentString } } = ctx;
67
+ itemIndent += indentStep;
68
+ const itemCtx = Object.assign({}, ctx, {
69
+ indent: itemIndent,
70
+ inFlow: true,
71
+ type: null
72
+ });
73
+ let reqNewline = false;
74
+ let linesAtValue = 0;
75
+ const lines = [];
76
+ for (let i = 0; i < items.length; ++i) {
77
+ const item = items[i];
78
+ let comment = null;
79
+ if (Node.isNode(item)) {
80
+ if (item.spaceBefore)
81
+ lines.push('');
82
+ addCommentBefore(ctx, lines, item.commentBefore, false);
83
+ if (item.comment)
84
+ comment = item.comment;
85
+ }
86
+ else if (Node.isPair(item)) {
87
+ const ik = Node.isNode(item.key) ? item.key : null;
88
+ if (ik) {
89
+ if (ik.spaceBefore)
90
+ lines.push('');
91
+ addCommentBefore(ctx, lines, ik.commentBefore, false);
92
+ if (ik.comment)
93
+ reqNewline = true;
94
+ }
95
+ const iv = Node.isNode(item.value) ? item.value : null;
96
+ if (iv) {
97
+ if (iv.comment)
98
+ comment = iv.comment;
99
+ if (iv.commentBefore)
100
+ reqNewline = true;
101
+ }
102
+ else if (item.value == null && ik && ik.comment) {
103
+ comment = ik.comment;
93
104
  }
94
- singleLineLength += node.str.length + 2;
95
105
  }
96
- if (!singleLineOutput ||
97
- singleLineLength > Collection.Collection.maxFlowStringSingleLineLength) {
106
+ if (comment)
107
+ reqNewline = true;
108
+ let str = stringify.stringify(item, itemCtx, () => (comment = null));
109
+ if (i < items.length - 1)
110
+ str += ',';
111
+ if (comment)
112
+ str += stringifyComment.lineComment(str, itemIndent, commentString(comment));
113
+ if (!reqNewline && (lines.length > linesAtValue || str.includes('\n')))
114
+ reqNewline = true;
115
+ lines.push(str);
116
+ linesAtValue = lines.length;
117
+ }
118
+ let str;
119
+ const { start, end } = flowChars;
120
+ if (lines.length === 0) {
121
+ str = start + end;
122
+ }
123
+ else {
124
+ if (!reqNewline) {
125
+ const len = lines.reduce((sum, line) => sum + line.length + 2, 2);
126
+ reqNewline = len > Collection.Collection.maxFlowStringSingleLineLength;
127
+ }
128
+ if (reqNewline) {
98
129
  str = start;
99
- for (const s of strings) {
100
- str += s ? `\n${indentStep}${indent}${s}` : '\n';
101
- }
130
+ for (const line of lines)
131
+ str += line ? `\n${indentStep}${indent}${line}` : '\n';
102
132
  str += `\n${indent}${end}`;
103
133
  }
104
134
  else {
105
- str = `${start} ${strings.join(' ')} ${end}`;
135
+ str = `${start} ${lines.join(' ')} ${end}`;
106
136
  }
107
137
  }
108
- else {
109
- const strings = nodes.map(blockItem);
110
- str = strings.shift() || '';
111
- for (const s of strings)
112
- str += s ? `\n${indent}${s}` : '\n';
113
- }
114
138
  if (comment) {
115
- str += '\n' + stringifyComment.stringifyComment(comment, indent);
139
+ str += stringifyComment.lineComment(str, commentString(comment), indent);
116
140
  if (onComment)
117
141
  onComment();
118
142
  }
119
- else if (chompKeep && onChompKeep)
120
- onChompKeep();
121
143
  return str;
122
144
  }
145
+ function addCommentBefore({ indent, options: { commentString } }, lines, comment, chompKeep) {
146
+ if (comment && chompKeep)
147
+ comment = comment.replace(/^\n+/, '');
148
+ if (comment) {
149
+ const ic = stringifyComment.indentComment(commentString(comment), indent);
150
+ lines.push(ic.trimStart()); // Avoid double indent on first line
151
+ }
152
+ }
123
153
 
124
154
  exports.stringifyCollection = stringifyCollection;
@@ -1,2 +1,10 @@
1
- export declare const stringifyComment: (comment: string, indent: string) => string;
2
- export declare function addComment(str: string, indent: string, comment?: string | null): string;
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
+ export declare const stringifyComment: (str: string) => string;
9
+ export declare function indentComment(comment: string, indent: string): string;
10
+ export declare const lineComment: (str: string, indent: string, comment: string) => string;
@@ -1,17 +1,24 @@
1
1
  'use strict';
2
2
 
3
- const stringifyComment = (comment, indent) => /^\n+$/.test(comment)
4
- ? comment.substring(1)
5
- : comment.replace(/^(?!$)(?: $)?/gm, `${indent}#`);
6
- function addComment(str, indent, comment) {
7
- return !comment
8
- ? str
9
- : comment.includes('\n')
10
- ? `${str}\n` + stringifyComment(comment, indent)
11
- : str.endsWith(' ')
12
- ? `${str}#${comment}`
13
- : `${str} #${comment}`;
3
+ /**
4
+ * Stringifies a comment.
5
+ *
6
+ * Empty comment lines are left empty,
7
+ * lines consisting of a single space are replaced by `#`,
8
+ * and all other lines are prefixed with a `#`.
9
+ */
10
+ const stringifyComment = (str) => str.replace(/^(?!$)(?: $)?/gm, '#');
11
+ function indentComment(comment, indent) {
12
+ if (/^\n+$/.test(comment))
13
+ return comment.substring(1);
14
+ return indent ? comment.replace(/^(?! *$)/gm, indent) : comment;
14
15
  }
16
+ const lineComment = (str, indent, comment) => str.endsWith('\n')
17
+ ? indentComment(comment, indent)
18
+ : comment.includes('\n')
19
+ ? '\n' + indentComment(comment, indent)
20
+ : (str.endsWith(' ') ? '' : ' ') + comment;
15
21
 
16
- exports.addComment = addComment;
22
+ exports.indentComment = indentComment;
23
+ exports.lineComment = lineComment;
17
24
  exports.stringifyComment = stringifyComment;
@@ -5,33 +5,38 @@ var stringify = require('./stringify.js');
5
5
  var stringifyComment = require('./stringifyComment.js');
6
6
 
7
7
  function stringifyDocument(doc, options) {
8
+ var _a;
8
9
  const lines = [];
9
10
  let hasDirectives = options.directives === true;
10
- if (options.directives !== false) {
11
+ if (options.directives !== false && doc.directives) {
11
12
  const dir = doc.directives.toString(doc);
12
13
  if (dir) {
13
14
  lines.push(dir);
14
15
  hasDirectives = true;
15
16
  }
16
- else if (doc.directives.marker)
17
+ else if (doc.directives.docStart)
17
18
  hasDirectives = true;
18
19
  }
19
20
  if (hasDirectives)
20
21
  lines.push('---');
22
+ const ctx = stringify.createStringifyContext(doc, options);
23
+ const { commentString } = ctx.options;
21
24
  if (doc.commentBefore) {
22
25
  if (lines.length !== 1)
23
26
  lines.unshift('');
24
- lines.unshift(stringifyComment.stringifyComment(doc.commentBefore, ''));
27
+ const cs = commentString(doc.commentBefore);
28
+ lines.unshift(stringifyComment.indentComment(cs, ''));
25
29
  }
26
- const ctx = stringify.createStringifyContext(doc, options);
27
30
  let chompKeep = false;
28
31
  let contentComment = null;
29
32
  if (doc.contents) {
30
33
  if (Node.isNode(doc.contents)) {
31
34
  if (doc.contents.spaceBefore && hasDirectives)
32
35
  lines.push('');
33
- if (doc.contents.commentBefore)
34
- lines.push(stringifyComment.stringifyComment(doc.contents.commentBefore, ''));
36
+ if (doc.contents.commentBefore) {
37
+ const cs = commentString(doc.contents.commentBefore);
38
+ lines.push(stringifyComment.indentComment(cs, ''));
39
+ }
35
40
  // top-level block scalars need to be indented if followed by a comment
36
41
  ctx.forceBlockIndent = !!doc.comment;
37
42
  contentComment = doc.contents.comment;
@@ -39,7 +44,7 @@ function stringifyDocument(doc, options) {
39
44
  const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
40
45
  let body = stringify.stringify(doc.contents, ctx, () => (contentComment = null), onChompKeep);
41
46
  if (contentComment)
42
- body = stringifyComment.addComment(body, '', contentComment);
47
+ body += stringifyComment.lineComment(body, '', commentString(contentComment));
43
48
  if ((body[0] === '|' || body[0] === '>') &&
44
49
  lines[lines.length - 1] === '---') {
45
50
  // Top-level block scalars with a preceding doc marker ought to use the
@@ -52,13 +57,30 @@ function stringifyDocument(doc, options) {
52
57
  else {
53
58
  lines.push(stringify.stringify(doc.contents, ctx));
54
59
  }
55
- let dc = doc.comment;
56
- if (dc && chompKeep)
57
- dc = dc.replace(/^\n+/, '');
58
- if (dc) {
59
- if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
60
- lines.push('');
61
- lines.push(stringifyComment.stringifyComment(dc, ''));
60
+ if ((_a = doc.directives) === null || _a === void 0 ? void 0 : _a.docEnd) {
61
+ if (doc.comment) {
62
+ const cs = commentString(doc.comment);
63
+ if (cs.includes('\n')) {
64
+ lines.push('...');
65
+ lines.push(stringifyComment.indentComment(cs, ''));
66
+ }
67
+ else {
68
+ lines.push(`... ${cs}`);
69
+ }
70
+ }
71
+ else {
72
+ lines.push('...');
73
+ }
74
+ }
75
+ else {
76
+ let dc = doc.comment;
77
+ if (dc && chompKeep)
78
+ dc = dc.replace(/^\n+/, '');
79
+ if (dc) {
80
+ if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
81
+ lines.push('');
82
+ lines.push(stringifyComment.indentComment(commentString(dc), ''));
83
+ }
62
84
  }
63
85
  return lines.join('\n') + '\n';
64
86
  }
@@ -6,7 +6,7 @@ var stringify = require('./stringify.js');
6
6
  var stringifyComment = require('./stringifyComment.js');
7
7
 
8
8
  function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
9
- const { allNullValues, doc, indent, indentStep, options: { indentSeq, simpleKeys } } = ctx;
9
+ const { allNullValues, doc, indent, indentStep, options: { commentString, indentSeq, simpleKeys } } = ctx;
10
10
  let keyComment = (Node.isNode(key) && key.comment) || null;
11
11
  if (simpleKeys) {
12
12
  if (keyComment) {
@@ -41,28 +41,39 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
41
41
  if (allNullValues || value == null) {
42
42
  if (keyCommentDone && onComment)
43
43
  onComment();
44
- return explicitKey ? `? ${str}` : str;
44
+ return str === '' ? '?' : explicitKey ? `? ${str}` : str;
45
45
  }
46
46
  }
47
47
  else if ((allNullValues && !simpleKeys) || (value == null && explicitKey)) {
48
- if (keyCommentDone)
49
- keyComment = null;
50
- if (chompKeep && !keyComment && onChompKeep)
48
+ str = `? ${str}`;
49
+ if (keyComment && !keyCommentDone) {
50
+ str += stringifyComment.lineComment(str, ctx.indent, commentString(keyComment));
51
+ }
52
+ else if (chompKeep && onChompKeep)
51
53
  onChompKeep();
52
- return stringifyComment.addComment(`? ${str}`, ctx.indent, keyComment);
54
+ return str;
53
55
  }
54
56
  if (keyCommentDone)
55
57
  keyComment = null;
56
- str = explicitKey
57
- ? `? ${stringifyComment.addComment(str, ctx.indent, keyComment)}\n${indent}:`
58
- : stringifyComment.addComment(`${str}:`, ctx.indent, keyComment);
58
+ if (explicitKey) {
59
+ if (keyComment)
60
+ str += stringifyComment.lineComment(str, ctx.indent, commentString(keyComment));
61
+ str = `? ${str}\n${indent}:`;
62
+ }
63
+ else {
64
+ str = `${str}:`;
65
+ if (keyComment)
66
+ str += stringifyComment.lineComment(str, ctx.indent, commentString(keyComment));
67
+ }
59
68
  let vcb = '';
60
69
  let valueComment = null;
61
70
  if (Node.isNode(value)) {
62
71
  if (value.spaceBefore)
63
72
  vcb = '\n';
64
- if (value.commentBefore)
65
- vcb += `\n${stringifyComment.stringifyComment(value.commentBefore, ctx.indent)}`;
73
+ if (value.commentBefore) {
74
+ const cs = commentString(value.commentBefore);
75
+ vcb += `\n${stringifyComment.indentComment(cs, ctx.indent)}`;
76
+ }
66
77
  valueComment = value.comment;
67
78
  }
68
79
  else if (value && typeof value === 'object') {
@@ -87,27 +98,30 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
87
98
  const valueStr = stringify.stringify(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true));
88
99
  let ws = ' ';
89
100
  if (vcb || keyComment) {
90
- ws = `${vcb}\n${ctx.indent}`;
101
+ if (valueStr === '' && !ctx.inFlow)
102
+ ws = vcb === '\n' ? '\n\n' : vcb;
103
+ else
104
+ ws = `${vcb}\n${ctx.indent}`;
91
105
  }
92
106
  else if (!explicitKey && Node.isCollection(value)) {
93
107
  const flow = valueStr[0] === '[' || valueStr[0] === '{';
94
108
  if (!flow || valueStr.includes('\n'))
95
109
  ws = `\n${ctx.indent}`;
96
110
  }
97
- else if (valueStr[0] === '\n')
111
+ else if (valueStr === '' || valueStr[0] === '\n')
98
112
  ws = '';
113
+ str += ws + valueStr;
99
114
  if (ctx.inFlow) {
100
115
  if (valueCommentDone && onComment)
101
116
  onComment();
102
- return str + ws + valueStr;
103
117
  }
104
- else {
105
- if (valueCommentDone)
106
- valueComment = null;
107
- if (chompKeep && !valueComment && onChompKeep)
108
- onChompKeep();
109
- return stringifyComment.addComment(str + ws + valueStr, ctx.indent, valueComment);
118
+ else if (valueComment && !valueCommentDone) {
119
+ str += stringifyComment.lineComment(str, ctx.indent, commentString(valueComment));
120
+ }
121
+ else if (chompKeep && onChompKeep) {
122
+ onChompKeep();
110
123
  }
124
+ return str;
111
125
  }
112
126
 
113
127
  exports.stringifyPair = stringifyPair;