yaml 2.0.0-9 → 2.1.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 (120) hide show
  1. package/README.md +1 -1
  2. package/browser/dist/compose/compose-collection.js +1 -1
  3. package/browser/dist/compose/compose-doc.js +4 -3
  4. package/browser/dist/compose/compose-node.js +13 -4
  5. package/browser/dist/compose/compose-scalar.js +18 -13
  6. package/browser/dist/compose/composer.js +8 -10
  7. package/browser/dist/compose/resolve-block-map.js +15 -8
  8. package/browser/dist/compose/resolve-block-scalar.js +12 -3
  9. package/browser/dist/compose/resolve-block-seq.js +5 -0
  10. package/browser/dist/compose/resolve-flow-collection.js +13 -5
  11. package/browser/dist/compose/resolve-flow-scalar.js +1 -1
  12. package/browser/dist/compose/resolve-props.js +9 -1
  13. package/browser/dist/compose/util-empty-scalar-position.js +1 -1
  14. package/browser/dist/compose/util-flow-indent-check.js +15 -0
  15. package/browser/dist/doc/Document.js +53 -20
  16. package/browser/dist/doc/anchors.js +2 -2
  17. package/browser/dist/doc/createNode.js +8 -7
  18. package/browser/dist/doc/directives.js +7 -4
  19. package/browser/dist/index.js +1 -2
  20. package/browser/dist/nodes/Collection.js +2 -1
  21. package/browser/dist/nodes/Pair.js +2 -2
  22. package/browser/dist/nodes/Scalar.js +1 -1
  23. package/browser/dist/nodes/YAMLMap.js +7 -7
  24. package/browser/dist/nodes/YAMLSeq.js +2 -10
  25. package/browser/dist/nodes/addPairToJSMap.js +1 -1
  26. package/browser/dist/nodes/toJS.js +3 -1
  27. package/browser/dist/parse/cst-scalar.js +1 -2
  28. package/browser/dist/parse/cst-visit.js +2 -2
  29. package/browser/dist/parse/lexer.js +20 -8
  30. package/browser/dist/parse/parser.js +142 -68
  31. package/browser/dist/public-api.js +5 -7
  32. package/browser/dist/schema/Schema.js +14 -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 +9 -9
  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 +1 -1
  39. package/browser/dist/schema/yaml-1.1/set.js +4 -0
  40. package/browser/dist/stringify/stringify.js +49 -22
  41. package/browser/dist/stringify/stringifyCollection.js +114 -85
  42. package/browser/dist/stringify/stringifyComment.js +18 -12
  43. package/browser/dist/stringify/stringifyDocument.js +36 -15
  44. package/browser/dist/stringify/stringifyPair.js +34 -20
  45. package/browser/dist/stringify/stringifyString.js +8 -11
  46. package/browser/dist/visit.js +149 -62
  47. package/dist/compose/compose-collection.js +1 -1
  48. package/dist/compose/compose-doc.js +4 -3
  49. package/dist/compose/compose-node.d.ts +1 -0
  50. package/dist/compose/compose-node.js +13 -4
  51. package/dist/compose/compose-scalar.js +17 -12
  52. package/dist/compose/composer.d.ts +1 -1
  53. package/dist/compose/composer.js +10 -12
  54. package/dist/compose/resolve-block-map.js +15 -8
  55. package/dist/compose/resolve-block-scalar.js +12 -3
  56. package/dist/compose/resolve-block-seq.js +5 -0
  57. package/dist/compose/resolve-flow-collection.js +13 -5
  58. package/dist/compose/resolve-flow-scalar.js +1 -1
  59. package/dist/compose/resolve-props.d.ts +2 -1
  60. package/dist/compose/resolve-props.js +9 -1
  61. package/dist/compose/util-empty-scalar-position.js +1 -1
  62. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  63. package/dist/compose/util-flow-indent-check.js +17 -0
  64. package/dist/doc/Document.d.ts +17 -14
  65. package/dist/doc/Document.js +58 -25
  66. package/dist/doc/anchors.d.ts +3 -3
  67. package/dist/doc/anchors.js +2 -2
  68. package/dist/doc/createNode.d.ts +1 -1
  69. package/dist/doc/createNode.js +8 -7
  70. package/dist/doc/directives.d.ts +4 -2
  71. package/dist/doc/directives.js +7 -4
  72. package/dist/index.d.ts +4 -2
  73. package/dist/index.js +1 -2
  74. package/dist/nodes/Collection.d.ts +2 -2
  75. package/dist/nodes/Collection.js +2 -1
  76. package/dist/nodes/Node.d.ts +14 -10
  77. package/dist/nodes/Pair.d.ts +1 -1
  78. package/dist/nodes/Pair.js +2 -2
  79. package/dist/nodes/Scalar.js +1 -1
  80. package/dist/nodes/YAMLMap.d.ts +9 -4
  81. package/dist/nodes/YAMLMap.js +7 -7
  82. package/dist/nodes/YAMLSeq.d.ts +4 -1
  83. package/dist/nodes/YAMLSeq.js +2 -10
  84. package/dist/nodes/addPairToJSMap.js +1 -1
  85. package/dist/nodes/toJS.js +3 -1
  86. package/dist/options.d.ts +44 -13
  87. package/dist/parse/cst-scalar.js +1 -2
  88. package/dist/parse/cst-visit.js +2 -2
  89. package/dist/parse/lexer.js +20 -8
  90. package/dist/parse/parser.js +142 -68
  91. package/dist/public-api.js +5 -7
  92. package/dist/schema/Schema.d.ts +5 -4
  93. package/dist/schema/Schema.js +13 -3
  94. package/dist/schema/common/null.js +3 -1
  95. package/dist/schema/core/float.js +4 -1
  96. package/dist/schema/json-schema.d.ts +69 -0
  97. package/dist/schema/tags.d.ts +1 -2
  98. package/dist/schema/tags.js +9 -9
  99. package/dist/schema/yaml-1.1/float.js +4 -1
  100. package/dist/schema/yaml-1.1/omap.d.ts +7 -3
  101. package/dist/schema/yaml-1.1/omap.js +1 -1
  102. package/dist/schema/yaml-1.1/pairs.js +1 -1
  103. package/dist/schema/yaml-1.1/set.d.ts +6 -2
  104. package/dist/schema/yaml-1.1/set.js +4 -0
  105. package/dist/stringify/stringify.d.ts +5 -3
  106. package/dist/stringify/stringify.js +49 -22
  107. package/dist/stringify/stringifyCollection.d.ts +2 -6
  108. package/dist/stringify/stringifyCollection.js +113 -84
  109. package/dist/stringify/stringifyComment.d.ts +10 -2
  110. package/dist/stringify/stringifyComment.js +19 -12
  111. package/dist/stringify/stringifyDocument.js +35 -14
  112. package/dist/stringify/stringifyPair.js +33 -19
  113. package/dist/stringify/stringifyString.js +8 -11
  114. package/dist/test-events.js +14 -14
  115. package/dist/visit.d.ts +49 -10
  116. package/dist/visit.js +148 -60
  117. package/package.json +11 -10
  118. package/browser/dist/options.js +0 -18
  119. package/dist/node_modules/tslib/tslib.es6.js +0 -76
  120. package/dist/options.js +0 -20
@@ -20,7 +20,7 @@ function resolvePairs(seq, onError) {
20
20
  ? `${item.commentBefore}\n${pair.key.commentBefore}`
21
21
  : item.commentBefore;
22
22
  if (item.comment) {
23
- const cn = pair.value || pair.key;
23
+ const cn = pair.value ?? pair.key;
24
24
  cn.comment = cn.comment
25
25
  ? `${item.comment}\n${cn.comment}`
26
26
  : item.comment;
@@ -12,9 +12,13 @@ export declare class YAMLSet<T = unknown> extends YAMLMap<T, Scalar<null> | null
12
12
  key: T;
13
13
  value: Scalar<null> | null;
14
14
  }): void;
15
- get(key?: T, keepPair?: boolean): unknown;
15
+ /**
16
+ * If `keepPair` is `true`, returns the Pair matching `key`.
17
+ * Otherwise, returns the value of that Pair's key.
18
+ */
19
+ get(key: unknown, keepPair?: boolean): any;
16
20
  set(key: T, value: boolean): void;
17
- /** Will throw; `value` must be boolean */
21
+ /** @deprecated Will throw; `value` must be boolean */
18
22
  set(key: T, value: null): void;
19
23
  toJSON(_?: unknown, ctx?: ToJSContext): any;
20
24
  toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
@@ -24,6 +24,10 @@ class YAMLSet extends YAMLMap.YAMLMap {
24
24
  if (!prev)
25
25
  this.items.push(pair);
26
26
  }
27
+ /**
28
+ * If `keepPair` is `true`, returns the Pair matching `key`.
29
+ * Otherwise, returns the value of that Pair's key.
30
+ */
27
31
  get(key, keepPair) {
28
32
  const pair = YAMLMap.findPair(this.items, key);
29
33
  return !keepPair && Node.isPair(pair)
@@ -1,4 +1,5 @@
1
1
  import type { Document } from '../doc/Document.js';
2
+ import type { Alias } from '../nodes/Alias.js';
2
3
  import type { ToStringOptions } from '../options.js';
3
4
  export declare type StringifyContext = {
4
5
  actualString?: boolean;
@@ -10,9 +11,10 @@ export declare type StringifyContext = {
10
11
  indent: string;
11
12
  indentStep: string;
12
13
  indentAtStart?: number;
13
- inFlow?: boolean;
14
+ inFlow: boolean | null;
14
15
  inStringifyKey?: boolean;
15
- options: Readonly<Required<Omit<ToStringOptions, 'indent'>>>;
16
+ options: Readonly<Required<Omit<ToStringOptions, 'collectionStyle' | 'indent'>>>;
17
+ resolvedAliases?: Set<Alias>;
16
18
  };
17
- export declare const createStringifyContext: (doc: Document, options: ToStringOptions) => StringifyContext;
19
+ export declare function createStringifyContext(doc: Document, options: ToStringOptions): StringifyContext;
18
20
  export declare function stringify(item: unknown, ctx: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
@@ -2,15 +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({
13
10
  blockQuote: true,
11
+ commentString: stringifyComment.stringifyComment,
14
12
  defaultKeyType: null,
15
13
  defaultStringType: 'PLAIN',
16
14
  directives: null,
@@ -25,54 +23,83 @@ const createStringifyContext = (doc, options) => ({
25
23
  singleQuote: null,
26
24
  trueStr: 'true',
27
25
  verifyAliasOrder: true
28
- }, options)
29
- });
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
+ }
30
47
  function getTagObject(tags, item) {
31
48
  if (item.tag) {
32
49
  const match = tags.filter(t => t.tag === item.tag);
33
50
  if (match.length > 0)
34
- return match.find(t => t.format === item.format) || match[0];
51
+ return match.find(t => t.format === item.format) ?? match[0];
35
52
  }
36
53
  let tagObj = undefined;
37
54
  let obj;
38
55
  if (Node.isScalar(item)) {
39
56
  obj = item.value;
40
- const match = tags.filter(t => t.identify && t.identify(obj));
57
+ const match = tags.filter(t => t.identify?.(obj));
41
58
  tagObj =
42
- match.find(t => t.format === item.format) || match.find(t => !t.format);
59
+ match.find(t => t.format === item.format) ?? match.find(t => !t.format);
43
60
  }
44
61
  else {
45
62
  obj = item;
46
63
  tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
47
64
  }
48
65
  if (!tagObj) {
49
- // @ts-ignore
50
- const name = obj && obj.constructor ? obj.constructor.name : typeof obj;
66
+ const name = obj?.constructor?.name ?? typeof obj;
51
67
  throw new Error(`Tag not resolved for ${name} value`);
52
68
  }
53
69
  return tagObj;
54
70
  }
55
71
  // needs to be called before value stringifier to allow for circular anchor refs
56
72
  function stringifyProps(node, tagObj, { anchors: anchors$1, doc }) {
73
+ if (!doc.directives)
74
+ return '';
57
75
  const props = [];
58
76
  const anchor = (Node.isScalar(node) || Node.isCollection(node)) && node.anchor;
59
77
  if (anchor && anchors.anchorIsValid(anchor)) {
60
78
  anchors$1.add(anchor);
61
79
  props.push(`&${anchor}`);
62
80
  }
63
- if (node.tag) {
64
- props.push(doc.directives.tagString(node.tag));
65
- }
66
- else if (!tagObj.default) {
67
- props.push(doc.directives.tagString(tagObj.tag));
68
- }
81
+ const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
82
+ if (tag)
83
+ props.push(doc.directives.tagString(tag));
69
84
  return props.join(' ');
70
85
  }
71
86
  function stringify(item, ctx, onComment, onChompKeep) {
72
87
  if (Node.isPair(item))
73
88
  return item.toString(ctx, onComment, onChompKeep);
74
- if (Node.isAlias(item))
75
- return item.toString(ctx);
89
+ if (Node.isAlias(item)) {
90
+ if (ctx.doc.directives)
91
+ return item.toString(ctx);
92
+ if (ctx.resolvedAliases?.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
+ }
76
103
  let tagObj = undefined;
77
104
  const node = Node.isNode(item)
78
105
  ? item
@@ -81,7 +108,7 @@ function stringify(item, ctx, onComment, onChompKeep) {
81
108
  tagObj = getTagObject(ctx.doc.schema.tags, node);
82
109
  const props = stringifyProps(node, tagObj, ctx);
83
110
  if (props.length > 0)
84
- ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1;
111
+ ctx.indentAtStart = (ctx.indentAtStart ?? 0) + props.length + 1;
85
112
  const str = typeof tagObj.stringify === 'function'
86
113
  ? tagObj.stringify(node, ctx, onComment, onChompKeep)
87
114
  : 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,149 @@ 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
+ const flow = ctx.inFlow ?? collection.flow;
10
+ const stringify = flow ? stringifyFlowCollection : stringifyBlockCollection;
11
+ return stringify(collection, ctx, options);
12
+ }
13
+ function stringifyBlockCollection({ comment, items }, ctx, { blockItemPrefix, flowChars, itemIndent, onChompKeep, onComment }) {
14
+ const { indent, options: { commentString } } = ctx;
15
+ const itemCtx = Object.assign({}, ctx, { indent: itemIndent, type: null });
15
16
  let chompKeep = false; // flag for the preceding node's status
16
- const nodes = items.reduce((nodes, item, i) => {
17
+ const lines = [];
18
+ for (let i = 0; i < items.length; ++i) {
19
+ const item = items[i];
17
20
  let comment = null;
18
21
  if (Node.isNode(item)) {
19
22
  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) {
23
+ lines.push('');
24
+ addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
25
+ if (item.comment)
34
26
  comment = item.comment;
35
- singleLineOutput = false;
36
- }
37
27
  }
38
28
  else if (Node.isPair(item)) {
39
29
  const ik = Node.isNode(item.key) ? item.key : null;
40
30
  if (ik) {
41
31
  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
- }
32
+ lines.push('');
33
+ addCommentBefore(ctx, lines, ik.commentBefore, chompKeep);
69
34
  }
70
35
  }
71
36
  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))
37
+ let str = stringify.stringify(item, itemCtx, () => (comment = null), () => (chompKeep = true));
38
+ if (comment)
39
+ str += stringifyComment.lineComment(str, itemIndent, commentString(comment));
40
+ if (chompKeep && comment)
77
41
  chompKeep = false;
78
- nodes.push({ comment: false, str });
79
- return nodes;
80
- }, []);
42
+ lines.push(blockItemPrefix + str);
43
+ }
81
44
  let str;
82
- if (nodes.length === 0) {
45
+ if (lines.length === 0) {
83
46
  str = flowChars.start + flowChars.end;
84
47
  }
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;
48
+ else {
49
+ str = lines[0];
50
+ for (let i = 1; i < lines.length; ++i) {
51
+ const line = lines[i];
52
+ str += line ? `\n${indent}${line}` : '\n';
53
+ }
54
+ }
55
+ if (comment) {
56
+ str += '\n' + stringifyComment.indentComment(commentString(comment), indent);
57
+ if (onComment)
58
+ onComment();
59
+ }
60
+ else if (chompKeep && onChompKeep)
61
+ onChompKeep();
62
+ return str;
63
+ }
64
+ function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemIndent, onComment }) {
65
+ const { indent, indentStep, options: { commentString } } = ctx;
66
+ itemIndent += indentStep;
67
+ const itemCtx = Object.assign({}, ctx, {
68
+ indent: itemIndent,
69
+ inFlow: true,
70
+ type: null
71
+ });
72
+ let reqNewline = false;
73
+ let linesAtValue = 0;
74
+ const lines = [];
75
+ for (let i = 0; i < items.length; ++i) {
76
+ const item = items[i];
77
+ let comment = null;
78
+ if (Node.isNode(item)) {
79
+ if (item.spaceBefore)
80
+ lines.push('');
81
+ addCommentBefore(ctx, lines, item.commentBefore, false);
82
+ if (item.comment)
83
+ comment = item.comment;
84
+ }
85
+ else if (Node.isPair(item)) {
86
+ const ik = Node.isNode(item.key) ? item.key : null;
87
+ if (ik) {
88
+ if (ik.spaceBefore)
89
+ lines.push('');
90
+ addCommentBefore(ctx, lines, ik.commentBefore, false);
91
+ if (ik.comment)
92
+ reqNewline = true;
93
+ }
94
+ const iv = Node.isNode(item.value) ? item.value : null;
95
+ if (iv) {
96
+ if (iv.comment)
97
+ comment = iv.comment;
98
+ if (iv.commentBefore)
99
+ reqNewline = true;
100
+ }
101
+ else if (item.value == null && ik && ik.comment) {
102
+ comment = ik.comment;
93
103
  }
94
- singleLineLength += node.str.length + 2;
95
104
  }
96
- if (!singleLineOutput ||
97
- singleLineLength > Collection.Collection.maxFlowStringSingleLineLength) {
105
+ if (comment)
106
+ reqNewline = true;
107
+ let str = stringify.stringify(item, itemCtx, () => (comment = null));
108
+ if (i < items.length - 1)
109
+ str += ',';
110
+ if (comment)
111
+ str += stringifyComment.lineComment(str, itemIndent, commentString(comment));
112
+ if (!reqNewline && (lines.length > linesAtValue || str.includes('\n')))
113
+ reqNewline = true;
114
+ lines.push(str);
115
+ linesAtValue = lines.length;
116
+ }
117
+ let str;
118
+ const { start, end } = flowChars;
119
+ if (lines.length === 0) {
120
+ str = start + end;
121
+ }
122
+ else {
123
+ if (!reqNewline) {
124
+ const len = lines.reduce((sum, line) => sum + line.length + 2, 2);
125
+ reqNewline = len > Collection.Collection.maxFlowStringSingleLineLength;
126
+ }
127
+ if (reqNewline) {
98
128
  str = start;
99
- for (const s of strings) {
100
- str += s ? `\n${indentStep}${indent}${s}` : '\n';
101
- }
129
+ for (const line of lines)
130
+ str += line ? `\n${indentStep}${indent}${line}` : '\n';
102
131
  str += `\n${indent}${end}`;
103
132
  }
104
133
  else {
105
- str = `${start} ${strings.join(' ')} ${end}`;
134
+ str = `${start} ${lines.join(' ')} ${end}`;
106
135
  }
107
136
  }
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
137
  if (comment) {
115
- str += '\n' + stringifyComment.stringifyComment(comment, indent);
138
+ str += stringifyComment.lineComment(str, commentString(comment), indent);
116
139
  if (onComment)
117
140
  onComment();
118
141
  }
119
- else if (chompKeep && onChompKeep)
120
- onChompKeep();
121
142
  return str;
122
143
  }
144
+ function addCommentBefore({ indent, options: { commentString } }, lines, comment, chompKeep) {
145
+ if (comment && chompKeep)
146
+ comment = comment.replace(/^\n+/, '');
147
+ if (comment) {
148
+ const ic = stringifyComment.indentComment(commentString(comment), indent);
149
+ lines.push(ic.trimStart()); // Avoid double indent on first line
150
+ }
151
+ }
123
152
 
124
153
  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;
@@ -7,31 +7,35 @@ var stringifyComment = require('./stringifyComment.js');
7
7
  function stringifyDocument(doc, options) {
8
8
  const lines = [];
9
9
  let hasDirectives = options.directives === true;
10
- if (options.directives !== false) {
10
+ if (options.directives !== false && doc.directives) {
11
11
  const dir = doc.directives.toString(doc);
12
12
  if (dir) {
13
13
  lines.push(dir);
14
14
  hasDirectives = true;
15
15
  }
16
- else if (doc.directives.marker)
16
+ else if (doc.directives.docStart)
17
17
  hasDirectives = true;
18
18
  }
19
19
  if (hasDirectives)
20
20
  lines.push('---');
21
+ const ctx = stringify.createStringifyContext(doc, options);
22
+ const { commentString } = ctx.options;
21
23
  if (doc.commentBefore) {
22
24
  if (lines.length !== 1)
23
25
  lines.unshift('');
24
- lines.unshift(stringifyComment.stringifyComment(doc.commentBefore, ''));
26
+ const cs = commentString(doc.commentBefore);
27
+ lines.unshift(stringifyComment.indentComment(cs, ''));
25
28
  }
26
- const ctx = stringify.createStringifyContext(doc, options);
27
29
  let chompKeep = false;
28
30
  let contentComment = null;
29
31
  if (doc.contents) {
30
32
  if (Node.isNode(doc.contents)) {
31
33
  if (doc.contents.spaceBefore && hasDirectives)
32
34
  lines.push('');
33
- if (doc.contents.commentBefore)
34
- lines.push(stringifyComment.stringifyComment(doc.contents.commentBefore, ''));
35
+ if (doc.contents.commentBefore) {
36
+ const cs = commentString(doc.contents.commentBefore);
37
+ lines.push(stringifyComment.indentComment(cs, ''));
38
+ }
35
39
  // top-level block scalars need to be indented if followed by a comment
36
40
  ctx.forceBlockIndent = !!doc.comment;
37
41
  contentComment = doc.contents.comment;
@@ -39,7 +43,7 @@ function stringifyDocument(doc, options) {
39
43
  const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
40
44
  let body = stringify.stringify(doc.contents, ctx, () => (contentComment = null), onChompKeep);
41
45
  if (contentComment)
42
- body = stringifyComment.addComment(body, '', contentComment);
46
+ body += stringifyComment.lineComment(body, '', commentString(contentComment));
43
47
  if ((body[0] === '|' || body[0] === '>') &&
44
48
  lines[lines.length - 1] === '---') {
45
49
  // Top-level block scalars with a preceding doc marker ought to use the
@@ -52,13 +56,30 @@ function stringifyDocument(doc, options) {
52
56
  else {
53
57
  lines.push(stringify.stringify(doc.contents, ctx));
54
58
  }
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, ''));
59
+ if (doc.directives?.docEnd) {
60
+ if (doc.comment) {
61
+ const cs = commentString(doc.comment);
62
+ if (cs.includes('\n')) {
63
+ lines.push('...');
64
+ lines.push(stringifyComment.indentComment(cs, ''));
65
+ }
66
+ else {
67
+ lines.push(`... ${cs}`);
68
+ }
69
+ }
70
+ else {
71
+ lines.push('...');
72
+ }
73
+ }
74
+ else {
75
+ let dc = doc.comment;
76
+ if (dc && chompKeep)
77
+ dc = dc.replace(/^\n+/, '');
78
+ if (dc) {
79
+ if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
80
+ lines.push('');
81
+ lines.push(stringifyComment.indentComment(commentString(dc), ''));
82
+ }
62
83
  }
63
84
  return lines.join('\n') + '\n';
64
85
  }