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
@@ -1,18 +1,16 @@
1
1
  import { MAP, SCALAR, SEQ } from '../nodes/Node.js';
2
2
  import type { Pair } from '../nodes/Pair.js';
3
- import type { SchemaOptions, ToStringOptions } from '../options.js';
3
+ import type { SchemaOptions } from '../options.js';
4
4
  import type { CollectionTag, ScalarTag } from './types.js';
5
+ export declare type SchemaName = 'core' | 'failsafe' | 'json' | 'yaml-1.1';
5
6
  export declare class Schema {
6
- compat: Array<CollectionTag | ScalarTag> | null;
7
7
  knownTags: Record<string, CollectionTag | ScalarTag>;
8
8
  merge: boolean;
9
- name: string;
9
+ name: SchemaName;
10
10
  sortMapEntries: ((a: Pair, b: Pair) => number) | null;
11
11
  tags: Array<CollectionTag | ScalarTag>;
12
- toStringOptions: Readonly<ToStringOptions> | null;
13
12
  [MAP]: CollectionTag;
14
13
  [SCALAR]: ScalarTag;
15
14
  [SEQ]: CollectionTag;
16
- constructor({ compat, customTags, merge, resolveKnownTags, schema, sortMapEntries, toStringDefaults }: SchemaOptions);
17
- clone(): Schema;
15
+ constructor({ customTags, merge, resolveKnownTags, schema, sortMapEntries }: SchemaOptions);
18
16
  }
@@ -8,17 +8,11 @@ var tags = require('./tags.js');
8
8
 
9
9
  const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
10
10
  class Schema {
11
- constructor({ compat, customTags, merge, resolveKnownTags, schema, sortMapEntries, toStringDefaults }) {
12
- this.compat = Array.isArray(compat)
13
- ? tags.getTags(compat, 'compat')
14
- : compat
15
- ? tags.getTags(null, compat)
16
- : null;
11
+ constructor({ customTags, merge, resolveKnownTags, schema, sortMapEntries }) {
17
12
  this.merge = !!merge;
18
- this.name = (typeof schema === 'string' && schema) || 'core';
13
+ this.name = schema || 'core';
19
14
  this.knownTags = resolveKnownTags ? tags.coreKnownTags : {};
20
15
  this.tags = tags.getTags(customTags, this.name);
21
- this.toStringOptions = toStringDefaults || null;
22
16
  Object.defineProperty(this, Node.MAP, { value: map.map });
23
17
  Object.defineProperty(this, Node.SCALAR, { value: string.string });
24
18
  Object.defineProperty(this, Node.SEQ, { value: seq.seq });
@@ -26,11 +20,6 @@ class Schema {
26
20
  this.sortMapEntries =
27
21
  sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null;
28
22
  }
29
- clone() {
30
- const copy = Object.create(Schema.prototype, Object.getOwnPropertyDescriptors(this));
31
- copy.tags = this.tags.slice();
32
- return copy;
33
- }
34
23
  }
35
24
 
36
25
  exports.Schema = Schema;
@@ -22,10 +22,7 @@ const floatExp = {
22
22
  format: 'EXP',
23
23
  test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
24
24
  resolve: str => parseFloat(str),
25
- stringify(node) {
26
- const num = Number(node.value);
27
- return isFinite(num) ? num.toExponential() : stringifyNumber.stringifyNumber(node);
28
- }
25
+ stringify: ({ value }) => Number(value).toExponential()
29
26
  };
30
27
  const float = {
31
28
  identify: value => typeof value === 'number',
@@ -1,4 +1,5 @@
1
1
  import { SchemaOptions } from '../options.js';
2
+ import type { SchemaName } from './Schema.js';
2
3
  import type { CollectionTag, ScalarTag } from './types.js';
3
4
  declare const tagsByName: {
4
5
  binary: ScalarTag;
@@ -36,5 +37,5 @@ export declare const coreKnownTags: {
36
37
  test: RegExp;
37
38
  };
38
39
  };
39
- export declare function getTags(customTags: SchemaOptions['customTags'] | undefined, schemaName: string): (ScalarTag | CollectionTag)[];
40
+ export declare function getTags(customTags: SchemaOptions['customTags'] | undefined, schemaName: SchemaName): (ScalarTag | CollectionTag)[];
40
41
  export {};
@@ -16,13 +16,13 @@ var schema$2 = require('./yaml-1.1/schema.js');
16
16
  var set = require('./yaml-1.1/set.js');
17
17
  var timestamp = require('./yaml-1.1/timestamp.js');
18
18
 
19
- const schemas = new Map([
20
- ['core', schema.schema],
21
- ['failsafe', [map.map, seq.seq, string.string]],
22
- ['json', schema$1.schema],
23
- ['yaml11', schema$2.schema],
24
- ['yaml-1.1', schema$2.schema]
25
- ]);
19
+ const schemas = {
20
+ core: schema.schema,
21
+ failsafe: [map.map, seq.seq, string.string],
22
+ json: schema$1.schema,
23
+ yaml11: schema$2.schema,
24
+ 'yaml-1.1': schema$2.schema
25
+ };
26
26
  const tagsByName = {
27
27
  binary: binary.binary,
28
28
  bool: bool.boolTag,
@@ -50,17 +50,13 @@ const coreKnownTags = {
50
50
  'tag:yaml.org,2002:timestamp': timestamp.timestamp
51
51
  };
52
52
  function getTags(customTags, schemaName) {
53
- let tags = schemas.get(schemaName);
53
+ let tags = schemas[schemaName];
54
54
  if (!tags) {
55
- if (Array.isArray(customTags))
56
- tags = [];
57
- else {
58
- const keys = Array.from(schemas.keys())
59
- .filter(key => key !== 'yaml11')
60
- .map(key => JSON.stringify(key))
61
- .join(', ');
62
- throw new Error(`Unknown schema "${schemaName}"; use one of ${keys} or define customTags array`);
63
- }
55
+ const keys = Object.keys(schemas)
56
+ .filter(key => key !== 'yaml11')
57
+ .map(key => JSON.stringify(key))
58
+ .join(', ');
59
+ throw new Error(`Unknown schema "${schemaName}"; use one of ${keys}`);
64
60
  }
65
61
  if (Array.isArray(customTags)) {
66
62
  for (const tag of customTags)
@@ -22,10 +22,7 @@ const floatExp = {
22
22
  format: 'EXP',
23
23
  test: /^[-+]?(?:[0-9][0-9_]*)?(?:\.[0-9_]*)?[eE][-+]?[0-9]+$/,
24
24
  resolve: (str) => parseFloat(str.replace(/_/g, '')),
25
- stringify(node) {
26
- const num = Number(node.value);
27
- return isFinite(num) ? num.toExponential() : stringifyNumber.stringifyNumber(node);
28
- }
25
+ stringify: ({ value }) => Number(value).toExponential()
29
26
  };
30
27
  const float = {
31
28
  identify: value => typeof value === 'number',
@@ -0,0 +1,2 @@
1
+ export declare function addCommentBefore(str: string, indent: string, comment?: string | null): string;
2
+ export declare function addComment(str: string, indent: string, comment?: string | null): string;
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ function addComment(str, indent, comment) {
4
+ return !comment
5
+ ? str
6
+ : comment.includes('\n')
7
+ ? `${str}\n` + comment.replace(/^/gm, `${indent || ''}#`)
8
+ : str.endsWith(' ')
9
+ ? `${str}#${comment}`
10
+ : `${str} #${comment}`;
11
+ }
12
+
13
+ exports.addComment = addComment;
@@ -1,5 +1,4 @@
1
1
  import type { Document } from '../doc/Document.js';
2
- import type { Alias } from '../nodes/Alias.js';
3
2
  import type { ToStringOptions } from '../options.js';
4
3
  export declare type StringifyContext = {
5
4
  actualString?: boolean;
@@ -11,10 +10,9 @@ export declare type StringifyContext = {
11
10
  indent: string;
12
11
  indentStep: string;
13
12
  indentAtStart?: number;
14
- inFlow: boolean | null;
13
+ inFlow?: boolean;
15
14
  inStringifyKey?: boolean;
16
- options: Readonly<Required<Omit<ToStringOptions, 'collectionStyle' | 'indent'>>>;
17
- resolvedAliases?: Set<Alias>;
15
+ options: Readonly<Required<Omit<ToStringOptions, 'indent'>>>;
18
16
  };
19
- export declare function createStringifyContext(doc: Document, options: ToStringOptions): StringifyContext;
17
+ export declare const createStringifyContext: (doc: Document, options: ToStringOptions) => StringifyContext;
20
18
  export declare function stringify(item: unknown, ctx: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
@@ -2,13 +2,14 @@
2
2
 
3
3
  var anchors = require('../doc/anchors.js');
4
4
  var Node = require('../nodes/Node.js');
5
- var stringifyComment = require('./stringifyComment.js');
6
5
  var stringifyString = require('./stringifyString.js');
7
6
 
8
- function createStringifyContext(doc, options) {
9
- const opt = Object.assign({
10
- blockQuote: true,
11
- commentString: stringifyComment.stringifyComment,
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({
12
13
  defaultKeyType: null,
13
14
  defaultStringType: 'PLAIN',
14
15
  directives: null,
@@ -20,30 +21,11 @@ function createStringifyContext(doc, options) {
20
21
  minContentWidth: 20,
21
22
  nullStr: 'null',
22
23
  simpleKeys: false,
23
- singleQuote: null,
24
+ singleQuote: false,
24
25
  trueStr: 'true',
25
26
  verifyAliasOrder: true
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
- }
27
+ }, options)
28
+ });
47
29
  function getTagObject(tags, item) {
48
30
  if (item.tag) {
49
31
  const match = tags.filter(t => t.tag === item.tag);
@@ -71,37 +53,25 @@ function getTagObject(tags, item) {
71
53
  }
72
54
  // needs to be called before value stringifier to allow for circular anchor refs
73
55
  function stringifyProps(node, tagObj, { anchors: anchors$1, doc }) {
74
- if (!doc.directives)
75
- return '';
76
56
  const props = [];
77
57
  const anchor = (Node.isScalar(node) || Node.isCollection(node)) && node.anchor;
78
58
  if (anchor && anchors.anchorIsValid(anchor)) {
79
59
  anchors$1.add(anchor);
80
60
  props.push(`&${anchor}`);
81
61
  }
82
- const tag = node.tag || (tagObj.default ? null : tagObj.tag);
83
- if (tag)
84
- props.push(doc.directives.tagString(tag));
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
+ }
85
68
  return props.join(' ');
86
69
  }
87
70
  function stringify(item, ctx, onComment, onChompKeep) {
88
- var _a;
89
71
  if (Node.isPair(item))
90
72
  return item.toString(ctx, onComment, onChompKeep);
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
- }
73
+ if (Node.isAlias(item))
74
+ return item.toString(ctx);
105
75
  let tagObj = undefined;
106
76
  const node = Node.isNode(item)
107
77
  ? item
@@ -1,7 +1,11 @@
1
1
  import { Collection } from '../nodes/Collection.js';
2
- import { StringifyContext } from './stringify.js';
2
+ import { StringifyContext } from '../stringify/stringify.js';
3
+ declare type StringifyNode = {
4
+ comment: boolean;
5
+ str: string;
6
+ };
3
7
  interface StringifyCollectionOptions {
4
- blockItemPrefix: string;
8
+ blockItem(node: StringifyNode): string;
5
9
  flowChars: {
6
10
  start: '{';
7
11
  end: '}';
@@ -13,5 +17,5 @@ interface StringifyCollectionOptions {
13
17
  onChompKeep?: () => void;
14
18
  onComment?: () => void;
15
19
  }
16
- export declare function stringifyCollection(collection: Readonly<Collection>, ctx: StringifyContext, options: StringifyCollectionOptions): string;
20
+ export declare function stringifyCollection({ comment, flow, items }: Readonly<Collection>, ctx: StringifyContext, { blockItem, flowChars, itemIndent, onChompKeep, onComment }: StringifyCollectionOptions): string;
17
21
  export {};
@@ -1,154 +1,110 @@
1
1
  'use strict';
2
2
 
3
3
  var Collection = require('../nodes/Collection.js');
4
- var Node = require('../nodes/Node.js');
4
+ var addComment = require('./addComment.js');
5
5
  var stringify = require('./stringify.js');
6
- var stringifyComment = require('./stringifyComment.js');
6
+ var Node = require('../nodes/Node.js');
7
7
 
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 });
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;
17
15
  let chompKeep = false; // flag for the preceding node's status
18
- const lines = [];
19
- for (let i = 0; i < items.length; ++i) {
20
- const item = items[i];
16
+ const nodes = items.reduce((nodes, item, i) => {
21
17
  let comment = null;
22
18
  if (Node.isNode(item)) {
23
19
  if (!chompKeep && item.spaceBefore)
24
- lines.push('');
25
- addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
26
- if (item.comment)
20
+ nodes.push({ comment: true, str: '' });
21
+ if (item.commentBefore) {
22
+ // This match will always succeed on a non-empty string
23
+ for (const line of item.commentBefore.match(/^.*$/gm))
24
+ nodes.push({ comment: true, str: `#${line}` });
25
+ }
26
+ if (item.comment) {
27
27
  comment = item.comment;
28
+ singleLineOutput = false;
29
+ }
28
30
  }
29
31
  else if (Node.isPair(item)) {
30
32
  const ik = Node.isNode(item.key) ? item.key : null;
31
33
  if (ik) {
32
34
  if (!chompKeep && ik.spaceBefore)
33
- lines.push('');
34
- addCommentBefore(ctx, lines, ik.commentBefore, chompKeep);
35
+ nodes.push({ comment: true, str: '' });
36
+ if (ik.commentBefore) {
37
+ // This match will always succeed on a non-empty string
38
+ for (const line of ik.commentBefore.match(/^.*$/gm))
39
+ nodes.push({ comment: true, str: `#${line}` });
40
+ }
41
+ if (ik.comment)
42
+ singleLineOutput = false;
43
+ }
44
+ if (inFlow) {
45
+ const iv = Node.isNode(item.value) ? item.value : null;
46
+ if (iv) {
47
+ if (iv.comment)
48
+ comment = iv.comment;
49
+ if (iv.comment || iv.commentBefore)
50
+ singleLineOutput = false;
51
+ }
52
+ else if (item.value == null && ik && ik.comment) {
53
+ comment = ik.comment;
54
+ }
35
55
  }
36
56
  }
37
57
  chompKeep = false;
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)
58
+ let str = stringify.stringify(item, ctx, () => (comment = null), () => (chompKeep = true));
59
+ if (inFlow && i < items.length - 1)
60
+ str += ',';
61
+ str = addComment.addComment(str, itemIndent, comment);
62
+ if (chompKeep && (comment || inFlow))
42
63
  chompKeep = false;
43
- lines.push(blockItemPrefix + str);
44
- }
64
+ nodes.push({ comment: false, str });
65
+ return nodes;
66
+ }, []);
45
67
  let str;
46
- if (lines.length === 0) {
68
+ if (nodes.length === 0) {
47
69
  str = flowChars.start + flowChars.end;
48
70
  }
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;
71
+ else if (inFlow) {
72
+ const { start, end } = flowChars;
73
+ const strings = nodes.map(n => n.str);
74
+ let singleLineLength = 2;
75
+ for (const node of nodes) {
76
+ if (node.comment || node.str.includes('\n')) {
77
+ singleLineOutput = false;
78
+ break;
94
79
  }
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;
104
- }
105
- }
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;
80
+ singleLineLength += node.str.length + 2;
127
81
  }
128
- if (reqNewline) {
82
+ if (!singleLineOutput ||
83
+ singleLineLength > Collection.Collection.maxFlowStringSingleLineLength) {
129
84
  str = start;
130
- for (const line of lines)
131
- str += line ? `\n${indentStep}${indent}${line}` : '\n';
85
+ for (const s of strings) {
86
+ str += s ? `\n${indentStep}${indent}${s}` : '\n';
87
+ }
132
88
  str += `\n${indent}${end}`;
133
89
  }
134
90
  else {
135
- str = `${start} ${lines.join(' ')} ${end}`;
91
+ str = `${start} ${strings.join(' ')} ${end}`;
136
92
  }
137
93
  }
94
+ else {
95
+ const strings = nodes.map(blockItem);
96
+ str = strings.shift() || '';
97
+ for (const s of strings)
98
+ str += s ? `\n${indent}${s}` : '\n';
99
+ }
138
100
  if (comment) {
139
- str += stringifyComment.lineComment(str, commentString(comment), indent);
101
+ str += '\n' + comment.replace(/^/gm, `${indent}#`);
140
102
  if (onComment)
141
103
  onComment();
142
104
  }
105
+ else if (chompKeep && onChompKeep)
106
+ onChompKeep();
143
107
  return str;
144
108
  }
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
- }
153
109
 
154
110
  exports.stringifyCollection = stringifyCollection;
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  var Node = require('../nodes/Node.js');
4
+ var addComment = require('./addComment.js');
4
5
  var stringify = require('./stringify.js');
5
- var stringifyComment = require('./stringifyComment.js');
6
6
 
7
7
  function stringifyDocument(doc, options) {
8
8
  const lines = [];
9
9
  let hasDirectives = options.directives === true;
10
- if (options.directives !== false && doc.directives) {
10
+ if (options.directives !== false) {
11
11
  const dir = doc.directives.toString(doc);
12
12
  if (dir) {
13
13
  lines.push(dir);
@@ -18,24 +18,20 @@ function stringifyDocument(doc, options) {
18
18
  }
19
19
  if (hasDirectives)
20
20
  lines.push('---');
21
- const ctx = stringify.createStringifyContext(doc, options);
22
- const { commentString } = ctx.options;
23
21
  if (doc.commentBefore) {
24
22
  if (lines.length !== 1)
25
23
  lines.unshift('');
26
- const cs = commentString(doc.commentBefore);
27
- lines.unshift(stringifyComment.indentComment(cs, ''));
24
+ lines.unshift(doc.commentBefore.replace(/^/gm, '#'));
28
25
  }
26
+ const ctx = stringify.createStringifyContext(doc, options);
29
27
  let chompKeep = false;
30
28
  let contentComment = null;
31
29
  if (doc.contents) {
32
30
  if (Node.isNode(doc.contents)) {
33
31
  if (doc.contents.spaceBefore && hasDirectives)
34
32
  lines.push('');
35
- if (doc.contents.commentBefore) {
36
- const cs = commentString(doc.contents.commentBefore);
37
- lines.push(stringifyComment.indentComment(cs, ''));
38
- }
33
+ if (doc.contents.commentBefore)
34
+ lines.push(doc.contents.commentBefore.replace(/^/gm, '#'));
39
35
  // top-level block scalars need to be indented if followed by a comment
40
36
  ctx.forceBlockIndent = !!doc.comment;
41
37
  contentComment = doc.contents.comment;
@@ -43,7 +39,7 @@ function stringifyDocument(doc, options) {
43
39
  const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
44
40
  let body = stringify.stringify(doc.contents, ctx, () => (contentComment = null), onChompKeep);
45
41
  if (contentComment)
46
- body += stringifyComment.lineComment(body, '', commentString(contentComment));
42
+ body = addComment.addComment(body, '', contentComment);
47
43
  if ((body[0] === '|' || body[0] === '>') &&
48
44
  lines[lines.length - 1] === '---') {
49
45
  // Top-level block scalars with a preceding doc marker ought to use the
@@ -56,13 +52,10 @@ function stringifyDocument(doc, options) {
56
52
  else {
57
53
  lines.push(stringify.stringify(doc.contents, ctx));
58
54
  }
59
- let dc = doc.comment;
60
- if (dc && chompKeep)
61
- dc = dc.replace(/^\n+/, '');
62
- if (dc) {
55
+ if (doc.comment) {
63
56
  if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
64
57
  lines.push('');
65
- lines.push(stringifyComment.indentComment(commentString(dc), ''));
58
+ lines.push(doc.comment.replace(/^/gm, '#'));
66
59
  }
67
60
  return lines.join('\n') + '\n';
68
61
  }