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,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,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 !== 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
26
  const msg = error instanceof Error ? error.message : String(error);
25
- onError(tagToken || token, 'TAG_RESOLVE_FAILED', msg);
27
+ onError(tagToken !== null && tagToken !== void 0 ? tagToken : token, 'TAG_RESOLVE_FAILED', msg);
26
28
  scalar = new Scalar.Scalar(value);
27
29
  }
28
30
  scalar.range = range;
@@ -63,15 +65,19 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
63
65
  onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
64
66
  return schema[Node.SCALAR];
65
67
  }
66
- function findScalarTagByTest(schema, value, apply) {
68
+ function findScalarTagByTest({ directives, schema }, value, token, onError) {
67
69
  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;
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);
72
78
  }
73
79
  }
74
- return schema[Node.SCALAR];
80
+ return tag;
75
81
  }
76
82
 
77
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) {
@@ -4,18 +4,22 @@ 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
12
  var _a;
12
13
  const map = new YAMLMap.YAMLMap(ctx.schema);
14
+ if (ctx.atRoot)
15
+ ctx.atRoot = false;
13
16
  let offset = bm.offset;
14
- for (const { start, key, sep, value } of bm.items) {
17
+ for (const collItem of bm.items) {
18
+ const { start, key, sep, value } = collItem;
15
19
  // key properties
16
20
  const keyProps = resolveProps.resolveProps(start, {
17
21
  indicator: 'explicit-key-ind',
18
- next: key || (sep === null || sep === void 0 ? void 0 : sep[0]),
22
+ next: key !== null && key !== void 0 ? key : sep === null || sep === void 0 ? void 0 : sep[0],
19
23
  offset,
20
24
  onError,
21
25
  startOnNewline: true
@@ -38,21 +42,24 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
38
42
  }
39
43
  continue;
40
44
  }
45
+ if (keyProps.hasNewlineAfterProp || utilContainsNewline.containsNewline(key)) {
46
+ onError(key !== null && key !== void 0 ? key : start[start.length - 1], 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
47
+ }
41
48
  }
42
- else if (((_a = keyProps.found) === null || _a === void 0 ? void 0 : _a.indent) !== bm.indent)
49
+ else if (((_a = keyProps.found) === null || _a === void 0 ? void 0 : _a.indent) !== bm.indent) {
43
50
  onError(offset, 'BAD_INDENT', startColMsg);
44
- if (implicitKey && utilContainsNewline.containsNewline(key))
45
- onError(key, // checked by containsNewline()
46
- 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
51
+ }
47
52
  // key value
48
53
  const keyStart = keyProps.end;
49
54
  const keyNode = key
50
55
  ? composeNode(ctx, key, keyProps, onError)
51
56
  : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
57
+ if (ctx.schema.compat)
58
+ utilFlowIndentCheck.flowIndentCheck(bm.indent, key, onError);
52
59
  if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
53
60
  onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
54
61
  // value properties
55
- const valueProps = resolveProps.resolveProps(sep || [], {
62
+ const valueProps = resolveProps.resolveProps(sep !== null && sep !== void 0 ? sep : [], {
56
63
  indicator: 'map-value-ind',
57
64
  next: value,
58
65
  offset: keyNode.range[2],
@@ -72,8 +79,13 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
72
79
  const valueNode = value
73
80
  ? composeNode(ctx, value, valueProps, onError)
74
81
  : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
82
+ if (ctx.schema.compat)
83
+ utilFlowIndentCheck.flowIndentCheck(bm.indent, value, onError);
75
84
  offset = valueNode.range[2];
76
- map.items.push(new Pair.Pair(keyNode, valueNode));
85
+ const pair = new Pair.Pair(keyNode, valueNode);
86
+ if (ctx.options.keepSourceTokens)
87
+ pair.srcToken = collItem;
88
+ map.items.push(pair);
77
89
  }
78
90
  else {
79
91
  // key with no value
@@ -85,7 +97,10 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
85
97
  else
86
98
  keyNode.comment = valueProps.comment;
87
99
  }
88
- map.items.push(new Pair.Pair(keyNode));
100
+ const pair = new Pair.Pair(keyNode);
101
+ if (ctx.options.keepSourceTokens)
102
+ pair.srcToken = collItem;
103
+ map.items.push(pair);
89
104
  }
90
105
  }
91
106
  map.range = [bm.offset, offset, offset];
@@ -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 === '+' ? lines.map(line => line[0]).join('\n') : '';
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 === null || m === void 0 ? void 0 : 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
  }
@@ -12,19 +12,24 @@ var utilMapIncludes = require('./util-map-includes.js');
12
12
  const blockMsg = 'Block collections are not allowed within flow collections';
13
13
  const isBlock = (token) => token && (token.type === 'block-map' || token.type === 'block-seq');
14
14
  function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError) {
15
+ var _a;
15
16
  const isMap = fc.start.source === '{';
16
17
  const fcName = isMap ? 'flow map' : 'flow sequence';
17
18
  const coll = isMap
18
19
  ? new YAMLMap.YAMLMap(ctx.schema)
19
20
  : new YAMLSeq.YAMLSeq(ctx.schema);
20
21
  coll.flow = true;
21
- let offset = fc.offset;
22
+ const atRoot = ctx.atRoot;
23
+ if (atRoot)
24
+ ctx.atRoot = false;
25
+ let offset = fc.offset + fc.start.source.length;
22
26
  for (let i = 0; i < fc.items.length; ++i) {
23
- const { start, key, sep, value } = fc.items[i];
27
+ const collItem = fc.items[i];
28
+ const { start, key, sep, value } = collItem;
24
29
  const props = resolveProps.resolveProps(start, {
25
30
  flow: fcName,
26
31
  indicator: 'explicit-key-ind',
27
- next: key || (sep === null || sep === void 0 ? void 0 : sep[0]),
32
+ next: key !== null && key !== void 0 ? key : sep === null || sep === void 0 ? void 0 : sep[0],
28
33
  offset,
29
34
  onError,
30
35
  startOnNewline: false
@@ -41,6 +46,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
41
46
  else
42
47
  coll.comment = props.comment;
43
48
  }
49
+ offset = props.end;
44
50
  continue;
45
51
  }
46
52
  if (!isMap && ctx.options.strict && utilContainsNewline.containsNewline(key))
@@ -71,7 +77,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
71
77
  if (prevItemComment) {
72
78
  let prev = coll.items[coll.items.length - 1];
73
79
  if (Node.isPair(prev))
74
- prev = prev.value || prev.key;
80
+ prev = (_a = prev.value) !== null && _a !== void 0 ? _a : prev.key;
75
81
  if (prev.comment)
76
82
  prev.comment += '\n' + prevItemComment;
77
83
  else
@@ -101,7 +107,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
101
107
  if (isBlock(key))
102
108
  onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
103
109
  // value properties
104
- const valueProps = resolveProps.resolveProps(sep || [], {
110
+ const valueProps = resolveProps.resolveProps(sep !== null && sep !== void 0 ? sep : [], {
105
111
  flow: fcName,
106
112
  indicator: 'map-value-ind',
107
113
  next: value,
@@ -147,6 +153,8 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
147
153
  keyNode.comment = valueProps.comment;
148
154
  }
149
155
  const pair = new Pair.Pair(keyNode, valueNode);
156
+ if (ctx.options.keepSourceTokens)
157
+ pair.srcToken = collItem;
150
158
  if (isMap) {
151
159
  const map = coll;
152
160
  if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
@@ -168,7 +176,11 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
168
176
  if (ce && ce.source === expectedEnd)
169
177
  cePos = ce.offset + ce.source.length;
170
178
  else {
171
- onError(offset + 1, 'MISSING_CHAR', `Expected ${fcName} to end with ${expectedEnd}`);
179
+ const name = fcName[0].toUpperCase() + fcName.substring(1);
180
+ const msg = atRoot
181
+ ? `${name} must end with a ${expectedEnd}`
182
+ : `${name} in block collection must be sufficiently indented and end with a ${expectedEnd}`;
183
+ onError(offset, atRoot ? 'MISSING_CHAR' : 'BAD_INDENT', msg);
172
184
  if (ce && ce.source.length !== 1)
173
185
  ee.unshift(ce);
174
186
  }