yaml 2.0.0-8 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/README.md +1 -1
  2. package/browser/dist/compose/compose-doc.js +3 -2
  3. package/browser/dist/compose/compose-node.js +14 -3
  4. package/browser/dist/compose/compose-scalar.js +16 -10
  5. package/browser/dist/compose/composer.js +6 -7
  6. package/browser/dist/compose/resolve-block-map.js +24 -9
  7. package/browser/dist/compose/resolve-block-scalar.js +12 -3
  8. package/browser/dist/compose/resolve-block-seq.js +5 -0
  9. package/browser/dist/compose/resolve-flow-collection.js +18 -6
  10. package/browser/dist/compose/resolve-flow-scalar.js +2 -1
  11. package/browser/dist/compose/resolve-props.js +8 -0
  12. package/browser/dist/compose/util-flow-indent-check.js +15 -0
  13. package/browser/dist/doc/Document.js +49 -16
  14. package/browser/dist/doc/anchors.js +2 -2
  15. package/browser/dist/doc/createNode.js +7 -4
  16. package/browser/dist/doc/directives.js +7 -4
  17. package/browser/dist/index.js +1 -2
  18. package/browser/dist/node_modules/tslib/tslib.es6.js +164 -0
  19. package/browser/dist/nodes/Collection.js +8 -4
  20. package/browser/dist/nodes/Pair.js +2 -2
  21. package/browser/dist/nodes/Scalar.js +1 -1
  22. package/browser/dist/nodes/YAMLMap.js +6 -5
  23. package/browser/dist/nodes/YAMLSeq.js +2 -2
  24. package/browser/dist/nodes/addPairToJSMap.js +3 -2
  25. package/browser/dist/nodes/toJS.js +3 -1
  26. package/browser/dist/parse/cst-scalar.js +2 -6
  27. package/browser/dist/parse/cst-visit.js +2 -2
  28. package/browser/dist/parse/lexer.js +27 -11
  29. package/browser/dist/parse/parser.js +135 -57
  30. package/browser/dist/public-api.js +4 -5
  31. package/browser/dist/schema/Schema.js +14 -4
  32. package/browser/dist/schema/common/null.js +3 -1
  33. package/browser/dist/schema/core/float.js +4 -1
  34. package/browser/dist/schema/tags.js +17 -13
  35. package/browser/dist/schema/yaml-1.1/float.js +4 -1
  36. package/browser/dist/schema/yaml-1.1/omap.js +1 -1
  37. package/browser/dist/schema/yaml-1.1/pairs.js +2 -1
  38. package/browser/dist/stringify/stringify.js +53 -23
  39. package/browser/dist/stringify/stringifyCollection.js +115 -85
  40. package/browser/dist/stringify/stringifyComment.js +18 -12
  41. package/browser/dist/stringify/stringifyDocument.js +37 -15
  42. package/browser/dist/stringify/stringifyPair.js +35 -21
  43. package/browser/dist/stringify/stringifyString.js +41 -43
  44. package/browser/dist/util.js +3 -0
  45. package/browser/dist/visit.js +150 -62
  46. package/dist/compose/compose-doc.js +3 -2
  47. package/dist/compose/compose-node.d.ts +1 -0
  48. package/dist/compose/compose-node.js +14 -3
  49. package/dist/compose/compose-scalar.js +15 -9
  50. package/dist/compose/composer.d.ts +1 -1
  51. package/dist/compose/composer.js +8 -9
  52. package/dist/compose/resolve-block-map.js +24 -9
  53. package/dist/compose/resolve-block-scalar.js +12 -3
  54. package/dist/compose/resolve-block-seq.js +5 -0
  55. package/dist/compose/resolve-flow-collection.js +18 -6
  56. package/dist/compose/resolve-flow-scalar.js +2 -1
  57. package/dist/compose/resolve-props.d.ts +2 -1
  58. package/dist/compose/resolve-props.js +8 -0
  59. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  60. package/dist/compose/util-flow-indent-check.js +17 -0
  61. package/dist/doc/Document.d.ts +7 -4
  62. package/dist/doc/Document.js +56 -23
  63. package/dist/doc/anchors.d.ts +2 -2
  64. package/dist/doc/anchors.js +2 -2
  65. package/dist/doc/createNode.d.ts +1 -1
  66. package/dist/doc/createNode.js +7 -4
  67. package/dist/doc/directives.d.ts +4 -2
  68. package/dist/doc/directives.js +7 -4
  69. package/dist/index.d.ts +4 -4
  70. package/dist/index.js +1 -2
  71. package/dist/node_modules/tslib/tslib.es6.js +76 -0
  72. package/dist/nodes/Alias.d.ts +7 -3
  73. package/dist/nodes/Collection.d.ts +4 -4
  74. package/dist/nodes/Collection.js +8 -4
  75. package/dist/nodes/Node.d.ts +3 -0
  76. package/dist/nodes/Pair.d.ts +3 -0
  77. package/dist/nodes/Pair.js +2 -2
  78. package/dist/nodes/Scalar.d.ts +2 -0
  79. package/dist/nodes/Scalar.js +1 -1
  80. package/dist/nodes/YAMLMap.d.ts +5 -1
  81. package/dist/nodes/YAMLMap.js +6 -5
  82. package/dist/nodes/YAMLSeq.d.ts +2 -0
  83. package/dist/nodes/YAMLSeq.js +2 -2
  84. package/dist/nodes/addPairToJSMap.js +2 -1
  85. package/dist/nodes/toJS.js +3 -1
  86. package/dist/options.d.ts +60 -15
  87. package/dist/parse/cst-scalar.d.ts +6 -0
  88. package/dist/parse/cst-scalar.js +2 -6
  89. package/dist/parse/cst-visit.js +2 -2
  90. package/dist/parse/lexer.js +27 -11
  91. package/dist/parse/parser.js +135 -57
  92. package/dist/public-api.js +4 -5
  93. package/dist/schema/Schema.d.ts +5 -4
  94. package/dist/schema/Schema.js +13 -3
  95. package/dist/schema/common/null.js +3 -1
  96. package/dist/schema/core/float.js +4 -1
  97. package/dist/schema/tags.d.ts +1 -2
  98. package/dist/schema/tags.js +17 -13
  99. package/dist/schema/yaml-1.1/float.js +4 -1
  100. package/dist/schema/yaml-1.1/omap.js +1 -1
  101. package/dist/schema/yaml-1.1/pairs.js +2 -1
  102. package/dist/stringify/stringify.d.ts +5 -3
  103. package/dist/stringify/stringify.js +53 -23
  104. package/dist/stringify/stringifyCollection.d.ts +2 -6
  105. package/dist/stringify/stringifyCollection.js +114 -84
  106. package/dist/stringify/stringifyComment.d.ts +10 -2
  107. package/dist/stringify/stringifyComment.js +19 -12
  108. package/dist/stringify/stringifyDocument.js +36 -14
  109. package/dist/stringify/stringifyPair.js +34 -20
  110. package/dist/stringify/stringifyString.js +41 -43
  111. package/dist/test-events.d.ts +1 -1
  112. package/dist/test-events.js +13 -12
  113. package/dist/util.d.ts +3 -0
  114. package/dist/util.js +6 -0
  115. package/dist/visit.d.ts +49 -10
  116. package/dist/visit.js +149 -60
  117. package/package.json +10 -8
  118. package/util.d.ts +3 -0
  119. package/browser/dist/options.js +0 -17
  120. package/dist/options.js +0 -19
@@ -1,4 +1,5 @@
1
1
  import type { Document } from '../doc/Document.js';
2
+ import { Token } from '../parse/cst.js';
2
3
  import type { StringifyContext } from '../stringify/stringify.js';
3
4
  import type { Alias } from './Alias.js';
4
5
  import type { Pair } from './Pair.js';
@@ -39,6 +40,8 @@ export declare abstract class NodeBase {
39
40
  range?: Range | null;
40
41
  /** A blank line before this node and its commentBefore */
41
42
  spaceBefore?: boolean;
43
+ /** The CST token that was composed into this node. */
44
+ srcToken?: Token;
42
45
  /** A fully qualified tag, if required */
43
46
  tag?: string;
44
47
  /** A plain JS representation of this node */
@@ -1,4 +1,5 @@
1
1
  import { CreateNodeContext } from '../doc/createNode.js';
2
+ import type { CollectionItem } from '../parse/cst.js';
2
3
  import type { Schema } from '../schema/Schema.js';
3
4
  import type { StringifyContext } from '../stringify/stringify.js';
4
5
  import { addPairToJSMap } from './addPairToJSMap.js';
@@ -11,6 +12,8 @@ export declare class Pair<K = unknown, V = unknown> {
11
12
  key: K;
12
13
  /** Always Node or null when parsed, but can be set to anything. */
13
14
  value: V | null;
15
+ /** The CST token that was composed into this pair. */
16
+ srcToken?: CollectionItem;
14
17
  constructor(key: K, value?: V | null);
15
18
  clone(schema?: Schema): Pair<K, V>;
16
19
  toJSON(_?: unknown, ctx?: ToJSContext): ReturnType<typeof addPairToJSMap>;
@@ -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 === null || ctx === void 0 ? void 0 : 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 === null || ctx === void 0 ? void 0 : ctx.doc)
33
33
  ? stringifyPair.stringifyPair(this, ctx, onComment, onChompKeep)
34
34
  : JSON.stringify(this);
35
35
  }
@@ -1,3 +1,4 @@
1
+ import type { BlockScalar, FlowScalar } from '../parse/cst.js';
1
2
  import { NodeBase, Range } from './Node.js';
2
3
  import { ToJSContext } from './toJS.js';
3
4
  export declare const isScalarValue: (value: unknown) => boolean;
@@ -5,6 +6,7 @@ export declare namespace Scalar {
5
6
  interface Parsed extends Scalar {
6
7
  range: Range;
7
8
  source: string;
9
+ srcToken?: FlowScalar | BlockScalar;
8
10
  }
9
11
  type BLOCK_FOLDED = 'BLOCK_FOLDED';
10
12
  type BLOCK_LITERAL = 'BLOCK_LITERAL';
@@ -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 === null || ctx === void 0 ? void 0 : ctx.keep) ? this.value : toJS.toJS(this.value, arg, ctx);
14
14
  }
15
15
  toString() {
16
16
  return String(this.value);
@@ -1,3 +1,4 @@
1
+ import type { BlockMap, FlowCollection } from '../parse/cst.js';
1
2
  import type { Schema } from '../schema/Schema.js';
2
3
  import type { StringifyContext } from '../stringify/stringify.js';
3
4
  import { Collection } from './Collection.js';
@@ -9,6 +10,7 @@ export declare namespace YAMLMap {
9
10
  interface Parsed<K extends ParsedNode = ParsedNode, V extends ParsedNode | null = ParsedNode | null> extends YAMLMap<K, V> {
10
11
  items: Pair<K, V>[];
11
12
  range: Range;
13
+ srcToken?: BlockMap | FlowCollection;
12
14
  }
13
15
  }
14
16
  export declare class YAMLMap<K = unknown, V = unknown> extends Collection {
@@ -34,6 +36,8 @@ export declare class YAMLMap<K = unknown, V = unknown> extends Collection {
34
36
  * @param {Class} Type - If set, forces the returned collection type
35
37
  * @returns Instance of Type, Map, or Object
36
38
  */
37
- toJSON(_?: unknown, ctx?: ToJSContext, Type?: any): any;
39
+ toJSON<T = unknown>(_?: unknown, ctx?: ToJSContext, Type?: {
40
+ new (): T;
41
+ }): any;
38
42
  toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
39
43
  }
@@ -34,6 +34,7 @@ class YAMLMap extends Collection.Collection {
34
34
  * collection will throw. Otherwise, overwrites the previous value.
35
35
  */
36
36
  add(pair, overwrite) {
37
+ var _a;
37
38
  let _pair;
38
39
  if (Node.isPair(pair))
39
40
  _pair = pair;
@@ -44,7 +45,7 @@ class YAMLMap extends Collection.Collection {
44
45
  else
45
46
  _pair = new Pair.Pair(pair.key, pair.value);
46
47
  const prev = findPair(this.items, _pair.key);
47
- const sortEntries = this.schema && this.schema.sortMapEntries;
48
+ const sortEntries = (_a = this.schema) === null || _a === void 0 ? void 0 : _a.sortMapEntries;
48
49
  if (prev) {
49
50
  if (!overwrite)
50
51
  throw new Error(`Key ${_pair.key} already set`);
@@ -74,7 +75,7 @@ class YAMLMap extends Collection.Collection {
74
75
  }
75
76
  get(key, keepScalar) {
76
77
  const it = findPair(this.items, key);
77
- const node = it && it.value;
78
+ const node = it === null || it === void 0 ? void 0 : it.value;
78
79
  return !keepScalar && Node.isScalar(node) ? node.value : node;
79
80
  }
80
81
  has(key) {
@@ -89,8 +90,8 @@ class YAMLMap extends Collection.Collection {
89
90
  * @returns Instance of Type, Map, or Object
90
91
  */
91
92
  toJSON(_, ctx, Type) {
92
- const map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {};
93
- if (ctx && ctx.onCreate)
93
+ const map = Type ? new Type() : (ctx === null || ctx === void 0 ? void 0 : ctx.mapAsMap) ? new Map() : {};
94
+ if (ctx === null || ctx === void 0 ? void 0 : ctx.onCreate)
94
95
  ctx.onCreate(map);
95
96
  for (const item of this.items)
96
97
  addPairToJSMap.addPairToJSMap(ctx, map, item);
@@ -106,7 +107,7 @@ class YAMLMap extends Collection.Collection {
106
107
  if (!ctx.allNullValues && this.hasAllNullValues(false))
107
108
  ctx = Object.assign({}, ctx, { allNullValues: true });
108
109
  return stringifyCollection.stringifyCollection(this, ctx, {
109
- blockItem: n => n.str,
110
+ blockItemPrefix: '',
110
111
  flowChars: { start: '{', end: '}' },
111
112
  itemIndent: ctx.indent || '',
112
113
  onChompKeep,
@@ -1,3 +1,4 @@
1
+ import type { BlockSequence, FlowCollection } from '../parse/cst.js';
1
2
  import type { Schema } from '../schema/Schema.js';
2
3
  import type { StringifyContext } from '../stringify/stringify.js';
3
4
  import { Collection } from './Collection.js';
@@ -8,6 +9,7 @@ export declare namespace YAMLSeq {
8
9
  interface Parsed<T extends ParsedNode | Pair<ParsedNode, ParsedNode | null> = ParsedNode> extends YAMLSeq<T> {
9
10
  items: T[];
10
11
  range: Range;
12
+ srcToken?: BlockSequence | FlowCollection;
11
13
  }
12
14
  }
13
15
  export declare class YAMLSeq<T = unknown> extends Collection {
@@ -76,7 +76,7 @@ class YAMLSeq extends Collection.Collection {
76
76
  }
77
77
  toJSON(_, ctx) {
78
78
  const seq = [];
79
- if (ctx && ctx.onCreate)
79
+ if (ctx === null || ctx === void 0 ? void 0 : ctx.onCreate)
80
80
  ctx.onCreate(seq);
81
81
  let i = 0;
82
82
  for (const item of this.items)
@@ -87,7 +87,7 @@ class YAMLSeq extends Collection.Collection {
87
87
  if (!ctx)
88
88
  return JSON.stringify(this);
89
89
  return stringifyCollection.stringifyCollection(this, ctx, {
90
- blockItem: n => (n.comment ? n.str : `- ${n.str}`),
90
+ blockItemPrefix: '- ',
91
91
  flowChars: { start: '[', end: ']' },
92
92
  itemIndent: (ctx.indent || '') + ' ',
93
93
  onChompKeep,
@@ -8,7 +8,8 @@ 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 === null || ctx === void 0 ? void 0 : ctx.doc.schema.merge) && isMergeKey(key)) {
12
+ value = Node.isAlias(value) ? value.resolve(ctx.doc) : value;
12
13
  if (Node.isSeq(value))
13
14
  for (const it of value.items)
14
15
  mergeToJSMap(ctx, map, it);
@@ -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 === null || ctx === void 0 ? void 0 : 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 = {
@@ -17,6 +17,13 @@ export declare type ParseOptions = {
17
17
  * https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/BigInt
18
18
  */
19
19
  intAsBigInt?: boolean;
20
+ /**
21
+ * Include a `srcToken` value on each parsed `Node`, containing the CST token
22
+ * that was composed into this node.
23
+ *
24
+ * Default: `false`
25
+ */
26
+ keepSourceTokens?: boolean;
20
27
  /**
21
28
  * If set, newlines will be tracked, to allow for `lineCounter.linePos(offset)`
22
29
  * to provide the `{ line, col }` positions within the input.
@@ -66,9 +73,17 @@ export declare type DocumentOptions = {
66
73
  *
67
74
  * Default: `"1.2"`
68
75
  */
69
- version?: '1.1' | '1.2';
76
+ version?: '1.1' | '1.2' | 'next';
70
77
  };
71
78
  export declare type SchemaOptions = {
79
+ /**
80
+ * When parsing, warn about compatibility issues with the given schema.
81
+ * When stringifying, use scalar styles that are parsed correctly
82
+ * by the `compat` schema as well as the actual schema.
83
+ *
84
+ * Default: `null`
85
+ */
86
+ compat?: string | Tags | null;
72
87
  /**
73
88
  * Array of additional tags to include in the schema, or a function that may
74
89
  * modify the schema's base tag array.
@@ -92,9 +107,18 @@ export declare type SchemaOptions = {
92
107
  /**
93
108
  * The base schema to use.
94
109
  *
95
- * Default: `"core"` for YAML 1.2, `"yaml-1.1"` for earlier versions
110
+ * The core library has built-in support for the following:
111
+ * - `'failsafe'`: A minimal schema that parses all scalars as strings
112
+ * - `'core'`: The YAML 1.2 core schema
113
+ * - `'json'`: The YAML 1.2 JSON schema, with minimal rules for JSON compatibility
114
+ * - `'yaml-1.1'`: The YAML 1.1 schema
115
+ *
116
+ * If using another (custom) schema, the `customTags` array needs to
117
+ * fully define the schema's tags.
118
+ *
119
+ * Default: `'core'` for YAML 1.2, `'yaml-1.1'` for earlier versions
96
120
  */
97
- schema?: SchemaName;
121
+ schema?: string | Schema;
98
122
  /**
99
123
  * When adding to or stringifying a map, sort the entries.
100
124
  * If `true`, sort by comparing key values with `<`.
@@ -103,6 +127,10 @@ export declare type SchemaOptions = {
103
127
  * Default: `false`
104
128
  */
105
129
  sortMapEntries?: boolean | ((a: Pair, b: Pair) => number);
130
+ /**
131
+ * Override default values for `toString()` options.
132
+ */
133
+ toStringDefaults?: ToStringOptions;
106
134
  };
107
135
  export declare type CreateNodeOptions = {
108
136
  /**
@@ -161,6 +189,30 @@ export declare type ToJSOptions = {
161
189
  reviver?: Reviver;
162
190
  };
163
191
  export declare type ToStringOptions = {
192
+ /**
193
+ * Use block quote styles for scalar values where applicable.
194
+ * Set to `false` to disable block quotes completely.
195
+ *
196
+ * Default: `true`
197
+ */
198
+ blockQuote?: boolean | 'folded' | 'literal';
199
+ /**
200
+ * Enforce `'block'` or `'flow'` style on maps and sequences.
201
+ * Empty collections will always be stringified as `{}` or `[]`.
202
+ *
203
+ * Default: `'any'`, allowing each node to set its style separately
204
+ * with its `flow: boolean` (default `false`) property.
205
+ */
206
+ collectionStyle?: 'any' | 'block' | 'flow';
207
+ /**
208
+ * Comment stringifier.
209
+ * Output should be valid for the current schema.
210
+ *
211
+ * By default, empty comment lines are left empty,
212
+ * lines consisting of a single space are replaced by `#`,
213
+ * and all other lines are prefixed with a `#`.
214
+ */
215
+ commentString?: (comment: string) => string;
164
216
  /**
165
217
  * The default type of string literal used to stringify implicit key values.
166
218
  * Output may use other types if required to fully represent the value.
@@ -254,11 +306,12 @@ export declare type ToStringOptions = {
254
306
  */
255
307
  simpleKeys?: boolean;
256
308
  /**
257
- * Prefer 'single quote' rather than "double quote" where applicable.
309
+ * Use 'single quote' rather than "double quote" where applicable.
310
+ * Set to `false` to disable single quotes completely.
258
311
  *
259
- * Default: `false`
312
+ * Default: `null`
260
313
  */
261
- singleQuote?: boolean;
314
+ singleQuote?: boolean | null;
262
315
  /**
263
316
  * String representation for `true`.
264
317
  * With the core schema, use `'true'`, `'True'`, or `'TRUE'`.
@@ -275,11 +328,3 @@ export declare type ToStringOptions = {
275
328
  */
276
329
  verifyAliasOrder?: boolean;
277
330
  };
278
- /**
279
- * `yaml` defines document-specific options in three places: as an argument of
280
- * parse, create and stringify calls, in the values of `YAML.defaultOptions`,
281
- * and in the version-dependent `YAML.Document.defaults` object. Values set in
282
- * `YAML.defaultOptions` override version-dependent defaults, and argument
283
- * options override both.
284
- */
285
- export declare const defaultOptions: Required<Omit<ParseOptions, 'lineCounter'> & Omit<DocumentOptions, 'directives'>>;
@@ -6,6 +6,12 @@ import type { BlockScalar, FlowScalar, SourceToken, Token } from './cst.js';
6
6
  * If `token` is a CST flow or block scalar, determine its string value and a few other attributes.
7
7
  * Otherwise, return `null`.
8
8
  */
9
+ export declare function resolveAsScalar(token: FlowScalar | BlockScalar, strict?: boolean, onError?: (offset: number, code: ErrorCode, message: string) => void): {
10
+ value: string;
11
+ type: Scalar.Type | null;
12
+ comment: string;
13
+ range: Range;
14
+ };
9
15
  export declare function resolveAsScalar(token: Token | null | undefined, strict?: boolean, onError?: (offset: number, code: ErrorCode, message: string) => void): {
10
16
  value: string;
11
17
  type: Scalar.Type | null;
@@ -5,10 +5,6 @@ var resolveFlowScalar = require('../compose/resolve-flow-scalar.js');
5
5
  var errors = require('../errors.js');
6
6
  var stringifyString = require('../stringify/stringifyString.js');
7
7
 
8
- /**
9
- * If `token` is a CST flow or block scalar, determine its string value and a few other attributes.
10
- * Otherwise, return `null`.
11
- */
12
8
  function resolveAsScalar(token, strict = true, onError) {
13
9
  if (token) {
14
10
  const _onError = (pos, code, message) => {
@@ -50,7 +46,7 @@ function createScalarToken(value, context) {
50
46
  implicitKey,
51
47
  indent: indent > 0 ? ' '.repeat(indent) : '',
52
48
  inFlow,
53
- options: { lineWidth: -1 }
49
+ options: { blockQuote: true, lineWidth: -1 }
54
50
  });
55
51
  const end = (_a = context.end) !== null && _a !== void 0 ? _a : [
56
52
  { type: 'newline', offset: -1, indent, source: '\n' }
@@ -119,7 +115,7 @@ function setScalarValue(token, value, context = {}) {
119
115
  implicitKey: implicitKey || indent === null,
120
116
  indent: indent !== null && indent > 0 ? ' '.repeat(indent) : '',
121
117
  inFlow,
122
- options: { lineWidth: -1 }
118
+ options: { blockQuote: true, lineWidth: -1 }
123
119
  });
124
120
  switch (source[0]) {
125
121
  case '|':
@@ -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 === null || item === void 0 ? void 0 : 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 === null || parent === void 0 ? void 0 : parent[field];
70
70
  if (coll && 'items' in coll)
71
71
  return coll;
72
72
  throw new Error('Parent collection not found');
@@ -150,12 +150,13 @@ class Lexer {
150
150
  * @returns A generator of lexical tokens
151
151
  */
152
152
  *lex(source, incomplete = false) {
153
+ var _a;
153
154
  if (source) {
154
155
  this.buffer = this.buffer ? this.buffer + source : source;
155
156
  this.lineEndPos = null;
156
157
  }
157
158
  this.atEnd = !incomplete;
158
- let next = this.next || 'stream';
159
+ let next = (_a = this.next) !== null && _a !== void 0 ? _a : 'stream';
159
160
  while (next && (incomplete || this.hasChars(1)))
160
161
  next = yield* this.parseNext(next);
161
162
  }
@@ -359,14 +360,19 @@ class Lexer {
359
360
  let indent = -1;
360
361
  do {
361
362
  nl = yield* this.pushNewline();
362
- sp = yield* this.pushSpaces(true);
363
- if (nl > 0)
363
+ if (nl > 0) {
364
+ sp = yield* this.pushSpaces(false);
364
365
  this.indentValue = indent = sp;
366
+ }
367
+ else {
368
+ sp = 0;
369
+ }
370
+ sp += yield* this.pushSpaces(true);
365
371
  } while (nl + sp > 0);
366
372
  const line = this.getLine();
367
373
  if (line === null)
368
374
  return this.setNext('flow');
369
- if ((indent !== -1 && indent < this.indentNext) ||
375
+ if ((indent !== -1 && indent < this.indentNext && line[0] !== '#') ||
370
376
  (indent === 0 &&
371
377
  (line.startsWith('---') || line.startsWith('...')) &&
372
378
  isEmpty(line[3]))) {
@@ -384,8 +390,11 @@ class Lexer {
384
390
  }
385
391
  }
386
392
  let n = 0;
387
- while (line[n] === ',')
388
- n += (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
393
+ while (line[n] === ',') {
394
+ n += yield* this.pushCount(1);
395
+ n += yield* this.pushSpaces(true);
396
+ this.flowKey = false;
397
+ }
389
398
  n += yield* this.pushIndicators();
390
399
  switch (line[n]) {
391
400
  case undefined:
@@ -415,6 +424,7 @@ class Lexer {
415
424
  case ':': {
416
425
  const next = this.charAt(1);
417
426
  if (this.flowKey || isEmpty(next) || next === ',') {
427
+ this.flowKey = false;
418
428
  yield* this.pushCount(1);
419
429
  yield* this.pushSpaces(true);
420
430
  return 'flow';
@@ -531,9 +541,10 @@ class Lexer {
531
541
  let ch = this.buffer[i];
532
542
  if (ch === '\r')
533
543
  ch = this.buffer[--i];
544
+ const lastChar = i; // Drop the line if last char not more indented
534
545
  while (ch === ' ' || ch === '\t')
535
546
  ch = this.buffer[--i];
536
- if (ch === '\n' && i >= this.pos)
547
+ if (ch === '\n' && i >= this.pos && i + 1 + indent > lastChar)
537
548
  nl = i;
538
549
  else
539
550
  break;
@@ -616,16 +627,21 @@ class Lexer {
616
627
  return ((yield* this.pushUntil(isNotAnchorChar)) +
617
628
  (yield* this.pushSpaces(true)) +
618
629
  (yield* this.pushIndicators()));
619
- case ':':
620
- case '?': // this is an error outside flow collections
621
630
  case '-': // this is an error
622
- if (isEmpty(this.charAt(1))) {
623
- if (this.flowLevel === 0)
631
+ case '?': // this is an error outside flow collections
632
+ case ':': {
633
+ const inFlow = this.flowLevel > 0;
634
+ const ch1 = this.charAt(1);
635
+ if (isEmpty(ch1) || (inFlow && invalidFlowScalarChars.includes(ch1))) {
636
+ if (!inFlow)
624
637
  this.indentNext = this.indentValue + 1;
638
+ else if (this.flowKey)
639
+ this.flowKey = false;
625
640
  return ((yield* this.pushCount(1)) +
626
641
  (yield* this.pushSpaces(true)) +
627
642
  (yield* this.pushIndicators()));
628
643
  }
644
+ }
629
645
  }
630
646
  return 0;
631
647
  }