yaml 2.0.0 → 2.1.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 (85) hide show
  1. package/browser/dist/compose/compose-collection.js +1 -1
  2. package/browser/dist/compose/compose-doc.js +2 -2
  3. package/browser/dist/compose/compose-scalar.js +6 -7
  4. package/browser/dist/compose/composer.js +2 -3
  5. package/browser/dist/compose/resolve-block-map.js +8 -8
  6. package/browser/dist/compose/resolve-block-scalar.js +1 -1
  7. package/browser/dist/compose/resolve-flow-collection.js +3 -4
  8. package/browser/dist/compose/resolve-flow-scalar.js +1 -2
  9. package/browser/dist/compose/resolve-props.js +6 -2
  10. package/browser/dist/compose/util-empty-scalar-position.js +1 -1
  11. package/browser/dist/compose/util-flow-indent-check.js +1 -1
  12. package/browser/dist/doc/Document.js +6 -6
  13. package/browser/dist/doc/createNode.js +5 -7
  14. package/browser/dist/nodes/Collection.js +2 -1
  15. package/browser/dist/nodes/Pair.js +2 -2
  16. package/browser/dist/nodes/Scalar.js +1 -1
  17. package/browser/dist/nodes/YAMLMap.js +6 -7
  18. package/browser/dist/nodes/YAMLSeq.js +1 -9
  19. package/browser/dist/nodes/addPairToJSMap.js +1 -1
  20. package/browser/dist/nodes/toJS.js +1 -1
  21. package/browser/dist/parse/cst-scalar.js +1 -2
  22. package/browser/dist/parse/cst-visit.js +2 -2
  23. package/browser/dist/parse/lexer.js +1 -2
  24. package/browser/dist/parse/parser.js +24 -26
  25. package/browser/dist/public-api.js +3 -4
  26. package/browser/dist/schema/Schema.js +1 -1
  27. package/browser/dist/schema/yaml-1.1/omap.js +1 -1
  28. package/browser/dist/schema/yaml-1.1/pairs.js +1 -2
  29. package/browser/dist/schema/yaml-1.1/set.js +4 -0
  30. package/browser/dist/stringify/stringify.js +6 -8
  31. package/browser/dist/stringify/stringifyCollection.js +1 -2
  32. package/browser/dist/stringify/stringifyDocument.js +1 -2
  33. package/browser/dist/stringify/stringifyString.js +2 -2
  34. package/browser/dist/visit.js +5 -6
  35. package/dist/compose/compose-collection.js +1 -1
  36. package/dist/compose/compose-doc.js +2 -2
  37. package/dist/compose/compose-scalar.js +6 -7
  38. package/dist/compose/composer.js +2 -3
  39. package/dist/compose/resolve-block-map.js +8 -8
  40. package/dist/compose/resolve-block-scalar.js +1 -1
  41. package/dist/compose/resolve-flow-collection.js +3 -4
  42. package/dist/compose/resolve-flow-scalar.js +1 -2
  43. package/dist/compose/resolve-props.d.ts +1 -0
  44. package/dist/compose/resolve-props.js +6 -2
  45. package/dist/compose/util-empty-scalar-position.js +1 -1
  46. package/dist/compose/util-flow-indent-check.js +1 -1
  47. package/dist/doc/Document.d.ts +10 -10
  48. package/dist/doc/Document.js +6 -6
  49. package/dist/doc/anchors.d.ts +1 -1
  50. package/dist/doc/createNode.js +5 -7
  51. package/dist/index.d.ts +2 -0
  52. package/dist/nodes/Collection.d.ts +2 -2
  53. package/dist/nodes/Collection.js +2 -1
  54. package/dist/nodes/Node.d.ts +15 -9
  55. package/dist/nodes/Pair.d.ts +1 -1
  56. package/dist/nodes/Pair.js +2 -2
  57. package/dist/nodes/Scalar.js +1 -1
  58. package/dist/nodes/YAMLMap.d.ts +6 -3
  59. package/dist/nodes/YAMLMap.js +6 -7
  60. package/dist/nodes/YAMLSeq.d.ts +4 -1
  61. package/dist/nodes/YAMLSeq.js +1 -9
  62. package/dist/nodes/addPairToJSMap.js +1 -1
  63. package/dist/nodes/toJS.js +1 -1
  64. package/dist/options.d.ts +2 -1
  65. package/dist/parse/cst-scalar.js +1 -2
  66. package/dist/parse/cst-visit.js +2 -2
  67. package/dist/parse/lexer.js +1 -2
  68. package/dist/parse/parser.js +24 -26
  69. package/dist/public-api.js +3 -4
  70. package/dist/schema/Schema.d.ts +3 -3
  71. package/dist/schema/Schema.js +1 -1
  72. package/dist/schema/json-schema.d.ts +69 -0
  73. package/dist/schema/yaml-1.1/omap.d.ts +7 -3
  74. package/dist/schema/yaml-1.1/omap.js +1 -1
  75. package/dist/schema/yaml-1.1/pairs.js +1 -2
  76. package/dist/schema/yaml-1.1/set.d.ts +6 -2
  77. package/dist/schema/yaml-1.1/set.js +4 -0
  78. package/dist/stringify/stringify.js +6 -8
  79. package/dist/stringify/stringifyCollection.js +1 -2
  80. package/dist/stringify/stringifyDocument.js +1 -2
  81. package/dist/stringify/stringifyString.js +2 -2
  82. package/dist/test-events.js +2 -3
  83. package/dist/visit.js +5 -6
  84. package/package.json +3 -3
  85. package/dist/node_modules/tslib/tslib.es6.js +0 -76
@@ -7,7 +7,7 @@ function includesToken(list, type) {
7
7
  return true;
8
8
  return false;
9
9
  }
10
- function includesNonEmpty(list) {
10
+ function findNonEmptyIndex(list) {
11
11
  for (let i = 0; i < list.length; ++i) {
12
12
  switch (list[i].type) {
13
13
  case 'space':
@@ -15,13 +15,13 @@ function includesNonEmpty(list) {
15
15
  case 'newline':
16
16
  break;
17
17
  default:
18
- return true;
18
+ return i;
19
19
  }
20
20
  }
21
- return false;
21
+ return -1;
22
22
  }
23
23
  function isFlowToken(token) {
24
- switch (token === null || token === void 0 ? void 0 : token.type) {
24
+ switch (token?.type) {
25
25
  case 'alias':
26
26
  case 'scalar':
27
27
  case 'single-quoted-scalar':
@@ -33,13 +33,12 @@ function isFlowToken(token) {
33
33
  }
34
34
  }
35
35
  function getPrevProps(parent) {
36
- var _a;
37
36
  switch (parent.type) {
38
37
  case 'document':
39
38
  return parent.start;
40
39
  case 'block-map': {
41
40
  const it = parent.items[parent.items.length - 1];
42
- return (_a = it.sep) !== null && _a !== void 0 ? _a : it.start;
41
+ return it.sep ?? it.start;
43
42
  }
44
43
  case 'block-seq':
45
44
  return parent.items[parent.items.length - 1].start;
@@ -50,7 +49,6 @@ function getPrevProps(parent) {
50
49
  }
51
50
  /** Note: May modify input array */
52
51
  function getFirstKeyStartProps(prev) {
53
- var _a;
54
52
  if (prev.length === 0)
55
53
  return [];
56
54
  let i = prev.length;
@@ -64,7 +62,7 @@ function getFirstKeyStartProps(prev) {
64
62
  break loop;
65
63
  }
66
64
  }
67
- while (((_a = prev[++i]) === null || _a === void 0 ? void 0 : _a.type) === 'space') {
65
+ while (prev[++i]?.type === 'space') {
68
66
  /* loop */
69
67
  }
70
68
  return prev.splice(i, prev.length);
@@ -266,7 +264,7 @@ class Parser {
266
264
  return this.stack[this.stack.length - n];
267
265
  }
268
266
  *pop(error) {
269
- const token = error !== null && error !== void 0 ? error : this.stack.pop();
267
+ const token = error ?? this.stack.pop();
270
268
  /* istanbul ignore if should not happen */
271
269
  if (!token) {
272
270
  const message = 'Tried to pop an empty stack';
@@ -343,7 +341,7 @@ class Parser {
343
341
  !last.sep &&
344
342
  !last.value &&
345
343
  last.start.length > 0 &&
346
- !includesNonEmpty(last.start) &&
344
+ findNonEmptyIndex(last.start) === -1 &&
347
345
  (token.indent === 0 ||
348
346
  last.start.every(st => st.type !== 'comment' || st.indent < token.indent))) {
349
347
  if (top.type === 'document')
@@ -391,7 +389,7 @@ class Parser {
391
389
  return yield* this.lineEnd(doc);
392
390
  switch (this.type) {
393
391
  case 'doc-start': {
394
- if (includesNonEmpty(doc.start)) {
392
+ if (findNonEmptyIndex(doc.start) !== -1) {
395
393
  yield* this.pop();
396
394
  yield* this.step();
397
395
  }
@@ -471,7 +469,6 @@ class Parser {
471
469
  }
472
470
  }
473
471
  *blockMap(map) {
474
- var _a;
475
472
  const it = map.items[map.items.length - 1];
476
473
  // it.sep is true-ish if pair already has key or : separator
477
474
  switch (this.type) {
@@ -480,26 +477,30 @@ class Parser {
480
477
  if (it.value) {
481
478
  const end = 'end' in it.value ? it.value.end : undefined;
482
479
  const last = Array.isArray(end) ? end[end.length - 1] : undefined;
483
- if ((last === null || last === void 0 ? void 0 : last.type) === 'comment')
484
- end === null || end === void 0 ? void 0 : end.push(this.sourceToken);
480
+ if (last?.type === 'comment')
481
+ end?.push(this.sourceToken);
485
482
  else
486
483
  map.items.push({ start: [this.sourceToken] });
487
484
  }
488
- else if (it.sep)
485
+ else if (it.sep) {
489
486
  it.sep.push(this.sourceToken);
490
- else
487
+ }
488
+ else {
491
489
  it.start.push(this.sourceToken);
490
+ }
492
491
  return;
493
492
  case 'space':
494
493
  case 'comment':
495
- if (it.value)
494
+ if (it.value) {
496
495
  map.items.push({ start: [this.sourceToken] });
497
- else if (it.sep)
496
+ }
497
+ else if (it.sep) {
498
498
  it.sep.push(this.sourceToken);
499
+ }
499
500
  else {
500
501
  if (this.atIndentedComment(it.start, map.indent)) {
501
502
  const prev = map.items[map.items.length - 2];
502
- const end = (_a = prev === null || prev === void 0 ? void 0 : prev.value) === null || _a === void 0 ? void 0 : _a.end;
503
+ const end = prev?.value?.end;
503
504
  if (Array.isArray(end)) {
504
505
  Array.prototype.push.apply(end, it.start);
505
506
  end.push(this.sourceToken);
@@ -512,9 +513,7 @@ class Parser {
512
513
  return;
513
514
  }
514
515
  if (this.indent >= map.indent) {
515
- const atNextItem = !this.onKeyLine &&
516
- this.indent === map.indent &&
517
- (it.sep || includesNonEmpty(it.start));
516
+ const atNextItem = !this.onKeyLine && this.indent === map.indent && it.sep;
518
517
  // For empty nodes, assign newline-separated not indented empty tokens to following node
519
518
  let start = [];
520
519
  if (atNextItem && it.sep && !it.value) {
@@ -678,15 +677,14 @@ class Parser {
678
677
  yield* this.step();
679
678
  }
680
679
  *blockSequence(seq) {
681
- var _a;
682
680
  const it = seq.items[seq.items.length - 1];
683
681
  switch (this.type) {
684
682
  case 'newline':
685
683
  if (it.value) {
686
684
  const end = 'end' in it.value ? it.value.end : undefined;
687
685
  const last = Array.isArray(end) ? end[end.length - 1] : undefined;
688
- if ((last === null || last === void 0 ? void 0 : last.type) === 'comment')
689
- end === null || end === void 0 ? void 0 : end.push(this.sourceToken);
686
+ if (last?.type === 'comment')
687
+ end?.push(this.sourceToken);
690
688
  else
691
689
  seq.items.push({ start: [this.sourceToken] });
692
690
  }
@@ -700,7 +698,7 @@ class Parser {
700
698
  else {
701
699
  if (this.atIndentedComment(it.start, seq.indent)) {
702
700
  const prev = seq.items[seq.items.length - 2];
703
- const end = (_a = prev === null || prev === void 0 ? void 0 : prev.value) === null || _a === void 0 ? void 0 : _a.end;
701
+ const end = prev?.value?.end;
704
702
  if (Array.isArray(end)) {
705
703
  Array.prototype.push.apply(end, it.start);
706
704
  end.push(this.sourceToken);
@@ -21,7 +21,7 @@ function parseOptions(options) {
21
21
  */
22
22
  function parseAllDocuments(source, options = {}) {
23
23
  const { lineCounter, prettyErrors } = parseOptions(options);
24
- const parser = new Parser(lineCounter === null || lineCounter === void 0 ? void 0 : lineCounter.addNewLine);
24
+ const parser = new Parser(lineCounter?.addNewLine);
25
25
  const composer = new Composer(options);
26
26
  const docs = Array.from(composer.compose(parser.parse(source)));
27
27
  if (prettyErrors && lineCounter)
@@ -36,7 +36,7 @@ function parseAllDocuments(source, options = {}) {
36
36
  /** Parse an input string into a single YAML.Document */
37
37
  function parseDocument(source, options = {}) {
38
38
  const { lineCounter, prettyErrors } = parseOptions(options);
39
- const parser = new Parser(lineCounter === null || lineCounter === void 0 ? void 0 : lineCounter.addNewLine);
39
+ const parser = new Parser(lineCounter?.addNewLine);
40
40
  const composer = new Composer(options);
41
41
  // `doc` is always set by compose.end(true) at the very latest
42
42
  let doc = null;
@@ -75,7 +75,6 @@ function parse(src, reviver, options) {
75
75
  return doc.toJS(Object.assign({ reviver: _reviver }, options));
76
76
  }
77
77
  function stringify(value, replacer, options) {
78
- var _a;
79
78
  let _replacer = null;
80
79
  if (typeof replacer === 'function' || Array.isArray(replacer)) {
81
80
  _replacer = replacer;
@@ -90,7 +89,7 @@ function stringify(value, replacer, options) {
90
89
  options = indent < 1 ? undefined : indent > 8 ? { indent: 8 } : { indent };
91
90
  }
92
91
  if (value === undefined) {
93
- const { keepUndefined } = (_a = options !== null && options !== void 0 ? options : replacer) !== null && _a !== void 0 ? _a : {};
92
+ const { keepUndefined } = options ?? replacer ?? {};
94
93
  if (!keepUndefined)
95
94
  return undefined;
96
95
  }
@@ -16,7 +16,7 @@ class Schema {
16
16
  this.name = (typeof schema === 'string' && schema) || 'core';
17
17
  this.knownTags = resolveKnownTags ? coreKnownTags : {};
18
18
  this.tags = getTags(customTags, this.name);
19
- this.toStringOptions = toStringDefaults !== null && toStringDefaults !== void 0 ? toStringDefaults : null;
19
+ this.toStringOptions = toStringDefaults ?? null;
20
20
  Object.defineProperty(this, MAP, { value: map });
21
21
  Object.defineProperty(this, SCALAR, { value: string });
22
22
  Object.defineProperty(this, SEQ, { value: seq });
@@ -22,7 +22,7 @@ class YAMLOMap extends YAMLSeq {
22
22
  if (!ctx)
23
23
  return super.toJSON(_);
24
24
  const map = new Map();
25
- if (ctx === null || ctx === void 0 ? void 0 : ctx.onCreate)
25
+ if (ctx?.onCreate)
26
26
  ctx.onCreate(map);
27
27
  for (const pair of this.items) {
28
28
  let key, value;
@@ -4,7 +4,6 @@ import { Scalar } from '../../nodes/Scalar.js';
4
4
  import { YAMLSeq } from '../../nodes/YAMLSeq.js';
5
5
 
6
6
  function resolvePairs(seq, onError) {
7
- var _a;
8
7
  if (isSeq(seq)) {
9
8
  for (let i = 0; i < seq.items.length; ++i) {
10
9
  let item = seq.items[i];
@@ -19,7 +18,7 @@ function resolvePairs(seq, onError) {
19
18
  ? `${item.commentBefore}\n${pair.key.commentBefore}`
20
19
  : item.commentBefore;
21
20
  if (item.comment) {
22
- const cn = (_a = pair.value) !== null && _a !== void 0 ? _a : pair.key;
21
+ const cn = pair.value ?? pair.key;
23
22
  cn.comment = cn.comment
24
23
  ? `${item.comment}\n${cn.comment}`
25
24
  : item.comment;
@@ -22,6 +22,10 @@ class YAMLSet extends YAMLMap {
22
22
  if (!prev)
23
23
  this.items.push(pair);
24
24
  }
25
+ /**
26
+ * If `keepPair` is `true`, returns the Pair matching `key`.
27
+ * Otherwise, returns the value of that Pair's key.
28
+ */
25
29
  get(key, keepPair) {
26
30
  const pair = findPair(this.items, key);
27
31
  return !keepPair && isPair(pair)
@@ -43,26 +43,25 @@ function createStringifyContext(doc, options) {
43
43
  };
44
44
  }
45
45
  function getTagObject(tags, item) {
46
- var _a, _b, _c, _d;
47
46
  if (item.tag) {
48
47
  const match = tags.filter(t => t.tag === item.tag);
49
48
  if (match.length > 0)
50
- return (_a = match.find(t => t.format === item.format)) !== null && _a !== void 0 ? _a : match[0];
49
+ return match.find(t => t.format === item.format) ?? match[0];
51
50
  }
52
51
  let tagObj = undefined;
53
52
  let obj;
54
53
  if (isScalar(item)) {
55
54
  obj = item.value;
56
- const match = tags.filter(t => { var _a; return (_a = t.identify) === null || _a === void 0 ? void 0 : _a.call(t, obj); });
55
+ const match = tags.filter(t => t.identify?.(obj));
57
56
  tagObj =
58
- (_b = match.find(t => t.format === item.format)) !== null && _b !== void 0 ? _b : match.find(t => !t.format);
57
+ match.find(t => t.format === item.format) ?? match.find(t => !t.format);
59
58
  }
60
59
  else {
61
60
  obj = item;
62
61
  tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
63
62
  }
64
63
  if (!tagObj) {
65
- const name = (_d = (_c = obj === null || obj === void 0 ? void 0 : obj.constructor) === null || _c === void 0 ? void 0 : _c.name) !== null && _d !== void 0 ? _d : typeof obj;
64
+ const name = obj?.constructor?.name ?? typeof obj;
66
65
  throw new Error(`Tag not resolved for ${name} value`);
67
66
  }
68
67
  return tagObj;
@@ -83,13 +82,12 @@ function stringifyProps(node, tagObj, { anchors, doc }) {
83
82
  return props.join(' ');
84
83
  }
85
84
  function stringify(item, ctx, onComment, onChompKeep) {
86
- var _a, _b;
87
85
  if (isPair(item))
88
86
  return item.toString(ctx, onComment, onChompKeep);
89
87
  if (isAlias(item)) {
90
88
  if (ctx.doc.directives)
91
89
  return item.toString(ctx);
92
- if ((_a = ctx.resolvedAliases) === null || _a === void 0 ? void 0 : _a.has(item)) {
90
+ if (ctx.resolvedAliases?.has(item)) {
93
91
  throw new TypeError(`Cannot stringify circular structure without alias nodes`);
94
92
  }
95
93
  else {
@@ -108,7 +106,7 @@ function stringify(item, ctx, onComment, onChompKeep) {
108
106
  tagObj = getTagObject(ctx.doc.schema.tags, node);
109
107
  const props = stringifyProps(node, tagObj, ctx);
110
108
  if (props.length > 0)
111
- ctx.indentAtStart = ((_b = ctx.indentAtStart) !== null && _b !== void 0 ? _b : 0) + props.length + 1;
109
+ ctx.indentAtStart = (ctx.indentAtStart ?? 0) + props.length + 1;
112
110
  const str = typeof tagObj.stringify === 'function'
113
111
  ? tagObj.stringify(node, ctx, onComment, onChompKeep)
114
112
  : isScalar(node)
@@ -4,8 +4,7 @@ import { stringify } from './stringify.js';
4
4
  import { lineComment, indentComment } from './stringifyComment.js';
5
5
 
6
6
  function stringifyCollection(collection, ctx, options) {
7
- var _a;
8
- const flow = (_a = ctx.inFlow) !== null && _a !== void 0 ? _a : collection.flow;
7
+ const flow = ctx.inFlow ?? collection.flow;
9
8
  const stringify = flow ? stringifyFlowCollection : stringifyBlockCollection;
10
9
  return stringify(collection, ctx, options);
11
10
  }
@@ -3,7 +3,6 @@ import { createStringifyContext, stringify } from './stringify.js';
3
3
  import { indentComment, lineComment } from './stringifyComment.js';
4
4
 
5
5
  function stringifyDocument(doc, options) {
6
- var _a;
7
6
  const lines = [];
8
7
  let hasDirectives = options.directives === true;
9
8
  if (options.directives !== false && doc.directives) {
@@ -55,7 +54,7 @@ function stringifyDocument(doc, options) {
55
54
  else {
56
55
  lines.push(stringify(doc.contents, ctx));
57
56
  }
58
- if ((_a = doc.directives) === null || _a === void 0 ? void 0 : _a.docEnd) {
57
+ if (doc.directives?.docEnd) {
59
58
  if (doc.comment) {
60
59
  const cs = commentString(doc.comment);
61
60
  if (cs.includes('\n')) {
@@ -263,9 +263,9 @@ 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
- 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)); };
266
+ const test = (tag) => tag.default && tag.tag !== 'tag:yaml.org,2002:str' && tag.test?.test(str);
267
267
  const { compat, tags } = ctx.doc.schema;
268
- if (tags.some(test) || (compat === null || compat === void 0 ? void 0 : compat.some(test)))
268
+ if (tags.some(test) || compat?.some(test))
269
269
  return quotedString(value, ctx);
270
270
  }
271
271
  return implicitKey
@@ -196,19 +196,18 @@ function initVisitor(visitor) {
196
196
  return visitor;
197
197
  }
198
198
  function callVisitor(key, node, visitor, path) {
199
- var _a, _b, _c, _d, _e;
200
199
  if (typeof visitor === 'function')
201
200
  return visitor(key, node, path);
202
201
  if (isMap(node))
203
- return (_a = visitor.Map) === null || _a === void 0 ? void 0 : _a.call(visitor, key, node, path);
202
+ return visitor.Map?.(key, node, path);
204
203
  if (isSeq(node))
205
- return (_b = visitor.Seq) === null || _b === void 0 ? void 0 : _b.call(visitor, key, node, path);
204
+ return visitor.Seq?.(key, node, path);
206
205
  if (isPair(node))
207
- return (_c = visitor.Pair) === null || _c === void 0 ? void 0 : _c.call(visitor, key, node, path);
206
+ return visitor.Pair?.(key, node, path);
208
207
  if (isScalar(node))
209
- return (_d = visitor.Scalar) === null || _d === void 0 ? void 0 : _d.call(visitor, key, node, path);
208
+ return visitor.Scalar?.(key, node, path);
210
209
  if (isAlias(node))
211
- return (_e = visitor.Alias) === null || _e === void 0 ? void 0 : _e.call(visitor, key, node, path);
210
+ return visitor.Alias?.(key, node, path);
212
211
  return undefined;
213
212
  }
214
213
  function replaceNode(key, path, node) {
@@ -53,7 +53,7 @@ function composeCollection(CN, ctx, token, tagToken, onError) {
53
53
  : new Scalar.Scalar(res);
54
54
  node.range = coll.range;
55
55
  node.tag = tagName;
56
- if (tag === null || tag === void 0 ? void 0 : tag.format)
56
+ if (tag?.format)
57
57
  node.format = tag.format;
58
58
  return node;
59
59
  }
@@ -6,7 +6,7 @@ var resolveEnd = require('./resolve-end.js');
6
6
  var resolveProps = require('./resolve-props.js');
7
7
 
8
8
  function composeDoc(options, directives, { offset, start, value, end }, onError) {
9
- const opts = Object.assign({ directives }, options);
9
+ const opts = Object.assign({ _directives: directives }, options);
10
10
  const doc = new Document.Document(undefined, opts);
11
11
  const ctx = {
12
12
  atRoot: true,
@@ -16,7 +16,7 @@ function composeDoc(options, directives, { offset, start, value, end }, onError)
16
16
  };
17
17
  const props = resolveProps.resolveProps(start, {
18
18
  indicator: 'doc-start',
19
- next: value !== null && value !== void 0 ? value : end === null || end === void 0 ? void 0 : end[0],
19
+ next: value ?? end?.[0],
20
20
  offset,
21
21
  onError,
22
22
  startOnNewline: true
@@ -19,12 +19,12 @@ function composeScalar(ctx, token, tagToken, onError) {
19
19
  : ctx.schema[Node.SCALAR];
20
20
  let scalar;
21
21
  try {
22
- const res = tag.resolve(value, msg => onError(tagToken !== null && tagToken !== void 0 ? tagToken : token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
22
+ const res = tag.resolve(value, msg => onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
23
23
  scalar = Node.isScalar(res) ? res : new Scalar.Scalar(res);
24
24
  }
25
25
  catch (error) {
26
26
  const msg = error instanceof Error ? error.message : String(error);
27
- onError(tagToken !== null && tagToken !== void 0 ? tagToken : token, 'TAG_RESOLVE_FAILED', msg);
27
+ onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg);
28
28
  scalar = new Scalar.Scalar(value);
29
29
  }
30
30
  scalar.range = range;
@@ -40,7 +40,6 @@ function composeScalar(ctx, token, tagToken, onError) {
40
40
  return scalar;
41
41
  }
42
42
  function findScalarTagByName(schema, value, tagName, tagToken, onError) {
43
- var _a;
44
43
  if (tagName === '!')
45
44
  return schema[Node.SCALAR]; // non-specific tag
46
45
  const matchWithTest = [];
@@ -53,7 +52,7 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
53
52
  }
54
53
  }
55
54
  for (const tag of matchWithTest)
56
- if ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(value))
55
+ if (tag.test?.test(value))
57
56
  return tag;
58
57
  const kt = schema.knownTags[tagName];
59
58
  if (kt && !kt.collection) {
@@ -66,10 +65,10 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
66
65
  return schema[Node.SCALAR];
67
66
  }
68
67
  function findScalarTagByTest({ directives, schema }, value, token, onError) {
69
- var _a;
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];
68
+ const tag = schema.tags.find(tag => tag.default && tag.test?.test(value)) || schema[Node.SCALAR];
71
69
  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];
70
+ const compat = schema.compat.find(tag => tag.default && tag.test?.test(value)) ??
71
+ schema[Node.SCALAR];
73
72
  if (tag.tag !== compat.tag) {
74
73
  const ts = directives.tagString(tag.tag);
75
74
  const cs = directives.tagString(compat.tag);
@@ -16,7 +16,6 @@ function getErrorPos(src) {
16
16
  return [offset, offset + (typeof source === 'string' ? source.length : 1)];
17
17
  }
18
18
  function parsePrelude(prelude) {
19
- var _a;
20
19
  let comment = '';
21
20
  let atComment = false;
22
21
  let afterEmptyLine = false;
@@ -31,7 +30,7 @@ function parsePrelude(prelude) {
31
30
  afterEmptyLine = false;
32
31
  break;
33
32
  case '%':
34
- if (((_a = prelude[i + 1]) === null || _a === void 0 ? void 0 : _a[0]) !== '#')
33
+ if (prelude[i + 1]?.[0] !== '#')
35
34
  i += 1;
36
35
  atComment = false;
37
36
  break;
@@ -208,7 +207,7 @@ class Composer {
208
207
  this.doc = null;
209
208
  }
210
209
  else if (forceDoc) {
211
- const opts = Object.assign({ directives: this.directives }, this.options);
210
+ const opts = Object.assign({ _directives: this.directives }, this.options);
212
211
  const doc = new Document.Document(undefined, opts);
213
212
  if (this.atDirectives)
214
213
  this.onError(endOffset, 'MISSING_CHAR', 'Missing directives-end indicator line');
@@ -9,7 +9,6 @@ var utilMapIncludes = require('./util-map-includes.js');
9
9
 
10
10
  const startColMsg = 'All mapping items must start at the same column';
11
11
  function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
12
- var _a;
13
12
  const map = new YAMLMap.YAMLMap(ctx.schema);
14
13
  if (ctx.atRoot)
15
14
  ctx.atRoot = false;
@@ -19,7 +18,7 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
19
18
  // key properties
20
19
  const keyProps = resolveProps.resolveProps(start, {
21
20
  indicator: 'explicit-key-ind',
22
- next: key !== null && key !== void 0 ? key : sep === null || sep === void 0 ? void 0 : sep[0],
21
+ next: key ?? sep?.[0],
23
22
  offset,
24
23
  onError,
25
24
  startOnNewline: true
@@ -42,12 +41,13 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
42
41
  }
43
42
  continue;
44
43
  }
44
+ if (keyProps.hasNewlineAfterProp || utilContainsNewline.containsNewline(key)) {
45
+ onError(key ?? start[start.length - 1], 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
46
+ }
45
47
  }
46
- else if (((_a = keyProps.found) === null || _a === void 0 ? void 0 : _a.indent) !== bm.indent)
48
+ else if (keyProps.found?.indent !== bm.indent) {
47
49
  onError(offset, 'BAD_INDENT', startColMsg);
48
- if (implicitKey && utilContainsNewline.containsNewline(key))
49
- onError(key, // checked by containsNewline()
50
- 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
50
+ }
51
51
  // key value
52
52
  const keyStart = keyProps.end;
53
53
  const keyNode = key
@@ -58,7 +58,7 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
58
58
  if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
59
59
  onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
60
60
  // value properties
61
- const valueProps = resolveProps.resolveProps(sep !== null && sep !== void 0 ? sep : [], {
61
+ const valueProps = resolveProps.resolveProps(sep ?? [], {
62
62
  indicator: 'map-value-ind',
63
63
  next: value,
64
64
  offset: keyNode.range[2],
@@ -68,7 +68,7 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
68
68
  offset = valueProps.end;
69
69
  if (valueProps.found) {
70
70
  if (implicitKey) {
71
- if ((value === null || value === void 0 ? void 0 : value.type) === 'block-map' && !valueProps.hasNewline)
71
+ if (value?.type === 'block-map' && !valueProps.hasNewline)
72
72
  onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
73
73
  if (ctx.options.strict &&
74
74
  keyProps.start < valueProps.found.offset - 1024)
@@ -184,7 +184,7 @@ function splitLines(source) {
184
184
  const split = source.split(/\n( *)/);
185
185
  const first = split[0];
186
186
  const m = first.match(/^( *)/);
187
- const line0 = (m === null || m === void 0 ? void 0 : m[1])
187
+ const line0 = m?.[1]
188
188
  ? [m[1], first.slice(m[1].length)]
189
189
  : ['', first];
190
190
  const lines = [line0];
@@ -12,7 +12,6 @@ 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;
16
15
  const isMap = fc.start.source === '{';
17
16
  const fcName = isMap ? 'flow map' : 'flow sequence';
18
17
  const coll = isMap
@@ -29,7 +28,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
29
28
  const props = resolveProps.resolveProps(start, {
30
29
  flow: fcName,
31
30
  indicator: 'explicit-key-ind',
32
- next: key !== null && key !== void 0 ? key : sep === null || sep === void 0 ? void 0 : sep[0],
31
+ next: key ?? sep?.[0],
33
32
  offset,
34
33
  onError,
35
34
  startOnNewline: false
@@ -77,7 +76,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
77
76
  if (prevItemComment) {
78
77
  let prev = coll.items[coll.items.length - 1];
79
78
  if (Node.isPair(prev))
80
- prev = (_a = prev.value) !== null && _a !== void 0 ? _a : prev.key;
79
+ prev = prev.value ?? prev.key;
81
80
  if (prev.comment)
82
81
  prev.comment += '\n' + prevItemComment;
83
82
  else
@@ -107,7 +106,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
107
106
  if (isBlock(key))
108
107
  onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
109
108
  // value properties
110
- const valueProps = resolveProps.resolveProps(sep !== null && sep !== void 0 ? sep : [], {
109
+ const valueProps = resolveProps.resolveProps(sep ?? [], {
111
110
  flow: fcName,
112
111
  indicator: 'map-value-ind',
113
112
  next: value,
@@ -74,7 +74,6 @@ function singleQuotedValue(source, onError) {
74
74
  return foldLines(source.slice(1, -1)).replace(/''/g, "'");
75
75
  }
76
76
  function foldLines(source) {
77
- var _a;
78
77
  /**
79
78
  * The negative lookbehind here and in the `re` RegExp is to
80
79
  * prevent causing a polynomial search time in certain cases.
@@ -114,7 +113,7 @@ function foldLines(source) {
114
113
  const last = /[ \t]*(.*)/sy;
115
114
  last.lastIndex = pos;
116
115
  match = last.exec(source);
117
- return res + sep + ((_a = match === null || match === void 0 ? void 0 : match[1]) !== null && _a !== void 0 ? _a : '');
116
+ return res + sep + (match?.[1] ?? '');
118
117
  }
119
118
  function doubleQuotedValue(source, onError) {
120
119
  let res = '';
@@ -14,6 +14,7 @@ export declare function resolveProps(tokens: SourceToken[], { flow, indicator, n
14
14
  spaceBefore: boolean;
15
15
  comment: string;
16
16
  hasNewline: boolean;
17
+ hasNewlineAfterProp: boolean;
17
18
  anchor: SourceToken | null;
18
19
  tag: SourceToken | null;
19
20
  end: number;
@@ -7,6 +7,7 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
7
7
  let comment = '';
8
8
  let commentSep = '';
9
9
  let hasNewline = false;
10
+ let hasNewlineAfterProp = false;
10
11
  let reqSpace = false;
11
12
  let anchor = null;
12
13
  let tag = null;
@@ -56,6 +57,8 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
56
57
  commentSep += token.source;
57
58
  atNewline = true;
58
59
  hasNewline = true;
60
+ if (anchor || tag)
61
+ hasNewlineAfterProp = true;
59
62
  hasSpace = true;
60
63
  break;
61
64
  case 'anchor':
@@ -86,7 +89,7 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
86
89
  if (anchor || tag)
87
90
  onError(token, 'BAD_PROP_ORDER', `Anchors and tags must be after the ${token.source} indicator`);
88
91
  if (found)
89
- onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.source} in ${flow !== null && flow !== void 0 ? flow : 'collection'}`);
92
+ onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.source} in ${flow ?? 'collection'}`);
90
93
  found = token;
91
94
  atNewline = false;
92
95
  hasSpace = false;
@@ -122,10 +125,11 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
122
125
  spaceBefore,
123
126
  comment,
124
127
  hasNewline,
128
+ hasNewlineAfterProp,
125
129
  anchor,
126
130
  tag,
127
131
  end,
128
- start: start !== null && start !== void 0 ? start : end
132
+ start: start ?? end
129
133
  };
130
134
  }
131
135
 
@@ -16,7 +16,7 @@ function emptyScalarPosition(offset, before, pos) {
16
16
  // Technically, an empty scalar is immediately after the last non-empty
17
17
  // node, but it's more useful to place it after any whitespace.
18
18
  st = before[++i];
19
- while ((st === null || st === void 0 ? void 0 : st.type) === 'space') {
19
+ while (st?.type === 'space') {
20
20
  offset += st.source.length;
21
21
  st = before[++i];
22
22
  }