yaml 2.0.0-9 → 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 (102) 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 +13 -4
  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 +9 -2
  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 +14 -5
  10. package/browser/dist/compose/resolve-flow-scalar.js +2 -1
  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 +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/nodes/Pair.js +2 -2
  19. package/browser/dist/nodes/Scalar.js +1 -1
  20. package/browser/dist/nodes/YAMLMap.js +6 -5
  21. package/browser/dist/nodes/YAMLSeq.js +2 -2
  22. package/browser/dist/nodes/addPairToJSMap.js +1 -1
  23. package/browser/dist/nodes/toJS.js +3 -1
  24. package/browser/dist/parse/cst-visit.js +2 -2
  25. package/browser/dist/parse/lexer.js +21 -8
  26. package/browser/dist/parse/parser.js +121 -45
  27. package/browser/dist/public-api.js +4 -5
  28. package/browser/dist/schema/Schema.js +14 -4
  29. package/browser/dist/schema/common/null.js +3 -1
  30. package/browser/dist/schema/core/float.js +4 -1
  31. package/browser/dist/schema/tags.js +9 -9
  32. package/browser/dist/schema/yaml-1.1/float.js +4 -1
  33. package/browser/dist/schema/yaml-1.1/omap.js +1 -1
  34. package/browser/dist/schema/yaml-1.1/pairs.js +2 -1
  35. package/browser/dist/stringify/stringify.js +51 -22
  36. package/browser/dist/stringify/stringifyCollection.js +115 -85
  37. package/browser/dist/stringify/stringifyComment.js +18 -12
  38. package/browser/dist/stringify/stringifyDocument.js +37 -15
  39. package/browser/dist/stringify/stringifyPair.js +34 -20
  40. package/browser/dist/stringify/stringifyString.js +8 -11
  41. package/browser/dist/visit.js +150 -62
  42. package/dist/compose/compose-doc.js +3 -2
  43. package/dist/compose/compose-node.d.ts +1 -0
  44. package/dist/compose/compose-node.js +13 -4
  45. package/dist/compose/compose-scalar.js +15 -9
  46. package/dist/compose/composer.d.ts +1 -1
  47. package/dist/compose/composer.js +8 -9
  48. package/dist/compose/resolve-block-map.js +9 -2
  49. package/dist/compose/resolve-block-scalar.js +12 -3
  50. package/dist/compose/resolve-block-seq.js +5 -0
  51. package/dist/compose/resolve-flow-collection.js +14 -5
  52. package/dist/compose/resolve-flow-scalar.js +2 -1
  53. package/dist/compose/resolve-props.d.ts +1 -1
  54. package/dist/compose/resolve-props.js +4 -0
  55. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  56. package/dist/compose/util-flow-indent-check.js +17 -0
  57. package/dist/doc/Document.d.ts +7 -4
  58. package/dist/doc/Document.js +56 -23
  59. package/dist/doc/anchors.d.ts +2 -2
  60. package/dist/doc/anchors.js +2 -2
  61. package/dist/doc/createNode.d.ts +1 -1
  62. package/dist/doc/createNode.js +7 -4
  63. package/dist/doc/directives.d.ts +4 -2
  64. package/dist/doc/directives.js +7 -4
  65. package/dist/index.d.ts +2 -2
  66. package/dist/index.js +1 -2
  67. package/dist/nodes/Pair.js +2 -2
  68. package/dist/nodes/Scalar.js +1 -1
  69. package/dist/nodes/YAMLMap.d.ts +3 -1
  70. package/dist/nodes/YAMLMap.js +6 -5
  71. package/dist/nodes/YAMLSeq.js +2 -2
  72. package/dist/nodes/addPairToJSMap.js +1 -1
  73. package/dist/nodes/toJS.js +3 -1
  74. package/dist/options.d.ts +42 -12
  75. package/dist/parse/cst-visit.js +2 -2
  76. package/dist/parse/lexer.js +21 -8
  77. package/dist/parse/parser.js +121 -45
  78. package/dist/public-api.js +4 -5
  79. package/dist/schema/Schema.d.ts +5 -4
  80. package/dist/schema/Schema.js +13 -3
  81. package/dist/schema/common/null.js +3 -1
  82. package/dist/schema/core/float.js +4 -1
  83. package/dist/schema/tags.d.ts +1 -2
  84. package/dist/schema/tags.js +9 -9
  85. package/dist/schema/yaml-1.1/float.js +4 -1
  86. package/dist/schema/yaml-1.1/omap.js +1 -1
  87. package/dist/schema/yaml-1.1/pairs.js +2 -1
  88. package/dist/stringify/stringify.d.ts +5 -3
  89. package/dist/stringify/stringify.js +51 -22
  90. package/dist/stringify/stringifyCollection.d.ts +2 -6
  91. package/dist/stringify/stringifyCollection.js +114 -84
  92. package/dist/stringify/stringifyComment.d.ts +10 -2
  93. package/dist/stringify/stringifyComment.js +19 -12
  94. package/dist/stringify/stringifyDocument.js +36 -14
  95. package/dist/stringify/stringifyPair.js +33 -19
  96. package/dist/stringify/stringifyString.js +8 -11
  97. package/dist/test-events.js +13 -12
  98. package/dist/visit.d.ts +49 -10
  99. package/dist/visit.js +149 -60
  100. package/package.json +9 -8
  101. package/browser/dist/options.js +0 -18
  102. 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,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,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 !== 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,19 +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
17
  for (const collItem of bm.items) {
15
18
  const { start, key, sep, value } = collItem;
16
19
  // key properties
17
20
  const keyProps = resolveProps.resolveProps(start, {
18
21
  indicator: 'explicit-key-ind',
19
- 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],
20
23
  offset,
21
24
  onError,
22
25
  startOnNewline: true
@@ -50,10 +53,12 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
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 !== null && sep !== void 0 ? sep : [], {
57
62
  indicator: 'map-value-ind',
58
63
  next: value,
59
64
  offset: keyNode.range[2],
@@ -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 === 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,20 +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
27
  const collItem = fc.items[i];
24
28
  const { start, key, sep, value } = collItem;
25
29
  const props = resolveProps.resolveProps(start, {
26
30
  flow: fcName,
27
31
  indicator: 'explicit-key-ind',
28
- 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],
29
33
  offset,
30
34
  onError,
31
35
  startOnNewline: false
@@ -42,6 +46,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
42
46
  else
43
47
  coll.comment = props.comment;
44
48
  }
49
+ offset = props.end;
45
50
  continue;
46
51
  }
47
52
  if (!isMap && ctx.options.strict && utilContainsNewline.containsNewline(key))
@@ -72,7 +77,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
72
77
  if (prevItemComment) {
73
78
  let prev = coll.items[coll.items.length - 1];
74
79
  if (Node.isPair(prev))
75
- prev = prev.value || prev.key;
80
+ prev = (_a = prev.value) !== null && _a !== void 0 ? _a : prev.key;
76
81
  if (prev.comment)
77
82
  prev.comment += '\n' + prevItemComment;
78
83
  else
@@ -102,7 +107,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
102
107
  if (isBlock(key))
103
108
  onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
104
109
  // value properties
105
- const valueProps = resolveProps.resolveProps(sep || [], {
110
+ const valueProps = resolveProps.resolveProps(sep !== null && sep !== void 0 ? sep : [], {
106
111
  flow: fcName,
107
112
  indicator: 'map-value-ind',
108
113
  next: value,
@@ -171,7 +176,11 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
171
176
  if (ce && ce.source === expectedEnd)
172
177
  cePos = ce.offset + ce.source.length;
173
178
  else {
174
- 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);
175
184
  if (ce && ce.source.length !== 1)
176
185
  ee.unshift(ce);
177
186
  }
@@ -74,6 +74,7 @@ function singleQuotedValue(source, onError) {
74
74
  return foldLines(source.slice(1, -1)).replace(/''/g, "'");
75
75
  }
76
76
  function foldLines(source) {
77
+ var _a;
77
78
  /**
78
79
  * The negative lookbehind here and in the `re` RegExp is to
79
80
  * prevent causing a polynomial search time in certain cases.
@@ -113,7 +114,7 @@ function foldLines(source) {
113
114
  const last = /[ \t]*(.*)/sy;
114
115
  last.lastIndex = pos;
115
116
  match = last.exec(source);
116
- return res + sep + ((match && match[1]) || '');
117
+ return res + sep + ((_a = match === null || match === void 0 ? void 0 : match[1]) !== null && _a !== void 0 ? _a : '');
117
118
  }
118
119
  function doubleQuotedValue(source, onError) {
119
120
  let res = '';
@@ -1,7 +1,7 @@
1
1
  import type { SourceToken, Token } from '../parse/cst.js';
2
2
  import type { ComposeErrorHandler } from './composer.js';
3
3
  export interface ResolvePropsArg {
4
- flow?: string;
4
+ flow?: 'flow map' | 'flow sequence';
5
5
  indicator: 'doc-start' | 'explicit-key-ind' | 'map-value-ind' | 'seq-item-ind';
6
6
  next: Token | null | undefined;
7
7
  offset: number;