yaml 2.0.0-7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/README.md +1 -1
  2. package/browser/dist/compose/compose-doc.js +3 -2
  3. package/browser/dist/compose/compose-node.js +14 -3
  4. package/browser/dist/compose/compose-scalar.js +17 -10
  5. package/browser/dist/compose/composer.js +6 -7
  6. package/browser/dist/compose/resolve-block-map.js +19 -5
  7. package/browser/dist/compose/resolve-block-scalar.js +12 -3
  8. package/browser/dist/compose/resolve-block-seq.js +5 -0
  9. package/browser/dist/compose/resolve-flow-collection.js +18 -6
  10. package/browser/dist/compose/resolve-flow-scalar.js +9 -2
  11. package/browser/dist/compose/resolve-props.js +4 -0
  12. package/browser/dist/compose/util-flow-indent-check.js +15 -0
  13. package/browser/dist/doc/Document.js +73 -16
  14. package/browser/dist/doc/anchors.js +2 -2
  15. package/browser/dist/doc/createNode.js +12 -7
  16. package/browser/dist/doc/directives.js +11 -3
  17. package/browser/dist/index.js +1 -2
  18. package/browser/dist/node_modules/tslib/tslib.es6.js +164 -0
  19. package/browser/dist/nodes/Collection.js +28 -15
  20. package/browser/dist/nodes/Node.js +7 -0
  21. package/browser/dist/nodes/Pair.js +11 -3
  22. package/browser/dist/nodes/Scalar.js +1 -1
  23. package/browser/dist/nodes/YAMLMap.js +6 -5
  24. package/browser/dist/nodes/YAMLSeq.js +2 -2
  25. package/browser/dist/nodes/addPairToJSMap.js +3 -2
  26. package/browser/dist/nodes/toJS.js +3 -1
  27. package/browser/dist/parse/cst-scalar.js +2 -6
  28. package/browser/dist/parse/cst-visit.js +2 -2
  29. package/browser/dist/parse/lexer.js +50 -22
  30. package/browser/dist/parse/parser.js +121 -45
  31. package/browser/dist/public-api.js +4 -5
  32. package/browser/dist/schema/Schema.js +19 -4
  33. package/browser/dist/schema/common/null.js +3 -1
  34. package/browser/dist/schema/core/float.js +4 -1
  35. package/browser/dist/schema/tags.js +17 -13
  36. package/browser/dist/schema/yaml-1.1/float.js +4 -1
  37. package/browser/dist/schema/yaml-1.1/omap.js +1 -1
  38. package/browser/dist/schema/yaml-1.1/pairs.js +2 -1
  39. package/browser/dist/stringify/stringify.js +53 -23
  40. package/browser/dist/stringify/stringifyCollection.js +115 -85
  41. package/browser/dist/stringify/stringifyComment.js +18 -12
  42. package/browser/dist/stringify/stringifyDocument.js +37 -15
  43. package/browser/dist/stringify/stringifyPair.js +35 -21
  44. package/browser/dist/stringify/stringifyString.js +41 -43
  45. package/browser/dist/util.js +3 -0
  46. package/browser/dist/visit.js +150 -62
  47. package/dist/compose/compose-doc.js +3 -2
  48. package/dist/compose/compose-node.d.ts +1 -0
  49. package/dist/compose/compose-node.js +14 -3
  50. package/dist/compose/compose-scalar.js +16 -9
  51. package/dist/compose/composer.d.ts +1 -1
  52. package/dist/compose/composer.js +8 -9
  53. package/dist/compose/resolve-block-map.js +19 -5
  54. package/dist/compose/resolve-block-scalar.js +12 -3
  55. package/dist/compose/resolve-block-seq.js +5 -0
  56. package/dist/compose/resolve-flow-collection.js +18 -6
  57. package/dist/compose/resolve-flow-scalar.js +9 -2
  58. package/dist/compose/resolve-props.d.ts +1 -1
  59. package/dist/compose/resolve-props.js +4 -0
  60. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  61. package/dist/compose/util-flow-indent-check.js +17 -0
  62. package/dist/doc/Document.d.ts +13 -4
  63. package/dist/doc/Document.js +79 -22
  64. package/dist/doc/anchors.d.ts +2 -2
  65. package/dist/doc/anchors.js +2 -2
  66. package/dist/doc/createNode.d.ts +3 -2
  67. package/dist/doc/createNode.js +11 -6
  68. package/dist/doc/directives.d.ts +5 -2
  69. package/dist/doc/directives.js +11 -3
  70. package/dist/index.d.ts +4 -4
  71. package/dist/index.js +1 -2
  72. package/dist/node_modules/tslib/tslib.es6.js +76 -0
  73. package/dist/nodes/Alias.d.ts +7 -3
  74. package/dist/nodes/Collection.d.ts +10 -4
  75. package/dist/nodes/Collection.js +27 -14
  76. package/dist/nodes/Node.d.ts +6 -1
  77. package/dist/nodes/Node.js +7 -0
  78. package/dist/nodes/Pair.d.ts +7 -2
  79. package/dist/nodes/Pair.js +10 -2
  80. package/dist/nodes/Scalar.d.ts +2 -0
  81. package/dist/nodes/Scalar.js +1 -1
  82. package/dist/nodes/YAMLMap.d.ts +5 -1
  83. package/dist/nodes/YAMLMap.js +6 -5
  84. package/dist/nodes/YAMLSeq.d.ts +2 -0
  85. package/dist/nodes/YAMLSeq.js +2 -2
  86. package/dist/nodes/addPairToJSMap.js +2 -1
  87. package/dist/nodes/toJS.js +3 -1
  88. package/dist/options.d.ts +67 -15
  89. package/dist/parse/cst-scalar.d.ts +6 -0
  90. package/dist/parse/cst-scalar.js +2 -6
  91. package/dist/parse/cst-visit.js +2 -2
  92. package/dist/parse/lexer.d.ts +2 -0
  93. package/dist/parse/lexer.js +50 -22
  94. package/dist/parse/parser.js +121 -45
  95. package/dist/public-api.js +4 -5
  96. package/dist/schema/Schema.d.ts +6 -4
  97. package/dist/schema/Schema.js +18 -3
  98. package/dist/schema/common/null.js +3 -1
  99. package/dist/schema/core/float.js +4 -1
  100. package/dist/schema/tags.d.ts +1 -2
  101. package/dist/schema/tags.js +17 -13
  102. package/dist/schema/yaml-1.1/float.js +4 -1
  103. package/dist/schema/yaml-1.1/omap.js +1 -1
  104. package/dist/schema/yaml-1.1/pairs.js +2 -1
  105. package/dist/stringify/stringify.d.ts +5 -3
  106. package/dist/stringify/stringify.js +53 -23
  107. package/dist/stringify/stringifyCollection.d.ts +2 -6
  108. package/dist/stringify/stringifyCollection.js +114 -84
  109. package/dist/stringify/stringifyComment.d.ts +10 -2
  110. package/dist/stringify/stringifyComment.js +19 -12
  111. package/dist/stringify/stringifyDocument.js +36 -14
  112. package/dist/stringify/stringifyPair.js +34 -20
  113. package/dist/stringify/stringifyString.js +41 -43
  114. package/dist/test-events.d.ts +1 -1
  115. package/dist/test-events.js +13 -12
  116. package/dist/util.d.ts +3 -0
  117. package/dist/util.js +6 -0
  118. package/dist/visit.d.ts +49 -10
  119. package/dist/visit.js +149 -60
  120. package/package.json +12 -10
  121. package/util.d.ts +3 -0
  122. package/browser/dist/options.js +0 -17
  123. package/dist/options.js +0 -19
@@ -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
@@ -49,10 +53,12 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
49
53
  const keyNode = key
50
54
  ? composeNode(ctx, key, keyProps, onError)
51
55
  : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
56
+ if (ctx.schema.compat)
57
+ utilFlowIndentCheck.flowIndentCheck(bm.indent, key, onError);
52
58
  if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
53
59
  onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
54
60
  // value properties
55
- const valueProps = resolveProps.resolveProps(sep || [], {
61
+ const valueProps = resolveProps.resolveProps(sep !== null && sep !== void 0 ? sep : [], {
56
62
  indicator: 'map-value-ind',
57
63
  next: value,
58
64
  offset: keyNode.range[2],
@@ -72,8 +78,13 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
72
78
  const valueNode = value
73
79
  ? composeNode(ctx, value, valueProps, onError)
74
80
  : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
81
+ if (ctx.schema.compat)
82
+ utilFlowIndentCheck.flowIndentCheck(bm.indent, value, onError);
75
83
  offset = valueNode.range[2];
76
- map.items.push(new Pair.Pair(keyNode, valueNode));
84
+ const pair = new Pair.Pair(keyNode, valueNode);
85
+ if (ctx.options.keepSourceTokens)
86
+ pair.srcToken = collItem;
87
+ map.items.push(pair);
77
88
  }
78
89
  else {
79
90
  // key with no value
@@ -85,7 +96,10 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
85
96
  else
86
97
  keyNode.comment = valueProps.comment;
87
98
  }
88
- map.items.push(new Pair.Pair(keyNode));
99
+ const pair = new Pair.Pair(keyNode);
100
+ if (ctx.options.keepSourceTokens)
101
+ pair.srcToken = collItem;
102
+ map.items.push(pair);
89
103
  }
90
104
  }
91
105
  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
  }
@@ -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 = '';
@@ -137,6 +138,12 @@ function doubleQuotedValue(source, onError) {
137
138
  while (next === ' ' || next === '\t')
138
139
  next = source[++i + 1];
139
140
  }
141
+ else if (next === '\r' && source[i + 1] === '\n') {
142
+ // skip escaped CRLF newlines, but still trim the following line
143
+ next = source[++i + 1];
144
+ while (next === ' ' || next === '\t')
145
+ next = source[++i + 1];
146
+ }
140
147
  else if (next === 'x' || next === 'u' || next === 'U') {
141
148
  const length = { x: 2, u: 4, U: 8 }[next];
142
149
  res += parseCharCode(source, i + 1, length, onError);
@@ -154,7 +161,7 @@ function doubleQuotedValue(source, onError) {
154
161
  let next = source[i + 1];
155
162
  while (next === ' ' || next === '\t')
156
163
  next = source[++i + 1];
157
- if (next !== '\n')
164
+ if (next !== '\n' && !(next === '\r' && source[i + 2] === '\n'))
158
165
  res += i > wsStart ? source.slice(wsStart, i + 1) : ch;
159
166
  }
160
167
  else {
@@ -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;
@@ -61,6 +61,8 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
61
61
  case 'anchor':
62
62
  if (anchor)
63
63
  onError(token, 'MULTIPLE_ANCHORS', 'A node can have at most one anchor');
64
+ if (token.source.endsWith(':'))
65
+ onError(token.offset + token.source.length - 1, 'BAD_ALIAS', 'Anchor ending in : is ambiguous', true);
64
66
  anchor = token;
65
67
  if (start === null)
66
68
  start = token.offset;
@@ -83,6 +85,8 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
83
85
  // Could here handle preceding comments differently
84
86
  if (anchor || tag)
85
87
  onError(token, 'BAD_PROP_ORDER', `Anchors and tags must be after the ${token.source} indicator`);
88
+ if (found)
89
+ onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.source} in ${flow !== null && flow !== void 0 ? flow : 'collection'}`);
86
90
  found = token;
87
91
  atNewline = false;
88
92
  hasSpace = false;
@@ -0,0 +1,3 @@
1
+ import { Token } from '../parse/cst';
2
+ import { ComposeErrorHandler } from './composer';
3
+ export declare function flowIndentCheck(indent: number, fc: Token | null | undefined, onError: ComposeErrorHandler): void;
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ var utilContainsNewline = require('./util-contains-newline.js');
4
+
5
+ function flowIndentCheck(indent, fc, onError) {
6
+ if ((fc === null || fc === void 0 ? void 0 : fc.type) === 'flow-collection') {
7
+ const end = fc.end[0];
8
+ if (end.indent === indent &&
9
+ (end.source === ']' || end.source === '}') &&
10
+ utilContainsNewline.containsNewline(fc)) {
11
+ const msg = 'Flow end indicator should be more indented than parent';
12
+ onError(end, 'BAD_INDENT', msg, true);
13
+ }
14
+ }
15
+ }
16
+
17
+ exports.flowIndentCheck = flowIndentCheck;
@@ -5,12 +5,13 @@ import { Pair } from '../nodes/Pair.js';
5
5
  import type { Scalar } from '../nodes/Scalar.js';
6
6
  import type { YAMLMap } from '../nodes/YAMLMap.js';
7
7
  import type { YAMLSeq } from '../nodes/YAMLSeq.js';
8
- import { CreateNodeOptions, DocumentOptions, ParseOptions, SchemaOptions, ToJSOptions, ToStringOptions } from '../options.js';
8
+ import type { CreateNodeOptions, DocumentOptions, ParseOptions, SchemaOptions, ToJSOptions, ToStringOptions } from '../options.js';
9
9
  import { Schema } from '../schema/Schema.js';
10
10
  import { Directives } from './directives.js';
11
11
  export declare type Replacer = any[] | ((key: any, value: any) => unknown);
12
12
  export declare namespace Document {
13
13
  interface Parsed<T extends ParsedNode = ParsedNode> extends Document<T> {
14
+ directives: Directives;
14
15
  range: Range;
15
16
  }
16
17
  }
@@ -22,7 +23,7 @@ export declare class Document<T = unknown> {
22
23
  comment: string | null;
23
24
  /** The document contents. */
24
25
  contents: T | null;
25
- directives: Directives;
26
+ directives?: Directives;
26
27
  /** Errors encountered during parsing. */
27
28
  errors: YAMLError[];
28
29
  options: Required<Omit<ParseOptions & DocumentOptions, 'lineCounter' | 'directives' | 'version'>>;
@@ -43,6 +44,12 @@ export declare class Document<T = unknown> {
43
44
  */
44
45
  constructor(value?: any, options?: DocumentOptions & SchemaOptions & ParseOptions & CreateNodeOptions);
45
46
  constructor(value: any, replacer: null | Replacer, options?: DocumentOptions & SchemaOptions & ParseOptions & CreateNodeOptions);
47
+ /**
48
+ * Create a deep copy of this Document and its contents.
49
+ *
50
+ * Custom Node values that inherit from `Object` still refer to their original instances.
51
+ */
52
+ clone(): Document<T>;
46
53
  /** Adds a value to the document. */
47
54
  add(value: any): void;
48
55
  /** Adds a value to the document. */
@@ -110,10 +117,12 @@ export declare class Document<T = unknown> {
110
117
  setIn(path: Iterable<unknown>, value: unknown): void;
111
118
  /**
112
119
  * Change the YAML version and schema used by the document.
120
+ * A `null` version disables support for directives, explicit tags, anchors, and aliases.
121
+ * It also requires the `schema` option to be given as a `Schema` instance value.
113
122
  *
114
- * Overrides all previously set schema options
123
+ * Overrides all previously set schema options.
115
124
  */
116
- setSchema(version: '1.1' | '1.2', options?: SchemaOptions): void;
125
+ setSchema(version: '1.1' | '1.2' | 'next' | null, options?: SchemaOptions): void;
117
126
  /** A plain JavaScript representation of the document `contents`. */
118
127
  toJS(opt?: ToJSOptions & {
119
128
  [ignored: string]: unknown;
@@ -5,7 +5,6 @@ var Collection = require('../nodes/Collection.js');
5
5
  var Node = require('../nodes/Node.js');
6
6
  var Pair = require('../nodes/Pair.js');
7
7
  var toJS = require('../nodes/toJS.js');
8
- var options = require('../options.js');
9
8
  var Schema = require('../schema/Schema.js');
10
9
  var stringify = require('../stringify/stringify.js');
11
10
  var stringifyDocument = require('../stringify/stringifyDocument.js');
@@ -15,7 +14,7 @@ var createNode = require('./createNode.js');
15
14
  var directives = require('./directives.js');
16
15
 
17
16
  class Document {
18
- constructor(value, replacer, options$1) {
17
+ constructor(value, replacer, options) {
19
18
  /** A comment before this Document */
20
19
  this.commentBefore = null;
21
20
  /** A comment immediately after this Document */
@@ -29,27 +28,59 @@ class Document {
29
28
  if (typeof replacer === 'function' || Array.isArray(replacer)) {
30
29
  _replacer = replacer;
31
30
  }
32
- else if (options$1 === undefined && replacer) {
33
- options$1 = replacer;
31
+ else if (options === undefined && replacer) {
32
+ options = replacer;
34
33
  replacer = undefined;
35
34
  }
36
- const opt = Object.assign({}, options.defaultOptions, options$1);
35
+ const opt = Object.assign({
36
+ intAsBigInt: false,
37
+ keepSourceTokens: false,
38
+ logLevel: 'warn',
39
+ prettyErrors: true,
40
+ strict: true,
41
+ uniqueKeys: true,
42
+ version: '1.2'
43
+ }, options);
37
44
  this.options = opt;
38
45
  let { version } = opt;
39
- if (options$1 === null || options$1 === void 0 ? void 0 : options$1.directives) {
40
- this.directives = options$1.directives.atDocument();
46
+ if (options === null || options === void 0 ? void 0 : options.directives) {
47
+ this.directives = options.directives.atDocument();
41
48
  if (this.directives.yaml.explicit)
42
49
  version = this.directives.yaml.version;
43
50
  }
44
51
  else
45
52
  this.directives = new directives.Directives({ version });
46
- this.setSchema(version, options$1);
53
+ this.setSchema(version, options);
47
54
  if (value === undefined)
48
55
  this.contents = null;
49
56
  else {
50
- this.contents = this.createNode(value, _replacer, options$1);
57
+ this.contents = this.createNode(value, _replacer, options);
51
58
  }
52
59
  }
60
+ /**
61
+ * Create a deep copy of this Document and its contents.
62
+ *
63
+ * Custom Node values that inherit from `Object` still refer to their original instances.
64
+ */
65
+ clone() {
66
+ const copy = Object.create(Document.prototype, {
67
+ [Node.NODE_TYPE]: { value: Node.DOC }
68
+ });
69
+ copy.commentBefore = this.commentBefore;
70
+ copy.comment = this.comment;
71
+ copy.errors = this.errors.slice();
72
+ copy.warnings = this.warnings.slice();
73
+ copy.options = Object.assign({}, this.options);
74
+ if (this.directives)
75
+ copy.directives = this.directives.clone();
76
+ copy.schema = this.schema.clone();
77
+ copy.contents = Node.isNode(this.contents)
78
+ ? this.contents.clone(copy.schema)
79
+ : this.contents;
80
+ if (this.range)
81
+ copy.range = this.range.slice();
82
+ return copy;
83
+ }
53
84
  /** Adds a value to the document. */
54
85
  add(value) {
55
86
  if (assertCollection(this.contents))
@@ -73,6 +104,7 @@ class Document {
73
104
  if (!node.anchor) {
74
105
  const prev = anchors.anchorNames(this);
75
106
  node.anchor =
107
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
76
108
  !name || prev.has(name) ? anchors.findNewAnchor(name || 'a', prev) : name;
77
109
  }
78
110
  return new Alias.Alias(node.anchor);
@@ -94,9 +126,12 @@ class Document {
94
126
  options = replacer;
95
127
  replacer = undefined;
96
128
  }
97
- const { anchorPrefix, flow, keepUndefined, onTagObj, tag } = options || {};
98
- const { onAnchor, setAnchors, sourceObjects } = anchors.createNodeAnchors(this, anchorPrefix || 'a');
129
+ const { aliasDuplicateObjects, anchorPrefix, flow, keepUndefined, onTagObj, tag } = options !== null && options !== void 0 ? options : {};
130
+ const { onAnchor, setAnchors, sourceObjects } = anchors.createNodeAnchors(this,
131
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
132
+ anchorPrefix || 'a');
99
133
  const ctx = {
134
+ aliasDuplicateObjects: aliasDuplicateObjects !== null && aliasDuplicateObjects !== void 0 ? aliasDuplicateObjects : true,
100
135
  keepUndefined: keepUndefined !== null && keepUndefined !== void 0 ? keepUndefined : false,
101
136
  onAnchor,
102
137
  onTagObj,
@@ -207,26 +242,48 @@ class Document {
207
242
  }
208
243
  /**
209
244
  * Change the YAML version and schema used by the document.
245
+ * A `null` version disables support for directives, explicit tags, anchors, and aliases.
246
+ * It also requires the `schema` option to be given as a `Schema` instance value.
210
247
  *
211
- * Overrides all previously set schema options
248
+ * Overrides all previously set schema options.
212
249
  */
213
- setSchema(version, options) {
214
- let _options;
215
- switch (String(version)) {
250
+ setSchema(version, options = {}) {
251
+ if (typeof version === 'number')
252
+ version = String(version);
253
+ let opt;
254
+ switch (version) {
216
255
  case '1.1':
217
- this.directives.yaml.version = '1.1';
218
- _options = Object.assign({ merge: true, resolveKnownTags: false, schema: 'yaml-1.1' }, options);
256
+ if (this.directives)
257
+ this.directives.yaml.version = '1.1';
258
+ else
259
+ this.directives = new directives.Directives({ version: '1.1' });
260
+ opt = { merge: true, resolveKnownTags: false, schema: 'yaml-1.1' };
219
261
  break;
220
262
  case '1.2':
221
- this.directives.yaml.version = '1.2';
222
- _options = Object.assign({ merge: false, resolveKnownTags: true, schema: 'core' }, options);
263
+ case 'next':
264
+ if (this.directives)
265
+ this.directives.yaml.version = version;
266
+ else
267
+ this.directives = new directives.Directives({ version });
268
+ opt = { merge: false, resolveKnownTags: true, schema: 'core' };
269
+ break;
270
+ case null:
271
+ if (this.directives)
272
+ delete this.directives;
273
+ opt = null;
223
274
  break;
224
275
  default: {
225
276
  const sv = JSON.stringify(version);
226
- throw new Error(`Expected '1.1' or '1.2' as version, but found: ${sv}`);
277
+ throw new Error(`Expected '1.1', '1.2' or null as first argument, but found: ${sv}`);
227
278
  }
228
279
  }
229
- this.schema = new Schema.Schema(_options);
280
+ // Not using `instanceof Schema` to allow for duck typing
281
+ if (options.schema instanceof Object)
282
+ this.schema = options.schema;
283
+ else if (opt)
284
+ this.schema = new Schema.Schema(Object.assign(opt, options));
285
+ else
286
+ throw new Error(`With a null YAML version, the { schema: Schema } option is required`);
230
287
  }
231
288
  // json & jsonArg are only used from toJSON()
232
289
  toJS({ json, jsonArg, mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
@@ -239,7 +296,7 @@ class Document {
239
296
  maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100,
240
297
  stringify: stringify.stringify
241
298
  };
242
- const res = toJS.toJS(this.contents, jsonArg || '', ctx);
299
+ const res = toJS.toJS(this.contents, jsonArg !== null && jsonArg !== void 0 ? jsonArg : '', ctx);
243
300
  if (typeof onAnchor === 'function')
244
301
  for (const { count, res } of ctx.anchors.values())
245
302
  onAnchor(res, count);
@@ -10,13 +10,13 @@ export declare function anchorNames(root: Document | Node): Set<string>;
10
10
  /** Find a new anchor name with the given `prefix` and a one-indexed suffix. */
11
11
  export declare function findNewAnchor(prefix: string, exclude: Set<string>): string;
12
12
  export declare function createNodeAnchors(doc: Document, prefix: string): {
13
- onAnchor(source: unknown): string;
13
+ onAnchor: (source: unknown) => string;
14
14
  /**
15
15
  * With circular references, the source node is only resolved after all
16
16
  * of its child nodes are. This is why anchors are set only after all of
17
17
  * the nodes have been created.
18
18
  */
19
- setAnchors(): void;
19
+ setAnchors: () => void;
20
20
  sourceObjects: Map<unknown, {
21
21
  anchor: string | null;
22
22
  node: Node | null;
@@ -39,7 +39,7 @@ function createNodeAnchors(doc, prefix) {
39
39
  const sourceObjects = new Map();
40
40
  let prevAnchors = null;
41
41
  return {
42
- onAnchor(source) {
42
+ onAnchor: (source) => {
43
43
  aliasObjects.push(source);
44
44
  if (!prevAnchors)
45
45
  prevAnchors = anchorNames(doc);
@@ -52,7 +52,7 @@ function createNodeAnchors(doc, prefix) {
52
52
  * of its child nodes are. This is why anchors are set only after all of
53
53
  * the nodes have been created.
54
54
  */
55
- setAnchors() {
55
+ setAnchors: () => {
56
56
  for (const source of aliasObjects) {
57
57
  const ref = sourceObjects.get(source);
58
58
  if (typeof ref === 'object' &&
@@ -3,8 +3,9 @@ import type { Schema } from '../schema/Schema.js';
3
3
  import type { CollectionTag, ScalarTag } from '../schema/types.js';
4
4
  import type { Replacer } from './Document.js';
5
5
  export interface CreateNodeContext {
6
- keepUndefined?: boolean;
7
- onAnchor(source: unknown): string;
6
+ aliasDuplicateObjects: boolean;
7
+ keepUndefined: boolean;
8
+ onAnchor: (source: unknown) => string;
8
9
  onTagObj?: (tagObj: ScalarTag | CollectionTag) => void;
9
10
  sourceObjects: Map<unknown, {
10
11
  anchor: string | null;
@@ -6,17 +6,20 @@ var Scalar = require('../nodes/Scalar.js');
6
6
 
7
7
  const defaultTagPrefix = 'tag:yaml.org,2002:';
8
8
  function findTagObject(value, tagName, tags) {
9
+ var _a;
9
10
  if (tagName) {
10
11
  const match = tags.filter(t => t.tag === tagName);
11
- const tagObj = match.find(t => !t.format) || match[0];
12
+ const tagObj = (_a = match.find(t => !t.format)) !== null && _a !== void 0 ? _a : match[0];
12
13
  if (!tagObj)
13
14
  throw new Error(`Tag ${tagName} not found`);
14
15
  return tagObj;
15
16
  }
16
- return tags.find(t => t.identify && t.identify(value) && !t.format);
17
+ return tags.find(t => { var _a; return ((_a = t.identify) === null || _a === void 0 ? void 0 : _a.call(t, value)) && !t.format; });
17
18
  }
18
19
  function createNode(value, tagName, ctx) {
19
20
  var _a, _b;
21
+ if (Node.isDocument(value))
22
+ value = value.contents;
20
23
  if (Node.isNode(value))
21
24
  return value;
22
25
  if (Node.isPair(value)) {
@@ -32,11 +35,11 @@ function createNode(value, tagName, ctx) {
32
35
  // https://tc39.es/ecma262/#sec-serializejsonproperty
33
36
  value = value.valueOf();
34
37
  }
35
- const { onAnchor, onTagObj, schema, sourceObjects } = ctx;
38
+ const { aliasDuplicateObjects, onAnchor, onTagObj, schema, sourceObjects } = ctx;
36
39
  // Detect duplicate references to the same object & use Alias nodes for all
37
40
  // after first. The `ref` wrapper allows for circular references to resolve.
38
41
  let ref = undefined;
39
- if (value && typeof value === 'object') {
42
+ if (aliasDuplicateObjects && value && typeof value === 'object') {
40
43
  ref = sourceObjects.get(value);
41
44
  if (ref) {
42
45
  if (!ref.anchor)
@@ -48,12 +51,14 @@ function createNode(value, tagName, ctx) {
48
51
  sourceObjects.set(value, ref);
49
52
  }
50
53
  }
51
- if (tagName && tagName.startsWith('!!'))
54
+ if (tagName === null || tagName === void 0 ? void 0 : tagName.startsWith('!!'))
52
55
  tagName = defaultTagPrefix + tagName.slice(2);
53
56
  let tagObj = findTagObject(value, tagName, schema.tags);
54
57
  if (!tagObj) {
55
- if (value && typeof value.toJSON === 'function')
58
+ if (value && typeof value.toJSON === 'function') {
59
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
56
60
  value = value.toJSON();
61
+ }
57
62
  if (!value || typeof value !== 'object') {
58
63
  const node = new Scalar.Scalar(value);
59
64
  if (ref)
@@ -3,7 +3,7 @@ export declare class Directives {
3
3
  static defaultYaml: Directives['yaml'];
4
4
  static defaultTags: Directives['tags'];
5
5
  yaml: {
6
- version: '1.1' | '1.2';
6
+ version: '1.1' | '1.2' | 'next';
7
7
  explicit?: boolean;
8
8
  };
9
9
  tags: Record<string, string>;
@@ -11,7 +11,9 @@ export declare class Directives {
11
11
  * The directives-end/doc-start marker `---`. If `null`, a marker may still be
12
12
  * included in the document's stringified representation.
13
13
  */
14
- marker: true | null;
14
+ docStart: true | null;
15
+ /** The doc-end marker `...`. */
16
+ docEnd: boolean;
15
17
  /**
16
18
  * Used when parsing YAML 1.1, where:
17
19
  * > If the document specifies no directives, it is parsed using the same
@@ -20,6 +22,7 @@ export declare class Directives {
20
22
  */
21
23
  private atNextDocument?;
22
24
  constructor(yaml?: Directives['yaml'], tags?: Directives['tags']);
25
+ clone(): Directives;
23
26
  /**
24
27
  * During parsing, get a Directives instance for the current document and
25
28
  * update the stream state according to the current version's spec.