yaml 2.8.2 → 3.0.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 (238) hide show
  1. package/README.md +65 -85
  2. package/{bin.mjs → bin.js} +1 -1
  3. package/dist/chunk-BYypO7fO.js +18 -0
  4. package/dist/cli.d.ts +10 -7
  5. package/dist/cli.js +181 -0
  6. package/dist/directives-CG6n4xSM.js +2856 -0
  7. package/dist/index.d.ts +335 -25
  8. package/dist/index.js +4 -49
  9. package/dist/line-counter-TFl552k_.js +3098 -0
  10. package/dist/public-api-BE74JyVm.js +79 -0
  11. package/dist/stringifyString-S_ITVLTO.d.ts +1306 -0
  12. package/dist/test-events.d.ts +6 -3
  13. package/dist/test-events.js +98 -124
  14. package/dist/util.d.ts +2 -16
  15. package/dist/util.js +2 -27
  16. package/package.json +20 -44
  17. package/browser/dist/compose/compose-collection.js +0 -88
  18. package/browser/dist/compose/compose-doc.js +0 -43
  19. package/browser/dist/compose/compose-node.js +0 -102
  20. package/browser/dist/compose/compose-scalar.js +0 -86
  21. package/browser/dist/compose/composer.js +0 -217
  22. package/browser/dist/compose/resolve-block-map.js +0 -115
  23. package/browser/dist/compose/resolve-block-scalar.js +0 -198
  24. package/browser/dist/compose/resolve-block-seq.js +0 -49
  25. package/browser/dist/compose/resolve-end.js +0 -37
  26. package/browser/dist/compose/resolve-flow-collection.js +0 -207
  27. package/browser/dist/compose/resolve-flow-scalar.js +0 -223
  28. package/browser/dist/compose/resolve-props.js +0 -146
  29. package/browser/dist/compose/util-contains-newline.js +0 -34
  30. package/browser/dist/compose/util-empty-scalar-position.js +0 -26
  31. package/browser/dist/compose/util-flow-indent-check.js +0 -15
  32. package/browser/dist/compose/util-map-includes.js +0 -13
  33. package/browser/dist/doc/Document.js +0 -335
  34. package/browser/dist/doc/anchors.js +0 -71
  35. package/browser/dist/doc/applyReviver.js +0 -55
  36. package/browser/dist/doc/createNode.js +0 -88
  37. package/browser/dist/doc/directives.js +0 -176
  38. package/browser/dist/errors.js +0 -57
  39. package/browser/dist/index.js +0 -17
  40. package/browser/dist/log.js +0 -11
  41. package/browser/dist/nodes/Alias.js +0 -114
  42. package/browser/dist/nodes/Collection.js +0 -147
  43. package/browser/dist/nodes/Node.js +0 -38
  44. package/browser/dist/nodes/Pair.js +0 -36
  45. package/browser/dist/nodes/Scalar.js +0 -24
  46. package/browser/dist/nodes/YAMLMap.js +0 -144
  47. package/browser/dist/nodes/YAMLSeq.js +0 -113
  48. package/browser/dist/nodes/addPairToJSMap.js +0 -63
  49. package/browser/dist/nodes/identity.js +0 -36
  50. package/browser/dist/nodes/toJS.js +0 -37
  51. package/browser/dist/parse/cst-scalar.js +0 -214
  52. package/browser/dist/parse/cst-stringify.js +0 -61
  53. package/browser/dist/parse/cst-visit.js +0 -97
  54. package/browser/dist/parse/cst.js +0 -98
  55. package/browser/dist/parse/lexer.js +0 -717
  56. package/browser/dist/parse/line-counter.js +0 -39
  57. package/browser/dist/parse/parser.js +0 -967
  58. package/browser/dist/public-api.js +0 -102
  59. package/browser/dist/schema/Schema.js +0 -37
  60. package/browser/dist/schema/common/map.js +0 -17
  61. package/browser/dist/schema/common/null.js +0 -15
  62. package/browser/dist/schema/common/seq.js +0 -17
  63. package/browser/dist/schema/common/string.js +0 -14
  64. package/browser/dist/schema/core/bool.js +0 -19
  65. package/browser/dist/schema/core/float.js +0 -43
  66. package/browser/dist/schema/core/int.js +0 -38
  67. package/browser/dist/schema/core/schema.js +0 -23
  68. package/browser/dist/schema/json/schema.js +0 -62
  69. package/browser/dist/schema/tags.js +0 -96
  70. package/browser/dist/schema/yaml-1.1/binary.js +0 -58
  71. package/browser/dist/schema/yaml-1.1/bool.js +0 -26
  72. package/browser/dist/schema/yaml-1.1/float.js +0 -46
  73. package/browser/dist/schema/yaml-1.1/int.js +0 -71
  74. package/browser/dist/schema/yaml-1.1/merge.js +0 -64
  75. package/browser/dist/schema/yaml-1.1/omap.js +0 -74
  76. package/browser/dist/schema/yaml-1.1/pairs.js +0 -78
  77. package/browser/dist/schema/yaml-1.1/schema.js +0 -39
  78. package/browser/dist/schema/yaml-1.1/set.js +0 -93
  79. package/browser/dist/schema/yaml-1.1/timestamp.js +0 -101
  80. package/browser/dist/stringify/foldFlowLines.js +0 -146
  81. package/browser/dist/stringify/stringify.js +0 -128
  82. package/browser/dist/stringify/stringifyCollection.js +0 -143
  83. package/browser/dist/stringify/stringifyComment.js +0 -20
  84. package/browser/dist/stringify/stringifyDocument.js +0 -85
  85. package/browser/dist/stringify/stringifyNumber.js +0 -24
  86. package/browser/dist/stringify/stringifyPair.js +0 -150
  87. package/browser/dist/stringify/stringifyString.js +0 -336
  88. package/browser/dist/util.js +0 -11
  89. package/browser/dist/visit.js +0 -233
  90. package/browser/index.js +0 -5
  91. package/browser/package.json +0 -3
  92. package/dist/cli.mjs +0 -201
  93. package/dist/compose/compose-collection.d.ts +0 -11
  94. package/dist/compose/compose-collection.js +0 -90
  95. package/dist/compose/compose-doc.d.ts +0 -7
  96. package/dist/compose/compose-doc.js +0 -45
  97. package/dist/compose/compose-node.d.ts +0 -29
  98. package/dist/compose/compose-node.js +0 -105
  99. package/dist/compose/compose-scalar.d.ts +0 -5
  100. package/dist/compose/compose-scalar.js +0 -88
  101. package/dist/compose/composer.d.ts +0 -63
  102. package/dist/compose/composer.js +0 -222
  103. package/dist/compose/resolve-block-map.d.ts +0 -6
  104. package/dist/compose/resolve-block-map.js +0 -117
  105. package/dist/compose/resolve-block-scalar.d.ts +0 -11
  106. package/dist/compose/resolve-block-scalar.js +0 -200
  107. package/dist/compose/resolve-block-seq.d.ts +0 -6
  108. package/dist/compose/resolve-block-seq.js +0 -51
  109. package/dist/compose/resolve-end.d.ts +0 -6
  110. package/dist/compose/resolve-end.js +0 -39
  111. package/dist/compose/resolve-flow-collection.d.ts +0 -7
  112. package/dist/compose/resolve-flow-collection.js +0 -209
  113. package/dist/compose/resolve-flow-scalar.d.ts +0 -10
  114. package/dist/compose/resolve-flow-scalar.js +0 -225
  115. package/dist/compose/resolve-props.d.ts +0 -23
  116. package/dist/compose/resolve-props.js +0 -148
  117. package/dist/compose/util-contains-newline.d.ts +0 -2
  118. package/dist/compose/util-contains-newline.js +0 -36
  119. package/dist/compose/util-empty-scalar-position.d.ts +0 -2
  120. package/dist/compose/util-empty-scalar-position.js +0 -28
  121. package/dist/compose/util-flow-indent-check.d.ts +0 -3
  122. package/dist/compose/util-flow-indent-check.js +0 -17
  123. package/dist/compose/util-map-includes.d.ts +0 -4
  124. package/dist/compose/util-map-includes.js +0 -15
  125. package/dist/doc/Document.d.ts +0 -141
  126. package/dist/doc/Document.js +0 -337
  127. package/dist/doc/anchors.d.ts +0 -24
  128. package/dist/doc/anchors.js +0 -76
  129. package/dist/doc/applyReviver.d.ts +0 -9
  130. package/dist/doc/applyReviver.js +0 -57
  131. package/dist/doc/createNode.d.ts +0 -17
  132. package/dist/doc/createNode.js +0 -90
  133. package/dist/doc/directives.d.ts +0 -49
  134. package/dist/doc/directives.js +0 -178
  135. package/dist/errors.d.ts +0 -21
  136. package/dist/errors.js +0 -62
  137. package/dist/log.d.ts +0 -3
  138. package/dist/log.js +0 -19
  139. package/dist/nodes/Alias.d.ts +0 -29
  140. package/dist/nodes/Alias.js +0 -116
  141. package/dist/nodes/Collection.d.ts +0 -73
  142. package/dist/nodes/Collection.js +0 -151
  143. package/dist/nodes/Node.d.ts +0 -53
  144. package/dist/nodes/Node.js +0 -40
  145. package/dist/nodes/Pair.d.ts +0 -22
  146. package/dist/nodes/Pair.js +0 -39
  147. package/dist/nodes/Scalar.d.ts +0 -43
  148. package/dist/nodes/Scalar.js +0 -27
  149. package/dist/nodes/YAMLMap.d.ts +0 -53
  150. package/dist/nodes/YAMLMap.js +0 -147
  151. package/dist/nodes/YAMLSeq.d.ts +0 -60
  152. package/dist/nodes/YAMLSeq.js +0 -115
  153. package/dist/nodes/addPairToJSMap.d.ts +0 -4
  154. package/dist/nodes/addPairToJSMap.js +0 -65
  155. package/dist/nodes/identity.d.ts +0 -23
  156. package/dist/nodes/identity.js +0 -53
  157. package/dist/nodes/toJS.d.ts +0 -29
  158. package/dist/nodes/toJS.js +0 -39
  159. package/dist/options.d.ts +0 -344
  160. package/dist/parse/cst-scalar.d.ts +0 -64
  161. package/dist/parse/cst-scalar.js +0 -218
  162. package/dist/parse/cst-stringify.d.ts +0 -8
  163. package/dist/parse/cst-stringify.js +0 -63
  164. package/dist/parse/cst-visit.d.ts +0 -39
  165. package/dist/parse/cst-visit.js +0 -99
  166. package/dist/parse/cst.d.ts +0 -109
  167. package/dist/parse/cst.js +0 -112
  168. package/dist/parse/lexer.d.ts +0 -87
  169. package/dist/parse/lexer.js +0 -719
  170. package/dist/parse/line-counter.d.ts +0 -22
  171. package/dist/parse/line-counter.js +0 -41
  172. package/dist/parse/parser.d.ts +0 -84
  173. package/dist/parse/parser.js +0 -972
  174. package/dist/public-api.d.ts +0 -44
  175. package/dist/public-api.js +0 -107
  176. package/dist/schema/Schema.d.ts +0 -17
  177. package/dist/schema/Schema.js +0 -39
  178. package/dist/schema/common/map.d.ts +0 -2
  179. package/dist/schema/common/map.js +0 -19
  180. package/dist/schema/common/null.d.ts +0 -4
  181. package/dist/schema/common/null.js +0 -17
  182. package/dist/schema/common/seq.d.ts +0 -2
  183. package/dist/schema/common/seq.js +0 -19
  184. package/dist/schema/common/string.d.ts +0 -2
  185. package/dist/schema/common/string.js +0 -16
  186. package/dist/schema/core/bool.d.ts +0 -4
  187. package/dist/schema/core/bool.js +0 -21
  188. package/dist/schema/core/float.d.ts +0 -4
  189. package/dist/schema/core/float.js +0 -47
  190. package/dist/schema/core/int.d.ts +0 -4
  191. package/dist/schema/core/int.js +0 -42
  192. package/dist/schema/core/schema.d.ts +0 -1
  193. package/dist/schema/core/schema.js +0 -25
  194. package/dist/schema/json/schema.d.ts +0 -2
  195. package/dist/schema/json/schema.js +0 -64
  196. package/dist/schema/json-schema.d.ts +0 -69
  197. package/dist/schema/tags.d.ts +0 -48
  198. package/dist/schema/tags.js +0 -99
  199. package/dist/schema/types.d.ts +0 -92
  200. package/dist/schema/yaml-1.1/binary.d.ts +0 -2
  201. package/dist/schema/yaml-1.1/binary.js +0 -70
  202. package/dist/schema/yaml-1.1/bool.d.ts +0 -7
  203. package/dist/schema/yaml-1.1/bool.js +0 -29
  204. package/dist/schema/yaml-1.1/float.d.ts +0 -4
  205. package/dist/schema/yaml-1.1/float.js +0 -50
  206. package/dist/schema/yaml-1.1/int.d.ts +0 -5
  207. package/dist/schema/yaml-1.1/int.js +0 -76
  208. package/dist/schema/yaml-1.1/merge.d.ts +0 -9
  209. package/dist/schema/yaml-1.1/merge.js +0 -68
  210. package/dist/schema/yaml-1.1/omap.d.ts +0 -22
  211. package/dist/schema/yaml-1.1/omap.js +0 -77
  212. package/dist/schema/yaml-1.1/pairs.d.ts +0 -10
  213. package/dist/schema/yaml-1.1/pairs.js +0 -82
  214. package/dist/schema/yaml-1.1/schema.d.ts +0 -1
  215. package/dist/schema/yaml-1.1/schema.js +0 -41
  216. package/dist/schema/yaml-1.1/set.d.ts +0 -28
  217. package/dist/schema/yaml-1.1/set.js +0 -96
  218. package/dist/schema/yaml-1.1/timestamp.d.ts +0 -6
  219. package/dist/schema/yaml-1.1/timestamp.js +0 -105
  220. package/dist/stringify/foldFlowLines.d.ts +0 -34
  221. package/dist/stringify/foldFlowLines.js +0 -151
  222. package/dist/stringify/stringify.d.ts +0 -21
  223. package/dist/stringify/stringify.js +0 -131
  224. package/dist/stringify/stringifyCollection.d.ts +0 -17
  225. package/dist/stringify/stringifyCollection.js +0 -145
  226. package/dist/stringify/stringifyComment.d.ts +0 -10
  227. package/dist/stringify/stringifyComment.js +0 -24
  228. package/dist/stringify/stringifyDocument.d.ts +0 -4
  229. package/dist/stringify/stringifyDocument.js +0 -87
  230. package/dist/stringify/stringifyNumber.d.ts +0 -2
  231. package/dist/stringify/stringifyNumber.js +0 -26
  232. package/dist/stringify/stringifyPair.d.ts +0 -3
  233. package/dist/stringify/stringifyPair.js +0 -152
  234. package/dist/stringify/stringifyString.d.ts +0 -9
  235. package/dist/stringify/stringifyString.js +0 -338
  236. package/dist/visit.d.ts +0 -102
  237. package/dist/visit.js +0 -236
  238. package/util.js +0 -2
@@ -1,338 +0,0 @@
1
- 'use strict';
2
-
3
- var Scalar = require('../nodes/Scalar.js');
4
- var foldFlowLines = require('./foldFlowLines.js');
5
-
6
- const getFoldOptions = (ctx, isBlock) => ({
7
- indentAtStart: isBlock ? ctx.indent.length : ctx.indentAtStart,
8
- lineWidth: ctx.options.lineWidth,
9
- minContentWidth: ctx.options.minContentWidth
10
- });
11
- // Also checks for lines starting with %, as parsing the output as YAML 1.1 will
12
- // presume that's starting a new document.
13
- const containsDocumentMarker = (str) => /^(%|---|\.\.\.)/m.test(str);
14
- function lineLengthOverLimit(str, lineWidth, indentLength) {
15
- if (!lineWidth || lineWidth < 0)
16
- return false;
17
- const limit = lineWidth - indentLength;
18
- const strLen = str.length;
19
- if (strLen <= limit)
20
- return false;
21
- for (let i = 0, start = 0; i < strLen; ++i) {
22
- if (str[i] === '\n') {
23
- if (i - start > limit)
24
- return true;
25
- start = i + 1;
26
- if (strLen - start <= limit)
27
- return false;
28
- }
29
- }
30
- return true;
31
- }
32
- function doubleQuotedString(value, ctx) {
33
- const json = JSON.stringify(value);
34
- if (ctx.options.doubleQuotedAsJSON)
35
- return json;
36
- const { implicitKey } = ctx;
37
- const minMultiLineLength = ctx.options.doubleQuotedMinMultiLineLength;
38
- const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
39
- let str = '';
40
- let start = 0;
41
- for (let i = 0, ch = json[i]; ch; ch = json[++i]) {
42
- if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') {
43
- // space before newline needs to be escaped to not be folded
44
- str += json.slice(start, i) + '\\ ';
45
- i += 1;
46
- start = i;
47
- ch = '\\';
48
- }
49
- if (ch === '\\')
50
- switch (json[i + 1]) {
51
- case 'u':
52
- {
53
- str += json.slice(start, i);
54
- const code = json.substr(i + 2, 4);
55
- switch (code) {
56
- case '0000':
57
- str += '\\0';
58
- break;
59
- case '0007':
60
- str += '\\a';
61
- break;
62
- case '000b':
63
- str += '\\v';
64
- break;
65
- case '001b':
66
- str += '\\e';
67
- break;
68
- case '0085':
69
- str += '\\N';
70
- break;
71
- case '00a0':
72
- str += '\\_';
73
- break;
74
- case '2028':
75
- str += '\\L';
76
- break;
77
- case '2029':
78
- str += '\\P';
79
- break;
80
- default:
81
- if (code.substr(0, 2) === '00')
82
- str += '\\x' + code.substr(2);
83
- else
84
- str += json.substr(i, 6);
85
- }
86
- i += 5;
87
- start = i + 1;
88
- }
89
- break;
90
- case 'n':
91
- if (implicitKey ||
92
- json[i + 2] === '"' ||
93
- json.length < minMultiLineLength) {
94
- i += 1;
95
- }
96
- else {
97
- // folding will eat first newline
98
- str += json.slice(start, i) + '\n\n';
99
- while (json[i + 2] === '\\' &&
100
- json[i + 3] === 'n' &&
101
- json[i + 4] !== '"') {
102
- str += '\n';
103
- i += 2;
104
- }
105
- str += indent;
106
- // space after newline needs to be escaped to not be folded
107
- if (json[i + 2] === ' ')
108
- str += '\\';
109
- i += 1;
110
- start = i + 1;
111
- }
112
- break;
113
- default:
114
- i += 1;
115
- }
116
- }
117
- str = start ? str + json.slice(start) : json;
118
- return implicitKey
119
- ? str
120
- : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_QUOTED, getFoldOptions(ctx, false));
121
- }
122
- function singleQuotedString(value, ctx) {
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);
128
- const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
129
- const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
130
- return ctx.implicitKey
131
- ? res
132
- : foldFlowLines.foldFlowLines(res, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx, false));
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
- }
151
- // The negative lookbehind avoids a polynomial search,
152
- // but isn't supported yet on Safari: https://caniuse.com/js-regexp-lookbehind
153
- let blockEndNewlines;
154
- try {
155
- blockEndNewlines = new RegExp('(^|(?<!\n))\n+(?!\n|$)', 'g');
156
- }
157
- catch {
158
- blockEndNewlines = /\n+(?!\n|$)/g;
159
- }
160
- function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
161
- const { blockQuote, commentString, lineWidth } = ctx.options;
162
- // 1. Block can't end in whitespace unless the last line is non-empty.
163
- // 2. Strings consisting of only whitespace are best rendered explicitly.
164
- if (!blockQuote || /\n[\t ]+$/.test(value)) {
165
- return quotedString(value, ctx);
166
- }
167
- const indent = ctx.indent ||
168
- (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
169
- const literal = blockQuote === 'literal'
170
- ? true
171
- : blockQuote === 'folded' || type === Scalar.Scalar.BLOCK_FOLDED
172
- ? false
173
- : type === Scalar.Scalar.BLOCK_LITERAL
174
- ? true
175
- : !lineLengthOverLimit(value, lineWidth, indent.length);
176
- if (!value)
177
- return literal ? '|\n' : '>\n';
178
- // determine chomping from whitespace at value end
179
- let chomp;
180
- let endStart;
181
- for (endStart = value.length; endStart > 0; --endStart) {
182
- const ch = value[endStart - 1];
183
- if (ch !== '\n' && ch !== '\t' && ch !== ' ')
184
- break;
185
- }
186
- let end = value.substring(endStart);
187
- const endNlPos = end.indexOf('\n');
188
- if (endNlPos === -1) {
189
- chomp = '-'; // strip
190
- }
191
- else if (value === end || endNlPos !== end.length - 1) {
192
- chomp = '+'; // keep
193
- if (onChompKeep)
194
- onChompKeep();
195
- }
196
- else {
197
- chomp = ''; // clip
198
- }
199
- if (end) {
200
- value = value.slice(0, -end.length);
201
- if (end[end.length - 1] === '\n')
202
- end = end.slice(0, -1);
203
- end = end.replace(blockEndNewlines, `$&${indent}`);
204
- }
205
- // determine indent indicator from whitespace at value start
206
- let startWithSpace = false;
207
- let startEnd;
208
- let startNlPos = -1;
209
- for (startEnd = 0; startEnd < value.length; ++startEnd) {
210
- const ch = value[startEnd];
211
- if (ch === ' ')
212
- startWithSpace = true;
213
- else if (ch === '\n')
214
- startNlPos = startEnd;
215
- else
216
- break;
217
- }
218
- let start = value.substring(0, startNlPos < startEnd ? startNlPos + 1 : startEnd);
219
- if (start) {
220
- value = value.substring(start.length);
221
- start = start.replace(/\n+/g, `$&${indent}`);
222
- }
223
- const indentSize = indent ? '2' : '1'; // root is at -1
224
- // Leading | or > is added later
225
- let header = (startWithSpace ? indentSize : '') + chomp;
226
- if (comment) {
227
- header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
228
- if (onComment)
229
- onComment();
230
- }
231
- if (!literal) {
232
- const foldedValue = value
233
- .replace(/\n+/g, '\n$&')
234
- .replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
235
- // ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
236
- .replace(/\n+/g, `$&${indent}`);
237
- let literalFallback = false;
238
- const foldOptions = getFoldOptions(ctx, true);
239
- if (blockQuote !== 'folded' && type !== Scalar.Scalar.BLOCK_FOLDED) {
240
- foldOptions.onOverflow = () => {
241
- literalFallback = true;
242
- };
243
- }
244
- const body = foldFlowLines.foldFlowLines(`${start}${foldedValue}${end}`, indent, foldFlowLines.FOLD_BLOCK, foldOptions);
245
- if (!literalFallback)
246
- return `>${header}\n${indent}${body}`;
247
- }
248
- value = value.replace(/\n+/g, `$&${indent}`);
249
- return `|${header}\n${indent}${start}${value}${end}`;
250
- }
251
- function plainString(item, ctx, onComment, onChompKeep) {
252
- const { type, value } = item;
253
- const { actualString, implicitKey, indent, indentStep, inFlow } = ctx;
254
- if ((implicitKey && value.includes('\n')) ||
255
- (inFlow && /[[\]{},]/.test(value))) {
256
- return quotedString(value, ctx);
257
- }
258
- if (/^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
259
- // not allowed:
260
- // - '-' or '?'
261
- // - start with an indicator character (except [?:-]) or /[?-] /
262
- // - '\n ', ': ' or ' \n' anywhere
263
- // - '#' not preceded by a non-space char
264
- // - end with ' ' or ':'
265
- return implicitKey || inFlow || !value.includes('\n')
266
- ? quotedString(value, ctx)
267
- : blockString(item, ctx, onComment, onChompKeep);
268
- }
269
- if (!implicitKey &&
270
- !inFlow &&
271
- type !== Scalar.Scalar.PLAIN &&
272
- value.includes('\n')) {
273
- // Where allowed & type not set explicitly, prefer block style for multiline strings
274
- return blockString(item, ctx, onComment, onChompKeep);
275
- }
276
- if (containsDocumentMarker(value)) {
277
- if (indent === '') {
278
- ctx.forceBlockIndent = true;
279
- return blockString(item, ctx, onComment, onChompKeep);
280
- }
281
- else if (implicitKey && indent === indentStep) {
282
- return quotedString(value, ctx);
283
- }
284
- }
285
- const str = value.replace(/\n+/g, `$&\n${indent}`);
286
- // Verify that output will be parsed as a string, as e.g. plain numbers and
287
- // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
288
- // and others in v1.1.
289
- if (actualString) {
290
- const test = (tag) => tag.default && tag.tag !== 'tag:yaml.org,2002:str' && tag.test?.test(str);
291
- const { compat, tags } = ctx.doc.schema;
292
- if (tags.some(test) || compat?.some(test))
293
- return quotedString(value, ctx);
294
- }
295
- return implicitKey
296
- ? str
297
- : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx, false));
298
- }
299
- function stringifyString(item, ctx, onComment, onChompKeep) {
300
- const { implicitKey, inFlow } = ctx;
301
- const ss = typeof item.value === 'string'
302
- ? item
303
- : Object.assign({}, item, { value: String(item.value) });
304
- let { type } = item;
305
- if (type !== Scalar.Scalar.QUOTE_DOUBLE) {
306
- // force double quotes on control characters & unpaired surrogates
307
- if (/[\x00-\x08\x0b-\x1f\x7f-\x9f\u{D800}-\u{DFFF}]/u.test(ss.value))
308
- type = Scalar.Scalar.QUOTE_DOUBLE;
309
- }
310
- const _stringify = (_type) => {
311
- switch (_type) {
312
- case Scalar.Scalar.BLOCK_FOLDED:
313
- case Scalar.Scalar.BLOCK_LITERAL:
314
- return implicitKey || inFlow
315
- ? quotedString(ss.value, ctx) // blocks are not valid inside flow containers
316
- : blockString(ss, ctx, onComment, onChompKeep);
317
- case Scalar.Scalar.QUOTE_DOUBLE:
318
- return doubleQuotedString(ss.value, ctx);
319
- case Scalar.Scalar.QUOTE_SINGLE:
320
- return singleQuotedString(ss.value, ctx);
321
- case Scalar.Scalar.PLAIN:
322
- return plainString(ss, ctx, onComment, onChompKeep);
323
- default:
324
- return null;
325
- }
326
- };
327
- let res = _stringify(type);
328
- if (res === null) {
329
- const { defaultKeyType, defaultStringType } = ctx.options;
330
- const t = (implicitKey && defaultKeyType) || defaultStringType;
331
- res = _stringify(t);
332
- if (res === null)
333
- throw new Error(`Unsupported default string type ${t}`);
334
- }
335
- return res;
336
- }
337
-
338
- exports.stringifyString = stringifyString;
package/dist/visit.d.ts DELETED
@@ -1,102 +0,0 @@
1
- import type { Document } from './doc/Document';
2
- import type { Alias } from './nodes/Alias';
3
- import type { Node } from './nodes/Node';
4
- import type { Pair } from './nodes/Pair';
5
- import type { Scalar } from './nodes/Scalar';
6
- import type { YAMLMap } from './nodes/YAMLMap';
7
- import type { YAMLSeq } from './nodes/YAMLSeq';
8
- export type visitorFn<T> = (key: number | 'key' | 'value' | null, node: T, path: readonly (Document | Node | Pair)[]) => void | symbol | number | Node | Pair;
9
- export type visitor = visitorFn<unknown> | {
10
- Alias?: visitorFn<Alias>;
11
- Collection?: visitorFn<YAMLMap | YAMLSeq>;
12
- Map?: visitorFn<YAMLMap>;
13
- Node?: visitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
14
- Pair?: visitorFn<Pair>;
15
- Scalar?: visitorFn<Scalar>;
16
- Seq?: visitorFn<YAMLSeq>;
17
- Value?: visitorFn<Scalar | YAMLMap | YAMLSeq>;
18
- };
19
- export 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 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
- };
30
- /**
31
- * Apply a visitor to an AST node or document.
32
- *
33
- * Walks through the tree (depth-first) starting from `node`, calling a
34
- * `visitor` function with three arguments:
35
- * - `key`: For sequence values and map `Pair`, the node's index in the
36
- * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
37
- * `null` for the root node.
38
- * - `node`: The current node.
39
- * - `path`: The ancestry of the current node.
40
- *
41
- * The return value of the visitor may be used to control the traversal:
42
- * - `undefined` (default): Do nothing and continue
43
- * - `visit.SKIP`: Do not visit the children of this node, continue with next
44
- * sibling
45
- * - `visit.BREAK`: Terminate traversal completely
46
- * - `visit.REMOVE`: Remove the current node, then continue with the next one
47
- * - `Node`: Replace the current node, then continue by visiting it
48
- * - `number`: While iterating the items of a sequence or map, set the index
49
- * of the next step. This is useful especially if the index of the current
50
- * node has changed.
51
- *
52
- * If `visitor` is a single function, it will be called with all values
53
- * encountered in the tree, including e.g. `null` values. Alternatively,
54
- * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
55
- * `Alias` and `Scalar` node. To define the same visitor function for more than
56
- * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
57
- * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
58
- * specific defined one will be used for each node.
59
- */
60
- export declare function visit(node: Node | Document | null, visitor: visitor): void;
61
- export declare namespace visit {
62
- var BREAK: symbol;
63
- var SKIP: symbol;
64
- var REMOVE: symbol;
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
- }
package/dist/visit.js DELETED
@@ -1,236 +0,0 @@
1
- 'use strict';
2
-
3
- var identity = require('./nodes/identity.js');
4
-
5
- const BREAK = Symbol('break visit');
6
- const SKIP = Symbol('skip children');
7
- const REMOVE = Symbol('remove node');
8
- /**
9
- * Apply a visitor to an AST node or document.
10
- *
11
- * Walks through the tree (depth-first) starting from `node`, calling a
12
- * `visitor` function with three arguments:
13
- * - `key`: For sequence values and map `Pair`, the node's index in the
14
- * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
15
- * `null` for the root node.
16
- * - `node`: The current node.
17
- * - `path`: The ancestry of the current node.
18
- *
19
- * The return value of the visitor may be used to control the traversal:
20
- * - `undefined` (default): Do nothing and continue
21
- * - `visit.SKIP`: Do not visit the children of this node, continue with next
22
- * sibling
23
- * - `visit.BREAK`: Terminate traversal completely
24
- * - `visit.REMOVE`: Remove the current node, then continue with the next one
25
- * - `Node`: Replace the current node, then continue by visiting it
26
- * - `number`: While iterating the items of a sequence or map, set the index
27
- * of the next step. This is useful especially if the index of the current
28
- * node has changed.
29
- *
30
- * If `visitor` is a single function, it will be called with all values
31
- * encountered in the tree, including e.g. `null` values. Alternatively,
32
- * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
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.
37
- */
38
- function visit(node, visitor) {
39
- const visitor_ = initVisitor(visitor);
40
- if (identity.isDocument(node)) {
41
- const cd = visit_(null, node.contents, visitor_, Object.freeze([node]));
42
- if (cd === REMOVE)
43
- node.contents = null;
44
- }
45
- else
46
- visit_(null, node, visitor_, Object.freeze([]));
47
- }
48
- // Without the `as symbol` casts, TS declares these in the `visit`
49
- // namespace using `var`, but then complains about that because
50
- // `unique symbol` must be `const`.
51
- /** Terminate visit traversal completely */
52
- visit.BREAK = BREAK;
53
- /** Do not visit the children of the current node */
54
- visit.SKIP = SKIP;
55
- /** Remove the current node */
56
- visit.REMOVE = REMOVE;
57
- function visit_(key, node, visitor, path) {
58
- const ctrl = callVisitor(key, node, visitor, path);
59
- if (identity.isNode(ctrl) || identity.isPair(ctrl)) {
60
- replaceNode(key, path, ctrl);
61
- return visit_(key, ctrl, visitor, path);
62
- }
63
- if (typeof ctrl !== 'symbol') {
64
- if (identity.isCollection(node)) {
65
- path = Object.freeze(path.concat(node));
66
- for (let i = 0; i < node.items.length; ++i) {
67
- const ci = visit_(i, node.items[i], visitor, path);
68
- if (typeof ci === 'number')
69
- i = ci - 1;
70
- else if (ci === BREAK)
71
- return BREAK;
72
- else if (ci === REMOVE) {
73
- node.items.splice(i, 1);
74
- i -= 1;
75
- }
76
- }
77
- }
78
- else if (identity.isPair(node)) {
79
- path = Object.freeze(path.concat(node));
80
- const ck = visit_('key', node.key, visitor, path);
81
- if (ck === BREAK)
82
- return BREAK;
83
- else if (ck === REMOVE)
84
- node.key = null;
85
- const cv = visit_('value', node.value, visitor, path);
86
- if (cv === BREAK)
87
- return BREAK;
88
- else if (cv === REMOVE)
89
- node.value = null;
90
- }
91
- }
92
- return ctrl;
93
- }
94
- /**
95
- * Apply an async visitor to an AST node or document.
96
- *
97
- * Walks through the tree (depth-first) starting from `node`, calling a
98
- * `visitor` function with three arguments:
99
- * - `key`: For sequence values and map `Pair`, the node's index in the
100
- * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
101
- * `null` for the root node.
102
- * - `node`: The current node.
103
- * - `path`: The ancestry of the current node.
104
- *
105
- * The return value of the visitor may be used to control the traversal:
106
- * - `Promise`: Must resolve to one of the following values
107
- * - `undefined` (default): Do nothing and continue
108
- * - `visit.SKIP`: Do not visit the children of this node, continue with next
109
- * sibling
110
- * - `visit.BREAK`: Terminate traversal completely
111
- * - `visit.REMOVE`: Remove the current node, then continue with the next one
112
- * - `Node`: Replace the current node, then continue by visiting it
113
- * - `number`: While iterating the items of a sequence or map, set the index
114
- * of the next step. This is useful especially if the index of the current
115
- * node has changed.
116
- *
117
- * If `visitor` is a single function, it will be called with all values
118
- * encountered in the tree, including e.g. `null` values. Alternatively,
119
- * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
120
- * `Alias` and `Scalar` node. To define the same visitor function for more than
121
- * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
122
- * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
123
- * specific defined one will be used for each node.
124
- */
125
- async function visitAsync(node, visitor) {
126
- const visitor_ = initVisitor(visitor);
127
- if (identity.isDocument(node)) {
128
- const cd = await visitAsync_(null, node.contents, visitor_, Object.freeze([node]));
129
- if (cd === REMOVE)
130
- node.contents = null;
131
- }
132
- else
133
- await visitAsync_(null, node, visitor_, Object.freeze([]));
134
- }
135
- // Without the `as symbol` casts, TS declares these in the `visit`
136
- // namespace using `var`, but then complains about that because
137
- // `unique symbol` must be `const`.
138
- /** Terminate visit traversal completely */
139
- visitAsync.BREAK = BREAK;
140
- /** Do not visit the children of the current node */
141
- visitAsync.SKIP = SKIP;
142
- /** Remove the current node */
143
- visitAsync.REMOVE = REMOVE;
144
- async function visitAsync_(key, node, visitor, path) {
145
- const ctrl = await callVisitor(key, node, visitor, path);
146
- if (identity.isNode(ctrl) || identity.isPair(ctrl)) {
147
- replaceNode(key, path, ctrl);
148
- return visitAsync_(key, ctrl, visitor, path);
149
- }
150
- if (typeof ctrl !== 'symbol') {
151
- if (identity.isCollection(node)) {
152
- path = Object.freeze(path.concat(node));
153
- for (let i = 0; i < node.items.length; ++i) {
154
- const ci = await visitAsync_(i, node.items[i], visitor, path);
155
- if (typeof ci === 'number')
156
- i = ci - 1;
157
- else if (ci === BREAK)
158
- return BREAK;
159
- else if (ci === REMOVE) {
160
- node.items.splice(i, 1);
161
- i -= 1;
162
- }
163
- }
164
- }
165
- else if (identity.isPair(node)) {
166
- path = Object.freeze(path.concat(node));
167
- const ck = await visitAsync_('key', node.key, visitor, path);
168
- if (ck === BREAK)
169
- return BREAK;
170
- else if (ck === REMOVE)
171
- node.key = null;
172
- const cv = await visitAsync_('value', node.value, visitor, path);
173
- if (cv === BREAK)
174
- return BREAK;
175
- else if (cv === REMOVE)
176
- node.value = null;
177
- }
178
- }
179
- return ctrl;
180
- }
181
- function initVisitor(visitor) {
182
- if (typeof visitor === 'object' &&
183
- (visitor.Collection || visitor.Node || visitor.Value)) {
184
- return Object.assign({
185
- Alias: visitor.Node,
186
- Map: visitor.Node,
187
- Scalar: visitor.Node,
188
- Seq: visitor.Node
189
- }, visitor.Value && {
190
- Map: visitor.Value,
191
- Scalar: visitor.Value,
192
- Seq: visitor.Value
193
- }, visitor.Collection && {
194
- Map: visitor.Collection,
195
- Seq: visitor.Collection
196
- }, visitor);
197
- }
198
- return visitor;
199
- }
200
- function callVisitor(key, node, visitor, path) {
201
- if (typeof visitor === 'function')
202
- return visitor(key, node, path);
203
- if (identity.isMap(node))
204
- return visitor.Map?.(key, node, path);
205
- if (identity.isSeq(node))
206
- return visitor.Seq?.(key, node, path);
207
- if (identity.isPair(node))
208
- return visitor.Pair?.(key, node, path);
209
- if (identity.isScalar(node))
210
- return visitor.Scalar?.(key, node, path);
211
- if (identity.isAlias(node))
212
- return visitor.Alias?.(key, node, path);
213
- return undefined;
214
- }
215
- function replaceNode(key, path, node) {
216
- const parent = path[path.length - 1];
217
- if (identity.isCollection(parent)) {
218
- parent.items[key] = node;
219
- }
220
- else if (identity.isPair(parent)) {
221
- if (key === 'key')
222
- parent.key = node;
223
- else
224
- parent.value = node;
225
- }
226
- else if (identity.isDocument(parent)) {
227
- parent.contents = node;
228
- }
229
- else {
230
- const pt = identity.isAlias(parent) ? 'alias' : 'scalar';
231
- throw new Error(`Cannot replace node with ${pt} parent`);
232
- }
233
- }
234
-
235
- exports.visit = visit;
236
- exports.visitAsync = visitAsync;
package/util.js DELETED
@@ -1,2 +0,0 @@
1
- // Re-exporter for Node.js < 12.16.0
2
- module.exports = require('./dist/util.js')