yaml 2.0.0-7 → 2.0.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 (123) 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 +17 -10
  5. package/browser/dist/compose/composer.js +6 -7
  6. package/browser/dist/compose/resolve-block-map.js +19 -5
  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 +9 -2
  11. package/browser/dist/compose/resolve-props.js +4 -0
  12. package/browser/dist/compose/util-flow-indent-check.js +15 -0
  13. package/browser/dist/doc/Document.js +73 -16
  14. package/browser/dist/doc/anchors.js +2 -2
  15. package/browser/dist/doc/createNode.js +12 -7
  16. package/browser/dist/doc/directives.js +11 -3
  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 +28 -15
  20. package/browser/dist/nodes/Node.js +7 -0
  21. package/browser/dist/nodes/Pair.js +11 -3
  22. package/browser/dist/nodes/Scalar.js +1 -1
  23. package/browser/dist/nodes/YAMLMap.js +6 -5
  24. package/browser/dist/nodes/YAMLSeq.js +2 -2
  25. package/browser/dist/nodes/addPairToJSMap.js +3 -2
  26. package/browser/dist/nodes/toJS.js +3 -1
  27. package/browser/dist/parse/cst-scalar.js +2 -6
  28. package/browser/dist/parse/cst-visit.js +2 -2
  29. package/browser/dist/parse/lexer.js +50 -22
  30. package/browser/dist/parse/parser.js +121 -45
  31. package/browser/dist/public-api.js +4 -5
  32. package/browser/dist/schema/Schema.js +19 -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 +17 -13
  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 +2 -1
  39. package/browser/dist/stringify/stringify.js +53 -23
  40. package/browser/dist/stringify/stringifyCollection.js +115 -85
  41. package/browser/dist/stringify/stringifyComment.js +18 -12
  42. package/browser/dist/stringify/stringifyDocument.js +37 -15
  43. package/browser/dist/stringify/stringifyPair.js +35 -21
  44. package/browser/dist/stringify/stringifyString.js +41 -43
  45. package/browser/dist/util.js +3 -0
  46. package/browser/dist/visit.js +150 -62
  47. package/dist/compose/compose-doc.js +3 -2
  48. package/dist/compose/compose-node.d.ts +1 -0
  49. package/dist/compose/compose-node.js +14 -3
  50. package/dist/compose/compose-scalar.js +16 -9
  51. package/dist/compose/composer.d.ts +1 -1
  52. package/dist/compose/composer.js +8 -9
  53. package/dist/compose/resolve-block-map.js +19 -5
  54. package/dist/compose/resolve-block-scalar.js +12 -3
  55. package/dist/compose/resolve-block-seq.js +5 -0
  56. package/dist/compose/resolve-flow-collection.js +18 -6
  57. package/dist/compose/resolve-flow-scalar.js +9 -2
  58. package/dist/compose/resolve-props.d.ts +1 -1
  59. package/dist/compose/resolve-props.js +4 -0
  60. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  61. package/dist/compose/util-flow-indent-check.js +17 -0
  62. package/dist/doc/Document.d.ts +13 -4
  63. package/dist/doc/Document.js +79 -22
  64. package/dist/doc/anchors.d.ts +2 -2
  65. package/dist/doc/anchors.js +2 -2
  66. package/dist/doc/createNode.d.ts +3 -2
  67. package/dist/doc/createNode.js +11 -6
  68. package/dist/doc/directives.d.ts +5 -2
  69. package/dist/doc/directives.js +11 -3
  70. package/dist/index.d.ts +4 -4
  71. package/dist/index.js +1 -2
  72. package/dist/node_modules/tslib/tslib.es6.js +76 -0
  73. package/dist/nodes/Alias.d.ts +7 -3
  74. package/dist/nodes/Collection.d.ts +10 -4
  75. package/dist/nodes/Collection.js +27 -14
  76. package/dist/nodes/Node.d.ts +6 -1
  77. package/dist/nodes/Node.js +7 -0
  78. package/dist/nodes/Pair.d.ts +7 -2
  79. package/dist/nodes/Pair.js +10 -2
  80. package/dist/nodes/Scalar.d.ts +2 -0
  81. package/dist/nodes/Scalar.js +1 -1
  82. package/dist/nodes/YAMLMap.d.ts +5 -1
  83. package/dist/nodes/YAMLMap.js +6 -5
  84. package/dist/nodes/YAMLSeq.d.ts +2 -0
  85. package/dist/nodes/YAMLSeq.js +2 -2
  86. package/dist/nodes/addPairToJSMap.js +2 -1
  87. package/dist/nodes/toJS.js +3 -1
  88. package/dist/options.d.ts +67 -15
  89. package/dist/parse/cst-scalar.d.ts +6 -0
  90. package/dist/parse/cst-scalar.js +2 -6
  91. package/dist/parse/cst-visit.js +2 -2
  92. package/dist/parse/lexer.d.ts +2 -0
  93. package/dist/parse/lexer.js +50 -22
  94. package/dist/parse/parser.js +121 -45
  95. package/dist/public-api.js +4 -5
  96. package/dist/schema/Schema.d.ts +6 -4
  97. package/dist/schema/Schema.js +18 -3
  98. package/dist/schema/common/null.js +3 -1
  99. package/dist/schema/core/float.js +4 -1
  100. package/dist/schema/tags.d.ts +1 -2
  101. package/dist/schema/tags.js +17 -13
  102. package/dist/schema/yaml-1.1/float.js +4 -1
  103. package/dist/schema/yaml-1.1/omap.js +1 -1
  104. package/dist/schema/yaml-1.1/pairs.js +2 -1
  105. package/dist/stringify/stringify.d.ts +5 -3
  106. package/dist/stringify/stringify.js +53 -23
  107. package/dist/stringify/stringifyCollection.d.ts +2 -6
  108. package/dist/stringify/stringifyCollection.js +114 -84
  109. package/dist/stringify/stringifyComment.d.ts +10 -2
  110. package/dist/stringify/stringifyComment.js +19 -12
  111. package/dist/stringify/stringifyDocument.js +36 -14
  112. package/dist/stringify/stringifyPair.js +34 -20
  113. package/dist/stringify/stringifyString.js +41 -43
  114. package/dist/test-events.d.ts +1 -1
  115. package/dist/test-events.js +13 -12
  116. package/dist/util.d.ts +3 -0
  117. package/dist/util.js +6 -0
  118. package/dist/visit.d.ts +49 -10
  119. package/dist/visit.js +149 -60
  120. package/package.json +12 -10
  121. package/util.d.ts +3 -0
  122. package/browser/dist/options.js +0 -17
  123. package/dist/options.js +0 -19
@@ -118,34 +118,50 @@ function doubleQuotedString(value, ctx) {
118
118
  : foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx));
119
119
  }
120
120
  function singleQuotedString(value, ctx) {
121
- if (ctx.implicitKey) {
122
- if (/\n/.test(value))
123
- return doubleQuotedString(value, ctx);
124
- }
125
- else {
126
- // single quoted string can't have leading or trailing whitespace around newline
127
- if (/[ \t]\n|\n[ \t]/.test(value))
128
- return doubleQuotedString(value, ctx);
129
- }
121
+ if (ctx.options.singleQuote === false ||
122
+ (ctx.implicitKey && value.includes('\n')) ||
123
+ /[ \t]\n|\n[ \t]/.test(value) // single quoted string can't have leading or trailing whitespace around newline
124
+ )
125
+ return doubleQuotedString(value, ctx);
130
126
  const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
131
127
  const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
132
128
  return ctx.implicitKey
133
129
  ? res
134
130
  : foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx));
135
131
  }
132
+ function quotedString(value, ctx) {
133
+ const { singleQuote } = ctx.options;
134
+ let qs;
135
+ if (singleQuote === false)
136
+ qs = doubleQuotedString;
137
+ else {
138
+ const hasDouble = value.includes('"');
139
+ const hasSingle = value.includes("'");
140
+ if (hasDouble && !hasSingle)
141
+ qs = singleQuotedString;
142
+ else if (hasSingle && !hasDouble)
143
+ qs = doubleQuotedString;
144
+ else
145
+ qs = singleQuote ? singleQuotedString : doubleQuotedString;
146
+ }
147
+ return qs(value, ctx);
148
+ }
136
149
  function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
150
+ const { blockQuote, commentString, lineWidth } = ctx.options;
137
151
  // 1. Block can't end in whitespace unless the last line is non-empty.
138
152
  // 2. Strings consisting of only whitespace are best rendered explicitly.
139
- if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
140
- return doubleQuotedString(value, ctx);
153
+ if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
154
+ return quotedString(value, ctx);
141
155
  }
142
156
  const indent = ctx.indent ||
143
157
  (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
144
- const literal = type === Scalar.BLOCK_FOLDED
145
- ? false
146
- : type === Scalar.BLOCK_LITERAL
147
- ? true
148
- : !lineLengthOverLimit(value, ctx.options.lineWidth, indent.length);
158
+ const literal = blockQuote === 'literal'
159
+ ? true
160
+ : blockQuote === 'folded' || type === Scalar.BLOCK_FOLDED
161
+ ? false
162
+ : type === Scalar.BLOCK_LITERAL
163
+ ? true
164
+ : !lineLengthOverLimit(value, lineWidth, indent.length);
149
165
  if (!value)
150
166
  return literal ? '|\n' : '>\n';
151
167
  // determine chomping from whitespace at value end
@@ -196,7 +212,7 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
196
212
  const indentSize = indent ? '2' : '1'; // root is at -1
197
213
  let header = (literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp;
198
214
  if (comment) {
199
- header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ');
215
+ header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
200
216
  if (onComment)
201
217
  onComment();
202
218
  }
@@ -213,44 +229,28 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
213
229
  return `${header}\n${indent}${body}`;
214
230
  }
215
231
  function plainString(item, ctx, onComment, onChompKeep) {
216
- var _a;
217
232
  const { type, value } = item;
218
233
  const { actualString, implicitKey, indent, inFlow } = ctx;
219
234
  if ((implicitKey && /[\n[\]{},]/.test(value)) ||
220
235
  (inFlow && /[[\]{},]/.test(value))) {
221
- return doubleQuotedString(value, ctx);
236
+ return quotedString(value, ctx);
222
237
  }
223
238
  if (!value ||
224
239
  /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
225
- const hasDouble = value.indexOf('"') !== -1;
226
- const hasSingle = value.indexOf("'") !== -1;
227
- let quotedString;
228
- if (hasDouble && !hasSingle) {
229
- quotedString = singleQuotedString;
230
- }
231
- else if (hasSingle && !hasDouble) {
232
- quotedString = doubleQuotedString;
233
- }
234
- else if (ctx.options.singleQuote) {
235
- quotedString = singleQuotedString;
236
- }
237
- else {
238
- quotedString = doubleQuotedString;
239
- }
240
240
  // not allowed:
241
241
  // - empty string, '-' or '?'
242
242
  // - start with an indicator character (except [?:-]) or /[?-] /
243
243
  // - '\n ', ': ' or ' \n' anywhere
244
244
  // - '#' not preceded by a non-space char
245
245
  // - end with ' ' or ':'
246
- return implicitKey || inFlow || value.indexOf('\n') === -1
246
+ return implicitKey || inFlow || !value.includes('\n')
247
247
  ? quotedString(value, ctx)
248
248
  : blockString(item, ctx, onComment, onChompKeep);
249
249
  }
250
250
  if (!implicitKey &&
251
251
  !inFlow &&
252
252
  type !== Scalar.PLAIN &&
253
- value.indexOf('\n') !== -1) {
253
+ value.includes('\n')) {
254
254
  // Where allowed & type not set explicitly, prefer block style for multiline strings
255
255
  return blockString(item, ctx, onComment, onChompKeep);
256
256
  }
@@ -263,12 +263,10 @@ function plainString(item, ctx, onComment, onChompKeep) {
263
263
  // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
264
264
  // and others in v1.1.
265
265
  if (actualString) {
266
- for (const tag of ctx.doc.schema.tags) {
267
- if (tag.default &&
268
- tag.tag !== 'tag:yaml.org,2002:str' &&
269
- ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str)))
270
- return doubleQuotedString(value, ctx);
271
- }
266
+ const test = (tag) => { var _a; return tag.default && tag.tag !== 'tag:yaml.org,2002:str' && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str)); };
267
+ const { compat, tags } = ctx.doc.schema;
268
+ if (tags.some(test) || (compat === null || compat === void 0 ? void 0 : compat.some(test)))
269
+ return quotedString(value, ctx);
272
270
  }
273
271
  return implicitKey
274
272
  ? str
@@ -290,7 +288,7 @@ function stringifyString(item, ctx, onComment, onChompKeep) {
290
288
  case Scalar.BLOCK_FOLDED:
291
289
  case Scalar.BLOCK_LITERAL:
292
290
  return implicitKey || inFlow
293
- ? doubleQuotedString(ss.value, ctx) // blocks are not valid inside flow containers
291
+ ? quotedString(ss.value, ctx) // blocks are not valid inside flow containers
294
292
  : blockString(ss, ctx, onComment, onChompKeep);
295
293
  case Scalar.QUOTE_DOUBLE:
296
294
  return doubleQuotedString(ss.value, ctx);
@@ -1,6 +1,9 @@
1
1
  export { debug, warn } from './log.js';
2
2
  export { findPair } from './nodes/YAMLMap.js';
3
3
  export { toJS } from './nodes/toJS.js';
4
+ export { map as mapTag } from './schema/common/map.js';
5
+ export { seq as seqTag } from './schema/common/seq.js';
6
+ export { string as stringTag } from './schema/common/string.js';
4
7
  export { foldFlowLines } from './stringify/foldFlowLines.js';
5
8
  export { stringifyNumber } from './stringify/stringifyNumber.js';
6
9
  export { stringifyString } from './stringify/stringifyString.js';
@@ -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,59 @@ 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
+ var _a, _b, _c, _d, _e;
200
+ if (typeof visitor === 'function')
201
+ return visitor(key, node, path);
202
+ if (isMap(node))
203
+ return (_a = visitor.Map) === null || _a === void 0 ? void 0 : _a.call(visitor, key, node, path);
204
+ if (isSeq(node))
205
+ return (_b = visitor.Seq) === null || _b === void 0 ? void 0 : _b.call(visitor, key, node, path);
206
+ if (isPair(node))
207
+ return (_c = visitor.Pair) === null || _c === void 0 ? void 0 : _c.call(visitor, key, node, path);
208
+ if (isScalar(node))
209
+ return (_d = visitor.Scalar) === null || _d === void 0 ? void 0 : _d.call(visitor, key, node, path);
210
+ if (isAlias(node))
211
+ return (_e = visitor.Alias) === null || _e === void 0 ? void 0 : _e.call(visitor, key, node, path);
212
+ return undefined;
213
+ }
214
+ function replaceNode(key, path, node) {
215
+ const parent = path[path.length - 1];
216
+ if (isCollection(parent)) {
217
+ parent.items[key] = node;
218
+ }
219
+ else if (isPair(parent)) {
220
+ if (key === 'key')
221
+ parent.key = node;
222
+ else
223
+ parent.value = node;
224
+ }
225
+ else if (isDocument(parent)) {
226
+ parent.contents = node;
227
+ }
228
+ else {
229
+ const pt = isAlias(parent) ? 'alias' : 'scalar';
230
+ throw new Error(`Cannot replace node with ${pt} parent`);
231
+ }
232
+ }
145
233
 
146
- export { visit };
234
+ export { visit, visitAsync };
@@ -9,19 +9,20 @@ function composeDoc(options, directives, { offset, start, value, end }, onError)
9
9
  const opts = Object.assign({ 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 !== null && value !== void 0 ? value : end === null || end === void 0 ? void 0 : 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,6 +51,9 @@ function composeNode(ctx, token, props, onError) {
45
51
  else
46
52
  node.commentBefore = comment;
47
53
  }
54
+ // @ts-expect-error Type checking misses meaning of isSrcToken
55
+ if (ctx.options.keepSourceTokens && isSrcToken)
56
+ node.srcToken = token;
48
57
  return node;
49
58
  }
50
59
  function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anchor, tag }, onError) {
@@ -70,6 +79,8 @@ function composeAlias({ options }, { offset, source, end }, onError) {
70
79
  const alias = new Alias.Alias(source.substring(1));
71
80
  if (alias.source === '')
72
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);
73
84
  const valueEnd = offset + source.length;
74
85
  const re = resolveEnd.resolveEnd(end, valueEnd, options.strict, onError);
75
86
  alias.range = [offset, valueEnd, re.offset];
@@ -14,14 +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 !== null && tagToken !== void 0 ? 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
- onError(tagToken || token, 'TAG_RESOLVE_FAILED', error.message);
26
+ const msg = error instanceof Error ? error.message : String(error);
27
+ onError(tagToken !== null && tagToken !== void 0 ? tagToken : token, 'TAG_RESOLVE_FAILED', msg);
25
28
  scalar = new Scalar.Scalar(value);
26
29
  }
27
30
  scalar.range = range;
@@ -62,15 +65,19 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
62
65
  onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
63
66
  return schema[Node.SCALAR];
64
67
  }
65
- function findScalarTagByTest(schema, value, apply) {
68
+ function findScalarTagByTest({ directives, schema }, value, token, onError) {
66
69
  var _a;
67
- if (apply) {
68
- for (const tag of schema.tags) {
69
- if (tag.default && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(value)))
70
- return tag;
70
+ const tag = schema.tags.find(tag => { var _a; return tag.default && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(value)); }) || schema[Node.SCALAR];
71
+ if (schema.compat) {
72
+ const compat = (_a = schema.compat.find(tag => { var _a; return tag.default && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(value)); })) !== null && _a !== void 0 ? _a : schema[Node.SCALAR];
73
+ if (tag.tag !== compat.tag) {
74
+ const ts = directives.tagString(tag.tag);
75
+ const cs = directives.tagString(compat.tag);
76
+ const msg = `Value may be parsed as either ${ts} or ${cs}`;
77
+ onError(token, 'TAG_RESOLVE_FAILED', msg, true);
71
78
  }
72
79
  }
73
- return schema[Node.SCALAR];
80
+ return tag;
74
81
  }
75
82
 
76
83
  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
 
@@ -57,7 +56,7 @@ function parsePrelude(prelude) {
57
56
  * ```
58
57
  */
59
58
  class Composer {
60
- constructor(options$1 = {}) {
59
+ constructor(options = {}) {
61
60
  this.doc = null;
62
61
  this.atDirectives = false;
63
62
  this.prelude = [];
@@ -70,10 +69,9 @@ class Composer {
70
69
  else
71
70
  this.errors.push(new errors.YAMLParseError(pos, code, message));
72
71
  };
73
- this.directives = new directives.Directives({
74
- version: options$1.version || options.defaultOptions.version
75
- });
76
- this.options = options$1;
72
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
73
+ this.directives = new directives.Directives({ version: options.version || '1.2' });
74
+ this.options = options;
77
75
  }
78
76
  decorate(doc, afterDoc) {
79
77
  const { comment, afterEmptyLine } = parsePrelude(this.prelude);
@@ -83,7 +81,7 @@ class Composer {
83
81
  if (afterDoc) {
84
82
  doc.comment = doc.comment ? `${doc.comment}\n${comment}` : comment;
85
83
  }
86
- else if (afterEmptyLine || doc.directives.marker || !dc) {
84
+ else if (afterEmptyLine || doc.directives.docStart || !dc) {
87
85
  doc.commentBefore = comment;
88
86
  }
89
87
  else if (Node.isCollection(dc) && !dc.flow && dc.items.length > 0) {
@@ -150,8 +148,8 @@ class Composer {
150
148
  break;
151
149
  case 'document': {
152
150
  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');
151
+ if (this.atDirectives && !doc.directives.docStart)
152
+ this.onError(token, 'MISSING_CHAR', 'Missing directives-end/doc-start indicator line');
155
153
  this.decorate(doc, false);
156
154
  if (this.doc)
157
155
  yield this.doc;
@@ -183,6 +181,7 @@ class Composer {
183
181
  this.errors.push(new errors.YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg));
184
182
  break;
185
183
  }
184
+ this.doc.directives.docEnd = true;
186
185
  const end = resolveEnd.resolveEnd(token.end, token.offset + token.source.length, this.doc.options.strict, this.onError);
187
186
  this.decorate(this.doc, true);
188
187
  if (end.comment) {