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
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  var Scalar = require('../nodes/Scalar.js');
4
- var addComment = require('./addComment.js');
5
4
  var foldFlowLines = require('./foldFlowLines.js');
6
5
 
7
6
  const getFoldOptions = (ctx) => ({
@@ -144,70 +143,80 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
144
143
  }
145
144
  const indent = ctx.indent ||
146
145
  (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
147
- const indentSize = indent ? '2' : '1'; // root is at -1
148
146
  const literal = type === Scalar.Scalar.BLOCK_FOLDED
149
147
  ? false
150
148
  : type === Scalar.Scalar.BLOCK_LITERAL
151
149
  ? true
152
150
  : !lineLengthOverLimit(value, ctx.options.lineWidth, indent.length);
153
- let header = literal ? '|' : '>';
154
151
  if (!value)
155
- return header + '\n';
156
- let wsStart = '';
157
- let wsEnd = '';
158
- value = value
159
- .replace(/[\n\t ]*$/, ws => {
160
- const n = ws.indexOf('\n');
161
- if (n === -1) {
162
- header += '-'; // strip
163
- }
164
- else if (value === ws || n !== ws.length - 1) {
165
- header += '+'; // keep
166
- if (onChompKeep)
167
- onChompKeep();
168
- }
169
- wsEnd = ws.replace(/\n$/, '');
170
- return '';
171
- })
172
- .replace(/^[\n ]*/, ws => {
173
- if (ws.indexOf(' ') !== -1)
174
- header += indentSize;
175
- const m = ws.match(/ +$/);
176
- if (m) {
177
- wsStart = ws.slice(0, -m[0].length);
178
- return m[0];
179
- }
180
- else {
181
- wsStart = ws;
182
- return '';
183
- }
184
- });
185
- if (wsEnd)
186
- wsEnd = wsEnd.replace(/\n+(?!\n|$)/g, `$&${indent}`);
187
- if (wsStart)
188
- wsStart = wsStart.replace(/\n+/g, `$&${indent}`);
152
+ return literal ? '|\n' : '>\n';
153
+ // determine chomping from whitespace at value end
154
+ let chomp;
155
+ let endStart;
156
+ for (endStart = value.length; endStart > 0; --endStart) {
157
+ const ch = value[endStart - 1];
158
+ if (ch !== '\n' && ch !== '\t' && ch !== ' ')
159
+ break;
160
+ }
161
+ let end = value.substring(endStart);
162
+ const endNlPos = end.indexOf('\n');
163
+ if (endNlPos === -1) {
164
+ chomp = '-'; // strip
165
+ }
166
+ else if (value === end || endNlPos !== end.length - 1) {
167
+ chomp = '+'; // keep
168
+ if (onChompKeep)
169
+ onChompKeep();
170
+ }
171
+ else {
172
+ chomp = ''; // clip
173
+ }
174
+ if (end) {
175
+ value = value.slice(0, -end.length);
176
+ if (end[end.length - 1] === '\n')
177
+ end = end.slice(0, -1);
178
+ end = end.replace(/\n+(?!\n|$)/g, `$&${indent}`);
179
+ }
180
+ // determine indent indicator from whitespace at value start
181
+ let startWithSpace = false;
182
+ let startEnd;
183
+ let startNlPos = -1;
184
+ for (startEnd = 0; startEnd < value.length; ++startEnd) {
185
+ const ch = value[startEnd];
186
+ if (ch === ' ')
187
+ startWithSpace = true;
188
+ else if (ch === '\n')
189
+ startNlPos = startEnd;
190
+ else
191
+ break;
192
+ }
193
+ let start = value.substring(0, startNlPos < startEnd ? startNlPos + 1 : startEnd);
194
+ if (start) {
195
+ value = value.substring(start.length);
196
+ start = start.replace(/\n+/g, `$&${indent}`);
197
+ }
198
+ const indentSize = indent ? '2' : '1'; // root is at -1
199
+ let header = (literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp;
189
200
  if (comment) {
190
201
  header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ');
191
202
  if (onComment)
192
203
  onComment();
193
204
  }
194
- if (!value)
195
- return `${header}${indentSize}\n${indent}${wsEnd}`;
196
205
  if (literal) {
197
206
  value = value.replace(/\n+/g, `$&${indent}`);
198
- return `${header}\n${indent}${wsStart}${value}${wsEnd}`;
207
+ return `${header}\n${indent}${start}${value}${end}`;
199
208
  }
200
209
  value = value
201
210
  .replace(/\n+/g, '\n$&')
202
211
  .replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
203
- // ^ ind.line ^ empty ^ capture next empty lines only at end of indent
212
+ // ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
204
213
  .replace(/\n+/g, `$&${indent}`);
205
- const body = foldFlowLines.foldFlowLines(`${wsStart}${value}${wsEnd}`, indent, foldFlowLines.FOLD_BLOCK, getFoldOptions(ctx));
214
+ const body = foldFlowLines.foldFlowLines(`${start}${value}${end}`, indent, foldFlowLines.FOLD_BLOCK, getFoldOptions(ctx));
206
215
  return `${header}\n${indent}${body}`;
207
216
  }
208
217
  function plainString(item, ctx, onComment, onChompKeep) {
209
218
  var _a;
210
- const { comment, type, value } = item;
219
+ const { type, value } = item;
211
220
  const { actualString, implicitKey, indent, inFlow } = ctx;
212
221
  if ((implicitKey && /[\n[\]{},]/.test(value)) ||
213
222
  (inFlow && /[[\]{},]/.test(value))) {
@@ -263,17 +272,9 @@ function plainString(item, ctx, onComment, onChompKeep) {
263
272
  return doubleQuotedString(value, ctx);
264
273
  }
265
274
  }
266
- const body = implicitKey
275
+ return implicitKey
267
276
  ? str
268
277
  : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
269
- if (comment &&
270
- !inFlow &&
271
- (body.indexOf('\n') !== -1 || comment.indexOf('\n') !== -1)) {
272
- if (onComment)
273
- onComment();
274
- return addComment.addCommentBefore(body, indent, comment);
275
- }
276
- return body;
277
278
  }
278
279
  function stringifyString(item, ctx, onComment, onChompKeep) {
279
280
  const { implicitKey, inFlow } = ctx;
@@ -1,5 +1,4 @@
1
- import type { Options } from './options.js';
2
- export declare function testEvents(src: string, options?: Options): {
1
+ export declare function testEvents(src: string): {
3
2
  events: string[];
4
3
  error: any;
5
4
  };
@@ -2,6 +2,7 @@
2
2
 
3
3
  var Node = require('./nodes/Node.js');
4
4
  var publicApi = require('./public-api.js');
5
+ var visit = require('./visit.js');
5
6
 
6
7
  const scalarChar = {
7
8
  BLOCK_FOLDED: '>',
@@ -10,11 +11,22 @@ const scalarChar = {
10
11
  QUOTE_DOUBLE: '"',
11
12
  QUOTE_SINGLE: "'"
12
13
  };
14
+ function anchorExists(doc, anchor) {
15
+ let found = false;
16
+ visit.visit(doc, {
17
+ Value(_key, node) {
18
+ if (node.anchor === anchor) {
19
+ found = true;
20
+ return visit.visit.BREAK;
21
+ }
22
+ }
23
+ });
24
+ return found;
25
+ }
13
26
  // test harness for yaml-test-suite event tests
14
- function testEvents(src, options) {
27
+ function testEvents(src) {
15
28
  var _a;
16
- const opt = Object.assign({ version: '1.2' }, options);
17
- const docs = publicApi.parseAllDocuments(src, opt);
29
+ const docs = publicApi.parseAllDocuments(src);
18
30
  const errDoc = docs.find(doc => doc.errors.length > 0);
19
31
  const error = errDoc ? errDoc.errors[0].message : null;
20
32
  const events = ['+STR'];
@@ -24,17 +36,18 @@ function testEvents(src, options) {
24
36
  let root = doc.contents;
25
37
  if (Array.isArray(root))
26
38
  root = root[0];
27
- const [rootStart, rootEnd] = doc.range || [0, 0];
39
+ // eslint-disable-next-line no-sparse-arrays
40
+ const [rootStart, , rootEnd] = doc.range || [0, , 0];
28
41
  const error = doc.errors[0];
29
- if (error && (!error.offset || error.offset < rootStart))
42
+ if (error && (!error.pos || error.pos[0] < rootStart))
30
43
  throw new Error();
31
44
  let docStart = '+DOC';
32
45
  if (doc.directives.marker)
33
46
  docStart += ' ---';
34
- else if (doc.contents && doc.contents.range[1] === doc.contents.range[0])
47
+ else if (doc.contents && doc.contents.range[2] === doc.contents.range[0])
35
48
  continue;
36
49
  events.push(docStart);
37
- addEvents(events, doc, (_a = error === null || error === void 0 ? void 0 : error.offset) !== null && _a !== void 0 ? _a : -1, root);
50
+ addEvents(events, doc, (_a = error === null || error === void 0 ? void 0 : error.pos[0]) !== null && _a !== void 0 ? _a : -1, root);
38
51
  let docEnd = '-DOC';
39
52
  if (rootEnd) {
40
53
  const post = src.slice(rootStart, rootEnd);
@@ -58,16 +71,16 @@ function addEvents(events, doc, errPos, node) {
58
71
  if (errPos !== -1 && Node.isNode(node) && node.range[0] >= errPos)
59
72
  throw new Error();
60
73
  let props = '';
61
- let anchor = Node.isNode(node) ? doc.anchors.getName(node) : undefined;
74
+ let anchor = Node.isScalar(node) || Node.isCollection(node) ? node.anchor : undefined;
62
75
  if (anchor) {
63
76
  if (/\d$/.test(anchor)) {
64
77
  const alt = anchor.replace(/\d$/, '');
65
- if (doc.anchors.getNode(alt))
78
+ if (anchorExists(doc, alt))
66
79
  anchor = alt;
67
80
  }
68
81
  props = ` &${anchor}`;
69
82
  }
70
- if (node.tag)
83
+ if (Node.isNode(node) && node.tag)
71
84
  props += ` <${node.tag}>`;
72
85
  if (Node.isMap(node)) {
73
86
  events.push(`+MAP${props}`);
@@ -91,10 +104,10 @@ function addEvents(events, doc, errPos, node) {
91
104
  events.push('-MAP');
92
105
  }
93
106
  else if (Node.isAlias(node)) {
94
- let alias = doc.anchors.getName(node.source);
107
+ let alias = node.source;
95
108
  if (alias && /\d$/.test(alias)) {
96
109
  const alt = alias.replace(/\d$/, '');
97
- if (doc.anchors.getNode(alt))
110
+ if (anchorExists(doc, alt))
98
111
  alias = alt;
99
112
  }
100
113
  events.push(`=ALI${props} *${alias}`);
package/dist/util.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { debug, LogLevelId, warn } from './log.js';
2
2
  export { findPair } from './nodes/YAMLMap.js';
3
- export { toJS, ToJSAnchorValue, ToJSContext } from './nodes/toJS.js';
3
+ export { toJS, ToJSContext } from './nodes/toJS.js';
4
4
  export { foldFlowLines } from './stringify/foldFlowLines';
5
5
  export { stringifyNumber } from './stringify/stringifyNumber.js';
6
6
  export { stringifyString } from './stringify/stringifyString.js';
package/dist/visit.d.ts CHANGED
@@ -8,10 +8,13 @@ import type { YAMLSeq } from './nodes/YAMLSeq.js';
8
8
  export declare type visitorFn<T> = (key: number | 'key' | 'value' | null, node: T, path: readonly (Document | Node | Pair)[]) => void | symbol | number | Node | Pair;
9
9
  export declare type visitor = visitorFn<unknown> | {
10
10
  Alias?: visitorFn<Alias>;
11
+ Collection?: visitorFn<YAMLMap | YAMLSeq>;
11
12
  Map?: visitorFn<YAMLMap>;
13
+ Node?: visitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
12
14
  Pair?: visitorFn<Pair>;
13
15
  Scalar?: visitorFn<Scalar>;
14
16
  Seq?: visitorFn<YAMLSeq>;
17
+ Value?: visitorFn<Scalar | YAMLMap | YAMLSeq>;
15
18
  };
16
19
  /**
17
20
  * Apply a visitor to an AST node or document.
@@ -38,14 +41,20 @@ export declare type visitor = visitorFn<unknown> | {
38
41
  * If `visitor` is a single function, it will be called with all values
39
42
  * encountered in the tree, including e.g. `null` values. Alternatively,
40
43
  * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
41
- * `Alias` and `Scalar` node.
44
+ * `Alias` and `Scalar` node. To define the same visitor function for more than
45
+ * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
46
+ * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
47
+ * specific defined one will be used for each node.
42
48
  */
43
49
  export declare function visit(node: Node | Document | null, visitor: visitorFn<unknown> | {
44
50
  Alias?: visitorFn<Alias>;
51
+ Collection?: visitorFn<YAMLMap | YAMLSeq>;
45
52
  Map?: visitorFn<YAMLMap>;
53
+ Node?: visitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
46
54
  Pair?: visitorFn<Pair>;
47
55
  Scalar?: visitorFn<Scalar>;
48
56
  Seq?: visitorFn<YAMLSeq>;
57
+ Value?: visitorFn<Scalar | YAMLMap | YAMLSeq>;
49
58
  }): void;
50
59
  export declare namespace visit {
51
60
  var BREAK: symbol;
package/dist/visit.js CHANGED
@@ -30,9 +30,28 @@ const REMOVE = Symbol('remove node');
30
30
  * If `visitor` is a single function, it will be called with all values
31
31
  * encountered in the tree, including e.g. `null` values. Alternatively,
32
32
  * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
33
- * `Alias` and `Scalar` node.
33
+ * `Alias` and `Scalar` node. To define the same visitor function for more than
34
+ * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
35
+ * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
36
+ * specific defined one will be used for each node.
34
37
  */
35
38
  function visit(node, visitor) {
39
+ if (typeof visitor === 'object' &&
40
+ (visitor.Collection || visitor.Node || visitor.Value)) {
41
+ visitor = Object.assign({
42
+ Alias: visitor.Node,
43
+ Map: visitor.Node,
44
+ Scalar: visitor.Node,
45
+ Seq: visitor.Node
46
+ }, visitor.Value && {
47
+ Map: visitor.Value,
48
+ Scalar: visitor.Value,
49
+ Seq: visitor.Value
50
+ }, visitor.Collection && {
51
+ Map: visitor.Collection,
52
+ Seq: visitor.Collection
53
+ }, visitor);
54
+ }
36
55
  if (Node.isDocument(node)) {
37
56
  const cd = _visit(null, node.contents, visitor, Object.freeze([node]));
38
57
  if (cd === REMOVE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml",
3
- "version": "2.0.0-4",
3
+ "version": "2.0.0-8",
4
4
  "license": "ISC",
5
5
  "author": "Eemeli Aro <eemeli@gmail.com>",
6
6
  "repository": "github:eemeli/yaml",
@@ -46,6 +46,7 @@
46
46
  "test": "jest --config config/jest.config.js",
47
47
  "test:browsers": "cd playground && npm test",
48
48
  "test:dist": "npm run build:node && jest --config config/jest.config.js",
49
+ "test:dist:types": "tsc --allowJs --moduleResolution node --noEmit --target es2017 dist/index.js",
49
50
  "test:types": "tsc --noEmit",
50
51
  "docs:install": "cd docs-slate && bundle install",
51
52
  "docs:deploy": "cd docs-slate && ./deploy.sh",
@@ -53,7 +54,7 @@
53
54
  "preversion": "npm test && npm run build",
54
55
  "prepublishOnly": "npm run clean && npm test && npm run build"
55
56
  },
56
- "browserslist": "> 0.5%, not dead, not ie 11",
57
+ "browserslist": "defaults, not ie 11",
57
58
  "prettier": {
58
59
  "arrowParens": "avoid",
59
60
  "semi": false,
@@ -69,23 +70,23 @@
69
70
  "@rollup/plugin-babel": "^5.2.3",
70
71
  "@rollup/plugin-replace": "^2.3.4",
71
72
  "@rollup/plugin-typescript": "^8.1.1",
72
- "@types/jest": "^26.0.20",
73
+ "@types/jest": "^27.0.1",
74
+ "@types/node": "^15.6.1",
73
75
  "@typescript-eslint/eslint-plugin": "^4.15.2",
74
76
  "@typescript-eslint/parser": "^4.15.2",
75
- "babel-jest": "^26.6.3",
77
+ "babel-jest": "^27.0.1",
76
78
  "cross-env": "^7.0.3",
77
79
  "eslint": "^7.20.0",
78
80
  "eslint-config-prettier": "^8.1.0",
79
81
  "fast-check": "^2.12.0",
80
- "jest": "^26.6.3",
82
+ "jest": "^27.0.1",
81
83
  "jest-ts-webcompat-resolver": "^1.0.0",
82
84
  "prettier": "^2.2.1",
83
85
  "rollup": "^2.38.2",
84
- "rollup-plugin-copy": "^3.3.0",
85
86
  "tslib": "^2.1.0",
86
- "typescript": "^4.1.3"
87
+ "typescript": "^4.3.5"
87
88
  },
88
89
  "engines": {
89
- "node": ">= 10"
90
+ "node": ">= 12"
90
91
  }
91
92
  }
@@ -1,102 +0,0 @@
1
- import { Alias } from '../nodes/Alias.js';
2
- import { isAlias, isMap, isScalar, isCollection } from '../nodes/Node.js';
3
- import { Pair } from '../nodes/Pair.js';
4
- import { Scalar } from '../nodes/Scalar.js';
5
- import { YAMLSeq } from '../nodes/YAMLSeq.js';
6
-
7
- class Anchors {
8
- constructor(prefix) {
9
- this.map = Object.create(null);
10
- this.prefix = prefix;
11
- }
12
- /**
13
- * Create a new `Alias` node, adding the required anchor for `node`.
14
- * If `name` is empty, a new anchor name will be generated.
15
- */
16
- createAlias(node, name) {
17
- this.setAnchor(node, name);
18
- return new Alias(node);
19
- }
20
- /**
21
- * Create a new merge `Pair` with the given source nodes.
22
- * Non-`Alias` sources will be automatically wrapped.
23
- */
24
- createMergePair(...sources) {
25
- const key = new Scalar(Pair.MERGE_KEY);
26
- const items = sources.map(s => {
27
- if (isAlias(s)) {
28
- if (isMap(s.source))
29
- return s;
30
- }
31
- else if (isMap(s)) {
32
- return this.createAlias(s);
33
- }
34
- throw new Error('Merge sources must be Map nodes or their Aliases');
35
- });
36
- if (items.length === 1)
37
- return new Pair(key, items[0]);
38
- const seq = new YAMLSeq();
39
- seq.items = items;
40
- return new Pair(key, seq);
41
- }
42
- /** The anchor name associated with `node`, if set. */
43
- getName(node) {
44
- return Object.keys(this.map).find(a => this.map[a] === node);
45
- }
46
- /** List of all defined anchor names. */
47
- getNames() {
48
- return Object.keys(this.map);
49
- }
50
- /** The node associated with the anchor `name`, if set. */
51
- getNode(name) {
52
- return this.map[name];
53
- }
54
- /**
55
- * Find an available anchor name with the given `prefix` and a
56
- * numerical suffix.
57
- */
58
- newName(prefix) {
59
- if (!prefix)
60
- prefix = this.prefix;
61
- const names = Object.keys(this.map);
62
- for (let i = 1; true; ++i) {
63
- const name = `${prefix}${i}`;
64
- if (!names.includes(name))
65
- return name;
66
- }
67
- }
68
- /**
69
- * Associate an anchor with `node`. If `name` is empty, a new name will be generated.
70
- * To remove an anchor, use `setAnchor(null, name)`.
71
- */
72
- setAnchor(node, name) {
73
- const { map } = this;
74
- if (!node) {
75
- if (!name)
76
- return null;
77
- delete map[name];
78
- return name;
79
- }
80
- if (!isScalar(node) && !isCollection(node))
81
- throw new Error('Anchors may only be set for Scalar, Seq and Map nodes');
82
- if (name) {
83
- if (/[\x00-\x19\s,[\]{}]/.test(name))
84
- throw new Error('Anchor names must not contain whitespace or control characters');
85
- const prevNode = map[name];
86
- if (prevNode && prevNode !== node)
87
- map[this.newName(name)] = prevNode;
88
- }
89
- const prevName = Object.keys(map).find(a => map[a] === node);
90
- if (prevName) {
91
- if (!name || prevName === name)
92
- return prevName;
93
- delete map[prevName];
94
- }
95
- else if (!name)
96
- name = this.newName();
97
- map[name] = node;
98
- return name;
99
- }
100
- }
101
-
102
- export { Anchors };
@@ -1,27 +0,0 @@
1
- import { tags, schemas } from '../tags/index.js';
2
- import { getSchemaTags } from './getSchemaTags.js';
3
-
4
- const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
5
- const coreKnownTags = {
6
- 'tag:yaml.org,2002:binary': tags.binary,
7
- 'tag:yaml.org,2002:omap': tags.omap,
8
- 'tag:yaml.org,2002:pairs': tags.pairs,
9
- 'tag:yaml.org,2002:set': tags.set,
10
- 'tag:yaml.org,2002:timestamp': tags.timestamp
11
- };
12
- class Schema {
13
- constructor({ customTags, merge, resolveKnownTags, schema, sortMapEntries }) {
14
- // Used by createNode(), to avoid circular dependencies
15
- this.map = tags.map;
16
- this.seq = tags.seq;
17
- this.merge = !!merge;
18
- this.name = schema || 'core';
19
- this.knownTags = resolveKnownTags ? coreKnownTags : {};
20
- this.tags = getSchemaTags(schemas, tags, customTags, this.name);
21
- // Used by createMap()
22
- this.sortMapEntries =
23
- sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null;
24
- }
25
- }
26
-
27
- export { Schema };
@@ -1,30 +0,0 @@
1
- function getSchemaTags(schemas, knownTags, customTags, schemaName) {
2
- const schemaId = schemaName.replace(/\W/g, ''); // 'yaml-1.1' -> 'yaml11'
3
- let tags = schemas[schemaId];
4
- if (!tags) {
5
- const keys = Object.keys(schemas)
6
- .map(key => JSON.stringify(key))
7
- .join(', ');
8
- throw new Error(`Unknown schema "${schemaName}"; use one of ${keys}`);
9
- }
10
- if (Array.isArray(customTags)) {
11
- for (const tag of customTags)
12
- tags = tags.concat(tag);
13
- }
14
- else if (typeof customTags === 'function') {
15
- tags = customTags(tags.slice());
16
- }
17
- return tags.map(tag => {
18
- if (typeof tag !== 'string')
19
- return tag;
20
- const tagObj = knownTags[tag];
21
- if (tagObj)
22
- return tagObj;
23
- const keys = Object.keys(knownTags)
24
- .map(key => JSON.stringify(key))
25
- .join(', ');
26
- throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
27
- });
28
- }
29
-
30
- export { getSchemaTags };
@@ -1,17 +0,0 @@
1
- function addCommentBefore(str, indent, comment) {
2
- if (!comment)
3
- return str;
4
- const cc = comment.replace(/[\s\S]^/gm, `$&${indent}#`);
5
- return `#${cc}\n${indent}${str}`;
6
- }
7
- function addComment(str, indent, comment) {
8
- return !comment
9
- ? str
10
- : comment.includes('\n')
11
- ? `${str}\n` + comment.replace(/^/gm, `${indent || ''}#`)
12
- : str.endsWith(' ')
13
- ? `${str}#${comment}`
14
- : `${str} #${comment}`;
15
- }
16
-
17
- export { addComment, addCommentBefore };
@@ -1,109 +0,0 @@
1
- import { Scalar } from '../nodes/Scalar.js';
2
- import { stringifyNumber } from '../stringify/stringifyNumber.js';
3
- import { failsafe } from './failsafe/index.js';
4
-
5
- const intIdentify = (value) => typeof value === 'bigint' || Number.isInteger(value);
6
- const intResolve = (str, offset, radix, { intAsBigInt }) => (intAsBigInt ? BigInt(str) : parseInt(str.substring(offset), radix));
7
- function intStringify(node, radix, prefix) {
8
- const { value } = node;
9
- if (intIdentify(value) && value >= 0)
10
- return prefix + value.toString(radix);
11
- return stringifyNumber(node);
12
- }
13
- const nullObj = {
14
- identify: value => value == null,
15
- createNode: () => new Scalar(null),
16
- default: true,
17
- tag: 'tag:yaml.org,2002:null',
18
- test: /^(?:~|[Nn]ull|NULL)?$/,
19
- resolve: () => new Scalar(null),
20
- stringify: ({ source }, ctx) => source && nullObj.test.test(source) ? source : ctx.options.nullStr
21
- };
22
- const boolObj = {
23
- identify: value => typeof value === 'boolean',
24
- default: true,
25
- tag: 'tag:yaml.org,2002:bool',
26
- test: /^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$/,
27
- resolve: str => new Scalar(str[0] === 't' || str[0] === 'T'),
28
- stringify({ source, value }, ctx) {
29
- if (source && boolObj.test.test(source)) {
30
- const sv = source[0] === 't' || source[0] === 'T';
31
- if (value === sv)
32
- return source;
33
- }
34
- return value ? ctx.options.trueStr : ctx.options.falseStr;
35
- }
36
- };
37
- const octObj = {
38
- identify: value => intIdentify(value) && value >= 0,
39
- default: true,
40
- tag: 'tag:yaml.org,2002:int',
41
- format: 'OCT',
42
- test: /^0o[0-7]+$/,
43
- resolve: (str, _onError, opt) => intResolve(str, 2, 8, opt),
44
- stringify: node => intStringify(node, 8, '0o')
45
- };
46
- const intObj = {
47
- identify: intIdentify,
48
- default: true,
49
- tag: 'tag:yaml.org,2002:int',
50
- test: /^[-+]?[0-9]+$/,
51
- resolve: (str, _onError, opt) => intResolve(str, 0, 10, opt),
52
- stringify: stringifyNumber
53
- };
54
- const hexObj = {
55
- identify: value => intIdentify(value) && value >= 0,
56
- default: true,
57
- tag: 'tag:yaml.org,2002:int',
58
- format: 'HEX',
59
- test: /^0x[0-9a-fA-F]+$/,
60
- resolve: (str, _onError, opt) => intResolve(str, 2, 16, opt),
61
- stringify: node => intStringify(node, 16, '0x')
62
- };
63
- const nanObj = {
64
- identify: value => typeof value === 'number',
65
- default: true,
66
- tag: 'tag:yaml.org,2002:float',
67
- test: /^(?:[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN))$/,
68
- resolve: str => str.slice(-3).toLowerCase() === 'nan'
69
- ? NaN
70
- : str[0] === '-'
71
- ? Number.NEGATIVE_INFINITY
72
- : Number.POSITIVE_INFINITY,
73
- stringify: stringifyNumber
74
- };
75
- const expObj = {
76
- identify: value => typeof value === 'number',
77
- default: true,
78
- tag: 'tag:yaml.org,2002:float',
79
- format: 'EXP',
80
- test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
81
- resolve: str => parseFloat(str),
82
- stringify: ({ value }) => Number(value).toExponential()
83
- };
84
- const floatObj = {
85
- identify: value => typeof value === 'number',
86
- default: true,
87
- tag: 'tag:yaml.org,2002:float',
88
- test: /^[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*)$/,
89
- resolve(str) {
90
- const node = new Scalar(parseFloat(str));
91
- const dot = str.indexOf('.');
92
- if (dot !== -1 && str[str.length - 1] === '0')
93
- node.minFractionDigits = str.length - dot - 1;
94
- return node;
95
- },
96
- stringify: stringifyNumber
97
- };
98
- const core = failsafe.concat([
99
- nullObj,
100
- boolObj,
101
- octObj,
102
- intObj,
103
- hexObj,
104
- nanObj,
105
- expObj,
106
- floatObj
107
- ]);
108
-
109
- export { boolObj, core, expObj, floatObj, hexObj, intObj, nanObj, nullObj, octObj };