yaml 2.0.0-10 → 2.0.0-5

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 (112) hide show
  1. package/browser/dist/compose/compose-doc.js +1 -2
  2. package/browser/dist/compose/compose-node.js +1 -10
  3. package/browser/dist/compose/compose-scalar.js +10 -17
  4. package/browser/dist/compose/composer.js +2 -3
  5. package/browser/dist/compose/resolve-block-map.js +5 -22
  6. package/browser/dist/compose/resolve-block-scalar.js +2 -2
  7. package/browser/dist/compose/resolve-block-seq.js +1 -6
  8. package/browser/dist/compose/resolve-end.js +2 -2
  9. package/browser/dist/compose/resolve-flow-collection.js +8 -24
  10. package/browser/dist/compose/resolve-flow-scalar.js +7 -19
  11. package/browser/dist/compose/resolve-props.js +7 -32
  12. package/browser/dist/doc/Document.js +12 -58
  13. package/browser/dist/doc/createNode.js +5 -11
  14. package/browser/dist/doc/directives.js +0 -5
  15. package/browser/dist/nodes/Collection.js +15 -28
  16. package/browser/dist/nodes/Node.js +0 -7
  17. package/browser/dist/nodes/Pair.js +1 -9
  18. package/browser/dist/nodes/YAMLMap.js +1 -1
  19. package/browser/dist/nodes/YAMLSeq.js +1 -1
  20. package/browser/dist/nodes/addPairToJSMap.js +3 -4
  21. package/browser/dist/options.js +0 -2
  22. package/browser/dist/parse/cst-scalar.js +6 -2
  23. package/browser/dist/parse/lexer.js +31 -67
  24. package/browser/dist/parse/parser.js +55 -62
  25. package/browser/dist/public-api.js +4 -2
  26. package/browser/dist/schema/Schema.js +3 -14
  27. package/browser/dist/schema/core/float.js +1 -4
  28. package/browser/dist/schema/tags.js +13 -17
  29. package/browser/dist/schema/yaml-1.1/float.js +1 -4
  30. package/browser/dist/stringify/addComment.js +11 -0
  31. package/browser/dist/stringify/stringify.js +17 -47
  32. package/browser/dist/stringify/stringifyCollection.js +72 -116
  33. package/browser/dist/stringify/stringifyDocument.js +9 -16
  34. package/browser/dist/stringify/stringifyPair.js +20 -29
  35. package/browser/dist/stringify/stringifyString.js +41 -39
  36. package/browser/dist/util.js +0 -3
  37. package/dist/compose/compose-doc.js +1 -2
  38. package/dist/compose/compose-node.d.ts +0 -1
  39. package/dist/compose/compose-node.js +1 -10
  40. package/dist/compose/compose-scalar.js +9 -16
  41. package/dist/compose/composer.js +2 -3
  42. package/dist/compose/resolve-block-map.d.ts +1 -2
  43. package/dist/compose/resolve-block-map.js +5 -22
  44. package/dist/compose/resolve-block-scalar.js +2 -2
  45. package/dist/compose/resolve-block-seq.js +1 -6
  46. package/dist/compose/resolve-end.js +2 -2
  47. package/dist/compose/resolve-flow-collection.js +8 -24
  48. package/dist/compose/resolve-flow-scalar.js +7 -19
  49. package/dist/compose/resolve-props.d.ts +4 -3
  50. package/dist/compose/resolve-props.js +7 -32
  51. package/dist/doc/Document.d.ts +5 -16
  52. package/dist/doc/Document.js +11 -57
  53. package/dist/doc/createNode.d.ts +1 -2
  54. package/dist/doc/createNode.js +4 -10
  55. package/dist/doc/directives.d.ts +0 -1
  56. package/dist/doc/directives.js +0 -5
  57. package/dist/errors.d.ts +1 -1
  58. package/dist/index.d.ts +2 -2
  59. package/dist/nodes/Alias.d.ts +3 -7
  60. package/dist/nodes/Collection.d.ts +4 -10
  61. package/dist/nodes/Collection.js +14 -27
  62. package/dist/nodes/Node.d.ts +3 -10
  63. package/dist/nodes/Node.js +0 -7
  64. package/dist/nodes/Pair.d.ts +2 -7
  65. package/dist/nodes/Pair.js +0 -8
  66. package/dist/nodes/Scalar.d.ts +0 -2
  67. package/dist/nodes/YAMLMap.d.ts +0 -2
  68. package/dist/nodes/YAMLMap.js +1 -1
  69. package/dist/nodes/YAMLSeq.d.ts +0 -2
  70. package/dist/nodes/YAMLSeq.js +1 -1
  71. package/dist/nodes/addPairToJSMap.js +2 -3
  72. package/dist/options.d.ts +6 -81
  73. package/dist/options.js +0 -2
  74. package/dist/parse/cst-scalar.d.ts +0 -6
  75. package/dist/parse/cst-scalar.js +6 -2
  76. package/dist/parse/lexer.d.ts +1 -3
  77. package/dist/parse/lexer.js +31 -67
  78. package/dist/parse/parser.d.ts +0 -1
  79. package/dist/parse/parser.js +55 -62
  80. package/dist/public-api.js +4 -2
  81. package/dist/schema/Schema.d.ts +4 -6
  82. package/dist/schema/Schema.js +2 -13
  83. package/dist/schema/core/float.js +1 -4
  84. package/dist/schema/tags.d.ts +2 -1
  85. package/dist/schema/tags.js +13 -17
  86. package/dist/schema/yaml-1.1/float.js +1 -4
  87. package/dist/stringify/addComment.d.ts +2 -0
  88. package/dist/stringify/addComment.js +13 -0
  89. package/dist/stringify/stringify.d.ts +3 -5
  90. package/dist/stringify/stringify.js +17 -47
  91. package/dist/stringify/stringifyCollection.d.ts +7 -3
  92. package/dist/stringify/stringifyCollection.js +72 -116
  93. package/dist/stringify/stringifyDocument.js +9 -16
  94. package/dist/stringify/stringifyPair.js +20 -29
  95. package/dist/stringify/stringifyString.js +41 -39
  96. package/dist/test-events.d.ts +1 -1
  97. package/dist/test-events.js +2 -4
  98. package/dist/util.d.ts +0 -3
  99. package/dist/util.js +0 -6
  100. package/package.json +12 -14
  101. package/browser/dist/compose/util-flow-indent-check.js +0 -15
  102. package/browser/dist/compose/util-map-includes.js +0 -17
  103. package/browser/dist/node_modules/tslib/tslib.es6.js +0 -164
  104. package/browser/dist/stringify/stringifyComment.js +0 -18
  105. package/dist/compose/util-flow-indent-check.d.ts +0 -3
  106. package/dist/compose/util-flow-indent-check.js +0 -17
  107. package/dist/compose/util-map-includes.d.ts +0 -4
  108. package/dist/compose/util-map-includes.js +0 -19
  109. package/dist/node_modules/tslib/tslib.es6.js +0 -76
  110. package/dist/stringify/stringifyComment.d.ts +0 -10
  111. package/dist/stringify/stringifyComment.js +0 -22
  112. package/util.d.ts +0 -3
@@ -7,14 +7,13 @@ function composeDoc(options, directives, { offset, start, value, end }, onError)
7
7
  const opts = Object.assign({ directives }, options);
8
8
  const doc = new Document(undefined, opts);
9
9
  const ctx = {
10
- atRoot: true,
11
10
  directives: doc.directives,
12
11
  options: doc.options,
13
12
  schema: doc.schema
14
13
  };
15
14
  const props = resolveProps(start, {
15
+ ctx,
16
16
  indicator: 'doc-start',
17
- next: value || (end === null || end === void 0 ? void 0 : end[0]),
18
17
  offset,
19
18
  onError,
20
19
  startOnNewline: true
@@ -33,8 +33,6 @@ function composeNode(ctx, token, props, onError) {
33
33
  console.log(token);
34
34
  throw new Error(`Unsupporten token type: ${token.type}`);
35
35
  }
36
- if (anchor && node.anchor === '')
37
- onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
38
36
  if (spaceBefore)
39
37
  node.spaceBefore = true;
40
38
  if (comment) {
@@ -43,8 +41,6 @@ function composeNode(ctx, token, props, onError) {
43
41
  else
44
42
  node.commentBefore = comment;
45
43
  }
46
- if (ctx.options.keepSourceTokens)
47
- node.srcToken = token;
48
44
  return node;
49
45
  }
50
46
  function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anchor, tag }, onError) {
@@ -55,11 +51,8 @@ function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anch
55
51
  source: ''
56
52
  };
57
53
  const node = composeScalar(ctx, token, tag, onError);
58
- if (anchor) {
54
+ if (anchor)
59
55
  node.anchor = anchor.source.substring(1);
60
- if (node.anchor === '')
61
- onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
62
- }
63
56
  if (spaceBefore)
64
57
  node.spaceBefore = true;
65
58
  if (comment)
@@ -68,8 +61,6 @@ function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anch
68
61
  }
69
62
  function composeAlias({ options }, { offset, source, end }, onError) {
70
63
  const alias = new Alias(source.substring(1));
71
- if (alias.source === '')
72
- onError(offset, 'BAD_ALIAS', 'Alias cannot be an empty string');
73
64
  const valueEnd = offset + source.length;
74
65
  const re = resolveEnd(end, valueEnd, options.strict, onError);
75
66
  alias.range = [offset, valueEnd, re.offset];
@@ -1,4 +1,4 @@
1
- import { SCALAR, isScalar } from '../nodes/Node.js';
1
+ import { isScalar, SCALAR } from '../nodes/Node.js';
2
2
  import { Scalar } from '../nodes/Scalar.js';
3
3
  import { resolveBlockScalar } from './resolve-block-scalar.js';
4
4
  import { resolveFlowScalar } from './resolve-flow-scalar.js';
@@ -12,17 +12,14 @@ function composeScalar(ctx, token, tagToken, onError) {
12
12
  : null;
13
13
  const tag = tagToken && tagName
14
14
  ? findScalarTagByName(ctx.schema, value, tagName, tagToken, onError)
15
- : token.type === 'scalar'
16
- ? findScalarTagByTest(ctx, value, token, onError)
17
- : ctx.schema[SCALAR];
15
+ : findScalarTagByTest(ctx.schema, value, token.type === 'scalar');
18
16
  let scalar;
19
17
  try {
20
18
  const res = tag.resolve(value, msg => onError(tagToken || token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
21
19
  scalar = isScalar(res) ? res : new Scalar(res);
22
20
  }
23
21
  catch (error) {
24
- const msg = error instanceof Error ? error.message : String(error);
25
- onError(tagToken || token, 'TAG_RESOLVE_FAILED', msg);
22
+ onError(tagToken || token, 'TAG_RESOLVE_FAILED', error.message);
26
23
  scalar = new Scalar(value);
27
24
  }
28
25
  scalar.range = range;
@@ -63,19 +60,15 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
63
60
  onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
64
61
  return schema[SCALAR];
65
62
  }
66
- function findScalarTagByTest({ directives, schema }, value, token, onError) {
67
- const tag = schema.tags.find(tag => { var _a; return tag.default && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(value)); }) || schema[SCALAR];
68
- if (schema.compat) {
69
- const compat = schema.compat.find(tag => { var _a; return tag.default && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(value)); }) ||
70
- schema[SCALAR];
71
- if (tag.tag !== compat.tag) {
72
- const ts = directives.tagString(tag.tag);
73
- const cs = directives.tagString(compat.tag);
74
- const msg = `Value may be parsed as either ${ts} or ${cs}`;
75
- onError(token, 'TAG_RESOLVE_FAILED', msg, true);
63
+ function findScalarTagByTest(schema, value, apply) {
64
+ var _a;
65
+ if (apply) {
66
+ for (const tag of schema.tags) {
67
+ if (tag.default && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(value)))
68
+ return tag;
76
69
  }
77
70
  }
78
- return tag;
71
+ return schema[SCALAR];
79
72
  }
80
73
 
81
74
  export { composeScalar };
@@ -15,7 +15,6 @@ function getErrorPos(src) {
15
15
  return [offset, offset + (typeof source === 'string' ? source.length : 1)];
16
16
  }
17
17
  function parsePrelude(prelude) {
18
- var _a;
19
18
  let comment = '';
20
19
  let atComment = false;
21
20
  let afterEmptyLine = false;
@@ -25,12 +24,12 @@ function parsePrelude(prelude) {
25
24
  case '#':
26
25
  comment +=
27
26
  (comment === '' ? '' : afterEmptyLine ? '\n\n' : '\n') +
28
- (source.substring(1) || ' ');
27
+ source.substring(1);
29
28
  atComment = true;
30
29
  afterEmptyLine = false;
31
30
  break;
32
31
  case '%':
33
- if (((_a = prelude[i + 1]) === null || _a === void 0 ? void 0 : _a[0]) !== '#')
32
+ if (prelude[i + 1][0] !== '#')
34
33
  i += 1;
35
34
  atComment = false;
36
35
  break;
@@ -2,22 +2,17 @@ import { Pair } from '../nodes/Pair.js';
2
2
  import { YAMLMap } from '../nodes/YAMLMap.js';
3
3
  import { resolveProps } from './resolve-props.js';
4
4
  import { containsNewline } from './util-contains-newline.js';
5
- import { flowIndentCheck } from './util-flow-indent-check.js';
6
- import { mapIncludes } from './util-map-includes.js';
7
5
 
8
6
  const startColMsg = 'All mapping items must start at the same column';
9
7
  function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
10
8
  var _a;
11
9
  const map = new YAMLMap(ctx.schema);
12
- if (ctx.atRoot)
13
- ctx.atRoot = false;
14
10
  let offset = bm.offset;
15
- for (const collItem of bm.items) {
16
- const { start, key, sep, value } = collItem;
11
+ for (const { start, key, sep, value } of bm.items) {
17
12
  // key properties
18
13
  const keyProps = resolveProps(start, {
14
+ ctx,
19
15
  indicator: 'explicit-key-ind',
20
- next: key || (sep === null || sep === void 0 ? void 0 : sep[0]),
21
16
  offset,
22
17
  onError,
23
18
  startOnNewline: true
@@ -51,14 +46,10 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
51
46
  const keyNode = key
52
47
  ? composeNode(ctx, key, keyProps, onError)
53
48
  : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
54
- if (ctx.schema.compat)
55
- flowIndentCheck(bm.indent, key, onError);
56
- if (mapIncludes(ctx, map.items, keyNode))
57
- onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
58
49
  // value properties
59
50
  const valueProps = resolveProps(sep || [], {
51
+ ctx,
60
52
  indicator: 'map-value-ind',
61
- next: value,
62
53
  offset: keyNode.range[2],
63
54
  onError,
64
55
  startOnNewline: !key || key.type === 'block-scalar'
@@ -76,13 +67,8 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
76
67
  const valueNode = value
77
68
  ? composeNode(ctx, value, valueProps, onError)
78
69
  : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
79
- if (ctx.schema.compat)
80
- flowIndentCheck(bm.indent, value, onError);
81
70
  offset = valueNode.range[2];
82
- const pair = new Pair(keyNode, valueNode);
83
- if (ctx.options.keepSourceTokens)
84
- pair.srcToken = collItem;
85
- map.items.push(pair);
71
+ map.items.push(new Pair(keyNode, valueNode));
86
72
  }
87
73
  else {
88
74
  // key with no value
@@ -94,10 +80,7 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
94
80
  else
95
81
  keyNode.comment = valueProps.comment;
96
82
  }
97
- const pair = new Pair(keyNode);
98
- if (ctx.options.keepSourceTokens)
99
- pair.srcToken = collItem;
100
- map.items.push(pair);
83
+ map.items.push(new Pair(keyNode));
101
84
  }
102
85
  }
103
86
  map.range = [bm.offset, offset, offset];
@@ -18,7 +18,7 @@ function resolveBlockScalar(scalar, strict, onError) {
18
18
  }
19
19
  // shortcut for empty contents
20
20
  if (!scalar.source || chompStart === 0) {
21
- const value = header.chomp === '+' ? '\n'.repeat(Math.max(0, lines.length - 1)) : '';
21
+ const value = header.chomp === '+' ? lines.map(line => line[0]).join('\n') : '';
22
22
  let end = start + header.length;
23
23
  if (scalar.source)
24
24
  end += scalar.source.length;
@@ -149,7 +149,7 @@ function parseBlockScalarHeader({ offset, props }, strict, onError) {
149
149
  case 'comment':
150
150
  if (strict && !hasSpace) {
151
151
  const message = 'Comments must be separated from other tokens by white space characters';
152
- onError(token, 'MISSING_CHAR', message);
152
+ onError(token, 'COMMENT_SPACE', message);
153
153
  }
154
154
  length += token.source.length;
155
155
  comment = token.source.substring(1);
@@ -1,16 +1,13 @@
1
1
  import { YAMLSeq } from '../nodes/YAMLSeq.js';
2
2
  import { resolveProps } from './resolve-props.js';
3
- import { flowIndentCheck } from './util-flow-indent-check.js';
4
3
 
5
4
  function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError) {
6
5
  const seq = new YAMLSeq(ctx.schema);
7
- if (ctx.atRoot)
8
- ctx.atRoot = false;
9
6
  let offset = bs.offset;
10
7
  for (const { start, value } of bs.items) {
11
8
  const props = resolveProps(start, {
9
+ ctx,
12
10
  indicator: 'seq-item-ind',
13
- next: value,
14
11
  offset,
15
12
  onError,
16
13
  startOnNewline: true
@@ -33,8 +30,6 @@ function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError) {
33
30
  const node = value
34
31
  ? composeNode(ctx, value, props, onError)
35
32
  : composeEmptyNode(ctx, offset, start, null, props, onError);
36
- if (ctx.schema.compat)
37
- flowIndentCheck(bs.indent, value, onError);
38
33
  offset = node.range[2];
39
34
  seq.items.push(node);
40
35
  }
@@ -11,8 +11,8 @@ function resolveEnd(end, offset, reqSpace, onError) {
11
11
  break;
12
12
  case 'comment': {
13
13
  if (reqSpace && !hasSpace)
14
- onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
15
- const cb = source.substring(1) || ' ';
14
+ onError(token, 'COMMENT_SPACE', 'Comments must be separated from other tokens by white space characters');
15
+ const cb = source.substring(1);
16
16
  if (!comment)
17
17
  comment = cb;
18
18
  else
@@ -5,7 +5,6 @@ import { YAMLSeq } from '../nodes/YAMLSeq.js';
5
5
  import { resolveEnd } from './resolve-end.js';
6
6
  import { resolveProps } from './resolve-props.js';
7
7
  import { containsNewline } from './util-contains-newline.js';
8
- import { mapIncludes } from './util-map-includes.js';
9
8
 
10
9
  const blockMsg = 'Block collections are not allowed within flow collections';
11
10
  const isBlock = (token) => token && (token.type === 'block-map' || token.type === 'block-seq');
@@ -16,17 +15,13 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
16
15
  ? new YAMLMap(ctx.schema)
17
16
  : new YAMLSeq(ctx.schema);
18
17
  coll.flow = true;
19
- const atRoot = ctx.atRoot;
20
- if (atRoot)
21
- ctx.atRoot = false;
22
- let offset = fc.offset + fc.start.source.length;
18
+ let offset = fc.offset;
23
19
  for (let i = 0; i < fc.items.length; ++i) {
24
- const collItem = fc.items[i];
25
- const { start, key, sep, value } = collItem;
20
+ const { start, key, sep, value } = fc.items[i];
26
21
  const props = resolveProps(start, {
22
+ ctx,
27
23
  flow: fcName,
28
24
  indicator: 'explicit-key-ind',
29
- next: key || (sep === null || sep === void 0 ? void 0 : sep[0]),
30
25
  offset,
31
26
  onError,
32
27
  startOnNewline: false
@@ -43,7 +38,6 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
43
38
  else
44
39
  coll.comment = props.comment;
45
40
  }
46
- offset = props.end;
47
41
  continue;
48
42
  }
49
43
  if (!isMap && ctx.options.strict && containsNewline(key))
@@ -105,9 +99,9 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
105
99
  onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
106
100
  // value properties
107
101
  const valueProps = resolveProps(sep || [], {
102
+ ctx,
108
103
  flow: fcName,
109
104
  indicator: 'map-value-ind',
110
- next: value,
111
105
  offset: keyNode.range[2],
112
106
  onError,
113
107
  startOnNewline: false
@@ -150,14 +144,8 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
150
144
  keyNode.comment = valueProps.comment;
151
145
  }
152
146
  const pair = new Pair(keyNode, valueNode);
153
- if (ctx.options.keepSourceTokens)
154
- pair.srcToken = collItem;
155
- if (isMap) {
156
- const map = coll;
157
- if (mapIncludes(ctx, map.items, keyNode))
158
- onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
159
- map.items.push(pair);
160
- }
147
+ if (isMap)
148
+ coll.items.push(pair);
161
149
  else {
162
150
  const map = new YAMLMap(ctx.schema);
163
151
  map.flow = true;
@@ -171,13 +159,9 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
171
159
  const [ce, ...ee] = fc.end;
172
160
  let cePos = offset;
173
161
  if (ce && ce.source === expectedEnd)
174
- cePos = ce.offset + ce.source.length;
162
+ cePos += ce.source.length;
175
163
  else {
176
- const name = fcName[0].toUpperCase() + fcName.substring(1);
177
- const msg = atRoot
178
- ? `${name} must end with a ${expectedEnd}`
179
- : `${name} in block collection must be sufficiently indented and end with a ${expectedEnd}`;
180
- onError(offset, atRoot ? 'MISSING_CHAR' : 'BAD_INDENT', msg);
164
+ onError(offset + 1, 'MISSING_CHAR', `Expected ${fcName} to end with ${expectedEnd}`);
181
165
  if (ce && ce.source.length !== 1)
182
166
  ee.unshift(ce);
183
167
  }
@@ -39,31 +39,25 @@ function resolveFlowScalar(scalar, strict, onError) {
39
39
  };
40
40
  }
41
41
  function plainValue(source, onError) {
42
- let badChar = '';
42
+ let message = '';
43
43
  switch (source[0]) {
44
44
  /* istanbul ignore next should not happen */
45
45
  case '\t':
46
- badChar = 'a tab character';
47
- break;
48
- case ',':
49
- badChar = 'flow indicator character ,';
50
- break;
51
- case '%':
52
- badChar = 'directive indicator character %';
46
+ message = 'Plain value cannot start with a tab character';
53
47
  break;
54
48
  case '|':
55
49
  case '>': {
56
- badChar = `block scalar indicator ${source[0]}`;
50
+ message = `Plain value cannot start with block scalar indicator ${source[0]}`;
57
51
  break;
58
52
  }
59
53
  case '@':
60
54
  case '`': {
61
- badChar = `reserved character ${source[0]}`;
55
+ message = `Plain value cannot start with reserved character ${source[0]}`;
62
56
  break;
63
57
  }
64
58
  }
65
- if (badChar)
66
- onError(0, 'BAD_SCALAR_START', `Plain value cannot start with ${badChar}`);
59
+ if (message)
60
+ onError(0, 'BAD_SCALAR_START', message);
67
61
  return foldLines(source);
68
62
  }
69
63
  function singleQuotedValue(source, onError) {
@@ -135,12 +129,6 @@ function doubleQuotedValue(source, onError) {
135
129
  while (next === ' ' || next === '\t')
136
130
  next = source[++i + 1];
137
131
  }
138
- else if (next === '\r' && source[i + 1] === '\n') {
139
- // skip escaped CRLF newlines, but still trim the following line
140
- next = source[++i + 1];
141
- while (next === ' ' || next === '\t')
142
- next = source[++i + 1];
143
- }
144
132
  else if (next === 'x' || next === 'u' || next === 'U') {
145
133
  const length = { x: 2, u: 4, U: 8 }[next];
146
134
  res += parseCharCode(source, i + 1, length, onError);
@@ -158,7 +146,7 @@ function doubleQuotedValue(source, onError) {
158
146
  let next = source[i + 1];
159
147
  while (next === ' ' || next === '\t')
160
148
  next = source[++i + 1];
161
- if (next !== '\n' && !(next === '\r' && source[i + 2] === '\n'))
149
+ if (next !== '\n')
162
150
  res += i > wsStart ? source.slice(wsStart, i + 1) : ch;
163
151
  }
164
152
  else {
@@ -1,24 +1,16 @@
1
- function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnNewline }) {
1
+ function resolveProps(tokens, { ctx, flow, indicator, offset, onError, startOnNewline }) {
2
2
  let spaceBefore = false;
3
3
  let atNewline = startOnNewline;
4
4
  let hasSpace = startOnNewline;
5
5
  let comment = '';
6
6
  let commentSep = '';
7
7
  let hasNewline = false;
8
- let reqSpace = false;
9
8
  let anchor = null;
10
9
  let tag = null;
11
10
  let comma = null;
12
11
  let found = null;
13
12
  let start = null;
14
13
  for (const token of tokens) {
15
- if (reqSpace) {
16
- if (token.type !== 'space' &&
17
- token.type !== 'newline' &&
18
- token.type !== 'comma')
19
- onError(token.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space');
20
- reqSpace = false;
21
- }
22
14
  switch (token.type) {
23
15
  case 'space':
24
16
  // At the doc level, tabs at line start may be parsed
@@ -32,29 +24,23 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
32
24
  hasSpace = true;
33
25
  break;
34
26
  case 'comment': {
35
- if (!hasSpace)
36
- onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
37
- const cb = token.source.substring(1) || ' ';
27
+ if (ctx.options.strict && !hasSpace)
28
+ onError(token, 'COMMENT_SPACE', 'Comments must be separated from other tokens by white space characters');
29
+ const cb = token.source.substring(1);
38
30
  if (!comment)
39
31
  comment = cb;
40
32
  else
41
33
  comment += commentSep + cb;
42
34
  commentSep = '';
43
- atNewline = false;
44
35
  break;
45
36
  }
46
37
  case 'newline':
47
- if (atNewline) {
48
- if (comment)
49
- comment += token.source;
50
- else
51
- spaceBefore = true;
52
- }
53
- else
54
- commentSep += token.source;
38
+ if (atNewline && !comment)
39
+ spaceBefore = true;
55
40
  atNewline = true;
56
41
  hasNewline = true;
57
42
  hasSpace = true;
43
+ commentSep += token.source;
58
44
  break;
59
45
  case 'anchor':
60
46
  if (anchor)
@@ -64,7 +50,6 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
64
50
  start = token.offset;
65
51
  atNewline = false;
66
52
  hasSpace = false;
67
- reqSpace = true;
68
53
  break;
69
54
  case 'tag': {
70
55
  if (tag)
@@ -74,15 +59,12 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
74
59
  start = token.offset;
75
60
  atNewline = false;
76
61
  hasSpace = false;
77
- reqSpace = true;
78
62
  break;
79
63
  }
80
64
  case indicator:
81
65
  // Could here handle preceding comments differently
82
66
  if (anchor || tag)
83
67
  onError(token, 'BAD_PROP_ORDER', `Anchors and tags must be after the ${token.source} indicator`);
84
- if (found)
85
- onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.source} in ${flow || 'collection'}`);
86
68
  found = token;
87
69
  atNewline = false;
88
70
  hasSpace = false;
@@ -105,13 +87,6 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
105
87
  }
106
88
  const last = tokens[tokens.length - 1];
107
89
  const end = last ? last.offset + last.source.length : offset;
108
- if (reqSpace &&
109
- next &&
110
- next.type !== 'space' &&
111
- next.type !== 'newline' &&
112
- next.type !== 'comma' &&
113
- (next.type !== 'scalar' || next.source !== ''))
114
- onError(next.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space');
115
90
  return {
116
91
  comma,
117
92
  found,
@@ -1,6 +1,6 @@
1
1
  import { Alias } from '../nodes/Alias.js';
2
2
  import { isEmptyPath, collectionFromPath } from '../nodes/Collection.js';
3
- import { NODE_TYPE, DOC, isNode, isCollection, isScalar } from '../nodes/Node.js';
3
+ import { NODE_TYPE, DOC, isCollection, isScalar } from '../nodes/Node.js';
4
4
  import { Pair } from '../nodes/Pair.js';
5
5
  import { toJS } from '../nodes/toJS.js';
6
6
  import { defaultOptions } from '../options.js';
@@ -48,30 +48,6 @@ class Document {
48
48
  this.contents = this.createNode(value, _replacer, options);
49
49
  }
50
50
  }
51
- /**
52
- * Create a deep copy of this Document and its contents.
53
- *
54
- * Custom Node values that inherit from `Object` still refer to their original instances.
55
- */
56
- clone() {
57
- const copy = Object.create(Document.prototype, {
58
- [NODE_TYPE]: { value: DOC }
59
- });
60
- copy.commentBefore = this.commentBefore;
61
- copy.comment = this.comment;
62
- copy.errors = this.errors.slice();
63
- copy.warnings = this.warnings.slice();
64
- copy.options = Object.assign({}, this.options);
65
- if (this.directives)
66
- copy.directives = this.directives.clone();
67
- copy.schema = this.schema.clone();
68
- copy.contents = isNode(this.contents)
69
- ? this.contents.clone(copy.schema)
70
- : this.contents;
71
- if (this.range)
72
- copy.range = this.range.slice();
73
- return copy;
74
- }
75
51
  /** Adds a value to the document. */
76
52
  add(value) {
77
53
  if (assertCollection(this.contents))
@@ -116,10 +92,9 @@ class Document {
116
92
  options = replacer;
117
93
  replacer = undefined;
118
94
  }
119
- const { aliasDuplicateObjects, anchorPrefix, flow, keepUndefined, onTagObj, tag } = options || {};
95
+ const { anchorPrefix, flow, keepUndefined, onTagObj, tag } = options || {};
120
96
  const { onAnchor, setAnchors, sourceObjects } = createNodeAnchors(this, anchorPrefix || 'a');
121
97
  const ctx = {
122
- aliasDuplicateObjects: aliasDuplicateObjects !== null && aliasDuplicateObjects !== void 0 ? aliasDuplicateObjects : true,
123
98
  keepUndefined: keepUndefined !== null && keepUndefined !== void 0 ? keepUndefined : false,
124
99
  onAnchor,
125
100
  onTagObj,
@@ -230,47 +205,26 @@ class Document {
230
205
  }
231
206
  /**
232
207
  * Change the YAML version and schema used by the document.
233
- * A `null` version disables support for directives, explicit tags, anchors, and aliases.
234
- * It also requires the `schema` option to be given as a `Schema` instance value.
235
208
  *
236
- * Overrides all previously set schema options.
209
+ * Overrides all previously set schema options
237
210
  */
238
- setSchema(version, options = {}) {
239
- if (typeof version === 'number')
240
- version = String(version);
241
- let opt;
242
- switch (version) {
211
+ setSchema(version, options) {
212
+ let _options;
213
+ switch (String(version)) {
243
214
  case '1.1':
244
- if (this.directives)
245
- this.directives.yaml.version = '1.1';
246
- else
247
- this.directives = new Directives({ version: '1.1' });
248
- opt = { merge: true, resolveKnownTags: false, schema: 'yaml-1.1' };
215
+ this.directives.yaml.version = '1.1';
216
+ _options = Object.assign({ merge: true, resolveKnownTags: false, schema: 'yaml-1.1' }, options);
249
217
  break;
250
218
  case '1.2':
251
- if (this.directives)
252
- this.directives.yaml.version = '1.2';
253
- else
254
- this.directives = new Directives({ version: '1.2' });
255
- opt = { merge: false, resolveKnownTags: true, schema: 'core' };
256
- break;
257
- case null:
258
- if (this.directives)
259
- delete this.directives;
260
- opt = null;
219
+ this.directives.yaml.version = '1.2';
220
+ _options = Object.assign({ merge: false, resolveKnownTags: true, schema: 'core' }, options);
261
221
  break;
262
222
  default: {
263
223
  const sv = JSON.stringify(version);
264
- throw new Error(`Expected '1.1', '1.2' or null as first argument, but found: ${sv}`);
224
+ throw new Error(`Expected '1.1' or '1.2' as version, but found: ${sv}`);
265
225
  }
266
226
  }
267
- // Not using `instanceof Schema` to allow for duck typing
268
- if (options.schema instanceof Object)
269
- this.schema = options.schema;
270
- else if (opt)
271
- this.schema = new Schema(Object.assign(opt, options));
272
- else
273
- throw new Error(`With a null YAML version, the { schema: Schema } option is required`);
227
+ this.schema = new Schema(_options);
274
228
  }
275
229
  // json & jsonArg are only used from toJSON()
276
230
  toJS({ json, jsonArg, mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
@@ -1,5 +1,5 @@
1
1
  import { Alias } from '../nodes/Alias.js';
2
- import { isNode, isPair, MAP, SEQ, isDocument } from '../nodes/Node.js';
2
+ import { isNode, isPair, MAP, SEQ } from '../nodes/Node.js';
3
3
  import { Scalar } from '../nodes/Scalar.js';
4
4
 
5
5
  const defaultTagPrefix = 'tag:yaml.org,2002:';
@@ -15,8 +15,6 @@ function findTagObject(value, tagName, tags) {
15
15
  }
16
16
  function createNode(value, tagName, ctx) {
17
17
  var _a, _b;
18
- if (isDocument(value))
19
- value = value.contents;
20
18
  if (isNode(value))
21
19
  return value;
22
20
  if (isPair(value)) {
@@ -32,11 +30,11 @@ function createNode(value, tagName, ctx) {
32
30
  // https://tc39.es/ecma262/#sec-serializejsonproperty
33
31
  value = value.valueOf();
34
32
  }
35
- const { aliasDuplicateObjects, onAnchor, onTagObj, schema, sourceObjects } = ctx;
33
+ const { onAnchor, onTagObj, schema, sourceObjects } = ctx;
36
34
  // Detect duplicate references to the same object & use Alias nodes for all
37
35
  // after first. The `ref` wrapper allows for circular references to resolve.
38
36
  let ref = undefined;
39
- if (aliasDuplicateObjects && value && typeof value === 'object') {
37
+ if (value && typeof value === 'object') {
40
38
  ref = sourceObjects.get(value);
41
39
  if (ref) {
42
40
  if (!ref.anchor)
@@ -54,12 +52,8 @@ function createNode(value, tagName, ctx) {
54
52
  if (!tagObj) {
55
53
  if (value && typeof value.toJSON === 'function')
56
54
  value = value.toJSON();
57
- if (!value || typeof value !== 'object') {
58
- const node = new Scalar(value);
59
- if (ref)
60
- ref.node = node;
61
- return node;
62
- }
55
+ if (!value || typeof value !== 'object')
56
+ return new Scalar(value);
63
57
  tagObj =
64
58
  value instanceof Map
65
59
  ? schema[MAP]
@@ -20,11 +20,6 @@ class Directives {
20
20
  this.yaml = Object.assign({}, Directives.defaultYaml, yaml);
21
21
  this.tags = Object.assign({}, Directives.defaultTags, tags);
22
22
  }
23
- clone() {
24
- const copy = new Directives(this.yaml, this.tags);
25
- copy.marker = this.marker;
26
- return copy;
27
- }
28
23
  /**
29
24
  * During parsing, get a Directives instance for the current document and
30
25
  * update the stream state according to the current version's spec.