yaml 2.0.0-4 → 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 (210) hide show
  1. package/README.md +5 -5
  2. package/browser/dist/compose/compose-collection.js +12 -9
  3. package/browser/dist/compose/compose-doc.js +23 -6
  4. package/browser/dist/compose/compose-node.js +20 -17
  5. package/browser/dist/compose/compose-scalar.js +20 -23
  6. package/browser/dist/compose/composer.js +105 -76
  7. package/browser/dist/compose/resolve-block-map.js +38 -29
  8. package/browser/dist/compose/resolve-block-scalar.js +14 -17
  9. package/browser/dist/compose/resolve-block-seq.js +21 -17
  10. package/browser/dist/compose/resolve-end.js +6 -7
  11. package/browser/dist/compose/resolve-flow-collection.js +153 -199
  12. package/browser/dist/compose/resolve-flow-scalar.js +49 -25
  13. package/browser/dist/compose/resolve-props.js +47 -35
  14. package/browser/dist/compose/util-contains-newline.js +15 -12
  15. package/browser/dist/doc/Document.js +50 -99
  16. package/browser/dist/doc/anchors.js +72 -0
  17. package/browser/dist/doc/createNode.js +27 -17
  18. package/browser/dist/errors.js +16 -12
  19. package/browser/dist/index.js +3 -3
  20. package/browser/dist/nodes/Alias.js +51 -28
  21. package/browser/dist/nodes/Collection.js +5 -83
  22. package/browser/dist/nodes/Node.js +2 -1
  23. package/browser/dist/nodes/Pair.js +9 -232
  24. package/browser/dist/nodes/YAMLMap.js +8 -4
  25. package/browser/dist/nodes/YAMLSeq.js +7 -4
  26. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  27. package/browser/dist/nodes/toJS.js +12 -10
  28. package/browser/dist/options.js +0 -2
  29. package/browser/dist/parse/cst-scalar.js +219 -0
  30. package/browser/dist/parse/cst-stringify.js +61 -0
  31. package/browser/dist/parse/cst-visit.js +97 -0
  32. package/{dist/parse/tokens.js → browser/dist/parse/cst.js} +12 -7
  33. package/browser/dist/parse/lexer.js +99 -105
  34. package/browser/dist/parse/parser.js +219 -144
  35. package/browser/dist/public-api.js +11 -14
  36. package/browser/dist/schema/Schema.js +23 -0
  37. package/browser/dist/{tags/failsafe → schema/common}/map.js +0 -0
  38. package/browser/dist/schema/common/null.js +13 -0
  39. package/browser/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  40. package/browser/dist/{tags/failsafe → schema/common}/string.js +0 -0
  41. package/browser/dist/schema/core/bool.js +19 -0
  42. package/browser/dist/schema/core/float.js +40 -0
  43. package/browser/dist/schema/core/int.js +38 -0
  44. package/browser/dist/schema/core/schema.js +23 -0
  45. package/browser/dist/{tags/json.js → schema/json/schema.js} +5 -6
  46. package/browser/dist/schema/tags.js +79 -0
  47. package/browser/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  48. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  49. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  50. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  51. package/browser/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  52. package/browser/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  53. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  54. package/browser/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  55. package/browser/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  56. package/browser/dist/stringify/addComment.js +1 -7
  57. package/browser/dist/stringify/stringify.js +8 -6
  58. package/browser/dist/stringify/stringifyCollection.js +108 -0
  59. package/browser/dist/stringify/stringifyDocument.js +61 -0
  60. package/browser/dist/stringify/stringifyPair.js +113 -0
  61. package/browser/dist/stringify/stringifyString.js +53 -52
  62. package/browser/dist/visit.js +20 -1
  63. package/dist/compose/compose-collection.d.ts +4 -4
  64. package/dist/compose/compose-collection.js +12 -9
  65. package/dist/compose/compose-doc.d.ts +4 -3
  66. package/dist/compose/compose-doc.js +23 -6
  67. package/dist/compose/compose-node.d.ts +15 -7
  68. package/dist/compose/compose-node.js +20 -17
  69. package/dist/compose/compose-scalar.d.ts +4 -3
  70. package/dist/compose/compose-scalar.js +19 -22
  71. package/dist/compose/composer.d.ts +28 -23
  72. package/dist/compose/composer.js +106 -77
  73. package/dist/compose/resolve-block-map.d.ts +4 -4
  74. package/dist/compose/resolve-block-map.js +38 -29
  75. package/dist/compose/resolve-block-scalar.d.ts +5 -3
  76. package/dist/compose/resolve-block-scalar.js +14 -17
  77. package/dist/compose/resolve-block-seq.d.ts +4 -4
  78. package/dist/compose/resolve-block-seq.js +21 -17
  79. package/dist/compose/resolve-end.d.ts +3 -2
  80. package/dist/compose/resolve-end.js +6 -7
  81. package/dist/compose/resolve-flow-collection.d.ts +4 -5
  82. package/dist/compose/resolve-flow-collection.js +152 -198
  83. package/dist/compose/resolve-flow-scalar.d.ts +5 -3
  84. package/dist/compose/resolve-flow-scalar.js +49 -25
  85. package/dist/compose/resolve-props.d.ts +17 -10
  86. package/dist/compose/resolve-props.js +47 -35
  87. package/dist/compose/util-contains-newline.d.ts +1 -1
  88. package/dist/compose/util-contains-newline.js +15 -12
  89. package/dist/compose/util-empty-scalar-position.d.ts +1 -1
  90. package/dist/doc/Document.d.ts +27 -15
  91. package/dist/doc/Document.js +48 -97
  92. package/dist/doc/anchors.d.ts +24 -0
  93. package/dist/doc/anchors.js +77 -0
  94. package/dist/doc/createNode.d.ts +8 -10
  95. package/dist/doc/createNode.js +26 -16
  96. package/dist/errors.d.ts +11 -8
  97. package/dist/errors.js +16 -12
  98. package/dist/index.d.ts +5 -3
  99. package/dist/index.js +3 -3
  100. package/dist/nodes/Alias.d.ts +19 -9
  101. package/dist/nodes/Alias.js +51 -28
  102. package/dist/nodes/Collection.d.ts +3 -17
  103. package/dist/nodes/Collection.js +4 -82
  104. package/dist/nodes/Node.d.ts +6 -4
  105. package/dist/nodes/Node.js +2 -0
  106. package/dist/nodes/Pair.d.ts +6 -11
  107. package/dist/nodes/Pair.js +8 -231
  108. package/dist/nodes/Scalar.d.ts +6 -3
  109. package/dist/nodes/YAMLMap.d.ts +3 -3
  110. package/dist/nodes/YAMLMap.js +8 -4
  111. package/dist/nodes/YAMLSeq.d.ts +3 -3
  112. package/dist/nodes/YAMLSeq.js +7 -4
  113. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  114. package/dist/nodes/addPairToJSMap.js +105 -0
  115. package/dist/nodes/toJS.d.ts +4 -5
  116. package/dist/nodes/toJS.js +12 -10
  117. package/dist/options.d.ts +20 -26
  118. package/dist/options.js +0 -2
  119. package/dist/parse/cst-scalar.d.ts +58 -0
  120. package/dist/parse/cst-scalar.js +223 -0
  121. package/dist/parse/cst-stringify.d.ts +8 -0
  122. package/dist/parse/cst-stringify.js +63 -0
  123. package/dist/parse/cst-visit.d.ts +39 -0
  124. package/dist/parse/cst-visit.js +99 -0
  125. package/dist/parse/{tokens.d.ts → cst.d.ts} +23 -7
  126. package/{browser/dist/parse/tokens.js → dist/parse/cst.js} +27 -1
  127. package/dist/parse/lexer.d.ts +4 -13
  128. package/dist/parse/lexer.js +100 -106
  129. package/dist/parse/parser.d.ts +30 -24
  130. package/dist/parse/parser.js +221 -146
  131. package/dist/public-api.d.ts +7 -7
  132. package/dist/public-api.js +11 -14
  133. package/dist/{doc → schema}/Schema.d.ts +5 -3
  134. package/dist/schema/Schema.js +25 -0
  135. package/dist/{tags/failsafe → schema/common}/map.d.ts +0 -0
  136. package/dist/{tags/failsafe → schema/common}/map.js +0 -0
  137. package/dist/schema/common/null.d.ts +4 -0
  138. package/dist/schema/common/null.js +15 -0
  139. package/dist/{tags/failsafe → schema/common}/seq.d.ts +0 -0
  140. package/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  141. package/dist/{tags/failsafe → schema/common}/string.d.ts +0 -0
  142. package/dist/{tags/failsafe → schema/common}/string.js +0 -0
  143. package/dist/schema/core/bool.d.ts +4 -0
  144. package/dist/schema/core/bool.js +21 -0
  145. package/dist/schema/core/float.d.ts +4 -0
  146. package/dist/schema/core/float.js +44 -0
  147. package/dist/schema/core/int.d.ts +4 -0
  148. package/dist/schema/core/int.js +42 -0
  149. package/dist/schema/core/schema.d.ts +1 -0
  150. package/dist/schema/core/schema.js +25 -0
  151. package/dist/schema/json/schema.d.ts +2 -0
  152. package/dist/{tags/json.js → schema/json/schema.js} +5 -6
  153. package/dist/schema/tags.d.ts +41 -0
  154. package/dist/schema/tags.js +82 -0
  155. package/dist/{tags → schema}/types.d.ts +3 -7
  156. package/dist/{tags → schema}/yaml-1.1/binary.d.ts +0 -0
  157. package/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  158. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  159. package/dist/schema/yaml-1.1/bool.js +29 -0
  160. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  161. package/dist/schema/yaml-1.1/float.js +47 -0
  162. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  163. package/dist/schema/yaml-1.1/int.js +76 -0
  164. package/dist/{tags → schema}/yaml-1.1/omap.d.ts +0 -0
  165. package/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  166. package/dist/{tags → schema}/yaml-1.1/pairs.d.ts +3 -2
  167. package/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  168. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  169. package/dist/schema/yaml-1.1/schema.js +39 -0
  170. package/dist/{tags → schema}/yaml-1.1/set.d.ts +1 -1
  171. package/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  172. package/dist/{tags → schema}/yaml-1.1/timestamp.d.ts +0 -0
  173. package/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  174. package/dist/stringify/addComment.js +0 -7
  175. package/dist/stringify/stringify.d.ts +1 -2
  176. package/dist/stringify/stringify.js +8 -6
  177. package/dist/stringify/stringifyCollection.d.ts +21 -0
  178. package/dist/stringify/stringifyCollection.js +110 -0
  179. package/dist/stringify/stringifyDocument.d.ts +3 -0
  180. package/dist/stringify/stringifyDocument.js +63 -0
  181. package/dist/stringify/stringifyPair.d.ts +3 -0
  182. package/dist/stringify/stringifyPair.js +115 -0
  183. package/dist/stringify/stringifyString.js +53 -52
  184. package/dist/test-events.d.ts +1 -2
  185. package/dist/test-events.js +25 -12
  186. package/dist/util.d.ts +1 -1
  187. package/dist/visit.d.ts +10 -1
  188. package/dist/visit.js +20 -1
  189. package/package.json +4 -3
  190. package/browser/dist/doc/Anchors.js +0 -102
  191. package/browser/dist/doc/Schema.js +0 -27
  192. package/browser/dist/doc/getSchemaTags.js +0 -30
  193. package/browser/dist/tags/core.js +0 -109
  194. package/browser/dist/tags/failsafe/index.js +0 -7
  195. package/browser/dist/tags/index.js +0 -34
  196. package/browser/dist/tags/yaml-1.1/index.js +0 -152
  197. package/dist/doc/Anchors.d.ts +0 -36
  198. package/dist/doc/Anchors.js +0 -104
  199. package/dist/doc/Schema.js +0 -29
  200. package/dist/doc/getSchemaTags.d.ts +0 -3
  201. package/dist/doc/getSchemaTags.js +0 -32
  202. package/dist/tags/core.d.ts +0 -14
  203. package/dist/tags/core.js +0 -119
  204. package/dist/tags/failsafe/index.d.ts +0 -1
  205. package/dist/tags/failsafe/index.js +0 -9
  206. package/dist/tags/index.d.ts +0 -31
  207. package/dist/tags/index.js +0 -37
  208. package/dist/tags/json.d.ts +0 -2
  209. package/dist/tags/yaml-1.1/index.d.ts +0 -2
  210. package/dist/tags/yaml-1.1/index.js +0 -154
@@ -3,10 +3,11 @@
3
3
  var Scalar = require('../nodes/Scalar.js');
4
4
  var resolveEnd = require('./resolve-end.js');
5
5
 
6
- function resolveFlowScalar({ offset, type, source, end }, strict, onError) {
6
+ function resolveFlowScalar(scalar, strict, onError) {
7
+ const { offset, type, source, end } = scalar;
7
8
  let _type;
8
9
  let value;
9
- const _onError = (rel, msg) => onError(offset + rel, msg);
10
+ const _onError = (rel, code, msg) => onError(offset + rel, code, msg);
10
11
  switch (type) {
11
12
  case 'scalar':
12
13
  _type = Scalar.Scalar.PLAIN;
@@ -22,68 +23,91 @@ function resolveFlowScalar({ offset, type, source, end }, strict, onError) {
22
23
  break;
23
24
  /* istanbul ignore next should not happen */
24
25
  default:
25
- onError(offset, `Expected a flow scalar value, but found: ${type}`);
26
+ onError(scalar, 'UNEXPECTED_TOKEN', `Expected a flow scalar value, but found: ${type}`);
26
27
  return {
27
28
  value: '',
28
29
  type: null,
29
30
  comment: '',
30
- length: source.length
31
+ range: [offset, offset + source.length, offset + source.length]
31
32
  };
32
33
  }
33
- const re = resolveEnd.resolveEnd(end, 0, strict, _onError);
34
+ const valueEnd = offset + source.length;
35
+ const re = resolveEnd.resolveEnd(end, valueEnd, strict, onError);
34
36
  return {
35
37
  value,
36
38
  type: _type,
37
39
  comment: re.comment,
38
- length: source.length + re.offset
40
+ range: [offset, valueEnd, re.offset]
39
41
  };
40
42
  }
41
43
  function plainValue(source, onError) {
44
+ let message = '';
42
45
  switch (source[0]) {
43
46
  /* istanbul ignore next should not happen */
44
47
  case '\t':
45
- onError(0, 'Plain value cannot start with a tab character');
48
+ message = 'Plain value cannot start with a tab character';
46
49
  break;
47
50
  case '|':
48
51
  case '>': {
49
- const message = `Plain value cannot start with block scalar indicator ${source[0]}`;
50
- onError(0, message);
52
+ message = `Plain value cannot start with block scalar indicator ${source[0]}`;
51
53
  break;
52
54
  }
53
55
  case '@':
54
56
  case '`': {
55
- const message = `Plain value cannot start with reserved character ${source[0]}`;
56
- onError(0, message);
57
+ message = `Plain value cannot start with reserved character ${source[0]}`;
57
58
  break;
58
59
  }
59
60
  }
60
- return foldLines(source.trim());
61
+ if (message)
62
+ onError(0, 'BAD_SCALAR_START', message);
63
+ return foldLines(source);
61
64
  }
62
65
  function singleQuotedValue(source, onError) {
63
66
  if (source[source.length - 1] !== "'" || source.length === 1)
64
- onError(source.length, "Missing closing 'quote");
67
+ onError(source.length, 'MISSING_CHAR', "Missing closing 'quote");
65
68
  return foldLines(source.slice(1, -1)).replace(/''/g, "'");
66
69
  }
67
70
  function foldLines(source) {
68
- const lines = source.split(/[ \t]*\r?\n[ \t]*/);
69
- let res = lines[0];
71
+ /**
72
+ * The negative lookbehind here and in the `re` RegExp is to
73
+ * prevent causing a polynomial search time in certain cases.
74
+ *
75
+ * The try-catch is for Safari, which doesn't support this yet:
76
+ * https://caniuse.com/js-regexp-lookbehind
77
+ */
78
+ let first, line;
79
+ try {
80
+ first = new RegExp('(.*?)(?<![ \t])[ \t]*\r?\n', 'sy');
81
+ line = new RegExp('[ \t]*(.*?)(?:(?<![ \t])[ \t]*)?\r?\n', 'sy');
82
+ }
83
+ catch (_) {
84
+ first = /(.*?)[ \t]*\r?\n/sy;
85
+ line = /[ \t]*(.*?)[ \t]*\r?\n/sy;
86
+ }
87
+ let match = first.exec(source);
88
+ if (!match)
89
+ return source;
90
+ let res = match[1];
70
91
  let sep = ' ';
71
- for (let i = 1; i < lines.length - 1; ++i) {
72
- const line = lines[i];
73
- if (line === '') {
92
+ let pos = first.lastIndex;
93
+ line.lastIndex = pos;
94
+ while ((match = line.exec(source))) {
95
+ if (match[1] === '') {
74
96
  if (sep === '\n')
75
97
  res += sep;
76
98
  else
77
99
  sep = '\n';
78
100
  }
79
101
  else {
80
- res += sep + line;
102
+ res += sep + match[1];
81
103
  sep = ' ';
82
104
  }
105
+ pos = line.lastIndex;
83
106
  }
84
- if (lines.length > 1)
85
- res += sep + lines[lines.length - 1];
86
- return res;
107
+ const last = /[ \t]*(.*)/sy;
108
+ last.lastIndex = pos;
109
+ match = last.exec(source);
110
+ return res + sep + ((match && match[1]) || '');
87
111
  }
88
112
  function doubleQuotedValue(source, onError) {
89
113
  let res = '';
@@ -114,7 +138,7 @@ function doubleQuotedValue(source, onError) {
114
138
  }
115
139
  else {
116
140
  const raw = source.substr(i - 1, 2);
117
- onError(i - 1, `Invalid escape sequence ${raw}`);
141
+ onError(i - 1, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${raw}`);
118
142
  res += raw;
119
143
  }
120
144
  }
@@ -132,7 +156,7 @@ function doubleQuotedValue(source, onError) {
132
156
  }
133
157
  }
134
158
  if (source[source.length - 1] !== '"' || source.length === 1)
135
- onError(source.length, 'Missing closing "quote');
159
+ onError(source.length, 'MISSING_CHAR', 'Missing closing "quote');
136
160
  return res;
137
161
  }
138
162
  /**
@@ -180,7 +204,7 @@ function parseCharCode(source, offset, length, onError) {
180
204
  const code = ok ? parseInt(cc, 16) : NaN;
181
205
  if (isNaN(code)) {
182
206
  const raw = source.substr(offset - 2, length + 2);
183
- onError(offset - 2, `Invalid escape sequence ${raw}`);
207
+ onError(offset - 2, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${raw}`);
184
208
  return raw;
185
209
  }
186
210
  return String.fromCodePoint(code);
@@ -1,15 +1,22 @@
1
- import type { Document } from '../doc/Document.js';
2
- import type { SourceToken } from '../parse/tokens.js';
3
- export declare function resolveProps(doc: Document.Parsed, tokens: SourceToken[], startOnNewline: boolean, indicator: 'doc-start' | 'explicit-key-ind' | 'map-value-ind' | 'seq-item-ind', offset: number, onError: (offset: number, message: string) => void): {
4
- found: {
5
- indent: number;
6
- offset: number;
7
- } | null;
1
+ import type { SourceToken } from '../parse/cst.js';
2
+ import type { ComposeContext } from './compose-node.js';
3
+ import type { ComposeErrorHandler } from './composer.js';
4
+ export interface ResolvePropsArg {
5
+ ctx: ComposeContext;
6
+ flow?: string;
7
+ indicator: 'doc-start' | 'explicit-key-ind' | 'map-value-ind' | 'seq-item-ind';
8
+ offset: number;
9
+ onError: ComposeErrorHandler;
10
+ startOnNewline: boolean;
11
+ }
12
+ export declare function resolveProps(tokens: SourceToken[], { ctx, flow, indicator, offset, onError, startOnNewline }: ResolvePropsArg): {
13
+ comma: SourceToken | null;
14
+ found: SourceToken | null;
8
15
  spaceBefore: boolean;
9
16
  comment: string;
10
17
  hasNewline: boolean;
11
- anchor: string;
12
- tagName: string;
13
- length: number;
18
+ anchor: SourceToken | null;
19
+ tag: SourceToken | null;
20
+ end: number;
14
21
  start: number;
15
22
  };
@@ -1,92 +1,104 @@
1
1
  'use strict';
2
2
 
3
- function resolveProps(doc, tokens, startOnNewline, indicator, offset, onError) {
4
- let length = 0;
3
+ function resolveProps(tokens, { ctx, flow, indicator, offset, onError, startOnNewline }) {
5
4
  let spaceBefore = false;
6
5
  let atNewline = startOnNewline;
7
6
  let hasSpace = startOnNewline;
8
7
  let comment = '';
9
- let hasComment = false;
8
+ let commentSep = '';
10
9
  let hasNewline = false;
11
- let sep = '';
12
- let anchor = '';
13
- let tagName = '';
10
+ let anchor = null;
11
+ let tag = null;
12
+ let comma = null;
14
13
  let found = null;
15
14
  let start = null;
16
15
  for (const token of tokens) {
17
16
  switch (token.type) {
18
17
  case 'space':
19
- // At the doc level, tabs at line start may be parsed as leading
20
- // white space rather than indentation.
21
- if (atNewline && indicator !== 'doc-start' && token.source[0] === '\t')
22
- onError(offset + length, 'Tabs are not allowed as indentation');
18
+ // At the doc level, tabs at line start may be parsed
19
+ // as leading white space rather than indentation.
20
+ // In a flow collection, only the parser handles indent.
21
+ if (!flow &&
22
+ atNewline &&
23
+ indicator !== 'doc-start' &&
24
+ token.source[0] === '\t')
25
+ onError(token, 'TAB_AS_INDENT', 'Tabs are not allowed as indentation');
23
26
  hasSpace = true;
24
27
  break;
25
28
  case 'comment': {
26
- if (doc.options.strict && !hasSpace)
27
- onError(offset + length, 'Comments must be separated from other tokens by white space characters');
29
+ if (ctx.options.strict && !hasSpace)
30
+ onError(token, 'COMMENT_SPACE', 'Comments must be separated from other tokens by white space characters');
28
31
  const cb = token.source.substring(1);
29
- if (!hasComment)
32
+ if (!comment)
30
33
  comment = cb;
31
34
  else
32
- comment += sep + cb;
33
- hasComment = true;
34
- sep = '';
35
+ comment += commentSep + cb;
36
+ commentSep = '';
35
37
  break;
36
38
  }
37
39
  case 'newline':
38
- if (atNewline && !hasComment)
40
+ if (atNewline && !comment)
39
41
  spaceBefore = true;
40
42
  atNewline = true;
41
43
  hasNewline = true;
42
44
  hasSpace = true;
43
- sep += token.source;
45
+ commentSep += token.source;
44
46
  break;
45
47
  case 'anchor':
46
48
  if (anchor)
47
- onError(offset + length, 'A node can have at most one anchor');
48
- anchor = token.source.substring(1);
49
+ onError(token, 'MULTIPLE_ANCHORS', 'A node can have at most one anchor');
50
+ anchor = token;
49
51
  if (start === null)
50
- start = offset + length;
52
+ start = token.offset;
51
53
  atNewline = false;
52
54
  hasSpace = false;
53
55
  break;
54
56
  case 'tag': {
55
- if (tagName)
56
- onError(offset + length, 'A node can have at most one tag');
57
- const tn = doc.directives.tagName(token.source, msg => onError(offset, msg));
58
- if (tn)
59
- tagName = tn;
57
+ if (tag)
58
+ onError(token, 'MULTIPLE_TAGS', 'A node can have at most one tag');
59
+ tag = token;
60
60
  if (start === null)
61
- start = offset + length;
61
+ start = token.offset;
62
62
  atNewline = false;
63
63
  hasSpace = false;
64
64
  break;
65
65
  }
66
66
  case indicator:
67
67
  // Could here handle preceding comments differently
68
- found = { indent: token.indent, offset: offset + length };
68
+ if (anchor || tag)
69
+ onError(token, 'BAD_PROP_ORDER', `Anchors and tags must be after the ${token.source} indicator`);
70
+ found = token;
69
71
  atNewline = false;
70
72
  hasSpace = false;
71
73
  break;
74
+ case 'comma':
75
+ if (flow) {
76
+ if (comma)
77
+ onError(token, 'UNEXPECTED_TOKEN', `Unexpected , in ${flow}`);
78
+ comma = token;
79
+ atNewline = false;
80
+ hasSpace = false;
81
+ break;
82
+ }
83
+ // else fallthrough
72
84
  default:
73
- onError(offset + length, `Unexpected ${token.type} token`);
85
+ onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.type} token`);
74
86
  atNewline = false;
75
87
  hasSpace = false;
76
88
  }
77
- /* istanbul ignore else should not happen */
78
- if (token.source)
79
- length += token.source.length;
80
89
  }
90
+ const last = tokens[tokens.length - 1];
91
+ const end = last ? last.offset + last.source.length : offset;
81
92
  return {
93
+ comma,
82
94
  found,
83
95
  spaceBefore,
84
96
  comment,
85
97
  hasNewline,
86
98
  anchor,
87
- tagName,
88
- length,
89
- start: start !== null && start !== void 0 ? start : offset + length
99
+ tag,
100
+ end,
101
+ start: start !== null && start !== void 0 ? start : end
90
102
  };
91
103
  }
92
104
 
@@ -1,2 +1,2 @@
1
- import type { Token } from '../parse/tokens.js';
1
+ import type { Token } from '../parse/cst.js';
2
2
  export declare function containsNewline(key: Token | null | undefined): boolean | null;
@@ -8,21 +8,24 @@ function containsNewline(key) {
8
8
  case 'scalar':
9
9
  case 'double-quoted-scalar':
10
10
  case 'single-quoted-scalar':
11
- return key.source.includes('\n');
11
+ if (key.source.includes('\n'))
12
+ return true;
13
+ if (key.end)
14
+ for (const st of key.end)
15
+ if (st.type === 'newline')
16
+ return true;
17
+ return false;
12
18
  case 'flow-collection':
13
- for (const token of key.items) {
14
- switch (token.type) {
15
- case 'newline':
19
+ for (const it of key.items) {
20
+ for (const st of it.start)
21
+ if (st.type === 'newline')
16
22
  return true;
17
- case 'alias':
18
- case 'scalar':
19
- case 'double-quoted-scalar':
20
- case 'single-quoted-scalar':
21
- case 'flow-collection':
22
- if (containsNewline(token))
23
+ if (it.sep)
24
+ for (const st of it.sep)
25
+ if (st.type === 'newline')
23
26
  return true;
24
- break;
25
- }
27
+ if (containsNewline(it.key) || containsNewline(it.value))
28
+ return true;
26
29
  }
27
30
  return false;
28
31
  default:
@@ -1,2 +1,2 @@
1
- import type { Token } from '../parse/tokens.js';
1
+ import type { Token } from '../parse/cst.js';
2
2
  export declare function emptyScalarPosition(offset: number, before: Token[] | undefined, pos: number | null): number;
@@ -1,25 +1,21 @@
1
1
  import type { YAMLError, YAMLWarning } from '../errors.js';
2
- import { Node, NODE_TYPE, ParsedNode } from '../nodes/Node.js';
2
+ import { Alias } from '../nodes/Alias.js';
3
+ import { Node, NODE_TYPE, ParsedNode, Range } from '../nodes/Node.js';
3
4
  import { Pair } from '../nodes/Pair.js';
4
- import { CreateNodeOptions, DocumentOptions, Options, ParseOptions, SchemaOptions, ToJSOptions, ToStringOptions } from '../options.js';
5
- import { Anchors } from './Anchors.js';
6
- import { Schema } from './Schema.js';
5
+ import type { Scalar } from '../nodes/Scalar.js';
6
+ import type { YAMLMap } from '../nodes/YAMLMap.js';
7
+ import type { YAMLSeq } from '../nodes/YAMLSeq.js';
8
+ import { CreateNodeOptions, DocumentOptions, ParseOptions, SchemaOptions, ToJSOptions, ToStringOptions } from '../options.js';
9
+ import { Schema } from '../schema/Schema.js';
7
10
  import { Directives } from './directives.js';
8
11
  export declare type Replacer = any[] | ((key: any, value: any) => unknown);
9
12
  export declare namespace Document {
10
13
  interface Parsed<T extends ParsedNode = ParsedNode> extends Document<T> {
11
- range: [number, number];
12
- /** The schema used with the document. */
13
- schema: Schema;
14
+ range: Range;
14
15
  }
15
16
  }
16
17
  export declare class Document<T = unknown> {
17
18
  readonly [NODE_TYPE]: symbol;
18
- /**
19
- * Anchors associated with the document's nodes;
20
- * also provides alias & merge node creators.
21
- */
22
- anchors: Anchors;
23
19
  /** A comment before this Document */
24
20
  commentBefore: string | null;
25
21
  /** A comment immediately after this Document */
@@ -30,6 +26,11 @@ export declare class Document<T = unknown> {
30
26
  /** Errors encountered during parsing. */
31
27
  errors: YAMLError[];
32
28
  options: Required<Omit<ParseOptions & DocumentOptions, 'lineCounter' | 'directives' | 'version'>>;
29
+ /**
30
+ * The [start, value-end, node-end] character offsets for the part of the
31
+ * source parsed into this document (undefined if not parsed).
32
+ */
33
+ range?: Range;
33
34
  /** The schema used with the document. Use `setSchema()` to change. */
34
35
  schema: Schema;
35
36
  /** Warnings encountered during parsing. */
@@ -38,17 +39,28 @@ export declare class Document<T = unknown> {
38
39
  * @param value - The initial value for the document, which will be wrapped
39
40
  * in a Node container.
40
41
  */
41
- constructor(value?: any, options?: Options);
42
- constructor(value: any, replacer: null | Replacer, options?: Options);
42
+ constructor(value?: any, options?: DocumentOptions & SchemaOptions & ParseOptions & CreateNodeOptions);
43
+ constructor(value: any, replacer: null | Replacer, options?: DocumentOptions & SchemaOptions & ParseOptions & CreateNodeOptions);
43
44
  /** Adds a value to the document. */
44
45
  add(value: any): void;
45
46
  /** Adds a value to the document. */
46
47
  addIn(path: Iterable<unknown>, value: unknown): void;
48
+ /**
49
+ * Create a new `Alias` node, ensuring that the target `node` has the required anchor.
50
+ *
51
+ * If `node` already has an anchor, `name` is ignored.
52
+ * Otherwise, the `node.anchor` value will be set to `name`,
53
+ * or if an anchor with that name is already present in the document,
54
+ * `name` will be used as a prefix for a new unique anchor.
55
+ * If `name` is undefined, the generated anchor will use 'a' as a prefix.
56
+ */
57
+ createAlias(node: Scalar | YAMLMap | YAMLSeq, name?: string): Alias;
47
58
  /**
48
59
  * Convert any value into a `Node` using the current schema, recursively
49
60
  * turning objects into collections.
50
61
  */
51
- createNode(value: unknown, { flow, keepUndefined, onTagObj, replacer, tag }?: CreateNodeOptions): Node;
62
+ createNode(value: unknown, options?: CreateNodeOptions): Node;
63
+ createNode(value: unknown, replacer: Replacer | CreateNodeOptions | null, options?: CreateNodeOptions): Node;
52
64
  /**
53
65
  * Convert a key and a value into a `Pair` using the current schema,
54
66
  * recursively wrapping all values as `Scalar` or `Collection` nodes.