yaml 2.0.0-7 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/browser/dist/compose/compose-doc.js +3 -2
- package/browser/dist/compose/compose-node.js +14 -3
- package/browser/dist/compose/compose-scalar.js +17 -10
- package/browser/dist/compose/composer.js +6 -7
- package/browser/dist/compose/resolve-block-map.js +19 -5
- package/browser/dist/compose/resolve-block-scalar.js +12 -3
- package/browser/dist/compose/resolve-block-seq.js +5 -0
- package/browser/dist/compose/resolve-flow-collection.js +18 -6
- package/browser/dist/compose/resolve-flow-scalar.js +9 -2
- package/browser/dist/compose/resolve-props.js +4 -0
- package/browser/dist/compose/util-flow-indent-check.js +15 -0
- package/browser/dist/doc/Document.js +73 -16
- package/browser/dist/doc/anchors.js +2 -2
- package/browser/dist/doc/createNode.js +12 -7
- package/browser/dist/doc/directives.js +11 -3
- package/browser/dist/index.js +1 -2
- package/browser/dist/node_modules/tslib/tslib.es6.js +164 -0
- package/browser/dist/nodes/Collection.js +28 -15
- package/browser/dist/nodes/Node.js +7 -0
- package/browser/dist/nodes/Pair.js +11 -3
- package/browser/dist/nodes/Scalar.js +1 -1
- package/browser/dist/nodes/YAMLMap.js +6 -5
- package/browser/dist/nodes/YAMLSeq.js +2 -2
- package/browser/dist/nodes/addPairToJSMap.js +3 -2
- package/browser/dist/nodes/toJS.js +3 -1
- package/browser/dist/parse/cst-scalar.js +2 -6
- package/browser/dist/parse/cst-visit.js +2 -2
- package/browser/dist/parse/lexer.js +50 -22
- package/browser/dist/parse/parser.js +121 -45
- package/browser/dist/public-api.js +4 -5
- package/browser/dist/schema/Schema.js +19 -4
- package/browser/dist/schema/common/null.js +3 -1
- package/browser/dist/schema/core/float.js +4 -1
- package/browser/dist/schema/tags.js +17 -13
- package/browser/dist/schema/yaml-1.1/float.js +4 -1
- package/browser/dist/schema/yaml-1.1/omap.js +1 -1
- package/browser/dist/schema/yaml-1.1/pairs.js +2 -1
- package/browser/dist/stringify/stringify.js +53 -23
- package/browser/dist/stringify/stringifyCollection.js +115 -85
- package/browser/dist/stringify/stringifyComment.js +18 -12
- package/browser/dist/stringify/stringifyDocument.js +37 -15
- package/browser/dist/stringify/stringifyPair.js +35 -21
- package/browser/dist/stringify/stringifyString.js +41 -43
- package/browser/dist/util.js +3 -0
- package/browser/dist/visit.js +150 -62
- package/dist/compose/compose-doc.js +3 -2
- package/dist/compose/compose-node.d.ts +1 -0
- package/dist/compose/compose-node.js +14 -3
- package/dist/compose/compose-scalar.js +16 -9
- package/dist/compose/composer.d.ts +1 -1
- package/dist/compose/composer.js +8 -9
- package/dist/compose/resolve-block-map.js +19 -5
- package/dist/compose/resolve-block-scalar.js +12 -3
- package/dist/compose/resolve-block-seq.js +5 -0
- package/dist/compose/resolve-flow-collection.js +18 -6
- package/dist/compose/resolve-flow-scalar.js +9 -2
- package/dist/compose/resolve-props.d.ts +1 -1
- package/dist/compose/resolve-props.js +4 -0
- package/dist/compose/util-flow-indent-check.d.ts +3 -0
- package/dist/compose/util-flow-indent-check.js +17 -0
- package/dist/doc/Document.d.ts +13 -4
- package/dist/doc/Document.js +79 -22
- package/dist/doc/anchors.d.ts +2 -2
- package/dist/doc/anchors.js +2 -2
- package/dist/doc/createNode.d.ts +3 -2
- package/dist/doc/createNode.js +11 -6
- package/dist/doc/directives.d.ts +5 -2
- package/dist/doc/directives.js +11 -3
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -2
- package/dist/node_modules/tslib/tslib.es6.js +76 -0
- package/dist/nodes/Alias.d.ts +7 -3
- package/dist/nodes/Collection.d.ts +10 -4
- package/dist/nodes/Collection.js +27 -14
- package/dist/nodes/Node.d.ts +6 -1
- package/dist/nodes/Node.js +7 -0
- package/dist/nodes/Pair.d.ts +7 -2
- package/dist/nodes/Pair.js +10 -2
- package/dist/nodes/Scalar.d.ts +2 -0
- package/dist/nodes/Scalar.js +1 -1
- package/dist/nodes/YAMLMap.d.ts +5 -1
- package/dist/nodes/YAMLMap.js +6 -5
- package/dist/nodes/YAMLSeq.d.ts +2 -0
- package/dist/nodes/YAMLSeq.js +2 -2
- package/dist/nodes/addPairToJSMap.js +2 -1
- package/dist/nodes/toJS.js +3 -1
- package/dist/options.d.ts +67 -15
- package/dist/parse/cst-scalar.d.ts +6 -0
- package/dist/parse/cst-scalar.js +2 -6
- package/dist/parse/cst-visit.js +2 -2
- package/dist/parse/lexer.d.ts +2 -0
- package/dist/parse/lexer.js +50 -22
- package/dist/parse/parser.js +121 -45
- package/dist/public-api.js +4 -5
- package/dist/schema/Schema.d.ts +6 -4
- package/dist/schema/Schema.js +18 -3
- package/dist/schema/common/null.js +3 -1
- package/dist/schema/core/float.js +4 -1
- package/dist/schema/tags.d.ts +1 -2
- package/dist/schema/tags.js +17 -13
- package/dist/schema/yaml-1.1/float.js +4 -1
- package/dist/schema/yaml-1.1/omap.js +1 -1
- package/dist/schema/yaml-1.1/pairs.js +2 -1
- package/dist/stringify/stringify.d.ts +5 -3
- package/dist/stringify/stringify.js +53 -23
- package/dist/stringify/stringifyCollection.d.ts +2 -6
- package/dist/stringify/stringifyCollection.js +114 -84
- package/dist/stringify/stringifyComment.d.ts +10 -2
- package/dist/stringify/stringifyComment.js +19 -12
- package/dist/stringify/stringifyDocument.js +36 -14
- package/dist/stringify/stringifyPair.js +34 -20
- package/dist/stringify/stringifyString.js +41 -43
- package/dist/test-events.d.ts +1 -1
- package/dist/test-events.js +13 -12
- package/dist/util.d.ts +3 -0
- package/dist/util.js +6 -0
- package/dist/visit.d.ts +49 -10
- package/dist/visit.js +149 -60
- package/package.json +12 -10
- package/util.d.ts +3 -0
- package/browser/dist/options.js +0 -17
- package/dist/options.js +0 -19
|
@@ -1,13 +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
|
-
|
|
8
|
-
|
|
9
|
-
indentStep: typeof options.indent === 'number' ? ' '.repeat(options.indent) : ' ',
|
|
10
|
-
options: Object.assign({
|
|
6
|
+
function createStringifyContext(doc, options) {
|
|
7
|
+
const opt = Object.assign({
|
|
8
|
+
blockQuote: true,
|
|
9
|
+
commentString: stringifyComment,
|
|
11
10
|
defaultKeyType: null,
|
|
12
11
|
defaultStringType: 'PLAIN',
|
|
13
12
|
directives: null,
|
|
@@ -19,57 +18,88 @@ const createStringifyContext = (doc, options) => ({
|
|
|
19
18
|
minContentWidth: 20,
|
|
20
19
|
nullStr: 'null',
|
|
21
20
|
simpleKeys: false,
|
|
22
|
-
singleQuote:
|
|
21
|
+
singleQuote: null,
|
|
23
22
|
trueStr: 'true',
|
|
24
23
|
verifyAliasOrder: true
|
|
25
|
-
}, options)
|
|
26
|
-
|
|
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
|
+
}
|
|
27
45
|
function getTagObject(tags, item) {
|
|
46
|
+
var _a, _b, _c, _d;
|
|
28
47
|
if (item.tag) {
|
|
29
48
|
const match = tags.filter(t => t.tag === item.tag);
|
|
30
49
|
if (match.length > 0)
|
|
31
|
-
return match.find(t => t.format === item.format)
|
|
50
|
+
return (_a = match.find(t => t.format === item.format)) !== null && _a !== void 0 ? _a : match[0];
|
|
32
51
|
}
|
|
33
52
|
let tagObj = undefined;
|
|
34
53
|
let obj;
|
|
35
54
|
if (isScalar(item)) {
|
|
36
55
|
obj = item.value;
|
|
37
|
-
const match = tags.filter(t => t.identify
|
|
56
|
+
const match = tags.filter(t => { var _a; return (_a = t.identify) === null || _a === void 0 ? void 0 : _a.call(t, obj); });
|
|
38
57
|
tagObj =
|
|
39
|
-
match.find(t => t.format === item.format)
|
|
58
|
+
(_b = match.find(t => t.format === item.format)) !== null && _b !== void 0 ? _b : match.find(t => !t.format);
|
|
40
59
|
}
|
|
41
60
|
else {
|
|
42
61
|
obj = item;
|
|
43
62
|
tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
|
|
44
63
|
}
|
|
45
64
|
if (!tagObj) {
|
|
46
|
-
|
|
47
|
-
const name = obj && obj.constructor ? obj.constructor.name : typeof obj;
|
|
65
|
+
const name = (_d = (_c = obj === null || obj === void 0 ? void 0 : obj.constructor) === null || _c === void 0 ? void 0 : _c.name) !== null && _d !== void 0 ? _d : typeof obj;
|
|
48
66
|
throw new Error(`Tag not resolved for ${name} value`);
|
|
49
67
|
}
|
|
50
68
|
return tagObj;
|
|
51
69
|
}
|
|
52
70
|
// needs to be called before value stringifier to allow for circular anchor refs
|
|
53
71
|
function stringifyProps(node, tagObj, { anchors, doc }) {
|
|
72
|
+
if (!doc.directives)
|
|
73
|
+
return '';
|
|
54
74
|
const props = [];
|
|
55
75
|
const anchor = (isScalar(node) || isCollection(node)) && node.anchor;
|
|
56
76
|
if (anchor && anchorIsValid(anchor)) {
|
|
57
77
|
anchors.add(anchor);
|
|
58
78
|
props.push(`&${anchor}`);
|
|
59
79
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
else if (!tagObj.default) {
|
|
64
|
-
props.push(doc.directives.tagString(tagObj.tag));
|
|
65
|
-
}
|
|
80
|
+
const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
|
|
81
|
+
if (tag)
|
|
82
|
+
props.push(doc.directives.tagString(tag));
|
|
66
83
|
return props.join(' ');
|
|
67
84
|
}
|
|
68
85
|
function stringify(item, ctx, onComment, onChompKeep) {
|
|
86
|
+
var _a, _b;
|
|
69
87
|
if (isPair(item))
|
|
70
88
|
return item.toString(ctx, onComment, onChompKeep);
|
|
71
|
-
if (isAlias(item))
|
|
72
|
-
|
|
89
|
+
if (isAlias(item)) {
|
|
90
|
+
if (ctx.doc.directives)
|
|
91
|
+
return item.toString(ctx);
|
|
92
|
+
if ((_a = ctx.resolvedAliases) === null || _a === void 0 ? void 0 : _a.has(item)) {
|
|
93
|
+
throw new TypeError(`Cannot stringify circular structure without alias nodes`);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
if (ctx.resolvedAliases)
|
|
97
|
+
ctx.resolvedAliases.add(item);
|
|
98
|
+
else
|
|
99
|
+
ctx.resolvedAliases = new Set([item]);
|
|
100
|
+
item = item.resolve(ctx.doc);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
73
103
|
let tagObj = undefined;
|
|
74
104
|
const node = isNode(item)
|
|
75
105
|
? item
|
|
@@ -78,7 +108,7 @@ function stringify(item, ctx, onComment, onChompKeep) {
|
|
|
78
108
|
tagObj = getTagObject(ctx.doc.schema.tags, node);
|
|
79
109
|
const props = stringifyProps(node, tagObj, ctx);
|
|
80
110
|
if (props.length > 0)
|
|
81
|
-
ctx.indentAtStart = (ctx.indentAtStart
|
|
111
|
+
ctx.indentAtStart = ((_b = ctx.indentAtStart) !== null && _b !== void 0 ? _b : 0) + props.length + 1;
|
|
82
112
|
const str = typeof tagObj.stringify === 'function'
|
|
83
113
|
? tagObj.stringify(node, ctx, onComment, onChompKeep)
|
|
84
114
|
: isScalar(node)
|
|
@@ -1,122 +1,152 @@
|
|
|
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
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
function stringifyCollection(collection, ctx, options) {
|
|
7
|
+
var _a;
|
|
8
|
+
const flow = (_a = ctx.inFlow) !== null && _a !== void 0 ? _a : collection.flow;
|
|
9
|
+
const stringify = flow ? stringifyFlowCollection : stringifyBlockCollection;
|
|
10
|
+
return stringify(collection, ctx, options);
|
|
11
|
+
}
|
|
12
|
+
function stringifyBlockCollection({ comment, items }, ctx, { blockItemPrefix, flowChars, itemIndent, onChompKeep, onComment }) {
|
|
13
|
+
const { indent, options: { commentString } } = ctx;
|
|
14
|
+
const itemCtx = Object.assign({}, ctx, { indent: itemIndent, type: null });
|
|
13
15
|
let chompKeep = false; // flag for the preceding node's status
|
|
14
|
-
const
|
|
16
|
+
const lines = [];
|
|
17
|
+
for (let i = 0; i < items.length; ++i) {
|
|
18
|
+
const item = items[i];
|
|
15
19
|
let comment = null;
|
|
16
20
|
if (isNode(item)) {
|
|
17
21
|
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) {
|
|
22
|
+
lines.push('');
|
|
23
|
+
addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
|
|
24
|
+
if (item.comment)
|
|
32
25
|
comment = item.comment;
|
|
33
|
-
singleLineOutput = false;
|
|
34
|
-
}
|
|
35
26
|
}
|
|
36
27
|
else if (isPair(item)) {
|
|
37
28
|
const ik = isNode(item.key) ? item.key : null;
|
|
38
29
|
if (ik) {
|
|
39
30
|
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
|
-
}
|
|
31
|
+
lines.push('');
|
|
32
|
+
addCommentBefore(ctx, lines, ik.commentBefore, chompKeep);
|
|
67
33
|
}
|
|
68
34
|
}
|
|
69
35
|
chompKeep = false;
|
|
70
|
-
let str = stringify(item,
|
|
71
|
-
if (
|
|
72
|
-
str +=
|
|
73
|
-
|
|
74
|
-
if (chompKeep && (comment || inFlow))
|
|
36
|
+
let str = stringify(item, itemCtx, () => (comment = null), () => (chompKeep = true));
|
|
37
|
+
if (comment)
|
|
38
|
+
str += lineComment(str, itemIndent, commentString(comment));
|
|
39
|
+
if (chompKeep && comment)
|
|
75
40
|
chompKeep = false;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}, []);
|
|
41
|
+
lines.push(blockItemPrefix + str);
|
|
42
|
+
}
|
|
79
43
|
let str;
|
|
80
|
-
if (
|
|
44
|
+
if (lines.length === 0) {
|
|
81
45
|
str = flowChars.start + flowChars.end;
|
|
82
46
|
}
|
|
83
|
-
else
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
47
|
+
else {
|
|
48
|
+
str = lines[0];
|
|
49
|
+
for (let i = 1; i < lines.length; ++i) {
|
|
50
|
+
const line = lines[i];
|
|
51
|
+
str += line ? `\n${indent}${line}` : '\n';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (comment) {
|
|
55
|
+
str += '\n' + indentComment(commentString(comment), indent);
|
|
56
|
+
if (onComment)
|
|
57
|
+
onComment();
|
|
58
|
+
}
|
|
59
|
+
else if (chompKeep && onChompKeep)
|
|
60
|
+
onChompKeep();
|
|
61
|
+
return str;
|
|
62
|
+
}
|
|
63
|
+
function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemIndent, onComment }) {
|
|
64
|
+
const { indent, indentStep, options: { commentString } } = ctx;
|
|
65
|
+
itemIndent += indentStep;
|
|
66
|
+
const itemCtx = Object.assign({}, ctx, {
|
|
67
|
+
indent: itemIndent,
|
|
68
|
+
inFlow: true,
|
|
69
|
+
type: null
|
|
70
|
+
});
|
|
71
|
+
let reqNewline = false;
|
|
72
|
+
let linesAtValue = 0;
|
|
73
|
+
const lines = [];
|
|
74
|
+
for (let i = 0; i < items.length; ++i) {
|
|
75
|
+
const item = items[i];
|
|
76
|
+
let comment = null;
|
|
77
|
+
if (isNode(item)) {
|
|
78
|
+
if (item.spaceBefore)
|
|
79
|
+
lines.push('');
|
|
80
|
+
addCommentBefore(ctx, lines, item.commentBefore, false);
|
|
81
|
+
if (item.comment)
|
|
82
|
+
comment = item.comment;
|
|
83
|
+
}
|
|
84
|
+
else if (isPair(item)) {
|
|
85
|
+
const ik = isNode(item.key) ? item.key : null;
|
|
86
|
+
if (ik) {
|
|
87
|
+
if (ik.spaceBefore)
|
|
88
|
+
lines.push('');
|
|
89
|
+
addCommentBefore(ctx, lines, ik.commentBefore, false);
|
|
90
|
+
if (ik.comment)
|
|
91
|
+
reqNewline = true;
|
|
92
|
+
}
|
|
93
|
+
const iv = isNode(item.value) ? item.value : null;
|
|
94
|
+
if (iv) {
|
|
95
|
+
if (iv.comment)
|
|
96
|
+
comment = iv.comment;
|
|
97
|
+
if (iv.commentBefore)
|
|
98
|
+
reqNewline = true;
|
|
99
|
+
}
|
|
100
|
+
else if (item.value == null && ik && ik.comment) {
|
|
101
|
+
comment = ik.comment;
|
|
91
102
|
}
|
|
92
|
-
singleLineLength += node.str.length + 2;
|
|
93
103
|
}
|
|
94
|
-
if (
|
|
95
|
-
|
|
104
|
+
if (comment)
|
|
105
|
+
reqNewline = true;
|
|
106
|
+
let str = stringify(item, itemCtx, () => (comment = null));
|
|
107
|
+
if (i < items.length - 1)
|
|
108
|
+
str += ',';
|
|
109
|
+
if (comment)
|
|
110
|
+
str += lineComment(str, itemIndent, commentString(comment));
|
|
111
|
+
if (!reqNewline && (lines.length > linesAtValue || str.includes('\n')))
|
|
112
|
+
reqNewline = true;
|
|
113
|
+
lines.push(str);
|
|
114
|
+
linesAtValue = lines.length;
|
|
115
|
+
}
|
|
116
|
+
let str;
|
|
117
|
+
const { start, end } = flowChars;
|
|
118
|
+
if (lines.length === 0) {
|
|
119
|
+
str = start + end;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
if (!reqNewline) {
|
|
123
|
+
const len = lines.reduce((sum, line) => sum + line.length + 2, 2);
|
|
124
|
+
reqNewline = len > Collection.maxFlowStringSingleLineLength;
|
|
125
|
+
}
|
|
126
|
+
if (reqNewline) {
|
|
96
127
|
str = start;
|
|
97
|
-
for (const
|
|
98
|
-
str +=
|
|
99
|
-
}
|
|
128
|
+
for (const line of lines)
|
|
129
|
+
str += line ? `\n${indentStep}${indent}${line}` : '\n';
|
|
100
130
|
str += `\n${indent}${end}`;
|
|
101
131
|
}
|
|
102
132
|
else {
|
|
103
|
-
str = `${start} ${
|
|
133
|
+
str = `${start} ${lines.join(' ')} ${end}`;
|
|
104
134
|
}
|
|
105
135
|
}
|
|
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
136
|
if (comment) {
|
|
113
|
-
str +=
|
|
137
|
+
str += lineComment(str, commentString(comment), indent);
|
|
114
138
|
if (onComment)
|
|
115
139
|
onComment();
|
|
116
140
|
}
|
|
117
|
-
else if (chompKeep && onChompKeep)
|
|
118
|
-
onChompKeep();
|
|
119
141
|
return str;
|
|
120
142
|
}
|
|
143
|
+
function addCommentBefore({ indent, options: { commentString } }, lines, comment, chompKeep) {
|
|
144
|
+
if (comment && chompKeep)
|
|
145
|
+
comment = comment.replace(/^\n+/, '');
|
|
146
|
+
if (comment) {
|
|
147
|
+
const ic = indentComment(commentString(comment), indent);
|
|
148
|
+
lines.push(ic.trimStart()); // Avoid double indent on first line
|
|
149
|
+
}
|
|
150
|
+
}
|
|
121
151
|
|
|
122
152
|
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,40 @@
|
|
|
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
|
+
var _a;
|
|
6
7
|
const lines = [];
|
|
7
8
|
let hasDirectives = options.directives === true;
|
|
8
|
-
if (options.directives !== false) {
|
|
9
|
+
if (options.directives !== false && doc.directives) {
|
|
9
10
|
const dir = doc.directives.toString(doc);
|
|
10
11
|
if (dir) {
|
|
11
12
|
lines.push(dir);
|
|
12
13
|
hasDirectives = true;
|
|
13
14
|
}
|
|
14
|
-
else if (doc.directives.
|
|
15
|
+
else if (doc.directives.docStart)
|
|
15
16
|
hasDirectives = true;
|
|
16
17
|
}
|
|
17
18
|
if (hasDirectives)
|
|
18
19
|
lines.push('---');
|
|
20
|
+
const ctx = createStringifyContext(doc, options);
|
|
21
|
+
const { commentString } = ctx.options;
|
|
19
22
|
if (doc.commentBefore) {
|
|
20
23
|
if (lines.length !== 1)
|
|
21
24
|
lines.unshift('');
|
|
22
|
-
|
|
25
|
+
const cs = commentString(doc.commentBefore);
|
|
26
|
+
lines.unshift(indentComment(cs, ''));
|
|
23
27
|
}
|
|
24
|
-
const ctx = createStringifyContext(doc, options);
|
|
25
28
|
let chompKeep = false;
|
|
26
29
|
let contentComment = null;
|
|
27
30
|
if (doc.contents) {
|
|
28
31
|
if (isNode(doc.contents)) {
|
|
29
32
|
if (doc.contents.spaceBefore && hasDirectives)
|
|
30
33
|
lines.push('');
|
|
31
|
-
if (doc.contents.commentBefore)
|
|
32
|
-
|
|
34
|
+
if (doc.contents.commentBefore) {
|
|
35
|
+
const cs = commentString(doc.contents.commentBefore);
|
|
36
|
+
lines.push(indentComment(cs, ''));
|
|
37
|
+
}
|
|
33
38
|
// top-level block scalars need to be indented if followed by a comment
|
|
34
39
|
ctx.forceBlockIndent = !!doc.comment;
|
|
35
40
|
contentComment = doc.contents.comment;
|
|
@@ -37,7 +42,7 @@ function stringifyDocument(doc, options) {
|
|
|
37
42
|
const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
|
|
38
43
|
let body = stringify(doc.contents, ctx, () => (contentComment = null), onChompKeep);
|
|
39
44
|
if (contentComment)
|
|
40
|
-
body
|
|
45
|
+
body += lineComment(body, '', commentString(contentComment));
|
|
41
46
|
if ((body[0] === '|' || body[0] === '>') &&
|
|
42
47
|
lines[lines.length - 1] === '---') {
|
|
43
48
|
// Top-level block scalars with a preceding doc marker ought to use the
|
|
@@ -50,13 +55,30 @@ function stringifyDocument(doc, options) {
|
|
|
50
55
|
else {
|
|
51
56
|
lines.push(stringify(doc.contents, ctx));
|
|
52
57
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
if ((_a = doc.directives) === null || _a === void 0 ? void 0 : _a.docEnd) {
|
|
59
|
+
if (doc.comment) {
|
|
60
|
+
const cs = commentString(doc.comment);
|
|
61
|
+
if (cs.includes('\n')) {
|
|
62
|
+
lines.push('...');
|
|
63
|
+
lines.push(indentComment(cs, ''));
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
lines.push(`... ${cs}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
lines.push('...');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
let dc = doc.comment;
|
|
75
|
+
if (dc && chompKeep)
|
|
76
|
+
dc = dc.replace(/^\n+/, '');
|
|
77
|
+
if (dc) {
|
|
78
|
+
if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
|
|
79
|
+
lines.push('');
|
|
80
|
+
lines.push(indentComment(commentString(dc), ''));
|
|
81
|
+
}
|
|
60
82
|
}
|
|
61
83
|
return lines.join('\n') + '\n';
|
|
62
84
|
}
|
|
@@ -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,27 +96,30 @@ 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] === '{';
|
|
92
106
|
if (!flow || valueStr.includes('\n'))
|
|
93
107
|
ws = `\n${ctx.indent}`;
|
|
94
108
|
}
|
|
95
|
-
else if (valueStr[0] === '\n')
|
|
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 };
|