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
@@ -1,4 +1,4 @@
1
- import { isDocument, isMap, isSeq, isPair, isScalar, isAlias, isNode, isCollection } from './nodes/Node.js';
1
+ import { isDocument, isNode, isPair, isCollection, isMap, isSeq, isScalar, isAlias } from './nodes/Node.js';
2
2
 
3
3
  const BREAK = Symbol('break visit');
4
4
  const SKIP = Symbol('skip children');
@@ -34,29 +34,14 @@ const REMOVE = Symbol('remove node');
34
34
  * specific defined one will be used for each node.
35
35
  */
36
36
  function visit(node, visitor) {
37
- if (typeof visitor === 'object' &&
38
- (visitor.Collection || visitor.Node || visitor.Value)) {
39
- visitor = Object.assign({
40
- Alias: visitor.Node,
41
- Map: visitor.Node,
42
- Scalar: visitor.Node,
43
- Seq: visitor.Node
44
- }, visitor.Value && {
45
- Map: visitor.Value,
46
- Scalar: visitor.Value,
47
- Seq: visitor.Value
48
- }, visitor.Collection && {
49
- Map: visitor.Collection,
50
- Seq: visitor.Collection
51
- }, visitor);
52
- }
37
+ const visitor_ = initVisitor(visitor);
53
38
  if (isDocument(node)) {
54
- const cd = _visit(null, node.contents, visitor, Object.freeze([node]));
39
+ const cd = visit_(null, node.contents, visitor_, Object.freeze([node]));
55
40
  if (cd === REMOVE)
56
41
  node.contents = null;
57
42
  }
58
43
  else
59
- _visit(null, node, visitor, Object.freeze([]));
44
+ visit_(null, node, visitor_, Object.freeze([]));
60
45
  }
61
46
  // Without the `as symbol` casts, TS declares these in the `visit`
62
47
  // namespace using `var`, but then complains about that because
@@ -67,55 +52,104 @@ visit.BREAK = BREAK;
67
52
  visit.SKIP = SKIP;
68
53
  /** Remove the current node */
69
54
  visit.REMOVE = REMOVE;
70
- function _visit(key, node, visitor, path) {
71
- let ctrl = undefined;
72
- if (typeof visitor === 'function')
73
- ctrl = visitor(key, node, path);
74
- else if (isMap(node)) {
75
- if (visitor.Map)
76
- ctrl = visitor.Map(key, node, path);
77
- }
78
- else if (isSeq(node)) {
79
- if (visitor.Seq)
80
- ctrl = visitor.Seq(key, node, path);
81
- }
82
- else if (isPair(node)) {
83
- if (visitor.Pair)
84
- ctrl = visitor.Pair(key, node, path);
55
+ function visit_(key, node, visitor, path) {
56
+ const ctrl = callVisitor(key, node, visitor, path);
57
+ if (isNode(ctrl) || isPair(ctrl)) {
58
+ replaceNode(key, path, ctrl);
59
+ return visit_(key, ctrl, visitor, path);
85
60
  }
86
- else if (isScalar(node)) {
87
- if (visitor.Scalar)
88
- ctrl = visitor.Scalar(key, node, path);
61
+ if (typeof ctrl !== 'symbol') {
62
+ if (isCollection(node)) {
63
+ path = Object.freeze(path.concat(node));
64
+ for (let i = 0; i < node.items.length; ++i) {
65
+ const ci = visit_(i, node.items[i], visitor, path);
66
+ if (typeof ci === 'number')
67
+ i = ci - 1;
68
+ else if (ci === BREAK)
69
+ return BREAK;
70
+ else if (ci === REMOVE) {
71
+ node.items.splice(i, 1);
72
+ i -= 1;
73
+ }
74
+ }
75
+ }
76
+ else if (isPair(node)) {
77
+ path = Object.freeze(path.concat(node));
78
+ const ck = visit_('key', node.key, visitor, path);
79
+ if (ck === BREAK)
80
+ return BREAK;
81
+ else if (ck === REMOVE)
82
+ node.key = null;
83
+ const cv = visit_('value', node.value, visitor, path);
84
+ if (cv === BREAK)
85
+ return BREAK;
86
+ else if (cv === REMOVE)
87
+ node.value = null;
88
+ }
89
89
  }
90
- else if (isAlias(node)) {
91
- if (visitor.Alias)
92
- ctrl = visitor.Alias(key, node, path);
90
+ return ctrl;
91
+ }
92
+ /**
93
+ * Apply an async visitor to an AST node or document.
94
+ *
95
+ * Walks through the tree (depth-first) starting from `node`, calling a
96
+ * `visitor` function with three arguments:
97
+ * - `key`: For sequence values and map `Pair`, the node's index in the
98
+ * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
99
+ * `null` for the root node.
100
+ * - `node`: The current node.
101
+ * - `path`: The ancestry of the current node.
102
+ *
103
+ * The return value of the visitor may be used to control the traversal:
104
+ * - `Promise`: Must resolve to one of the following values
105
+ * - `undefined` (default): Do nothing and continue
106
+ * - `visit.SKIP`: Do not visit the children of this node, continue with next
107
+ * sibling
108
+ * - `visit.BREAK`: Terminate traversal completely
109
+ * - `visit.REMOVE`: Remove the current node, then continue with the next one
110
+ * - `Node`: Replace the current node, then continue by visiting it
111
+ * - `number`: While iterating the items of a sequence or map, set the index
112
+ * of the next step. This is useful especially if the index of the current
113
+ * node has changed.
114
+ *
115
+ * If `visitor` is a single function, it will be called with all values
116
+ * encountered in the tree, including e.g. `null` values. Alternatively,
117
+ * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
118
+ * `Alias` and `Scalar` node. To define the same visitor function for more than
119
+ * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
120
+ * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
121
+ * specific defined one will be used for each node.
122
+ */
123
+ async function visitAsync(node, visitor) {
124
+ const visitor_ = initVisitor(visitor);
125
+ if (isDocument(node)) {
126
+ const cd = await visitAsync_(null, node.contents, visitor_, Object.freeze([node]));
127
+ if (cd === REMOVE)
128
+ node.contents = null;
93
129
  }
130
+ else
131
+ await visitAsync_(null, node, visitor_, Object.freeze([]));
132
+ }
133
+ // Without the `as symbol` casts, TS declares these in the `visit`
134
+ // namespace using `var`, but then complains about that because
135
+ // `unique symbol` must be `const`.
136
+ /** Terminate visit traversal completely */
137
+ visitAsync.BREAK = BREAK;
138
+ /** Do not visit the children of the current node */
139
+ visitAsync.SKIP = SKIP;
140
+ /** Remove the current node */
141
+ visitAsync.REMOVE = REMOVE;
142
+ async function visitAsync_(key, node, visitor, path) {
143
+ const ctrl = await callVisitor(key, node, visitor, path);
94
144
  if (isNode(ctrl) || isPair(ctrl)) {
95
- const parent = path[path.length - 1];
96
- if (isCollection(parent)) {
97
- parent.items[key] = ctrl;
98
- }
99
- else if (isPair(parent)) {
100
- if (key === 'key')
101
- parent.key = ctrl;
102
- else
103
- parent.value = ctrl;
104
- }
105
- else if (isDocument(parent)) {
106
- parent.contents = ctrl;
107
- }
108
- else {
109
- const pt = isAlias(parent) ? 'alias' : 'scalar';
110
- throw new Error(`Cannot replace node with ${pt} parent`);
111
- }
112
- return _visit(key, ctrl, visitor, path);
145
+ replaceNode(key, path, ctrl);
146
+ return visitAsync_(key, ctrl, visitor, path);
113
147
  }
114
148
  if (typeof ctrl !== 'symbol') {
115
149
  if (isCollection(node)) {
116
150
  path = Object.freeze(path.concat(node));
117
151
  for (let i = 0; i < node.items.length; ++i) {
118
- const ci = _visit(i, node.items[i], visitor, path);
152
+ const ci = await visitAsync_(i, node.items[i], visitor, path);
119
153
  if (typeof ci === 'number')
120
154
  i = ci - 1;
121
155
  else if (ci === BREAK)
@@ -128,12 +162,12 @@ function _visit(key, node, visitor, path) {
128
162
  }
129
163
  else if (isPair(node)) {
130
164
  path = Object.freeze(path.concat(node));
131
- const ck = _visit('key', node.key, visitor, path);
165
+ const ck = await visitAsync_('key', node.key, visitor, path);
132
166
  if (ck === BREAK)
133
167
  return BREAK;
134
168
  else if (ck === REMOVE)
135
169
  node.key = null;
136
- const cv = _visit('value', node.value, visitor, path);
170
+ const cv = await visitAsync_('value', node.value, visitor, path);
137
171
  if (cv === BREAK)
138
172
  return BREAK;
139
173
  else if (cv === REMOVE)
@@ -142,5 +176,58 @@ function _visit(key, node, visitor, path) {
142
176
  }
143
177
  return ctrl;
144
178
  }
179
+ function initVisitor(visitor) {
180
+ if (typeof visitor === 'object' &&
181
+ (visitor.Collection || visitor.Node || visitor.Value)) {
182
+ return Object.assign({
183
+ Alias: visitor.Node,
184
+ Map: visitor.Node,
185
+ Scalar: visitor.Node,
186
+ Seq: visitor.Node
187
+ }, visitor.Value && {
188
+ Map: visitor.Value,
189
+ Scalar: visitor.Value,
190
+ Seq: visitor.Value
191
+ }, visitor.Collection && {
192
+ Map: visitor.Collection,
193
+ Seq: visitor.Collection
194
+ }, visitor);
195
+ }
196
+ return visitor;
197
+ }
198
+ function callVisitor(key, node, visitor, path) {
199
+ if (typeof visitor === 'function')
200
+ return visitor(key, node, path);
201
+ if (isMap(node))
202
+ return visitor.Map?.(key, node, path);
203
+ if (isSeq(node))
204
+ return visitor.Seq?.(key, node, path);
205
+ if (isPair(node))
206
+ return visitor.Pair?.(key, node, path);
207
+ if (isScalar(node))
208
+ return visitor.Scalar?.(key, node, path);
209
+ if (isAlias(node))
210
+ return visitor.Alias?.(key, node, path);
211
+ return undefined;
212
+ }
213
+ function replaceNode(key, path, node) {
214
+ const parent = path[path.length - 1];
215
+ if (isCollection(parent)) {
216
+ parent.items[key] = node;
217
+ }
218
+ else if (isPair(parent)) {
219
+ if (key === 'key')
220
+ parent.key = node;
221
+ else
222
+ parent.value = node;
223
+ }
224
+ else if (isDocument(parent)) {
225
+ parent.contents = node;
226
+ }
227
+ else {
228
+ const pt = isAlias(parent) ? 'alias' : 'scalar';
229
+ throw new Error(`Cannot replace node with ${pt} parent`);
230
+ }
231
+ }
145
232
 
146
- export { visit };
233
+ export { visit, visitAsync };
@@ -53,7 +53,7 @@ function composeCollection(CN, ctx, token, tagToken, onError) {
53
53
  : new Scalar.Scalar(res);
54
54
  node.range = coll.range;
55
55
  node.tag = tagName;
56
- if (tag === null || tag === void 0 ? void 0 : tag.format)
56
+ if (tag?.format)
57
57
  node.format = tag.format;
58
58
  return node;
59
59
  }
@@ -6,22 +6,23 @@ var resolveEnd = require('./resolve-end.js');
6
6
  var resolveProps = require('./resolve-props.js');
7
7
 
8
8
  function composeDoc(options, directives, { offset, start, value, end }, onError) {
9
- const opts = Object.assign({ directives }, options);
9
+ const opts = Object.assign({ _directives: directives }, options);
10
10
  const doc = new Document.Document(undefined, opts);
11
11
  const ctx = {
12
+ atRoot: true,
12
13
  directives: doc.directives,
13
14
  options: doc.options,
14
15
  schema: doc.schema
15
16
  };
16
17
  const props = resolveProps.resolveProps(start, {
17
18
  indicator: 'doc-start',
18
- next: value || (end === null || end === void 0 ? void 0 : end[0]),
19
+ next: value ?? end?.[0],
19
20
  offset,
20
21
  onError,
21
22
  startOnNewline: true
22
23
  });
23
24
  if (props.found) {
24
- doc.directives.marker = true;
25
+ doc.directives.docStart = true;
25
26
  if (value &&
26
27
  (value.type === 'block-map' || value.type === 'block-seq') &&
27
28
  !props.hasNewline)
@@ -5,6 +5,7 @@ import type { SourceToken, Token } from '../parse/cst.js';
5
5
  import type { Schema } from '../schema/Schema.js';
6
6
  import type { ComposeErrorHandler } from './composer.js';
7
7
  export interface ComposeContext {
8
+ atRoot: boolean;
8
9
  directives: Directives;
9
10
  options: Readonly<Required<Omit<ParseOptions, 'lineCounter'>>>;
10
11
  schema: Readonly<Schema>;
@@ -10,6 +10,7 @@ const CN = { composeNode, composeEmptyNode };
10
10
  function composeNode(ctx, token, props, onError) {
11
11
  const { spaceBefore, comment, anchor, tag } = props;
12
12
  let node;
13
+ let isSrcToken = true;
13
14
  switch (token.type) {
14
15
  case 'alias':
15
16
  node = composeAlias(ctx, token, onError);
@@ -31,9 +32,14 @@ function composeNode(ctx, token, props, onError) {
31
32
  if (anchor)
32
33
  node.anchor = anchor.source.substring(1);
33
34
  break;
34
- default:
35
- console.log(token);
36
- throw new Error(`Unsupporten token type: ${token.type}`);
35
+ default: {
36
+ const message = token.type === 'error'
37
+ ? token.message
38
+ : `Unsupported token (type: ${token.type})`;
39
+ onError(token, 'UNEXPECTED_TOKEN', message);
40
+ node = composeEmptyNode(ctx, token.offset, undefined, null, props, onError);
41
+ isSrcToken = false;
42
+ }
37
43
  }
38
44
  if (anchor && node.anchor === '')
39
45
  onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
@@ -45,7 +51,8 @@ function composeNode(ctx, token, props, onError) {
45
51
  else
46
52
  node.commentBefore = comment;
47
53
  }
48
- if (ctx.options.keepSourceTokens)
54
+ // @ts-expect-error Type checking misses meaning of isSrcToken
55
+ if (ctx.options.keepSourceTokens && isSrcToken)
49
56
  node.srcToken = token;
50
57
  return node;
51
58
  }
@@ -72,6 +79,8 @@ function composeAlias({ options }, { offset, source, end }, onError) {
72
79
  const alias = new Alias.Alias(source.substring(1));
73
80
  if (alias.source === '')
74
81
  onError(offset, 'BAD_ALIAS', 'Alias cannot be an empty string');
82
+ if (alias.source.endsWith(':'))
83
+ onError(offset + source.length - 1, 'BAD_ALIAS', 'Alias ending in : is ambiguous', true);
75
84
  const valueEnd = offset + source.length;
76
85
  const re = resolveEnd.resolveEnd(end, valueEnd, options.strict, onError);
77
86
  alias.range = [offset, valueEnd, re.offset];
@@ -14,15 +14,17 @@ function composeScalar(ctx, token, tagToken, onError) {
14
14
  : null;
15
15
  const tag = tagToken && tagName
16
16
  ? findScalarTagByName(ctx.schema, value, tagName, tagToken, onError)
17
- : findScalarTagByTest(ctx.schema, value, token.type === 'scalar');
17
+ : token.type === 'scalar'
18
+ ? findScalarTagByTest(ctx, value, token, onError)
19
+ : ctx.schema[Node.SCALAR];
18
20
  let scalar;
19
21
  try {
20
- const res = tag.resolve(value, msg => onError(tagToken || token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
22
+ const res = tag.resolve(value, msg => onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
21
23
  scalar = Node.isScalar(res) ? res : new Scalar.Scalar(res);
22
24
  }
23
25
  catch (error) {
24
26
  const msg = error instanceof Error ? error.message : String(error);
25
- onError(tagToken || token, 'TAG_RESOLVE_FAILED', msg);
27
+ onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg);
26
28
  scalar = new Scalar.Scalar(value);
27
29
  }
28
30
  scalar.range = range;
@@ -38,7 +40,6 @@ function composeScalar(ctx, token, tagToken, onError) {
38
40
  return scalar;
39
41
  }
40
42
  function findScalarTagByName(schema, value, tagName, tagToken, onError) {
41
- var _a;
42
43
  if (tagName === '!')
43
44
  return schema[Node.SCALAR]; // non-specific tag
44
45
  const matchWithTest = [];
@@ -51,7 +52,7 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
51
52
  }
52
53
  }
53
54
  for (const tag of matchWithTest)
54
- if ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(value))
55
+ if (tag.test?.test(value))
55
56
  return tag;
56
57
  const kt = schema.knownTags[tagName];
57
58
  if (kt && !kt.collection) {
@@ -63,15 +64,19 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
63
64
  onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
64
65
  return schema[Node.SCALAR];
65
66
  }
66
- function findScalarTagByTest(schema, value, apply) {
67
- var _a;
68
- if (apply) {
69
- for (const tag of schema.tags) {
70
- if (tag.default && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(value)))
71
- return tag;
67
+ function findScalarTagByTest({ directives, schema }, value, token, onError) {
68
+ const tag = schema.tags.find(tag => tag.default && tag.test?.test(value)) || schema[Node.SCALAR];
69
+ if (schema.compat) {
70
+ const compat = schema.compat.find(tag => tag.default && tag.test?.test(value)) ??
71
+ schema[Node.SCALAR];
72
+ if (tag.tag !== compat.tag) {
73
+ const ts = directives.tagString(tag.tag);
74
+ const cs = directives.tagString(compat.tag);
75
+ const msg = `Value may be parsed as either ${ts} or ${cs}`;
76
+ onError(token, 'TAG_RESOLVE_FAILED', msg, true);
72
77
  }
73
78
  }
74
- return schema[Node.SCALAR];
79
+ return tag;
75
80
  }
76
81
 
77
82
  exports.composeScalar = composeScalar;
@@ -2,7 +2,7 @@ import { Directives } from '../doc/directives.js';
2
2
  import { Document } from '../doc/Document.js';
3
3
  import { ErrorCode, YAMLParseError, YAMLWarning } from '../errors.js';
4
4
  import { Range } from '../nodes/Node.js';
5
- import { DocumentOptions, ParseOptions, SchemaOptions } from '../options.js';
5
+ import type { DocumentOptions, ParseOptions, SchemaOptions } from '../options.js';
6
6
  import type { Token } from '../parse/cst.js';
7
7
  declare type ErrorSource = number | [number, number] | Range | {
8
8
  offset: number;
@@ -4,7 +4,6 @@ var directives = require('../doc/directives.js');
4
4
  var Document = require('../doc/Document.js');
5
5
  var errors = require('../errors.js');
6
6
  var Node = require('../nodes/Node.js');
7
- var options = require('../options.js');
8
7
  var composeDoc = require('./compose-doc.js');
9
8
  var resolveEnd = require('./resolve-end.js');
10
9
 
@@ -17,7 +16,6 @@ function getErrorPos(src) {
17
16
  return [offset, offset + (typeof source === 'string' ? source.length : 1)];
18
17
  }
19
18
  function parsePrelude(prelude) {
20
- var _a;
21
19
  let comment = '';
22
20
  let atComment = false;
23
21
  let afterEmptyLine = false;
@@ -32,7 +30,7 @@ function parsePrelude(prelude) {
32
30
  afterEmptyLine = false;
33
31
  break;
34
32
  case '%':
35
- if (((_a = prelude[i + 1]) === null || _a === void 0 ? void 0 : _a[0]) !== '#')
33
+ if (prelude[i + 1]?.[0] !== '#')
36
34
  i += 1;
37
35
  atComment = false;
38
36
  break;
@@ -57,7 +55,7 @@ function parsePrelude(prelude) {
57
55
  * ```
58
56
  */
59
57
  class Composer {
60
- constructor(options$1 = {}) {
58
+ constructor(options = {}) {
61
59
  this.doc = null;
62
60
  this.atDirectives = false;
63
61
  this.prelude = [];
@@ -70,10 +68,9 @@ class Composer {
70
68
  else
71
69
  this.errors.push(new errors.YAMLParseError(pos, code, message));
72
70
  };
73
- this.directives = new directives.Directives({
74
- version: options$1.version || options.defaultOptions.version
75
- });
76
- this.options = options$1;
71
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
72
+ this.directives = new directives.Directives({ version: options.version || '1.2' });
73
+ this.options = options;
77
74
  }
78
75
  decorate(doc, afterDoc) {
79
76
  const { comment, afterEmptyLine } = parsePrelude(this.prelude);
@@ -83,7 +80,7 @@ class Composer {
83
80
  if (afterDoc) {
84
81
  doc.comment = doc.comment ? `${doc.comment}\n${comment}` : comment;
85
82
  }
86
- else if (afterEmptyLine || doc.directives.marker || !dc) {
83
+ else if (afterEmptyLine || doc.directives.docStart || !dc) {
87
84
  doc.commentBefore = comment;
88
85
  }
89
86
  else if (Node.isCollection(dc) && !dc.flow && dc.items.length > 0) {
@@ -150,8 +147,8 @@ class Composer {
150
147
  break;
151
148
  case 'document': {
152
149
  const doc = composeDoc.composeDoc(this.options, this.directives, token, this.onError);
153
- if (this.atDirectives && !doc.directives.marker)
154
- this.onError(token, 'MISSING_CHAR', 'Missing directives-end indicator line');
150
+ if (this.atDirectives && !doc.directives.docStart)
151
+ this.onError(token, 'MISSING_CHAR', 'Missing directives-end/doc-start indicator line');
155
152
  this.decorate(doc, false);
156
153
  if (this.doc)
157
154
  yield this.doc;
@@ -183,6 +180,7 @@ class Composer {
183
180
  this.errors.push(new errors.YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg));
184
181
  break;
185
182
  }
183
+ this.doc.directives.docEnd = true;
186
184
  const end = resolveEnd.resolveEnd(token.end, token.offset + token.source.length, this.doc.options.strict, this.onError);
187
185
  this.decorate(this.doc, true);
188
186
  if (end.comment) {
@@ -209,7 +207,7 @@ class Composer {
209
207
  this.doc = null;
210
208
  }
211
209
  else if (forceDoc) {
212
- const opts = Object.assign({ directives: this.directives }, this.options);
210
+ const opts = Object.assign({ _directives: this.directives }, this.options);
213
211
  const doc = new Document.Document(undefined, opts);
214
212
  if (this.atDirectives)
215
213
  this.onError(endOffset, 'MISSING_CHAR', 'Missing directives-end indicator line');
@@ -4,19 +4,21 @@ var Pair = require('../nodes/Pair.js');
4
4
  var YAMLMap = require('../nodes/YAMLMap.js');
5
5
  var resolveProps = require('./resolve-props.js');
6
6
  var utilContainsNewline = require('./util-contains-newline.js');
7
+ var utilFlowIndentCheck = require('./util-flow-indent-check.js');
7
8
  var utilMapIncludes = require('./util-map-includes.js');
8
9
 
9
10
  const startColMsg = 'All mapping items must start at the same column';
10
11
  function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
11
- var _a;
12
12
  const map = new YAMLMap.YAMLMap(ctx.schema);
13
+ if (ctx.atRoot)
14
+ ctx.atRoot = false;
13
15
  let offset = bm.offset;
14
16
  for (const collItem of bm.items) {
15
17
  const { start, key, sep, value } = collItem;
16
18
  // key properties
17
19
  const keyProps = resolveProps.resolveProps(start, {
18
20
  indicator: 'explicit-key-ind',
19
- next: key || (sep === null || sep === void 0 ? void 0 : sep[0]),
21
+ next: key ?? sep?.[0],
20
22
  offset,
21
23
  onError,
22
24
  startOnNewline: true
@@ -39,21 +41,24 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
39
41
  }
40
42
  continue;
41
43
  }
44
+ if (keyProps.hasNewlineAfterProp || utilContainsNewline.containsNewline(key)) {
45
+ onError(key ?? start[start.length - 1], 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
46
+ }
42
47
  }
43
- else if (((_a = keyProps.found) === null || _a === void 0 ? void 0 : _a.indent) !== bm.indent)
48
+ else if (keyProps.found?.indent !== bm.indent) {
44
49
  onError(offset, 'BAD_INDENT', startColMsg);
45
- if (implicitKey && utilContainsNewline.containsNewline(key))
46
- onError(key, // checked by containsNewline()
47
- 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
50
+ }
48
51
  // key value
49
52
  const keyStart = keyProps.end;
50
53
  const keyNode = key
51
54
  ? composeNode(ctx, key, keyProps, onError)
52
55
  : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
56
+ if (ctx.schema.compat)
57
+ utilFlowIndentCheck.flowIndentCheck(bm.indent, key, onError);
53
58
  if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
54
59
  onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
55
60
  // value properties
56
- const valueProps = resolveProps.resolveProps(sep || [], {
61
+ const valueProps = resolveProps.resolveProps(sep ?? [], {
57
62
  indicator: 'map-value-ind',
58
63
  next: value,
59
64
  offset: keyNode.range[2],
@@ -63,7 +68,7 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
63
68
  offset = valueProps.end;
64
69
  if (valueProps.found) {
65
70
  if (implicitKey) {
66
- if ((value === null || value === void 0 ? void 0 : value.type) === 'block-map' && !valueProps.hasNewline)
71
+ if (value?.type === 'block-map' && !valueProps.hasNewline)
67
72
  onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
68
73
  if (ctx.options.strict &&
69
74
  keyProps.start < valueProps.found.offset - 1024)
@@ -73,6 +78,8 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
73
78
  const valueNode = value
74
79
  ? composeNode(ctx, value, valueProps, onError)
75
80
  : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
81
+ if (ctx.schema.compat)
82
+ utilFlowIndentCheck.flowIndentCheck(bm.indent, value, onError);
76
83
  offset = valueNode.range[2];
77
84
  const pair = new Pair.Pair(keyNode, valueNode);
78
85
  if (ctx.options.keepSourceTokens)
@@ -19,8 +19,10 @@ function resolveBlockScalar(scalar, strict, onError) {
19
19
  break;
20
20
  }
21
21
  // shortcut for empty contents
22
- if (!scalar.source || chompStart === 0) {
23
- const value = header.chomp === '+' ? '\n'.repeat(Math.max(0, lines.length - 1)) : '';
22
+ if (chompStart === 0) {
23
+ const value = header.chomp === '+' && lines.length > 0
24
+ ? '\n'.repeat(Math.max(1, lines.length - 1))
25
+ : '';
24
26
  let end = start + header.length;
25
27
  if (scalar.source)
26
28
  end += scalar.source.length;
@@ -48,6 +50,11 @@ function resolveBlockScalar(scalar, strict, onError) {
48
50
  }
49
51
  offset += indent.length + content.length + 1;
50
52
  }
53
+ // include trailing more-indented empty lines in content
54
+ for (let i = lines.length - 1; i >= chompStart; --i) {
55
+ if (lines[i][0].length > trimIndent)
56
+ chompStart = i + 1;
57
+ }
51
58
  let value = '';
52
59
  let sep = '';
53
60
  let prevMoreIndented = false;
@@ -177,7 +184,9 @@ function splitLines(source) {
177
184
  const split = source.split(/\n( *)/);
178
185
  const first = split[0];
179
186
  const m = first.match(/^( *)/);
180
- const line0 = m && m[1] ? [m[1], first.slice(m[1].length)] : ['', first];
187
+ const line0 = m?.[1]
188
+ ? [m[1], first.slice(m[1].length)]
189
+ : ['', first];
181
190
  const lines = [line0];
182
191
  for (let i = 1; i < split.length; i += 2)
183
192
  lines.push([split[i], split[i + 1]]);
@@ -2,9 +2,12 @@
2
2
 
3
3
  var YAMLSeq = require('../nodes/YAMLSeq.js');
4
4
  var resolveProps = require('./resolve-props.js');
5
+ var utilFlowIndentCheck = require('./util-flow-indent-check.js');
5
6
 
6
7
  function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError) {
7
8
  const seq = new YAMLSeq.YAMLSeq(ctx.schema);
9
+ if (ctx.atRoot)
10
+ ctx.atRoot = false;
8
11
  let offset = bs.offset;
9
12
  for (const { start, value } of bs.items) {
10
13
  const props = resolveProps.resolveProps(start, {
@@ -32,6 +35,8 @@ function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError) {
32
35
  const node = value
33
36
  ? composeNode(ctx, value, props, onError)
34
37
  : composeEmptyNode(ctx, offset, start, null, props, onError);
38
+ if (ctx.schema.compat)
39
+ utilFlowIndentCheck.flowIndentCheck(bs.indent, value, onError);
35
40
  offset = node.range[2];
36
41
  seq.items.push(node);
37
42
  }