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
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var Node = require('../nodes/Node.js');
|
|
4
|
+
var visit = require('../visit.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Verify that the input string is a valid anchor.
|
|
8
|
+
*
|
|
9
|
+
* Will throw on errors.
|
|
10
|
+
*/
|
|
11
|
+
function anchorIsValid(anchor) {
|
|
12
|
+
if (/[\x00-\x19\s,[\]{}]/.test(anchor)) {
|
|
13
|
+
const sa = JSON.stringify(anchor);
|
|
14
|
+
const msg = `Anchor must not contain whitespace or control characters: ${sa}`;
|
|
15
|
+
throw new Error(msg);
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
function anchorNames(root) {
|
|
20
|
+
const anchors = new Set();
|
|
21
|
+
visit.visit(root, {
|
|
22
|
+
Value(_key, node) {
|
|
23
|
+
if (node.anchor)
|
|
24
|
+
anchors.add(node.anchor);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return anchors;
|
|
28
|
+
}
|
|
29
|
+
/** Find a new anchor name with the given `prefix` and a one-indexed suffix. */
|
|
30
|
+
function findNewAnchor(prefix, exclude) {
|
|
31
|
+
for (let i = 1; true; ++i) {
|
|
32
|
+
const name = `${prefix}${i}`;
|
|
33
|
+
if (!exclude.has(name))
|
|
34
|
+
return name;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function createNodeAnchors(doc, prefix) {
|
|
38
|
+
const aliasObjects = [];
|
|
39
|
+
const sourceObjects = new Map();
|
|
40
|
+
let prevAnchors = null;
|
|
41
|
+
return {
|
|
42
|
+
onAnchor(source) {
|
|
43
|
+
aliasObjects.push(source);
|
|
44
|
+
if (!prevAnchors)
|
|
45
|
+
prevAnchors = anchorNames(doc);
|
|
46
|
+
const anchor = findNewAnchor(prefix, prevAnchors);
|
|
47
|
+
prevAnchors.add(anchor);
|
|
48
|
+
return anchor;
|
|
49
|
+
},
|
|
50
|
+
/**
|
|
51
|
+
* With circular references, the source node is only resolved after all
|
|
52
|
+
* of its child nodes are. This is why anchors are set only after all of
|
|
53
|
+
* the nodes have been created.
|
|
54
|
+
*/
|
|
55
|
+
setAnchors() {
|
|
56
|
+
for (const source of aliasObjects) {
|
|
57
|
+
const ref = sourceObjects.get(source);
|
|
58
|
+
if (typeof ref === 'object' &&
|
|
59
|
+
ref.anchor &&
|
|
60
|
+
(Node.isScalar(ref.node) || Node.isCollection(ref.node))) {
|
|
61
|
+
ref.node.anchor = ref.anchor;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const error = new Error('Failed to resolve repeated object (this should not happen)');
|
|
65
|
+
error.source = source;
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
sourceObjects
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
exports.anchorIsValid = anchorIsValid;
|
|
75
|
+
exports.anchorNames = anchorNames;
|
|
76
|
+
exports.createNodeAnchors = createNodeAnchors;
|
|
77
|
+
exports.findNewAnchor = findNewAnchor;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare type Reviver = (key: unknown, value: unknown) => unknown;
|
|
2
|
+
/**
|
|
3
|
+
* Applies the JSON.parse reviver algorithm as defined in the ECMA-262 spec,
|
|
4
|
+
* in section 24.5.1.1 "Runtime Semantics: InternalizeJSONProperty" of the
|
|
5
|
+
* 2021 edition: https://tc39.es/ecma262/#sec-json.parse
|
|
6
|
+
*
|
|
7
|
+
* Includes extensions for handling Map and Set objects.
|
|
8
|
+
*/
|
|
9
|
+
export declare function applyReviver(reviver: Reviver, obj: unknown, key: unknown, val: any): unknown;
|
package/dist/doc/applyReviver.js
CHANGED
|
@@ -8,36 +8,49 @@
|
|
|
8
8
|
* Includes extensions for handling Map and Set objects.
|
|
9
9
|
*/
|
|
10
10
|
function applyReviver(reviver, obj, key, val) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
11
|
+
if (val && typeof val === 'object') {
|
|
12
|
+
if (Array.isArray(val)) {
|
|
13
|
+
for (let i = 0, len = val.length; i < len; ++i) {
|
|
14
|
+
const v0 = val[i];
|
|
15
|
+
const v1 = applyReviver(reviver, val, String(i), v0);
|
|
16
|
+
if (v1 === undefined)
|
|
17
|
+
delete val[i];
|
|
18
|
+
else if (v1 !== v0)
|
|
19
|
+
val[i] = v1;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
else if (val instanceof Map) {
|
|
23
|
+
for (const k of Array.from(val.keys())) {
|
|
24
|
+
const v0 = val.get(k);
|
|
25
|
+
const v1 = applyReviver(reviver, val, k, v0);
|
|
26
|
+
if (v1 === undefined)
|
|
27
|
+
val.delete(k);
|
|
28
|
+
else if (v1 !== v0)
|
|
29
|
+
val.set(k, v1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else if (val instanceof Set) {
|
|
33
|
+
for (const v0 of Array.from(val)) {
|
|
34
|
+
const v1 = applyReviver(reviver, val, v0, v0);
|
|
35
|
+
if (v1 === undefined)
|
|
36
|
+
val.delete(v0);
|
|
37
|
+
else if (v1 !== v0) {
|
|
38
|
+
val.delete(v0);
|
|
39
|
+
val.add(v1);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
for (const [k, v0] of Object.entries(val)) {
|
|
45
|
+
const v1 = applyReviver(reviver, val, k, v0);
|
|
46
|
+
if (v1 === undefined)
|
|
47
|
+
delete val[k];
|
|
48
|
+
else if (v1 !== v0)
|
|
49
|
+
val[k] = v1;
|
|
50
|
+
}
|
|
30
51
|
}
|
|
31
|
-
}
|
|
32
|
-
} else {
|
|
33
|
-
for (const [k, v0] of Object.entries(val)) {
|
|
34
|
-
const v1 = applyReviver(reviver, val, k, v0);
|
|
35
|
-
if (v1 === undefined) delete val[k];else if (v1 !== v0) val[k] = v1;
|
|
36
|
-
}
|
|
37
52
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return reviver.call(obj, key, val);
|
|
53
|
+
return reviver.call(obj, key, val);
|
|
41
54
|
}
|
|
42
55
|
|
|
43
56
|
exports.applyReviver = applyReviver;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Node } from '../nodes/Node.js';
|
|
2
|
+
import type { Schema } from '../schema/Schema.js';
|
|
3
|
+
import type { CollectionTag, ScalarTag } from '../schema/types.js';
|
|
4
|
+
import type { Replacer } from './Document.js';
|
|
5
|
+
export interface CreateNodeContext {
|
|
6
|
+
keepUndefined?: boolean;
|
|
7
|
+
onAnchor(source: unknown): string;
|
|
8
|
+
onTagObj?: (tagObj: ScalarTag | CollectionTag) => void;
|
|
9
|
+
sourceObjects: Map<unknown, {
|
|
10
|
+
anchor: string | null;
|
|
11
|
+
node: Node | null;
|
|
12
|
+
}>;
|
|
13
|
+
replacer?: Replacer;
|
|
14
|
+
schema: Schema;
|
|
15
|
+
}
|
|
16
|
+
export declare function createNode(value: unknown, tagName: string | undefined, ctx: CreateNodeContext): Node;
|
package/dist/doc/createNode.js
CHANGED
|
@@ -1,64 +1,84 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
3
|
+
var Alias = require('../nodes/Alias.js');
|
|
4
|
+
var Node = require('../nodes/Node.js');
|
|
5
|
+
var Scalar = require('../nodes/Scalar.js');
|
|
6
6
|
|
|
7
|
+
const defaultTagPrefix = 'tag:yaml.org,2002:';
|
|
7
8
|
function findTagObject(value, tagName, tags) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
if (tagName) {
|
|
10
|
+
const match = tags.filter(t => t.tag === tagName);
|
|
11
|
+
const tagObj = match.find(t => !t.format) || match[0];
|
|
12
|
+
if (!tagObj)
|
|
13
|
+
throw new Error(`Tag ${tagName} not found`);
|
|
14
|
+
return tagObj;
|
|
15
|
+
}
|
|
16
|
+
return tags.find(t => t.identify && t.identify(value) && !t.format);
|
|
16
17
|
}
|
|
17
|
-
|
|
18
18
|
function createNode(value, tagName, ctx) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
19
|
+
var _a, _b;
|
|
20
|
+
if (Node.isNode(value))
|
|
21
|
+
return value;
|
|
22
|
+
if (Node.isPair(value)) {
|
|
23
|
+
const map = (_b = (_a = ctx.schema[Node.MAP]).createNode) === null || _b === void 0 ? void 0 : _b.call(_a, ctx.schema, null, ctx);
|
|
24
|
+
map.items.push(value);
|
|
25
|
+
return map;
|
|
26
|
+
}
|
|
27
|
+
if (value instanceof String ||
|
|
28
|
+
value instanceof Number ||
|
|
29
|
+
value instanceof Boolean ||
|
|
30
|
+
(typeof BigInt === 'function' && value instanceof BigInt) // not supported everywhere
|
|
31
|
+
) {
|
|
32
|
+
// https://tc39.es/ecma262/#sec-serializejsonproperty
|
|
33
|
+
value = value.valueOf();
|
|
34
|
+
}
|
|
35
|
+
const { onAnchor, onTagObj, schema, sourceObjects } = ctx;
|
|
36
|
+
// Detect duplicate references to the same object & use Alias nodes for all
|
|
37
|
+
// after first. The `ref` wrapper allows for circular references to resolve.
|
|
38
|
+
let ref = undefined;
|
|
39
|
+
if (value && typeof value === 'object') {
|
|
40
|
+
ref = sourceObjects.get(value);
|
|
41
|
+
if (ref) {
|
|
42
|
+
if (!ref.anchor)
|
|
43
|
+
ref.anchor = onAnchor(value);
|
|
44
|
+
return new Alias.Alias(ref.anchor);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
ref = { anchor: null, node: null };
|
|
48
|
+
sourceObjects.set(value, ref);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (tagName && tagName.startsWith('!!'))
|
|
52
|
+
tagName = defaultTagPrefix + tagName.slice(2);
|
|
53
|
+
let tagObj = findTagObject(value, tagName, schema.tags);
|
|
54
|
+
if (!tagObj) {
|
|
55
|
+
if (value && typeof value.toJSON === 'function')
|
|
56
|
+
value = value.toJSON();
|
|
57
|
+
if (!value || typeof value !== 'object') {
|
|
58
|
+
const node = new Scalar.Scalar(value);
|
|
59
|
+
if (ref)
|
|
60
|
+
ref.node = node;
|
|
61
|
+
return node;
|
|
62
|
+
}
|
|
63
|
+
tagObj =
|
|
64
|
+
value instanceof Map
|
|
65
|
+
? schema[Node.MAP]
|
|
66
|
+
: Symbol.iterator in Object(value)
|
|
67
|
+
? schema[Node.SEQ]
|
|
68
|
+
: schema[Node.MAP];
|
|
69
|
+
}
|
|
70
|
+
if (onTagObj) {
|
|
71
|
+
onTagObj(tagObj);
|
|
72
|
+
delete ctx.onTagObj;
|
|
73
|
+
}
|
|
74
|
+
const node = (tagObj === null || tagObj === void 0 ? void 0 : tagObj.createNode)
|
|
75
|
+
? tagObj.createNode(ctx.schema, value, ctx)
|
|
76
|
+
: new Scalar.Scalar(value);
|
|
77
|
+
if (tagName)
|
|
78
|
+
node.tag = tagName;
|
|
79
|
+
if (ref)
|
|
80
|
+
ref.node = node;
|
|
81
|
+
return node;
|
|
62
82
|
}
|
|
63
83
|
|
|
64
84
|
exports.createNode = createNode;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Document } from './Document.js';
|
|
2
|
+
export declare class Directives {
|
|
3
|
+
static defaultYaml: Directives['yaml'];
|
|
4
|
+
static defaultTags: Directives['tags'];
|
|
5
|
+
yaml: {
|
|
6
|
+
version: '1.1' | '1.2';
|
|
7
|
+
explicit?: boolean;
|
|
8
|
+
};
|
|
9
|
+
tags: Record<string, string>;
|
|
10
|
+
/**
|
|
11
|
+
* The directives-end/doc-start marker `---`. If `null`, a marker may still be
|
|
12
|
+
* included in the document's stringified representation.
|
|
13
|
+
*/
|
|
14
|
+
marker: true | null;
|
|
15
|
+
/**
|
|
16
|
+
* Used when parsing YAML 1.1, where:
|
|
17
|
+
* > If the document specifies no directives, it is parsed using the same
|
|
18
|
+
* > settings as the previous document. If the document does specify any
|
|
19
|
+
* > directives, all directives of previous documents, if any, are ignored.
|
|
20
|
+
*/
|
|
21
|
+
private atNextDocument?;
|
|
22
|
+
constructor(yaml?: Directives['yaml'], tags?: Directives['tags']);
|
|
23
|
+
/**
|
|
24
|
+
* During parsing, get a Directives instance for the current document and
|
|
25
|
+
* update the stream state according to the current version's spec.
|
|
26
|
+
*/
|
|
27
|
+
atDocument(): Directives;
|
|
28
|
+
/**
|
|
29
|
+
* @param onError - May be called even if the action was successful
|
|
30
|
+
* @returns `true` on success
|
|
31
|
+
*/
|
|
32
|
+
add(line: string, onError: (offset: number, message: string, warning?: boolean) => void): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Resolves a tag, matching handles to those defined in %TAG directives.
|
|
35
|
+
*
|
|
36
|
+
* @returns Resolved tag, which may also be the non-specific tag `'!'` or a
|
|
37
|
+
* `'!local'` tag, or `null` if unresolvable.
|
|
38
|
+
*/
|
|
39
|
+
tagName(source: string, onError: (message: string) => void): string | null;
|
|
40
|
+
/**
|
|
41
|
+
* Given a fully resolved tag, returns its printable string form,
|
|
42
|
+
* taking into account current tag prefixes and defaults.
|
|
43
|
+
*/
|
|
44
|
+
tagString(tag: string): string;
|
|
45
|
+
toString(doc?: Document): string;
|
|
46
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var Node = require('../nodes/Node.js');
|
|
4
|
+
var visit = require('../visit.js');
|
|
5
|
+
|
|
6
|
+
const escapeChars = {
|
|
7
|
+
'!': '%21',
|
|
8
|
+
',': '%2C',
|
|
9
|
+
'[': '%5B',
|
|
10
|
+
']': '%5D',
|
|
11
|
+
'{': '%7B',
|
|
12
|
+
'}': '%7D'
|
|
13
|
+
};
|
|
14
|
+
const escapeTagName = (tn) => tn.replace(/[!,[\]{}]/g, ch => escapeChars[ch]);
|
|
15
|
+
class Directives {
|
|
16
|
+
constructor(yaml, tags) {
|
|
17
|
+
/**
|
|
18
|
+
* The directives-end/doc-start marker `---`. If `null`, a marker may still be
|
|
19
|
+
* included in the document's stringified representation.
|
|
20
|
+
*/
|
|
21
|
+
this.marker = null;
|
|
22
|
+
this.yaml = Object.assign({}, Directives.defaultYaml, yaml);
|
|
23
|
+
this.tags = Object.assign({}, Directives.defaultTags, tags);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* During parsing, get a Directives instance for the current document and
|
|
27
|
+
* update the stream state according to the current version's spec.
|
|
28
|
+
*/
|
|
29
|
+
atDocument() {
|
|
30
|
+
const res = new Directives(this.yaml, this.tags);
|
|
31
|
+
switch (this.yaml.version) {
|
|
32
|
+
case '1.1':
|
|
33
|
+
this.atNextDocument = true;
|
|
34
|
+
break;
|
|
35
|
+
case '1.2':
|
|
36
|
+
this.atNextDocument = false;
|
|
37
|
+
this.yaml = {
|
|
38
|
+
explicit: Directives.defaultYaml.explicit,
|
|
39
|
+
version: '1.2'
|
|
40
|
+
};
|
|
41
|
+
this.tags = Object.assign({}, Directives.defaultTags);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
return res;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @param onError - May be called even if the action was successful
|
|
48
|
+
* @returns `true` on success
|
|
49
|
+
*/
|
|
50
|
+
add(line, onError) {
|
|
51
|
+
if (this.atNextDocument) {
|
|
52
|
+
this.yaml = { explicit: Directives.defaultYaml.explicit, version: '1.1' };
|
|
53
|
+
this.tags = Object.assign({}, Directives.defaultTags);
|
|
54
|
+
this.atNextDocument = false;
|
|
55
|
+
}
|
|
56
|
+
const parts = line.trim().split(/[ \t]+/);
|
|
57
|
+
const name = parts.shift();
|
|
58
|
+
switch (name) {
|
|
59
|
+
case '%TAG': {
|
|
60
|
+
if (parts.length !== 2) {
|
|
61
|
+
onError(0, '%TAG directive should contain exactly two parts');
|
|
62
|
+
if (parts.length < 2)
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const [handle, prefix] = parts;
|
|
66
|
+
this.tags[handle] = prefix;
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
case '%YAML': {
|
|
70
|
+
this.yaml.explicit = true;
|
|
71
|
+
if (parts.length < 1) {
|
|
72
|
+
onError(0, '%YAML directive should contain exactly one part');
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const [version] = parts;
|
|
76
|
+
if (version === '1.1' || version === '1.2') {
|
|
77
|
+
this.yaml.version = version;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
onError(6, `Unsupported YAML version ${version}`, true);
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
default:
|
|
86
|
+
onError(0, `Unknown directive ${name}`, true);
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Resolves a tag, matching handles to those defined in %TAG directives.
|
|
92
|
+
*
|
|
93
|
+
* @returns Resolved tag, which may also be the non-specific tag `'!'` or a
|
|
94
|
+
* `'!local'` tag, or `null` if unresolvable.
|
|
95
|
+
*/
|
|
96
|
+
tagName(source, onError) {
|
|
97
|
+
if (source === '!')
|
|
98
|
+
return '!'; // non-specific tag
|
|
99
|
+
if (source[0] !== '!') {
|
|
100
|
+
onError(`Not a valid tag: ${source}`);
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
if (source[1] === '<') {
|
|
104
|
+
const verbatim = source.slice(2, -1);
|
|
105
|
+
if (verbatim === '!' || verbatim === '!!') {
|
|
106
|
+
onError(`Verbatim tags aren't resolved, so ${source} is invalid.`);
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
if (source[source.length - 1] !== '>')
|
|
110
|
+
onError('Verbatim tags must end with a >');
|
|
111
|
+
return verbatim;
|
|
112
|
+
}
|
|
113
|
+
const [, handle, suffix] = source.match(/^(.*!)([^!]*)$/);
|
|
114
|
+
if (!suffix)
|
|
115
|
+
onError(`The ${source} tag has no suffix`);
|
|
116
|
+
const prefix = this.tags[handle];
|
|
117
|
+
if (prefix)
|
|
118
|
+
return prefix + decodeURIComponent(suffix);
|
|
119
|
+
if (handle === '!')
|
|
120
|
+
return source; // local tag
|
|
121
|
+
onError(`Could not resolve tag: ${source}`);
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Given a fully resolved tag, returns its printable string form,
|
|
126
|
+
* taking into account current tag prefixes and defaults.
|
|
127
|
+
*/
|
|
128
|
+
tagString(tag) {
|
|
129
|
+
for (const [handle, prefix] of Object.entries(this.tags)) {
|
|
130
|
+
if (tag.startsWith(prefix))
|
|
131
|
+
return handle + escapeTagName(tag.substring(prefix.length));
|
|
132
|
+
}
|
|
133
|
+
return tag[0] === '!' ? tag : `!<${tag}>`;
|
|
134
|
+
}
|
|
135
|
+
toString(doc) {
|
|
136
|
+
const lines = this.yaml.explicit
|
|
137
|
+
? [`%YAML ${this.yaml.version || '1.2'}`]
|
|
138
|
+
: [];
|
|
139
|
+
const tagEntries = Object.entries(this.tags);
|
|
140
|
+
let tagNames;
|
|
141
|
+
if (doc && tagEntries.length > 0 && Node.isNode(doc.contents)) {
|
|
142
|
+
const tags = {};
|
|
143
|
+
visit.visit(doc.contents, (_key, node) => {
|
|
144
|
+
if (Node.isNode(node) && node.tag)
|
|
145
|
+
tags[node.tag] = true;
|
|
146
|
+
});
|
|
147
|
+
tagNames = Object.keys(tags);
|
|
148
|
+
}
|
|
149
|
+
else
|
|
150
|
+
tagNames = [];
|
|
151
|
+
for (const [handle, prefix] of tagEntries) {
|
|
152
|
+
if (handle === '!!' && prefix === 'tag:yaml.org,2002:')
|
|
153
|
+
continue;
|
|
154
|
+
if (!doc || tagNames.some(tn => tn.startsWith(prefix)))
|
|
155
|
+
lines.push(`%TAG ${handle} ${prefix}`);
|
|
156
|
+
}
|
|
157
|
+
return lines.join('\n');
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
Directives.defaultYaml = { explicit: false, version: '1.2' };
|
|
161
|
+
Directives.defaultTags = { '!!': 'tag:yaml.org,2002:' };
|
|
162
|
+
|
|
163
|
+
exports.Directives = Directives;
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { LineCounter } from './parse/line-counter';
|
|
2
|
+
export declare type ErrorCode = 'ALIAS_PROPS' | 'BAD_ALIAS' | 'BAD_DIRECTIVE' | 'BAD_DQ_ESCAPE' | 'BAD_INDENT' | 'BAD_PROP_ORDER' | 'BAD_SCALAR_START' | 'BLOCK_AS_IMPLICIT_KEY' | 'BLOCK_IN_FLOW' | 'DUPLICATE_KEY' | 'IMPOSSIBLE' | 'KEY_OVER_1024_CHARS' | 'MISSING_CHAR' | 'MULTILINE_IMPLICIT_KEY' | 'MULTIPLE_ANCHORS' | 'MULTIPLE_DOCS' | 'MULTIPLE_TAGS' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN';
|
|
3
|
+
export declare type LinePos = {
|
|
4
|
+
line: number;
|
|
5
|
+
col: number;
|
|
6
|
+
};
|
|
7
|
+
export declare class YAMLError extends Error {
|
|
8
|
+
name: 'YAMLParseError' | 'YAMLWarning';
|
|
9
|
+
code: ErrorCode;
|
|
10
|
+
message: string;
|
|
11
|
+
pos: [number, number];
|
|
12
|
+
linePos?: [LinePos] | [LinePos, LinePos];
|
|
13
|
+
constructor(name: YAMLError['name'], pos: [number, number], code: ErrorCode, message: string);
|
|
14
|
+
}
|
|
15
|
+
export declare class YAMLParseError extends YAMLError {
|
|
16
|
+
constructor(pos: [number, number], code: ErrorCode, message: string);
|
|
17
|
+
}
|
|
18
|
+
export declare class YAMLWarning extends YAMLError {
|
|
19
|
+
constructor(pos: [number, number], code: ErrorCode, message: string);
|
|
20
|
+
}
|
|
21
|
+
export declare const prettifyError: (src: string, lc: LineCounter) => (error: YAMLError) => void;
|