yaml 2.0.0-7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/README.md +1 -1
  2. package/browser/dist/compose/compose-doc.js +3 -2
  3. package/browser/dist/compose/compose-node.js +14 -3
  4. package/browser/dist/compose/compose-scalar.js +17 -10
  5. package/browser/dist/compose/composer.js +6 -7
  6. package/browser/dist/compose/resolve-block-map.js +19 -5
  7. package/browser/dist/compose/resolve-block-scalar.js +12 -3
  8. package/browser/dist/compose/resolve-block-seq.js +5 -0
  9. package/browser/dist/compose/resolve-flow-collection.js +18 -6
  10. package/browser/dist/compose/resolve-flow-scalar.js +9 -2
  11. package/browser/dist/compose/resolve-props.js +4 -0
  12. package/browser/dist/compose/util-flow-indent-check.js +15 -0
  13. package/browser/dist/doc/Document.js +73 -16
  14. package/browser/dist/doc/anchors.js +2 -2
  15. package/browser/dist/doc/createNode.js +12 -7
  16. package/browser/dist/doc/directives.js +11 -3
  17. package/browser/dist/index.js +1 -2
  18. package/browser/dist/node_modules/tslib/tslib.es6.js +164 -0
  19. package/browser/dist/nodes/Collection.js +28 -15
  20. package/browser/dist/nodes/Node.js +7 -0
  21. package/browser/dist/nodes/Pair.js +11 -3
  22. package/browser/dist/nodes/Scalar.js +1 -1
  23. package/browser/dist/nodes/YAMLMap.js +6 -5
  24. package/browser/dist/nodes/YAMLSeq.js +2 -2
  25. package/browser/dist/nodes/addPairToJSMap.js +3 -2
  26. package/browser/dist/nodes/toJS.js +3 -1
  27. package/browser/dist/parse/cst-scalar.js +2 -6
  28. package/browser/dist/parse/cst-visit.js +2 -2
  29. package/browser/dist/parse/lexer.js +50 -22
  30. package/browser/dist/parse/parser.js +121 -45
  31. package/browser/dist/public-api.js +4 -5
  32. package/browser/dist/schema/Schema.js +19 -4
  33. package/browser/dist/schema/common/null.js +3 -1
  34. package/browser/dist/schema/core/float.js +4 -1
  35. package/browser/dist/schema/tags.js +17 -13
  36. package/browser/dist/schema/yaml-1.1/float.js +4 -1
  37. package/browser/dist/schema/yaml-1.1/omap.js +1 -1
  38. package/browser/dist/schema/yaml-1.1/pairs.js +2 -1
  39. package/browser/dist/stringify/stringify.js +53 -23
  40. package/browser/dist/stringify/stringifyCollection.js +115 -85
  41. package/browser/dist/stringify/stringifyComment.js +18 -12
  42. package/browser/dist/stringify/stringifyDocument.js +37 -15
  43. package/browser/dist/stringify/stringifyPair.js +35 -21
  44. package/browser/dist/stringify/stringifyString.js +41 -43
  45. package/browser/dist/util.js +3 -0
  46. package/browser/dist/visit.js +150 -62
  47. package/dist/compose/compose-doc.js +3 -2
  48. package/dist/compose/compose-node.d.ts +1 -0
  49. package/dist/compose/compose-node.js +14 -3
  50. package/dist/compose/compose-scalar.js +16 -9
  51. package/dist/compose/composer.d.ts +1 -1
  52. package/dist/compose/composer.js +8 -9
  53. package/dist/compose/resolve-block-map.js +19 -5
  54. package/dist/compose/resolve-block-scalar.js +12 -3
  55. package/dist/compose/resolve-block-seq.js +5 -0
  56. package/dist/compose/resolve-flow-collection.js +18 -6
  57. package/dist/compose/resolve-flow-scalar.js +9 -2
  58. package/dist/compose/resolve-props.d.ts +1 -1
  59. package/dist/compose/resolve-props.js +4 -0
  60. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  61. package/dist/compose/util-flow-indent-check.js +17 -0
  62. package/dist/doc/Document.d.ts +13 -4
  63. package/dist/doc/Document.js +79 -22
  64. package/dist/doc/anchors.d.ts +2 -2
  65. package/dist/doc/anchors.js +2 -2
  66. package/dist/doc/createNode.d.ts +3 -2
  67. package/dist/doc/createNode.js +11 -6
  68. package/dist/doc/directives.d.ts +5 -2
  69. package/dist/doc/directives.js +11 -3
  70. package/dist/index.d.ts +4 -4
  71. package/dist/index.js +1 -2
  72. package/dist/node_modules/tslib/tslib.es6.js +76 -0
  73. package/dist/nodes/Alias.d.ts +7 -3
  74. package/dist/nodes/Collection.d.ts +10 -4
  75. package/dist/nodes/Collection.js +27 -14
  76. package/dist/nodes/Node.d.ts +6 -1
  77. package/dist/nodes/Node.js +7 -0
  78. package/dist/nodes/Pair.d.ts +7 -2
  79. package/dist/nodes/Pair.js +10 -2
  80. package/dist/nodes/Scalar.d.ts +2 -0
  81. package/dist/nodes/Scalar.js +1 -1
  82. package/dist/nodes/YAMLMap.d.ts +5 -1
  83. package/dist/nodes/YAMLMap.js +6 -5
  84. package/dist/nodes/YAMLSeq.d.ts +2 -0
  85. package/dist/nodes/YAMLSeq.js +2 -2
  86. package/dist/nodes/addPairToJSMap.js +2 -1
  87. package/dist/nodes/toJS.js +3 -1
  88. package/dist/options.d.ts +67 -15
  89. package/dist/parse/cst-scalar.d.ts +6 -0
  90. package/dist/parse/cst-scalar.js +2 -6
  91. package/dist/parse/cst-visit.js +2 -2
  92. package/dist/parse/lexer.d.ts +2 -0
  93. package/dist/parse/lexer.js +50 -22
  94. package/dist/parse/parser.js +121 -45
  95. package/dist/public-api.js +4 -5
  96. package/dist/schema/Schema.d.ts +6 -4
  97. package/dist/schema/Schema.js +18 -3
  98. package/dist/schema/common/null.js +3 -1
  99. package/dist/schema/core/float.js +4 -1
  100. package/dist/schema/tags.d.ts +1 -2
  101. package/dist/schema/tags.js +17 -13
  102. package/dist/schema/yaml-1.1/float.js +4 -1
  103. package/dist/schema/yaml-1.1/omap.js +1 -1
  104. package/dist/schema/yaml-1.1/pairs.js +2 -1
  105. package/dist/stringify/stringify.d.ts +5 -3
  106. package/dist/stringify/stringify.js +53 -23
  107. package/dist/stringify/stringifyCollection.d.ts +2 -6
  108. package/dist/stringify/stringifyCollection.js +114 -84
  109. package/dist/stringify/stringifyComment.d.ts +10 -2
  110. package/dist/stringify/stringifyComment.js +19 -12
  111. package/dist/stringify/stringifyDocument.js +36 -14
  112. package/dist/stringify/stringifyPair.js +34 -20
  113. package/dist/stringify/stringifyString.js +41 -43
  114. package/dist/test-events.d.ts +1 -1
  115. package/dist/test-events.js +13 -12
  116. package/dist/util.d.ts +3 -0
  117. package/dist/util.js +6 -0
  118. package/dist/visit.d.ts +49 -10
  119. package/dist/visit.js +149 -60
  120. package/package.json +12 -10
  121. package/util.d.ts +3 -0
  122. package/browser/dist/options.js +0 -17
  123. package/dist/options.js +0 -19
@@ -5,33 +5,38 @@ var stringify = require('./stringify.js');
5
5
  var stringifyComment = require('./stringifyComment.js');
6
6
 
7
7
  function stringifyDocument(doc, options) {
8
+ var _a;
8
9
  const lines = [];
9
10
  let hasDirectives = options.directives === true;
10
- if (options.directives !== false) {
11
+ if (options.directives !== false && doc.directives) {
11
12
  const dir = doc.directives.toString(doc);
12
13
  if (dir) {
13
14
  lines.push(dir);
14
15
  hasDirectives = true;
15
16
  }
16
- else if (doc.directives.marker)
17
+ else if (doc.directives.docStart)
17
18
  hasDirectives = true;
18
19
  }
19
20
  if (hasDirectives)
20
21
  lines.push('---');
22
+ const ctx = stringify.createStringifyContext(doc, options);
23
+ const { commentString } = ctx.options;
21
24
  if (doc.commentBefore) {
22
25
  if (lines.length !== 1)
23
26
  lines.unshift('');
24
- lines.unshift(stringifyComment.stringifyComment(doc.commentBefore, ''));
27
+ const cs = commentString(doc.commentBefore);
28
+ lines.unshift(stringifyComment.indentComment(cs, ''));
25
29
  }
26
- const ctx = stringify.createStringifyContext(doc, options);
27
30
  let chompKeep = false;
28
31
  let contentComment = null;
29
32
  if (doc.contents) {
30
33
  if (Node.isNode(doc.contents)) {
31
34
  if (doc.contents.spaceBefore && hasDirectives)
32
35
  lines.push('');
33
- if (doc.contents.commentBefore)
34
- lines.push(stringifyComment.stringifyComment(doc.contents.commentBefore, ''));
36
+ if (doc.contents.commentBefore) {
37
+ const cs = commentString(doc.contents.commentBefore);
38
+ lines.push(stringifyComment.indentComment(cs, ''));
39
+ }
35
40
  // top-level block scalars need to be indented if followed by a comment
36
41
  ctx.forceBlockIndent = !!doc.comment;
37
42
  contentComment = doc.contents.comment;
@@ -39,7 +44,7 @@ function stringifyDocument(doc, options) {
39
44
  const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
40
45
  let body = stringify.stringify(doc.contents, ctx, () => (contentComment = null), onChompKeep);
41
46
  if (contentComment)
42
- body = stringifyComment.addComment(body, '', contentComment);
47
+ body += stringifyComment.lineComment(body, '', commentString(contentComment));
43
48
  if ((body[0] === '|' || body[0] === '>') &&
44
49
  lines[lines.length - 1] === '---') {
45
50
  // Top-level block scalars with a preceding doc marker ought to use the
@@ -52,13 +57,30 @@ function stringifyDocument(doc, options) {
52
57
  else {
53
58
  lines.push(stringify.stringify(doc.contents, ctx));
54
59
  }
55
- let dc = doc.comment;
56
- if (dc && chompKeep)
57
- dc = dc.replace(/^\n+/, '');
58
- if (dc) {
59
- if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
60
- lines.push('');
61
- lines.push(stringifyComment.stringifyComment(dc, ''));
60
+ if ((_a = doc.directives) === null || _a === void 0 ? void 0 : _a.docEnd) {
61
+ if (doc.comment) {
62
+ const cs = commentString(doc.comment);
63
+ if (cs.includes('\n')) {
64
+ lines.push('...');
65
+ lines.push(stringifyComment.indentComment(cs, ''));
66
+ }
67
+ else {
68
+ lines.push(`... ${cs}`);
69
+ }
70
+ }
71
+ else {
72
+ lines.push('...');
73
+ }
74
+ }
75
+ else {
76
+ let dc = doc.comment;
77
+ if (dc && chompKeep)
78
+ dc = dc.replace(/^\n+/, '');
79
+ if (dc) {
80
+ if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
81
+ lines.push('');
82
+ lines.push(stringifyComment.indentComment(commentString(dc), ''));
83
+ }
62
84
  }
63
85
  return lines.join('\n') + '\n';
64
86
  }
@@ -6,7 +6,7 @@ var stringify = require('./stringify.js');
6
6
  var stringifyComment = require('./stringifyComment.js');
7
7
 
8
8
  function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
9
- const { allNullValues, doc, indent, indentStep, options: { indentSeq, simpleKeys } } = ctx;
9
+ const { allNullValues, doc, indent, indentStep, options: { commentString, indentSeq, simpleKeys } } = ctx;
10
10
  let keyComment = (Node.isNode(key) && key.comment) || null;
11
11
  if (simpleKeys) {
12
12
  if (keyComment) {
@@ -41,28 +41,39 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
41
41
  if (allNullValues || value == null) {
42
42
  if (keyCommentDone && onComment)
43
43
  onComment();
44
- return explicitKey ? `? ${str}` : str;
44
+ return str === '' ? '?' : explicitKey ? `? ${str}` : str;
45
45
  }
46
46
  }
47
47
  else if ((allNullValues && !simpleKeys) || (value == null && explicitKey)) {
48
- if (keyCommentDone)
49
- keyComment = null;
50
- if (chompKeep && !keyComment && onChompKeep)
48
+ str = `? ${str}`;
49
+ if (keyComment && !keyCommentDone) {
50
+ str += stringifyComment.lineComment(str, ctx.indent, commentString(keyComment));
51
+ }
52
+ else if (chompKeep && onChompKeep)
51
53
  onChompKeep();
52
- return stringifyComment.addComment(`? ${str}`, ctx.indent, keyComment);
54
+ return str;
53
55
  }
54
56
  if (keyCommentDone)
55
57
  keyComment = null;
56
- str = explicitKey
57
- ? `? ${stringifyComment.addComment(str, ctx.indent, keyComment)}\n${indent}:`
58
- : stringifyComment.addComment(`${str}:`, ctx.indent, keyComment);
58
+ if (explicitKey) {
59
+ if (keyComment)
60
+ str += stringifyComment.lineComment(str, ctx.indent, commentString(keyComment));
61
+ str = `? ${str}\n${indent}:`;
62
+ }
63
+ else {
64
+ str = `${str}:`;
65
+ if (keyComment)
66
+ str += stringifyComment.lineComment(str, ctx.indent, commentString(keyComment));
67
+ }
59
68
  let vcb = '';
60
69
  let valueComment = null;
61
70
  if (Node.isNode(value)) {
62
71
  if (value.spaceBefore)
63
72
  vcb = '\n';
64
- if (value.commentBefore)
65
- vcb += `\n${stringifyComment.stringifyComment(value.commentBefore, ctx.indent)}`;
73
+ if (value.commentBefore) {
74
+ const cs = commentString(value.commentBefore);
75
+ vcb += `\n${stringifyComment.indentComment(cs, ctx.indent)}`;
76
+ }
66
77
  valueComment = value.comment;
67
78
  }
68
79
  else if (value && typeof value === 'object') {
@@ -87,27 +98,30 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
87
98
  const valueStr = stringify.stringify(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true));
88
99
  let ws = ' ';
89
100
  if (vcb || keyComment) {
90
- ws = `${vcb}\n${ctx.indent}`;
101
+ if (valueStr === '' && !ctx.inFlow)
102
+ ws = vcb === '\n' ? '\n\n' : vcb;
103
+ else
104
+ ws = `${vcb}\n${ctx.indent}`;
91
105
  }
92
106
  else if (!explicitKey && Node.isCollection(value)) {
93
107
  const flow = valueStr[0] === '[' || valueStr[0] === '{';
94
108
  if (!flow || valueStr.includes('\n'))
95
109
  ws = `\n${ctx.indent}`;
96
110
  }
97
- else if (valueStr[0] === '\n')
111
+ else if (valueStr === '' || valueStr[0] === '\n')
98
112
  ws = '';
113
+ str += ws + valueStr;
99
114
  if (ctx.inFlow) {
100
115
  if (valueCommentDone && onComment)
101
116
  onComment();
102
- return str + ws + valueStr;
103
117
  }
104
- else {
105
- if (valueCommentDone)
106
- valueComment = null;
107
- if (chompKeep && !valueComment && onChompKeep)
108
- onChompKeep();
109
- return stringifyComment.addComment(str + ws + valueStr, ctx.indent, valueComment);
118
+ else if (valueComment && !valueCommentDone) {
119
+ str += stringifyComment.lineComment(str, ctx.indent, commentString(valueComment));
120
+ }
121
+ else if (chompKeep && onChompKeep) {
122
+ onChompKeep();
110
123
  }
124
+ return str;
111
125
  }
112
126
 
113
127
  exports.stringifyPair = stringifyPair;
@@ -120,34 +120,50 @@ function doubleQuotedString(value, ctx) {
120
120
  : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_QUOTED, getFoldOptions(ctx));
121
121
  }
122
122
  function singleQuotedString(value, ctx) {
123
- if (ctx.implicitKey) {
124
- if (/\n/.test(value))
125
- return doubleQuotedString(value, ctx);
126
- }
127
- else {
128
- // single quoted string can't have leading or trailing whitespace around newline
129
- if (/[ \t]\n|\n[ \t]/.test(value))
130
- return doubleQuotedString(value, ctx);
131
- }
123
+ if (ctx.options.singleQuote === false ||
124
+ (ctx.implicitKey && value.includes('\n')) ||
125
+ /[ \t]\n|\n[ \t]/.test(value) // single quoted string can't have leading or trailing whitespace around newline
126
+ )
127
+ return doubleQuotedString(value, ctx);
132
128
  const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
133
129
  const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
134
130
  return ctx.implicitKey
135
131
  ? res
136
132
  : foldFlowLines.foldFlowLines(res, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
137
133
  }
134
+ function quotedString(value, ctx) {
135
+ const { singleQuote } = ctx.options;
136
+ let qs;
137
+ if (singleQuote === false)
138
+ qs = doubleQuotedString;
139
+ else {
140
+ const hasDouble = value.includes('"');
141
+ const hasSingle = value.includes("'");
142
+ if (hasDouble && !hasSingle)
143
+ qs = singleQuotedString;
144
+ else if (hasSingle && !hasDouble)
145
+ qs = doubleQuotedString;
146
+ else
147
+ qs = singleQuote ? singleQuotedString : doubleQuotedString;
148
+ }
149
+ return qs(value, ctx);
150
+ }
138
151
  function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
152
+ const { blockQuote, commentString, lineWidth } = ctx.options;
139
153
  // 1. Block can't end in whitespace unless the last line is non-empty.
140
154
  // 2. Strings consisting of only whitespace are best rendered explicitly.
141
- if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
142
- return doubleQuotedString(value, ctx);
155
+ if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
156
+ return quotedString(value, ctx);
143
157
  }
144
158
  const indent = ctx.indent ||
145
159
  (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
146
- const literal = type === Scalar.Scalar.BLOCK_FOLDED
147
- ? false
148
- : type === Scalar.Scalar.BLOCK_LITERAL
149
- ? true
150
- : !lineLengthOverLimit(value, ctx.options.lineWidth, indent.length);
160
+ const literal = blockQuote === 'literal'
161
+ ? true
162
+ : blockQuote === 'folded' || type === Scalar.Scalar.BLOCK_FOLDED
163
+ ? false
164
+ : type === Scalar.Scalar.BLOCK_LITERAL
165
+ ? true
166
+ : !lineLengthOverLimit(value, lineWidth, indent.length);
151
167
  if (!value)
152
168
  return literal ? '|\n' : '>\n';
153
169
  // determine chomping from whitespace at value end
@@ -198,7 +214,7 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
198
214
  const indentSize = indent ? '2' : '1'; // root is at -1
199
215
  let header = (literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp;
200
216
  if (comment) {
201
- header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ');
217
+ header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
202
218
  if (onComment)
203
219
  onComment();
204
220
  }
@@ -215,44 +231,28 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
215
231
  return `${header}\n${indent}${body}`;
216
232
  }
217
233
  function plainString(item, ctx, onComment, onChompKeep) {
218
- var _a;
219
234
  const { type, value } = item;
220
235
  const { actualString, implicitKey, indent, inFlow } = ctx;
221
236
  if ((implicitKey && /[\n[\]{},]/.test(value)) ||
222
237
  (inFlow && /[[\]{},]/.test(value))) {
223
- return doubleQuotedString(value, ctx);
238
+ return quotedString(value, ctx);
224
239
  }
225
240
  if (!value ||
226
241
  /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
227
- const hasDouble = value.indexOf('"') !== -1;
228
- const hasSingle = value.indexOf("'") !== -1;
229
- let quotedString;
230
- if (hasDouble && !hasSingle) {
231
- quotedString = singleQuotedString;
232
- }
233
- else if (hasSingle && !hasDouble) {
234
- quotedString = doubleQuotedString;
235
- }
236
- else if (ctx.options.singleQuote) {
237
- quotedString = singleQuotedString;
238
- }
239
- else {
240
- quotedString = doubleQuotedString;
241
- }
242
242
  // not allowed:
243
243
  // - empty string, '-' or '?'
244
244
  // - start with an indicator character (except [?:-]) or /[?-] /
245
245
  // - '\n ', ': ' or ' \n' anywhere
246
246
  // - '#' not preceded by a non-space char
247
247
  // - end with ' ' or ':'
248
- return implicitKey || inFlow || value.indexOf('\n') === -1
248
+ return implicitKey || inFlow || !value.includes('\n')
249
249
  ? quotedString(value, ctx)
250
250
  : blockString(item, ctx, onComment, onChompKeep);
251
251
  }
252
252
  if (!implicitKey &&
253
253
  !inFlow &&
254
254
  type !== Scalar.Scalar.PLAIN &&
255
- value.indexOf('\n') !== -1) {
255
+ value.includes('\n')) {
256
256
  // Where allowed & type not set explicitly, prefer block style for multiline strings
257
257
  return blockString(item, ctx, onComment, onChompKeep);
258
258
  }
@@ -265,12 +265,10 @@ function plainString(item, ctx, onComment, onChompKeep) {
265
265
  // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
266
266
  // and others in v1.1.
267
267
  if (actualString) {
268
- for (const tag of ctx.doc.schema.tags) {
269
- if (tag.default &&
270
- tag.tag !== 'tag:yaml.org,2002:str' &&
271
- ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str)))
272
- return doubleQuotedString(value, ctx);
273
- }
268
+ const test = (tag) => { var _a; return tag.default && tag.tag !== 'tag:yaml.org,2002:str' && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str)); };
269
+ const { compat, tags } = ctx.doc.schema;
270
+ if (tags.some(test) || (compat === null || compat === void 0 ? void 0 : compat.some(test)))
271
+ return quotedString(value, ctx);
274
272
  }
275
273
  return implicitKey
276
274
  ? str
@@ -292,7 +290,7 @@ function stringifyString(item, ctx, onComment, onChompKeep) {
292
290
  case Scalar.Scalar.BLOCK_FOLDED:
293
291
  case Scalar.Scalar.BLOCK_LITERAL:
294
292
  return implicitKey || inFlow
295
- ? doubleQuotedString(ss.value, ctx) // blocks are not valid inside flow containers
293
+ ? quotedString(ss.value, ctx) // blocks are not valid inside flow containers
296
294
  : blockString(ss, ctx, onComment, onChompKeep);
297
295
  case Scalar.Scalar.QUOTE_DOUBLE:
298
296
  return doubleQuotedString(ss.value, ctx);
@@ -1,4 +1,4 @@
1
1
  export declare function testEvents(src: string): {
2
2
  events: string[];
3
- error: any;
3
+ error: unknown;
4
4
  };
@@ -36,29 +36,28 @@ function testEvents(src) {
36
36
  let root = doc.contents;
37
37
  if (Array.isArray(root))
38
38
  root = root[0];
39
- // eslint-disable-next-line no-sparse-arrays
40
- const [rootStart, , rootEnd] = doc.range || [0, , 0];
39
+ const [rootStart] = doc.range || [0];
41
40
  const error = doc.errors[0];
42
41
  if (error && (!error.pos || error.pos[0] < rootStart))
43
42
  throw new Error();
44
43
  let docStart = '+DOC';
45
- if (doc.directives.marker)
44
+ if (doc.directives.docStart)
46
45
  docStart += ' ---';
47
- else if (doc.contents && doc.contents.range[2] === doc.contents.range[0])
46
+ else if (doc.contents &&
47
+ doc.contents.range[2] === doc.contents.range[0] &&
48
+ !doc.contents.anchor &&
49
+ !doc.contents.tag)
48
50
  continue;
49
51
  events.push(docStart);
50
52
  addEvents(events, doc, (_a = error === null || error === void 0 ? void 0 : error.pos[0]) !== null && _a !== void 0 ? _a : -1, root);
51
53
  let docEnd = '-DOC';
52
- if (rootEnd) {
53
- const post = src.slice(rootStart, rootEnd);
54
- if (/^\.\.\.($|\s)/m.test(post))
55
- docEnd += ' ...';
56
- }
54
+ if (doc.directives.docEnd)
55
+ docEnd += ' ...';
57
56
  events.push(docEnd);
58
57
  }
59
58
  }
60
59
  catch (e) {
61
- return { events, error: error || e };
60
+ return { events, error: error !== null && error !== void 0 ? error : e };
62
61
  }
63
62
  events.push('-STR');
64
63
  return { events, error };
@@ -83,7 +82,8 @@ function addEvents(events, doc, errPos, node) {
83
82
  if (Node.isNode(node) && node.tag)
84
83
  props += ` <${node.tag}>`;
85
84
  if (Node.isMap(node)) {
86
- events.push(`+MAP${props}`);
85
+ const ev = node.flow ? '+MAP {}' : '+MAP';
86
+ events.push(`${ev}${props}`);
87
87
  node.items.forEach(({ key, value }) => {
88
88
  addEvents(events, doc, errPos, key);
89
89
  addEvents(events, doc, errPos, value);
@@ -91,7 +91,8 @@ function addEvents(events, doc, errPos, node) {
91
91
  events.push('-MAP');
92
92
  }
93
93
  else if (Node.isSeq(node)) {
94
- events.push(`+SEQ${props}`);
94
+ const ev = node.flow ? '+SEQ []' : '+SEQ';
95
+ events.push(`${ev}${props}`);
95
96
  node.items.forEach(item => {
96
97
  addEvents(events, doc, errPos, item);
97
98
  });
package/dist/util.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  export { debug, LogLevelId, warn } from './log.js';
2
2
  export { findPair } from './nodes/YAMLMap.js';
3
3
  export { toJS, ToJSContext } from './nodes/toJS.js';
4
+ export { map as mapTag } from './schema/common/map.js';
5
+ export { seq as seqTag } from './schema/common/seq.js';
6
+ export { string as stringTag } from './schema/common/string.js';
4
7
  export { foldFlowLines } from './stringify/foldFlowLines';
5
8
  export { stringifyNumber } from './stringify/stringifyNumber.js';
6
9
  export { stringifyString } from './stringify/stringifyString.js';
package/dist/util.js CHANGED
@@ -3,6 +3,9 @@
3
3
  var log = require('./log.js');
4
4
  var YAMLMap = require('./nodes/YAMLMap.js');
5
5
  var toJS = require('./nodes/toJS.js');
6
+ var map = require('./schema/common/map.js');
7
+ var seq = require('./schema/common/seq.js');
8
+ var string = require('./schema/common/string.js');
6
9
  var foldFlowLines = require('./stringify/foldFlowLines.js');
7
10
  var stringifyNumber = require('./stringify/stringifyNumber.js');
8
11
  var stringifyString = require('./stringify/stringifyString.js');
@@ -13,6 +16,9 @@ exports.debug = log.debug;
13
16
  exports.warn = log.warn;
14
17
  exports.findPair = YAMLMap.findPair;
15
18
  exports.toJS = toJS.toJS;
19
+ exports.mapTag = map.map;
20
+ exports.seqTag = seq.seq;
21
+ exports.stringTag = string.string;
16
22
  exports.foldFlowLines = foldFlowLines.foldFlowLines;
17
23
  exports.stringifyNumber = stringifyNumber.stringifyNumber;
18
24
  exports.stringifyString = stringifyString.stringifyString;
package/dist/visit.d.ts CHANGED
@@ -16,6 +16,17 @@ export declare type visitor = visitorFn<unknown> | {
16
16
  Seq?: visitorFn<YAMLSeq>;
17
17
  Value?: visitorFn<Scalar | YAMLMap | YAMLSeq>;
18
18
  };
19
+ export declare type asyncVisitorFn<T> = (key: number | 'key' | 'value' | null, node: T, path: readonly (Document | Node | Pair)[]) => void | symbol | number | Node | Pair | Promise<void | symbol | number | Node | Pair>;
20
+ export declare type asyncVisitor = asyncVisitorFn<unknown> | {
21
+ Alias?: asyncVisitorFn<Alias>;
22
+ Collection?: asyncVisitorFn<YAMLMap | YAMLSeq>;
23
+ Map?: asyncVisitorFn<YAMLMap>;
24
+ Node?: asyncVisitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
25
+ Pair?: asyncVisitorFn<Pair>;
26
+ Scalar?: asyncVisitorFn<Scalar>;
27
+ Seq?: asyncVisitorFn<YAMLSeq>;
28
+ Value?: asyncVisitorFn<Scalar | YAMLMap | YAMLSeq>;
29
+ };
19
30
  /**
20
31
  * Apply a visitor to an AST node or document.
21
32
  *
@@ -46,18 +57,46 @@ export declare type visitor = visitorFn<unknown> | {
46
57
  * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
47
58
  * specific defined one will be used for each node.
48
59
  */
49
- export declare function visit(node: Node | Document | null, visitor: visitorFn<unknown> | {
50
- Alias?: visitorFn<Alias>;
51
- Collection?: visitorFn<YAMLMap | YAMLSeq>;
52
- Map?: visitorFn<YAMLMap>;
53
- Node?: visitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
54
- Pair?: visitorFn<Pair>;
55
- Scalar?: visitorFn<Scalar>;
56
- Seq?: visitorFn<YAMLSeq>;
57
- Value?: visitorFn<Scalar | YAMLMap | YAMLSeq>;
58
- }): void;
60
+ export declare function visit(node: Node | Document | null, visitor: visitor): void;
59
61
  export declare namespace visit {
60
62
  var BREAK: symbol;
61
63
  var SKIP: symbol;
62
64
  var REMOVE: symbol;
63
65
  }
66
+ /**
67
+ * Apply an async visitor to an AST node or document.
68
+ *
69
+ * Walks through the tree (depth-first) starting from `node`, calling a
70
+ * `visitor` function with three arguments:
71
+ * - `key`: For sequence values and map `Pair`, the node's index in the
72
+ * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
73
+ * `null` for the root node.
74
+ * - `node`: The current node.
75
+ * - `path`: The ancestry of the current node.
76
+ *
77
+ * The return value of the visitor may be used to control the traversal:
78
+ * - `Promise`: Must resolve to one of the following values
79
+ * - `undefined` (default): Do nothing and continue
80
+ * - `visit.SKIP`: Do not visit the children of this node, continue with next
81
+ * sibling
82
+ * - `visit.BREAK`: Terminate traversal completely
83
+ * - `visit.REMOVE`: Remove the current node, then continue with the next one
84
+ * - `Node`: Replace the current node, then continue by visiting it
85
+ * - `number`: While iterating the items of a sequence or map, set the index
86
+ * of the next step. This is useful especially if the index of the current
87
+ * node has changed.
88
+ *
89
+ * If `visitor` is a single function, it will be called with all values
90
+ * encountered in the tree, including e.g. `null` values. Alternatively,
91
+ * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
92
+ * `Alias` and `Scalar` node. To define the same visitor function for more than
93
+ * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
94
+ * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
95
+ * specific defined one will be used for each node.
96
+ */
97
+ export declare function visitAsync(node: Node | Document | null, visitor: asyncVisitor): Promise<void>;
98
+ export declare namespace visitAsync {
99
+ var BREAK: symbol;
100
+ var SKIP: symbol;
101
+ var REMOVE: symbol;
102
+ }