cdk8s 2.7.62 → 2.7.63

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