yaml 2.0.0-4 → 2.0.0-8

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 (220) 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 +27 -17
  5. package/browser/dist/compose/compose-scalar.js +21 -23
  6. package/browser/dist/compose/composer.js +108 -78
  7. package/browser/dist/compose/resolve-block-map.js +41 -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 +7 -8
  11. package/browser/dist/compose/resolve-flow-collection.js +158 -199
  12. package/browser/dist/compose/resolve-flow-scalar.js +62 -26
  13. package/browser/dist/compose/resolve-props.js +72 -37
  14. package/browser/dist/compose/util-contains-newline.js +15 -12
  15. package/browser/dist/compose/util-map-includes.js +17 -0
  16. package/browser/dist/doc/Document.js +74 -99
  17. package/browser/dist/doc/anchors.js +72 -0
  18. package/browser/dist/doc/createNode.js +35 -19
  19. package/browser/dist/doc/directives.js +5 -0
  20. package/browser/dist/errors.js +16 -12
  21. package/browser/dist/index.js +3 -3
  22. package/browser/dist/nodes/Alias.js +51 -28
  23. package/browser/dist/nodes/Collection.js +23 -92
  24. package/browser/dist/nodes/Node.js +9 -1
  25. package/browser/dist/nodes/Pair.js +16 -231
  26. package/browser/dist/nodes/YAMLMap.js +8 -4
  27. package/browser/dist/nodes/YAMLSeq.js +7 -4
  28. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  29. package/browser/dist/nodes/toJS.js +12 -10
  30. package/browser/dist/options.js +1 -2
  31. package/browser/dist/parse/cst-scalar.js +219 -0
  32. package/browser/dist/parse/cst-stringify.js +61 -0
  33. package/browser/dist/parse/cst-visit.js +97 -0
  34. package/{dist/parse/tokens.js → browser/dist/parse/cst.js} +12 -7
  35. package/browser/dist/parse/lexer.js +149 -125
  36. package/browser/dist/parse/parser.js +272 -196
  37. package/browser/dist/public-api.js +11 -14
  38. package/browser/dist/schema/Schema.js +28 -0
  39. package/browser/dist/{tags/failsafe → schema/common}/map.js +0 -0
  40. package/browser/dist/schema/common/null.js +13 -0
  41. package/browser/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  42. package/browser/dist/{tags/failsafe → schema/common}/string.js +0 -0
  43. package/browser/dist/schema/core/bool.js +19 -0
  44. package/browser/dist/schema/core/float.js +40 -0
  45. package/browser/dist/schema/core/int.js +38 -0
  46. package/browser/dist/schema/core/schema.js +23 -0
  47. package/browser/dist/{tags/json.js → schema/json/schema.js} +5 -6
  48. package/browser/dist/schema/tags.js +79 -0
  49. package/browser/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  50. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  51. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  52. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  53. package/browser/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  54. package/browser/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  55. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  56. package/browser/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  57. package/browser/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  58. package/browser/dist/stringify/stringify.js +8 -6
  59. package/browser/dist/stringify/stringifyCollection.js +122 -0
  60. package/browser/dist/stringify/stringifyComment.js +14 -0
  61. package/browser/dist/stringify/stringifyDocument.js +64 -0
  62. package/browser/dist/stringify/stringifyPair.js +111 -0
  63. package/browser/dist/stringify/stringifyString.js +53 -52
  64. package/browser/dist/visit.js +20 -1
  65. package/dist/compose/compose-collection.d.ts +4 -4
  66. package/dist/compose/compose-collection.js +12 -9
  67. package/dist/compose/compose-doc.d.ts +4 -3
  68. package/dist/compose/compose-doc.js +23 -6
  69. package/dist/compose/compose-node.d.ts +15 -7
  70. package/dist/compose/compose-node.js +27 -17
  71. package/dist/compose/compose-scalar.d.ts +4 -3
  72. package/dist/compose/compose-scalar.js +20 -22
  73. package/dist/compose/composer.d.ts +28 -23
  74. package/dist/compose/composer.js +109 -79
  75. package/dist/compose/resolve-block-map.d.ts +5 -4
  76. package/dist/compose/resolve-block-map.js +41 -29
  77. package/dist/compose/resolve-block-scalar.d.ts +5 -3
  78. package/dist/compose/resolve-block-scalar.js +14 -17
  79. package/dist/compose/resolve-block-seq.d.ts +4 -4
  80. package/dist/compose/resolve-block-seq.js +21 -17
  81. package/dist/compose/resolve-end.d.ts +3 -2
  82. package/dist/compose/resolve-end.js +7 -8
  83. package/dist/compose/resolve-flow-collection.d.ts +4 -5
  84. package/dist/compose/resolve-flow-collection.js +157 -198
  85. package/dist/compose/resolve-flow-scalar.d.ts +5 -3
  86. package/dist/compose/resolve-flow-scalar.js +62 -26
  87. package/dist/compose/resolve-props.d.ts +16 -10
  88. package/dist/compose/resolve-props.js +72 -37
  89. package/dist/compose/util-contains-newline.d.ts +1 -1
  90. package/dist/compose/util-contains-newline.js +15 -12
  91. package/dist/compose/util-empty-scalar-position.d.ts +1 -1
  92. package/dist/compose/util-map-includes.d.ts +4 -0
  93. package/dist/compose/util-map-includes.js +19 -0
  94. package/dist/doc/Document.d.ts +35 -15
  95. package/dist/doc/Document.js +72 -97
  96. package/dist/doc/anchors.d.ts +24 -0
  97. package/dist/doc/anchors.js +77 -0
  98. package/dist/doc/createNode.d.ts +10 -11
  99. package/dist/doc/createNode.js +34 -18
  100. package/dist/doc/directives.d.ts +1 -0
  101. package/dist/doc/directives.js +5 -0
  102. package/dist/errors.d.ts +11 -8
  103. package/dist/errors.js +16 -12
  104. package/dist/index.d.ts +5 -3
  105. package/dist/index.js +3 -3
  106. package/dist/nodes/Alias.d.ts +19 -9
  107. package/dist/nodes/Alias.js +51 -28
  108. package/dist/nodes/Collection.d.ts +9 -17
  109. package/dist/nodes/Collection.js +22 -91
  110. package/dist/nodes/Node.d.ts +10 -4
  111. package/dist/nodes/Node.js +9 -0
  112. package/dist/nodes/Pair.d.ts +10 -13
  113. package/dist/nodes/Pair.js +15 -230
  114. package/dist/nodes/Scalar.d.ts +6 -3
  115. package/dist/nodes/YAMLMap.d.ts +3 -3
  116. package/dist/nodes/YAMLMap.js +8 -4
  117. package/dist/nodes/YAMLSeq.d.ts +3 -3
  118. package/dist/nodes/YAMLSeq.js +7 -4
  119. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  120. package/dist/nodes/addPairToJSMap.js +105 -0
  121. package/dist/nodes/toJS.d.ts +4 -5
  122. package/dist/nodes/toJS.js +12 -10
  123. package/dist/options.d.ts +41 -25
  124. package/dist/options.js +1 -2
  125. package/dist/parse/cst-scalar.d.ts +58 -0
  126. package/dist/parse/cst-scalar.js +223 -0
  127. package/dist/parse/cst-stringify.d.ts +8 -0
  128. package/dist/parse/cst-stringify.js +63 -0
  129. package/dist/parse/cst-visit.d.ts +39 -0
  130. package/dist/parse/cst-visit.js +99 -0
  131. package/dist/parse/{tokens.d.ts → cst.d.ts} +23 -7
  132. package/{browser/dist/parse/tokens.js → dist/parse/cst.js} +27 -1
  133. package/dist/parse/lexer.d.ts +7 -14
  134. package/dist/parse/lexer.js +150 -126
  135. package/dist/parse/parser.d.ts +31 -24
  136. package/dist/parse/parser.js +274 -198
  137. package/dist/public-api.d.ts +7 -7
  138. package/dist/public-api.js +11 -14
  139. package/dist/{doc → schema}/Schema.d.ts +6 -3
  140. package/dist/schema/Schema.js +30 -0
  141. package/dist/{tags/failsafe → schema/common}/map.d.ts +0 -0
  142. package/dist/{tags/failsafe → schema/common}/map.js +0 -0
  143. package/dist/schema/common/null.d.ts +4 -0
  144. package/dist/schema/common/null.js +15 -0
  145. package/dist/{tags/failsafe → schema/common}/seq.d.ts +0 -0
  146. package/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  147. package/dist/{tags/failsafe → schema/common}/string.d.ts +0 -0
  148. package/dist/{tags/failsafe → schema/common}/string.js +0 -0
  149. package/dist/schema/core/bool.d.ts +4 -0
  150. package/dist/schema/core/bool.js +21 -0
  151. package/dist/schema/core/float.d.ts +4 -0
  152. package/dist/schema/core/float.js +44 -0
  153. package/dist/schema/core/int.d.ts +4 -0
  154. package/dist/schema/core/int.js +42 -0
  155. package/dist/schema/core/schema.d.ts +1 -0
  156. package/dist/schema/core/schema.js +25 -0
  157. package/dist/schema/json/schema.d.ts +2 -0
  158. package/dist/{tags/json.js → schema/json/schema.js} +5 -6
  159. package/dist/schema/tags.d.ts +41 -0
  160. package/dist/schema/tags.js +82 -0
  161. package/dist/{tags → schema}/types.d.ts +3 -7
  162. package/dist/{tags → schema}/yaml-1.1/binary.d.ts +0 -0
  163. package/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  164. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  165. package/dist/schema/yaml-1.1/bool.js +29 -0
  166. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  167. package/dist/schema/yaml-1.1/float.js +47 -0
  168. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  169. package/dist/schema/yaml-1.1/int.js +76 -0
  170. package/dist/{tags → schema}/yaml-1.1/omap.d.ts +0 -0
  171. package/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  172. package/dist/{tags → schema}/yaml-1.1/pairs.d.ts +3 -2
  173. package/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  174. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  175. package/dist/schema/yaml-1.1/schema.js +39 -0
  176. package/dist/{tags → schema}/yaml-1.1/set.d.ts +1 -1
  177. package/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  178. package/dist/{tags → schema}/yaml-1.1/timestamp.d.ts +0 -0
  179. package/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  180. package/dist/stringify/stringify.d.ts +1 -2
  181. package/dist/stringify/stringify.js +8 -6
  182. package/dist/stringify/stringifyCollection.d.ts +21 -0
  183. package/dist/stringify/stringifyCollection.js +124 -0
  184. package/dist/stringify/stringifyComment.d.ts +2 -0
  185. package/dist/stringify/stringifyComment.js +17 -0
  186. package/dist/stringify/stringifyDocument.d.ts +3 -0
  187. package/dist/stringify/stringifyDocument.js +66 -0
  188. package/dist/stringify/stringifyPair.d.ts +3 -0
  189. package/dist/stringify/stringifyPair.js +113 -0
  190. package/dist/stringify/stringifyString.js +53 -52
  191. package/dist/test-events.d.ts +1 -2
  192. package/dist/test-events.js +25 -12
  193. package/dist/util.d.ts +1 -1
  194. package/dist/visit.d.ts +10 -1
  195. package/dist/visit.js +20 -1
  196. package/package.json +9 -8
  197. package/browser/dist/doc/Anchors.js +0 -102
  198. package/browser/dist/doc/Schema.js +0 -27
  199. package/browser/dist/doc/getSchemaTags.js +0 -30
  200. package/browser/dist/stringify/addComment.js +0 -17
  201. package/browser/dist/tags/core.js +0 -109
  202. package/browser/dist/tags/failsafe/index.js +0 -7
  203. package/browser/dist/tags/index.js +0 -34
  204. package/browser/dist/tags/yaml-1.1/index.js +0 -152
  205. package/dist/doc/Anchors.d.ts +0 -36
  206. package/dist/doc/Anchors.js +0 -104
  207. package/dist/doc/Schema.js +0 -29
  208. package/dist/doc/getSchemaTags.d.ts +0 -3
  209. package/dist/doc/getSchemaTags.js +0 -32
  210. package/dist/stringify/addComment.d.ts +0 -2
  211. package/dist/stringify/addComment.js +0 -20
  212. package/dist/tags/core.d.ts +0 -14
  213. package/dist/tags/core.js +0 -119
  214. package/dist/tags/failsafe/index.d.ts +0 -1
  215. package/dist/tags/failsafe/index.js +0 -9
  216. package/dist/tags/index.d.ts +0 -31
  217. package/dist/tags/index.js +0 -37
  218. package/dist/tags/json.d.ts +0 -2
  219. package/dist/tags/yaml-1.1/index.d.ts +0 -2
  220. package/dist/tags/yaml-1.1/index.js +0 -154
@@ -3,9 +3,10 @@
3
3
  var Scalar = require('../nodes/Scalar.js');
4
4
 
5
5
  function resolveBlockScalar(scalar, strict, onError) {
6
+ const start = scalar.offset;
6
7
  const header = parseBlockScalarHeader(scalar, strict, onError);
7
8
  if (!header)
8
- return { value: '', type: null, comment: '', length: 0 };
9
+ return { value: '', type: null, comment: '', range: [start, start, start] };
9
10
  const type = header.mode === '>' ? Scalar.Scalar.BLOCK_FOLDED : Scalar.Scalar.BLOCK_LITERAL;
10
11
  const lines = scalar.source ? splitLines(scalar.source) : [];
11
12
  // determine the end of content & start of chomping
@@ -20,10 +21,10 @@ function resolveBlockScalar(scalar, strict, onError) {
20
21
  // shortcut for empty contents
21
22
  if (!scalar.source || chompStart === 0) {
22
23
  const value = header.chomp === '+' ? lines.map(line => line[0]).join('\n') : '';
23
- let length = header.length;
24
+ let end = start + header.length;
24
25
  if (scalar.source)
25
- length += scalar.source.length;
26
- return { value, type, comment: header.comment, length };
26
+ end += scalar.source.length;
27
+ return { value, type, comment: header.comment, range: [start, end, end] };
27
28
  }
28
29
  // find the indentation level to trim from start
29
30
  let trimIndent = scalar.indent + header.indent;
@@ -38,7 +39,7 @@ function resolveBlockScalar(scalar, strict, onError) {
38
39
  else {
39
40
  if (indent.length < trimIndent) {
40
41
  const message = 'Block scalars with more-indented leading empty lines must use an explicit indentation indicator';
41
- onError(offset + indent.length, message);
42
+ onError(offset + indent.length, 'MISSING_CHAR', message);
42
43
  }
43
44
  if (header.indent === 0)
44
45
  trimIndent = indent.length;
@@ -65,7 +66,7 @@ function resolveBlockScalar(scalar, strict, onError) {
65
66
  ? 'explicit indentation indicator'
66
67
  : 'first line';
67
68
  const message = `Block scalar lines must not be less indented than their ${src}`;
68
- onError(offset - content.length - (crlf ? 2 : 1), message);
69
+ onError(offset - content.length - (crlf ? 2 : 1), 'BAD_INDENT', message);
69
70
  indent = '';
70
71
  }
71
72
  if (type === Scalar.Scalar.BLOCK_LITERAL) {
@@ -107,17 +108,13 @@ function resolveBlockScalar(scalar, strict, onError) {
107
108
  default:
108
109
  value += '\n';
109
110
  }
110
- return {
111
- value,
112
- type,
113
- comment: header.comment,
114
- length: header.length + scalar.source.length
115
- };
111
+ const end = start + header.length + scalar.source.length;
112
+ return { value, type, comment: header.comment, range: [start, end, end] };
116
113
  }
117
114
  function parseBlockScalarHeader({ offset, props }, strict, onError) {
118
115
  /* istanbul ignore if should not happen */
119
116
  if (props[0].type !== 'block-scalar-header') {
120
- onError(offset, 'Block scalar header not found');
117
+ onError(props[0], 'IMPOSSIBLE', 'Block scalar header not found');
121
118
  return null;
122
119
  }
123
120
  const { source } = props[0];
@@ -138,7 +135,7 @@ function parseBlockScalarHeader({ offset, props }, strict, onError) {
138
135
  }
139
136
  }
140
137
  if (error !== -1)
141
- onError(error, `Block scalar header includes extra characters: ${source}`);
138
+ onError(error, 'UNEXPECTED_TOKEN', `Block scalar header includes extra characters: ${source}`);
142
139
  let hasSpace = false;
143
140
  let comment = '';
144
141
  let length = source.length;
@@ -154,19 +151,19 @@ function parseBlockScalarHeader({ offset, props }, strict, onError) {
154
151
  case 'comment':
155
152
  if (strict && !hasSpace) {
156
153
  const message = 'Comments must be separated from other tokens by white space characters';
157
- onError(offset + length, message);
154
+ onError(token, 'MISSING_CHAR', message);
158
155
  }
159
156
  length += token.source.length;
160
157
  comment = token.source.substring(1);
161
158
  break;
162
159
  case 'error':
163
- onError(offset + length, token.message);
160
+ onError(token, 'UNEXPECTED_TOKEN', token.message);
164
161
  length += token.source.length;
165
162
  break;
166
163
  /* istanbul ignore next should not happen */
167
164
  default: {
168
165
  const message = `Unexpected token in block scalar header: ${token.type}`;
169
- onError(offset + length, message);
166
+ onError(token, 'UNEXPECTED_TOKEN', message);
170
167
  const ts = token.source;
171
168
  if (ts && typeof ts === 'string')
172
169
  length += ts.length;
@@ -1,5 +1,5 @@
1
- import type { Document } from '../doc/Document.js';
2
1
  import { YAMLSeq } from '../nodes/YAMLSeq.js';
3
- import type { BlockSequence } from '../parse/tokens.js';
4
- import type { ComposeNode } from './compose-node.js';
5
- export declare function resolveBlockSeq({ composeNode, composeEmptyNode }: ComposeNode, doc: Document.Parsed, { items, offset }: BlockSequence, anchor: string | null, onError: (offset: number, message: string, warning?: boolean) => void): YAMLSeq.Parsed<import("../index.js").ParsedNode>;
2
+ import type { BlockSequence } from '../parse/cst.js';
3
+ import type { ComposeContext, ComposeNode } from './compose-node.js';
4
+ import type { ComposeErrorHandler } from './composer.js';
5
+ export declare function resolveBlockSeq({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bs: BlockSequence, onError: ComposeErrorHandler): YAMLSeq.Parsed<import("../index.js").ParsedNode>;
@@ -3,20 +3,24 @@
3
3
  var YAMLSeq = require('../nodes/YAMLSeq.js');
4
4
  var resolveProps = require('./resolve-props.js');
5
5
 
6
- function resolveBlockSeq({ composeNode, composeEmptyNode }, doc, { items, offset }, anchor, onError) {
7
- const start = offset;
8
- const seq = new YAMLSeq.YAMLSeq(doc.schema);
9
- if (anchor)
10
- doc.anchors.setAnchor(seq, anchor);
11
- for (const { start, value } of items) {
12
- const props = resolveProps.resolveProps(doc, start, true, 'seq-item-ind', offset, onError);
13
- offset += props.length;
6
+ function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError) {
7
+ const seq = new YAMLSeq.YAMLSeq(ctx.schema);
8
+ let offset = bs.offset;
9
+ for (const { start, value } of bs.items) {
10
+ const props = resolveProps.resolveProps(start, {
11
+ indicator: 'seq-item-ind',
12
+ next: value,
13
+ offset,
14
+ onError,
15
+ startOnNewline: true
16
+ });
17
+ offset = props.end;
14
18
  if (!props.found) {
15
- if (props.anchor || props.tagName || value) {
16
- const msg = value && value.type === 'block-seq'
17
- ? 'All sequence items must start at the same column'
18
- : 'Sequence item without - indicator';
19
- onError(offset, msg);
19
+ if (props.anchor || props.tag || value) {
20
+ if (value && value.type === 'block-seq')
21
+ onError(offset, 'BAD_INDENT', 'All sequence items must start at the same column');
22
+ else
23
+ onError(offset, 'MISSING_CHAR', 'Sequence item without - indicator');
20
24
  }
21
25
  else {
22
26
  // TODO: assert being at last item?
@@ -26,12 +30,12 @@ function resolveBlockSeq({ composeNode, composeEmptyNode }, doc, { items, offset
26
30
  }
27
31
  }
28
32
  const node = value
29
- ? composeNode(doc, value, props, onError)
30
- : composeEmptyNode(doc, offset, start, null, props, onError);
31
- offset = node.range[1];
33
+ ? composeNode(ctx, value, props, onError)
34
+ : composeEmptyNode(ctx, offset, start, null, props, onError);
35
+ offset = node.range[2];
32
36
  seq.items.push(node);
33
37
  }
34
- seq.range = [start, offset];
38
+ seq.range = [bs.offset, offset, offset];
35
39
  return seq;
36
40
  }
37
41
 
@@ -1,5 +1,6 @@
1
- import type { SourceToken } from '../parse/tokens.js';
2
- export declare function resolveEnd(end: SourceToken[] | undefined, offset: number, reqSpace: boolean, onError: (offset: number, message: string) => void): {
1
+ import type { SourceToken } from '../parse/cst.js';
2
+ import type { ComposeErrorHandler } from './composer.js';
3
+ export declare function resolveEnd(end: SourceToken[] | undefined, offset: number, reqSpace: boolean, onError: ComposeErrorHandler): {
3
4
  comment: string;
4
5
  offset: number;
5
6
  };
@@ -4,32 +4,31 @@ function resolveEnd(end, offset, reqSpace, onError) {
4
4
  let comment = '';
5
5
  if (end) {
6
6
  let hasSpace = false;
7
- let hasComment = false;
8
7
  let sep = '';
9
- for (const { source, type } of end) {
8
+ for (const token of end) {
9
+ const { source, type } = token;
10
10
  switch (type) {
11
11
  case 'space':
12
12
  hasSpace = true;
13
13
  break;
14
14
  case 'comment': {
15
15
  if (reqSpace && !hasSpace)
16
- onError(offset, 'Comments must be separated from other tokens by white space characters');
17
- const cb = source.substring(1);
18
- if (!hasComment)
16
+ onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
17
+ const cb = source.substring(1) || ' ';
18
+ if (!comment)
19
19
  comment = cb;
20
20
  else
21
21
  comment += sep + cb;
22
- hasComment = true;
23
22
  sep = '';
24
23
  break;
25
24
  }
26
25
  case 'newline':
27
- if (hasComment)
26
+ if (comment)
28
27
  sep += source;
29
28
  hasSpace = true;
30
29
  break;
31
30
  default:
32
- onError(offset, `Unexpected ${type} at node end`);
31
+ onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${type} at node end`);
33
32
  }
34
33
  offset += source.length;
35
34
  }
@@ -1,7 +1,6 @@
1
- import type { Document } from '../doc/Document.js';
2
- import { ParsedNode } from '../nodes/Node.js';
3
1
  import { YAMLMap } from '../nodes/YAMLMap.js';
4
2
  import { YAMLSeq } from '../nodes/YAMLSeq.js';
5
- import type { FlowCollection } from '../parse/tokens.js';
6
- import type { ComposeNode } from './compose-node.js';
7
- export declare function resolveFlowCollection({ composeNode, composeEmptyNode }: ComposeNode, doc: Document.Parsed, fc: FlowCollection, _anchor: string | null, onError: (offset: number, message: string, warning?: boolean) => void): YAMLMap.Parsed<ParsedNode, ParsedNode | null> | YAMLSeq.Parsed<ParsedNode>;
3
+ import type { FlowCollection } from '../parse/cst.js';
4
+ import type { ComposeContext, ComposeNode } from './compose-node.js';
5
+ import type { ComposeErrorHandler } from './composer.js';
6
+ export declare function resolveFlowCollection({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, fc: FlowCollection, onError: ComposeErrorHandler): YAMLMap.Parsed<import("../nodes/Node.js").ParsedNode, import("../nodes/Node.js").ParsedNode | null> | YAMLSeq.Parsed<import("../nodes/Node.js").ParsedNode>;
@@ -5,227 +5,186 @@ var Pair = require('../nodes/Pair.js');
5
5
  var YAMLMap = require('../nodes/YAMLMap.js');
6
6
  var YAMLSeq = require('../nodes/YAMLSeq.js');
7
7
  var resolveEnd = require('./resolve-end.js');
8
+ var resolveProps = require('./resolve-props.js');
8
9
  var utilContainsNewline = require('./util-contains-newline.js');
10
+ var utilMapIncludes = require('./util-map-includes.js');
9
11
 
10
- function resolveFlowCollection({ composeNode, composeEmptyNode }, doc, fc, _anchor, onError) {
12
+ const blockMsg = 'Block collections are not allowed within flow collections';
13
+ const isBlock = (token) => token && (token.type === 'block-map' || token.type === 'block-seq');
14
+ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError) {
11
15
  const isMap = fc.start.source === '{';
12
- const coll = isMap ? new YAMLMap.YAMLMap(doc.schema) : new YAMLSeq.YAMLSeq(doc.schema);
16
+ const fcName = isMap ? 'flow map' : 'flow sequence';
17
+ const coll = isMap
18
+ ? new YAMLMap.YAMLMap(ctx.schema)
19
+ : new YAMLSeq.YAMLSeq(ctx.schema);
13
20
  coll.flow = true;
14
- if (_anchor)
15
- doc.anchors.setAnchor(coll, _anchor);
16
- let key = null;
17
- let value = null;
18
- let spaceBefore = false;
19
- let comment = '';
20
- let hasSpace = false;
21
- let hasComment = false;
22
- let newlines = '';
23
- let anchor = '';
24
- let tagName = '';
25
- let offset = fc.offset + 1;
26
- let atLineStart = false;
27
- let atExplicitKey = false;
28
- let atValueEnd = false;
29
- let nlAfterValueInSeq = false;
30
- let seqKeyToken = null;
31
- function getProps() {
32
- const props = { spaceBefore, comment, anchor, tagName };
33
- spaceBefore = false;
34
- comment = '';
35
- hasComment = false;
36
- newlines = '';
37
- anchor = '';
38
- tagName = '';
39
- return props;
40
- }
41
- function addItem(pos) {
42
- if (value) {
43
- if (hasComment)
44
- value.comment = comment;
45
- }
46
- else {
47
- value = composeEmptyNode(doc, offset, fc.items, pos, getProps(), onError);
21
+ let offset = fc.offset;
22
+ for (let i = 0; i < fc.items.length; ++i) {
23
+ const { start, key, sep, value } = fc.items[i];
24
+ const props = resolveProps.resolveProps(start, {
25
+ flow: fcName,
26
+ indicator: 'explicit-key-ind',
27
+ next: key || (sep === null || sep === void 0 ? void 0 : sep[0]),
28
+ offset,
29
+ onError,
30
+ startOnNewline: false
31
+ });
32
+ if (!props.found) {
33
+ if (!props.anchor && !props.tag && !sep && !value) {
34
+ if (i === 0 && props.comma)
35
+ onError(props.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${fcName}`);
36
+ else if (i < fc.items.length - 1)
37
+ onError(props.start, 'UNEXPECTED_TOKEN', `Unexpected empty item in ${fcName}`);
38
+ if (props.comment) {
39
+ if (coll.comment)
40
+ coll.comment += '\n' + props.comment;
41
+ else
42
+ coll.comment = props.comment;
43
+ }
44
+ continue;
45
+ }
46
+ if (!isMap && ctx.options.strict && utilContainsNewline.containsNewline(key))
47
+ onError(key, // checked by containsNewline()
48
+ 'MULTILINE_IMPLICIT_KEY', 'Implicit keys of flow sequence pairs need to be on a single line');
48
49
  }
49
- if (isMap || atExplicitKey) {
50
- coll.items.push(key ? new Pair.Pair(key, value) : new Pair.Pair(value));
50
+ if (i === 0) {
51
+ if (props.comma)
52
+ onError(props.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${fcName}`);
51
53
  }
52
54
  else {
53
- const seq = coll;
54
- if (key) {
55
- const map = new YAMLMap.YAMLMap(doc.schema);
56
- map.flow = true;
57
- map.items.push(new Pair.Pair(key, value));
58
- seq.items.push(map);
59
- }
60
- else
61
- seq.items.push(value);
62
- }
63
- }
64
- for (let i = 0; i < fc.items.length; ++i) {
65
- const token = fc.items[i];
66
- let isSourceToken = true;
67
- switch (token.type) {
68
- case 'space':
69
- hasSpace = true;
70
- break;
71
- case 'comment': {
72
- if (doc.options.strict && !hasSpace)
73
- onError(offset, 'Comments must be separated from other tokens by white space characters');
74
- const cb = token.source.substring(1);
75
- if (!hasComment)
76
- comment = cb;
77
- else
78
- comment += newlines + cb;
79
- atLineStart = false;
80
- hasComment = true;
81
- newlines = '';
82
- break;
83
- }
84
- case 'newline':
85
- if (atLineStart && !hasComment)
86
- spaceBefore = true;
87
- if (atValueEnd) {
88
- if (hasComment) {
89
- let node = coll.items[coll.items.length - 1];
90
- if (Node.isPair(node))
91
- node = node.value || node.key;
92
- /* istanbul ignore else should not happen */
93
- if (Node.isNode(node))
94
- node.comment = comment;
95
- else
96
- onError(offset, 'Error adding trailing comment to node');
97
- comment = '';
98
- hasComment = false;
55
+ if (!props.comma)
56
+ onError(props.start, 'MISSING_CHAR', `Missing , between ${fcName} items`);
57
+ if (props.comment) {
58
+ let prevItemComment = '';
59
+ loop: for (const st of start) {
60
+ switch (st.type) {
61
+ case 'comma':
62
+ case 'space':
63
+ break;
64
+ case 'comment':
65
+ prevItemComment = st.source.substring(1);
66
+ break loop;
67
+ default:
68
+ break loop;
99
69
  }
100
- atValueEnd = false;
101
70
  }
102
- else {
103
- newlines += token.source;
104
- if (!isMap && !key && value)
105
- nlAfterValueInSeq = true;
71
+ if (prevItemComment) {
72
+ let prev = coll.items[coll.items.length - 1];
73
+ if (Node.isPair(prev))
74
+ prev = prev.value || prev.key;
75
+ if (prev.comment)
76
+ prev.comment += '\n' + prevItemComment;
77
+ else
78
+ prev.comment = prevItemComment;
79
+ props.comment = props.comment.substring(prevItemComment.length + 1);
106
80
  }
107
- atLineStart = true;
108
- hasSpace = true;
109
- break;
110
- case 'anchor':
111
- if (anchor)
112
- onError(offset, 'A node can have at most one anchor');
113
- anchor = token.source.substring(1);
114
- atLineStart = false;
115
- atValueEnd = false;
116
- hasSpace = false;
117
- break;
118
- case 'tag': {
119
- if (tagName)
120
- onError(offset, 'A node can have at most one tag');
121
- const tn = doc.directives.tagName(token.source, m => onError(offset, m));
122
- if (tn)
123
- tagName = tn;
124
- atLineStart = false;
125
- atValueEnd = false;
126
- hasSpace = false;
127
- break;
128
81
  }
129
- case 'explicit-key-ind':
130
- if (anchor || tagName)
131
- onError(offset, 'Anchors and tags must be after the ? indicator');
132
- atExplicitKey = true;
133
- atLineStart = false;
134
- atValueEnd = false;
135
- hasSpace = false;
136
- break;
137
- case 'map-value-ind': {
138
- if (key) {
139
- if (value) {
140
- onError(offset, 'Missing {} around pair used as mapping key');
141
- const map = new YAMLMap.YAMLMap(doc.schema);
142
- map.flow = true;
143
- map.items.push(new Pair.Pair(key, value));
144
- map.range = [key.range[0], value.range[1]];
145
- key = map;
146
- value = null;
147
- } // else explicit key
148
- }
149
- else if (value) {
150
- if (doc.options.strict) {
151
- const slMsg = 'Implicit keys of flow sequence pairs need to be on a single line';
152
- if (nlAfterValueInSeq)
153
- onError(offset, slMsg);
154
- else if (seqKeyToken) {
155
- if (utilContainsNewline.containsNewline(seqKeyToken))
156
- onError(offset, slMsg);
157
- const start = 'offset' in seqKeyToken && seqKeyToken.offset;
158
- if (typeof start === 'number' && start < offset - 1024)
159
- onError(offset, 'The : indicator must be at most 1024 chars after the start of an implicit flow sequence key');
160
- seqKeyToken = null;
82
+ }
83
+ if (!isMap && !sep && !props.found) {
84
+ // item is a value in a seq
85
+ // → key & sep are empty, start does not include ? or :
86
+ const valueNode = value
87
+ ? composeNode(ctx, value, props, onError)
88
+ : composeEmptyNode(ctx, props.end, sep, null, props, onError);
89
+ coll.items.push(valueNode);
90
+ offset = valueNode.range[2];
91
+ if (isBlock(value))
92
+ onError(valueNode.range, 'BLOCK_IN_FLOW', blockMsg);
93
+ }
94
+ else {
95
+ // item is a key+value pair
96
+ // key value
97
+ const keyStart = props.end;
98
+ const keyNode = key
99
+ ? composeNode(ctx, key, props, onError)
100
+ : composeEmptyNode(ctx, keyStart, start, null, props, onError);
101
+ if (isBlock(key))
102
+ onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
103
+ // value properties
104
+ const valueProps = resolveProps.resolveProps(sep || [], {
105
+ flow: fcName,
106
+ indicator: 'map-value-ind',
107
+ next: value,
108
+ offset: keyNode.range[2],
109
+ onError,
110
+ startOnNewline: false
111
+ });
112
+ if (valueProps.found) {
113
+ if (!isMap && !props.found && ctx.options.strict) {
114
+ if (sep)
115
+ for (const st of sep) {
116
+ if (st === valueProps.found)
117
+ break;
118
+ if (st.type === 'newline') {
119
+ onError(st, 'MULTILINE_IMPLICIT_KEY', 'Implicit keys of flow sequence pairs need to be on a single line');
120
+ break;
121
+ }
161
122
  }
162
- }
163
- key = value;
164
- value = null;
123
+ if (props.start < valueProps.found.offset - 1024)
124
+ onError(valueProps.found, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit flow sequence key');
165
125
  }
166
- else {
167
- key = composeEmptyNode(doc, offset, fc.items, i, getProps(), onError);
168
- }
169
- if (hasComment) {
170
- key.comment = comment;
171
- comment = '';
172
- hasComment = false;
173
- }
174
- atExplicitKey = false;
175
- atValueEnd = false;
176
- hasSpace = false;
177
- break;
178
126
  }
179
- case 'comma':
180
- if (key || value || anchor || tagName || atExplicitKey)
181
- addItem(i);
127
+ else if (value) {
128
+ if ('source' in value && value.source && value.source[0] === ':')
129
+ onError(value, 'MISSING_CHAR', `Missing space after : in ${fcName}`);
182
130
  else
183
- onError(offset, `Unexpected , in flow ${isMap ? 'map' : 'sequence'}`);
184
- key = null;
185
- value = null;
186
- atExplicitKey = false;
187
- atValueEnd = true;
188
- hasSpace = false;
189
- nlAfterValueInSeq = false;
190
- seqKeyToken = null;
191
- break;
192
- case 'block-map':
193
- case 'block-seq':
194
- onError(offset, 'Block collections are not allowed within flow collections');
195
- // fallthrough
196
- default: {
197
- if (value)
198
- onError(offset, 'Missing , between flow collection items');
199
- if (!isMap && !key && !atExplicitKey)
200
- seqKeyToken = token;
201
- value = composeNode(doc, token, getProps(), onError);
202
- offset = value.range[1];
203
- atLineStart = false;
204
- isSourceToken = false;
205
- atValueEnd = false;
206
- hasSpace = false;
131
+ onError(valueProps.start, 'MISSING_CHAR', `Missing , or : between ${fcName} items`);
207
132
  }
133
+ // value value
134
+ const valueNode = value
135
+ ? composeNode(ctx, value, valueProps, onError)
136
+ : valueProps.found
137
+ ? composeEmptyNode(ctx, valueProps.end, sep, null, valueProps, onError)
138
+ : null;
139
+ if (valueNode) {
140
+ if (isBlock(value))
141
+ onError(valueNode.range, 'BLOCK_IN_FLOW', blockMsg);
142
+ }
143
+ else if (valueProps.comment) {
144
+ if (keyNode.comment)
145
+ keyNode.comment += '\n' + valueProps.comment;
146
+ else
147
+ keyNode.comment = valueProps.comment;
148
+ }
149
+ const pair = new Pair.Pair(keyNode, valueNode);
150
+ if (isMap) {
151
+ const map = coll;
152
+ if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
153
+ onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
154
+ map.items.push(pair);
155
+ }
156
+ else {
157
+ const map = new YAMLMap.YAMLMap(ctx.schema);
158
+ map.flow = true;
159
+ map.items.push(pair);
160
+ coll.items.push(map);
161
+ }
162
+ offset = valueNode ? valueNode.range[2] : valueProps.end;
208
163
  }
209
- if (isSourceToken)
210
- offset += token.source.length;
211
164
  }
212
- if (key || value || anchor || tagName || atExplicitKey)
213
- addItem(fc.items.length);
214
165
  const expectedEnd = isMap ? '}' : ']';
215
166
  const [ce, ...ee] = fc.end;
216
- if (!ce || ce.source !== expectedEnd) {
217
- const cs = isMap ? 'map' : 'sequence';
218
- onError(offset, `Expected flow ${cs} to end with ${expectedEnd}`);
167
+ let cePos = offset;
168
+ if (ce && ce.source === expectedEnd)
169
+ cePos = ce.offset + ce.source.length;
170
+ else {
171
+ onError(offset + 1, 'MISSING_CHAR', `Expected ${fcName} to end with ${expectedEnd}`);
172
+ if (ce && ce.source.length !== 1)
173
+ ee.unshift(ce);
219
174
  }
220
- if (ce)
221
- offset += ce.source.length;
222
175
  if (ee.length > 0) {
223
- const end = resolveEnd.resolveEnd(ee, offset, doc.options.strict, onError);
224
- if (end.comment)
225
- coll.comment = comment;
226
- offset = end.offset;
176
+ const end = resolveEnd.resolveEnd(ee, cePos, ctx.options.strict, onError);
177
+ if (end.comment) {
178
+ if (coll.comment)
179
+ coll.comment += '\n' + end.comment;
180
+ else
181
+ coll.comment = end.comment;
182
+ }
183
+ coll.range = [fc.offset, cePos, end.offset];
184
+ }
185
+ else {
186
+ coll.range = [fc.offset, cePos, cePos];
227
187
  }
228
- coll.range = [fc.offset, offset];
229
188
  return coll;
230
189
  }
231
190
 
@@ -1,8 +1,10 @@
1
+ import { Range } from '../nodes/Node.js';
1
2
  import { Scalar } from '../nodes/Scalar.js';
2
- import type { FlowScalar } from '../parse/tokens.js';
3
- export declare function resolveFlowScalar({ offset, type, source, end }: FlowScalar, strict: boolean, onError: (offset: number, message: string) => void): {
3
+ import type { FlowScalar } from '../parse/cst.js';
4
+ import type { ComposeErrorHandler } from './composer.js';
5
+ export declare function resolveFlowScalar(scalar: FlowScalar, strict: boolean, onError: ComposeErrorHandler): {
4
6
  value: string;
5
7
  type: Scalar.PLAIN | Scalar.QUOTE_DOUBLE | Scalar.QUOTE_SINGLE | null;
6
8
  comment: string;
7
- length: number;
9
+ range: Range;
8
10
  };