yaml 2.0.1 → 2.1.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.
- package/browser/dist/compose/compose-collection.js +1 -1
- package/browser/dist/compose/compose-doc.js +2 -2
- package/browser/dist/compose/compose-scalar.js +6 -7
- package/browser/dist/compose/composer.js +2 -3
- package/browser/dist/compose/resolve-block-map.js +5 -6
- package/browser/dist/compose/resolve-block-scalar.js +1 -1
- package/browser/dist/compose/resolve-flow-collection.js +3 -4
- package/browser/dist/compose/resolve-flow-scalar.js +1 -2
- package/browser/dist/compose/resolve-props.js +2 -2
- package/browser/dist/compose/util-empty-scalar-position.js +1 -1
- package/browser/dist/compose/util-flow-indent-check.js +1 -1
- package/browser/dist/doc/Document.js +6 -6
- package/browser/dist/doc/createNode.js +5 -7
- package/browser/dist/nodes/Collection.js +2 -1
- package/browser/dist/nodes/Pair.js +2 -2
- package/browser/dist/nodes/Scalar.js +1 -1
- package/browser/dist/nodes/YAMLMap.js +6 -7
- package/browser/dist/nodes/YAMLSeq.js +1 -9
- package/browser/dist/nodes/addPairToJSMap.js +1 -1
- package/browser/dist/nodes/toJS.js +1 -1
- package/browser/dist/parse/cst-scalar.js +1 -2
- package/browser/dist/parse/cst-visit.js +2 -2
- package/browser/dist/parse/lexer.js +1 -2
- package/browser/dist/parse/parser.js +10 -14
- package/browser/dist/public-api.js +3 -4
- package/browser/dist/schema/Schema.js +1 -1
- package/browser/dist/schema/yaml-1.1/omap.js +1 -1
- package/browser/dist/schema/yaml-1.1/pairs.js +1 -2
- package/browser/dist/schema/yaml-1.1/set.js +4 -0
- package/browser/dist/stringify/stringify.js +6 -8
- package/browser/dist/stringify/stringifyCollection.js +1 -2
- package/browser/dist/stringify/stringifyDocument.js +1 -2
- package/browser/dist/stringify/stringifyString.js +2 -2
- package/browser/dist/visit.js +5 -6
- package/dist/compose/compose-collection.js +1 -1
- package/dist/compose/compose-doc.js +2 -2
- package/dist/compose/compose-scalar.js +6 -7
- package/dist/compose/composer.js +2 -3
- package/dist/compose/resolve-block-map.js +5 -6
- package/dist/compose/resolve-block-scalar.js +1 -1
- package/dist/compose/resolve-flow-collection.js +3 -4
- package/dist/compose/resolve-flow-scalar.js +1 -2
- package/dist/compose/resolve-props.js +2 -2
- package/dist/compose/util-empty-scalar-position.js +1 -1
- package/dist/compose/util-flow-indent-check.js +1 -1
- package/dist/doc/Document.d.ts +10 -10
- package/dist/doc/Document.js +6 -6
- package/dist/doc/anchors.d.ts +1 -1
- package/dist/doc/createNode.js +5 -7
- package/dist/index.d.ts +2 -0
- package/dist/nodes/Collection.d.ts +2 -2
- package/dist/nodes/Collection.js +2 -1
- package/dist/nodes/Node.d.ts +14 -10
- package/dist/nodes/Pair.d.ts +1 -1
- package/dist/nodes/Pair.js +2 -2
- package/dist/nodes/Scalar.js +1 -1
- package/dist/nodes/YAMLMap.d.ts +6 -3
- package/dist/nodes/YAMLMap.js +6 -7
- package/dist/nodes/YAMLSeq.d.ts +4 -1
- package/dist/nodes/YAMLSeq.js +1 -9
- package/dist/nodes/addPairToJSMap.js +1 -1
- package/dist/nodes/toJS.js +1 -1
- package/dist/options.d.ts +2 -1
- package/dist/parse/cst-scalar.js +1 -2
- package/dist/parse/cst-visit.js +2 -2
- package/dist/parse/lexer.js +1 -2
- package/dist/parse/parser.js +10 -14
- package/dist/public-api.js +3 -4
- package/dist/schema/Schema.js +1 -1
- package/dist/schema/json-schema.d.ts +69 -0
- package/dist/schema/yaml-1.1/omap.d.ts +7 -3
- package/dist/schema/yaml-1.1/omap.js +1 -1
- package/dist/schema/yaml-1.1/pairs.js +1 -2
- package/dist/schema/yaml-1.1/set.d.ts +6 -2
- package/dist/schema/yaml-1.1/set.js +4 -0
- package/dist/stringify/stringify.js +6 -8
- package/dist/stringify/stringifyCollection.js +1 -2
- package/dist/stringify/stringifyDocument.js +1 -2
- package/dist/stringify/stringifyString.js +2 -2
- package/dist/test-events.js +2 -3
- package/dist/visit.js +5 -6
- package/package.json +3 -3
- package/dist/node_modules/tslib/tslib.es6.js +0 -76
|
@@ -51,7 +51,7 @@ function composeCollection(CN, ctx, token, tagToken, onError) {
|
|
|
51
51
|
: new Scalar(res);
|
|
52
52
|
node.range = coll.range;
|
|
53
53
|
node.tag = tagName;
|
|
54
|
-
if (tag
|
|
54
|
+
if (tag?.format)
|
|
55
55
|
node.format = tag.format;
|
|
56
56
|
return node;
|
|
57
57
|
}
|
|
@@ -4,7 +4,7 @@ import { resolveEnd } from './resolve-end.js';
|
|
|
4
4
|
import { resolveProps } from './resolve-props.js';
|
|
5
5
|
|
|
6
6
|
function composeDoc(options, directives, { offset, start, value, end }, onError) {
|
|
7
|
-
const opts = Object.assign({ directives }, options);
|
|
7
|
+
const opts = Object.assign({ _directives: directives }, options);
|
|
8
8
|
const doc = new Document(undefined, opts);
|
|
9
9
|
const ctx = {
|
|
10
10
|
atRoot: true,
|
|
@@ -14,7 +14,7 @@ function composeDoc(options, directives, { offset, start, value, end }, onError)
|
|
|
14
14
|
};
|
|
15
15
|
const props = resolveProps(start, {
|
|
16
16
|
indicator: 'doc-start',
|
|
17
|
-
next: value
|
|
17
|
+
next: value ?? end?.[0],
|
|
18
18
|
offset,
|
|
19
19
|
onError,
|
|
20
20
|
startOnNewline: true
|
|
@@ -17,12 +17,12 @@ function composeScalar(ctx, token, tagToken, onError) {
|
|
|
17
17
|
: ctx.schema[SCALAR];
|
|
18
18
|
let scalar;
|
|
19
19
|
try {
|
|
20
|
-
const res = tag.resolve(value, msg => onError(tagToken
|
|
20
|
+
const res = tag.resolve(value, msg => onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
|
|
21
21
|
scalar = isScalar(res) ? res : new Scalar(res);
|
|
22
22
|
}
|
|
23
23
|
catch (error) {
|
|
24
24
|
const msg = error instanceof Error ? error.message : String(error);
|
|
25
|
-
onError(tagToken
|
|
25
|
+
onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg);
|
|
26
26
|
scalar = new Scalar(value);
|
|
27
27
|
}
|
|
28
28
|
scalar.range = range;
|
|
@@ -38,7 +38,6 @@ function composeScalar(ctx, token, tagToken, onError) {
|
|
|
38
38
|
return scalar;
|
|
39
39
|
}
|
|
40
40
|
function findScalarTagByName(schema, value, tagName, tagToken, onError) {
|
|
41
|
-
var _a;
|
|
42
41
|
if (tagName === '!')
|
|
43
42
|
return schema[SCALAR]; // non-specific tag
|
|
44
43
|
const matchWithTest = [];
|
|
@@ -51,7 +50,7 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
|
|
|
51
50
|
}
|
|
52
51
|
}
|
|
53
52
|
for (const tag of matchWithTest)
|
|
54
|
-
if (
|
|
53
|
+
if (tag.test?.test(value))
|
|
55
54
|
return tag;
|
|
56
55
|
const kt = schema.knownTags[tagName];
|
|
57
56
|
if (kt && !kt.collection) {
|
|
@@ -64,10 +63,10 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
|
|
|
64
63
|
return schema[SCALAR];
|
|
65
64
|
}
|
|
66
65
|
function findScalarTagByTest({ directives, schema }, value, token, onError) {
|
|
67
|
-
|
|
68
|
-
const tag = schema.tags.find(tag => { var _a; return tag.default && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(value)); }) || schema[SCALAR];
|
|
66
|
+
const tag = schema.tags.find(tag => tag.default && tag.test?.test(value)) || schema[SCALAR];
|
|
69
67
|
if (schema.compat) {
|
|
70
|
-
const compat =
|
|
68
|
+
const compat = schema.compat.find(tag => tag.default && tag.test?.test(value)) ??
|
|
69
|
+
schema[SCALAR];
|
|
71
70
|
if (tag.tag !== compat.tag) {
|
|
72
71
|
const ts = directives.tagString(tag.tag);
|
|
73
72
|
const cs = directives.tagString(compat.tag);
|
|
@@ -14,7 +14,6 @@ function getErrorPos(src) {
|
|
|
14
14
|
return [offset, offset + (typeof source === 'string' ? source.length : 1)];
|
|
15
15
|
}
|
|
16
16
|
function parsePrelude(prelude) {
|
|
17
|
-
var _a;
|
|
18
17
|
let comment = '';
|
|
19
18
|
let atComment = false;
|
|
20
19
|
let afterEmptyLine = false;
|
|
@@ -29,7 +28,7 @@ function parsePrelude(prelude) {
|
|
|
29
28
|
afterEmptyLine = false;
|
|
30
29
|
break;
|
|
31
30
|
case '%':
|
|
32
|
-
if (
|
|
31
|
+
if (prelude[i + 1]?.[0] !== '#')
|
|
33
32
|
i += 1;
|
|
34
33
|
atComment = false;
|
|
35
34
|
break;
|
|
@@ -204,7 +203,7 @@ class Composer {
|
|
|
204
203
|
this.doc = null;
|
|
205
204
|
}
|
|
206
205
|
else if (forceDoc) {
|
|
207
|
-
const opts = Object.assign({
|
|
206
|
+
const opts = Object.assign({ _directives: this.directives }, this.options);
|
|
208
207
|
const doc = new Document(undefined, opts);
|
|
209
208
|
if (this.atDirectives)
|
|
210
209
|
this.onError(endOffset, 'MISSING_CHAR', 'Missing directives-end indicator line');
|
|
@@ -7,7 +7,6 @@ import { mapIncludes } from './util-map-includes.js';
|
|
|
7
7
|
|
|
8
8
|
const startColMsg = 'All mapping items must start at the same column';
|
|
9
9
|
function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
|
|
10
|
-
var _a;
|
|
11
10
|
const map = new YAMLMap(ctx.schema);
|
|
12
11
|
if (ctx.atRoot)
|
|
13
12
|
ctx.atRoot = false;
|
|
@@ -17,7 +16,7 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
|
|
|
17
16
|
// key properties
|
|
18
17
|
const keyProps = resolveProps(start, {
|
|
19
18
|
indicator: 'explicit-key-ind',
|
|
20
|
-
next: key
|
|
19
|
+
next: key ?? sep?.[0],
|
|
21
20
|
offset,
|
|
22
21
|
onError,
|
|
23
22
|
startOnNewline: true
|
|
@@ -41,10 +40,10 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
|
|
|
41
40
|
continue;
|
|
42
41
|
}
|
|
43
42
|
if (keyProps.hasNewlineAfterProp || containsNewline(key)) {
|
|
44
|
-
onError(key
|
|
43
|
+
onError(key ?? start[start.length - 1], 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
|
|
45
44
|
}
|
|
46
45
|
}
|
|
47
|
-
else if (
|
|
46
|
+
else if (keyProps.found?.indent !== bm.indent) {
|
|
48
47
|
onError(offset, 'BAD_INDENT', startColMsg);
|
|
49
48
|
}
|
|
50
49
|
// key value
|
|
@@ -57,7 +56,7 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
|
|
|
57
56
|
if (mapIncludes(ctx, map.items, keyNode))
|
|
58
57
|
onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
|
|
59
58
|
// value properties
|
|
60
|
-
const valueProps = resolveProps(sep
|
|
59
|
+
const valueProps = resolveProps(sep ?? [], {
|
|
61
60
|
indicator: 'map-value-ind',
|
|
62
61
|
next: value,
|
|
63
62
|
offset: keyNode.range[2],
|
|
@@ -67,7 +66,7 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
|
|
|
67
66
|
offset = valueProps.end;
|
|
68
67
|
if (valueProps.found) {
|
|
69
68
|
if (implicitKey) {
|
|
70
|
-
if (
|
|
69
|
+
if (value?.type === 'block-map' && !valueProps.hasNewline)
|
|
71
70
|
onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
|
|
72
71
|
if (ctx.options.strict &&
|
|
73
72
|
keyProps.start < valueProps.found.offset - 1024)
|
|
@@ -182,7 +182,7 @@ function splitLines(source) {
|
|
|
182
182
|
const split = source.split(/\n( *)/);
|
|
183
183
|
const first = split[0];
|
|
184
184
|
const m = first.match(/^( *)/);
|
|
185
|
-
const line0 =
|
|
185
|
+
const line0 = m?.[1]
|
|
186
186
|
? [m[1], first.slice(m[1].length)]
|
|
187
187
|
: ['', first];
|
|
188
188
|
const lines = [line0];
|
|
@@ -10,7 +10,6 @@ import { mapIncludes } from './util-map-includes.js';
|
|
|
10
10
|
const blockMsg = 'Block collections are not allowed within flow collections';
|
|
11
11
|
const isBlock = (token) => token && (token.type === 'block-map' || token.type === 'block-seq');
|
|
12
12
|
function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError) {
|
|
13
|
-
var _a;
|
|
14
13
|
const isMap = fc.start.source === '{';
|
|
15
14
|
const fcName = isMap ? 'flow map' : 'flow sequence';
|
|
16
15
|
const coll = isMap
|
|
@@ -27,7 +26,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
|
|
|
27
26
|
const props = resolveProps(start, {
|
|
28
27
|
flow: fcName,
|
|
29
28
|
indicator: 'explicit-key-ind',
|
|
30
|
-
next: key
|
|
29
|
+
next: key ?? sep?.[0],
|
|
31
30
|
offset,
|
|
32
31
|
onError,
|
|
33
32
|
startOnNewline: false
|
|
@@ -75,7 +74,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
|
|
|
75
74
|
if (prevItemComment) {
|
|
76
75
|
let prev = coll.items[coll.items.length - 1];
|
|
77
76
|
if (isPair(prev))
|
|
78
|
-
prev =
|
|
77
|
+
prev = prev.value ?? prev.key;
|
|
79
78
|
if (prev.comment)
|
|
80
79
|
prev.comment += '\n' + prevItemComment;
|
|
81
80
|
else
|
|
@@ -105,7 +104,7 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
|
|
|
105
104
|
if (isBlock(key))
|
|
106
105
|
onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
|
|
107
106
|
// value properties
|
|
108
|
-
const valueProps = resolveProps(sep
|
|
107
|
+
const valueProps = resolveProps(sep ?? [], {
|
|
109
108
|
flow: fcName,
|
|
110
109
|
indicator: 'map-value-ind',
|
|
111
110
|
next: value,
|
|
@@ -72,7 +72,6 @@ function singleQuotedValue(source, onError) {
|
|
|
72
72
|
return foldLines(source.slice(1, -1)).replace(/''/g, "'");
|
|
73
73
|
}
|
|
74
74
|
function foldLines(source) {
|
|
75
|
-
var _a;
|
|
76
75
|
/**
|
|
77
76
|
* The negative lookbehind here and in the `re` RegExp is to
|
|
78
77
|
* prevent causing a polynomial search time in certain cases.
|
|
@@ -112,7 +111,7 @@ function foldLines(source) {
|
|
|
112
111
|
const last = /[ \t]*(.*)/sy;
|
|
113
112
|
last.lastIndex = pos;
|
|
114
113
|
match = last.exec(source);
|
|
115
|
-
return res + sep + (
|
|
114
|
+
return res + sep + (match?.[1] ?? '');
|
|
116
115
|
}
|
|
117
116
|
function doubleQuotedValue(source, onError) {
|
|
118
117
|
let res = '';
|
|
@@ -87,7 +87,7 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
|
|
|
87
87
|
if (anchor || tag)
|
|
88
88
|
onError(token, 'BAD_PROP_ORDER', `Anchors and tags must be after the ${token.source} indicator`);
|
|
89
89
|
if (found)
|
|
90
|
-
onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.source} in ${flow
|
|
90
|
+
onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.source} in ${flow ?? 'collection'}`);
|
|
91
91
|
found = token;
|
|
92
92
|
atNewline = false;
|
|
93
93
|
hasSpace = false;
|
|
@@ -127,7 +127,7 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, startOnN
|
|
|
127
127
|
anchor,
|
|
128
128
|
tag,
|
|
129
129
|
end,
|
|
130
|
-
start: start
|
|
130
|
+
start: start ?? end
|
|
131
131
|
};
|
|
132
132
|
}
|
|
133
133
|
|
|
@@ -14,7 +14,7 @@ function emptyScalarPosition(offset, before, pos) {
|
|
|
14
14
|
// Technically, an empty scalar is immediately after the last non-empty
|
|
15
15
|
// node, but it's more useful to place it after any whitespace.
|
|
16
16
|
st = before[++i];
|
|
17
|
-
while (
|
|
17
|
+
while (st?.type === 'space') {
|
|
18
18
|
offset += st.source.length;
|
|
19
19
|
st = before[++i];
|
|
20
20
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { containsNewline } from './util-contains-newline.js';
|
|
2
2
|
|
|
3
3
|
function flowIndentCheck(indent, fc, onError) {
|
|
4
|
-
if (
|
|
4
|
+
if (fc?.type === 'flow-collection') {
|
|
5
5
|
const end = fc.end[0];
|
|
6
6
|
if (end.indent === indent &&
|
|
7
7
|
(end.source === ']' || end.source === '}') &&
|
|
@@ -41,8 +41,8 @@ class Document {
|
|
|
41
41
|
}, options);
|
|
42
42
|
this.options = opt;
|
|
43
43
|
let { version } = opt;
|
|
44
|
-
if (options
|
|
45
|
-
this.directives = options.
|
|
44
|
+
if (options?._directives) {
|
|
45
|
+
this.directives = options._directives.atDocument();
|
|
46
46
|
if (this.directives.yaml.explicit)
|
|
47
47
|
version = this.directives.yaml.version;
|
|
48
48
|
}
|
|
@@ -124,13 +124,13 @@ class Document {
|
|
|
124
124
|
options = replacer;
|
|
125
125
|
replacer = undefined;
|
|
126
126
|
}
|
|
127
|
-
const { aliasDuplicateObjects, anchorPrefix, flow, keepUndefined, onTagObj, tag } = options
|
|
127
|
+
const { aliasDuplicateObjects, anchorPrefix, flow, keepUndefined, onTagObj, tag } = options ?? {};
|
|
128
128
|
const { onAnchor, setAnchors, sourceObjects } = createNodeAnchors(this,
|
|
129
129
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
130
130
|
anchorPrefix || 'a');
|
|
131
131
|
const ctx = {
|
|
132
|
-
aliasDuplicateObjects: aliasDuplicateObjects
|
|
133
|
-
keepUndefined: keepUndefined
|
|
132
|
+
aliasDuplicateObjects: aliasDuplicateObjects ?? true,
|
|
133
|
+
keepUndefined: keepUndefined ?? false,
|
|
134
134
|
onAnchor,
|
|
135
135
|
onTagObj,
|
|
136
136
|
replacer: _replacer,
|
|
@@ -294,7 +294,7 @@ class Document {
|
|
|
294
294
|
maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100,
|
|
295
295
|
stringify
|
|
296
296
|
};
|
|
297
|
-
const res = toJS(this.contents, jsonArg
|
|
297
|
+
const res = toJS(this.contents, jsonArg ?? '', ctx);
|
|
298
298
|
if (typeof onAnchor === 'function')
|
|
299
299
|
for (const { count, res } of ctx.anchors.values())
|
|
300
300
|
onAnchor(res, count);
|
|
@@ -4,24 +4,22 @@ import { Scalar } from '../nodes/Scalar.js';
|
|
|
4
4
|
|
|
5
5
|
const defaultTagPrefix = 'tag:yaml.org,2002:';
|
|
6
6
|
function findTagObject(value, tagName, tags) {
|
|
7
|
-
var _a;
|
|
8
7
|
if (tagName) {
|
|
9
8
|
const match = tags.filter(t => t.tag === tagName);
|
|
10
|
-
const tagObj =
|
|
9
|
+
const tagObj = match.find(t => !t.format) ?? match[0];
|
|
11
10
|
if (!tagObj)
|
|
12
11
|
throw new Error(`Tag ${tagName} not found`);
|
|
13
12
|
return tagObj;
|
|
14
13
|
}
|
|
15
|
-
return tags.find(t =>
|
|
14
|
+
return tags.find(t => t.identify?.(value) && !t.format);
|
|
16
15
|
}
|
|
17
16
|
function createNode(value, tagName, ctx) {
|
|
18
|
-
var _a, _b;
|
|
19
17
|
if (isDocument(value))
|
|
20
18
|
value = value.contents;
|
|
21
19
|
if (isNode(value))
|
|
22
20
|
return value;
|
|
23
21
|
if (isPair(value)) {
|
|
24
|
-
const map =
|
|
22
|
+
const map = ctx.schema[MAP].createNode?.(ctx.schema, null, ctx);
|
|
25
23
|
map.items.push(value);
|
|
26
24
|
return map;
|
|
27
25
|
}
|
|
@@ -49,7 +47,7 @@ function createNode(value, tagName, ctx) {
|
|
|
49
47
|
sourceObjects.set(value, ref);
|
|
50
48
|
}
|
|
51
49
|
}
|
|
52
|
-
if (tagName
|
|
50
|
+
if (tagName?.startsWith('!!'))
|
|
53
51
|
tagName = defaultTagPrefix + tagName.slice(2);
|
|
54
52
|
let tagObj = findTagObject(value, tagName, schema.tags);
|
|
55
53
|
if (!tagObj) {
|
|
@@ -74,7 +72,7 @@ function createNode(value, tagName, ctx) {
|
|
|
74
72
|
onTagObj(tagObj);
|
|
75
73
|
delete ctx.onTagObj;
|
|
76
74
|
}
|
|
77
|
-
const node =
|
|
75
|
+
const node = tagObj?.createNode
|
|
78
76
|
? tagObj.createNode(ctx.schema, value, ctx)
|
|
79
77
|
: new Scalar(value);
|
|
80
78
|
if (tagName)
|
|
@@ -24,7 +24,8 @@ function collectionFromPath(schema, path, value) {
|
|
|
24
24
|
sourceObjects: new Map()
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
|
-
//
|
|
27
|
+
// Type guard is intentionally a little wrong so as to be more useful,
|
|
28
|
+
// as it does not cover untypable empty non-string iterables (e.g. []).
|
|
28
29
|
const isEmptyPath = (path) => path == null ||
|
|
29
30
|
(typeof path === 'object' && !!path[Symbol.iterator]().next().done);
|
|
30
31
|
class Collection extends NodeBase {
|
|
@@ -23,11 +23,11 @@ class Pair {
|
|
|
23
23
|
return new Pair(key, value);
|
|
24
24
|
}
|
|
25
25
|
toJSON(_, ctx) {
|
|
26
|
-
const pair =
|
|
26
|
+
const pair = ctx?.mapAsMap ? new Map() : {};
|
|
27
27
|
return addPairToJSMap(ctx, pair, this);
|
|
28
28
|
}
|
|
29
29
|
toString(ctx, onComment, onChompKeep) {
|
|
30
|
-
return
|
|
30
|
+
return ctx?.doc
|
|
31
31
|
? stringifyPair(this, ctx, onComment, onChompKeep)
|
|
32
32
|
: JSON.stringify(this);
|
|
33
33
|
}
|
|
@@ -8,7 +8,7 @@ class Scalar extends NodeBase {
|
|
|
8
8
|
this.value = value;
|
|
9
9
|
}
|
|
10
10
|
toJSON(arg, ctx) {
|
|
11
|
-
return
|
|
11
|
+
return ctx?.keep ? this.value : toJS(this.value, arg, ctx);
|
|
12
12
|
}
|
|
13
13
|
toString() {
|
|
14
14
|
return String(this.value);
|
|
@@ -32,18 +32,17 @@ class YAMLMap extends Collection {
|
|
|
32
32
|
* collection will throw. Otherwise, overwrites the previous value.
|
|
33
33
|
*/
|
|
34
34
|
add(pair, overwrite) {
|
|
35
|
-
var _a;
|
|
36
35
|
let _pair;
|
|
37
36
|
if (isPair(pair))
|
|
38
37
|
_pair = pair;
|
|
39
38
|
else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
|
|
40
39
|
// In TypeScript, this never happens.
|
|
41
|
-
_pair = new Pair(pair, pair
|
|
40
|
+
_pair = new Pair(pair, pair?.value);
|
|
42
41
|
}
|
|
43
42
|
else
|
|
44
43
|
_pair = new Pair(pair.key, pair.value);
|
|
45
44
|
const prev = findPair(this.items, _pair.key);
|
|
46
|
-
const sortEntries =
|
|
45
|
+
const sortEntries = this.schema?.sortMapEntries;
|
|
47
46
|
if (prev) {
|
|
48
47
|
if (!overwrite)
|
|
49
48
|
throw new Error(`Key ${_pair.key} already set`);
|
|
@@ -73,8 +72,8 @@ class YAMLMap extends Collection {
|
|
|
73
72
|
}
|
|
74
73
|
get(key, keepScalar) {
|
|
75
74
|
const it = findPair(this.items, key);
|
|
76
|
-
const node = it
|
|
77
|
-
return !keepScalar && isScalar(node) ? node.value : node;
|
|
75
|
+
const node = it?.value;
|
|
76
|
+
return (!keepScalar && isScalar(node) ? node.value : node) ?? undefined;
|
|
78
77
|
}
|
|
79
78
|
has(key) {
|
|
80
79
|
return !!findPair(this.items, key);
|
|
@@ -88,8 +87,8 @@ class YAMLMap extends Collection {
|
|
|
88
87
|
* @returns Instance of Type, Map, or Object
|
|
89
88
|
*/
|
|
90
89
|
toJSON(_, ctx, Type) {
|
|
91
|
-
const map = Type ? new Type() :
|
|
92
|
-
if (ctx
|
|
90
|
+
const map = Type ? new Type() : ctx?.mapAsMap ? new Map() : {};
|
|
91
|
+
if (ctx?.onCreate)
|
|
93
92
|
ctx.onCreate(map);
|
|
94
93
|
for (const item of this.items)
|
|
95
94
|
addPairToJSMap(ctx, map, item);
|
|
@@ -30,14 +30,6 @@ class YAMLSeq extends Collection {
|
|
|
30
30
|
const del = this.items.splice(idx, 1);
|
|
31
31
|
return del.length > 0;
|
|
32
32
|
}
|
|
33
|
-
/**
|
|
34
|
-
* Returns item at `key`, or `undefined` if not found. By default unwraps
|
|
35
|
-
* scalar values from their surrounding node; to disable set `keepScalar` to
|
|
36
|
-
* `true` (collections are always returned intact).
|
|
37
|
-
*
|
|
38
|
-
* `key` must contain a representation of an integer for this to succeed.
|
|
39
|
-
* It may be wrapped in a `Scalar`.
|
|
40
|
-
*/
|
|
41
33
|
get(key, keepScalar) {
|
|
42
34
|
const idx = asItemIndex(key);
|
|
43
35
|
if (typeof idx !== 'number')
|
|
@@ -74,7 +66,7 @@ class YAMLSeq extends Collection {
|
|
|
74
66
|
}
|
|
75
67
|
toJSON(_, ctx) {
|
|
76
68
|
const seq = [];
|
|
77
|
-
if (ctx
|
|
69
|
+
if (ctx?.onCreate)
|
|
78
70
|
ctx.onCreate(seq);
|
|
79
71
|
let i = 0;
|
|
80
72
|
for (const item of this.items)
|
|
@@ -6,7 +6,7 @@ import { toJS } from './toJS.js';
|
|
|
6
6
|
|
|
7
7
|
const MERGE_KEY = '<<';
|
|
8
8
|
function addPairToJSMap(ctx, map, { key, value }) {
|
|
9
|
-
if (
|
|
9
|
+
if (ctx?.doc.schema.merge && isMergeKey(key)) {
|
|
10
10
|
value = isAlias(value) ? value.resolve(ctx.doc) : value;
|
|
11
11
|
if (isSeq(value))
|
|
12
12
|
for (const it of value.items)
|
|
@@ -29,7 +29,7 @@ function toJS(value, arg, ctx) {
|
|
|
29
29
|
ctx.onCreate(res);
|
|
30
30
|
return res;
|
|
31
31
|
}
|
|
32
|
-
if (typeof value === 'bigint' && !
|
|
32
|
+
if (typeof value === 'bigint' && !ctx?.keep)
|
|
33
33
|
return Number(value);
|
|
34
34
|
return value;
|
|
35
35
|
}
|
|
@@ -38,7 +38,6 @@ function resolveAsScalar(token, strict = true, onError) {
|
|
|
38
38
|
* @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
|
|
39
39
|
*/
|
|
40
40
|
function createScalarToken(value, context) {
|
|
41
|
-
var _a;
|
|
42
41
|
const { implicitKey = false, indent, inFlow = false, offset = -1, type = 'PLAIN' } = context;
|
|
43
42
|
const source = stringifyString({ type, value }, {
|
|
44
43
|
implicitKey,
|
|
@@ -46,7 +45,7 @@ function createScalarToken(value, context) {
|
|
|
46
45
|
inFlow,
|
|
47
46
|
options: { blockQuote: true, lineWidth: -1 }
|
|
48
47
|
});
|
|
49
|
-
const end =
|
|
48
|
+
const end = context.end ?? [
|
|
50
49
|
{ type: 'newline', offset: -1, indent, source: '\n' }
|
|
51
50
|
];
|
|
52
51
|
switch (source[0]) {
|
|
@@ -47,7 +47,7 @@ visit.REMOVE = REMOVE;
|
|
|
47
47
|
visit.itemAtPath = (cst, path) => {
|
|
48
48
|
let item = cst;
|
|
49
49
|
for (const [field, index] of path) {
|
|
50
|
-
const tok = item
|
|
50
|
+
const tok = item?.[field];
|
|
51
51
|
if (tok && 'items' in tok) {
|
|
52
52
|
item = tok.items[index];
|
|
53
53
|
}
|
|
@@ -64,7 +64,7 @@ visit.itemAtPath = (cst, path) => {
|
|
|
64
64
|
visit.parentCollection = (cst, path) => {
|
|
65
65
|
const parent = visit.itemAtPath(cst, path.slice(0, -1));
|
|
66
66
|
const field = path[path.length - 1][0];
|
|
67
|
-
const coll = parent
|
|
67
|
+
const coll = parent?.[field];
|
|
68
68
|
if (coll && 'items' in coll)
|
|
69
69
|
return coll;
|
|
70
70
|
throw new Error('Parent collection not found');
|
|
@@ -148,13 +148,12 @@ class Lexer {
|
|
|
148
148
|
* @returns A generator of lexical tokens
|
|
149
149
|
*/
|
|
150
150
|
*lex(source, incomplete = false) {
|
|
151
|
-
var _a;
|
|
152
151
|
if (source) {
|
|
153
152
|
this.buffer = this.buffer ? this.buffer + source : source;
|
|
154
153
|
this.lineEndPos = null;
|
|
155
154
|
}
|
|
156
155
|
this.atEnd = !incomplete;
|
|
157
|
-
let next =
|
|
156
|
+
let next = this.next ?? 'stream';
|
|
158
157
|
while (next && (incomplete || this.hasChars(1)))
|
|
159
158
|
next = yield* this.parseNext(next);
|
|
160
159
|
}
|
|
@@ -21,7 +21,7 @@ function findNonEmptyIndex(list) {
|
|
|
21
21
|
return -1;
|
|
22
22
|
}
|
|
23
23
|
function isFlowToken(token) {
|
|
24
|
-
switch (token
|
|
24
|
+
switch (token?.type) {
|
|
25
25
|
case 'alias':
|
|
26
26
|
case 'scalar':
|
|
27
27
|
case 'single-quoted-scalar':
|
|
@@ -33,13 +33,12 @@ function isFlowToken(token) {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
function getPrevProps(parent) {
|
|
36
|
-
var _a;
|
|
37
36
|
switch (parent.type) {
|
|
38
37
|
case 'document':
|
|
39
38
|
return parent.start;
|
|
40
39
|
case 'block-map': {
|
|
41
40
|
const it = parent.items[parent.items.length - 1];
|
|
42
|
-
return
|
|
41
|
+
return it.sep ?? it.start;
|
|
43
42
|
}
|
|
44
43
|
case 'block-seq':
|
|
45
44
|
return parent.items[parent.items.length - 1].start;
|
|
@@ -50,7 +49,6 @@ function getPrevProps(parent) {
|
|
|
50
49
|
}
|
|
51
50
|
/** Note: May modify input array */
|
|
52
51
|
function getFirstKeyStartProps(prev) {
|
|
53
|
-
var _a;
|
|
54
52
|
if (prev.length === 0)
|
|
55
53
|
return [];
|
|
56
54
|
let i = prev.length;
|
|
@@ -64,7 +62,7 @@ function getFirstKeyStartProps(prev) {
|
|
|
64
62
|
break loop;
|
|
65
63
|
}
|
|
66
64
|
}
|
|
67
|
-
while (
|
|
65
|
+
while (prev[++i]?.type === 'space') {
|
|
68
66
|
/* loop */
|
|
69
67
|
}
|
|
70
68
|
return prev.splice(i, prev.length);
|
|
@@ -266,7 +264,7 @@ class Parser {
|
|
|
266
264
|
return this.stack[this.stack.length - n];
|
|
267
265
|
}
|
|
268
266
|
*pop(error) {
|
|
269
|
-
const token = error
|
|
267
|
+
const token = error ?? this.stack.pop();
|
|
270
268
|
/* istanbul ignore if should not happen */
|
|
271
269
|
if (!token) {
|
|
272
270
|
const message = 'Tried to pop an empty stack';
|
|
@@ -471,7 +469,6 @@ class Parser {
|
|
|
471
469
|
}
|
|
472
470
|
}
|
|
473
471
|
*blockMap(map) {
|
|
474
|
-
var _a;
|
|
475
472
|
const it = map.items[map.items.length - 1];
|
|
476
473
|
// it.sep is true-ish if pair already has key or : separator
|
|
477
474
|
switch (this.type) {
|
|
@@ -480,8 +477,8 @@ class Parser {
|
|
|
480
477
|
if (it.value) {
|
|
481
478
|
const end = 'end' in it.value ? it.value.end : undefined;
|
|
482
479
|
const last = Array.isArray(end) ? end[end.length - 1] : undefined;
|
|
483
|
-
if (
|
|
484
|
-
end
|
|
480
|
+
if (last?.type === 'comment')
|
|
481
|
+
end?.push(this.sourceToken);
|
|
485
482
|
else
|
|
486
483
|
map.items.push({ start: [this.sourceToken] });
|
|
487
484
|
}
|
|
@@ -503,7 +500,7 @@ class Parser {
|
|
|
503
500
|
else {
|
|
504
501
|
if (this.atIndentedComment(it.start, map.indent)) {
|
|
505
502
|
const prev = map.items[map.items.length - 2];
|
|
506
|
-
const end =
|
|
503
|
+
const end = prev?.value?.end;
|
|
507
504
|
if (Array.isArray(end)) {
|
|
508
505
|
Array.prototype.push.apply(end, it.start);
|
|
509
506
|
end.push(this.sourceToken);
|
|
@@ -680,15 +677,14 @@ class Parser {
|
|
|
680
677
|
yield* this.step();
|
|
681
678
|
}
|
|
682
679
|
*blockSequence(seq) {
|
|
683
|
-
var _a;
|
|
684
680
|
const it = seq.items[seq.items.length - 1];
|
|
685
681
|
switch (this.type) {
|
|
686
682
|
case 'newline':
|
|
687
683
|
if (it.value) {
|
|
688
684
|
const end = 'end' in it.value ? it.value.end : undefined;
|
|
689
685
|
const last = Array.isArray(end) ? end[end.length - 1] : undefined;
|
|
690
|
-
if (
|
|
691
|
-
end
|
|
686
|
+
if (last?.type === 'comment')
|
|
687
|
+
end?.push(this.sourceToken);
|
|
692
688
|
else
|
|
693
689
|
seq.items.push({ start: [this.sourceToken] });
|
|
694
690
|
}
|
|
@@ -702,7 +698,7 @@ class Parser {
|
|
|
702
698
|
else {
|
|
703
699
|
if (this.atIndentedComment(it.start, seq.indent)) {
|
|
704
700
|
const prev = seq.items[seq.items.length - 2];
|
|
705
|
-
const end =
|
|
701
|
+
const end = prev?.value?.end;
|
|
706
702
|
if (Array.isArray(end)) {
|
|
707
703
|
Array.prototype.push.apply(end, it.start);
|
|
708
704
|
end.push(this.sourceToken);
|
|
@@ -21,7 +21,7 @@ function parseOptions(options) {
|
|
|
21
21
|
*/
|
|
22
22
|
function parseAllDocuments(source, options = {}) {
|
|
23
23
|
const { lineCounter, prettyErrors } = parseOptions(options);
|
|
24
|
-
const parser = new Parser(lineCounter
|
|
24
|
+
const parser = new Parser(lineCounter?.addNewLine);
|
|
25
25
|
const composer = new Composer(options);
|
|
26
26
|
const docs = Array.from(composer.compose(parser.parse(source)));
|
|
27
27
|
if (prettyErrors && lineCounter)
|
|
@@ -36,7 +36,7 @@ function parseAllDocuments(source, options = {}) {
|
|
|
36
36
|
/** Parse an input string into a single YAML.Document */
|
|
37
37
|
function parseDocument(source, options = {}) {
|
|
38
38
|
const { lineCounter, prettyErrors } = parseOptions(options);
|
|
39
|
-
const parser = new Parser(lineCounter
|
|
39
|
+
const parser = new Parser(lineCounter?.addNewLine);
|
|
40
40
|
const composer = new Composer(options);
|
|
41
41
|
// `doc` is always set by compose.end(true) at the very latest
|
|
42
42
|
let doc = null;
|
|
@@ -75,7 +75,6 @@ function parse(src, reviver, options) {
|
|
|
75
75
|
return doc.toJS(Object.assign({ reviver: _reviver }, options));
|
|
76
76
|
}
|
|
77
77
|
function stringify(value, replacer, options) {
|
|
78
|
-
var _a;
|
|
79
78
|
let _replacer = null;
|
|
80
79
|
if (typeof replacer === 'function' || Array.isArray(replacer)) {
|
|
81
80
|
_replacer = replacer;
|
|
@@ -90,7 +89,7 @@ function stringify(value, replacer, options) {
|
|
|
90
89
|
options = indent < 1 ? undefined : indent > 8 ? { indent: 8 } : { indent };
|
|
91
90
|
}
|
|
92
91
|
if (value === undefined) {
|
|
93
|
-
const { keepUndefined } =
|
|
92
|
+
const { keepUndefined } = options ?? replacer ?? {};
|
|
94
93
|
if (!keepUndefined)
|
|
95
94
|
return undefined;
|
|
96
95
|
}
|