yaml 2.0.0-4 → 2.0.0-8
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 +5 -5
- package/browser/dist/compose/compose-collection.js +12 -9
- package/browser/dist/compose/compose-doc.js +23 -6
- package/browser/dist/compose/compose-node.js +27 -17
- package/browser/dist/compose/compose-scalar.js +21 -23
- package/browser/dist/compose/composer.js +108 -78
- package/browser/dist/compose/resolve-block-map.js +41 -29
- package/browser/dist/compose/resolve-block-scalar.js +14 -17
- package/browser/dist/compose/resolve-block-seq.js +21 -17
- package/browser/dist/compose/resolve-end.js +7 -8
- package/browser/dist/compose/resolve-flow-collection.js +158 -199
- package/browser/dist/compose/resolve-flow-scalar.js +62 -26
- package/browser/dist/compose/resolve-props.js +72 -37
- package/browser/dist/compose/util-contains-newline.js +15 -12
- package/browser/dist/compose/util-map-includes.js +17 -0
- package/browser/dist/doc/Document.js +74 -99
- package/browser/dist/doc/anchors.js +72 -0
- package/browser/dist/doc/createNode.js +35 -19
- package/browser/dist/doc/directives.js +5 -0
- package/browser/dist/errors.js +16 -12
- package/browser/dist/index.js +3 -3
- package/browser/dist/nodes/Alias.js +51 -28
- package/browser/dist/nodes/Collection.js +23 -92
- package/browser/dist/nodes/Node.js +9 -1
- package/browser/dist/nodes/Pair.js +16 -231
- package/browser/dist/nodes/YAMLMap.js +8 -4
- package/browser/dist/nodes/YAMLSeq.js +7 -4
- package/browser/dist/nodes/addPairToJSMap.js +103 -0
- package/browser/dist/nodes/toJS.js +12 -10
- package/browser/dist/options.js +1 -2
- 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/{dist/parse/tokens.js → browser/dist/parse/cst.js} +12 -7
- package/browser/dist/parse/lexer.js +149 -125
- package/browser/dist/parse/parser.js +272 -196
- package/browser/dist/public-api.js +11 -14
- package/browser/dist/schema/Schema.js +28 -0
- package/browser/dist/{tags/failsafe → schema/common}/map.js +0 -0
- package/browser/dist/schema/common/null.js +13 -0
- package/browser/dist/{tags/failsafe → schema/common}/seq.js +1 -1
- package/browser/dist/{tags/failsafe → schema/common}/string.js +0 -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/{tags/json.js → schema/json/schema.js} +5 -6
- package/browser/dist/schema/tags.js +79 -0
- package/browser/dist/{tags → schema}/yaml-1.1/binary.js +0 -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/{tags → schema}/yaml-1.1/omap.js +0 -0
- package/browser/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
- package/browser/dist/schema/yaml-1.1/schema.js +37 -0
- package/browser/dist/{tags → schema}/yaml-1.1/set.js +0 -0
- package/browser/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
- package/browser/dist/stringify/stringify.js +8 -6
- 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/stringifyPair.js +111 -0
- package/browser/dist/stringify/stringifyString.js +53 -52
- package/browser/dist/visit.js +20 -1
- package/dist/compose/compose-collection.d.ts +4 -4
- package/dist/compose/compose-collection.js +12 -9
- package/dist/compose/compose-doc.d.ts +4 -3
- package/dist/compose/compose-doc.js +23 -6
- package/dist/compose/compose-node.d.ts +15 -7
- package/dist/compose/compose-node.js +27 -17
- package/dist/compose/compose-scalar.d.ts +4 -3
- package/dist/compose/compose-scalar.js +20 -22
- package/dist/compose/composer.d.ts +28 -23
- package/dist/compose/composer.js +109 -79
- package/dist/compose/resolve-block-map.d.ts +5 -4
- package/dist/compose/resolve-block-map.js +41 -29
- package/dist/compose/resolve-block-scalar.d.ts +5 -3
- package/dist/compose/resolve-block-scalar.js +14 -17
- package/dist/compose/resolve-block-seq.d.ts +4 -4
- package/dist/compose/resolve-block-seq.js +21 -17
- package/dist/compose/resolve-end.d.ts +3 -2
- package/dist/compose/resolve-end.js +7 -8
- package/dist/compose/resolve-flow-collection.d.ts +4 -5
- package/dist/compose/resolve-flow-collection.js +157 -198
- package/dist/compose/resolve-flow-scalar.d.ts +5 -3
- package/dist/compose/resolve-flow-scalar.js +62 -26
- package/dist/compose/resolve-props.d.ts +16 -10
- package/dist/compose/resolve-props.js +72 -37
- package/dist/compose/util-contains-newline.d.ts +1 -1
- package/dist/compose/util-contains-newline.js +15 -12
- package/dist/compose/util-empty-scalar-position.d.ts +1 -1
- 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 +35 -15
- package/dist/doc/Document.js +72 -97
- package/dist/doc/anchors.d.ts +24 -0
- package/dist/doc/anchors.js +77 -0
- package/dist/doc/createNode.d.ts +10 -11
- package/dist/doc/createNode.js +34 -18
- package/dist/doc/directives.d.ts +1 -0
- package/dist/doc/directives.js +5 -0
- package/dist/errors.d.ts +11 -8
- package/dist/errors.js +16 -12
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -3
- package/dist/nodes/Alias.d.ts +19 -9
- package/dist/nodes/Alias.js +51 -28
- package/dist/nodes/Collection.d.ts +9 -17
- package/dist/nodes/Collection.js +22 -91
- package/dist/nodes/Node.d.ts +10 -4
- package/dist/nodes/Node.js +9 -0
- package/dist/nodes/Pair.d.ts +10 -13
- package/dist/nodes/Pair.js +15 -230
- package/dist/nodes/Scalar.d.ts +6 -3
- package/dist/nodes/YAMLMap.d.ts +3 -3
- package/dist/nodes/YAMLMap.js +8 -4
- package/dist/nodes/YAMLSeq.d.ts +3 -3
- package/dist/nodes/YAMLSeq.js +7 -4
- package/dist/nodes/addPairToJSMap.d.ts +3 -0
- package/dist/nodes/addPairToJSMap.js +105 -0
- package/dist/nodes/toJS.d.ts +4 -5
- package/dist/nodes/toJS.js +12 -10
- package/dist/options.d.ts +41 -25
- package/dist/options.js +1 -2
- 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/{tokens.d.ts → cst.d.ts} +23 -7
- package/{browser/dist/parse/tokens.js → dist/parse/cst.js} +27 -1
- package/dist/parse/lexer.d.ts +7 -14
- package/dist/parse/lexer.js +150 -126
- package/dist/parse/parser.d.ts +31 -24
- package/dist/parse/parser.js +274 -198
- package/dist/public-api.d.ts +7 -7
- package/dist/public-api.js +11 -14
- package/dist/{doc → schema}/Schema.d.ts +6 -3
- package/dist/schema/Schema.js +30 -0
- package/dist/{tags/failsafe → schema/common}/map.d.ts +0 -0
- package/dist/{tags/failsafe → schema/common}/map.js +0 -0
- package/dist/schema/common/null.d.ts +4 -0
- package/dist/schema/common/null.js +15 -0
- package/dist/{tags/failsafe → schema/common}/seq.d.ts +0 -0
- package/dist/{tags/failsafe → schema/common}/seq.js +1 -1
- package/dist/{tags/failsafe → schema/common}/string.d.ts +0 -0
- package/dist/{tags/failsafe → schema/common}/string.js +0 -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/{tags/json.js → schema/json/schema.js} +5 -6
- package/dist/schema/tags.d.ts +41 -0
- package/dist/schema/tags.js +82 -0
- package/dist/{tags → schema}/types.d.ts +3 -7
- package/dist/{tags → schema}/yaml-1.1/binary.d.ts +0 -0
- package/dist/{tags → schema}/yaml-1.1/binary.js +0 -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/{tags → schema}/yaml-1.1/omap.d.ts +0 -0
- package/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
- package/dist/{tags → schema}/yaml-1.1/pairs.d.ts +3 -2
- package/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
- package/dist/schema/yaml-1.1/schema.d.ts +1 -0
- package/dist/schema/yaml-1.1/schema.js +39 -0
- package/dist/{tags → schema}/yaml-1.1/set.d.ts +1 -1
- package/dist/{tags → schema}/yaml-1.1/set.js +0 -0
- package/dist/{tags → schema}/yaml-1.1/timestamp.d.ts +0 -0
- package/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
- package/dist/stringify/stringify.d.ts +1 -2
- package/dist/stringify/stringify.js +8 -6
- 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/stringifyPair.d.ts +3 -0
- package/dist/stringify/stringifyPair.js +113 -0
- package/dist/stringify/stringifyString.js +53 -52
- package/dist/test-events.d.ts +1 -2
- package/dist/test-events.js +25 -12
- package/dist/util.d.ts +1 -1
- package/dist/visit.d.ts +10 -1
- package/dist/visit.js +20 -1
- package/package.json +9 -8
- package/browser/dist/doc/Anchors.js +0 -102
- package/browser/dist/doc/Schema.js +0 -27
- package/browser/dist/doc/getSchemaTags.js +0 -30
- package/browser/dist/stringify/addComment.js +0 -17
- package/browser/dist/tags/core.js +0 -109
- package/browser/dist/tags/failsafe/index.js +0 -7
- package/browser/dist/tags/index.js +0 -34
- package/browser/dist/tags/yaml-1.1/index.js +0 -152
- package/dist/doc/Anchors.d.ts +0 -36
- package/dist/doc/Anchors.js +0 -104
- package/dist/doc/Schema.js +0 -29
- package/dist/doc/getSchemaTags.d.ts +0 -3
- package/dist/doc/getSchemaTags.js +0 -32
- package/dist/stringify/addComment.d.ts +0 -2
- package/dist/stringify/addComment.js +0 -20
- package/dist/tags/core.d.ts +0 -14
- package/dist/tags/core.js +0 -119
- package/dist/tags/failsafe/index.d.ts +0 -1
- package/dist/tags/failsafe/index.js +0 -9
- package/dist/tags/index.d.ts +0 -31
- package/dist/tags/index.js +0 -37
- package/dist/tags/json.d.ts +0 -2
- package/dist/tags/yaml-1.1/index.d.ts +0 -2
- package/dist/tags/yaml-1.1/index.js +0 -154
package/dist/options.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { Reviver } from './doc/applyReviver.js';
|
|
2
2
|
import type { Directives } from './doc/directives.js';
|
|
3
|
-
import type { Replacer } from './doc/Document.js';
|
|
4
|
-
import type { SchemaName } from './doc/Schema.js';
|
|
5
3
|
import type { LogLevelId } from './log.js';
|
|
4
|
+
import type { ParsedNode } from './nodes/Node.js';
|
|
6
5
|
import type { Pair } from './nodes/Pair.js';
|
|
7
6
|
import type { Scalar } from './nodes/Scalar.js';
|
|
8
7
|
import type { LineCounter } from './parse/line-counter.js';
|
|
9
|
-
import type {
|
|
8
|
+
import type { SchemaName } from './schema/Schema.js';
|
|
9
|
+
import type { Tags } from './schema/tags.js';
|
|
10
|
+
import type { CollectionTag, ScalarTag } from './schema/types.js';
|
|
10
11
|
export declare type ParseOptions = {
|
|
11
12
|
/**
|
|
12
13
|
* Whether integers should be parsed into BigInt rather than number values.
|
|
@@ -34,26 +35,26 @@ export declare type ParseOptions = {
|
|
|
34
35
|
* Default: `true`
|
|
35
36
|
*/
|
|
36
37
|
strict?: boolean;
|
|
37
|
-
};
|
|
38
|
-
export declare type DocumentOptions = {
|
|
39
38
|
/**
|
|
40
|
-
*
|
|
39
|
+
* YAML requires map keys to be unique. By default, this is checked by
|
|
40
|
+
* comparing scalar values with `===`; deep equality is not checked for
|
|
41
|
+
* aliases or collections. If merge keys are enabled by the schema,
|
|
42
|
+
* multiple `<<` keys are allowed.
|
|
41
43
|
*
|
|
42
|
-
*
|
|
44
|
+
* Set `false` to disable, or provide your own comparator function to
|
|
45
|
+
* customise. The comparator will be passed two `ParsedNode` values, and
|
|
46
|
+
* is expected to return a `boolean` indicating their equality.
|
|
47
|
+
*
|
|
48
|
+
* Default: `true`
|
|
43
49
|
*/
|
|
44
|
-
|
|
50
|
+
uniqueKeys?: boolean | ((a: ParsedNode, b: ParsedNode) => boolean);
|
|
51
|
+
};
|
|
52
|
+
export declare type DocumentOptions = {
|
|
45
53
|
/**
|
|
46
54
|
* Used internally by Composer. If set and includes an explicit version,
|
|
47
55
|
* that overrides the `version` option.
|
|
48
56
|
*/
|
|
49
57
|
directives?: Directives;
|
|
50
|
-
/**
|
|
51
|
-
* Keep `undefined` object values when creating mappings and return a Scalar
|
|
52
|
-
* node when calling `YAML.stringify(undefined)`, rather than `undefined`.
|
|
53
|
-
*
|
|
54
|
-
* Default: `false`
|
|
55
|
-
*/
|
|
56
|
-
keepUndefined?: boolean;
|
|
57
58
|
/**
|
|
58
59
|
* Control the logging level during parsing
|
|
59
60
|
*
|
|
@@ -72,7 +73,7 @@ export declare type SchemaOptions = {
|
|
|
72
73
|
* Array of additional tags to include in the schema, or a function that may
|
|
73
74
|
* modify the schema's base tag array.
|
|
74
75
|
*/
|
|
75
|
-
customTags?:
|
|
76
|
+
customTags?: Tags | ((tags: Tags) => Tags) | null;
|
|
76
77
|
/**
|
|
77
78
|
* Enable support for `<<` merge keys.
|
|
78
79
|
*
|
|
@@ -97,12 +98,26 @@ export declare type SchemaOptions = {
|
|
|
97
98
|
/**
|
|
98
99
|
* When adding to or stringifying a map, sort the entries.
|
|
99
100
|
* If `true`, sort by comparing key values with `<`.
|
|
101
|
+
* Does not affect item order when parsing.
|
|
100
102
|
*
|
|
101
103
|
* Default: `false`
|
|
102
104
|
*/
|
|
103
105
|
sortMapEntries?: boolean | ((a: Pair, b: Pair) => number);
|
|
104
106
|
};
|
|
105
107
|
export declare type CreateNodeOptions = {
|
|
108
|
+
/**
|
|
109
|
+
* During node construction, use anchors and aliases to keep strictly equal
|
|
110
|
+
* non-null objects as equivalent in YAML.
|
|
111
|
+
*
|
|
112
|
+
* Default: `true`
|
|
113
|
+
*/
|
|
114
|
+
aliasDuplicateObjects?: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* Default prefix for anchors.
|
|
117
|
+
*
|
|
118
|
+
* Default: `'a'`, resulting in anchors `a1`, `a2`, etc.
|
|
119
|
+
*/
|
|
120
|
+
anchorPrefix?: string;
|
|
106
121
|
/** Force the top-level collection node to use flow style. */
|
|
107
122
|
flow?: boolean;
|
|
108
123
|
/**
|
|
@@ -114,14 +129,8 @@ export declare type CreateNodeOptions = {
|
|
|
114
129
|
keepUndefined?: boolean | null;
|
|
115
130
|
onTagObj?: (tagObj: ScalarTag | CollectionTag) => void;
|
|
116
131
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
|
|
120
|
-
*/
|
|
121
|
-
replacer?: Replacer;
|
|
122
|
-
/**
|
|
123
|
-
* Specify the collection type, e.g. `"!!omap"`. Note that this requires the
|
|
124
|
-
* corresponding tag to be available in this document's schema.
|
|
132
|
+
* Specify the top-level collection type, e.g. `"!!omap"`. Note that this
|
|
133
|
+
* requires the corresponding tag to be available in this document's schema.
|
|
125
134
|
*/
|
|
126
135
|
tag?: string;
|
|
127
136
|
};
|
|
@@ -257,8 +266,15 @@ export declare type ToStringOptions = {
|
|
|
257
266
|
* Default: `'true'`
|
|
258
267
|
*/
|
|
259
268
|
trueStr?: string;
|
|
269
|
+
/**
|
|
270
|
+
* The anchor used by an alias must be defined before the alias node. As it's
|
|
271
|
+
* possible for the document to be modified manually, the order may be
|
|
272
|
+
* verified during stringification.
|
|
273
|
+
*
|
|
274
|
+
* Default: `'true'`
|
|
275
|
+
*/
|
|
276
|
+
verifyAliasOrder?: boolean;
|
|
260
277
|
};
|
|
261
|
-
export declare type Options = ParseOptions & DocumentOptions & SchemaOptions;
|
|
262
278
|
/**
|
|
263
279
|
* `yaml` defines document-specific options in three places: as an argument of
|
|
264
280
|
* parse, create and stringify calls, in the values of `YAML.defaultOptions`,
|
package/dist/options.js
CHANGED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ErrorCode } from '../errors.js';
|
|
2
|
+
import { Range } from '../nodes/Node.js';
|
|
3
|
+
import type { Scalar } from '../nodes/Scalar.js';
|
|
4
|
+
import type { BlockScalar, FlowScalar, SourceToken, Token } from './cst.js';
|
|
5
|
+
/**
|
|
6
|
+
* If `token` is a CST flow or block scalar, determine its string value and a few other attributes.
|
|
7
|
+
* Otherwise, return `null`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function resolveAsScalar(token: Token | null | undefined, strict?: boolean, onError?: (offset: number, code: ErrorCode, message: string) => void): {
|
|
10
|
+
value: string;
|
|
11
|
+
type: Scalar.Type | null;
|
|
12
|
+
comment: string;
|
|
13
|
+
range: Range;
|
|
14
|
+
} | null;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new scalar token with `value`
|
|
17
|
+
*
|
|
18
|
+
* Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
|
|
19
|
+
* as this function does not support any schema operations and won't check for such conflicts.
|
|
20
|
+
*
|
|
21
|
+
* @param value The string representation of the value, which will have its content properly indented.
|
|
22
|
+
* @param context.end Comments and whitespace after the end of the value, or after the block scalar header. If undefined, a newline will be added.
|
|
23
|
+
* @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
|
|
24
|
+
* @param context.indent The indent level of the token.
|
|
25
|
+
* @param context.inFlow Is this scalar within a flow collection? This may affect the resolved type of the token's value.
|
|
26
|
+
* @param context.offset The offset position of the token.
|
|
27
|
+
* @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function createScalarToken(value: string, context: {
|
|
30
|
+
end?: SourceToken[];
|
|
31
|
+
implicitKey?: boolean;
|
|
32
|
+
indent: number;
|
|
33
|
+
inFlow?: boolean;
|
|
34
|
+
offset?: number;
|
|
35
|
+
type?: Scalar.Type;
|
|
36
|
+
}): BlockScalar | FlowScalar;
|
|
37
|
+
/**
|
|
38
|
+
* Set the value of `token` to the given string `value`, overwriting any previous contents and type that it may have.
|
|
39
|
+
*
|
|
40
|
+
* Best efforts are made to retain any comments previously associated with the `token`,
|
|
41
|
+
* though all contents within a collection's `items` will be overwritten.
|
|
42
|
+
*
|
|
43
|
+
* Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
|
|
44
|
+
* as this function does not support any schema operations and won't check for such conflicts.
|
|
45
|
+
*
|
|
46
|
+
* @param token Any token. If it does not include an `indent` value, the value will be stringified as if it were an implicit key.
|
|
47
|
+
* @param value The string representation of the value, which will have its content properly indented.
|
|
48
|
+
* @param context.afterKey In most cases, values after a key should have an additional level of indentation.
|
|
49
|
+
* @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
|
|
50
|
+
* @param context.inFlow Being within a flow collection may affect the resolved type of the token's value.
|
|
51
|
+
* @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
|
|
52
|
+
*/
|
|
53
|
+
export declare function setScalarValue(token: Token, value: string, context?: {
|
|
54
|
+
afterKey?: boolean;
|
|
55
|
+
implicitKey?: boolean;
|
|
56
|
+
inFlow?: boolean;
|
|
57
|
+
type?: Scalar.Type;
|
|
58
|
+
}): void;
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var resolveBlockScalar = require('../compose/resolve-block-scalar.js');
|
|
4
|
+
var resolveFlowScalar = require('../compose/resolve-flow-scalar.js');
|
|
5
|
+
var errors = require('../errors.js');
|
|
6
|
+
var stringifyString = require('../stringify/stringifyString.js');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* If `token` is a CST flow or block scalar, determine its string value and a few other attributes.
|
|
10
|
+
* Otherwise, return `null`.
|
|
11
|
+
*/
|
|
12
|
+
function resolveAsScalar(token, strict = true, onError) {
|
|
13
|
+
if (token) {
|
|
14
|
+
const _onError = (pos, code, message) => {
|
|
15
|
+
const offset = typeof pos === 'number' ? pos : Array.isArray(pos) ? pos[0] : pos.offset;
|
|
16
|
+
if (onError)
|
|
17
|
+
onError(offset, code, message);
|
|
18
|
+
else
|
|
19
|
+
throw new errors.YAMLParseError([offset, offset + 1], code, message);
|
|
20
|
+
};
|
|
21
|
+
switch (token.type) {
|
|
22
|
+
case 'scalar':
|
|
23
|
+
case 'single-quoted-scalar':
|
|
24
|
+
case 'double-quoted-scalar':
|
|
25
|
+
return resolveFlowScalar.resolveFlowScalar(token, strict, _onError);
|
|
26
|
+
case 'block-scalar':
|
|
27
|
+
return resolveBlockScalar.resolveBlockScalar(token, strict, _onError);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Create a new scalar token with `value`
|
|
34
|
+
*
|
|
35
|
+
* Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
|
|
36
|
+
* as this function does not support any schema operations and won't check for such conflicts.
|
|
37
|
+
*
|
|
38
|
+
* @param value The string representation of the value, which will have its content properly indented.
|
|
39
|
+
* @param context.end Comments and whitespace after the end of the value, or after the block scalar header. If undefined, a newline will be added.
|
|
40
|
+
* @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
|
|
41
|
+
* @param context.indent The indent level of the token.
|
|
42
|
+
* @param context.inFlow Is this scalar within a flow collection? This may affect the resolved type of the token's value.
|
|
43
|
+
* @param context.offset The offset position of the token.
|
|
44
|
+
* @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
|
|
45
|
+
*/
|
|
46
|
+
function createScalarToken(value, context) {
|
|
47
|
+
var _a;
|
|
48
|
+
const { implicitKey = false, indent, inFlow = false, offset = -1, type = 'PLAIN' } = context;
|
|
49
|
+
const source = stringifyString.stringifyString({ type, value }, {
|
|
50
|
+
implicitKey,
|
|
51
|
+
indent: indent > 0 ? ' '.repeat(indent) : '',
|
|
52
|
+
inFlow,
|
|
53
|
+
options: { lineWidth: -1 }
|
|
54
|
+
});
|
|
55
|
+
const end = (_a = context.end) !== null && _a !== void 0 ? _a : [
|
|
56
|
+
{ type: 'newline', offset: -1, indent, source: '\n' }
|
|
57
|
+
];
|
|
58
|
+
switch (source[0]) {
|
|
59
|
+
case '|':
|
|
60
|
+
case '>': {
|
|
61
|
+
const he = source.indexOf('\n');
|
|
62
|
+
const head = source.substring(0, he);
|
|
63
|
+
const body = source.substring(he + 1) + '\n';
|
|
64
|
+
const props = [
|
|
65
|
+
{ type: 'block-scalar-header', offset, indent, source: head }
|
|
66
|
+
];
|
|
67
|
+
if (!addEndtoBlockProps(props, end))
|
|
68
|
+
props.push({ type: 'newline', offset: -1, indent, source: '\n' });
|
|
69
|
+
return { type: 'block-scalar', offset, indent, props, source: body };
|
|
70
|
+
}
|
|
71
|
+
case '"':
|
|
72
|
+
return { type: 'double-quoted-scalar', offset, indent, source, end };
|
|
73
|
+
case "'":
|
|
74
|
+
return { type: 'single-quoted-scalar', offset, indent, source, end };
|
|
75
|
+
default:
|
|
76
|
+
return { type: 'scalar', offset, indent, source, end };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Set the value of `token` to the given string `value`, overwriting any previous contents and type that it may have.
|
|
81
|
+
*
|
|
82
|
+
* Best efforts are made to retain any comments previously associated with the `token`,
|
|
83
|
+
* though all contents within a collection's `items` will be overwritten.
|
|
84
|
+
*
|
|
85
|
+
* Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
|
|
86
|
+
* as this function does not support any schema operations and won't check for such conflicts.
|
|
87
|
+
*
|
|
88
|
+
* @param token Any token. If it does not include an `indent` value, the value will be stringified as if it were an implicit key.
|
|
89
|
+
* @param value The string representation of the value, which will have its content properly indented.
|
|
90
|
+
* @param context.afterKey In most cases, values after a key should have an additional level of indentation.
|
|
91
|
+
* @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
|
|
92
|
+
* @param context.inFlow Being within a flow collection may affect the resolved type of the token's value.
|
|
93
|
+
* @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
|
|
94
|
+
*/
|
|
95
|
+
function setScalarValue(token, value, context = {}) {
|
|
96
|
+
let { afterKey = false, implicitKey = false, inFlow = false, type } = context;
|
|
97
|
+
let indent = 'indent' in token ? token.indent : null;
|
|
98
|
+
if (afterKey && typeof indent === 'number')
|
|
99
|
+
indent += 2;
|
|
100
|
+
if (!type)
|
|
101
|
+
switch (token.type) {
|
|
102
|
+
case 'single-quoted-scalar':
|
|
103
|
+
type = 'QUOTE_SINGLE';
|
|
104
|
+
break;
|
|
105
|
+
case 'double-quoted-scalar':
|
|
106
|
+
type = 'QUOTE_DOUBLE';
|
|
107
|
+
break;
|
|
108
|
+
case 'block-scalar': {
|
|
109
|
+
const header = token.props[0];
|
|
110
|
+
if (header.type !== 'block-scalar-header')
|
|
111
|
+
throw new Error('Invalid block scalar header');
|
|
112
|
+
type = header.source[0] === '>' ? 'BLOCK_FOLDED' : 'BLOCK_LITERAL';
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
default:
|
|
116
|
+
type = 'PLAIN';
|
|
117
|
+
}
|
|
118
|
+
const source = stringifyString.stringifyString({ type, value }, {
|
|
119
|
+
implicitKey: implicitKey || indent === null,
|
|
120
|
+
indent: indent !== null && indent > 0 ? ' '.repeat(indent) : '',
|
|
121
|
+
inFlow,
|
|
122
|
+
options: { lineWidth: -1 }
|
|
123
|
+
});
|
|
124
|
+
switch (source[0]) {
|
|
125
|
+
case '|':
|
|
126
|
+
case '>':
|
|
127
|
+
setBlockScalarValue(token, source);
|
|
128
|
+
break;
|
|
129
|
+
case '"':
|
|
130
|
+
setFlowScalarValue(token, source, 'double-quoted-scalar');
|
|
131
|
+
break;
|
|
132
|
+
case "'":
|
|
133
|
+
setFlowScalarValue(token, source, 'single-quoted-scalar');
|
|
134
|
+
break;
|
|
135
|
+
default:
|
|
136
|
+
setFlowScalarValue(token, source, 'scalar');
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function setBlockScalarValue(token, source) {
|
|
140
|
+
const he = source.indexOf('\n');
|
|
141
|
+
const head = source.substring(0, he);
|
|
142
|
+
const body = source.substring(he + 1) + '\n';
|
|
143
|
+
if (token.type === 'block-scalar') {
|
|
144
|
+
const header = token.props[0];
|
|
145
|
+
if (header.type !== 'block-scalar-header')
|
|
146
|
+
throw new Error('Invalid block scalar header');
|
|
147
|
+
header.source = head;
|
|
148
|
+
token.source = body;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const { offset } = token;
|
|
152
|
+
const indent = 'indent' in token ? token.indent : -1;
|
|
153
|
+
const props = [
|
|
154
|
+
{ type: 'block-scalar-header', offset, indent, source: head }
|
|
155
|
+
];
|
|
156
|
+
if (!addEndtoBlockProps(props, 'end' in token ? token.end : undefined))
|
|
157
|
+
props.push({ type: 'newline', offset: -1, indent, source: '\n' });
|
|
158
|
+
for (const key of Object.keys(token))
|
|
159
|
+
if (key !== 'type' && key !== 'offset')
|
|
160
|
+
delete token[key];
|
|
161
|
+
Object.assign(token, { type: 'block-scalar', indent, props, source: body });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/** @returns `true` if last token is a newline */
|
|
165
|
+
function addEndtoBlockProps(props, end) {
|
|
166
|
+
if (end)
|
|
167
|
+
for (const st of end)
|
|
168
|
+
switch (st.type) {
|
|
169
|
+
case 'space':
|
|
170
|
+
case 'comment':
|
|
171
|
+
props.push(st);
|
|
172
|
+
break;
|
|
173
|
+
case 'newline':
|
|
174
|
+
props.push(st);
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
function setFlowScalarValue(token, source, type) {
|
|
180
|
+
switch (token.type) {
|
|
181
|
+
case 'scalar':
|
|
182
|
+
case 'double-quoted-scalar':
|
|
183
|
+
case 'single-quoted-scalar':
|
|
184
|
+
token.type = type;
|
|
185
|
+
token.source = source;
|
|
186
|
+
break;
|
|
187
|
+
case 'block-scalar': {
|
|
188
|
+
const end = token.props.slice(1);
|
|
189
|
+
let oa = source.length;
|
|
190
|
+
if (token.props[0].type === 'block-scalar-header')
|
|
191
|
+
oa -= token.props[0].source.length;
|
|
192
|
+
for (const tok of end)
|
|
193
|
+
tok.offset += oa;
|
|
194
|
+
delete token.props;
|
|
195
|
+
Object.assign(token, { type, source, end });
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
case 'block-map':
|
|
199
|
+
case 'block-seq': {
|
|
200
|
+
const offset = token.offset + source.length;
|
|
201
|
+
const nl = { type: 'newline', offset, indent: token.indent, source: '\n' };
|
|
202
|
+
delete token.items;
|
|
203
|
+
Object.assign(token, { type, source, end: [nl] });
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
default: {
|
|
207
|
+
const indent = 'indent' in token ? token.indent : -1;
|
|
208
|
+
const end = 'end' in token && Array.isArray(token.end)
|
|
209
|
+
? token.end.filter(st => st.type === 'space' ||
|
|
210
|
+
st.type === 'comment' ||
|
|
211
|
+
st.type === 'newline')
|
|
212
|
+
: [];
|
|
213
|
+
for (const key of Object.keys(token))
|
|
214
|
+
if (key !== 'type' && key !== 'offset')
|
|
215
|
+
delete token[key];
|
|
216
|
+
Object.assign(token, { type, indent, source, end });
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
exports.createScalarToken = createScalarToken;
|
|
222
|
+
exports.resolveAsScalar = resolveAsScalar;
|
|
223
|
+
exports.setScalarValue = setScalarValue;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CollectionItem, Token } from './cst.js';
|
|
2
|
+
/**
|
|
3
|
+
* Stringify a CST document, token, or collection item
|
|
4
|
+
*
|
|
5
|
+
* Fair warning: This applies no validation whatsoever, and
|
|
6
|
+
* simply concatenates the sources in their logical order.
|
|
7
|
+
*/
|
|
8
|
+
export declare const stringify: (cst: Token | CollectionItem) => string;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Stringify a CST document, token, or collection item
|
|
5
|
+
*
|
|
6
|
+
* Fair warning: This applies no validation whatsoever, and
|
|
7
|
+
* simply concatenates the sources in their logical order.
|
|
8
|
+
*/
|
|
9
|
+
const stringify = (cst) => 'type' in cst ? stringifyToken(cst) : stringifyItem(cst);
|
|
10
|
+
function stringifyToken(token) {
|
|
11
|
+
switch (token.type) {
|
|
12
|
+
case 'block-scalar': {
|
|
13
|
+
let res = '';
|
|
14
|
+
for (const tok of token.props)
|
|
15
|
+
res += stringifyToken(tok);
|
|
16
|
+
return res + token.source;
|
|
17
|
+
}
|
|
18
|
+
case 'block-map':
|
|
19
|
+
case 'block-seq': {
|
|
20
|
+
let res = '';
|
|
21
|
+
for (const item of token.items)
|
|
22
|
+
res += stringifyItem(item);
|
|
23
|
+
return res;
|
|
24
|
+
}
|
|
25
|
+
case 'flow-collection': {
|
|
26
|
+
let res = token.start.source;
|
|
27
|
+
for (const item of token.items)
|
|
28
|
+
res += stringifyItem(item);
|
|
29
|
+
for (const st of token.end)
|
|
30
|
+
res += st.source;
|
|
31
|
+
return res;
|
|
32
|
+
}
|
|
33
|
+
case 'document': {
|
|
34
|
+
let res = stringifyItem(token);
|
|
35
|
+
if (token.end)
|
|
36
|
+
for (const st of token.end)
|
|
37
|
+
res += st.source;
|
|
38
|
+
return res;
|
|
39
|
+
}
|
|
40
|
+
default: {
|
|
41
|
+
let res = token.source;
|
|
42
|
+
if ('end' in token && token.end)
|
|
43
|
+
for (const st of token.end)
|
|
44
|
+
res += st.source;
|
|
45
|
+
return res;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function stringifyItem({ start, key, sep, value }) {
|
|
50
|
+
let res = '';
|
|
51
|
+
for (const st of start)
|
|
52
|
+
res += st.source;
|
|
53
|
+
if (key)
|
|
54
|
+
res += stringifyToken(key);
|
|
55
|
+
if (sep)
|
|
56
|
+
for (const st of sep)
|
|
57
|
+
res += st.source;
|
|
58
|
+
if (value)
|
|
59
|
+
res += stringifyToken(value);
|
|
60
|
+
return res;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
exports.stringify = stringify;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { CollectionItem, Document } from './cst.js';
|
|
2
|
+
export declare type VisitPath = readonly ['key' | 'value', number][];
|
|
3
|
+
export declare type Visitor = (item: CollectionItem, path: VisitPath) => number | symbol | Visitor | void;
|
|
4
|
+
/**
|
|
5
|
+
* Apply a visitor to a CST document or item.
|
|
6
|
+
*
|
|
7
|
+
* Walks through the tree (depth-first) starting from the root, calling a
|
|
8
|
+
* `visitor` function with two arguments when entering each item:
|
|
9
|
+
* - `item`: The current item, which included the following members:
|
|
10
|
+
* - `start: SourceToken[]` – Source tokens before the key or value,
|
|
11
|
+
* possibly including its anchor or tag.
|
|
12
|
+
* - `key?: Token | null` – Set for pair values. May then be `null`, if
|
|
13
|
+
* the key before the `:` separator is empty.
|
|
14
|
+
* - `sep?: SourceToken[]` – Source tokens between the key and the value,
|
|
15
|
+
* which should include the `:` map value indicator if `value` is set.
|
|
16
|
+
* - `value?: Token` – The value of a sequence item, or of a map pair.
|
|
17
|
+
* - `path`: The steps from the root to the current node, as an array of
|
|
18
|
+
* `['key' | 'value', number]` tuples.
|
|
19
|
+
*
|
|
20
|
+
* The return value of the visitor may be used to control the traversal:
|
|
21
|
+
* - `undefined` (default): Do nothing and continue
|
|
22
|
+
* - `visit.SKIP`: Do not visit the children of this token, continue with
|
|
23
|
+
* next sibling
|
|
24
|
+
* - `visit.BREAK`: Terminate traversal completely
|
|
25
|
+
* - `visit.REMOVE`: Remove the current item, then continue with the next one
|
|
26
|
+
* - `number`: Set the index of the next step. This is useful especially if
|
|
27
|
+
* the index of the current token has changed.
|
|
28
|
+
* - `function`: Define the next visitor for this item. After the original
|
|
29
|
+
* visitor is called on item entry, next visitors are called after handling
|
|
30
|
+
* a non-empty `key` and when exiting the item.
|
|
31
|
+
*/
|
|
32
|
+
export declare function visit(cst: Document | CollectionItem, visitor: Visitor): void;
|
|
33
|
+
export declare namespace visit {
|
|
34
|
+
var BREAK: symbol;
|
|
35
|
+
var SKIP: symbol;
|
|
36
|
+
var REMOVE: symbol;
|
|
37
|
+
var itemAtPath: (cst: Document | CollectionItem, path: VisitPath) => CollectionItem | undefined;
|
|
38
|
+
var parentCollection: (cst: Document | CollectionItem, path: VisitPath) => import("./cst.js").BlockMap | import("./cst.js").BlockSequence | import("./cst.js").FlowCollection;
|
|
39
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BREAK = Symbol('break visit');
|
|
4
|
+
const SKIP = Symbol('skip children');
|
|
5
|
+
const REMOVE = Symbol('remove item');
|
|
6
|
+
/**
|
|
7
|
+
* Apply a visitor to a CST document or item.
|
|
8
|
+
*
|
|
9
|
+
* Walks through the tree (depth-first) starting from the root, calling a
|
|
10
|
+
* `visitor` function with two arguments when entering each item:
|
|
11
|
+
* - `item`: The current item, which included the following members:
|
|
12
|
+
* - `start: SourceToken[]` – Source tokens before the key or value,
|
|
13
|
+
* possibly including its anchor or tag.
|
|
14
|
+
* - `key?: Token | null` – Set for pair values. May then be `null`, if
|
|
15
|
+
* the key before the `:` separator is empty.
|
|
16
|
+
* - `sep?: SourceToken[]` – Source tokens between the key and the value,
|
|
17
|
+
* which should include the `:` map value indicator if `value` is set.
|
|
18
|
+
* - `value?: Token` – The value of a sequence item, or of a map pair.
|
|
19
|
+
* - `path`: The steps from the root to the current node, as an array of
|
|
20
|
+
* `['key' | 'value', number]` tuples.
|
|
21
|
+
*
|
|
22
|
+
* The return value of the visitor may be used to control the traversal:
|
|
23
|
+
* - `undefined` (default): Do nothing and continue
|
|
24
|
+
* - `visit.SKIP`: Do not visit the children of this token, continue with
|
|
25
|
+
* next sibling
|
|
26
|
+
* - `visit.BREAK`: Terminate traversal completely
|
|
27
|
+
* - `visit.REMOVE`: Remove the current item, then continue with the next one
|
|
28
|
+
* - `number`: Set the index of the next step. This is useful especially if
|
|
29
|
+
* the index of the current token has changed.
|
|
30
|
+
* - `function`: Define the next visitor for this item. After the original
|
|
31
|
+
* visitor is called on item entry, next visitors are called after handling
|
|
32
|
+
* a non-empty `key` and when exiting the item.
|
|
33
|
+
*/
|
|
34
|
+
function visit(cst, visitor) {
|
|
35
|
+
if ('type' in cst && cst.type === 'document')
|
|
36
|
+
cst = { start: cst.start, value: cst.value };
|
|
37
|
+
_visit(Object.freeze([]), cst, visitor);
|
|
38
|
+
}
|
|
39
|
+
// Without the `as symbol` casts, TS declares these in the `visit`
|
|
40
|
+
// namespace using `var`, but then complains about that because
|
|
41
|
+
// `unique symbol` must be `const`.
|
|
42
|
+
/** Terminate visit traversal completely */
|
|
43
|
+
visit.BREAK = BREAK;
|
|
44
|
+
/** Do not visit the children of the current item */
|
|
45
|
+
visit.SKIP = SKIP;
|
|
46
|
+
/** Remove the current item */
|
|
47
|
+
visit.REMOVE = REMOVE;
|
|
48
|
+
/** Find the item at `path` from `cst` as the root */
|
|
49
|
+
visit.itemAtPath = (cst, path) => {
|
|
50
|
+
let item = cst;
|
|
51
|
+
for (const [field, index] of path) {
|
|
52
|
+
const tok = item && item[field];
|
|
53
|
+
if (tok && 'items' in tok) {
|
|
54
|
+
item = tok.items[index];
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return item;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Get the immediate parent collection of the item at `path` from `cst` as the root.
|
|
63
|
+
*
|
|
64
|
+
* Throws an error if the collection is not found, which should never happen if the item itself exists.
|
|
65
|
+
*/
|
|
66
|
+
visit.parentCollection = (cst, path) => {
|
|
67
|
+
const parent = visit.itemAtPath(cst, path.slice(0, -1));
|
|
68
|
+
const field = path[path.length - 1][0];
|
|
69
|
+
const coll = parent && parent[field];
|
|
70
|
+
if (coll && 'items' in coll)
|
|
71
|
+
return coll;
|
|
72
|
+
throw new Error('Parent collection not found');
|
|
73
|
+
};
|
|
74
|
+
function _visit(path, item, visitor) {
|
|
75
|
+
let ctrl = visitor(item, path);
|
|
76
|
+
if (typeof ctrl === 'symbol')
|
|
77
|
+
return ctrl;
|
|
78
|
+
for (const field of ['key', 'value']) {
|
|
79
|
+
const token = item[field];
|
|
80
|
+
if (token && 'items' in token) {
|
|
81
|
+
for (let i = 0; i < token.items.length; ++i) {
|
|
82
|
+
const ci = _visit(Object.freeze(path.concat([[field, i]])), token.items[i], visitor);
|
|
83
|
+
if (typeof ci === 'number')
|
|
84
|
+
i = ci - 1;
|
|
85
|
+
else if (ci === BREAK)
|
|
86
|
+
return BREAK;
|
|
87
|
+
else if (ci === REMOVE) {
|
|
88
|
+
token.items.splice(i, 1);
|
|
89
|
+
i -= 1;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (typeof ctrl === 'function' && field === 'key')
|
|
93
|
+
ctrl = ctrl(item, path);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return typeof ctrl === 'function' ? ctrl(item, path) : ctrl;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
exports.visit = visit;
|