yaml 2.0.0-8 → 2.0.1
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.
- package/README.md +1 -1
- package/browser/dist/compose/compose-doc.js +3 -2
- package/browser/dist/compose/compose-node.js +14 -3
- package/browser/dist/compose/compose-scalar.js +16 -10
- package/browser/dist/compose/composer.js +6 -7
- package/browser/dist/compose/resolve-block-map.js +24 -9
- package/browser/dist/compose/resolve-block-scalar.js +12 -3
- package/browser/dist/compose/resolve-block-seq.js +5 -0
- package/browser/dist/compose/resolve-flow-collection.js +18 -6
- package/browser/dist/compose/resolve-flow-scalar.js +2 -1
- package/browser/dist/compose/resolve-props.js +8 -0
- package/browser/dist/compose/util-flow-indent-check.js +15 -0
- package/browser/dist/doc/Document.js +49 -16
- package/browser/dist/doc/anchors.js +2 -2
- package/browser/dist/doc/createNode.js +7 -4
- package/browser/dist/doc/directives.js +7 -4
- package/browser/dist/index.js +1 -2
- package/browser/dist/node_modules/tslib/tslib.es6.js +164 -0
- package/browser/dist/nodes/Collection.js +8 -4
- package/browser/dist/nodes/Pair.js +2 -2
- package/browser/dist/nodes/Scalar.js +1 -1
- package/browser/dist/nodes/YAMLMap.js +6 -5
- package/browser/dist/nodes/YAMLSeq.js +2 -2
- package/browser/dist/nodes/addPairToJSMap.js +3 -2
- package/browser/dist/nodes/toJS.js +3 -1
- package/browser/dist/parse/cst-scalar.js +2 -6
- package/browser/dist/parse/cst-visit.js +2 -2
- package/browser/dist/parse/lexer.js +27 -11
- package/browser/dist/parse/parser.js +135 -57
- package/browser/dist/public-api.js +4 -5
- package/browser/dist/schema/Schema.js +14 -4
- package/browser/dist/schema/common/null.js +3 -1
- package/browser/dist/schema/core/float.js +4 -1
- package/browser/dist/schema/tags.js +17 -13
- package/browser/dist/schema/yaml-1.1/float.js +4 -1
- package/browser/dist/schema/yaml-1.1/omap.js +1 -1
- package/browser/dist/schema/yaml-1.1/pairs.js +2 -1
- package/browser/dist/stringify/stringify.js +53 -23
- package/browser/dist/stringify/stringifyCollection.js +115 -85
- package/browser/dist/stringify/stringifyComment.js +18 -12
- package/browser/dist/stringify/stringifyDocument.js +37 -15
- package/browser/dist/stringify/stringifyPair.js +35 -21
- package/browser/dist/stringify/stringifyString.js +41 -43
- package/browser/dist/util.js +3 -0
- package/browser/dist/visit.js +150 -62
- package/dist/compose/compose-doc.js +3 -2
- package/dist/compose/compose-node.d.ts +1 -0
- package/dist/compose/compose-node.js +14 -3
- package/dist/compose/compose-scalar.js +15 -9
- package/dist/compose/composer.d.ts +1 -1
- package/dist/compose/composer.js +8 -9
- package/dist/compose/resolve-block-map.js +24 -9
- package/dist/compose/resolve-block-scalar.js +12 -3
- package/dist/compose/resolve-block-seq.js +5 -0
- package/dist/compose/resolve-flow-collection.js +18 -6
- package/dist/compose/resolve-flow-scalar.js +2 -1
- package/dist/compose/resolve-props.d.ts +2 -1
- package/dist/compose/resolve-props.js +8 -0
- package/dist/compose/util-flow-indent-check.d.ts +3 -0
- package/dist/compose/util-flow-indent-check.js +17 -0
- package/dist/doc/Document.d.ts +7 -4
- package/dist/doc/Document.js +56 -23
- package/dist/doc/anchors.d.ts +2 -2
- package/dist/doc/anchors.js +2 -2
- package/dist/doc/createNode.d.ts +1 -1
- package/dist/doc/createNode.js +7 -4
- package/dist/doc/directives.d.ts +4 -2
- package/dist/doc/directives.js +7 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -2
- package/dist/node_modules/tslib/tslib.es6.js +76 -0
- package/dist/nodes/Alias.d.ts +7 -3
- package/dist/nodes/Collection.d.ts +4 -4
- package/dist/nodes/Collection.js +8 -4
- package/dist/nodes/Node.d.ts +3 -0
- package/dist/nodes/Pair.d.ts +3 -0
- package/dist/nodes/Pair.js +2 -2
- package/dist/nodes/Scalar.d.ts +2 -0
- package/dist/nodes/Scalar.js +1 -1
- package/dist/nodes/YAMLMap.d.ts +5 -1
- package/dist/nodes/YAMLMap.js +6 -5
- package/dist/nodes/YAMLSeq.d.ts +2 -0
- package/dist/nodes/YAMLSeq.js +2 -2
- package/dist/nodes/addPairToJSMap.js +2 -1
- package/dist/nodes/toJS.js +3 -1
- package/dist/options.d.ts +60 -15
- package/dist/parse/cst-scalar.d.ts +6 -0
- package/dist/parse/cst-scalar.js +2 -6
- package/dist/parse/cst-visit.js +2 -2
- package/dist/parse/lexer.js +27 -11
- package/dist/parse/parser.js +135 -57
- package/dist/public-api.js +4 -5
- package/dist/schema/Schema.d.ts +5 -4
- package/dist/schema/Schema.js +13 -3
- package/dist/schema/common/null.js +3 -1
- package/dist/schema/core/float.js +4 -1
- package/dist/schema/tags.d.ts +1 -2
- package/dist/schema/tags.js +17 -13
- package/dist/schema/yaml-1.1/float.js +4 -1
- package/dist/schema/yaml-1.1/omap.js +1 -1
- package/dist/schema/yaml-1.1/pairs.js +2 -1
- package/dist/stringify/stringify.d.ts +5 -3
- package/dist/stringify/stringify.js +53 -23
- package/dist/stringify/stringifyCollection.d.ts +2 -6
- package/dist/stringify/stringifyCollection.js +114 -84
- package/dist/stringify/stringifyComment.d.ts +10 -2
- package/dist/stringify/stringifyComment.js +19 -12
- package/dist/stringify/stringifyDocument.js +36 -14
- package/dist/stringify/stringifyPair.js +34 -20
- package/dist/stringify/stringifyString.js +41 -43
- package/dist/test-events.d.ts +1 -1
- package/dist/test-events.js +13 -12
- package/dist/util.d.ts +3 -0
- package/dist/util.js +6 -0
- package/dist/visit.d.ts +49 -10
- package/dist/visit.js +149 -60
- package/package.json +10 -8
- package/util.d.ts +3 -0
- package/browser/dist/options.js +0 -17
- package/dist/options.js +0 -19
|
@@ -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.
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
|
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 =
|
|
147
|
-
?
|
|
148
|
-
: type === Scalar.Scalar.
|
|
149
|
-
?
|
|
150
|
-
:
|
|
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 += '
|
|
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
|
|
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.
|
|
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.
|
|
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
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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
|
-
?
|
|
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);
|
package/dist/test-events.d.ts
CHANGED
package/dist/test-events.js
CHANGED
|
@@ -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
|
-
|
|
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.
|
|
44
|
+
if (doc.directives.docStart)
|
|
46
45
|
docStart += ' ---';
|
|
47
|
-
else if (doc.contents &&
|
|
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 (
|
|
53
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
+
}
|
package/dist/visit.js
CHANGED
|
@@ -36,29 +36,14 @@ const REMOVE = Symbol('remove node');
|
|
|
36
36
|
* specific defined one will be used for each node.
|
|
37
37
|
*/
|
|
38
38
|
function visit(node, visitor) {
|
|
39
|
-
|
|
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
|
-
}
|
|
39
|
+
const visitor_ = initVisitor(visitor);
|
|
55
40
|
if (Node.isDocument(node)) {
|
|
56
|
-
const cd =
|
|
41
|
+
const cd = visit_(null, node.contents, visitor_, Object.freeze([node]));
|
|
57
42
|
if (cd === REMOVE)
|
|
58
43
|
node.contents = null;
|
|
59
44
|
}
|
|
60
45
|
else
|
|
61
|
-
|
|
46
|
+
visit_(null, node, visitor_, Object.freeze([]));
|
|
62
47
|
}
|
|
63
48
|
// Without the `as symbol` casts, TS declares these in the `visit`
|
|
64
49
|
// namespace using `var`, but then complains about that because
|
|
@@ -69,55 +54,104 @@ visit.BREAK = BREAK;
|
|
|
69
54
|
visit.SKIP = SKIP;
|
|
70
55
|
/** Remove the current node */
|
|
71
56
|
visit.REMOVE = REMOVE;
|
|
72
|
-
function
|
|
73
|
-
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (visitor.Map)
|
|
78
|
-
ctrl = visitor.Map(key, node, path);
|
|
79
|
-
}
|
|
80
|
-
else if (Node.isSeq(node)) {
|
|
81
|
-
if (visitor.Seq)
|
|
82
|
-
ctrl = visitor.Seq(key, node, path);
|
|
83
|
-
}
|
|
84
|
-
else if (Node.isPair(node)) {
|
|
85
|
-
if (visitor.Pair)
|
|
86
|
-
ctrl = visitor.Pair(key, node, path);
|
|
57
|
+
function visit_(key, node, visitor, path) {
|
|
58
|
+
const ctrl = callVisitor(key, node, visitor, path);
|
|
59
|
+
if (Node.isNode(ctrl) || Node.isPair(ctrl)) {
|
|
60
|
+
replaceNode(key, path, ctrl);
|
|
61
|
+
return visit_(key, ctrl, visitor, path);
|
|
87
62
|
}
|
|
88
|
-
|
|
89
|
-
if (
|
|
90
|
-
|
|
63
|
+
if (typeof ctrl !== 'symbol') {
|
|
64
|
+
if (Node.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 (Node.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
91
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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 (Node.isDocument(node)) {
|
|
128
|
+
const cd = await visitAsync_(null, node.contents, visitor_, Object.freeze([node]));
|
|
129
|
+
if (cd === REMOVE)
|
|
130
|
+
node.contents = null;
|
|
95
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);
|
|
96
146
|
if (Node.isNode(ctrl) || Node.isPair(ctrl)) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
parent.items[key] = ctrl;
|
|
100
|
-
}
|
|
101
|
-
else if (Node.isPair(parent)) {
|
|
102
|
-
if (key === 'key')
|
|
103
|
-
parent.key = ctrl;
|
|
104
|
-
else
|
|
105
|
-
parent.value = ctrl;
|
|
106
|
-
}
|
|
107
|
-
else if (Node.isDocument(parent)) {
|
|
108
|
-
parent.contents = ctrl;
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
const pt = Node.isAlias(parent) ? 'alias' : 'scalar';
|
|
112
|
-
throw new Error(`Cannot replace node with ${pt} parent`);
|
|
113
|
-
}
|
|
114
|
-
return _visit(key, ctrl, visitor, path);
|
|
147
|
+
replaceNode(key, path, ctrl);
|
|
148
|
+
return visitAsync_(key, ctrl, visitor, path);
|
|
115
149
|
}
|
|
116
150
|
if (typeof ctrl !== 'symbol') {
|
|
117
151
|
if (Node.isCollection(node)) {
|
|
118
152
|
path = Object.freeze(path.concat(node));
|
|
119
153
|
for (let i = 0; i < node.items.length; ++i) {
|
|
120
|
-
const ci =
|
|
154
|
+
const ci = await visitAsync_(i, node.items[i], visitor, path);
|
|
121
155
|
if (typeof ci === 'number')
|
|
122
156
|
i = ci - 1;
|
|
123
157
|
else if (ci === BREAK)
|
|
@@ -130,12 +164,12 @@ function _visit(key, node, visitor, path) {
|
|
|
130
164
|
}
|
|
131
165
|
else if (Node.isPair(node)) {
|
|
132
166
|
path = Object.freeze(path.concat(node));
|
|
133
|
-
const ck =
|
|
167
|
+
const ck = await visitAsync_('key', node.key, visitor, path);
|
|
134
168
|
if (ck === BREAK)
|
|
135
169
|
return BREAK;
|
|
136
170
|
else if (ck === REMOVE)
|
|
137
171
|
node.key = null;
|
|
138
|
-
const cv =
|
|
172
|
+
const cv = await visitAsync_('value', node.value, visitor, path);
|
|
139
173
|
if (cv === BREAK)
|
|
140
174
|
return BREAK;
|
|
141
175
|
else if (cv === REMOVE)
|
|
@@ -144,5 +178,60 @@ function _visit(key, node, visitor, path) {
|
|
|
144
178
|
}
|
|
145
179
|
return ctrl;
|
|
146
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
|
+
var _a, _b, _c, _d, _e;
|
|
202
|
+
if (typeof visitor === 'function')
|
|
203
|
+
return visitor(key, node, path);
|
|
204
|
+
if (Node.isMap(node))
|
|
205
|
+
return (_a = visitor.Map) === null || _a === void 0 ? void 0 : _a.call(visitor, key, node, path);
|
|
206
|
+
if (Node.isSeq(node))
|
|
207
|
+
return (_b = visitor.Seq) === null || _b === void 0 ? void 0 : _b.call(visitor, key, node, path);
|
|
208
|
+
if (Node.isPair(node))
|
|
209
|
+
return (_c = visitor.Pair) === null || _c === void 0 ? void 0 : _c.call(visitor, key, node, path);
|
|
210
|
+
if (Node.isScalar(node))
|
|
211
|
+
return (_d = visitor.Scalar) === null || _d === void 0 ? void 0 : _d.call(visitor, key, node, path);
|
|
212
|
+
if (Node.isAlias(node))
|
|
213
|
+
return (_e = visitor.Alias) === null || _e === void 0 ? void 0 : _e.call(visitor, key, node, path);
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
function replaceNode(key, path, node) {
|
|
217
|
+
const parent = path[path.length - 1];
|
|
218
|
+
if (Node.isCollection(parent)) {
|
|
219
|
+
parent.items[key] = node;
|
|
220
|
+
}
|
|
221
|
+
else if (Node.isPair(parent)) {
|
|
222
|
+
if (key === 'key')
|
|
223
|
+
parent.key = node;
|
|
224
|
+
else
|
|
225
|
+
parent.value = node;
|
|
226
|
+
}
|
|
227
|
+
else if (Node.isDocument(parent)) {
|
|
228
|
+
parent.contents = node;
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
const pt = Node.isAlias(parent) ? 'alias' : 'scalar';
|
|
232
|
+
throw new Error(`Cannot replace node with ${pt} parent`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
147
235
|
|
|
148
236
|
exports.visit = visit;
|
|
237
|
+
exports.visitAsync = visitAsync;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yaml",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"author": "Eemeli Aro <eemeli@gmail.com>",
|
|
6
6
|
"repository": "github:eemeli/yaml",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"files": [
|
|
15
15
|
"browser/",
|
|
16
16
|
"dist/",
|
|
17
|
+
"util.d.ts",
|
|
17
18
|
"util.js"
|
|
18
19
|
],
|
|
19
20
|
"type": "commonjs",
|
|
@@ -44,9 +45,10 @@
|
|
|
44
45
|
"prestart": "npm run build:node",
|
|
45
46
|
"start": "node -i -e 'YAML=require(\"./dist/index.js\")'",
|
|
46
47
|
"test": "jest --config config/jest.config.js",
|
|
48
|
+
"test:all": "npm test && npm run test:types && npm run test:dist && npm run test:dist:types",
|
|
47
49
|
"test:browsers": "cd playground && npm test",
|
|
48
50
|
"test:dist": "npm run build:node && jest --config config/jest.config.js",
|
|
49
|
-
"test:dist:types": "tsc --allowJs --moduleResolution node --noEmit --target
|
|
51
|
+
"test:dist:types": "tsc --allowJs --moduleResolution node --noEmit --target es5 dist/index.js",
|
|
50
52
|
"test:types": "tsc --noEmit",
|
|
51
53
|
"docs:install": "cd docs-slate && bundle install",
|
|
52
54
|
"docs:deploy": "cd docs-slate && ./deploy.sh",
|
|
@@ -68,15 +70,15 @@
|
|
|
68
70
|
"@babel/plugin-transform-typescript": "^7.12.17",
|
|
69
71
|
"@babel/preset-env": "^7.12.11",
|
|
70
72
|
"@rollup/plugin-babel": "^5.2.3",
|
|
71
|
-
"@rollup/plugin-replace": "^
|
|
73
|
+
"@rollup/plugin-replace": "^4.0.0",
|
|
72
74
|
"@rollup/plugin-typescript": "^8.1.1",
|
|
73
75
|
"@types/jest": "^27.0.1",
|
|
74
|
-
"@types/node": "^
|
|
75
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
76
|
-
"@typescript-eslint/parser": "^
|
|
76
|
+
"@types/node": "^12.20.47",
|
|
77
|
+
"@typescript-eslint/eslint-plugin": "^5.3.1",
|
|
78
|
+
"@typescript-eslint/parser": "^5.3.1",
|
|
77
79
|
"babel-jest": "^27.0.1",
|
|
78
80
|
"cross-env": "^7.0.3",
|
|
79
|
-
"eslint": "^
|
|
81
|
+
"eslint": "^8.2.0",
|
|
80
82
|
"eslint-config-prettier": "^8.1.0",
|
|
81
83
|
"fast-check": "^2.12.0",
|
|
82
84
|
"jest": "^27.0.1",
|
|
@@ -87,6 +89,6 @@
|
|
|
87
89
|
"typescript": "^4.3.5"
|
|
88
90
|
},
|
|
89
91
|
"engines": {
|
|
90
|
-
"node": ">=
|
|
92
|
+
"node": ">= 14"
|
|
91
93
|
}
|
|
92
94
|
}
|
package/util.d.ts
ADDED
package/browser/dist/options.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `yaml` defines document-specific options in three places: as an argument of
|
|
3
|
-
* parse, create and stringify calls, in the values of `YAML.defaultOptions`,
|
|
4
|
-
* and in the version-dependent `YAML.Document.defaults` object. Values set in
|
|
5
|
-
* `YAML.defaultOptions` override version-dependent defaults, and argument
|
|
6
|
-
* options override both.
|
|
7
|
-
*/
|
|
8
|
-
const defaultOptions = {
|
|
9
|
-
intAsBigInt: false,
|
|
10
|
-
logLevel: 'warn',
|
|
11
|
-
prettyErrors: true,
|
|
12
|
-
strict: true,
|
|
13
|
-
uniqueKeys: true,
|
|
14
|
-
version: '1.2'
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export { defaultOptions };
|