yaml 2.0.0-9 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/README.md +1 -1
  2. package/browser/dist/compose/compose-collection.js +1 -1
  3. package/browser/dist/compose/compose-doc.js +4 -3
  4. package/browser/dist/compose/compose-node.js +13 -4
  5. package/browser/dist/compose/compose-scalar.js +18 -13
  6. package/browser/dist/compose/composer.js +8 -10
  7. package/browser/dist/compose/resolve-block-map.js +15 -8
  8. package/browser/dist/compose/resolve-block-scalar.js +12 -3
  9. package/browser/dist/compose/resolve-block-seq.js +5 -0
  10. package/browser/dist/compose/resolve-flow-collection.js +13 -5
  11. package/browser/dist/compose/resolve-flow-scalar.js +1 -1
  12. package/browser/dist/compose/resolve-props.js +9 -1
  13. package/browser/dist/compose/util-empty-scalar-position.js +1 -1
  14. package/browser/dist/compose/util-flow-indent-check.js +15 -0
  15. package/browser/dist/doc/Document.js +53 -20
  16. package/browser/dist/doc/anchors.js +2 -2
  17. package/browser/dist/doc/createNode.js +8 -7
  18. package/browser/dist/doc/directives.js +7 -4
  19. package/browser/dist/index.js +1 -2
  20. package/browser/dist/nodes/Collection.js +2 -1
  21. package/browser/dist/nodes/Pair.js +2 -2
  22. package/browser/dist/nodes/Scalar.js +1 -1
  23. package/browser/dist/nodes/YAMLMap.js +7 -7
  24. package/browser/dist/nodes/YAMLSeq.js +2 -10
  25. package/browser/dist/nodes/addPairToJSMap.js +1 -1
  26. package/browser/dist/nodes/toJS.js +3 -1
  27. package/browser/dist/parse/cst-scalar.js +1 -2
  28. package/browser/dist/parse/cst-visit.js +2 -2
  29. package/browser/dist/parse/lexer.js +20 -8
  30. package/browser/dist/parse/parser.js +142 -68
  31. package/browser/dist/public-api.js +5 -7
  32. package/browser/dist/schema/Schema.js +14 -4
  33. package/browser/dist/schema/common/null.js +3 -1
  34. package/browser/dist/schema/core/float.js +4 -1
  35. package/browser/dist/schema/tags.js +9 -9
  36. package/browser/dist/schema/yaml-1.1/float.js +4 -1
  37. package/browser/dist/schema/yaml-1.1/omap.js +1 -1
  38. package/browser/dist/schema/yaml-1.1/pairs.js +1 -1
  39. package/browser/dist/schema/yaml-1.1/set.js +4 -0
  40. package/browser/dist/stringify/stringify.js +49 -22
  41. package/browser/dist/stringify/stringifyCollection.js +114 -85
  42. package/browser/dist/stringify/stringifyComment.js +18 -12
  43. package/browser/dist/stringify/stringifyDocument.js +36 -15
  44. package/browser/dist/stringify/stringifyPair.js +34 -20
  45. package/browser/dist/stringify/stringifyString.js +8 -11
  46. package/browser/dist/visit.js +149 -62
  47. package/dist/compose/compose-collection.js +1 -1
  48. package/dist/compose/compose-doc.js +4 -3
  49. package/dist/compose/compose-node.d.ts +1 -0
  50. package/dist/compose/compose-node.js +13 -4
  51. package/dist/compose/compose-scalar.js +17 -12
  52. package/dist/compose/composer.d.ts +1 -1
  53. package/dist/compose/composer.js +10 -12
  54. package/dist/compose/resolve-block-map.js +15 -8
  55. package/dist/compose/resolve-block-scalar.js +12 -3
  56. package/dist/compose/resolve-block-seq.js +5 -0
  57. package/dist/compose/resolve-flow-collection.js +13 -5
  58. package/dist/compose/resolve-flow-scalar.js +1 -1
  59. package/dist/compose/resolve-props.d.ts +2 -1
  60. package/dist/compose/resolve-props.js +9 -1
  61. package/dist/compose/util-empty-scalar-position.js +1 -1
  62. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  63. package/dist/compose/util-flow-indent-check.js +17 -0
  64. package/dist/doc/Document.d.ts +17 -14
  65. package/dist/doc/Document.js +58 -25
  66. package/dist/doc/anchors.d.ts +3 -3
  67. package/dist/doc/anchors.js +2 -2
  68. package/dist/doc/createNode.d.ts +1 -1
  69. package/dist/doc/createNode.js +8 -7
  70. package/dist/doc/directives.d.ts +4 -2
  71. package/dist/doc/directives.js +7 -4
  72. package/dist/index.d.ts +4 -2
  73. package/dist/index.js +1 -2
  74. package/dist/nodes/Collection.d.ts +2 -2
  75. package/dist/nodes/Collection.js +2 -1
  76. package/dist/nodes/Node.d.ts +14 -10
  77. package/dist/nodes/Pair.d.ts +1 -1
  78. package/dist/nodes/Pair.js +2 -2
  79. package/dist/nodes/Scalar.js +1 -1
  80. package/dist/nodes/YAMLMap.d.ts +9 -4
  81. package/dist/nodes/YAMLMap.js +7 -7
  82. package/dist/nodes/YAMLSeq.d.ts +4 -1
  83. package/dist/nodes/YAMLSeq.js +2 -10
  84. package/dist/nodes/addPairToJSMap.js +1 -1
  85. package/dist/nodes/toJS.js +3 -1
  86. package/dist/options.d.ts +44 -13
  87. package/dist/parse/cst-scalar.js +1 -2
  88. package/dist/parse/cst-visit.js +2 -2
  89. package/dist/parse/lexer.js +20 -8
  90. package/dist/parse/parser.js +142 -68
  91. package/dist/public-api.js +5 -7
  92. package/dist/schema/Schema.d.ts +5 -4
  93. package/dist/schema/Schema.js +13 -3
  94. package/dist/schema/common/null.js +3 -1
  95. package/dist/schema/core/float.js +4 -1
  96. package/dist/schema/json-schema.d.ts +69 -0
  97. package/dist/schema/tags.d.ts +1 -2
  98. package/dist/schema/tags.js +9 -9
  99. package/dist/schema/yaml-1.1/float.js +4 -1
  100. package/dist/schema/yaml-1.1/omap.d.ts +7 -3
  101. package/dist/schema/yaml-1.1/omap.js +1 -1
  102. package/dist/schema/yaml-1.1/pairs.js +1 -1
  103. package/dist/schema/yaml-1.1/set.d.ts +6 -2
  104. package/dist/schema/yaml-1.1/set.js +4 -0
  105. package/dist/stringify/stringify.d.ts +5 -3
  106. package/dist/stringify/stringify.js +49 -22
  107. package/dist/stringify/stringifyCollection.d.ts +2 -6
  108. package/dist/stringify/stringifyCollection.js +113 -84
  109. package/dist/stringify/stringifyComment.d.ts +10 -2
  110. package/dist/stringify/stringifyComment.js +19 -12
  111. package/dist/stringify/stringifyDocument.js +35 -14
  112. package/dist/stringify/stringifyPair.js +33 -19
  113. package/dist/stringify/stringifyString.js +8 -11
  114. package/dist/test-events.js +14 -14
  115. package/dist/visit.d.ts +49 -10
  116. package/dist/visit.js +148 -60
  117. package/package.json +11 -10
  118. package/browser/dist/options.js +0 -18
  119. package/dist/node_modules/tslib/tslib.es6.js +0 -76
  120. package/dist/options.js +0 -20
@@ -6,21 +6,20 @@ const defaultTagPrefix = 'tag:yaml.org,2002:';
6
6
  function findTagObject(value, tagName, tags) {
7
7
  if (tagName) {
8
8
  const match = tags.filter(t => t.tag === tagName);
9
- const tagObj = match.find(t => !t.format) || match[0];
9
+ const tagObj = match.find(t => !t.format) ?? match[0];
10
10
  if (!tagObj)
11
11
  throw new Error(`Tag ${tagName} not found`);
12
12
  return tagObj;
13
13
  }
14
- return tags.find(t => t.identify && t.identify(value) && !t.format);
14
+ return tags.find(t => t.identify?.(value) && !t.format);
15
15
  }
16
16
  function createNode(value, tagName, ctx) {
17
- var _a, _b;
18
17
  if (isDocument(value))
19
18
  value = value.contents;
20
19
  if (isNode(value))
21
20
  return value;
22
21
  if (isPair(value)) {
23
- const map = (_b = (_a = ctx.schema[MAP]).createNode) === null || _b === void 0 ? void 0 : _b.call(_a, ctx.schema, null, ctx);
22
+ const map = ctx.schema[MAP].createNode?.(ctx.schema, null, ctx);
24
23
  map.items.push(value);
25
24
  return map;
26
25
  }
@@ -48,12 +47,14 @@ function createNode(value, tagName, ctx) {
48
47
  sourceObjects.set(value, ref);
49
48
  }
50
49
  }
51
- if (tagName && tagName.startsWith('!!'))
50
+ if (tagName?.startsWith('!!'))
52
51
  tagName = defaultTagPrefix + tagName.slice(2);
53
52
  let tagObj = findTagObject(value, tagName, schema.tags);
54
53
  if (!tagObj) {
55
- if (value && typeof value.toJSON === 'function')
54
+ if (value && typeof value.toJSON === 'function') {
55
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
56
56
  value = value.toJSON();
57
+ }
57
58
  if (!value || typeof value !== 'object') {
58
59
  const node = new Scalar(value);
59
60
  if (ref)
@@ -71,7 +72,7 @@ function createNode(value, tagName, ctx) {
71
72
  onTagObj(tagObj);
72
73
  delete ctx.onTagObj;
73
74
  }
74
- const node = (tagObj === null || tagObj === void 0 ? void 0 : tagObj.createNode)
75
+ const node = tagObj?.createNode
75
76
  ? tagObj.createNode(ctx.schema, value, ctx)
76
77
  : new Scalar(value);
77
78
  if (tagName)
@@ -16,13 +16,15 @@ class Directives {
16
16
  * The directives-end/doc-start marker `---`. If `null`, a marker may still be
17
17
  * included in the document's stringified representation.
18
18
  */
19
- this.marker = null;
19
+ this.docStart = null;
20
+ /** The doc-end marker `...`. */
21
+ this.docEnd = false;
20
22
  this.yaml = Object.assign({}, Directives.defaultYaml, yaml);
21
23
  this.tags = Object.assign({}, Directives.defaultTags, tags);
22
24
  }
23
25
  clone() {
24
26
  const copy = new Directives(this.yaml, this.tags);
25
- copy.marker = this.marker;
27
+ copy.docStart = this.docStart;
26
28
  return copy;
27
29
  }
28
30
  /**
@@ -71,7 +73,7 @@ class Directives {
71
73
  }
72
74
  case '%YAML': {
73
75
  this.yaml.explicit = true;
74
- if (parts.length < 1) {
76
+ if (parts.length !== 1) {
75
77
  onError(0, '%YAML directive should contain exactly one part');
76
78
  return false;
77
79
  }
@@ -81,7 +83,8 @@ class Directives {
81
83
  return true;
82
84
  }
83
85
  else {
84
- onError(6, `Unsupported YAML version ${version}`, true);
86
+ const isValid = /^\d+\.\d+$/.test(version);
87
+ onError(6, `Unsupported YAML version ${version}`, isValid);
85
88
  return false;
86
89
  }
87
90
  }
@@ -8,11 +8,10 @@ export { Pair } from './nodes/Pair.js';
8
8
  export { Scalar } from './nodes/Scalar.js';
9
9
  export { YAMLMap } from './nodes/YAMLMap.js';
10
10
  export { YAMLSeq } from './nodes/YAMLSeq.js';
11
- export { defaultOptions } from './options.js';
12
11
  import * as cst from './parse/cst.js';
13
12
  export { cst as CST };
14
13
  export { Lexer } from './parse/lexer.js';
15
14
  export { LineCounter } from './parse/line-counter.js';
16
15
  export { Parser } from './parse/parser.js';
17
16
  export { parse, parseAllDocuments, parseDocument, stringify } from './public-api.js';
18
- export { visit } from './visit.js';
17
+ export { visit, visitAsync } from './visit.js';
@@ -24,7 +24,8 @@ function collectionFromPath(schema, path, value) {
24
24
  sourceObjects: new Map()
25
25
  });
26
26
  }
27
- // null, undefined, or an empty non-string iterable (e.g. [])
27
+ // Type guard is intentionally a little wrong so as to be more useful,
28
+ // as it does not cover untypable empty non-string iterables (e.g. []).
28
29
  const isEmptyPath = (path) => path == null ||
29
30
  (typeof path === 'object' && !!path[Symbol.iterator]().next().done);
30
31
  class Collection extends NodeBase {
@@ -23,11 +23,11 @@ class Pair {
23
23
  return new Pair(key, value);
24
24
  }
25
25
  toJSON(_, ctx) {
26
- const pair = ctx && ctx.mapAsMap ? new Map() : {};
26
+ const pair = ctx?.mapAsMap ? new Map() : {};
27
27
  return addPairToJSMap(ctx, pair, this);
28
28
  }
29
29
  toString(ctx, onComment, onChompKeep) {
30
- return ctx && ctx.doc
30
+ return ctx?.doc
31
31
  ? stringifyPair(this, ctx, onComment, onChompKeep)
32
32
  : JSON.stringify(this);
33
33
  }
@@ -8,7 +8,7 @@ class Scalar extends NodeBase {
8
8
  this.value = value;
9
9
  }
10
10
  toJSON(arg, ctx) {
11
- return ctx && ctx.keep ? this.value : toJS(this.value, arg, ctx);
11
+ return ctx?.keep ? this.value : toJS(this.value, arg, ctx);
12
12
  }
13
13
  toString() {
14
14
  return String(this.value);
@@ -37,12 +37,12 @@ class YAMLMap extends Collection {
37
37
  _pair = pair;
38
38
  else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
39
39
  // In TypeScript, this never happens.
40
- _pair = new Pair(pair, pair.value);
40
+ _pair = new Pair(pair, pair?.value);
41
41
  }
42
42
  else
43
43
  _pair = new Pair(pair.key, pair.value);
44
44
  const prev = findPair(this.items, _pair.key);
45
- const sortEntries = this.schema && this.schema.sortMapEntries;
45
+ const sortEntries = this.schema?.sortMapEntries;
46
46
  if (prev) {
47
47
  if (!overwrite)
48
48
  throw new Error(`Key ${_pair.key} already set`);
@@ -72,8 +72,8 @@ class YAMLMap extends Collection {
72
72
  }
73
73
  get(key, keepScalar) {
74
74
  const it = findPair(this.items, key);
75
- const node = it && it.value;
76
- return !keepScalar && isScalar(node) ? node.value : node;
75
+ const node = it?.value;
76
+ return (!keepScalar && isScalar(node) ? node.value : node) ?? undefined;
77
77
  }
78
78
  has(key) {
79
79
  return !!findPair(this.items, key);
@@ -87,8 +87,8 @@ class YAMLMap extends Collection {
87
87
  * @returns Instance of Type, Map, or Object
88
88
  */
89
89
  toJSON(_, ctx, Type) {
90
- const map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {};
91
- if (ctx && ctx.onCreate)
90
+ const map = Type ? new Type() : ctx?.mapAsMap ? new Map() : {};
91
+ if (ctx?.onCreate)
92
92
  ctx.onCreate(map);
93
93
  for (const item of this.items)
94
94
  addPairToJSMap(ctx, map, item);
@@ -104,7 +104,7 @@ class YAMLMap extends Collection {
104
104
  if (!ctx.allNullValues && this.hasAllNullValues(false))
105
105
  ctx = Object.assign({}, ctx, { allNullValues: true });
106
106
  return stringifyCollection(this, ctx, {
107
- blockItem: n => n.str,
107
+ blockItemPrefix: '',
108
108
  flowChars: { start: '{', end: '}' },
109
109
  itemIndent: ctx.indent || '',
110
110
  onChompKeep,
@@ -30,14 +30,6 @@ class YAMLSeq extends Collection {
30
30
  const del = this.items.splice(idx, 1);
31
31
  return del.length > 0;
32
32
  }
33
- /**
34
- * Returns item at `key`, or `undefined` if not found. By default unwraps
35
- * scalar values from their surrounding node; to disable set `keepScalar` to
36
- * `true` (collections are always returned intact).
37
- *
38
- * `key` must contain a representation of an integer for this to succeed.
39
- * It may be wrapped in a `Scalar`.
40
- */
41
33
  get(key, keepScalar) {
42
34
  const idx = asItemIndex(key);
43
35
  if (typeof idx !== 'number')
@@ -74,7 +66,7 @@ class YAMLSeq extends Collection {
74
66
  }
75
67
  toJSON(_, ctx) {
76
68
  const seq = [];
77
- if (ctx && ctx.onCreate)
69
+ if (ctx?.onCreate)
78
70
  ctx.onCreate(seq);
79
71
  let i = 0;
80
72
  for (const item of this.items)
@@ -85,7 +77,7 @@ class YAMLSeq extends Collection {
85
77
  if (!ctx)
86
78
  return JSON.stringify(this);
87
79
  return stringifyCollection(this, ctx, {
88
- blockItem: n => (n.comment ? n.str : `- ${n.str}`),
80
+ blockItemPrefix: '- ',
89
81
  flowChars: { start: '[', end: ']' },
90
82
  itemIndent: (ctx.indent || '') + ' ',
91
83
  onChompKeep,
@@ -6,7 +6,7 @@ import { toJS } from './toJS.js';
6
6
 
7
7
  const MERGE_KEY = '<<';
8
8
  function addPairToJSMap(ctx, map, { key, value }) {
9
- if (ctx && ctx.doc.schema.merge && isMergeKey(key)) {
9
+ if (ctx?.doc.schema.merge && isMergeKey(key)) {
10
10
  value = isAlias(value) ? value.resolve(ctx.doc) : value;
11
11
  if (isSeq(value))
12
12
  for (const it of value.items)
@@ -11,9 +11,11 @@ import { hasAnchor } from './Node.js';
11
11
  * stringification.
12
12
  */
13
13
  function toJS(value, arg, ctx) {
14
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
14
15
  if (Array.isArray(value))
15
16
  return value.map((v, i) => toJS(v, String(i), ctx));
16
17
  if (value && typeof value.toJSON === 'function') {
18
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
17
19
  if (!ctx || !hasAnchor(value))
18
20
  return value.toJSON(arg, ctx);
19
21
  const data = { aliasCount: 0, count: 1, res: undefined };
@@ -27,7 +29,7 @@ function toJS(value, arg, ctx) {
27
29
  ctx.onCreate(res);
28
30
  return res;
29
31
  }
30
- if (typeof value === 'bigint' && !(ctx && ctx.keep))
32
+ if (typeof value === 'bigint' && !ctx?.keep)
31
33
  return Number(value);
32
34
  return value;
33
35
  }
@@ -38,7 +38,6 @@ function resolveAsScalar(token, strict = true, onError) {
38
38
  * @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
39
39
  */
40
40
  function createScalarToken(value, context) {
41
- var _a;
42
41
  const { implicitKey = false, indent, inFlow = false, offset = -1, type = 'PLAIN' } = context;
43
42
  const source = stringifyString({ type, value }, {
44
43
  implicitKey,
@@ -46,7 +45,7 @@ function createScalarToken(value, context) {
46
45
  inFlow,
47
46
  options: { blockQuote: true, lineWidth: -1 }
48
47
  });
49
- const end = (_a = context.end) !== null && _a !== void 0 ? _a : [
48
+ const end = context.end ?? [
50
49
  { type: 'newline', offset: -1, indent, source: '\n' }
51
50
  ];
52
51
  switch (source[0]) {
@@ -47,7 +47,7 @@ visit.REMOVE = REMOVE;
47
47
  visit.itemAtPath = (cst, path) => {
48
48
  let item = cst;
49
49
  for (const [field, index] of path) {
50
- const tok = item && item[field];
50
+ const tok = item?.[field];
51
51
  if (tok && 'items' in tok) {
52
52
  item = tok.items[index];
53
53
  }
@@ -64,7 +64,7 @@ visit.itemAtPath = (cst, path) => {
64
64
  visit.parentCollection = (cst, path) => {
65
65
  const parent = visit.itemAtPath(cst, path.slice(0, -1));
66
66
  const field = path[path.length - 1][0];
67
- const coll = parent && parent[field];
67
+ const coll = parent?.[field];
68
68
  if (coll && 'items' in coll)
69
69
  return coll;
70
70
  throw new Error('Parent collection not found');
@@ -153,7 +153,7 @@ class Lexer {
153
153
  this.lineEndPos = null;
154
154
  }
155
155
  this.atEnd = !incomplete;
156
- let next = this.next || 'stream';
156
+ let next = this.next ?? 'stream';
157
157
  while (next && (incomplete || this.hasChars(1)))
158
158
  next = yield* this.parseNext(next);
159
159
  }
@@ -357,9 +357,14 @@ class Lexer {
357
357
  let indent = -1;
358
358
  do {
359
359
  nl = yield* this.pushNewline();
360
- sp = yield* this.pushSpaces(true);
361
- if (nl > 0)
360
+ if (nl > 0) {
361
+ sp = yield* this.pushSpaces(false);
362
362
  this.indentValue = indent = sp;
363
+ }
364
+ else {
365
+ sp = 0;
366
+ }
367
+ sp += yield* this.pushSpaces(true);
363
368
  } while (nl + sp > 0);
364
369
  const line = this.getLine();
365
370
  if (line === null)
@@ -416,6 +421,7 @@ class Lexer {
416
421
  case ':': {
417
422
  const next = this.charAt(1);
418
423
  if (this.flowKey || isEmpty(next) || next === ',') {
424
+ this.flowKey = false;
419
425
  yield* this.pushCount(1);
420
426
  yield* this.pushSpaces(true);
421
427
  return 'flow';
@@ -532,9 +538,10 @@ class Lexer {
532
538
  let ch = this.buffer[i];
533
539
  if (ch === '\r')
534
540
  ch = this.buffer[--i];
541
+ const lastChar = i; // Drop the line if last char not more indented
535
542
  while (ch === ' ' || ch === '\t')
536
543
  ch = this.buffer[--i];
537
- if (ch === '\n' && i >= this.pos)
544
+ if (ch === '\n' && i >= this.pos && i + 1 + indent > lastChar)
538
545
  nl = i;
539
546
  else
540
547
  break;
@@ -617,16 +624,21 @@ class Lexer {
617
624
  return ((yield* this.pushUntil(isNotAnchorChar)) +
618
625
  (yield* this.pushSpaces(true)) +
619
626
  (yield* this.pushIndicators()));
620
- case ':':
621
- case '?': // this is an error outside flow collections
622
627
  case '-': // this is an error
623
- if (isEmpty(this.charAt(1))) {
624
- if (this.flowLevel === 0)
628
+ case '?': // this is an error outside flow collections
629
+ case ':': {
630
+ const inFlow = this.flowLevel > 0;
631
+ const ch1 = this.charAt(1);
632
+ if (isEmpty(ch1) || (inFlow && invalidFlowScalarChars.includes(ch1))) {
633
+ if (!inFlow)
625
634
  this.indentNext = this.indentValue + 1;
635
+ else if (this.flowKey)
636
+ this.flowKey = false;
626
637
  return ((yield* this.pushCount(1)) +
627
638
  (yield* this.pushSpaces(true)) +
628
639
  (yield* this.pushIndicators()));
629
640
  }
641
+ }
630
642
  }
631
643
  return 0;
632
644
  }