yaml 2.0.0-3 → 2.0.0-7
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/LICENSE +1 -1
- package/README.md +46 -26
- package/browser/dist/compose/compose-collection.js +59 -0
- package/browser/dist/compose/compose-doc.js +39 -0
- package/browser/dist/compose/compose-node.js +79 -0
- package/browser/dist/compose/compose-scalar.js +74 -0
- package/browser/dist/compose/composer.js +219 -0
- package/browser/dist/compose/resolve-block-map.js +93 -0
- package/browser/dist/compose/resolve-block-scalar.js +185 -0
- package/browser/dist/compose/resolve-block-seq.js +40 -0
- package/browser/dist/compose/resolve-end.js +37 -0
- package/browser/dist/compose/resolve-flow-collection.js +189 -0
- package/browser/dist/compose/resolve-flow-scalar.js +217 -0
- package/browser/dist/compose/resolve-props.js +126 -0
- package/browser/dist/compose/util-contains-newline.js +34 -0
- package/browser/dist/compose/util-empty-scalar-position.js +27 -0
- package/browser/dist/compose/util-map-includes.js +17 -0
- package/browser/dist/doc/Document.js +251 -348
- package/browser/dist/doc/anchors.js +72 -0
- package/browser/dist/doc/applyReviver.js +41 -28
- package/browser/dist/doc/createNode.js +75 -55
- package/browser/dist/doc/directives.js +161 -0
- package/browser/dist/errors.js +50 -72
- package/browser/dist/index.js +16 -72
- package/browser/dist/log.js +11 -7
- package/browser/dist/nodes/Alias.js +94 -0
- package/browser/dist/nodes/Collection.js +133 -0
- package/browser/dist/nodes/Node.js +41 -0
- package/browser/dist/nodes/Pair.js +28 -0
- package/browser/dist/nodes/Scalar.js +23 -0
- package/browser/dist/nodes/YAMLMap.js +116 -0
- package/browser/dist/nodes/YAMLSeq.js +105 -0
- package/browser/dist/nodes/addPairToJSMap.js +103 -0
- package/browser/dist/nodes/toJS.js +35 -0
- package/browser/dist/options.js +14 -95
- package/browser/dist/parse/cst-scalar.js +219 -0
- package/browser/dist/parse/cst-stringify.js +61 -0
- package/browser/dist/parse/cst-visit.js +97 -0
- package/browser/dist/parse/cst.js +98 -0
- package/browser/dist/parse/lexer.js +674 -0
- package/browser/dist/parse/line-counter.js +39 -0
- package/browser/dist/parse/parser.js +876 -0
- package/browser/dist/public-api.js +101 -0
- package/browser/dist/schema/Schema.js +23 -0
- package/browser/dist/schema/common/map.js +42 -0
- package/browser/dist/schema/common/null.js +13 -0
- package/browser/dist/schema/common/seq.js +33 -0
- package/browser/dist/schema/common/string.js +14 -0
- package/browser/dist/schema/core/bool.js +19 -0
- package/browser/dist/schema/core/float.js +40 -0
- package/browser/dist/schema/core/int.js +38 -0
- package/browser/dist/schema/core/schema.js +23 -0
- package/browser/dist/schema/json/schema.js +62 -0
- package/browser/dist/schema/tags.js +79 -0
- package/browser/dist/schema/yaml-1.1/binary.js +66 -0
- package/browser/dist/schema/yaml-1.1/bool.js +26 -0
- package/browser/dist/schema/yaml-1.1/float.js +43 -0
- package/browser/dist/schema/yaml-1.1/int.js +71 -0
- package/browser/dist/schema/yaml-1.1/omap.js +73 -0
- package/browser/dist/schema/yaml-1.1/pairs.js +77 -0
- package/browser/dist/schema/yaml-1.1/schema.js +37 -0
- package/browser/dist/schema/yaml-1.1/set.js +87 -0
- package/browser/dist/schema/yaml-1.1/timestamp.js +101 -0
- package/browser/dist/stringify/foldFlowLines.js +121 -139
- package/browser/dist/stringify/stringify.js +87 -72
- package/browser/dist/stringify/stringifyCollection.js +122 -0
- package/browser/dist/stringify/stringifyComment.js +14 -0
- package/browser/dist/stringify/stringifyDocument.js +64 -0
- package/browser/dist/stringify/stringifyNumber.js +20 -23
- package/browser/dist/stringify/stringifyPair.js +111 -0
- package/browser/dist/stringify/stringifyString.js +295 -322
- package/browser/dist/util.js +4 -4
- package/browser/dist/visit.js +134 -71
- package/browser/index.js +4 -1
- package/dist/compose/compose-collection.d.ts +5 -0
- package/dist/compose/compose-collection.js +61 -0
- package/dist/compose/compose-doc.d.ts +6 -0
- package/dist/compose/compose-doc.js +41 -0
- package/dist/compose/compose-node.d.ts +25 -0
- package/dist/compose/compose-node.js +82 -0
- package/dist/compose/compose-scalar.d.ts +5 -0
- package/dist/compose/compose-scalar.js +76 -0
- package/dist/compose/composer.d.ts +62 -0
- package/dist/compose/composer.js +223 -0
- package/dist/compose/resolve-block-map.d.ts +6 -0
- package/dist/compose/resolve-block-map.js +95 -0
- package/dist/compose/resolve-block-scalar.d.ts +10 -0
- package/dist/compose/resolve-block-scalar.js +187 -0
- package/dist/compose/resolve-block-seq.d.ts +5 -0
- package/dist/compose/resolve-block-seq.js +42 -0
- package/dist/compose/resolve-end.d.ts +6 -0
- package/dist/compose/resolve-end.js +39 -0
- package/dist/compose/resolve-flow-collection.d.ts +6 -0
- package/dist/compose/resolve-flow-collection.js +191 -0
- package/dist/compose/resolve-flow-scalar.d.ts +10 -0
- package/dist/compose/resolve-flow-scalar.js +219 -0
- package/dist/compose/resolve-props.d.ts +21 -0
- package/dist/compose/resolve-props.js +128 -0
- package/dist/compose/util-contains-newline.d.ts +2 -0
- package/dist/compose/util-contains-newline.js +36 -0
- package/dist/compose/util-empty-scalar-position.d.ts +2 -0
- package/dist/compose/util-empty-scalar-position.js +29 -0
- package/dist/compose/util-map-includes.d.ts +4 -0
- package/dist/compose/util-map-includes.js +19 -0
- package/dist/doc/Document.d.ts +130 -0
- package/dist/doc/Document.js +250 -347
- package/dist/doc/anchors.d.ts +24 -0
- package/dist/doc/anchors.js +77 -0
- package/dist/doc/applyReviver.d.ts +9 -0
- package/dist/doc/applyReviver.js +41 -28
- package/dist/doc/createNode.d.ts +16 -0
- package/dist/doc/createNode.js +75 -55
- package/dist/doc/directives.d.ts +46 -0
- package/dist/doc/directives.js +163 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.js +51 -74
- package/dist/index.d.ts +19 -0
- package/dist/index.js +39 -70
- package/dist/log.d.ts +3 -0
- package/dist/log.js +11 -6
- package/dist/nodes/Alias.d.ts +24 -0
- package/dist/nodes/Alias.js +96 -0
- package/dist/nodes/Collection.d.ts +67 -0
- package/dist/nodes/Collection.js +137 -0
- package/dist/nodes/Node.d.ts +48 -0
- package/dist/nodes/Node.js +59 -0
- package/dist/nodes/Pair.d.ts +16 -0
- package/dist/nodes/Pair.js +31 -0
- package/dist/nodes/Scalar.d.ts +40 -0
- package/dist/nodes/Scalar.js +26 -0
- package/dist/nodes/YAMLMap.d.ts +39 -0
- package/dist/nodes/YAMLMap.js +119 -0
- package/dist/nodes/YAMLSeq.d.ts +53 -0
- package/dist/nodes/YAMLSeq.js +107 -0
- package/dist/nodes/addPairToJSMap.d.ts +3 -0
- package/dist/nodes/addPairToJSMap.js +105 -0
- package/dist/nodes/toJS.d.ts +30 -0
- package/dist/nodes/toJS.js +37 -0
- package/dist/options.d.ts +278 -0
- package/dist/options.js +13 -96
- package/dist/parse/cst-scalar.d.ts +58 -0
- package/dist/parse/cst-scalar.js +223 -0
- package/dist/parse/cst-stringify.d.ts +8 -0
- package/dist/parse/cst-stringify.js +63 -0
- package/dist/parse/cst-visit.d.ts +39 -0
- package/dist/parse/cst-visit.js +99 -0
- package/dist/parse/cst.d.ts +106 -0
- package/dist/parse/cst.js +112 -0
- package/dist/parse/lexer.d.ts +85 -0
- package/dist/parse/lexer.js +676 -0
- package/dist/parse/line-counter.d.ts +22 -0
- package/dist/parse/line-counter.js +41 -0
- package/dist/parse/parser.d.ts +84 -0
- package/dist/parse/parser.js +880 -0
- package/dist/public-api.d.ts +43 -0
- package/dist/public-api.js +106 -0
- package/dist/schema/Schema.d.ts +16 -0
- package/dist/schema/Schema.js +25 -0
- package/dist/schema/common/map.d.ts +2 -0
- package/dist/schema/common/map.js +44 -0
- package/dist/schema/common/null.d.ts +4 -0
- package/dist/schema/common/null.js +15 -0
- package/dist/schema/common/seq.d.ts +2 -0
- package/dist/schema/common/seq.js +35 -0
- package/dist/schema/common/string.d.ts +2 -0
- package/dist/schema/common/string.js +16 -0
- package/dist/schema/core/bool.d.ts +4 -0
- package/dist/schema/core/bool.js +21 -0
- package/dist/schema/core/float.d.ts +4 -0
- package/dist/schema/core/float.js +44 -0
- package/dist/schema/core/int.d.ts +4 -0
- package/dist/schema/core/int.js +42 -0
- package/dist/schema/core/schema.d.ts +1 -0
- package/dist/schema/core/schema.js +25 -0
- package/dist/schema/json/schema.d.ts +2 -0
- package/dist/schema/json/schema.js +64 -0
- package/dist/schema/tags.d.ts +41 -0
- package/dist/schema/tags.js +82 -0
- package/dist/schema/types.d.ts +82 -0
- package/dist/schema/yaml-1.1/binary.d.ts +2 -0
- package/dist/schema/yaml-1.1/binary.js +68 -0
- package/dist/schema/yaml-1.1/bool.d.ts +7 -0
- package/dist/schema/yaml-1.1/bool.js +29 -0
- package/dist/schema/yaml-1.1/float.d.ts +4 -0
- package/dist/schema/yaml-1.1/float.js +47 -0
- package/dist/schema/yaml-1.1/int.d.ts +5 -0
- package/dist/schema/yaml-1.1/int.js +76 -0
- package/dist/schema/yaml-1.1/omap.d.ts +21 -0
- package/dist/schema/yaml-1.1/omap.js +76 -0
- package/dist/schema/yaml-1.1/pairs.d.ts +10 -0
- package/dist/schema/yaml-1.1/pairs.js +81 -0
- package/dist/schema/yaml-1.1/schema.d.ts +1 -0
- package/dist/schema/yaml-1.1/schema.js +39 -0
- package/dist/schema/yaml-1.1/set.d.ts +22 -0
- package/dist/schema/yaml-1.1/set.js +90 -0
- package/dist/schema/yaml-1.1/timestamp.d.ts +6 -0
- package/dist/schema/yaml-1.1/timestamp.js +105 -0
- package/dist/stringify/foldFlowLines.d.ts +34 -0
- package/dist/stringify/foldFlowLines.js +121 -139
- package/dist/stringify/stringify.d.ts +18 -0
- package/dist/stringify/stringify.js +87 -71
- package/dist/stringify/stringifyCollection.d.ts +21 -0
- package/dist/stringify/stringifyCollection.js +124 -0
- package/dist/stringify/stringifyComment.d.ts +2 -0
- package/dist/stringify/stringifyComment.js +17 -0
- package/dist/stringify/stringifyDocument.d.ts +3 -0
- package/dist/stringify/stringifyDocument.js +66 -0
- package/dist/stringify/stringifyNumber.d.ts +2 -0
- package/dist/stringify/stringifyNumber.js +20 -23
- package/dist/stringify/stringifyPair.d.ts +3 -0
- package/dist/stringify/stringifyPair.js +113 -0
- package/dist/stringify/stringifyString.d.ts +3 -0
- package/dist/stringify/stringifyString.js +295 -322
- package/dist/test-events.d.ts +4 -0
- package/dist/test-events.js +124 -148
- package/dist/util.d.ts +6 -0
- package/dist/util.js +9 -12
- package/dist/visit.d.ts +63 -0
- package/dist/visit.js +134 -71
- package/package.json +31 -39
- package/util.js +2 -14
- package/browser/dist/_virtual/_rollupPluginBabelHelpers.js +0 -16
- package/browser/dist/ast/Alias.js +0 -95
- package/browser/dist/ast/Collection.js +0 -192
- package/browser/dist/ast/Merge.js +0 -71
- package/browser/dist/ast/Node.js +0 -3
- package/browser/dist/ast/Pair.js +0 -200
- package/browser/dist/ast/Scalar.js +0 -21
- package/browser/dist/ast/YAMLMap.js +0 -91
- package/browser/dist/ast/YAMLSeq.js +0 -67
- package/browser/dist/ast/toJS.js +0 -29
- package/browser/dist/constants.js +0 -40
- package/browser/dist/cst/Alias.js +0 -26
- package/browser/dist/cst/BlankLine.js +0 -34
- package/browser/dist/cst/BlockValue.js +0 -240
- package/browser/dist/cst/Collection.js +0 -259
- package/browser/dist/cst/CollectionItem.js +0 -123
- package/browser/dist/cst/Comment.js +0 -27
- package/browser/dist/cst/Directive.js +0 -53
- package/browser/dist/cst/Document.js +0 -262
- package/browser/dist/cst/FlowCollection.js +0 -201
- package/browser/dist/cst/Node.js +0 -355
- package/browser/dist/cst/ParseContext.js +0 -234
- package/browser/dist/cst/PlainValue.js +0 -171
- package/browser/dist/cst/QuoteDouble.js +0 -224
- package/browser/dist/cst/QuoteSingle.js +0 -100
- package/browser/dist/cst/Range.js +0 -56
- package/browser/dist/cst/parse.js +0 -46
- package/browser/dist/cst/source-utils.js +0 -169
- package/browser/dist/doc/Anchors.js +0 -115
- package/browser/dist/doc/Schema.js +0 -34
- package/browser/dist/doc/getSchemaTags.js +0 -33
- package/browser/dist/doc/listTagNames.js +0 -27
- package/browser/dist/doc/parseContents.js +0 -60
- package/browser/dist/doc/parseDirectives.js +0 -107
- package/browser/dist/resolve/collection-utils.js +0 -86
- package/browser/dist/resolve/resolveMap.js +0 -315
- package/browser/dist/resolve/resolveNode.js +0 -173
- package/browser/dist/resolve/resolveScalar.js +0 -20
- package/browser/dist/resolve/resolveSeq.js +0 -170
- package/browser/dist/resolve/resolveTag.js +0 -107
- package/browser/dist/resolve/resolveTagName.js +0 -87
- package/browser/dist/stringify/addComment.js +0 -10
- package/browser/dist/stringify/stringifyTag.js +0 -28
- package/browser/dist/tags/core.js +0 -128
- package/browser/dist/tags/failsafe/index.js +0 -7
- package/browser/dist/tags/failsafe/map.js +0 -37
- package/browser/dist/tags/failsafe/seq.js +0 -34
- package/browser/dist/tags/failsafe/string.js +0 -20
- package/browser/dist/tags/index.js +0 -39
- package/browser/dist/tags/json.js +0 -62
- package/browser/dist/tags/options.js +0 -31
- package/browser/dist/tags/yaml-1.1/binary.js +0 -83
- package/browser/dist/tags/yaml-1.1/index.js +0 -174
- package/browser/dist/tags/yaml-1.1/omap.js +0 -86
- package/browser/dist/tags/yaml-1.1/pairs.js +0 -62
- package/browser/dist/tags/yaml-1.1/set.js +0 -78
- package/browser/dist/tags/yaml-1.1/timestamp.js +0 -101
- package/browser/dist/types.js +0 -10
- package/cst.d.ts +0 -185
- package/dist/_virtual/_rollupPluginBabelHelpers.js +0 -18
- package/dist/ast/Alias.js +0 -97
- package/dist/ast/Collection.js +0 -196
- package/dist/ast/Merge.js +0 -74
- package/dist/ast/Node.js +0 -5
- package/dist/ast/Pair.js +0 -203
- package/dist/ast/Scalar.js +0 -24
- package/dist/ast/YAMLMap.js +0 -94
- package/dist/ast/YAMLSeq.js +0 -69
- package/dist/ast/toJS.js +0 -31
- package/dist/constants.js +0 -46
- package/dist/cst/Alias.js +0 -28
- package/dist/cst/BlankLine.js +0 -36
- package/dist/cst/BlockValue.js +0 -243
- package/dist/cst/Collection.js +0 -262
- package/dist/cst/CollectionItem.js +0 -125
- package/dist/cst/Comment.js +0 -29
- package/dist/cst/Directive.js +0 -55
- package/dist/cst/Document.js +0 -264
- package/dist/cst/FlowCollection.js +0 -203
- package/dist/cst/Node.js +0 -357
- package/dist/cst/ParseContext.js +0 -236
- package/dist/cst/PlainValue.js +0 -173
- package/dist/cst/QuoteDouble.js +0 -226
- package/dist/cst/QuoteSingle.js +0 -102
- package/dist/cst/Range.js +0 -58
- package/dist/cst/parse.js +0 -48
- package/dist/cst/source-utils.js +0 -173
- package/dist/doc/Anchors.js +0 -117
- package/dist/doc/Schema.js +0 -36
- package/dist/doc/getSchemaTags.js +0 -35
- package/dist/doc/listTagNames.js +0 -29
- package/dist/doc/parseContents.js +0 -62
- package/dist/doc/parseDirectives.js +0 -109
- package/dist/resolve/collection-utils.js +0 -91
- package/dist/resolve/resolveMap.js +0 -317
- package/dist/resolve/resolveNode.js +0 -175
- package/dist/resolve/resolveScalar.js +0 -22
- package/dist/resolve/resolveSeq.js +0 -172
- package/dist/resolve/resolveTag.js +0 -109
- package/dist/resolve/resolveTagName.js +0 -89
- package/dist/stringify/addComment.js +0 -13
- package/dist/stringify/stringifyTag.js +0 -30
- package/dist/tags/core.js +0 -138
- package/dist/tags/failsafe/index.js +0 -9
- package/dist/tags/failsafe/map.js +0 -39
- package/dist/tags/failsafe/seq.js +0 -36
- package/dist/tags/failsafe/string.js +0 -22
- package/dist/tags/index.js +0 -42
- package/dist/tags/json.js +0 -64
- package/dist/tags/options.js +0 -37
- package/dist/tags/yaml-1.1/binary.js +0 -85
- package/dist/tags/yaml-1.1/index.js +0 -176
- package/dist/tags/yaml-1.1/omap.js +0 -89
- package/dist/tags/yaml-1.1/pairs.js +0 -66
- package/dist/tags/yaml-1.1/set.js +0 -81
- package/dist/tags/yaml-1.1/timestamp.js +0 -105
- package/dist/types.js +0 -29
- package/index.d.ts +0 -519
- package/index.js +0 -1
- package/types.d.ts +0 -379
- package/types.js +0 -17
- package/types.mjs +0 -17
- package/util.d.ts +0 -76
- package/util.mjs +0 -15
package/dist/errors.js
CHANGED
|
@@ -1,85 +1,62 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var Node = require('./cst/Node.js');
|
|
4
|
-
var sourceUtils = require('./cst/source-utils.js');
|
|
5
|
-
var Range = require('./cst/Range.js');
|
|
6
|
-
|
|
7
3
|
class YAMLError extends Error {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
makePretty() {
|
|
17
|
-
if (!this.source) return;
|
|
18
|
-
this.nodeType = this.source.type;
|
|
19
|
-
const cst = this.source.context && this.source.context.root;
|
|
20
|
-
|
|
21
|
-
if (typeof this.offset === 'number') {
|
|
22
|
-
this.range = new Range.Range(this.offset, this.offset + 1);
|
|
23
|
-
const start = cst && sourceUtils.getLinePos(this.offset, cst);
|
|
24
|
-
|
|
25
|
-
if (start) {
|
|
26
|
-
const end = {
|
|
27
|
-
line: start.line,
|
|
28
|
-
col: start.col + 1
|
|
29
|
-
};
|
|
30
|
-
this.linePos = {
|
|
31
|
-
start,
|
|
32
|
-
end
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
delete this.offset;
|
|
37
|
-
} else {
|
|
38
|
-
this.range = this.source.range;
|
|
39
|
-
this.linePos = this.source.rangeAsLinePos;
|
|
4
|
+
constructor(name, pos, code, message) {
|
|
5
|
+
super();
|
|
6
|
+
this.name = name;
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.message = message;
|
|
9
|
+
this.pos = pos;
|
|
40
10
|
}
|
|
41
|
-
|
|
42
|
-
if (this.linePos) {
|
|
43
|
-
const {
|
|
44
|
-
line,
|
|
45
|
-
col
|
|
46
|
-
} = this.linePos.start;
|
|
47
|
-
this.message += ` at line ${line}, column ${col}`;
|
|
48
|
-
const ctx = cst && sourceUtils.getPrettyContext(this.linePos, cst);
|
|
49
|
-
if (ctx) this.message += `:\n\n${ctx}\n`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
delete this.source;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
class YAMLReferenceError extends YAMLError {
|
|
57
|
-
constructor(source, message) {
|
|
58
|
-
super('YAMLReferenceError', source, message);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
class YAMLSemanticError extends YAMLError {
|
|
63
|
-
constructor(source, message) {
|
|
64
|
-
super('YAMLSemanticError', source, message);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
11
|
}
|
|
68
|
-
class
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
12
|
+
class YAMLParseError extends YAMLError {
|
|
13
|
+
constructor(pos, code, message) {
|
|
14
|
+
super('YAMLParseError', pos, code, message);
|
|
15
|
+
}
|
|
73
16
|
}
|
|
74
17
|
class YAMLWarning extends YAMLError {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
18
|
+
constructor(pos, code, message) {
|
|
19
|
+
super('YAMLWarning', pos, code, message);
|
|
20
|
+
}
|
|
79
21
|
}
|
|
22
|
+
const prettifyError = (src, lc) => (error) => {
|
|
23
|
+
if (error.pos[0] === -1)
|
|
24
|
+
return;
|
|
25
|
+
error.linePos = error.pos.map(pos => lc.linePos(pos));
|
|
26
|
+
const { line, col } = error.linePos[0];
|
|
27
|
+
error.message += ` at line ${line}, column ${col}`;
|
|
28
|
+
let ci = col - 1;
|
|
29
|
+
let lineStr = src
|
|
30
|
+
.substring(lc.lineStarts[line - 1], lc.lineStarts[line])
|
|
31
|
+
.replace(/[\n\r]+$/, '');
|
|
32
|
+
// Trim to max 80 chars, keeping col position near the middle
|
|
33
|
+
if (ci >= 60 && lineStr.length > 80) {
|
|
34
|
+
const trimStart = Math.min(ci - 39, lineStr.length - 79);
|
|
35
|
+
lineStr = '…' + lineStr.substring(trimStart);
|
|
36
|
+
ci -= trimStart - 1;
|
|
37
|
+
}
|
|
38
|
+
if (lineStr.length > 80)
|
|
39
|
+
lineStr = lineStr.substring(0, 79) + '…';
|
|
40
|
+
// Include previous line in context if pointing at line start
|
|
41
|
+
if (line > 1 && /^ *$/.test(lineStr.substring(0, ci))) {
|
|
42
|
+
// Regexp won't match if start is trimmed
|
|
43
|
+
let prev = src.substring(lc.lineStarts[line - 2], lc.lineStarts[line - 1]);
|
|
44
|
+
if (prev.length > 80)
|
|
45
|
+
prev = prev.substring(0, 79) + '…\n';
|
|
46
|
+
lineStr = prev + lineStr;
|
|
47
|
+
}
|
|
48
|
+
if (/[^ ]/.test(lineStr)) {
|
|
49
|
+
let count = 1;
|
|
50
|
+
const end = error.linePos[1];
|
|
51
|
+
if (end && end.line === line && end.col > col) {
|
|
52
|
+
count = Math.min(end.col - col, 80 - ci);
|
|
53
|
+
}
|
|
54
|
+
const pointer = ' '.repeat(ci) + '^'.repeat(count);
|
|
55
|
+
error.message += `:\n\n${lineStr}\n${pointer}\n`;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
80
58
|
|
|
81
59
|
exports.YAMLError = YAMLError;
|
|
82
|
-
exports.
|
|
83
|
-
exports.YAMLSemanticError = YAMLSemanticError;
|
|
84
|
-
exports.YAMLSyntaxError = YAMLSyntaxError;
|
|
60
|
+
exports.YAMLParseError = YAMLParseError;
|
|
85
61
|
exports.YAMLWarning = YAMLWarning;
|
|
62
|
+
exports.prettifyError = prettifyError;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export { Composer } from './compose/composer.js';
|
|
2
|
+
export { Document } from './doc/Document.js';
|
|
3
|
+
export { Schema } from './schema/Schema.js';
|
|
4
|
+
export { YAMLError, YAMLParseError, YAMLWarning } from './errors.js';
|
|
5
|
+
export { Alias } from './nodes/Alias.js';
|
|
6
|
+
export { isAlias, isCollection, isDocument, isMap, isNode, isPair, isScalar, isSeq, Node, ParsedNode } from './nodes/Node.js';
|
|
7
|
+
export { Pair } from './nodes/Pair.js';
|
|
8
|
+
export { Scalar } from './nodes/Scalar.js';
|
|
9
|
+
export { YAMLMap } from './nodes/YAMLMap.js';
|
|
10
|
+
export { YAMLSeq } from './nodes/YAMLSeq.js';
|
|
11
|
+
export { CreateNodeOptions, defaultOptions, DocumentOptions, ParseOptions, SchemaOptions, ToJSOptions, ToStringOptions } from './options.js';
|
|
12
|
+
export * as CST from './parse/cst.js';
|
|
13
|
+
export { Lexer } from './parse/lexer.js';
|
|
14
|
+
export { LineCounter } from './parse/line-counter.js';
|
|
15
|
+
export { Parser } from './parse/parser.js';
|
|
16
|
+
export { EmptyStream, parse, parseAllDocuments, parseDocument, stringify } from './public-api.js';
|
|
17
|
+
export type { TagId, Tags } from './schema/tags';
|
|
18
|
+
export type { CollectionTag, ScalarTag } from './schema/types';
|
|
19
|
+
export { visit, visitor, visitorFn } from './visit.js';
|
package/dist/index.js
CHANGED
|
@@ -1,82 +1,51 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var parse$1 = require('./cst/parse.js');
|
|
3
|
+
var composer = require('./compose/composer.js');
|
|
5
4
|
var Document = require('./doc/Document.js');
|
|
5
|
+
var Schema = require('./schema/Schema.js');
|
|
6
6
|
var errors = require('./errors.js');
|
|
7
|
-
var
|
|
7
|
+
var Alias = require('./nodes/Alias.js');
|
|
8
|
+
var Node = require('./nodes/Node.js');
|
|
9
|
+
var Pair = require('./nodes/Pair.js');
|
|
10
|
+
var Scalar = require('./nodes/Scalar.js');
|
|
11
|
+
var YAMLMap = require('./nodes/YAMLMap.js');
|
|
12
|
+
var YAMLSeq = require('./nodes/YAMLSeq.js');
|
|
8
13
|
var options = require('./options.js');
|
|
14
|
+
var cst = require('./parse/cst.js');
|
|
15
|
+
var lexer = require('./parse/lexer.js');
|
|
16
|
+
var lineCounter = require('./parse/line-counter.js');
|
|
17
|
+
var parser = require('./parse/parser.js');
|
|
18
|
+
var publicApi = require('./public-api.js');
|
|
9
19
|
var visit = require('./visit.js');
|
|
10
20
|
|
|
11
|
-
function parseAllDocuments(src, options) {
|
|
12
|
-
const stream = [];
|
|
13
|
-
let prev;
|
|
14
21
|
|
|
15
|
-
for (const cstDoc of parse$1.parse(src)) {
|
|
16
|
-
const doc = new Document.Document(undefined, null, options);
|
|
17
|
-
doc.parse(cstDoc, prev);
|
|
18
|
-
stream.push(doc);
|
|
19
|
-
prev = doc;
|
|
20
|
-
}
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
function parseDocument(src, options) {
|
|
25
|
-
const cst = parse$1.parse(src);
|
|
26
|
-
const doc = new Document.Document(cst[0], null, options);
|
|
27
|
-
|
|
28
|
-
if (cst.length > 1 && constants.LogLevel.indexOf(doc.options.logLevel) >= constants.LogLevel.ERROR) {
|
|
29
|
-
const errMsg = 'Source contains multiple documents; please use YAML.parseAllDocuments()';
|
|
30
|
-
doc.errors.unshift(new errors.YAMLSemanticError(cst[1], errMsg));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return doc;
|
|
34
|
-
}
|
|
35
|
-
function parse(src, reviver, options) {
|
|
36
|
-
if (options === undefined && reviver && typeof reviver === 'object') {
|
|
37
|
-
options = reviver;
|
|
38
|
-
reviver = undefined;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const doc = parseDocument(src, options);
|
|
42
|
-
doc.warnings.forEach(warning => log.warn(doc.options.logLevel, warning));
|
|
43
|
-
|
|
44
|
-
if (doc.errors.length > 0) {
|
|
45
|
-
if (constants.LogLevel.indexOf(doc.options.logLevel) >= constants.LogLevel.ERROR) throw doc.errors[0];else doc.errors = [];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return doc.toJS({
|
|
49
|
-
reviver
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
function stringify(value, replacer, options) {
|
|
53
|
-
if (typeof options === 'string') options = options.length;
|
|
54
|
-
|
|
55
|
-
if (typeof options === 'number') {
|
|
56
|
-
const indent = Math.round(options);
|
|
57
|
-
options = indent < 1 ? undefined : indent > 8 ? {
|
|
58
|
-
indent: 8
|
|
59
|
-
} : {
|
|
60
|
-
indent
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (value === undefined) {
|
|
65
|
-
const {
|
|
66
|
-
keepUndefined
|
|
67
|
-
} = options || replacer || {};
|
|
68
|
-
if (!keepUndefined) return undefined;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return new Document.Document(value, replacer, options).toString();
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
exports.parseCST = parse$1.parse;
|
|
23
|
+
exports.Composer = composer.Composer;
|
|
75
24
|
exports.Document = Document.Document;
|
|
25
|
+
exports.Schema = Schema.Schema;
|
|
26
|
+
exports.YAMLError = errors.YAMLError;
|
|
27
|
+
exports.YAMLParseError = errors.YAMLParseError;
|
|
28
|
+
exports.YAMLWarning = errors.YAMLWarning;
|
|
29
|
+
exports.Alias = Alias.Alias;
|
|
30
|
+
exports.isAlias = Node.isAlias;
|
|
31
|
+
exports.isCollection = Node.isCollection;
|
|
32
|
+
exports.isDocument = Node.isDocument;
|
|
33
|
+
exports.isMap = Node.isMap;
|
|
34
|
+
exports.isNode = Node.isNode;
|
|
35
|
+
exports.isPair = Node.isPair;
|
|
36
|
+
exports.isScalar = Node.isScalar;
|
|
37
|
+
exports.isSeq = Node.isSeq;
|
|
38
|
+
exports.Pair = Pair.Pair;
|
|
39
|
+
exports.Scalar = Scalar.Scalar;
|
|
40
|
+
exports.YAMLMap = YAMLMap.YAMLMap;
|
|
41
|
+
exports.YAMLSeq = YAMLSeq.YAMLSeq;
|
|
76
42
|
exports.defaultOptions = options.defaultOptions;
|
|
77
|
-
exports.
|
|
43
|
+
exports.CST = cst;
|
|
44
|
+
exports.Lexer = lexer.Lexer;
|
|
45
|
+
exports.LineCounter = lineCounter.LineCounter;
|
|
46
|
+
exports.Parser = parser.Parser;
|
|
47
|
+
exports.parse = publicApi.parse;
|
|
48
|
+
exports.parseAllDocuments = publicApi.parseAllDocuments;
|
|
49
|
+
exports.parseDocument = publicApi.parseDocument;
|
|
50
|
+
exports.stringify = publicApi.stringify;
|
|
78
51
|
exports.visit = visit.visit;
|
|
79
|
-
exports.parse = parse;
|
|
80
|
-
exports.parseAllDocuments = parseAllDocuments;
|
|
81
|
-
exports.parseDocument = parseDocument;
|
|
82
|
-
exports.stringify = stringify;
|
package/dist/log.d.ts
ADDED
package/dist/log.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
function debug(logLevel, ...messages) {
|
|
4
|
+
if (logLevel === 'debug')
|
|
5
|
+
console.log(...messages);
|
|
6
|
+
}
|
|
6
7
|
function warn(logLevel, warning) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
if (logLevel === 'debug' || logLevel === 'warn') {
|
|
9
|
+
if (typeof process !== 'undefined' && process.emitWarning)
|
|
10
|
+
process.emitWarning(warning);
|
|
11
|
+
else
|
|
12
|
+
console.warn(warning);
|
|
13
|
+
}
|
|
10
14
|
}
|
|
11
15
|
|
|
16
|
+
exports.debug = debug;
|
|
12
17
|
exports.warn = warn;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Document } from '../doc/Document';
|
|
2
|
+
import type { StringifyContext } from '../stringify/stringify.js';
|
|
3
|
+
import { NodeBase, Range } from './Node.js';
|
|
4
|
+
import type { Scalar } from './Scalar';
|
|
5
|
+
import type { ToJSContext } from './toJS.js';
|
|
6
|
+
import type { YAMLMap } from './YAMLMap';
|
|
7
|
+
import type { YAMLSeq } from './YAMLSeq';
|
|
8
|
+
export declare namespace Alias {
|
|
9
|
+
interface Parsed extends Alias {
|
|
10
|
+
range: Range;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export declare class Alias extends NodeBase {
|
|
14
|
+
source: string;
|
|
15
|
+
anchor?: never;
|
|
16
|
+
constructor(source: string);
|
|
17
|
+
/**
|
|
18
|
+
* Resolve the value of this alias within `doc`, finding the last
|
|
19
|
+
* instance of the `source` anchor before this node.
|
|
20
|
+
*/
|
|
21
|
+
resolve(doc: Document): Scalar | YAMLMap | YAMLSeq | undefined;
|
|
22
|
+
toJSON(_arg?: unknown, ctx?: ToJSContext): unknown;
|
|
23
|
+
toString(ctx?: StringifyContext, _onComment?: () => void, _onChompKeep?: () => void): string;
|
|
24
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var anchors = require('../doc/anchors.js');
|
|
4
|
+
var visit = require('../visit.js');
|
|
5
|
+
var Node = require('./Node.js');
|
|
6
|
+
|
|
7
|
+
class Alias extends Node.NodeBase {
|
|
8
|
+
constructor(source) {
|
|
9
|
+
super(Node.ALIAS);
|
|
10
|
+
this.source = source;
|
|
11
|
+
Object.defineProperty(this, 'tag', {
|
|
12
|
+
set() {
|
|
13
|
+
throw new Error('Alias nodes cannot have tags');
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Resolve the value of this alias within `doc`, finding the last
|
|
19
|
+
* instance of the `source` anchor before this node.
|
|
20
|
+
*/
|
|
21
|
+
resolve(doc) {
|
|
22
|
+
let found = undefined;
|
|
23
|
+
visit.visit(doc, {
|
|
24
|
+
Node: (_key, node) => {
|
|
25
|
+
if (node === this)
|
|
26
|
+
return visit.visit.BREAK;
|
|
27
|
+
if (node.anchor === this.source)
|
|
28
|
+
found = node;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return found;
|
|
32
|
+
}
|
|
33
|
+
toJSON(_arg, ctx) {
|
|
34
|
+
if (!ctx)
|
|
35
|
+
return { source: this.source };
|
|
36
|
+
const { anchors, doc, maxAliasCount } = ctx;
|
|
37
|
+
const source = this.resolve(doc);
|
|
38
|
+
if (!source) {
|
|
39
|
+
const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
|
|
40
|
+
throw new ReferenceError(msg);
|
|
41
|
+
}
|
|
42
|
+
const data = anchors.get(source);
|
|
43
|
+
/* istanbul ignore if */
|
|
44
|
+
if (!data || data.res === undefined) {
|
|
45
|
+
const msg = 'This should not happen: Alias anchor was not resolved?';
|
|
46
|
+
throw new ReferenceError(msg);
|
|
47
|
+
}
|
|
48
|
+
if (maxAliasCount >= 0) {
|
|
49
|
+
data.count += 1;
|
|
50
|
+
if (data.aliasCount === 0)
|
|
51
|
+
data.aliasCount = getAliasCount(doc, source, anchors);
|
|
52
|
+
if (data.count * data.aliasCount > maxAliasCount) {
|
|
53
|
+
const msg = 'Excessive alias count indicates a resource exhaustion attack';
|
|
54
|
+
throw new ReferenceError(msg);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return data.res;
|
|
58
|
+
}
|
|
59
|
+
toString(ctx, _onComment, _onChompKeep) {
|
|
60
|
+
const src = `*${this.source}`;
|
|
61
|
+
if (ctx) {
|
|
62
|
+
anchors.anchorIsValid(this.source);
|
|
63
|
+
if (ctx.options.verifyAliasOrder && !ctx.anchors.has(this.source)) {
|
|
64
|
+
const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
|
|
65
|
+
throw new Error(msg);
|
|
66
|
+
}
|
|
67
|
+
if (ctx.implicitKey)
|
|
68
|
+
return `${src} `;
|
|
69
|
+
}
|
|
70
|
+
return src;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function getAliasCount(doc, node, anchors) {
|
|
74
|
+
if (Node.isAlias(node)) {
|
|
75
|
+
const source = node.resolve(doc);
|
|
76
|
+
const anchor = anchors && source && anchors.get(source);
|
|
77
|
+
return anchor ? anchor.count * anchor.aliasCount : 0;
|
|
78
|
+
}
|
|
79
|
+
else if (Node.isCollection(node)) {
|
|
80
|
+
let count = 0;
|
|
81
|
+
for (const item of node.items) {
|
|
82
|
+
const c = getAliasCount(doc, item, anchors);
|
|
83
|
+
if (c > count)
|
|
84
|
+
count = c;
|
|
85
|
+
}
|
|
86
|
+
return count;
|
|
87
|
+
}
|
|
88
|
+
else if (Node.isPair(node)) {
|
|
89
|
+
const kc = getAliasCount(doc, node.key, anchors);
|
|
90
|
+
const vc = getAliasCount(doc, node.value, anchors);
|
|
91
|
+
return Math.max(kc, vc);
|
|
92
|
+
}
|
|
93
|
+
return 1;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
exports.Alias = Alias;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { Schema } from '../schema/Schema.js';
|
|
2
|
+
import { NodeBase, NODE_TYPE } from './Node.js';
|
|
3
|
+
export declare function collectionFromPath(schema: Schema, path: unknown[], value: unknown): import("./Node.js").Node;
|
|
4
|
+
export declare const isEmptyPath: (path: Iterable<unknown> | null | undefined) => boolean;
|
|
5
|
+
export declare abstract class Collection extends NodeBase {
|
|
6
|
+
static maxFlowStringSingleLineLength: number;
|
|
7
|
+
schema: Schema | undefined;
|
|
8
|
+
[NODE_TYPE]: symbol;
|
|
9
|
+
items: unknown[];
|
|
10
|
+
/** An optional anchor on this node. Used by alias nodes. */
|
|
11
|
+
anchor?: string;
|
|
12
|
+
/**
|
|
13
|
+
* If true, stringify this and all child nodes using flow rather than
|
|
14
|
+
* block styles.
|
|
15
|
+
*/
|
|
16
|
+
flow?: boolean;
|
|
17
|
+
constructor(type: symbol, schema?: Schema);
|
|
18
|
+
/** Adds a value to the collection. */
|
|
19
|
+
abstract add(value: unknown): void;
|
|
20
|
+
/**
|
|
21
|
+
* Removes a value from the collection.
|
|
22
|
+
* @returns `true` if the item was found and removed.
|
|
23
|
+
*/
|
|
24
|
+
abstract delete(key: unknown): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Returns item at `key`, or `undefined` if not found. By default unwraps
|
|
27
|
+
* scalar values from their surrounding node; to disable set `keepScalar` to
|
|
28
|
+
* `true` (collections are always returned intact).
|
|
29
|
+
*/
|
|
30
|
+
abstract get(key: unknown, keepScalar?: boolean): unknown;
|
|
31
|
+
/**
|
|
32
|
+
* Checks if the collection includes a value with the key `key`.
|
|
33
|
+
*/
|
|
34
|
+
abstract has(key: unknown): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Sets a value in this collection. For `!!set`, `value` needs to be a
|
|
37
|
+
* boolean to add/remove the item from the set.
|
|
38
|
+
*/
|
|
39
|
+
abstract set(key: unknown, value: unknown): void;
|
|
40
|
+
/**
|
|
41
|
+
* Adds a value to the collection. For `!!map` and `!!omap` the value must
|
|
42
|
+
* be a Pair instance or a `{ key, value }` object, which may not have a key
|
|
43
|
+
* that already exists in the map.
|
|
44
|
+
*/
|
|
45
|
+
addIn(path: Iterable<unknown>, value: unknown): void;
|
|
46
|
+
/**
|
|
47
|
+
* Removes a value from the collection.
|
|
48
|
+
* @returns `true` if the item was found and removed.
|
|
49
|
+
*/
|
|
50
|
+
deleteIn([key, ...rest]: Iterable<unknown>): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Returns item at `key`, or `undefined` if not found. By default unwraps
|
|
53
|
+
* scalar values from their surrounding node; to disable set `keepScalar` to
|
|
54
|
+
* `true` (collections are always returned intact).
|
|
55
|
+
*/
|
|
56
|
+
getIn([key, ...rest]: Iterable<unknown>, keepScalar?: boolean): unknown;
|
|
57
|
+
hasAllNullValues(allowScalar?: boolean): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Checks if the collection includes a value with the key `key`.
|
|
60
|
+
*/
|
|
61
|
+
hasIn([key, ...rest]: Iterable<unknown>): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Sets a value in this collection. For `!!set`, `value` needs to be a
|
|
64
|
+
* boolean to add/remove the item from the set.
|
|
65
|
+
*/
|
|
66
|
+
setIn([key, ...rest]: Iterable<unknown>, value: unknown): void;
|
|
67
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var createNode = require('../doc/createNode.js');
|
|
4
|
+
var Node = require('./Node.js');
|
|
5
|
+
|
|
6
|
+
function collectionFromPath(schema, path, value) {
|
|
7
|
+
let v = value;
|
|
8
|
+
for (let i = path.length - 1; i >= 0; --i) {
|
|
9
|
+
const k = path[i];
|
|
10
|
+
if (typeof k === 'number' && Number.isInteger(k) && k >= 0) {
|
|
11
|
+
const a = [];
|
|
12
|
+
a[k] = v;
|
|
13
|
+
v = a;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
const o = {};
|
|
17
|
+
Object.defineProperty(o, typeof k === 'symbol' ? k : String(k), {
|
|
18
|
+
value: v,
|
|
19
|
+
writable: true,
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true
|
|
22
|
+
});
|
|
23
|
+
v = o;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return createNode.createNode(v, undefined, {
|
|
27
|
+
onAnchor() {
|
|
28
|
+
throw new Error('Repeated objects are not supported here');
|
|
29
|
+
},
|
|
30
|
+
schema,
|
|
31
|
+
sourceObjects: new Map()
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
// null, undefined, or an empty non-string iterable (e.g. [])
|
|
35
|
+
const isEmptyPath = (path) => path == null ||
|
|
36
|
+
(typeof path === 'object' && !!path[Symbol.iterator]().next().done);
|
|
37
|
+
class Collection extends Node.NodeBase {
|
|
38
|
+
constructor(type, schema) {
|
|
39
|
+
super(type);
|
|
40
|
+
Object.defineProperty(this, 'schema', {
|
|
41
|
+
value: schema,
|
|
42
|
+
configurable: true,
|
|
43
|
+
enumerable: false,
|
|
44
|
+
writable: true
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Adds a value to the collection. For `!!map` and `!!omap` the value must
|
|
49
|
+
* be a Pair instance or a `{ key, value }` object, which may not have a key
|
|
50
|
+
* that already exists in the map.
|
|
51
|
+
*/
|
|
52
|
+
addIn(path, value) {
|
|
53
|
+
if (isEmptyPath(path))
|
|
54
|
+
this.add(value);
|
|
55
|
+
else {
|
|
56
|
+
const [key, ...rest] = path;
|
|
57
|
+
const node = this.get(key, true);
|
|
58
|
+
if (Node.isCollection(node))
|
|
59
|
+
node.addIn(rest, value);
|
|
60
|
+
else if (node === undefined && this.schema)
|
|
61
|
+
this.set(key, collectionFromPath(this.schema, rest, value));
|
|
62
|
+
else
|
|
63
|
+
throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Removes a value from the collection.
|
|
68
|
+
* @returns `true` if the item was found and removed.
|
|
69
|
+
*/
|
|
70
|
+
deleteIn([key, ...rest]) {
|
|
71
|
+
if (rest.length === 0)
|
|
72
|
+
return this.delete(key);
|
|
73
|
+
const node = this.get(key, true);
|
|
74
|
+
if (Node.isCollection(node))
|
|
75
|
+
return node.deleteIn(rest);
|
|
76
|
+
else
|
|
77
|
+
throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Returns item at `key`, or `undefined` if not found. By default unwraps
|
|
81
|
+
* scalar values from their surrounding node; to disable set `keepScalar` to
|
|
82
|
+
* `true` (collections are always returned intact).
|
|
83
|
+
*/
|
|
84
|
+
getIn([key, ...rest], keepScalar) {
|
|
85
|
+
const node = this.get(key, true);
|
|
86
|
+
if (rest.length === 0)
|
|
87
|
+
return !keepScalar && Node.isScalar(node) ? node.value : node;
|
|
88
|
+
else
|
|
89
|
+
return Node.isCollection(node) ? node.getIn(rest, keepScalar) : undefined;
|
|
90
|
+
}
|
|
91
|
+
hasAllNullValues(allowScalar) {
|
|
92
|
+
return this.items.every(node => {
|
|
93
|
+
if (!Node.isPair(node))
|
|
94
|
+
return false;
|
|
95
|
+
const n = node.value;
|
|
96
|
+
return (n == null ||
|
|
97
|
+
(allowScalar &&
|
|
98
|
+
Node.isScalar(n) &&
|
|
99
|
+
n.value == null &&
|
|
100
|
+
!n.commentBefore &&
|
|
101
|
+
!n.comment &&
|
|
102
|
+
!n.tag));
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Checks if the collection includes a value with the key `key`.
|
|
107
|
+
*/
|
|
108
|
+
hasIn([key, ...rest]) {
|
|
109
|
+
if (rest.length === 0)
|
|
110
|
+
return this.has(key);
|
|
111
|
+
const node = this.get(key, true);
|
|
112
|
+
return Node.isCollection(node) ? node.hasIn(rest) : false;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Sets a value in this collection. For `!!set`, `value` needs to be a
|
|
116
|
+
* boolean to add/remove the item from the set.
|
|
117
|
+
*/
|
|
118
|
+
setIn([key, ...rest], value) {
|
|
119
|
+
if (rest.length === 0) {
|
|
120
|
+
this.set(key, value);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const node = this.get(key, true);
|
|
124
|
+
if (Node.isCollection(node))
|
|
125
|
+
node.setIn(rest, value);
|
|
126
|
+
else if (node === undefined && this.schema)
|
|
127
|
+
this.set(key, collectionFromPath(this.schema, rest, value));
|
|
128
|
+
else
|
|
129
|
+
throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
Collection.maxFlowStringSingleLineLength = 60;
|
|
134
|
+
|
|
135
|
+
exports.Collection = Collection;
|
|
136
|
+
exports.collectionFromPath = collectionFromPath;
|
|
137
|
+
exports.isEmptyPath = isEmptyPath;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Document } from '../doc/Document.js';
|
|
2
|
+
import type { StringifyContext } from '../stringify/stringify.js';
|
|
3
|
+
import type { Alias } from './Alias.js';
|
|
4
|
+
import type { Pair } from './Pair.js';
|
|
5
|
+
import type { Scalar } from './Scalar.js';
|
|
6
|
+
import type { YAMLMap } from './YAMLMap.js';
|
|
7
|
+
import type { YAMLSeq } from './YAMLSeq.js';
|
|
8
|
+
export declare type Node = Alias | Scalar | YAMLMap | YAMLSeq;
|
|
9
|
+
export declare type ParsedNode = Alias.Parsed | Scalar.Parsed | YAMLMap.Parsed | YAMLSeq.Parsed;
|
|
10
|
+
export declare type Range = [number, number, number];
|
|
11
|
+
export declare const ALIAS: unique symbol;
|
|
12
|
+
export declare const DOC: unique symbol;
|
|
13
|
+
export declare const MAP: unique symbol;
|
|
14
|
+
export declare const PAIR: unique symbol;
|
|
15
|
+
export declare const SCALAR: unique symbol;
|
|
16
|
+
export declare const SEQ: unique symbol;
|
|
17
|
+
export declare const NODE_TYPE: unique symbol;
|
|
18
|
+
export declare const isAlias: (node: any) => node is Alias;
|
|
19
|
+
export declare const isDocument: (node: any) => node is Document<unknown>;
|
|
20
|
+
export declare const isMap: (node: any) => node is YAMLMap<unknown, unknown>;
|
|
21
|
+
export declare const isPair: (node: any) => node is Pair<unknown, unknown>;
|
|
22
|
+
export declare const isScalar: (node: any) => node is Scalar<unknown>;
|
|
23
|
+
export declare const isSeq: (node: any) => node is YAMLSeq<unknown>;
|
|
24
|
+
export declare function isCollection(node: any): node is YAMLMap | YAMLSeq;
|
|
25
|
+
export declare function isNode(node: any): node is Node;
|
|
26
|
+
export declare const hasAnchor: (node: unknown) => node is YAMLMap<unknown, unknown> | YAMLSeq<unknown> | Scalar<unknown>;
|
|
27
|
+
export declare abstract class NodeBase {
|
|
28
|
+
readonly [NODE_TYPE]: symbol;
|
|
29
|
+
/** A comment on or immediately after this */
|
|
30
|
+
comment?: string | null;
|
|
31
|
+
/** A comment before this */
|
|
32
|
+
commentBefore?: string | null;
|
|
33
|
+
/**
|
|
34
|
+
* The `[start, value-end, node-end]` character offsets for the part of the
|
|
35
|
+
* source parsed into this node (undefined if not parsed). The `value-end`
|
|
36
|
+
* and `node-end` positions are themselves not included in their respective
|
|
37
|
+
* ranges.
|
|
38
|
+
*/
|
|
39
|
+
range?: Range | null;
|
|
40
|
+
/** A blank line before this node and its commentBefore */
|
|
41
|
+
spaceBefore?: boolean;
|
|
42
|
+
/** A fully qualified tag, if required */
|
|
43
|
+
tag?: string;
|
|
44
|
+
/** A plain JS representation of this node */
|
|
45
|
+
abstract toJSON(): any;
|
|
46
|
+
abstract toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
|
|
47
|
+
constructor(type: symbol);
|
|
48
|
+
}
|