yaml 2.0.0-9 → 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/README.md +1 -1
- package/browser/dist/compose/compose-collection.js +1 -1
- package/browser/dist/compose/compose-doc.js +4 -3
- package/browser/dist/compose/compose-node.js +13 -4
- package/browser/dist/compose/compose-scalar.js +18 -13
- package/browser/dist/compose/composer.js +8 -10
- package/browser/dist/compose/resolve-block-map.js +15 -8
- 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 +13 -5
- package/browser/dist/compose/resolve-flow-scalar.js +1 -1
- package/browser/dist/compose/resolve-props.js +9 -1
- package/browser/dist/compose/util-empty-scalar-position.js +1 -1
- package/browser/dist/compose/util-flow-indent-check.js +15 -0
- package/browser/dist/doc/Document.js +53 -20
- package/browser/dist/doc/anchors.js +2 -2
- package/browser/dist/doc/createNode.js +8 -7
- package/browser/dist/doc/directives.js +7 -4
- package/browser/dist/index.js +1 -2
- 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 +7 -7
- package/browser/dist/nodes/YAMLSeq.js +2 -10
- package/browser/dist/nodes/addPairToJSMap.js +1 -1
- package/browser/dist/nodes/toJS.js +3 -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 +20 -8
- package/browser/dist/parse/parser.js +142 -68
- package/browser/dist/public-api.js +5 -7
- 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 +9 -9
- 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 +1 -1
- package/browser/dist/schema/yaml-1.1/set.js +4 -0
- package/browser/dist/stringify/stringify.js +49 -22
- package/browser/dist/stringify/stringifyCollection.js +114 -85
- package/browser/dist/stringify/stringifyComment.js +18 -12
- package/browser/dist/stringify/stringifyDocument.js +36 -15
- package/browser/dist/stringify/stringifyPair.js +34 -20
- package/browser/dist/stringify/stringifyString.js +8 -11
- package/browser/dist/visit.js +149 -62
- package/dist/compose/compose-collection.js +1 -1
- package/dist/compose/compose-doc.js +4 -3
- package/dist/compose/compose-node.d.ts +1 -0
- package/dist/compose/compose-node.js +13 -4
- package/dist/compose/compose-scalar.js +17 -12
- package/dist/compose/composer.d.ts +1 -1
- package/dist/compose/composer.js +10 -12
- package/dist/compose/resolve-block-map.js +15 -8
- 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 +13 -5
- package/dist/compose/resolve-flow-scalar.js +1 -1
- package/dist/compose/resolve-props.d.ts +2 -1
- package/dist/compose/resolve-props.js +9 -1
- package/dist/compose/util-empty-scalar-position.js +1 -1
- 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 +17 -14
- package/dist/doc/Document.js +58 -25
- package/dist/doc/anchors.d.ts +3 -3
- package/dist/doc/anchors.js +2 -2
- package/dist/doc/createNode.d.ts +1 -1
- package/dist/doc/createNode.js +8 -7
- package/dist/doc/directives.d.ts +4 -2
- package/dist/doc/directives.js +7 -4
- package/dist/index.d.ts +4 -2
- package/dist/index.js +1 -2
- 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 +9 -4
- package/dist/nodes/YAMLMap.js +7 -7
- package/dist/nodes/YAMLSeq.d.ts +4 -1
- package/dist/nodes/YAMLSeq.js +2 -10
- package/dist/nodes/addPairToJSMap.js +1 -1
- package/dist/nodes/toJS.js +3 -1
- package/dist/options.d.ts +44 -13
- package/dist/parse/cst-scalar.js +1 -2
- package/dist/parse/cst-visit.js +2 -2
- package/dist/parse/lexer.js +20 -8
- package/dist/parse/parser.js +142 -68
- package/dist/public-api.js +5 -7
- 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/json-schema.d.ts +69 -0
- package/dist/schema/tags.d.ts +1 -2
- package/dist/schema/tags.js +9 -9
- package/dist/schema/yaml-1.1/float.js +4 -1
- 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 -1
- 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.d.ts +5 -3
- package/dist/stringify/stringify.js +49 -22
- package/dist/stringify/stringifyCollection.d.ts +2 -6
- package/dist/stringify/stringifyCollection.js +113 -84
- package/dist/stringify/stringifyComment.d.ts +10 -2
- package/dist/stringify/stringifyComment.js +19 -12
- package/dist/stringify/stringifyDocument.js +35 -14
- package/dist/stringify/stringifyPair.js +33 -19
- package/dist/stringify/stringifyString.js +8 -11
- package/dist/test-events.js +14 -14
- package/dist/visit.d.ts +49 -10
- package/dist/visit.js +148 -60
- package/package.json +11 -10
- package/browser/dist/options.js +0 -18
- package/dist/node_modules/tslib/tslib.es6.js +0 -76
- package/dist/options.js +0 -20
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { anchorIsValid } from '../doc/anchors.js';
|
|
2
2
|
import { isPair, isAlias, isNode, isScalar, isCollection } from '../nodes/Node.js';
|
|
3
|
+
import { stringifyComment } from './stringifyComment.js';
|
|
3
4
|
import { stringifyString } from './stringifyString.js';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
doc,
|
|
8
|
-
indent: '',
|
|
9
|
-
indentStep: typeof options.indent === 'number' ? ' '.repeat(options.indent) : ' ',
|
|
10
|
-
options: Object.assign({
|
|
6
|
+
function createStringifyContext(doc, options) {
|
|
7
|
+
const opt = Object.assign({
|
|
11
8
|
blockQuote: true,
|
|
9
|
+
commentString: stringifyComment,
|
|
12
10
|
defaultKeyType: null,
|
|
13
11
|
defaultStringType: 'PLAIN',
|
|
14
12
|
directives: null,
|
|
@@ -23,54 +21,83 @@ const createStringifyContext = (doc, options) => ({
|
|
|
23
21
|
singleQuote: null,
|
|
24
22
|
trueStr: 'true',
|
|
25
23
|
verifyAliasOrder: true
|
|
26
|
-
}, options)
|
|
27
|
-
|
|
24
|
+
}, doc.schema.toStringOptions, options);
|
|
25
|
+
let inFlow;
|
|
26
|
+
switch (opt.collectionStyle) {
|
|
27
|
+
case 'block':
|
|
28
|
+
inFlow = false;
|
|
29
|
+
break;
|
|
30
|
+
case 'flow':
|
|
31
|
+
inFlow = true;
|
|
32
|
+
break;
|
|
33
|
+
default:
|
|
34
|
+
inFlow = null;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
anchors: new Set(),
|
|
38
|
+
doc,
|
|
39
|
+
indent: '',
|
|
40
|
+
indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ',
|
|
41
|
+
inFlow,
|
|
42
|
+
options: opt
|
|
43
|
+
};
|
|
44
|
+
}
|
|
28
45
|
function getTagObject(tags, item) {
|
|
29
46
|
if (item.tag) {
|
|
30
47
|
const match = tags.filter(t => t.tag === item.tag);
|
|
31
48
|
if (match.length > 0)
|
|
32
|
-
return match.find(t => t.format === item.format)
|
|
49
|
+
return match.find(t => t.format === item.format) ?? match[0];
|
|
33
50
|
}
|
|
34
51
|
let tagObj = undefined;
|
|
35
52
|
let obj;
|
|
36
53
|
if (isScalar(item)) {
|
|
37
54
|
obj = item.value;
|
|
38
|
-
const match = tags.filter(t => t.identify
|
|
55
|
+
const match = tags.filter(t => t.identify?.(obj));
|
|
39
56
|
tagObj =
|
|
40
|
-
match.find(t => t.format === item.format)
|
|
57
|
+
match.find(t => t.format === item.format) ?? match.find(t => !t.format);
|
|
41
58
|
}
|
|
42
59
|
else {
|
|
43
60
|
obj = item;
|
|
44
61
|
tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
|
|
45
62
|
}
|
|
46
63
|
if (!tagObj) {
|
|
47
|
-
|
|
48
|
-
const name = obj && obj.constructor ? obj.constructor.name : typeof obj;
|
|
64
|
+
const name = obj?.constructor?.name ?? typeof obj;
|
|
49
65
|
throw new Error(`Tag not resolved for ${name} value`);
|
|
50
66
|
}
|
|
51
67
|
return tagObj;
|
|
52
68
|
}
|
|
53
69
|
// needs to be called before value stringifier to allow for circular anchor refs
|
|
54
70
|
function stringifyProps(node, tagObj, { anchors, doc }) {
|
|
71
|
+
if (!doc.directives)
|
|
72
|
+
return '';
|
|
55
73
|
const props = [];
|
|
56
74
|
const anchor = (isScalar(node) || isCollection(node)) && node.anchor;
|
|
57
75
|
if (anchor && anchorIsValid(anchor)) {
|
|
58
76
|
anchors.add(anchor);
|
|
59
77
|
props.push(`&${anchor}`);
|
|
60
78
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
else if (!tagObj.default) {
|
|
65
|
-
props.push(doc.directives.tagString(tagObj.tag));
|
|
66
|
-
}
|
|
79
|
+
const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
|
|
80
|
+
if (tag)
|
|
81
|
+
props.push(doc.directives.tagString(tag));
|
|
67
82
|
return props.join(' ');
|
|
68
83
|
}
|
|
69
84
|
function stringify(item, ctx, onComment, onChompKeep) {
|
|
70
85
|
if (isPair(item))
|
|
71
86
|
return item.toString(ctx, onComment, onChompKeep);
|
|
72
|
-
if (isAlias(item))
|
|
73
|
-
|
|
87
|
+
if (isAlias(item)) {
|
|
88
|
+
if (ctx.doc.directives)
|
|
89
|
+
return item.toString(ctx);
|
|
90
|
+
if (ctx.resolvedAliases?.has(item)) {
|
|
91
|
+
throw new TypeError(`Cannot stringify circular structure without alias nodes`);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
if (ctx.resolvedAliases)
|
|
95
|
+
ctx.resolvedAliases.add(item);
|
|
96
|
+
else
|
|
97
|
+
ctx.resolvedAliases = new Set([item]);
|
|
98
|
+
item = item.resolve(ctx.doc);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
74
101
|
let tagObj = undefined;
|
|
75
102
|
const node = isNode(item)
|
|
76
103
|
? item
|
|
@@ -79,7 +106,7 @@ function stringify(item, ctx, onComment, onChompKeep) {
|
|
|
79
106
|
tagObj = getTagObject(ctx.doc.schema.tags, node);
|
|
80
107
|
const props = stringifyProps(node, tagObj, ctx);
|
|
81
108
|
if (props.length > 0)
|
|
82
|
-
ctx.indentAtStart = (ctx.indentAtStart
|
|
109
|
+
ctx.indentAtStart = (ctx.indentAtStart ?? 0) + props.length + 1;
|
|
83
110
|
const str = typeof tagObj.stringify === 'function'
|
|
84
111
|
? tagObj.stringify(node, ctx, onComment, onChompKeep)
|
|
85
112
|
: isScalar(node)
|
|
@@ -1,122 +1,151 @@
|
|
|
1
1
|
import { Collection } from '../nodes/Collection.js';
|
|
2
2
|
import { isNode, isPair } from '../nodes/Node.js';
|
|
3
3
|
import { stringify } from './stringify.js';
|
|
4
|
-
import {
|
|
4
|
+
import { lineComment, indentComment } from './stringifyComment.js';
|
|
5
5
|
|
|
6
|
-
function stringifyCollection(
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
function stringifyCollection(collection, ctx, options) {
|
|
7
|
+
const flow = ctx.inFlow ?? collection.flow;
|
|
8
|
+
const stringify = flow ? stringifyFlowCollection : stringifyBlockCollection;
|
|
9
|
+
return stringify(collection, ctx, options);
|
|
10
|
+
}
|
|
11
|
+
function stringifyBlockCollection({ comment, items }, ctx, { blockItemPrefix, flowChars, itemIndent, onChompKeep, onComment }) {
|
|
12
|
+
const { indent, options: { commentString } } = ctx;
|
|
13
|
+
const itemCtx = Object.assign({}, ctx, { indent: itemIndent, type: null });
|
|
13
14
|
let chompKeep = false; // flag for the preceding node's status
|
|
14
|
-
const
|
|
15
|
+
const lines = [];
|
|
16
|
+
for (let i = 0; i < items.length; ++i) {
|
|
17
|
+
const item = items[i];
|
|
15
18
|
let comment = null;
|
|
16
19
|
if (isNode(item)) {
|
|
17
20
|
if (!chompKeep && item.spaceBefore)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (
|
|
21
|
-
cb = cb.replace(/^\n+/, '');
|
|
22
|
-
if (cb) {
|
|
23
|
-
if (/^\n+$/.test(cb))
|
|
24
|
-
cb = cb.substring(1);
|
|
25
|
-
// This match will always succeed on a non-empty string
|
|
26
|
-
for (const line of cb.match(/^.*$/gm)) {
|
|
27
|
-
const str = line === ' ' ? '#' : line ? `#${line}` : '';
|
|
28
|
-
nodes.push({ comment: true, str });
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
if (item.comment) {
|
|
21
|
+
lines.push('');
|
|
22
|
+
addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
|
|
23
|
+
if (item.comment)
|
|
32
24
|
comment = item.comment;
|
|
33
|
-
singleLineOutput = false;
|
|
34
|
-
}
|
|
35
25
|
}
|
|
36
26
|
else if (isPair(item)) {
|
|
37
27
|
const ik = isNode(item.key) ? item.key : null;
|
|
38
28
|
if (ik) {
|
|
39
29
|
if (!chompKeep && ik.spaceBefore)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (cb && chompKeep)
|
|
43
|
-
cb = cb.replace(/^\n+/, '');
|
|
44
|
-
if (cb) {
|
|
45
|
-
if (/^\n+$/.test(cb))
|
|
46
|
-
cb = cb.substring(1);
|
|
47
|
-
// This match will always succeed on a non-empty string
|
|
48
|
-
for (const line of cb.match(/^.*$/gm)) {
|
|
49
|
-
const str = line === ' ' ? '#' : line ? `#${line}` : '';
|
|
50
|
-
nodes.push({ comment: true, str });
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
if (ik.comment)
|
|
54
|
-
singleLineOutput = false;
|
|
55
|
-
}
|
|
56
|
-
if (inFlow) {
|
|
57
|
-
const iv = isNode(item.value) ? item.value : null;
|
|
58
|
-
if (iv) {
|
|
59
|
-
if (iv.comment)
|
|
60
|
-
comment = iv.comment;
|
|
61
|
-
if (iv.comment || iv.commentBefore)
|
|
62
|
-
singleLineOutput = false;
|
|
63
|
-
}
|
|
64
|
-
else if (item.value == null && ik && ik.comment) {
|
|
65
|
-
comment = ik.comment;
|
|
66
|
-
}
|
|
30
|
+
lines.push('');
|
|
31
|
+
addCommentBefore(ctx, lines, ik.commentBefore, chompKeep);
|
|
67
32
|
}
|
|
68
33
|
}
|
|
69
34
|
chompKeep = false;
|
|
70
|
-
let str = stringify(item,
|
|
71
|
-
if (
|
|
72
|
-
str +=
|
|
73
|
-
|
|
74
|
-
if (chompKeep && (comment || inFlow))
|
|
35
|
+
let str = stringify(item, itemCtx, () => (comment = null), () => (chompKeep = true));
|
|
36
|
+
if (comment)
|
|
37
|
+
str += lineComment(str, itemIndent, commentString(comment));
|
|
38
|
+
if (chompKeep && comment)
|
|
75
39
|
chompKeep = false;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}, []);
|
|
40
|
+
lines.push(blockItemPrefix + str);
|
|
41
|
+
}
|
|
79
42
|
let str;
|
|
80
|
-
if (
|
|
43
|
+
if (lines.length === 0) {
|
|
81
44
|
str = flowChars.start + flowChars.end;
|
|
82
45
|
}
|
|
83
|
-
else
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
46
|
+
else {
|
|
47
|
+
str = lines[0];
|
|
48
|
+
for (let i = 1; i < lines.length; ++i) {
|
|
49
|
+
const line = lines[i];
|
|
50
|
+
str += line ? `\n${indent}${line}` : '\n';
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (comment) {
|
|
54
|
+
str += '\n' + indentComment(commentString(comment), indent);
|
|
55
|
+
if (onComment)
|
|
56
|
+
onComment();
|
|
57
|
+
}
|
|
58
|
+
else if (chompKeep && onChompKeep)
|
|
59
|
+
onChompKeep();
|
|
60
|
+
return str;
|
|
61
|
+
}
|
|
62
|
+
function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemIndent, onComment }) {
|
|
63
|
+
const { indent, indentStep, options: { commentString } } = ctx;
|
|
64
|
+
itemIndent += indentStep;
|
|
65
|
+
const itemCtx = Object.assign({}, ctx, {
|
|
66
|
+
indent: itemIndent,
|
|
67
|
+
inFlow: true,
|
|
68
|
+
type: null
|
|
69
|
+
});
|
|
70
|
+
let reqNewline = false;
|
|
71
|
+
let linesAtValue = 0;
|
|
72
|
+
const lines = [];
|
|
73
|
+
for (let i = 0; i < items.length; ++i) {
|
|
74
|
+
const item = items[i];
|
|
75
|
+
let comment = null;
|
|
76
|
+
if (isNode(item)) {
|
|
77
|
+
if (item.spaceBefore)
|
|
78
|
+
lines.push('');
|
|
79
|
+
addCommentBefore(ctx, lines, item.commentBefore, false);
|
|
80
|
+
if (item.comment)
|
|
81
|
+
comment = item.comment;
|
|
82
|
+
}
|
|
83
|
+
else if (isPair(item)) {
|
|
84
|
+
const ik = isNode(item.key) ? item.key : null;
|
|
85
|
+
if (ik) {
|
|
86
|
+
if (ik.spaceBefore)
|
|
87
|
+
lines.push('');
|
|
88
|
+
addCommentBefore(ctx, lines, ik.commentBefore, false);
|
|
89
|
+
if (ik.comment)
|
|
90
|
+
reqNewline = true;
|
|
91
|
+
}
|
|
92
|
+
const iv = isNode(item.value) ? item.value : null;
|
|
93
|
+
if (iv) {
|
|
94
|
+
if (iv.comment)
|
|
95
|
+
comment = iv.comment;
|
|
96
|
+
if (iv.commentBefore)
|
|
97
|
+
reqNewline = true;
|
|
98
|
+
}
|
|
99
|
+
else if (item.value == null && ik && ik.comment) {
|
|
100
|
+
comment = ik.comment;
|
|
91
101
|
}
|
|
92
|
-
singleLineLength += node.str.length + 2;
|
|
93
102
|
}
|
|
94
|
-
if (
|
|
95
|
-
|
|
103
|
+
if (comment)
|
|
104
|
+
reqNewline = true;
|
|
105
|
+
let str = stringify(item, itemCtx, () => (comment = null));
|
|
106
|
+
if (i < items.length - 1)
|
|
107
|
+
str += ',';
|
|
108
|
+
if (comment)
|
|
109
|
+
str += lineComment(str, itemIndent, commentString(comment));
|
|
110
|
+
if (!reqNewline && (lines.length > linesAtValue || str.includes('\n')))
|
|
111
|
+
reqNewline = true;
|
|
112
|
+
lines.push(str);
|
|
113
|
+
linesAtValue = lines.length;
|
|
114
|
+
}
|
|
115
|
+
let str;
|
|
116
|
+
const { start, end } = flowChars;
|
|
117
|
+
if (lines.length === 0) {
|
|
118
|
+
str = start + end;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
if (!reqNewline) {
|
|
122
|
+
const len = lines.reduce((sum, line) => sum + line.length + 2, 2);
|
|
123
|
+
reqNewline = len > Collection.maxFlowStringSingleLineLength;
|
|
124
|
+
}
|
|
125
|
+
if (reqNewline) {
|
|
96
126
|
str = start;
|
|
97
|
-
for (const
|
|
98
|
-
str +=
|
|
99
|
-
}
|
|
127
|
+
for (const line of lines)
|
|
128
|
+
str += line ? `\n${indentStep}${indent}${line}` : '\n';
|
|
100
129
|
str += `\n${indent}${end}`;
|
|
101
130
|
}
|
|
102
131
|
else {
|
|
103
|
-
str = `${start} ${
|
|
132
|
+
str = `${start} ${lines.join(' ')} ${end}`;
|
|
104
133
|
}
|
|
105
134
|
}
|
|
106
|
-
else {
|
|
107
|
-
const strings = nodes.map(blockItem);
|
|
108
|
-
str = strings.shift() || '';
|
|
109
|
-
for (const s of strings)
|
|
110
|
-
str += s ? `\n${indent}${s}` : '\n';
|
|
111
|
-
}
|
|
112
135
|
if (comment) {
|
|
113
|
-
str +=
|
|
136
|
+
str += lineComment(str, commentString(comment), indent);
|
|
114
137
|
if (onComment)
|
|
115
138
|
onComment();
|
|
116
139
|
}
|
|
117
|
-
else if (chompKeep && onChompKeep)
|
|
118
|
-
onChompKeep();
|
|
119
140
|
return str;
|
|
120
141
|
}
|
|
142
|
+
function addCommentBefore({ indent, options: { commentString } }, lines, comment, chompKeep) {
|
|
143
|
+
if (comment && chompKeep)
|
|
144
|
+
comment = comment.replace(/^\n+/, '');
|
|
145
|
+
if (comment) {
|
|
146
|
+
const ic = indentComment(commentString(comment), indent);
|
|
147
|
+
lines.push(ic.trimStart()); // Avoid double indent on first line
|
|
148
|
+
}
|
|
149
|
+
}
|
|
121
150
|
|
|
122
151
|
export { stringifyCollection };
|
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Stringifies a comment.
|
|
3
|
+
*
|
|
4
|
+
* Empty comment lines are left empty,
|
|
5
|
+
* lines consisting of a single space are replaced by `#`,
|
|
6
|
+
* and all other lines are prefixed with a `#`.
|
|
7
|
+
*/
|
|
8
|
+
const stringifyComment = (str) => str.replace(/^(?!$)(?: $)?/gm, '#');
|
|
9
|
+
function indentComment(comment, indent) {
|
|
10
|
+
if (/^\n+$/.test(comment))
|
|
11
|
+
return comment.substring(1);
|
|
12
|
+
return indent ? comment.replace(/^(?! *$)/gm, indent) : comment;
|
|
12
13
|
}
|
|
14
|
+
const lineComment = (str, indent, comment) => str.endsWith('\n')
|
|
15
|
+
? indentComment(comment, indent)
|
|
16
|
+
: comment.includes('\n')
|
|
17
|
+
? '\n' + indentComment(comment, indent)
|
|
18
|
+
: (str.endsWith(' ') ? '' : ' ') + comment;
|
|
13
19
|
|
|
14
|
-
export {
|
|
20
|
+
export { indentComment, lineComment, stringifyComment };
|
|
@@ -1,35 +1,39 @@
|
|
|
1
1
|
import { isNode } from '../nodes/Node.js';
|
|
2
2
|
import { createStringifyContext, stringify } from './stringify.js';
|
|
3
|
-
import {
|
|
3
|
+
import { indentComment, lineComment } from './stringifyComment.js';
|
|
4
4
|
|
|
5
5
|
function stringifyDocument(doc, options) {
|
|
6
6
|
const lines = [];
|
|
7
7
|
let hasDirectives = options.directives === true;
|
|
8
|
-
if (options.directives !== false) {
|
|
8
|
+
if (options.directives !== false && doc.directives) {
|
|
9
9
|
const dir = doc.directives.toString(doc);
|
|
10
10
|
if (dir) {
|
|
11
11
|
lines.push(dir);
|
|
12
12
|
hasDirectives = true;
|
|
13
13
|
}
|
|
14
|
-
else if (doc.directives.
|
|
14
|
+
else if (doc.directives.docStart)
|
|
15
15
|
hasDirectives = true;
|
|
16
16
|
}
|
|
17
17
|
if (hasDirectives)
|
|
18
18
|
lines.push('---');
|
|
19
|
+
const ctx = createStringifyContext(doc, options);
|
|
20
|
+
const { commentString } = ctx.options;
|
|
19
21
|
if (doc.commentBefore) {
|
|
20
22
|
if (lines.length !== 1)
|
|
21
23
|
lines.unshift('');
|
|
22
|
-
|
|
24
|
+
const cs = commentString(doc.commentBefore);
|
|
25
|
+
lines.unshift(indentComment(cs, ''));
|
|
23
26
|
}
|
|
24
|
-
const ctx = createStringifyContext(doc, options);
|
|
25
27
|
let chompKeep = false;
|
|
26
28
|
let contentComment = null;
|
|
27
29
|
if (doc.contents) {
|
|
28
30
|
if (isNode(doc.contents)) {
|
|
29
31
|
if (doc.contents.spaceBefore && hasDirectives)
|
|
30
32
|
lines.push('');
|
|
31
|
-
if (doc.contents.commentBefore)
|
|
32
|
-
|
|
33
|
+
if (doc.contents.commentBefore) {
|
|
34
|
+
const cs = commentString(doc.contents.commentBefore);
|
|
35
|
+
lines.push(indentComment(cs, ''));
|
|
36
|
+
}
|
|
33
37
|
// top-level block scalars need to be indented if followed by a comment
|
|
34
38
|
ctx.forceBlockIndent = !!doc.comment;
|
|
35
39
|
contentComment = doc.contents.comment;
|
|
@@ -37,7 +41,7 @@ function stringifyDocument(doc, options) {
|
|
|
37
41
|
const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
|
|
38
42
|
let body = stringify(doc.contents, ctx, () => (contentComment = null), onChompKeep);
|
|
39
43
|
if (contentComment)
|
|
40
|
-
body
|
|
44
|
+
body += lineComment(body, '', commentString(contentComment));
|
|
41
45
|
if ((body[0] === '|' || body[0] === '>') &&
|
|
42
46
|
lines[lines.length - 1] === '---') {
|
|
43
47
|
// Top-level block scalars with a preceding doc marker ought to use the
|
|
@@ -50,13 +54,30 @@ function stringifyDocument(doc, options) {
|
|
|
50
54
|
else {
|
|
51
55
|
lines.push(stringify(doc.contents, ctx));
|
|
52
56
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
if (doc.directives?.docEnd) {
|
|
58
|
+
if (doc.comment) {
|
|
59
|
+
const cs = commentString(doc.comment);
|
|
60
|
+
if (cs.includes('\n')) {
|
|
61
|
+
lines.push('...');
|
|
62
|
+
lines.push(indentComment(cs, ''));
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
lines.push(`... ${cs}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
lines.push('...');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
let dc = doc.comment;
|
|
74
|
+
if (dc && chompKeep)
|
|
75
|
+
dc = dc.replace(/^\n+/, '');
|
|
76
|
+
if (dc) {
|
|
77
|
+
if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
|
|
78
|
+
lines.push('');
|
|
79
|
+
lines.push(indentComment(commentString(dc), ''));
|
|
80
|
+
}
|
|
60
81
|
}
|
|
61
82
|
return lines.join('\n') + '\n';
|
|
62
83
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isCollection, isNode, isScalar, isSeq } from '../nodes/Node.js';
|
|
2
2
|
import { Scalar } from '../nodes/Scalar.js';
|
|
3
3
|
import { stringify } from './stringify.js';
|
|
4
|
-
import {
|
|
4
|
+
import { lineComment, indentComment } from './stringifyComment.js';
|
|
5
5
|
|
|
6
6
|
function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
|
|
7
|
-
const { allNullValues, doc, indent, indentStep, options: { indentSeq, simpleKeys } } = ctx;
|
|
7
|
+
const { allNullValues, doc, indent, indentStep, options: { commentString, indentSeq, simpleKeys } } = ctx;
|
|
8
8
|
let keyComment = (isNode(key) && key.comment) || null;
|
|
9
9
|
if (simpleKeys) {
|
|
10
10
|
if (keyComment) {
|
|
@@ -39,28 +39,39 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
|
|
|
39
39
|
if (allNullValues || value == null) {
|
|
40
40
|
if (keyCommentDone && onComment)
|
|
41
41
|
onComment();
|
|
42
|
-
return explicitKey ? `? ${str}` : str;
|
|
42
|
+
return str === '' ? '?' : explicitKey ? `? ${str}` : str;
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
else if ((allNullValues && !simpleKeys) || (value == null && explicitKey)) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
str = `? ${str}`;
|
|
47
|
+
if (keyComment && !keyCommentDone) {
|
|
48
|
+
str += lineComment(str, ctx.indent, commentString(keyComment));
|
|
49
|
+
}
|
|
50
|
+
else if (chompKeep && onChompKeep)
|
|
49
51
|
onChompKeep();
|
|
50
|
-
return
|
|
52
|
+
return str;
|
|
51
53
|
}
|
|
52
54
|
if (keyCommentDone)
|
|
53
55
|
keyComment = null;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
if (explicitKey) {
|
|
57
|
+
if (keyComment)
|
|
58
|
+
str += lineComment(str, ctx.indent, commentString(keyComment));
|
|
59
|
+
str = `? ${str}\n${indent}:`;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
str = `${str}:`;
|
|
63
|
+
if (keyComment)
|
|
64
|
+
str += lineComment(str, ctx.indent, commentString(keyComment));
|
|
65
|
+
}
|
|
57
66
|
let vcb = '';
|
|
58
67
|
let valueComment = null;
|
|
59
68
|
if (isNode(value)) {
|
|
60
69
|
if (value.spaceBefore)
|
|
61
70
|
vcb = '\n';
|
|
62
|
-
if (value.commentBefore)
|
|
63
|
-
|
|
71
|
+
if (value.commentBefore) {
|
|
72
|
+
const cs = commentString(value.commentBefore);
|
|
73
|
+
vcb += `\n${indentComment(cs, ctx.indent)}`;
|
|
74
|
+
}
|
|
64
75
|
valueComment = value.comment;
|
|
65
76
|
}
|
|
66
77
|
else if (value && typeof value === 'object') {
|
|
@@ -85,7 +96,10 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
|
|
|
85
96
|
const valueStr = stringify(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true));
|
|
86
97
|
let ws = ' ';
|
|
87
98
|
if (vcb || keyComment) {
|
|
88
|
-
|
|
99
|
+
if (valueStr === '' && !ctx.inFlow)
|
|
100
|
+
ws = vcb === '\n' ? '\n\n' : vcb;
|
|
101
|
+
else
|
|
102
|
+
ws = `${vcb}\n${ctx.indent}`;
|
|
89
103
|
}
|
|
90
104
|
else if (!explicitKey && isCollection(value)) {
|
|
91
105
|
const flow = valueStr[0] === '[' || valueStr[0] === '{';
|
|
@@ -94,18 +108,18 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
|
|
|
94
108
|
}
|
|
95
109
|
else if (valueStr === '' || valueStr[0] === '\n')
|
|
96
110
|
ws = '';
|
|
111
|
+
str += ws + valueStr;
|
|
97
112
|
if (ctx.inFlow) {
|
|
98
113
|
if (valueCommentDone && onComment)
|
|
99
114
|
onComment();
|
|
100
|
-
return str + ws + valueStr;
|
|
101
115
|
}
|
|
102
|
-
else {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return addComment(str + ws + valueStr, ctx.indent, valueComment);
|
|
116
|
+
else if (valueComment && !valueCommentDone) {
|
|
117
|
+
str += lineComment(str, ctx.indent, commentString(valueComment));
|
|
118
|
+
}
|
|
119
|
+
else if (chompKeep && onChompKeep) {
|
|
120
|
+
onChompKeep();
|
|
108
121
|
}
|
|
122
|
+
return str;
|
|
109
123
|
}
|
|
110
124
|
|
|
111
125
|
export { stringifyPair };
|
|
@@ -147,7 +147,7 @@ function quotedString(value, ctx) {
|
|
|
147
147
|
return qs(value, ctx);
|
|
148
148
|
}
|
|
149
149
|
function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
|
|
150
|
-
const {
|
|
150
|
+
const { blockQuote, commentString, lineWidth } = ctx.options;
|
|
151
151
|
// 1. Block can't end in whitespace unless the last line is non-empty.
|
|
152
152
|
// 2. Strings consisting of only whitespace are best rendered explicitly.
|
|
153
153
|
if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
|
|
@@ -212,7 +212,7 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
|
|
|
212
212
|
const indentSize = indent ? '2' : '1'; // root is at -1
|
|
213
213
|
let header = (literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp;
|
|
214
214
|
if (comment) {
|
|
215
|
-
header += '
|
|
215
|
+
header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
|
|
216
216
|
if (onComment)
|
|
217
217
|
onComment();
|
|
218
218
|
}
|
|
@@ -229,7 +229,6 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
|
|
|
229
229
|
return `${header}\n${indent}${body}`;
|
|
230
230
|
}
|
|
231
231
|
function plainString(item, ctx, onComment, onChompKeep) {
|
|
232
|
-
var _a;
|
|
233
232
|
const { type, value } = item;
|
|
234
233
|
const { actualString, implicitKey, indent, inFlow } = ctx;
|
|
235
234
|
if ((implicitKey && /[\n[\]{},]/.test(value)) ||
|
|
@@ -244,14 +243,14 @@ function plainString(item, ctx, onComment, onChompKeep) {
|
|
|
244
243
|
// - '\n ', ': ' or ' \n' anywhere
|
|
245
244
|
// - '#' not preceded by a non-space char
|
|
246
245
|
// - end with ' ' or ':'
|
|
247
|
-
return implicitKey || inFlow || value.
|
|
246
|
+
return implicitKey || inFlow || !value.includes('\n')
|
|
248
247
|
? quotedString(value, ctx)
|
|
249
248
|
: blockString(item, ctx, onComment, onChompKeep);
|
|
250
249
|
}
|
|
251
250
|
if (!implicitKey &&
|
|
252
251
|
!inFlow &&
|
|
253
252
|
type !== Scalar.PLAIN &&
|
|
254
|
-
value.
|
|
253
|
+
value.includes('\n')) {
|
|
255
254
|
// Where allowed & type not set explicitly, prefer block style for multiline strings
|
|
256
255
|
return blockString(item, ctx, onComment, onChompKeep);
|
|
257
256
|
}
|
|
@@ -264,12 +263,10 @@ function plainString(item, ctx, onComment, onChompKeep) {
|
|
|
264
263
|
// booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
|
|
265
264
|
// and others in v1.1.
|
|
266
265
|
if (actualString) {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
return quotedString(value, ctx);
|
|
272
|
-
}
|
|
266
|
+
const test = (tag) => tag.default && tag.tag !== 'tag:yaml.org,2002:str' && tag.test?.test(str);
|
|
267
|
+
const { compat, tags } = ctx.doc.schema;
|
|
268
|
+
if (tags.some(test) || compat?.some(test))
|
|
269
|
+
return quotedString(value, ctx);
|
|
273
270
|
}
|
|
274
271
|
return implicitKey
|
|
275
272
|
? str
|