yaml 2.5.1 → 2.6.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 +17 -3
- package/browser/dist/compose/compose-doc.js +1 -0
- package/browser/dist/compose/compose-node.js +10 -0
- package/browser/dist/compose/compose-scalar.js +14 -8
- package/browser/dist/compose/resolve-block-map.js +2 -0
- package/browser/dist/compose/resolve-block-seq.js +2 -0
- package/browser/dist/compose/resolve-flow-collection.js +4 -0
- package/browser/dist/compose/util-map-includes.js +1 -5
- package/browser/dist/doc/Document.js +3 -2
- package/browser/dist/nodes/addPairToJSMap.js +7 -49
- package/browser/dist/public-api.js +3 -0
- package/browser/dist/schema/Schema.js +1 -2
- package/browser/dist/schema/json/schema.js +1 -1
- package/browser/dist/schema/tags.js +26 -13
- package/browser/dist/schema/yaml-1.1/merge.js +64 -0
- package/browser/dist/schema/yaml-1.1/schema.js +2 -0
- package/browser/dist/schema/yaml-1.1/timestamp.js +1 -1
- package/browser/dist/stringify/stringify.js +6 -1
- package/browser/dist/stringify/stringifyString.js +20 -11
- package/dist/compose/compose-doc.js +1 -0
- package/dist/compose/compose-node.d.ts +1 -0
- package/dist/compose/compose-node.js +10 -0
- package/dist/compose/compose-scalar.js +13 -7
- package/dist/compose/resolve-block-map.js +2 -0
- package/dist/compose/resolve-block-seq.js +2 -0
- package/dist/compose/resolve-flow-collection.js +4 -0
- package/dist/compose/util-map-includes.js +1 -5
- package/dist/doc/Document.js +3 -2
- package/dist/errors.d.ts +1 -1
- package/dist/nodes/Node.d.ts +7 -1
- package/dist/nodes/addPairToJSMap.js +6 -48
- package/dist/options.d.ts +6 -0
- package/dist/parse/lexer.d.ts +1 -1
- package/dist/parse/parser.d.ts +3 -3
- package/dist/public-api.js +3 -0
- package/dist/schema/Schema.d.ts +0 -1
- package/dist/schema/Schema.js +1 -2
- package/dist/schema/json/schema.js +1 -1
- package/dist/schema/tags.d.ts +9 -1
- package/dist/schema/tags.js +26 -13
- package/dist/schema/types.d.ts +6 -4
- package/dist/schema/yaml-1.1/merge.d.ts +9 -0
- package/dist/schema/yaml-1.1/merge.js +68 -0
- package/dist/schema/yaml-1.1/schema.js +2 -0
- package/dist/schema/yaml-1.1/timestamp.js +1 -1
- package/dist/stringify/stringify.js +6 -1
- package/dist/stringify/stringifyString.js +20 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,9 +30,23 @@ npm install yaml
|
|
|
30
30
|
|
|
31
31
|
The development and maintenance of this library is [sponsored](https://github.com/sponsors/eemeli) by:
|
|
32
32
|
|
|
33
|
-
<
|
|
34
|
-
<
|
|
35
|
-
|
|
33
|
+
<p align="center" width="100%">
|
|
34
|
+
<a href="https://www.scipress.io/"
|
|
35
|
+
><img
|
|
36
|
+
width="150"
|
|
37
|
+
align="top"
|
|
38
|
+
src="https://eemeli.org/yaml/images/scipress.svg"
|
|
39
|
+
alt="Scipress"
|
|
40
|
+
/></a>
|
|
41
|
+
|
|
42
|
+
<a href="https://manifest.build/"
|
|
43
|
+
><img
|
|
44
|
+
width="150"
|
|
45
|
+
align="top"
|
|
46
|
+
src="https://eemeli.org/yaml/images/manifest.svg"
|
|
47
|
+
alt="Manifest"
|
|
48
|
+
/></a>
|
|
49
|
+
</p>
|
|
36
50
|
|
|
37
51
|
## API Overview
|
|
38
52
|
|
|
@@ -7,6 +7,7 @@ function composeDoc(options, directives, { offset, start, value, end }, onError)
|
|
|
7
7
|
const opts = Object.assign({ _directives: directives }, options);
|
|
8
8
|
const doc = new Document(undefined, opts);
|
|
9
9
|
const ctx = {
|
|
10
|
+
atKey: false,
|
|
10
11
|
atRoot: true,
|
|
11
12
|
directives: doc.directives,
|
|
12
13
|
options: doc.options,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Alias } from '../nodes/Alias.js';
|
|
2
|
+
import { isScalar } from '../nodes/identity.js';
|
|
2
3
|
import { composeCollection } from './compose-collection.js';
|
|
3
4
|
import { composeScalar } from './compose-scalar.js';
|
|
4
5
|
import { resolveEnd } from './resolve-end.js';
|
|
@@ -6,6 +7,7 @@ import { emptyScalarPosition } from './util-empty-scalar-position.js';
|
|
|
6
7
|
|
|
7
8
|
const CN = { composeNode, composeEmptyNode };
|
|
8
9
|
function composeNode(ctx, token, props, onError) {
|
|
10
|
+
const atKey = ctx.atKey;
|
|
9
11
|
const { spaceBefore, comment, anchor, tag } = props;
|
|
10
12
|
let node;
|
|
11
13
|
let isSrcToken = true;
|
|
@@ -41,6 +43,14 @@ function composeNode(ctx, token, props, onError) {
|
|
|
41
43
|
}
|
|
42
44
|
if (anchor && node.anchor === '')
|
|
43
45
|
onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
|
|
46
|
+
if (atKey &&
|
|
47
|
+
ctx.options.stringKeys &&
|
|
48
|
+
(!isScalar(node) ||
|
|
49
|
+
typeof node.value !== 'string' ||
|
|
50
|
+
(node.tag && node.tag !== 'tag:yaml.org,2002:str'))) {
|
|
51
|
+
const msg = 'With stringKeys, all keys must be strings';
|
|
52
|
+
onError(tag ?? token, 'NON_STRING_KEY', msg);
|
|
53
|
+
}
|
|
44
54
|
if (spaceBefore)
|
|
45
55
|
node.spaceBefore = true;
|
|
46
56
|
if (comment) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isScalar, SCALAR } from '../nodes/identity.js';
|
|
2
2
|
import { Scalar } from '../nodes/Scalar.js';
|
|
3
3
|
import { resolveBlockScalar } from './resolve-block-scalar.js';
|
|
4
4
|
import { resolveFlowScalar } from './resolve-flow-scalar.js';
|
|
@@ -10,11 +10,16 @@ function composeScalar(ctx, token, tagToken, onError) {
|
|
|
10
10
|
const tagName = tagToken
|
|
11
11
|
? ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg))
|
|
12
12
|
: null;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
let tag;
|
|
14
|
+
if (ctx.options.stringKeys && ctx.atKey) {
|
|
15
|
+
tag = ctx.schema[SCALAR];
|
|
16
|
+
}
|
|
17
|
+
else if (tagName)
|
|
18
|
+
tag = findScalarTagByName(ctx.schema, value, tagName, tagToken, onError);
|
|
19
|
+
else if (token.type === 'scalar')
|
|
20
|
+
tag = findScalarTagByTest(ctx, value, token, onError);
|
|
21
|
+
else
|
|
22
|
+
tag = ctx.schema[SCALAR];
|
|
18
23
|
let scalar;
|
|
19
24
|
try {
|
|
20
25
|
const res = tag.resolve(value, msg => onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
|
|
@@ -62,8 +67,9 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
|
|
|
62
67
|
onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
|
|
63
68
|
return schema[SCALAR];
|
|
64
69
|
}
|
|
65
|
-
function findScalarTagByTest({ directives, schema }, value, token, onError) {
|
|
66
|
-
const tag = schema.tags.find(tag => tag.default && tag.
|
|
70
|
+
function findScalarTagByTest({ atKey, directives, schema }, value, token, onError) {
|
|
71
|
+
const tag = schema.tags.find(tag => (tag.default === true || (atKey && tag.default === 'key')) &&
|
|
72
|
+
tag.test?.test(value)) || schema[SCALAR];
|
|
67
73
|
if (schema.compat) {
|
|
68
74
|
const compat = schema.compat.find(tag => tag.default && tag.test?.test(value)) ??
|
|
69
75
|
schema[SCALAR];
|
|
@@ -50,12 +50,14 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, ta
|
|
|
50
50
|
onError(offset, 'BAD_INDENT', startColMsg);
|
|
51
51
|
}
|
|
52
52
|
// key value
|
|
53
|
+
ctx.atKey = true;
|
|
53
54
|
const keyStart = keyProps.end;
|
|
54
55
|
const keyNode = key
|
|
55
56
|
? composeNode(ctx, key, keyProps, onError)
|
|
56
57
|
: composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
|
|
57
58
|
if (ctx.schema.compat)
|
|
58
59
|
flowIndentCheck(bm.indent, key, onError);
|
|
60
|
+
ctx.atKey = false;
|
|
59
61
|
if (mapIncludes(ctx, map.items, keyNode))
|
|
60
62
|
onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
|
|
61
63
|
// value properties
|
|
@@ -7,6 +7,8 @@ function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError, ta
|
|
|
7
7
|
const seq = new NodeClass(ctx.schema);
|
|
8
8
|
if (ctx.atRoot)
|
|
9
9
|
ctx.atRoot = false;
|
|
10
|
+
if (ctx.atKey)
|
|
11
|
+
ctx.atKey = false;
|
|
10
12
|
let offset = bs.offset;
|
|
11
13
|
let commentEnd = null;
|
|
12
14
|
for (const { start, value } of bs.items) {
|
|
@@ -18,6 +18,8 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
|
|
|
18
18
|
const atRoot = ctx.atRoot;
|
|
19
19
|
if (atRoot)
|
|
20
20
|
ctx.atRoot = false;
|
|
21
|
+
if (ctx.atKey)
|
|
22
|
+
ctx.atKey = false;
|
|
21
23
|
let offset = fc.offset + fc.start.source.length;
|
|
22
24
|
for (let i = 0; i < fc.items.length; ++i) {
|
|
23
25
|
const collItem = fc.items[i];
|
|
@@ -97,12 +99,14 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
|
|
|
97
99
|
else {
|
|
98
100
|
// item is a key+value pair
|
|
99
101
|
// key value
|
|
102
|
+
ctx.atKey = true;
|
|
100
103
|
const keyStart = props.end;
|
|
101
104
|
const keyNode = key
|
|
102
105
|
? composeNode(ctx, key, props, onError)
|
|
103
106
|
: composeEmptyNode(ctx, keyStart, start, null, props, onError);
|
|
104
107
|
if (isBlock(key))
|
|
105
108
|
onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
|
|
109
|
+
ctx.atKey = false;
|
|
106
110
|
// value properties
|
|
107
111
|
const valueProps = resolveProps(sep ?? [], {
|
|
108
112
|
flow: fcName,
|
|
@@ -6,11 +6,7 @@ function mapIncludes(ctx, items, search) {
|
|
|
6
6
|
return false;
|
|
7
7
|
const isEqual = typeof uniqueKeys === 'function'
|
|
8
8
|
? uniqueKeys
|
|
9
|
-
: (a, b) => a === b ||
|
|
10
|
-
(isScalar(a) &&
|
|
11
|
-
isScalar(b) &&
|
|
12
|
-
a.value === b.value &&
|
|
13
|
-
!(a.value === '<<' && ctx.schema.merge));
|
|
9
|
+
: (a, b) => a === b || (isScalar(a) && isScalar(b) && a.value === b.value);
|
|
14
10
|
return items.some(pair => isEqual(pair.key, search));
|
|
15
11
|
}
|
|
16
12
|
|
|
@@ -35,6 +35,7 @@ class Document {
|
|
|
35
35
|
logLevel: 'warn',
|
|
36
36
|
prettyErrors: true,
|
|
37
37
|
strict: true,
|
|
38
|
+
stringKeys: false,
|
|
38
39
|
uniqueKeys: true,
|
|
39
40
|
version: '1.2'
|
|
40
41
|
}, options);
|
|
@@ -258,7 +259,7 @@ class Document {
|
|
|
258
259
|
this.directives.yaml.version = '1.1';
|
|
259
260
|
else
|
|
260
261
|
this.directives = new Directives({ version: '1.1' });
|
|
261
|
-
opt = {
|
|
262
|
+
opt = { resolveKnownTags: false, schema: 'yaml-1.1' };
|
|
262
263
|
break;
|
|
263
264
|
case '1.2':
|
|
264
265
|
case 'next':
|
|
@@ -266,7 +267,7 @@ class Document {
|
|
|
266
267
|
this.directives.yaml.version = version;
|
|
267
268
|
else
|
|
268
269
|
this.directives = new Directives({ version });
|
|
269
|
-
opt = {
|
|
270
|
+
opt = { resolveKnownTags: true, schema: 'core' };
|
|
270
271
|
break;
|
|
271
272
|
case null:
|
|
272
273
|
if (this.directives)
|
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
import { warn } from '../log.js';
|
|
2
|
+
import { isMergeKey, addMergeToJSMap } from '../schema/yaml-1.1/merge.js';
|
|
2
3
|
import { createStringifyContext } from '../stringify/stringify.js';
|
|
3
|
-
import {
|
|
4
|
-
import { Scalar } from './Scalar.js';
|
|
4
|
+
import { isNode } from './identity.js';
|
|
5
5
|
import { toJS } from './toJS.js';
|
|
6
6
|
|
|
7
|
-
const MERGE_KEY = '<<';
|
|
8
7
|
function addPairToJSMap(ctx, map, { key, value }) {
|
|
9
|
-
if (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
else if (Array.isArray(value))
|
|
15
|
-
for (const it of value)
|
|
16
|
-
mergeToJSMap(ctx, map, it);
|
|
17
|
-
else
|
|
18
|
-
mergeToJSMap(ctx, map, value);
|
|
19
|
-
}
|
|
8
|
+
if (isNode(key) && key.addToJSMap)
|
|
9
|
+
key.addToJSMap(ctx, map, value);
|
|
10
|
+
// TODO: Should drop this special case for bare << handling
|
|
11
|
+
else if (isMergeKey(ctx, key))
|
|
12
|
+
addMergeToJSMap(ctx, map, value);
|
|
20
13
|
else {
|
|
21
14
|
const jsKey = toJS(key, '', ctx);
|
|
22
15
|
if (map instanceof Map) {
|
|
@@ -41,41 +34,6 @@ function addPairToJSMap(ctx, map, { key, value }) {
|
|
|
41
34
|
}
|
|
42
35
|
return map;
|
|
43
36
|
}
|
|
44
|
-
const isMergeKey = (key) => key === MERGE_KEY ||
|
|
45
|
-
(isScalar(key) &&
|
|
46
|
-
key.value === MERGE_KEY &&
|
|
47
|
-
(!key.type || key.type === Scalar.PLAIN));
|
|
48
|
-
// If the value associated with a merge key is a single mapping node, each of
|
|
49
|
-
// its key/value pairs is inserted into the current mapping, unless the key
|
|
50
|
-
// already exists in it. If the value associated with the merge key is a
|
|
51
|
-
// sequence, then this sequence is expected to contain mapping nodes and each
|
|
52
|
-
// of these nodes is merged in turn according to its order in the sequence.
|
|
53
|
-
// Keys in mapping nodes earlier in the sequence override keys specified in
|
|
54
|
-
// later mapping nodes. -- http://yaml.org/type/merge.html
|
|
55
|
-
function mergeToJSMap(ctx, map, value) {
|
|
56
|
-
const source = ctx && isAlias(value) ? value.resolve(ctx.doc) : value;
|
|
57
|
-
if (!isMap(source))
|
|
58
|
-
throw new Error('Merge sources must be maps or map aliases');
|
|
59
|
-
const srcMap = source.toJSON(null, ctx, Map);
|
|
60
|
-
for (const [key, value] of srcMap) {
|
|
61
|
-
if (map instanceof Map) {
|
|
62
|
-
if (!map.has(key))
|
|
63
|
-
map.set(key, value);
|
|
64
|
-
}
|
|
65
|
-
else if (map instanceof Set) {
|
|
66
|
-
map.add(key);
|
|
67
|
-
}
|
|
68
|
-
else if (!Object.prototype.hasOwnProperty.call(map, key)) {
|
|
69
|
-
Object.defineProperty(map, key, {
|
|
70
|
-
value,
|
|
71
|
-
writable: true,
|
|
72
|
-
enumerable: true,
|
|
73
|
-
configurable: true
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return map;
|
|
78
|
-
}
|
|
79
37
|
function stringifyKey(key, jsKey, ctx) {
|
|
80
38
|
if (jsKey === null)
|
|
81
39
|
return '';
|
|
@@ -2,6 +2,7 @@ import { Composer } from './compose/composer.js';
|
|
|
2
2
|
import { Document } from './doc/Document.js';
|
|
3
3
|
import { prettifyError, YAMLParseError } from './errors.js';
|
|
4
4
|
import { warn } from './log.js';
|
|
5
|
+
import { isDocument } from './nodes/identity.js';
|
|
5
6
|
import { LineCounter } from './parse/line-counter.js';
|
|
6
7
|
import { Parser } from './parse/parser.js';
|
|
7
8
|
|
|
@@ -93,6 +94,8 @@ function stringify(value, replacer, options) {
|
|
|
93
94
|
if (!keepUndefined)
|
|
94
95
|
return undefined;
|
|
95
96
|
}
|
|
97
|
+
if (isDocument(value) && !_replacer)
|
|
98
|
+
return value.toString(options);
|
|
96
99
|
return new Document(value, _replacer, options).toString(options);
|
|
97
100
|
}
|
|
98
101
|
|
|
@@ -12,10 +12,9 @@ class Schema {
|
|
|
12
12
|
: compat
|
|
13
13
|
? getTags(null, compat)
|
|
14
14
|
: null;
|
|
15
|
-
this.merge = !!merge;
|
|
16
15
|
this.name = (typeof schema === 'string' && schema) || 'core';
|
|
17
16
|
this.knownTags = resolveKnownTags ? coreKnownTags : {};
|
|
18
|
-
this.tags = getTags(customTags, this.name);
|
|
17
|
+
this.tags = getTags(customTags, this.name, merge);
|
|
19
18
|
this.toStringOptions = toStringDefaults ?? null;
|
|
20
19
|
Object.defineProperty(this, MAP, { value: map });
|
|
21
20
|
Object.defineProperty(this, SCALAR, { value: string });
|
|
@@ -8,6 +8,7 @@ import { int, intHex, intOct } from './core/int.js';
|
|
|
8
8
|
import { schema } from './core/schema.js';
|
|
9
9
|
import { schema as schema$1 } from './json/schema.js';
|
|
10
10
|
import { binary } from './yaml-1.1/binary.js';
|
|
11
|
+
import { merge } from './yaml-1.1/merge.js';
|
|
11
12
|
import { omap } from './yaml-1.1/omap.js';
|
|
12
13
|
import { pairs } from './yaml-1.1/pairs.js';
|
|
13
14
|
import { schema as schema$2 } from './yaml-1.1/schema.js';
|
|
@@ -33,6 +34,7 @@ const tagsByName = {
|
|
|
33
34
|
intOct,
|
|
34
35
|
intTime,
|
|
35
36
|
map,
|
|
37
|
+
merge,
|
|
36
38
|
null: nullTag,
|
|
37
39
|
omap,
|
|
38
40
|
pairs,
|
|
@@ -42,13 +44,20 @@ const tagsByName = {
|
|
|
42
44
|
};
|
|
43
45
|
const coreKnownTags = {
|
|
44
46
|
'tag:yaml.org,2002:binary': binary,
|
|
47
|
+
'tag:yaml.org,2002:merge': merge,
|
|
45
48
|
'tag:yaml.org,2002:omap': omap,
|
|
46
49
|
'tag:yaml.org,2002:pairs': pairs,
|
|
47
50
|
'tag:yaml.org,2002:set': set,
|
|
48
51
|
'tag:yaml.org,2002:timestamp': timestamp
|
|
49
52
|
};
|
|
50
|
-
function getTags(customTags, schemaName) {
|
|
51
|
-
|
|
53
|
+
function getTags(customTags, schemaName, addMergeTag) {
|
|
54
|
+
const schemaTags = schemas.get(schemaName);
|
|
55
|
+
if (schemaTags && !customTags) {
|
|
56
|
+
return addMergeTag && !schemaTags.includes(merge)
|
|
57
|
+
? schemaTags.concat(merge)
|
|
58
|
+
: schemaTags.slice();
|
|
59
|
+
}
|
|
60
|
+
let tags = schemaTags;
|
|
52
61
|
if (!tags) {
|
|
53
62
|
if (Array.isArray(customTags))
|
|
54
63
|
tags = [];
|
|
@@ -67,17 +76,21 @@ function getTags(customTags, schemaName) {
|
|
|
67
76
|
else if (typeof customTags === 'function') {
|
|
68
77
|
tags = customTags(tags.slice());
|
|
69
78
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const tagObj = tagsByName[tag];
|
|
74
|
-
if (tagObj)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
if (addMergeTag)
|
|
80
|
+
tags = tags.concat(merge);
|
|
81
|
+
return tags.reduce((tags, tag) => {
|
|
82
|
+
const tagObj = typeof tag === 'string' ? tagsByName[tag] : tag;
|
|
83
|
+
if (!tagObj) {
|
|
84
|
+
const tagName = JSON.stringify(tag);
|
|
85
|
+
const keys = Object.keys(tagsByName)
|
|
86
|
+
.map(key => JSON.stringify(key))
|
|
87
|
+
.join(', ');
|
|
88
|
+
throw new Error(`Unknown custom tag ${tagName}; use one of ${keys}`);
|
|
89
|
+
}
|
|
90
|
+
if (!tags.includes(tagObj))
|
|
91
|
+
tags.push(tagObj);
|
|
92
|
+
return tags;
|
|
93
|
+
}, []);
|
|
81
94
|
}
|
|
82
95
|
|
|
83
96
|
export { coreKnownTags, getTags };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { isScalar, isAlias, isSeq, isMap } from '../../nodes/identity.js';
|
|
2
|
+
import { Scalar } from '../../nodes/Scalar.js';
|
|
3
|
+
|
|
4
|
+
// If the value associated with a merge key is a single mapping node, each of
|
|
5
|
+
// its key/value pairs is inserted into the current mapping, unless the key
|
|
6
|
+
// already exists in it. If the value associated with the merge key is a
|
|
7
|
+
// sequence, then this sequence is expected to contain mapping nodes and each
|
|
8
|
+
// of these nodes is merged in turn according to its order in the sequence.
|
|
9
|
+
// Keys in mapping nodes earlier in the sequence override keys specified in
|
|
10
|
+
// later mapping nodes. -- http://yaml.org/type/merge.html
|
|
11
|
+
const MERGE_KEY = '<<';
|
|
12
|
+
const merge = {
|
|
13
|
+
identify: value => value === MERGE_KEY ||
|
|
14
|
+
(typeof value === 'symbol' && value.description === MERGE_KEY),
|
|
15
|
+
default: 'key',
|
|
16
|
+
tag: 'tag:yaml.org,2002:merge',
|
|
17
|
+
test: /^<<$/,
|
|
18
|
+
resolve: () => Object.assign(new Scalar(Symbol(MERGE_KEY)), {
|
|
19
|
+
addToJSMap: addMergeToJSMap
|
|
20
|
+
}),
|
|
21
|
+
stringify: () => MERGE_KEY
|
|
22
|
+
};
|
|
23
|
+
const isMergeKey = (ctx, key) => (merge.identify(key) ||
|
|
24
|
+
(isScalar(key) &&
|
|
25
|
+
(!key.type || key.type === Scalar.PLAIN) &&
|
|
26
|
+
merge.identify(key.value))) &&
|
|
27
|
+
ctx?.doc.schema.tags.some(tag => tag.tag === merge.tag && tag.default);
|
|
28
|
+
function addMergeToJSMap(ctx, map, value) {
|
|
29
|
+
value = ctx && isAlias(value) ? value.resolve(ctx.doc) : value;
|
|
30
|
+
if (isSeq(value))
|
|
31
|
+
for (const it of value.items)
|
|
32
|
+
mergeValue(ctx, map, it);
|
|
33
|
+
else if (Array.isArray(value))
|
|
34
|
+
for (const it of value)
|
|
35
|
+
mergeValue(ctx, map, it);
|
|
36
|
+
else
|
|
37
|
+
mergeValue(ctx, map, value);
|
|
38
|
+
}
|
|
39
|
+
function mergeValue(ctx, map, value) {
|
|
40
|
+
const source = ctx && isAlias(value) ? value.resolve(ctx.doc) : value;
|
|
41
|
+
if (!isMap(source))
|
|
42
|
+
throw new Error('Merge sources must be maps or map aliases');
|
|
43
|
+
const srcMap = source.toJSON(null, ctx, Map);
|
|
44
|
+
for (const [key, value] of srcMap) {
|
|
45
|
+
if (map instanceof Map) {
|
|
46
|
+
if (!map.has(key))
|
|
47
|
+
map.set(key, value);
|
|
48
|
+
}
|
|
49
|
+
else if (map instanceof Set) {
|
|
50
|
+
map.add(key);
|
|
51
|
+
}
|
|
52
|
+
else if (!Object.prototype.hasOwnProperty.call(map, key)) {
|
|
53
|
+
Object.defineProperty(map, key, {
|
|
54
|
+
value,
|
|
55
|
+
writable: true,
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return map;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { addMergeToJSMap, isMergeKey, merge };
|
|
@@ -6,6 +6,7 @@ import { binary } from './binary.js';
|
|
|
6
6
|
import { trueTag, falseTag } from './bool.js';
|
|
7
7
|
import { floatNaN, floatExp, float } from './float.js';
|
|
8
8
|
import { intBin, intOct, int, intHex } from './int.js';
|
|
9
|
+
import { merge } from './merge.js';
|
|
9
10
|
import { omap } from './omap.js';
|
|
10
11
|
import { pairs } from './pairs.js';
|
|
11
12
|
import { set } from './set.js';
|
|
@@ -26,6 +27,7 @@ const schema = [
|
|
|
26
27
|
floatExp,
|
|
27
28
|
float,
|
|
28
29
|
binary,
|
|
30
|
+
merge,
|
|
29
31
|
omap,
|
|
30
32
|
pairs,
|
|
31
33
|
set,
|
|
@@ -95,7 +95,7 @@ const timestamp = {
|
|
|
95
95
|
}
|
|
96
96
|
return new Date(date);
|
|
97
97
|
},
|
|
98
|
-
stringify: ({ value }) => value.toISOString().replace(/(
|
|
98
|
+
stringify: ({ value }) => value.toISOString().replace(/(T00:00:00)?\.000Z$/, '')
|
|
99
99
|
};
|
|
100
100
|
|
|
101
101
|
export { floatTime, intTime, timestamp };
|
|
@@ -54,7 +54,12 @@ function getTagObject(tags, item) {
|
|
|
54
54
|
let obj;
|
|
55
55
|
if (isScalar(item)) {
|
|
56
56
|
obj = item.value;
|
|
57
|
-
|
|
57
|
+
let match = tags.filter(t => t.identify?.(obj));
|
|
58
|
+
if (match.length > 1) {
|
|
59
|
+
const testMatch = match.filter(t => t.test);
|
|
60
|
+
if (testMatch.length > 0)
|
|
61
|
+
match = testMatch;
|
|
62
|
+
}
|
|
58
63
|
tagObj =
|
|
59
64
|
match.find(t => t.format === item.format) ?? match.find(t => !t.format);
|
|
60
65
|
}
|
|
@@ -219,23 +219,32 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
|
|
|
219
219
|
start = start.replace(/\n+/g, `$&${indent}`);
|
|
220
220
|
}
|
|
221
221
|
const indentSize = indent ? '2' : '1'; // root is at -1
|
|
222
|
-
|
|
222
|
+
// Leading | or > is added later
|
|
223
|
+
let header = (startWithSpace ? indentSize : '') + chomp;
|
|
223
224
|
if (comment) {
|
|
224
225
|
header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
|
|
225
226
|
if (onComment)
|
|
226
227
|
onComment();
|
|
227
228
|
}
|
|
228
|
-
if (literal) {
|
|
229
|
-
|
|
230
|
-
|
|
229
|
+
if (!literal) {
|
|
230
|
+
const foldedValue = value
|
|
231
|
+
.replace(/\n+/g, '\n$&')
|
|
232
|
+
.replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
|
|
233
|
+
// ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
|
|
234
|
+
.replace(/\n+/g, `$&${indent}`);
|
|
235
|
+
let literalFallback = false;
|
|
236
|
+
const foldOptions = getFoldOptions(ctx, true);
|
|
237
|
+
if (blockQuote !== 'folded' && type !== Scalar.BLOCK_FOLDED) {
|
|
238
|
+
foldOptions.onOverflow = () => {
|
|
239
|
+
literalFallback = true;
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
const body = foldFlowLines(`${start}${foldedValue}${end}`, indent, FOLD_BLOCK, foldOptions);
|
|
243
|
+
if (!literalFallback)
|
|
244
|
+
return `>${header}\n${indent}${body}`;
|
|
231
245
|
}
|
|
232
|
-
value = value
|
|
233
|
-
|
|
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
|
-
const body = foldFlowLines(`${start}${value}${end}`, indent, FOLD_BLOCK, getFoldOptions(ctx, true));
|
|
238
|
-
return `${header}\n${indent}${body}`;
|
|
246
|
+
value = value.replace(/\n+/g, `$&${indent}`);
|
|
247
|
+
return `|${header}\n${indent}${start}${value}${end}`;
|
|
239
248
|
}
|
|
240
249
|
function plainString(item, ctx, onComment, onChompKeep) {
|
|
241
250
|
const { type, value } = item;
|
|
@@ -9,6 +9,7 @@ function composeDoc(options, directives, { offset, start, value, end }, onError)
|
|
|
9
9
|
const opts = Object.assign({ _directives: directives }, options);
|
|
10
10
|
const doc = new Document.Document(undefined, opts);
|
|
11
11
|
const ctx = {
|
|
12
|
+
atKey: false,
|
|
12
13
|
atRoot: true,
|
|
13
14
|
directives: doc.directives,
|
|
14
15
|
options: doc.options,
|
|
@@ -5,6 +5,7 @@ import type { SourceToken, Token } from '../parse/cst.js';
|
|
|
5
5
|
import type { Schema } from '../schema/Schema.js';
|
|
6
6
|
import type { ComposeErrorHandler } from './composer.js';
|
|
7
7
|
export interface ComposeContext {
|
|
8
|
+
atKey: boolean;
|
|
8
9
|
atRoot: boolean;
|
|
9
10
|
directives: Directives;
|
|
10
11
|
options: Readonly<Required<Omit<ParseOptions, 'lineCounter'>>>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var Alias = require('../nodes/Alias.js');
|
|
4
|
+
var identity = require('../nodes/identity.js');
|
|
4
5
|
var composeCollection = require('./compose-collection.js');
|
|
5
6
|
var composeScalar = require('./compose-scalar.js');
|
|
6
7
|
var resolveEnd = require('./resolve-end.js');
|
|
@@ -8,6 +9,7 @@ var utilEmptyScalarPosition = require('./util-empty-scalar-position.js');
|
|
|
8
9
|
|
|
9
10
|
const CN = { composeNode, composeEmptyNode };
|
|
10
11
|
function composeNode(ctx, token, props, onError) {
|
|
12
|
+
const atKey = ctx.atKey;
|
|
11
13
|
const { spaceBefore, comment, anchor, tag } = props;
|
|
12
14
|
let node;
|
|
13
15
|
let isSrcToken = true;
|
|
@@ -43,6 +45,14 @@ function composeNode(ctx, token, props, onError) {
|
|
|
43
45
|
}
|
|
44
46
|
if (anchor && node.anchor === '')
|
|
45
47
|
onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
|
|
48
|
+
if (atKey &&
|
|
49
|
+
ctx.options.stringKeys &&
|
|
50
|
+
(!identity.isScalar(node) ||
|
|
51
|
+
typeof node.value !== 'string' ||
|
|
52
|
+
(node.tag && node.tag !== 'tag:yaml.org,2002:str'))) {
|
|
53
|
+
const msg = 'With stringKeys, all keys must be strings';
|
|
54
|
+
onError(tag ?? token, 'NON_STRING_KEY', msg);
|
|
55
|
+
}
|
|
46
56
|
if (spaceBefore)
|
|
47
57
|
node.spaceBefore = true;
|
|
48
58
|
if (comment) {
|
|
@@ -12,11 +12,16 @@ function composeScalar(ctx, token, tagToken, onError) {
|
|
|
12
12
|
const tagName = tagToken
|
|
13
13
|
? ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg))
|
|
14
14
|
: null;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
let tag;
|
|
16
|
+
if (ctx.options.stringKeys && ctx.atKey) {
|
|
17
|
+
tag = ctx.schema[identity.SCALAR];
|
|
18
|
+
}
|
|
19
|
+
else if (tagName)
|
|
20
|
+
tag = findScalarTagByName(ctx.schema, value, tagName, tagToken, onError);
|
|
21
|
+
else if (token.type === 'scalar')
|
|
22
|
+
tag = findScalarTagByTest(ctx, value, token, onError);
|
|
23
|
+
else
|
|
24
|
+
tag = ctx.schema[identity.SCALAR];
|
|
20
25
|
let scalar;
|
|
21
26
|
try {
|
|
22
27
|
const res = tag.resolve(value, msg => onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
|
|
@@ -64,8 +69,9 @@ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
|
|
|
64
69
|
onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
|
|
65
70
|
return schema[identity.SCALAR];
|
|
66
71
|
}
|
|
67
|
-
function findScalarTagByTest({ directives, schema }, value, token, onError) {
|
|
68
|
-
const tag = schema.tags.find(tag => tag.default && tag.
|
|
72
|
+
function findScalarTagByTest({ atKey, directives, schema }, value, token, onError) {
|
|
73
|
+
const tag = schema.tags.find(tag => (tag.default === true || (atKey && tag.default === 'key')) &&
|
|
74
|
+
tag.test?.test(value)) || schema[identity.SCALAR];
|
|
69
75
|
if (schema.compat) {
|
|
70
76
|
const compat = schema.compat.find(tag => tag.default && tag.test?.test(value)) ??
|
|
71
77
|
schema[identity.SCALAR];
|
|
@@ -52,12 +52,14 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, ta
|
|
|
52
52
|
onError(offset, 'BAD_INDENT', startColMsg);
|
|
53
53
|
}
|
|
54
54
|
// key value
|
|
55
|
+
ctx.atKey = true;
|
|
55
56
|
const keyStart = keyProps.end;
|
|
56
57
|
const keyNode = key
|
|
57
58
|
? composeNode(ctx, key, keyProps, onError)
|
|
58
59
|
: composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
|
|
59
60
|
if (ctx.schema.compat)
|
|
60
61
|
utilFlowIndentCheck.flowIndentCheck(bm.indent, key, onError);
|
|
62
|
+
ctx.atKey = false;
|
|
61
63
|
if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
|
|
62
64
|
onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
|
|
63
65
|
// value properties
|
|
@@ -9,6 +9,8 @@ function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError, ta
|
|
|
9
9
|
const seq = new NodeClass(ctx.schema);
|
|
10
10
|
if (ctx.atRoot)
|
|
11
11
|
ctx.atRoot = false;
|
|
12
|
+
if (ctx.atKey)
|
|
13
|
+
ctx.atKey = false;
|
|
12
14
|
let offset = bs.offset;
|
|
13
15
|
let commentEnd = null;
|
|
14
16
|
for (const { start, value } of bs.items) {
|
|
@@ -20,6 +20,8 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
|
|
|
20
20
|
const atRoot = ctx.atRoot;
|
|
21
21
|
if (atRoot)
|
|
22
22
|
ctx.atRoot = false;
|
|
23
|
+
if (ctx.atKey)
|
|
24
|
+
ctx.atKey = false;
|
|
23
25
|
let offset = fc.offset + fc.start.source.length;
|
|
24
26
|
for (let i = 0; i < fc.items.length; ++i) {
|
|
25
27
|
const collItem = fc.items[i];
|
|
@@ -99,12 +101,14 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
|
|
|
99
101
|
else {
|
|
100
102
|
// item is a key+value pair
|
|
101
103
|
// key value
|
|
104
|
+
ctx.atKey = true;
|
|
102
105
|
const keyStart = props.end;
|
|
103
106
|
const keyNode = key
|
|
104
107
|
? composeNode(ctx, key, props, onError)
|
|
105
108
|
: composeEmptyNode(ctx, keyStart, start, null, props, onError);
|
|
106
109
|
if (isBlock(key))
|
|
107
110
|
onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
|
|
111
|
+
ctx.atKey = false;
|
|
108
112
|
// value properties
|
|
109
113
|
const valueProps = resolveProps.resolveProps(sep ?? [], {
|
|
110
114
|
flow: fcName,
|
|
@@ -8,11 +8,7 @@ function mapIncludes(ctx, items, search) {
|
|
|
8
8
|
return false;
|
|
9
9
|
const isEqual = typeof uniqueKeys === 'function'
|
|
10
10
|
? uniqueKeys
|
|
11
|
-
: (a, b) => a === b ||
|
|
12
|
-
(identity.isScalar(a) &&
|
|
13
|
-
identity.isScalar(b) &&
|
|
14
|
-
a.value === b.value &&
|
|
15
|
-
!(a.value === '<<' && ctx.schema.merge));
|
|
11
|
+
: (a, b) => a === b || (identity.isScalar(a) && identity.isScalar(b) && a.value === b.value);
|
|
16
12
|
return items.some(pair => isEqual(pair.key, search));
|
|
17
13
|
}
|
|
18
14
|
|
package/dist/doc/Document.js
CHANGED
|
@@ -37,6 +37,7 @@ class Document {
|
|
|
37
37
|
logLevel: 'warn',
|
|
38
38
|
prettyErrors: true,
|
|
39
39
|
strict: true,
|
|
40
|
+
stringKeys: false,
|
|
40
41
|
uniqueKeys: true,
|
|
41
42
|
version: '1.2'
|
|
42
43
|
}, options);
|
|
@@ -260,7 +261,7 @@ class Document {
|
|
|
260
261
|
this.directives.yaml.version = '1.1';
|
|
261
262
|
else
|
|
262
263
|
this.directives = new directives.Directives({ version: '1.1' });
|
|
263
|
-
opt = {
|
|
264
|
+
opt = { resolveKnownTags: false, schema: 'yaml-1.1' };
|
|
264
265
|
break;
|
|
265
266
|
case '1.2':
|
|
266
267
|
case 'next':
|
|
@@ -268,7 +269,7 @@ class Document {
|
|
|
268
269
|
this.directives.yaml.version = version;
|
|
269
270
|
else
|
|
270
271
|
this.directives = new directives.Directives({ version });
|
|
271
|
-
opt = {
|
|
272
|
+
opt = { resolveKnownTags: true, schema: 'core' };
|
|
272
273
|
break;
|
|
273
274
|
case null:
|
|
274
275
|
if (this.directives)
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { LineCounter } from './parse/line-counter';
|
|
2
|
-
export type ErrorCode = 'ALIAS_PROPS' | 'BAD_ALIAS' | 'BAD_DIRECTIVE' | 'BAD_DQ_ESCAPE' | 'BAD_INDENT' | 'BAD_PROP_ORDER' | 'BAD_SCALAR_START' | 'BLOCK_AS_IMPLICIT_KEY' | 'BLOCK_IN_FLOW' | 'DUPLICATE_KEY' | 'IMPOSSIBLE' | 'KEY_OVER_1024_CHARS' | 'MISSING_CHAR' | 'MULTILINE_IMPLICIT_KEY' | 'MULTIPLE_ANCHORS' | 'MULTIPLE_DOCS' | 'MULTIPLE_TAGS' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN' | 'BAD_COLLECTION_TYPE';
|
|
2
|
+
export type ErrorCode = 'ALIAS_PROPS' | 'BAD_ALIAS' | 'BAD_DIRECTIVE' | 'BAD_DQ_ESCAPE' | 'BAD_INDENT' | 'BAD_PROP_ORDER' | 'BAD_SCALAR_START' | 'BLOCK_AS_IMPLICIT_KEY' | 'BLOCK_IN_FLOW' | 'DUPLICATE_KEY' | 'IMPOSSIBLE' | 'KEY_OVER_1024_CHARS' | 'MISSING_CHAR' | 'MULTILINE_IMPLICIT_KEY' | 'MULTIPLE_ANCHORS' | 'MULTIPLE_DOCS' | 'MULTIPLE_TAGS' | 'NON_STRING_KEY' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN' | 'BAD_COLLECTION_TYPE';
|
|
3
3
|
export type LinePos = {
|
|
4
4
|
line: number;
|
|
5
5
|
col: number;
|
package/dist/nodes/Node.d.ts
CHANGED
|
@@ -5,7 +5,8 @@ import type { StringifyContext } from '../stringify/stringify.js';
|
|
|
5
5
|
import type { Alias } from './Alias.js';
|
|
6
6
|
import { NODE_TYPE } from './identity.js';
|
|
7
7
|
import type { Scalar } from './Scalar.js';
|
|
8
|
-
import
|
|
8
|
+
import { ToJSContext } from './toJS.js';
|
|
9
|
+
import type { MapLike, YAMLMap } from './YAMLMap.js';
|
|
9
10
|
import type { YAMLSeq } from './YAMLSeq.js';
|
|
10
11
|
export type Node<T = unknown> = Alias | Scalar<T> | YAMLMap<unknown, T> | YAMLSeq<T>;
|
|
11
12
|
/** Utility type mapper */
|
|
@@ -36,6 +37,11 @@ export declare abstract class NodeBase {
|
|
|
36
37
|
srcToken?: Token;
|
|
37
38
|
/** A fully qualified tag, if required */
|
|
38
39
|
tag?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Customize the way that a key-value pair is resolved.
|
|
42
|
+
* Used for YAML 1.1 !!merge << handling.
|
|
43
|
+
*/
|
|
44
|
+
addToJSMap?: (ctx: ToJSContext | undefined, map: MapLike, value: unknown) => void;
|
|
39
45
|
/** A plain JS representation of this node */
|
|
40
46
|
abstract toJSON(): any;
|
|
41
47
|
abstract toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
|
|
@@ -1,24 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var log = require('../log.js');
|
|
4
|
+
var merge = require('../schema/yaml-1.1/merge.js');
|
|
4
5
|
var stringify = require('../stringify/stringify.js');
|
|
5
6
|
var identity = require('./identity.js');
|
|
6
|
-
var Scalar = require('./Scalar.js');
|
|
7
7
|
var toJS = require('./toJS.js');
|
|
8
8
|
|
|
9
|
-
const MERGE_KEY = '<<';
|
|
10
9
|
function addPairToJSMap(ctx, map, { key, value }) {
|
|
11
|
-
if (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
else if (Array.isArray(value))
|
|
17
|
-
for (const it of value)
|
|
18
|
-
mergeToJSMap(ctx, map, it);
|
|
19
|
-
else
|
|
20
|
-
mergeToJSMap(ctx, map, value);
|
|
21
|
-
}
|
|
10
|
+
if (identity.isNode(key) && key.addToJSMap)
|
|
11
|
+
key.addToJSMap(ctx, map, value);
|
|
12
|
+
// TODO: Should drop this special case for bare << handling
|
|
13
|
+
else if (merge.isMergeKey(ctx, key))
|
|
14
|
+
merge.addMergeToJSMap(ctx, map, value);
|
|
22
15
|
else {
|
|
23
16
|
const jsKey = toJS.toJS(key, '', ctx);
|
|
24
17
|
if (map instanceof Map) {
|
|
@@ -43,41 +36,6 @@ function addPairToJSMap(ctx, map, { key, value }) {
|
|
|
43
36
|
}
|
|
44
37
|
return map;
|
|
45
38
|
}
|
|
46
|
-
const isMergeKey = (key) => key === MERGE_KEY ||
|
|
47
|
-
(identity.isScalar(key) &&
|
|
48
|
-
key.value === MERGE_KEY &&
|
|
49
|
-
(!key.type || key.type === Scalar.Scalar.PLAIN));
|
|
50
|
-
// If the value associated with a merge key is a single mapping node, each of
|
|
51
|
-
// its key/value pairs is inserted into the current mapping, unless the key
|
|
52
|
-
// already exists in it. If the value associated with the merge key is a
|
|
53
|
-
// sequence, then this sequence is expected to contain mapping nodes and each
|
|
54
|
-
// of these nodes is merged in turn according to its order in the sequence.
|
|
55
|
-
// Keys in mapping nodes earlier in the sequence override keys specified in
|
|
56
|
-
// later mapping nodes. -- http://yaml.org/type/merge.html
|
|
57
|
-
function mergeToJSMap(ctx, map, value) {
|
|
58
|
-
const source = ctx && identity.isAlias(value) ? value.resolve(ctx.doc) : value;
|
|
59
|
-
if (!identity.isMap(source))
|
|
60
|
-
throw new Error('Merge sources must be maps or map aliases');
|
|
61
|
-
const srcMap = source.toJSON(null, ctx, Map);
|
|
62
|
-
for (const [key, value] of srcMap) {
|
|
63
|
-
if (map instanceof Map) {
|
|
64
|
-
if (!map.has(key))
|
|
65
|
-
map.set(key, value);
|
|
66
|
-
}
|
|
67
|
-
else if (map instanceof Set) {
|
|
68
|
-
map.add(key);
|
|
69
|
-
}
|
|
70
|
-
else if (!Object.prototype.hasOwnProperty.call(map, key)) {
|
|
71
|
-
Object.defineProperty(map, key, {
|
|
72
|
-
value,
|
|
73
|
-
writable: true,
|
|
74
|
-
enumerable: true,
|
|
75
|
-
configurable: true
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return map;
|
|
80
|
-
}
|
|
81
39
|
function stringifyKey(key, jsKey, ctx) {
|
|
82
40
|
if (jsKey === null)
|
|
83
41
|
return '';
|
package/dist/options.d.ts
CHANGED
|
@@ -42,6 +42,12 @@ export type ParseOptions = {
|
|
|
42
42
|
* Default: `true`
|
|
43
43
|
*/
|
|
44
44
|
strict?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Parse all mapping keys as strings. Treat all non-scalar keys as errors.
|
|
47
|
+
*
|
|
48
|
+
* Default: `false`
|
|
49
|
+
*/
|
|
50
|
+
stringKeys?: boolean;
|
|
45
51
|
/**
|
|
46
52
|
* YAML requires map keys to be unique. By default, this is checked by
|
|
47
53
|
* comparing scalar values with `===`; deep equality is not checked for
|
package/dist/parse/lexer.d.ts
CHANGED
|
@@ -59,7 +59,7 @@ export declare class Lexer {
|
|
|
59
59
|
*
|
|
60
60
|
* @returns A generator of lexical tokens
|
|
61
61
|
*/
|
|
62
|
-
lex(source: string, incomplete?: boolean): Generator<string, void,
|
|
62
|
+
lex(source: string, incomplete?: boolean): Generator<string, void, any>;
|
|
63
63
|
private atLineEnd;
|
|
64
64
|
private charAt;
|
|
65
65
|
private continueScalar;
|
package/dist/parse/parser.d.ts
CHANGED
|
@@ -57,14 +57,14 @@ export declare class Parser {
|
|
|
57
57
|
*
|
|
58
58
|
* @returns A generator of tokens representing each directive, document, and other structure.
|
|
59
59
|
*/
|
|
60
|
-
parse(source: string, incomplete?: boolean): Generator<Token, void,
|
|
60
|
+
parse(source: string, incomplete?: boolean): Generator<Token, void, any>;
|
|
61
61
|
/**
|
|
62
62
|
* Advance the parser by the `source` of one lexical token.
|
|
63
63
|
*/
|
|
64
|
-
next(source: string): Generator<Token, void,
|
|
64
|
+
next(source: string): Generator<Token, void, any>;
|
|
65
65
|
private lexer;
|
|
66
66
|
/** Call at end of input to push out any remaining constructions */
|
|
67
|
-
end(): Generator<Token, void,
|
|
67
|
+
end(): Generator<Token, void, any>;
|
|
68
68
|
private get sourceToken();
|
|
69
69
|
private step;
|
|
70
70
|
private peek;
|
package/dist/public-api.js
CHANGED
|
@@ -4,6 +4,7 @@ var composer = require('./compose/composer.js');
|
|
|
4
4
|
var Document = require('./doc/Document.js');
|
|
5
5
|
var errors = require('./errors.js');
|
|
6
6
|
var log = require('./log.js');
|
|
7
|
+
var identity = require('./nodes/identity.js');
|
|
7
8
|
var lineCounter = require('./parse/line-counter.js');
|
|
8
9
|
var parser = require('./parse/parser.js');
|
|
9
10
|
|
|
@@ -95,6 +96,8 @@ function stringify(value, replacer, options) {
|
|
|
95
96
|
if (!keepUndefined)
|
|
96
97
|
return undefined;
|
|
97
98
|
}
|
|
99
|
+
if (identity.isDocument(value) && !_replacer)
|
|
100
|
+
return value.toString(options);
|
|
98
101
|
return new Document.Document(value, _replacer, options).toString(options);
|
|
99
102
|
}
|
|
100
103
|
|
package/dist/schema/Schema.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ import type { CollectionTag, ScalarTag } from './types.js';
|
|
|
5
5
|
export declare class Schema {
|
|
6
6
|
compat: Array<CollectionTag | ScalarTag> | null;
|
|
7
7
|
knownTags: Record<string, CollectionTag | ScalarTag>;
|
|
8
|
-
merge: boolean;
|
|
9
8
|
name: string;
|
|
10
9
|
sortMapEntries: ((a: Pair, b: Pair) => number) | null;
|
|
11
10
|
tags: Array<CollectionTag | ScalarTag>;
|
package/dist/schema/Schema.js
CHANGED
|
@@ -14,10 +14,9 @@ class Schema {
|
|
|
14
14
|
: compat
|
|
15
15
|
? tags.getTags(null, compat)
|
|
16
16
|
: null;
|
|
17
|
-
this.merge = !!merge;
|
|
18
17
|
this.name = (typeof schema === 'string' && schema) || 'core';
|
|
19
18
|
this.knownTags = resolveKnownTags ? tags.coreKnownTags : {};
|
|
20
|
-
this.tags = tags.getTags(customTags, this.name);
|
|
19
|
+
this.tags = tags.getTags(customTags, this.name, merge);
|
|
21
20
|
this.toStringOptions = toStringDefaults ?? null;
|
|
22
21
|
Object.defineProperty(this, identity.MAP, { value: map.map });
|
|
23
22
|
Object.defineProperty(this, identity.SCALAR, { value: string.string });
|
package/dist/schema/tags.d.ts
CHANGED
|
@@ -14,6 +14,10 @@ declare const tagsByName: {
|
|
|
14
14
|
intOct: ScalarTag;
|
|
15
15
|
intTime: ScalarTag;
|
|
16
16
|
map: CollectionTag;
|
|
17
|
+
merge: ScalarTag & {
|
|
18
|
+
identify(value: unknown): boolean;
|
|
19
|
+
test: RegExp;
|
|
20
|
+
};
|
|
17
21
|
null: ScalarTag & {
|
|
18
22
|
test: RegExp;
|
|
19
23
|
};
|
|
@@ -29,6 +33,10 @@ export type TagId = keyof typeof tagsByName;
|
|
|
29
33
|
export type Tags = Array<ScalarTag | CollectionTag | TagId>;
|
|
30
34
|
export declare const coreKnownTags: {
|
|
31
35
|
'tag:yaml.org,2002:binary': ScalarTag;
|
|
36
|
+
'tag:yaml.org,2002:merge': ScalarTag & {
|
|
37
|
+
identify(value: unknown): boolean;
|
|
38
|
+
test: RegExp;
|
|
39
|
+
};
|
|
32
40
|
'tag:yaml.org,2002:omap': CollectionTag;
|
|
33
41
|
'tag:yaml.org,2002:pairs': CollectionTag;
|
|
34
42
|
'tag:yaml.org,2002:set': CollectionTag;
|
|
@@ -36,5 +44,5 @@ export declare const coreKnownTags: {
|
|
|
36
44
|
test: RegExp;
|
|
37
45
|
};
|
|
38
46
|
};
|
|
39
|
-
export declare function getTags(customTags: SchemaOptions['customTags'] | undefined, schemaName: string): (CollectionTag | ScalarTag)[];
|
|
47
|
+
export declare function getTags(customTags: SchemaOptions['customTags'] | undefined, schemaName: string, addMergeTag?: boolean): (CollectionTag | ScalarTag)[];
|
|
40
48
|
export {};
|
package/dist/schema/tags.js
CHANGED
|
@@ -10,6 +10,7 @@ var int = require('./core/int.js');
|
|
|
10
10
|
var schema = require('./core/schema.js');
|
|
11
11
|
var schema$1 = require('./json/schema.js');
|
|
12
12
|
var binary = require('./yaml-1.1/binary.js');
|
|
13
|
+
var merge = require('./yaml-1.1/merge.js');
|
|
13
14
|
var omap = require('./yaml-1.1/omap.js');
|
|
14
15
|
var pairs = require('./yaml-1.1/pairs.js');
|
|
15
16
|
var schema$2 = require('./yaml-1.1/schema.js');
|
|
@@ -35,6 +36,7 @@ const tagsByName = {
|
|
|
35
36
|
intOct: int.intOct,
|
|
36
37
|
intTime: timestamp.intTime,
|
|
37
38
|
map: map.map,
|
|
39
|
+
merge: merge.merge,
|
|
38
40
|
null: _null.nullTag,
|
|
39
41
|
omap: omap.omap,
|
|
40
42
|
pairs: pairs.pairs,
|
|
@@ -44,13 +46,20 @@ const tagsByName = {
|
|
|
44
46
|
};
|
|
45
47
|
const coreKnownTags = {
|
|
46
48
|
'tag:yaml.org,2002:binary': binary.binary,
|
|
49
|
+
'tag:yaml.org,2002:merge': merge.merge,
|
|
47
50
|
'tag:yaml.org,2002:omap': omap.omap,
|
|
48
51
|
'tag:yaml.org,2002:pairs': pairs.pairs,
|
|
49
52
|
'tag:yaml.org,2002:set': set.set,
|
|
50
53
|
'tag:yaml.org,2002:timestamp': timestamp.timestamp
|
|
51
54
|
};
|
|
52
|
-
function getTags(customTags, schemaName) {
|
|
53
|
-
|
|
55
|
+
function getTags(customTags, schemaName, addMergeTag) {
|
|
56
|
+
const schemaTags = schemas.get(schemaName);
|
|
57
|
+
if (schemaTags && !customTags) {
|
|
58
|
+
return addMergeTag && !schemaTags.includes(merge.merge)
|
|
59
|
+
? schemaTags.concat(merge.merge)
|
|
60
|
+
: schemaTags.slice();
|
|
61
|
+
}
|
|
62
|
+
let tags = schemaTags;
|
|
54
63
|
if (!tags) {
|
|
55
64
|
if (Array.isArray(customTags))
|
|
56
65
|
tags = [];
|
|
@@ -69,17 +78,21 @@ function getTags(customTags, schemaName) {
|
|
|
69
78
|
else if (typeof customTags === 'function') {
|
|
70
79
|
tags = customTags(tags.slice());
|
|
71
80
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const tagObj = tagsByName[tag];
|
|
76
|
-
if (tagObj)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
if (addMergeTag)
|
|
82
|
+
tags = tags.concat(merge.merge);
|
|
83
|
+
return tags.reduce((tags, tag) => {
|
|
84
|
+
const tagObj = typeof tag === 'string' ? tagsByName[tag] : tag;
|
|
85
|
+
if (!tagObj) {
|
|
86
|
+
const tagName = JSON.stringify(tag);
|
|
87
|
+
const keys = Object.keys(tagsByName)
|
|
88
|
+
.map(key => JSON.stringify(key))
|
|
89
|
+
.join(', ');
|
|
90
|
+
throw new Error(`Unknown custom tag ${tagName}; use one of ${keys}`);
|
|
91
|
+
}
|
|
92
|
+
if (!tags.includes(tagObj))
|
|
93
|
+
tags.push(tagObj);
|
|
94
|
+
return tags;
|
|
95
|
+
}, []);
|
|
83
96
|
}
|
|
84
97
|
|
|
85
98
|
exports.coreKnownTags = coreKnownTags;
|
package/dist/schema/types.d.ts
CHANGED
|
@@ -12,11 +12,13 @@ interface TagBase {
|
|
|
12
12
|
*/
|
|
13
13
|
createNode?: (schema: Schema, value: unknown, ctx: CreateNodeContext) => Node;
|
|
14
14
|
/**
|
|
15
|
-
* If `true`,
|
|
16
|
-
* an explicit tag
|
|
17
|
-
*
|
|
15
|
+
* If `true`, allows for values to be stringified without
|
|
16
|
+
* an explicit tag together with `test`.
|
|
17
|
+
* If `'key'`, this only applies if the value is used as a mapping key.
|
|
18
|
+
* For most cases, it's unlikely that you'll actually want to use this,
|
|
19
|
+
* even if you first think you do.
|
|
18
20
|
*/
|
|
19
|
-
default?: boolean;
|
|
21
|
+
default?: boolean | 'key';
|
|
20
22
|
/**
|
|
21
23
|
* If a tag has multiple forms that should be parsed and/or stringified
|
|
22
24
|
* differently, use `format` to identify them.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ToJSContext } from '../../nodes/toJS.js';
|
|
2
|
+
import type { MapLike } from '../../nodes/YAMLMap.js';
|
|
3
|
+
import type { ScalarTag } from '../types.js';
|
|
4
|
+
export declare const merge: ScalarTag & {
|
|
5
|
+
identify(value: unknown): boolean;
|
|
6
|
+
test: RegExp;
|
|
7
|
+
};
|
|
8
|
+
export declare const isMergeKey: (ctx: ToJSContext | undefined, key: unknown) => boolean | undefined;
|
|
9
|
+
export declare function addMergeToJSMap(ctx: ToJSContext | undefined, map: MapLike, value: unknown): void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var identity = require('../../nodes/identity.js');
|
|
4
|
+
var Scalar = require('../../nodes/Scalar.js');
|
|
5
|
+
|
|
6
|
+
// If the value associated with a merge key is a single mapping node, each of
|
|
7
|
+
// its key/value pairs is inserted into the current mapping, unless the key
|
|
8
|
+
// already exists in it. If the value associated with the merge key is a
|
|
9
|
+
// sequence, then this sequence is expected to contain mapping nodes and each
|
|
10
|
+
// of these nodes is merged in turn according to its order in the sequence.
|
|
11
|
+
// Keys in mapping nodes earlier in the sequence override keys specified in
|
|
12
|
+
// later mapping nodes. -- http://yaml.org/type/merge.html
|
|
13
|
+
const MERGE_KEY = '<<';
|
|
14
|
+
const merge = {
|
|
15
|
+
identify: value => value === MERGE_KEY ||
|
|
16
|
+
(typeof value === 'symbol' && value.description === MERGE_KEY),
|
|
17
|
+
default: 'key',
|
|
18
|
+
tag: 'tag:yaml.org,2002:merge',
|
|
19
|
+
test: /^<<$/,
|
|
20
|
+
resolve: () => Object.assign(new Scalar.Scalar(Symbol(MERGE_KEY)), {
|
|
21
|
+
addToJSMap: addMergeToJSMap
|
|
22
|
+
}),
|
|
23
|
+
stringify: () => MERGE_KEY
|
|
24
|
+
};
|
|
25
|
+
const isMergeKey = (ctx, key) => (merge.identify(key) ||
|
|
26
|
+
(identity.isScalar(key) &&
|
|
27
|
+
(!key.type || key.type === Scalar.Scalar.PLAIN) &&
|
|
28
|
+
merge.identify(key.value))) &&
|
|
29
|
+
ctx?.doc.schema.tags.some(tag => tag.tag === merge.tag && tag.default);
|
|
30
|
+
function addMergeToJSMap(ctx, map, value) {
|
|
31
|
+
value = ctx && identity.isAlias(value) ? value.resolve(ctx.doc) : value;
|
|
32
|
+
if (identity.isSeq(value))
|
|
33
|
+
for (const it of value.items)
|
|
34
|
+
mergeValue(ctx, map, it);
|
|
35
|
+
else if (Array.isArray(value))
|
|
36
|
+
for (const it of value)
|
|
37
|
+
mergeValue(ctx, map, it);
|
|
38
|
+
else
|
|
39
|
+
mergeValue(ctx, map, value);
|
|
40
|
+
}
|
|
41
|
+
function mergeValue(ctx, map, value) {
|
|
42
|
+
const source = ctx && identity.isAlias(value) ? value.resolve(ctx.doc) : value;
|
|
43
|
+
if (!identity.isMap(source))
|
|
44
|
+
throw new Error('Merge sources must be maps or map aliases');
|
|
45
|
+
const srcMap = source.toJSON(null, ctx, Map);
|
|
46
|
+
for (const [key, value] of srcMap) {
|
|
47
|
+
if (map instanceof Map) {
|
|
48
|
+
if (!map.has(key))
|
|
49
|
+
map.set(key, value);
|
|
50
|
+
}
|
|
51
|
+
else if (map instanceof Set) {
|
|
52
|
+
map.add(key);
|
|
53
|
+
}
|
|
54
|
+
else if (!Object.prototype.hasOwnProperty.call(map, key)) {
|
|
55
|
+
Object.defineProperty(map, key, {
|
|
56
|
+
value,
|
|
57
|
+
writable: true,
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return map;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
exports.addMergeToJSMap = addMergeToJSMap;
|
|
67
|
+
exports.isMergeKey = isMergeKey;
|
|
68
|
+
exports.merge = merge;
|
|
@@ -8,6 +8,7 @@ var binary = require('./binary.js');
|
|
|
8
8
|
var bool = require('./bool.js');
|
|
9
9
|
var float = require('./float.js');
|
|
10
10
|
var int = require('./int.js');
|
|
11
|
+
var merge = require('./merge.js');
|
|
11
12
|
var omap = require('./omap.js');
|
|
12
13
|
var pairs = require('./pairs.js');
|
|
13
14
|
var set = require('./set.js');
|
|
@@ -28,6 +29,7 @@ const schema = [
|
|
|
28
29
|
float.floatExp,
|
|
29
30
|
float.float,
|
|
30
31
|
binary.binary,
|
|
32
|
+
merge.merge,
|
|
31
33
|
omap.omap,
|
|
32
34
|
pairs.pairs,
|
|
33
35
|
set.set,
|
|
@@ -97,7 +97,7 @@ const timestamp = {
|
|
|
97
97
|
}
|
|
98
98
|
return new Date(date);
|
|
99
99
|
},
|
|
100
|
-
stringify: ({ value }) => value.toISOString().replace(/(
|
|
100
|
+
stringify: ({ value }) => value.toISOString().replace(/(T00:00:00)?\.000Z$/, '')
|
|
101
101
|
};
|
|
102
102
|
|
|
103
103
|
exports.floatTime = floatTime;
|
|
@@ -56,7 +56,12 @@ function getTagObject(tags, item) {
|
|
|
56
56
|
let obj;
|
|
57
57
|
if (identity.isScalar(item)) {
|
|
58
58
|
obj = item.value;
|
|
59
|
-
|
|
59
|
+
let match = tags.filter(t => t.identify?.(obj));
|
|
60
|
+
if (match.length > 1) {
|
|
61
|
+
const testMatch = match.filter(t => t.test);
|
|
62
|
+
if (testMatch.length > 0)
|
|
63
|
+
match = testMatch;
|
|
64
|
+
}
|
|
60
65
|
tagObj =
|
|
61
66
|
match.find(t => t.format === item.format) ?? match.find(t => !t.format);
|
|
62
67
|
}
|
|
@@ -221,23 +221,32 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
|
|
|
221
221
|
start = start.replace(/\n+/g, `$&${indent}`);
|
|
222
222
|
}
|
|
223
223
|
const indentSize = indent ? '2' : '1'; // root is at -1
|
|
224
|
-
|
|
224
|
+
// Leading | or > is added later
|
|
225
|
+
let header = (startWithSpace ? indentSize : '') + chomp;
|
|
225
226
|
if (comment) {
|
|
226
227
|
header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
|
|
227
228
|
if (onComment)
|
|
228
229
|
onComment();
|
|
229
230
|
}
|
|
230
|
-
if (literal) {
|
|
231
|
-
|
|
232
|
-
|
|
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}`;
|
|
233
247
|
}
|
|
234
|
-
value = value
|
|
235
|
-
|
|
236
|
-
.replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
|
|
237
|
-
// ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
|
|
238
|
-
.replace(/\n+/g, `$&${indent}`);
|
|
239
|
-
const body = foldFlowLines.foldFlowLines(`${start}${value}${end}`, indent, foldFlowLines.FOLD_BLOCK, getFoldOptions(ctx, true));
|
|
240
|
-
return `${header}\n${indent}${body}`;
|
|
248
|
+
value = value.replace(/\n+/g, `$&${indent}`);
|
|
249
|
+
return `|${header}\n${indent}${start}${value}${end}`;
|
|
241
250
|
}
|
|
242
251
|
function plainString(item, ctx, onComment, onChompKeep) {
|
|
243
252
|
const { type, value } = item;
|