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
@@ -6,9 +6,13 @@ import type { Pair } from './Pair.js';
6
6
  import type { Scalar } from './Scalar.js';
7
7
  import type { YAMLMap } from './YAMLMap.js';
8
8
  import type { YAMLSeq } from './YAMLSeq.js';
9
- export declare type Node = Alias | Scalar | YAMLMap | YAMLSeq;
9
+ export declare type Node<T = unknown> = Alias | Scalar<T> | YAMLMap<unknown, T> | YAMLSeq<T>;
10
+ /** Utility type mapper */
11
+ export declare type NodeType<T> = T extends string | number | bigint | boolean | null ? Scalar<T> : T extends Array<any> ? YAMLSeq<NodeType<T[number]>> : T extends {
12
+ [key: string | number]: any;
13
+ } ? YAMLMap<NodeType<keyof T>, NodeType<T[keyof T]>> : Node;
10
14
  export declare type ParsedNode = Alias.Parsed | Scalar.Parsed | YAMLMap.Parsed | YAMLSeq.Parsed;
11
- export declare type Range = [number, number, number];
15
+ export declare type Range = [start: number, valueEnd: number, nodeEnd: number];
12
16
  export declare const ALIAS: unique symbol;
13
17
  export declare const DOC: unique symbol;
14
18
  export declare const MAP: unique symbol;
@@ -17,14 +21,14 @@ export declare const SCALAR: unique symbol;
17
21
  export declare const SEQ: unique symbol;
18
22
  export declare const NODE_TYPE: unique symbol;
19
23
  export declare const isAlias: (node: any) => node is Alias;
20
- export declare const isDocument: (node: any) => node is Document<unknown>;
21
- export declare const isMap: (node: any) => node is YAMLMap<unknown, unknown>;
22
- export declare const isPair: (node: any) => node is Pair<unknown, unknown>;
23
- export declare const isScalar: (node: any) => node is Scalar<unknown>;
24
- export declare const isSeq: (node: any) => node is YAMLSeq<unknown>;
25
- export declare function isCollection(node: any): node is YAMLMap | YAMLSeq;
26
- export declare function isNode(node: any): node is Node;
27
- export declare const hasAnchor: (node: unknown) => node is Scalar<unknown> | YAMLMap<unknown, unknown> | YAMLSeq<unknown>;
24
+ export declare const isDocument: <T extends Node<unknown> = Node<unknown>>(node: any) => node is Document<T>;
25
+ export declare const isMap: <K = unknown, V = unknown>(node: any) => node is YAMLMap<K, V>;
26
+ export declare const isPair: <K = unknown, V = unknown>(node: any) => node is Pair<K, V>;
27
+ export declare const isScalar: <T = unknown>(node: any) => node is Scalar<T>;
28
+ export declare const isSeq: <T = unknown>(node: any) => node is YAMLSeq<T>;
29
+ export declare function isCollection<K = unknown, V = unknown>(node: any): node is YAMLMap<K, V> | YAMLSeq<V>;
30
+ export declare function isNode<T = unknown>(node: any): node is Node<T>;
31
+ export declare const hasAnchor: <K = unknown, V = unknown>(node: unknown) => node is Scalar<V> | YAMLMap<K, V> | YAMLSeq<V>;
28
32
  export declare abstract class NodeBase {
29
33
  readonly [NODE_TYPE]: symbol;
30
34
  /** A comment on or immediately after this */
@@ -5,7 +5,7 @@ import type { StringifyContext } from '../stringify/stringify.js';
5
5
  import { addPairToJSMap } from './addPairToJSMap.js';
6
6
  import { NODE_TYPE } from './Node.js';
7
7
  import type { ToJSContext } from './toJS.js';
8
- export declare function createPair(key: unknown, value: unknown, ctx: CreateNodeContext): Pair<import("./Node.js").Node, import("./Alias.js").Alias | import("./Scalar.js").Scalar<unknown> | import("./YAMLMap.js").YAMLMap<unknown, unknown> | import("./YAMLSeq.js").YAMLSeq<unknown>>;
8
+ export declare function createPair(key: unknown, value: unknown, ctx: CreateNodeContext): Pair<import("./Node.js").Node<unknown>, import("./Alias.js").Alias | import("./Scalar.js").Scalar<unknown> | import("./YAMLMap.js").YAMLMap<unknown, unknown> | import("./YAMLSeq.js").YAMLSeq<unknown>>;
9
9
  export declare class Pair<K = unknown, V = unknown> {
10
10
  readonly [NODE_TYPE]: symbol;
11
11
  /** Always Node or null when parsed, but can be set to anything. */
@@ -25,11 +25,11 @@ class Pair {
25
25
  return new Pair(key, value);
26
26
  }
27
27
  toJSON(_, ctx) {
28
- const pair = ctx && ctx.mapAsMap ? new Map() : {};
28
+ const pair = ctx?.mapAsMap ? new Map() : {};
29
29
  return addPairToJSMap.addPairToJSMap(ctx, pair, this);
30
30
  }
31
31
  toString(ctx, onComment, onChompKeep) {
32
- return ctx && ctx.doc
32
+ return ctx?.doc
33
33
  ? stringifyPair.stringifyPair(this, ctx, onComment, onChompKeep)
34
34
  : JSON.stringify(this);
35
35
  }
@@ -10,7 +10,7 @@ class Scalar extends Node.NodeBase {
10
10
  this.value = value;
11
11
  }
12
12
  toJSON(arg, ctx) {
13
- return ctx && ctx.keep ? this.value : toJS.toJS(this.value, arg, ctx);
13
+ return ctx?.keep ? this.value : toJS.toJS(this.value, arg, ctx);
14
14
  }
15
15
  toString() {
16
16
  return String(this.value);
@@ -4,6 +4,7 @@ import type { StringifyContext } from '../stringify/stringify.js';
4
4
  import { Collection } from './Collection.js';
5
5
  import { ParsedNode, Range } from './Node.js';
6
6
  import { Pair } from './Pair.js';
7
+ import { Scalar } from './Scalar.js';
7
8
  import type { ToJSContext } from './toJS.js';
8
9
  export declare function findPair<K = unknown, V = unknown>(items: Iterable<Pair<K, V>>, key: unknown): Pair<K, V> | undefined;
9
10
  export declare namespace YAMLMap {
@@ -27,15 +28,19 @@ export declare class YAMLMap<K = unknown, V = unknown> extends Collection {
27
28
  key: K;
28
29
  value: V;
29
30
  }, overwrite?: boolean): void;
30
- delete(key: K): boolean;
31
- get(key: K, keepScalar?: boolean): unknown;
32
- has(key: K): boolean;
31
+ delete(key: unknown): boolean;
32
+ get(key: unknown, keepScalar: true): Scalar<V> | undefined;
33
+ get(key: unknown, keepScalar?: false): V | undefined;
34
+ get(key: unknown, keepScalar?: boolean): V | Scalar<V> | undefined;
35
+ has(key: unknown): boolean;
33
36
  set(key: K, value: V): void;
34
37
  /**
35
38
  * @param ctx - Conversion context, originally set in Document#toJS()
36
39
  * @param {Class} Type - If set, forces the returned collection type
37
40
  * @returns Instance of Type, Map, or Object
38
41
  */
39
- toJSON(_?: unknown, ctx?: ToJSContext, Type?: any): any;
42
+ toJSON<T = unknown>(_?: unknown, ctx?: ToJSContext, Type?: {
43
+ new (): T;
44
+ }): any;
40
45
  toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
41
46
  }
@@ -39,12 +39,12 @@ class YAMLMap extends Collection.Collection {
39
39
  _pair = pair;
40
40
  else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
41
41
  // In TypeScript, this never happens.
42
- _pair = new Pair.Pair(pair, pair.value);
42
+ _pair = new Pair.Pair(pair, pair?.value);
43
43
  }
44
44
  else
45
45
  _pair = new Pair.Pair(pair.key, pair.value);
46
46
  const prev = findPair(this.items, _pair.key);
47
- const sortEntries = this.schema && this.schema.sortMapEntries;
47
+ const sortEntries = this.schema?.sortMapEntries;
48
48
  if (prev) {
49
49
  if (!overwrite)
50
50
  throw new Error(`Key ${_pair.key} already set`);
@@ -74,8 +74,8 @@ class YAMLMap extends Collection.Collection {
74
74
  }
75
75
  get(key, keepScalar) {
76
76
  const it = findPair(this.items, key);
77
- const node = it && it.value;
78
- return !keepScalar && Node.isScalar(node) ? node.value : node;
77
+ const node = it?.value;
78
+ return (!keepScalar && Node.isScalar(node) ? node.value : node) ?? undefined;
79
79
  }
80
80
  has(key) {
81
81
  return !!findPair(this.items, key);
@@ -89,8 +89,8 @@ class YAMLMap extends Collection.Collection {
89
89
  * @returns Instance of Type, Map, or Object
90
90
  */
91
91
  toJSON(_, ctx, Type) {
92
- const map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {};
93
- if (ctx && ctx.onCreate)
92
+ const map = Type ? new Type() : ctx?.mapAsMap ? new Map() : {};
93
+ if (ctx?.onCreate)
94
94
  ctx.onCreate(map);
95
95
  for (const item of this.items)
96
96
  addPairToJSMap.addPairToJSMap(ctx, map, item);
@@ -106,7 +106,7 @@ class YAMLMap extends Collection.Collection {
106
106
  if (!ctx.allNullValues && this.hasAllNullValues(false))
107
107
  ctx = Object.assign({}, ctx, { allNullValues: true });
108
108
  return stringifyCollection.stringifyCollection(this, ctx, {
109
- blockItem: n => n.str,
109
+ blockItemPrefix: '',
110
110
  flowChars: { start: '{', end: '}' },
111
111
  itemIndent: ctx.indent || '',
112
112
  onChompKeep,
@@ -4,6 +4,7 @@ import type { StringifyContext } from '../stringify/stringify.js';
4
4
  import { Collection } from './Collection.js';
5
5
  import { ParsedNode, Range } from './Node.js';
6
6
  import type { Pair } from './Pair.js';
7
+ import { Scalar } from './Scalar.js';
7
8
  import { ToJSContext } from './toJS.js';
8
9
  export declare namespace YAMLSeq {
9
10
  interface Parsed<T extends ParsedNode | Pair<ParsedNode, ParsedNode | null> = ParsedNode> extends YAMLSeq<T> {
@@ -34,7 +35,9 @@ export declare class YAMLSeq<T = unknown> extends Collection {
34
35
  * `key` must contain a representation of an integer for this to succeed.
35
36
  * It may be wrapped in a `Scalar`.
36
37
  */
37
- get(key: unknown, keepScalar?: boolean): unknown;
38
+ get(key: unknown, keepScalar: true): Scalar<T> | undefined;
39
+ get(key: unknown, keepScalar?: false): T | undefined;
40
+ get(key: unknown, keepScalar?: boolean): T | Scalar<T> | undefined;
38
41
  /**
39
42
  * Checks if the collection includes a value with the key `key`.
40
43
  *
@@ -32,14 +32,6 @@ class YAMLSeq extends Collection.Collection {
32
32
  const del = this.items.splice(idx, 1);
33
33
  return del.length > 0;
34
34
  }
35
- /**
36
- * Returns item at `key`, or `undefined` if not found. By default unwraps
37
- * scalar values from their surrounding node; to disable set `keepScalar` to
38
- * `true` (collections are always returned intact).
39
- *
40
- * `key` must contain a representation of an integer for this to succeed.
41
- * It may be wrapped in a `Scalar`.
42
- */
43
35
  get(key, keepScalar) {
44
36
  const idx = asItemIndex(key);
45
37
  if (typeof idx !== 'number')
@@ -76,7 +68,7 @@ class YAMLSeq extends Collection.Collection {
76
68
  }
77
69
  toJSON(_, ctx) {
78
70
  const seq = [];
79
- if (ctx && ctx.onCreate)
71
+ if (ctx?.onCreate)
80
72
  ctx.onCreate(seq);
81
73
  let i = 0;
82
74
  for (const item of this.items)
@@ -87,7 +79,7 @@ class YAMLSeq extends Collection.Collection {
87
79
  if (!ctx)
88
80
  return JSON.stringify(this);
89
81
  return stringifyCollection.stringifyCollection(this, ctx, {
90
- blockItem: n => (n.comment ? n.str : `- ${n.str}`),
82
+ blockItemPrefix: '- ',
91
83
  flowChars: { start: '[', end: ']' },
92
84
  itemIndent: (ctx.indent || '') + ' ',
93
85
  onChompKeep,
@@ -8,7 +8,7 @@ var toJS = require('./toJS.js');
8
8
 
9
9
  const MERGE_KEY = '<<';
10
10
  function addPairToJSMap(ctx, map, { key, value }) {
11
- if (ctx && ctx.doc.schema.merge && isMergeKey(key)) {
11
+ if (ctx?.doc.schema.merge && isMergeKey(key)) {
12
12
  value = Node.isAlias(value) ? value.resolve(ctx.doc) : value;
13
13
  if (Node.isSeq(value))
14
14
  for (const it of value.items)
@@ -13,9 +13,11 @@ var Node = require('./Node.js');
13
13
  * stringification.
14
14
  */
15
15
  function toJS(value, arg, ctx) {
16
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
16
17
  if (Array.isArray(value))
17
18
  return value.map((v, i) => toJS(v, String(i), ctx));
18
19
  if (value && typeof value.toJSON === 'function') {
20
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
19
21
  if (!ctx || !Node.hasAnchor(value))
20
22
  return value.toJSON(arg, ctx);
21
23
  const data = { aliasCount: 0, count: 1, res: undefined };
@@ -29,7 +31,7 @@ function toJS(value, arg, ctx) {
29
31
  ctx.onCreate(res);
30
32
  return res;
31
33
  }
32
- if (typeof value === 'bigint' && !(ctx && ctx.keep))
34
+ if (typeof value === 'bigint' && !ctx?.keep)
33
35
  return Number(value);
34
36
  return value;
35
37
  }
package/dist/options.d.ts CHANGED
@@ -5,7 +5,7 @@ import type { ParsedNode } from './nodes/Node.js';
5
5
  import type { Pair } from './nodes/Pair.js';
6
6
  import type { Scalar } from './nodes/Scalar.js';
7
7
  import type { LineCounter } from './parse/line-counter.js';
8
- import type { SchemaName } from './schema/Schema.js';
8
+ import type { Schema } from './schema/Schema.js';
9
9
  import type { Tags } from './schema/tags.js';
10
10
  import type { CollectionTag, ScalarTag } from './schema/types.js';
11
11
  export declare type ParseOptions = {
@@ -58,10 +58,11 @@ export declare type ParseOptions = {
58
58
  };
59
59
  export declare type DocumentOptions = {
60
60
  /**
61
+ * @internal
61
62
  * Used internally by Composer. If set and includes an explicit version,
62
63
  * that overrides the `version` option.
63
64
  */
64
- directives?: Directives;
65
+ _directives?: Directives;
65
66
  /**
66
67
  * Control the logging level during parsing
67
68
  *
@@ -73,9 +74,17 @@ export declare type DocumentOptions = {
73
74
  *
74
75
  * Default: `"1.2"`
75
76
  */
76
- version?: '1.1' | '1.2';
77
+ version?: '1.1' | '1.2' | 'next';
77
78
  };
78
79
  export declare type SchemaOptions = {
80
+ /**
81
+ * When parsing, warn about compatibility issues with the given schema.
82
+ * When stringifying, use scalar styles that are parsed correctly
83
+ * by the `compat` schema as well as the actual schema.
84
+ *
85
+ * Default: `null`
86
+ */
87
+ compat?: string | Tags | null;
79
88
  /**
80
89
  * Array of additional tags to include in the schema, or a function that may
81
90
  * modify the schema's base tag array.
@@ -99,9 +108,18 @@ export declare type SchemaOptions = {
99
108
  /**
100
109
  * The base schema to use.
101
110
  *
102
- * Default: `"core"` for YAML 1.2, `"yaml-1.1"` for earlier versions
111
+ * The core library has built-in support for the following:
112
+ * - `'failsafe'`: A minimal schema that parses all scalars as strings
113
+ * - `'core'`: The YAML 1.2 core schema
114
+ * - `'json'`: The YAML 1.2 JSON schema, with minimal rules for JSON compatibility
115
+ * - `'yaml-1.1'`: The YAML 1.1 schema
116
+ *
117
+ * If using another (custom) schema, the `customTags` array needs to
118
+ * fully define the schema's tags.
119
+ *
120
+ * Default: `'core'` for YAML 1.2, `'yaml-1.1'` for earlier versions
103
121
  */
104
- schema?: SchemaName;
122
+ schema?: string | Schema;
105
123
  /**
106
124
  * When adding to or stringifying a map, sort the entries.
107
125
  * If `true`, sort by comparing key values with `<`.
@@ -110,6 +128,10 @@ export declare type SchemaOptions = {
110
128
  * Default: `false`
111
129
  */
112
130
  sortMapEntries?: boolean | ((a: Pair, b: Pair) => number);
131
+ /**
132
+ * Override default values for `toString()` options.
133
+ */
134
+ toStringDefaults?: ToStringOptions;
113
135
  };
114
136
  export declare type CreateNodeOptions = {
115
137
  /**
@@ -175,6 +197,23 @@ export declare type ToStringOptions = {
175
197
  * Default: `true`
176
198
  */
177
199
  blockQuote?: boolean | 'folded' | 'literal';
200
+ /**
201
+ * Enforce `'block'` or `'flow'` style on maps and sequences.
202
+ * Empty collections will always be stringified as `{}` or `[]`.
203
+ *
204
+ * Default: `'any'`, allowing each node to set its style separately
205
+ * with its `flow: boolean` (default `false`) property.
206
+ */
207
+ collectionStyle?: 'any' | 'block' | 'flow';
208
+ /**
209
+ * Comment stringifier.
210
+ * Output should be valid for the current schema.
211
+ *
212
+ * By default, empty comment lines are left empty,
213
+ * lines consisting of a single space are replaced by `#`,
214
+ * and all other lines are prefixed with a `#`.
215
+ */
216
+ commentString?: (comment: string) => string;
178
217
  /**
179
218
  * The default type of string literal used to stringify implicit key values.
180
219
  * Output may use other types if required to fully represent the value.
@@ -290,11 +329,3 @@ export declare type ToStringOptions = {
290
329
  */
291
330
  verifyAliasOrder?: boolean;
292
331
  };
293
- /**
294
- * `yaml` defines document-specific options in three places: as an argument of
295
- * parse, create and stringify calls, in the values of `YAML.defaultOptions`,
296
- * and in the version-dependent `YAML.Document.defaults` object. Values set in
297
- * `YAML.defaultOptions` override version-dependent defaults, and argument
298
- * options override both.
299
- */
300
- export declare const defaultOptions: Required<Omit<ParseOptions, 'lineCounter'> & Omit<DocumentOptions, 'directives'>>;
@@ -40,7 +40,6 @@ function resolveAsScalar(token, strict = true, onError) {
40
40
  * @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
41
41
  */
42
42
  function createScalarToken(value, context) {
43
- var _a;
44
43
  const { implicitKey = false, indent, inFlow = false, offset = -1, type = 'PLAIN' } = context;
45
44
  const source = stringifyString.stringifyString({ type, value }, {
46
45
  implicitKey,
@@ -48,7 +47,7 @@ function createScalarToken(value, context) {
48
47
  inFlow,
49
48
  options: { blockQuote: true, lineWidth: -1 }
50
49
  });
51
- const end = (_a = context.end) !== null && _a !== void 0 ? _a : [
50
+ const end = context.end ?? [
52
51
  { type: 'newline', offset: -1, indent, source: '\n' }
53
52
  ];
54
53
  switch (source[0]) {
@@ -49,7 +49,7 @@ visit.REMOVE = REMOVE;
49
49
  visit.itemAtPath = (cst, path) => {
50
50
  let item = cst;
51
51
  for (const [field, index] of path) {
52
- const tok = item && item[field];
52
+ const tok = item?.[field];
53
53
  if (tok && 'items' in tok) {
54
54
  item = tok.items[index];
55
55
  }
@@ -66,7 +66,7 @@ visit.itemAtPath = (cst, path) => {
66
66
  visit.parentCollection = (cst, path) => {
67
67
  const parent = visit.itemAtPath(cst, path.slice(0, -1));
68
68
  const field = path[path.length - 1][0];
69
- const coll = parent && parent[field];
69
+ const coll = parent?.[field];
70
70
  if (coll && 'items' in coll)
71
71
  return coll;
72
72
  throw new Error('Parent collection not found');
@@ -155,7 +155,7 @@ class Lexer {
155
155
  this.lineEndPos = null;
156
156
  }
157
157
  this.atEnd = !incomplete;
158
- let next = this.next || 'stream';
158
+ let next = this.next ?? 'stream';
159
159
  while (next && (incomplete || this.hasChars(1)))
160
160
  next = yield* this.parseNext(next);
161
161
  }
@@ -359,9 +359,14 @@ class Lexer {
359
359
  let indent = -1;
360
360
  do {
361
361
  nl = yield* this.pushNewline();
362
- sp = yield* this.pushSpaces(true);
363
- if (nl > 0)
362
+ if (nl > 0) {
363
+ sp = yield* this.pushSpaces(false);
364
364
  this.indentValue = indent = sp;
365
+ }
366
+ else {
367
+ sp = 0;
368
+ }
369
+ sp += yield* this.pushSpaces(true);
365
370
  } while (nl + sp > 0);
366
371
  const line = this.getLine();
367
372
  if (line === null)
@@ -418,6 +423,7 @@ class Lexer {
418
423
  case ':': {
419
424
  const next = this.charAt(1);
420
425
  if (this.flowKey || isEmpty(next) || next === ',') {
426
+ this.flowKey = false;
421
427
  yield* this.pushCount(1);
422
428
  yield* this.pushSpaces(true);
423
429
  return 'flow';
@@ -534,9 +540,10 @@ class Lexer {
534
540
  let ch = this.buffer[i];
535
541
  if (ch === '\r')
536
542
  ch = this.buffer[--i];
543
+ const lastChar = i; // Drop the line if last char not more indented
537
544
  while (ch === ' ' || ch === '\t')
538
545
  ch = this.buffer[--i];
539
- if (ch === '\n' && i >= this.pos)
546
+ if (ch === '\n' && i >= this.pos && i + 1 + indent > lastChar)
540
547
  nl = i;
541
548
  else
542
549
  break;
@@ -619,16 +626,21 @@ class Lexer {
619
626
  return ((yield* this.pushUntil(isNotAnchorChar)) +
620
627
  (yield* this.pushSpaces(true)) +
621
628
  (yield* this.pushIndicators()));
622
- case ':':
623
- case '?': // this is an error outside flow collections
624
629
  case '-': // this is an error
625
- if (isEmpty(this.charAt(1))) {
626
- if (this.flowLevel === 0)
630
+ case '?': // this is an error outside flow collections
631
+ case ':': {
632
+ const inFlow = this.flowLevel > 0;
633
+ const ch1 = this.charAt(1);
634
+ if (isEmpty(ch1) || (inFlow && invalidFlowScalarChars.includes(ch1))) {
635
+ if (!inFlow)
627
636
  this.indentNext = this.indentValue + 1;
637
+ else if (this.flowKey)
638
+ this.flowKey = false;
628
639
  return ((yield* this.pushCount(1)) +
629
640
  (yield* this.pushSpaces(true)) +
630
641
  (yield* this.pushIndicators()));
631
642
  }
643
+ }
632
644
  }
633
645
  return 0;
634
646
  }