yaml 1.9.2 → 1.10.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 +4 -4
- package/browser/dist/PlainValue-ff5147c6.js +1277 -0
- package/browser/dist/Schema-2bf2c74e.js +679 -0
- package/browser/dist/index.js +869 -34
- package/browser/dist/legacy-exports.js +3 -0
- package/browser/dist/parse-cst.js +1904 -0
- package/browser/dist/resolveSeq-04825f30.js +2373 -0
- package/browser/dist/types.js +4 -0
- package/browser/dist/util.js +2 -0
- package/browser/dist/warnings-0e4b70d3.js +499 -0
- package/browser/index.js +1 -1
- package/browser/map.js +2 -4
- package/browser/pair.js +2 -4
- package/browser/parse-cst.js +1 -1
- package/browser/scalar.js +2 -4
- package/browser/schema.js +7 -10
- package/browser/seq.js +2 -4
- package/browser/types/binary.js +7 -5
- package/browser/types/omap.js +3 -4
- package/browser/types/pairs.js +3 -4
- package/browser/types/set.js +3 -4
- package/browser/types/timestamp.js +9 -5
- package/browser/types.js +1 -13
- package/browser/util.js +1 -18
- package/dist/Document-2cf6b08c.js +757 -0
- package/dist/PlainValue-ec8e588e.js +876 -0
- package/dist/Schema-42e9705c.js +522 -0
- package/dist/index.js +20 -84
- package/dist/legacy-exports.js +16 -0
- package/dist/parse-cst.js +1747 -0
- package/dist/resolveSeq-4a68b39b.js +2115 -0
- package/dist/test-events.js +11 -9
- package/dist/types.js +23 -0
- package/dist/util.js +19 -0
- package/dist/warnings-39684f17.js +416 -0
- package/index.d.ts +22 -8
- package/map.js +2 -2
- package/package.json +22 -39
- package/pair.js +2 -2
- package/parse-cst.js +1 -1
- package/scalar.js +2 -2
- package/schema.js +8 -6
- package/seq.js +2 -2
- package/types/binary.js +3 -2
- package/types/omap.js +3 -2
- package/types/pairs.js +3 -2
- package/types/set.js +3 -2
- package/types/timestamp.js +6 -6
- package/types.d.ts +113 -99
- package/types.js +16 -11
- package/types.mjs +15 -19
- package/util.js +14 -14
- package/util.mjs +13 -20
- package/browser/dist/Anchors.js +0 -130
- package/browser/dist/Document.js +0 -824
- package/browser/dist/addComment.js +0 -8
- package/browser/dist/constants.js +0 -26
- package/browser/dist/cst/Alias.js +0 -46
- package/browser/dist/cst/BlankLine.js +0 -52
- package/browser/dist/cst/BlockValue.js +0 -251
- package/browser/dist/cst/Collection.js +0 -278
- package/browser/dist/cst/CollectionItem.js +0 -140
- package/browser/dist/cst/Comment.js +0 -44
- package/browser/dist/cst/Directive.js +0 -76
- package/browser/dist/cst/Document.js +0 -286
- package/browser/dist/cst/FlowCollection.js +0 -220
- package/browser/dist/cst/Node.js +0 -386
- package/browser/dist/cst/ParseContext.js +0 -246
- package/browser/dist/cst/PlainValue.js +0 -170
- package/browser/dist/cst/QuoteDouble.js +0 -245
- package/browser/dist/cst/QuoteSingle.js +0 -120
- package/browser/dist/cst/Range.js +0 -64
- package/browser/dist/cst/parse.js +0 -48
- package/browser/dist/cst/source-utils.js +0 -168
- package/browser/dist/errors.js +0 -127
- package/browser/dist/foldFlowLines.js +0 -138
- package/browser/dist/listTagNames.js +0 -28
- package/browser/dist/schema/Alias.js +0 -135
- package/browser/dist/schema/Collection.js +0 -260
- package/browser/dist/schema/Map.js +0 -161
- package/browser/dist/schema/Merge.js +0 -118
- package/browser/dist/schema/Node.js +0 -4
- package/browser/dist/schema/Pair.js +0 -194
- package/browser/dist/schema/Scalar.js +0 -42
- package/browser/dist/schema/Seq.js +0 -116
- package/browser/dist/schema/index.js +0 -368
- package/browser/dist/schema/parseMap.js +0 -312
- package/browser/dist/schema/parseSeq.js +0 -168
- package/browser/dist/schema/parseUtils.js +0 -106
- package/browser/dist/stringify.js +0 -332
- package/browser/dist/tags/core.js +0 -141
- package/browser/dist/tags/failsafe/index.js +0 -4
- package/browser/dist/tags/failsafe/map.js +0 -53
- package/browser/dist/tags/failsafe/seq.js +0 -39
- package/browser/dist/tags/failsafe/string.js +0 -28
- package/browser/dist/tags/index.js +0 -36
- package/browser/dist/tags/json.js +0 -77
- package/browser/dist/tags/options.js +0 -26
- package/browser/dist/tags/yaml-1.1/binary.js +0 -87
- package/browser/dist/tags/yaml-1.1/index.js +0 -189
- package/browser/dist/tags/yaml-1.1/omap.js +0 -137
- package/browser/dist/tags/yaml-1.1/pairs.js +0 -78
- package/browser/dist/tags/yaml-1.1/set.js +0 -118
- package/browser/dist/tags/yaml-1.1/timestamp.js +0 -97
- package/browser/dist/test-events.js +0 -163
- package/browser/dist/toJSON.js +0 -19
- package/browser/dist/warnings.js +0 -39
- package/dist/Anchors.js +0 -120
- package/dist/Document.js +0 -714
- package/dist/addComment.js +0 -14
- package/dist/constants.js +0 -31
- package/dist/cst/Alias.js +0 -33
- package/dist/cst/BlankLine.js +0 -40
- package/dist/cst/BlockValue.js +0 -243
- package/dist/cst/Collection.js +0 -266
- package/dist/cst/CollectionItem.js +0 -135
- package/dist/cst/Comment.js +0 -33
- package/dist/cst/Directive.js +0 -59
- package/dist/cst/Document.js +0 -276
- package/dist/cst/FlowCollection.js +0 -213
- package/dist/cst/Node.js +0 -360
- package/dist/cst/ParseContext.js +0 -253
- package/dist/cst/PlainValue.js +0 -156
- package/dist/cst/QuoteDouble.js +0 -232
- package/dist/cst/QuoteSingle.js +0 -108
- package/dist/cst/Range.js +0 -60
- package/dist/cst/parse.js +0 -50
- package/dist/cst/source-utils.js +0 -175
- package/dist/errors.js +0 -97
- package/dist/foldFlowLines.js +0 -143
- package/dist/listTagNames.js +0 -33
- package/dist/schema/Alias.js +0 -105
- package/dist/schema/Collection.js +0 -184
- package/dist/schema/Map.js +0 -98
- package/dist/schema/Merge.js +0 -75
- package/dist/schema/Node.js +0 -7
- package/dist/schema/Pair.js +0 -174
- package/dist/schema/Scalar.js +0 -26
- package/dist/schema/Seq.js +0 -73
- package/dist/schema/index.js +0 -310
- package/dist/schema/parseMap.js +0 -334
- package/dist/schema/parseSeq.js +0 -175
- package/dist/schema/parseUtils.js +0 -101
- package/dist/stringify.js +0 -352
- package/dist/tags/core.js +0 -121
- package/dist/tags/failsafe/index.js +0 -12
- package/dist/tags/failsafe/map.js +0 -32
- package/dist/tags/failsafe/seq.js +0 -29
- package/dist/tags/failsafe/string.js +0 -37
- package/dist/tags/index.js +0 -53
- package/dist/tags/json.js +0 -64
- package/dist/tags/options.js +0 -36
- package/dist/tags/yaml-1.1/binary.js +0 -94
- package/dist/tags/yaml-1.1/index.js +0 -162
- package/dist/tags/yaml-1.1/omap.js +0 -100
- package/dist/tags/yaml-1.1/pairs.js +0 -75
- package/dist/tags/yaml-1.1/set.js +0 -82
- package/dist/tags/yaml-1.1/timestamp.js +0 -90
- package/dist/toJSON.js +0 -21
- package/dist/warnings.js +0 -48
package/dist/schema/parseSeq.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.parseSeq = parseSeq;
|
|
4
|
-
|
|
5
|
-
var _constants = require("../constants");
|
|
6
|
-
|
|
7
|
-
var _errors = require("../errors");
|
|
8
|
-
|
|
9
|
-
var _Pair = require("./Pair");
|
|
10
|
-
|
|
11
|
-
var _parseUtils = require("./parseUtils");
|
|
12
|
-
|
|
13
|
-
var _Seq = require("./Seq");
|
|
14
|
-
|
|
15
|
-
var _Collection = require("./Collection");
|
|
16
|
-
|
|
17
|
-
function parseSeq(doc, cst) {
|
|
18
|
-
if (cst.type !== _constants.Type.SEQ && cst.type !== _constants.Type.FLOW_SEQ) {
|
|
19
|
-
const msg = `A ${cst.type} node cannot be resolved as a sequence`;
|
|
20
|
-
doc.errors.push(new _errors.YAMLSyntaxError(cst, msg));
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const {
|
|
25
|
-
comments,
|
|
26
|
-
items
|
|
27
|
-
} = cst.type === _constants.Type.FLOW_SEQ ? resolveFlowSeqItems(doc, cst) : resolveBlockSeqItems(doc, cst);
|
|
28
|
-
const seq = new _Seq.YAMLSeq();
|
|
29
|
-
seq.items = items;
|
|
30
|
-
(0, _parseUtils.resolveComments)(seq, comments);
|
|
31
|
-
|
|
32
|
-
if (!doc.options.mapAsMap && items.some(it => it instanceof _Pair.Pair && it.key instanceof _Collection.Collection)) {
|
|
33
|
-
const warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
|
|
34
|
-
doc.warnings.push(new _errors.YAMLWarning(cst, warn));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
cst.resolved = seq;
|
|
38
|
-
return seq;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function resolveBlockSeqItems(doc, cst) {
|
|
42
|
-
const comments = [];
|
|
43
|
-
const items = [];
|
|
44
|
-
|
|
45
|
-
for (let i = 0; i < cst.items.length; ++i) {
|
|
46
|
-
const item = cst.items[i];
|
|
47
|
-
|
|
48
|
-
switch (item.type) {
|
|
49
|
-
case _constants.Type.BLANK_LINE:
|
|
50
|
-
comments.push({
|
|
51
|
-
before: items.length
|
|
52
|
-
});
|
|
53
|
-
break;
|
|
54
|
-
|
|
55
|
-
case _constants.Type.COMMENT:
|
|
56
|
-
comments.push({
|
|
57
|
-
comment: item.comment,
|
|
58
|
-
before: items.length
|
|
59
|
-
});
|
|
60
|
-
break;
|
|
61
|
-
|
|
62
|
-
case _constants.Type.SEQ_ITEM:
|
|
63
|
-
if (item.error) doc.errors.push(item.error);
|
|
64
|
-
items.push(doc.resolveNode(item.node));
|
|
65
|
-
|
|
66
|
-
if (item.hasProps) {
|
|
67
|
-
const msg = 'Sequence items cannot have tags or anchors before the - indicator';
|
|
68
|
-
doc.errors.push(new _errors.YAMLSemanticError(item, msg));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
break;
|
|
72
|
-
|
|
73
|
-
default:
|
|
74
|
-
if (item.error) doc.errors.push(item.error);
|
|
75
|
-
doc.errors.push(new _errors.YAMLSyntaxError(item, `Unexpected ${item.type} node in sequence`));
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
comments,
|
|
81
|
-
items
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function resolveFlowSeqItems(doc, cst) {
|
|
86
|
-
const comments = [];
|
|
87
|
-
const items = [];
|
|
88
|
-
let explicitKey = false;
|
|
89
|
-
let key = undefined;
|
|
90
|
-
let keyStart = null;
|
|
91
|
-
let next = '[';
|
|
92
|
-
|
|
93
|
-
for (let i = 0; i < cst.items.length; ++i) {
|
|
94
|
-
const item = cst.items[i];
|
|
95
|
-
|
|
96
|
-
if (typeof item.char === 'string') {
|
|
97
|
-
const {
|
|
98
|
-
char,
|
|
99
|
-
offset
|
|
100
|
-
} = item;
|
|
101
|
-
|
|
102
|
-
if (char !== ':' && (explicitKey || key !== undefined)) {
|
|
103
|
-
if (explicitKey && key === undefined) key = next ? items.pop() : null;
|
|
104
|
-
items.push(new _Pair.Pair(key));
|
|
105
|
-
explicitKey = false;
|
|
106
|
-
key = undefined;
|
|
107
|
-
keyStart = null;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (char === next) {
|
|
111
|
-
next = null;
|
|
112
|
-
} else if (!next && char === '?') {
|
|
113
|
-
explicitKey = true;
|
|
114
|
-
} else if (next !== '[' && char === ':' && key === undefined) {
|
|
115
|
-
if (next === ',') {
|
|
116
|
-
key = items.pop();
|
|
117
|
-
|
|
118
|
-
if (key instanceof _Pair.Pair) {
|
|
119
|
-
const msg = 'Chaining flow sequence pairs is invalid';
|
|
120
|
-
const err = new _errors.YAMLSemanticError(cst, msg);
|
|
121
|
-
err.offset = offset;
|
|
122
|
-
doc.errors.push(err);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (!explicitKey) (0, _parseUtils.checkKeyLength)(doc.errors, cst, i, key, keyStart);
|
|
126
|
-
} else {
|
|
127
|
-
key = null;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
keyStart = null;
|
|
131
|
-
explicitKey = false; // TODO: add error for non-explicit multiline plain key
|
|
132
|
-
|
|
133
|
-
next = null;
|
|
134
|
-
} else if (next === '[' || char !== ']' || i < cst.items.length - 1) {
|
|
135
|
-
const msg = `Flow sequence contains an unexpected ${char}`;
|
|
136
|
-
const err = new _errors.YAMLSyntaxError(cst, msg);
|
|
137
|
-
err.offset = offset;
|
|
138
|
-
doc.errors.push(err);
|
|
139
|
-
}
|
|
140
|
-
} else if (item.type === _constants.Type.BLANK_LINE) {
|
|
141
|
-
comments.push({
|
|
142
|
-
before: items.length
|
|
143
|
-
});
|
|
144
|
-
} else if (item.type === _constants.Type.COMMENT) {
|
|
145
|
-
comments.push({
|
|
146
|
-
comment: item.comment,
|
|
147
|
-
before: items.length
|
|
148
|
-
});
|
|
149
|
-
} else {
|
|
150
|
-
if (next) {
|
|
151
|
-
const msg = `Expected a ${next} in flow sequence`;
|
|
152
|
-
doc.errors.push(new _errors.YAMLSemanticError(item, msg));
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const value = doc.resolveNode(item);
|
|
156
|
-
|
|
157
|
-
if (key === undefined) {
|
|
158
|
-
items.push(value);
|
|
159
|
-
} else {
|
|
160
|
-
items.push(new _Pair.Pair(key, value));
|
|
161
|
-
key = undefined;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
keyStart = item.range.start;
|
|
165
|
-
next = ',';
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
(0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst);
|
|
170
|
-
if (key !== undefined) items.push(new _Pair.Pair(key));
|
|
171
|
-
return {
|
|
172
|
-
comments,
|
|
173
|
-
items
|
|
174
|
-
};
|
|
175
|
-
}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.checkFlowCollectionEnd = checkFlowCollectionEnd;
|
|
4
|
-
exports.checkKeyLength = checkKeyLength;
|
|
5
|
-
exports.resolveComments = resolveComments;
|
|
6
|
-
|
|
7
|
-
var _errors = require("../errors");
|
|
8
|
-
|
|
9
|
-
var _constants = require("../constants");
|
|
10
|
-
|
|
11
|
-
function checkFlowCollectionEnd(errors, cst) {
|
|
12
|
-
let char, name;
|
|
13
|
-
|
|
14
|
-
switch (cst.type) {
|
|
15
|
-
case _constants.Type.FLOW_MAP:
|
|
16
|
-
char = '}';
|
|
17
|
-
name = 'flow map';
|
|
18
|
-
break;
|
|
19
|
-
|
|
20
|
-
case _constants.Type.FLOW_SEQ:
|
|
21
|
-
char = ']';
|
|
22
|
-
name = 'flow sequence';
|
|
23
|
-
break;
|
|
24
|
-
|
|
25
|
-
default:
|
|
26
|
-
errors.push(new _errors.YAMLSemanticError(cst, 'Not a flow collection!?'));
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
let lastItem;
|
|
31
|
-
|
|
32
|
-
for (let i = cst.items.length - 1; i >= 0; --i) {
|
|
33
|
-
const item = cst.items[i];
|
|
34
|
-
|
|
35
|
-
if (!item || item.type !== _constants.Type.COMMENT) {
|
|
36
|
-
lastItem = item;
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (lastItem && lastItem.char !== char) {
|
|
42
|
-
const msg = `Expected ${name} to end with ${char}`;
|
|
43
|
-
let err;
|
|
44
|
-
|
|
45
|
-
if (typeof lastItem.offset === 'number') {
|
|
46
|
-
err = new _errors.YAMLSemanticError(cst, msg);
|
|
47
|
-
err.offset = lastItem.offset + 1;
|
|
48
|
-
} else {
|
|
49
|
-
err = new _errors.YAMLSemanticError(lastItem, msg);
|
|
50
|
-
if (lastItem.range && lastItem.range.end) err.offset = lastItem.range.end - lastItem.range.start;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
errors.push(err);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function checkKeyLength(errors, node, itemIdx, key, keyStart) {
|
|
58
|
-
if (!key || typeof keyStart !== 'number') return;
|
|
59
|
-
const item = node.items[itemIdx];
|
|
60
|
-
let keyEnd = item && item.range && item.range.start;
|
|
61
|
-
|
|
62
|
-
if (!keyEnd) {
|
|
63
|
-
for (let i = itemIdx - 1; i >= 0; --i) {
|
|
64
|
-
const it = node.items[i];
|
|
65
|
-
|
|
66
|
-
if (it && it.range) {
|
|
67
|
-
keyEnd = it.range.end + 2 * (itemIdx - i);
|
|
68
|
-
break;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (keyEnd > keyStart + 1024) {
|
|
74
|
-
const k = String(key).substr(0, 8) + '...' + String(key).substr(-8);
|
|
75
|
-
errors.push(new _errors.YAMLSemanticError(node, `The "${k}" key is too long`));
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function resolveComments(collection, comments) {
|
|
80
|
-
for (const {
|
|
81
|
-
afterKey,
|
|
82
|
-
before,
|
|
83
|
-
comment
|
|
84
|
-
} of comments) {
|
|
85
|
-
let item = collection.items[before];
|
|
86
|
-
|
|
87
|
-
if (!item) {
|
|
88
|
-
if (comment !== undefined) {
|
|
89
|
-
if (collection.comment) collection.comment += '\n' + comment;else collection.comment = comment;
|
|
90
|
-
}
|
|
91
|
-
} else {
|
|
92
|
-
if (afterKey && item.value) item = item.value;
|
|
93
|
-
|
|
94
|
-
if (comment === undefined) {
|
|
95
|
-
if (afterKey || !item.commentBefore) item.spaceBefore = true;
|
|
96
|
-
} else {
|
|
97
|
-
if (item.commentBefore) item.commentBefore += '\n' + comment;else item.commentBefore = comment;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
package/dist/stringify.js
DELETED
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.stringifyNumber = stringifyNumber;
|
|
4
|
-
exports.stringifyString = stringifyString;
|
|
5
|
-
|
|
6
|
-
var _addComment = require("./addComment");
|
|
7
|
-
|
|
8
|
-
var _constants = require("./constants");
|
|
9
|
-
|
|
10
|
-
var _foldFlowLines = require("./foldFlowLines");
|
|
11
|
-
|
|
12
|
-
var _options = require("./tags/options");
|
|
13
|
-
|
|
14
|
-
const getFoldOptions = ({
|
|
15
|
-
indentAtStart
|
|
16
|
-
}) => indentAtStart ? Object.assign({
|
|
17
|
-
indentAtStart
|
|
18
|
-
}, _options.strOptions.fold) : _options.strOptions.fold;
|
|
19
|
-
|
|
20
|
-
function stringifyNumber({
|
|
21
|
-
format,
|
|
22
|
-
minFractionDigits,
|
|
23
|
-
tag,
|
|
24
|
-
value
|
|
25
|
-
}) {
|
|
26
|
-
if (typeof value === 'bigint') return String(value);
|
|
27
|
-
if (!isFinite(value)) return isNaN(value) ? '.nan' : value < 0 ? '-.inf' : '.inf';
|
|
28
|
-
let n = JSON.stringify(value);
|
|
29
|
-
|
|
30
|
-
if (!format && minFractionDigits && (!tag || tag === 'tag:yaml.org,2002:float') && /^\d/.test(n)) {
|
|
31
|
-
let i = n.indexOf('.');
|
|
32
|
-
|
|
33
|
-
if (i < 0) {
|
|
34
|
-
i = n.length;
|
|
35
|
-
n += '.';
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
let d = minFractionDigits - (n.length - i - 1);
|
|
39
|
-
|
|
40
|
-
while (d-- > 0) n += '0';
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return n;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function lineLengthOverLimit(str, limit) {
|
|
47
|
-
const strLen = str.length;
|
|
48
|
-
if (strLen <= limit) return false;
|
|
49
|
-
|
|
50
|
-
for (let i = 0, start = 0; i < strLen; ++i) {
|
|
51
|
-
if (str[i] === '\n') {
|
|
52
|
-
if (i - start > limit) return true;
|
|
53
|
-
start = i + 1;
|
|
54
|
-
if (strLen - start <= limit) return false;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function doubleQuotedString(value, ctx) {
|
|
62
|
-
const {
|
|
63
|
-
implicitKey,
|
|
64
|
-
indent
|
|
65
|
-
} = ctx;
|
|
66
|
-
const {
|
|
67
|
-
jsonEncoding,
|
|
68
|
-
minMultiLineLength
|
|
69
|
-
} = _options.strOptions.doubleQuoted;
|
|
70
|
-
const json = JSON.stringify(value);
|
|
71
|
-
if (jsonEncoding) return json;
|
|
72
|
-
let str = '';
|
|
73
|
-
let start = 0;
|
|
74
|
-
|
|
75
|
-
for (let i = 0, ch = json[i]; ch; ch = json[++i]) {
|
|
76
|
-
if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') {
|
|
77
|
-
// space before newline needs to be escaped to not be folded
|
|
78
|
-
str += json.slice(start, i) + '\\ ';
|
|
79
|
-
i += 1;
|
|
80
|
-
start = i;
|
|
81
|
-
ch = '\\';
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (ch === '\\') switch (json[i + 1]) {
|
|
85
|
-
case 'u':
|
|
86
|
-
{
|
|
87
|
-
str += json.slice(start, i);
|
|
88
|
-
const code = json.substr(i + 2, 4);
|
|
89
|
-
|
|
90
|
-
switch (code) {
|
|
91
|
-
case '0000':
|
|
92
|
-
str += '\\0';
|
|
93
|
-
break;
|
|
94
|
-
|
|
95
|
-
case '0007':
|
|
96
|
-
str += '\\a';
|
|
97
|
-
break;
|
|
98
|
-
|
|
99
|
-
case '000b':
|
|
100
|
-
str += '\\v';
|
|
101
|
-
break;
|
|
102
|
-
|
|
103
|
-
case '001b':
|
|
104
|
-
str += '\\e';
|
|
105
|
-
break;
|
|
106
|
-
|
|
107
|
-
case '0085':
|
|
108
|
-
str += '\\N';
|
|
109
|
-
break;
|
|
110
|
-
|
|
111
|
-
case '00a0':
|
|
112
|
-
str += '\\_';
|
|
113
|
-
break;
|
|
114
|
-
|
|
115
|
-
case '2028':
|
|
116
|
-
str += '\\L';
|
|
117
|
-
break;
|
|
118
|
-
|
|
119
|
-
case '2029':
|
|
120
|
-
str += '\\P';
|
|
121
|
-
break;
|
|
122
|
-
|
|
123
|
-
default:
|
|
124
|
-
if (code.substr(0, 2) === '00') str += '\\x' + code.substr(2);else str += json.substr(i, 6);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
i += 5;
|
|
128
|
-
start = i + 1;
|
|
129
|
-
}
|
|
130
|
-
break;
|
|
131
|
-
|
|
132
|
-
case 'n':
|
|
133
|
-
if (implicitKey || json[i + 2] === '"' || json.length < minMultiLineLength) {
|
|
134
|
-
i += 1;
|
|
135
|
-
} else {
|
|
136
|
-
// folding will eat first newline
|
|
137
|
-
str += json.slice(start, i) + '\n\n';
|
|
138
|
-
|
|
139
|
-
while (json[i + 2] === '\\' && json[i + 3] === 'n' && json[i + 4] !== '"') {
|
|
140
|
-
str += '\n';
|
|
141
|
-
i += 2;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
str += indent; // space after newline needs to be escaped to not be folded
|
|
145
|
-
|
|
146
|
-
if (json[i + 2] === ' ') str += '\\';
|
|
147
|
-
i += 1;
|
|
148
|
-
start = i + 1;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
break;
|
|
152
|
-
|
|
153
|
-
default:
|
|
154
|
-
i += 1;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
str = start ? str + json.slice(start) : json;
|
|
159
|
-
return implicitKey ? str : (0, _foldFlowLines.foldFlowLines)(str, indent, _foldFlowLines.FOLD_QUOTED, getFoldOptions(ctx));
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function singleQuotedString(value, ctx) {
|
|
163
|
-
const {
|
|
164
|
-
indent,
|
|
165
|
-
implicitKey
|
|
166
|
-
} = ctx;
|
|
167
|
-
|
|
168
|
-
if (implicitKey) {
|
|
169
|
-
if (/\n/.test(value)) return doubleQuotedString(value, ctx);
|
|
170
|
-
} else {
|
|
171
|
-
// single quoted string can't have leading or trailing whitespace around newline
|
|
172
|
-
if (/[ \t]\n|\n[ \t]/.test(value)) return doubleQuotedString(value, ctx);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
|
|
176
|
-
return implicitKey ? res : (0, _foldFlowLines.foldFlowLines)(res, indent, _foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function blockString({
|
|
180
|
-
comment,
|
|
181
|
-
type,
|
|
182
|
-
value
|
|
183
|
-
}, ctx, onComment, onChompKeep) {
|
|
184
|
-
// 1. Block can't end in whitespace unless the last line is non-empty.
|
|
185
|
-
// 2. Strings consisting of only whitespace are best rendered explicitly.
|
|
186
|
-
if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
|
|
187
|
-
return doubleQuotedString(value, ctx);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const indent = ctx.indent || (ctx.forceBlockIndent ? ' ' : '');
|
|
191
|
-
const indentSize = indent ? '2' : '1'; // root is at -1
|
|
192
|
-
|
|
193
|
-
const literal = type === _constants.Type.BLOCK_FOLDED ? false : type === _constants.Type.BLOCK_LITERAL ? true : !lineLengthOverLimit(value, _options.strOptions.fold.lineWidth - indent.length);
|
|
194
|
-
let header = literal ? '|' : '>';
|
|
195
|
-
if (!value) return header + '\n';
|
|
196
|
-
let wsStart = '';
|
|
197
|
-
let wsEnd = '';
|
|
198
|
-
value = value.replace(/[\n\t ]*$/, ws => {
|
|
199
|
-
const n = ws.indexOf('\n');
|
|
200
|
-
|
|
201
|
-
if (n === -1) {
|
|
202
|
-
header += '-'; // strip
|
|
203
|
-
} else if (value === ws || n !== ws.length - 1) {
|
|
204
|
-
header += '+'; // keep
|
|
205
|
-
|
|
206
|
-
if (onChompKeep) onChompKeep();
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
wsEnd = ws.replace(/\n$/, '');
|
|
210
|
-
return '';
|
|
211
|
-
}).replace(/^[\n ]*/, ws => {
|
|
212
|
-
if (ws.indexOf(' ') !== -1) header += indentSize;
|
|
213
|
-
const m = ws.match(/ +$/);
|
|
214
|
-
|
|
215
|
-
if (m) {
|
|
216
|
-
wsStart = ws.slice(0, -m[0].length);
|
|
217
|
-
return m[0];
|
|
218
|
-
} else {
|
|
219
|
-
wsStart = ws;
|
|
220
|
-
return '';
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
if (wsEnd) wsEnd = wsEnd.replace(/\n+(?!\n|$)/g, `$&${indent}`);
|
|
224
|
-
if (wsStart) wsStart = wsStart.replace(/\n+/g, `$&${indent}`);
|
|
225
|
-
|
|
226
|
-
if (comment) {
|
|
227
|
-
header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ');
|
|
228
|
-
if (onComment) onComment();
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if (!value) return `${header}${indentSize}\n${indent}${wsEnd}`;
|
|
232
|
-
|
|
233
|
-
if (literal) {
|
|
234
|
-
value = value.replace(/\n+/g, `$&${indent}`);
|
|
235
|
-
return `${header}\n${indent}${wsStart}${value}${wsEnd}`;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
value = value.replace(/\n+/g, '\n$&').replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
|
|
239
|
-
// ^ ind.line ^ empty ^ capture next empty lines only at end of indent
|
|
240
|
-
.replace(/\n+/g, `$&${indent}`);
|
|
241
|
-
const body = (0, _foldFlowLines.foldFlowLines)(`${wsStart}${value}${wsEnd}`, indent, _foldFlowLines.FOLD_BLOCK, _options.strOptions.fold);
|
|
242
|
-
return `${header}\n${indent}${body}`;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function plainString(item, ctx, onComment, onChompKeep) {
|
|
246
|
-
const {
|
|
247
|
-
comment,
|
|
248
|
-
type,
|
|
249
|
-
value
|
|
250
|
-
} = item;
|
|
251
|
-
const {
|
|
252
|
-
actualString,
|
|
253
|
-
implicitKey,
|
|
254
|
-
indent,
|
|
255
|
-
inFlow,
|
|
256
|
-
tags
|
|
257
|
-
} = ctx;
|
|
258
|
-
|
|
259
|
-
if (implicitKey && /[\n[\]{},]/.test(value) || inFlow && /[[\]{},]/.test(value)) {
|
|
260
|
-
return doubleQuotedString(value, ctx);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (!value || /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
|
|
264
|
-
// not allowed:
|
|
265
|
-
// - empty string, '-' or '?'
|
|
266
|
-
// - start with an indicator character (except [?:-]) or /[?-] /
|
|
267
|
-
// - '\n ', ': ' or ' \n' anywhere
|
|
268
|
-
// - '#' not preceded by a non-space char
|
|
269
|
-
// - end with ' ' or ':'
|
|
270
|
-
return implicitKey || inFlow || value.indexOf('\n') === -1 ? value.indexOf('"') !== -1 && value.indexOf("'") === -1 ? singleQuotedString(value, ctx) : doubleQuotedString(value, ctx) : blockString(item, ctx, onComment, onChompKeep);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if (!implicitKey && !inFlow && type !== _constants.Type.PLAIN && value.indexOf('\n') !== -1) {
|
|
274
|
-
// Where allowed & type not set explicitly, prefer block style for multiline strings
|
|
275
|
-
return blockString(item, ctx, onComment, onChompKeep);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const str = value.replace(/\n+/g, `$&\n${indent}`); // Verify that output will be parsed as a string, as e.g. plain numbers and
|
|
279
|
-
// booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
|
|
280
|
-
// and others in v1.1.
|
|
281
|
-
|
|
282
|
-
if (actualString && typeof tags.resolveScalar(str).value !== 'string') {
|
|
283
|
-
return doubleQuotedString(value, ctx);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
const body = implicitKey ? str : (0, _foldFlowLines.foldFlowLines)(str, indent, _foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
|
|
287
|
-
|
|
288
|
-
if (comment && !inFlow && (body.indexOf('\n') !== -1 || comment.indexOf('\n') !== -1)) {
|
|
289
|
-
if (onComment) onComment();
|
|
290
|
-
return (0, _addComment.addCommentBefore)(body, indent, comment);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return body;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function stringifyString(item, ctx, onComment, onChompKeep) {
|
|
297
|
-
const {
|
|
298
|
-
defaultType
|
|
299
|
-
} = _options.strOptions;
|
|
300
|
-
const {
|
|
301
|
-
implicitKey,
|
|
302
|
-
inFlow
|
|
303
|
-
} = ctx;
|
|
304
|
-
let {
|
|
305
|
-
type,
|
|
306
|
-
value
|
|
307
|
-
} = item;
|
|
308
|
-
|
|
309
|
-
if (typeof value !== 'string') {
|
|
310
|
-
value = String(value);
|
|
311
|
-
item = Object.assign({}, item, {
|
|
312
|
-
value
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
const _stringify = _type => {
|
|
317
|
-
switch (_type) {
|
|
318
|
-
case _constants.Type.BLOCK_FOLDED:
|
|
319
|
-
case _constants.Type.BLOCK_LITERAL:
|
|
320
|
-
return blockString(item, ctx, onComment, onChompKeep);
|
|
321
|
-
|
|
322
|
-
case _constants.Type.QUOTE_DOUBLE:
|
|
323
|
-
return doubleQuotedString(value, ctx);
|
|
324
|
-
|
|
325
|
-
case _constants.Type.QUOTE_SINGLE:
|
|
326
|
-
return singleQuotedString(value, ctx);
|
|
327
|
-
|
|
328
|
-
case _constants.Type.PLAIN:
|
|
329
|
-
return plainString(item, ctx, onComment, onChompKeep);
|
|
330
|
-
|
|
331
|
-
default:
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
if (type !== _constants.Type.QUOTE_DOUBLE && /[\x00-\x08\x0b-\x1f\x7f-\x9f]/.test(value)) {
|
|
337
|
-
// force double quotes on control characters
|
|
338
|
-
type = _constants.Type.QUOTE_DOUBLE;
|
|
339
|
-
} else if ((implicitKey || inFlow) && (type === _constants.Type.BLOCK_FOLDED || type === _constants.Type.BLOCK_LITERAL)) {
|
|
340
|
-
// should not happen; blocks are not valid inside flow containers
|
|
341
|
-
type = _constants.Type.QUOTE_DOUBLE;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
let res = _stringify(type);
|
|
345
|
-
|
|
346
|
-
if (res === null) {
|
|
347
|
-
res = _stringify(defaultType);
|
|
348
|
-
if (res === null) throw new Error(`Unsupported default string type ${defaultType}`);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
return res;
|
|
352
|
-
}
|