yaml 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +17 -3
  2. package/browser/dist/compose/compose-doc.js +1 -0
  3. package/browser/dist/compose/compose-node.js +10 -0
  4. package/browser/dist/compose/compose-scalar.js +14 -8
  5. package/browser/dist/compose/resolve-block-map.js +2 -0
  6. package/browser/dist/compose/resolve-block-seq.js +2 -0
  7. package/browser/dist/compose/resolve-flow-collection.js +6 -0
  8. package/browser/dist/compose/resolve-flow-scalar.js +1 -1
  9. package/browser/dist/compose/util-map-includes.js +1 -5
  10. package/browser/dist/doc/Document.js +3 -2
  11. package/browser/dist/doc/applyReviver.js +1 -0
  12. package/browser/dist/log.js +0 -2
  13. package/browser/dist/nodes/addPairToJSMap.js +7 -49
  14. package/browser/dist/parse/parser.js +3 -1
  15. package/browser/dist/public-api.js +3 -0
  16. package/browser/dist/schema/Schema.js +1 -2
  17. package/browser/dist/schema/tags.js +26 -13
  18. package/browser/dist/schema/yaml-1.1/merge.js +64 -0
  19. package/browser/dist/schema/yaml-1.1/schema.js +2 -0
  20. package/browser/dist/stringify/stringify.js +6 -1
  21. package/dist/compose/compose-doc.js +1 -0
  22. package/dist/compose/compose-node.d.ts +1 -0
  23. package/dist/compose/compose-node.js +10 -0
  24. package/dist/compose/compose-scalar.js +13 -7
  25. package/dist/compose/resolve-block-map.js +2 -0
  26. package/dist/compose/resolve-block-seq.js +2 -0
  27. package/dist/compose/resolve-flow-collection.js +6 -0
  28. package/dist/compose/resolve-flow-scalar.js +1 -1
  29. package/dist/compose/util-map-includes.js +1 -5
  30. package/dist/doc/Document.js +3 -2
  31. package/dist/doc/applyReviver.js +1 -0
  32. package/dist/errors.d.ts +1 -1
  33. package/dist/log.js +0 -2
  34. package/dist/nodes/Node.d.ts +8 -1
  35. package/dist/nodes/addPairToJSMap.js +6 -48
  36. package/dist/options.d.ts +6 -0
  37. package/dist/parse/lexer.d.ts +1 -1
  38. package/dist/parse/parser.d.ts +3 -3
  39. package/dist/parse/parser.js +3 -1
  40. package/dist/public-api.js +3 -0
  41. package/dist/schema/Schema.d.ts +0 -1
  42. package/dist/schema/Schema.js +1 -2
  43. package/dist/schema/tags.d.ts +9 -1
  44. package/dist/schema/tags.js +26 -13
  45. package/dist/schema/types.d.ts +6 -4
  46. package/dist/schema/yaml-1.1/merge.d.ts +9 -0
  47. package/dist/schema/yaml-1.1/merge.js +68 -0
  48. package/dist/schema/yaml-1.1/schema.js +2 -0
  49. package/dist/stringify/stringify.js +6 -1
  50. package/package.json +6 -8
  51. package/browser/dist/node_modules/tslib/tslib.es6.js +0 -21
@@ -20,6 +20,8 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
20
20
  const atRoot = ctx.atRoot;
21
21
  if (atRoot)
22
22
  ctx.atRoot = false;
23
+ if (ctx.atKey)
24
+ ctx.atKey = false;
23
25
  let offset = fc.offset + fc.start.source.length;
24
26
  for (let i = 0; i < fc.items.length; ++i) {
25
27
  const collItem = fc.items[i];
@@ -99,12 +101,14 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
99
101
  else {
100
102
  // item is a key+value pair
101
103
  // key value
104
+ ctx.atKey = true;
102
105
  const keyStart = props.end;
103
106
  const keyNode = key
104
107
  ? composeNode(ctx, key, props, onError)
105
108
  : composeEmptyNode(ctx, keyStart, start, null, props, onError);
106
109
  if (isBlock(key))
107
110
  onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
111
+ ctx.atKey = false;
108
112
  // value properties
109
113
  const valueProps = resolveProps.resolveProps(sep ?? [], {
110
114
  flow: fcName,
@@ -165,6 +169,8 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr
165
169
  const map = new YAMLMap.YAMLMap(ctx.schema);
166
170
  map.flow = true;
167
171
  map.items.push(pair);
172
+ const endRange = (valueNode ?? keyNode).range;
173
+ map.range = [keyNode.range[0], endRange[1], endRange[2]];
168
174
  coll.items.push(map);
169
175
  }
170
176
  offset = valueNode ? valueNode.range[2] : valueProps.end;
@@ -86,7 +86,7 @@ function foldLines(source) {
86
86
  first = new RegExp('(.*?)(?<![ \t])[ \t]*\r?\n', 'sy');
87
87
  line = new RegExp('[ \t]*(.*?)(?:(?<![ \t])[ \t]*)?\r?\n', 'sy');
88
88
  }
89
- catch (_) {
89
+ catch {
90
90
  first = /(.*?)[ \t]*\r?\n/sy;
91
91
  line = /[ \t]*(.*?)[ \t]*\r?\n/sy;
92
92
  }
@@ -8,11 +8,7 @@ function mapIncludes(ctx, items, search) {
8
8
  return false;
9
9
  const isEqual = typeof uniqueKeys === 'function'
10
10
  ? uniqueKeys
11
- : (a, b) => a === b ||
12
- (identity.isScalar(a) &&
13
- identity.isScalar(b) &&
14
- a.value === b.value &&
15
- !(a.value === '<<' && ctx.schema.merge));
11
+ : (a, b) => a === b || (identity.isScalar(a) && identity.isScalar(b) && a.value === b.value);
16
12
  return items.some(pair => isEqual(pair.key, search));
17
13
  }
18
14
 
@@ -37,6 +37,7 @@ class Document {
37
37
  logLevel: 'warn',
38
38
  prettyErrors: true,
39
39
  strict: true,
40
+ stringKeys: false,
40
41
  uniqueKeys: true,
41
42
  version: '1.2'
42
43
  }, options);
@@ -260,7 +261,7 @@ class Document {
260
261
  this.directives.yaml.version = '1.1';
261
262
  else
262
263
  this.directives = new directives.Directives({ version: '1.1' });
263
- opt = { merge: true, resolveKnownTags: false, schema: 'yaml-1.1' };
264
+ opt = { resolveKnownTags: false, schema: 'yaml-1.1' };
264
265
  break;
265
266
  case '1.2':
266
267
  case 'next':
@@ -268,7 +269,7 @@ class Document {
268
269
  this.directives.yaml.version = version;
269
270
  else
270
271
  this.directives = new directives.Directives({ version });
271
- opt = { merge: false, resolveKnownTags: true, schema: 'core' };
272
+ opt = { resolveKnownTags: true, schema: 'core' };
272
273
  break;
273
274
  case null:
274
275
  if (this.directives)
@@ -13,6 +13,7 @@ function applyReviver(reviver, obj, key, val) {
13
13
  for (let i = 0, len = val.length; i < len; ++i) {
14
14
  const v0 = val[i];
15
15
  const v1 = applyReviver(reviver, val, String(i), v0);
16
+ // eslint-disable-next-line @typescript-eslint/no-array-delete
16
17
  if (v1 === undefined)
17
18
  delete val[i];
18
19
  else if (v1 !== v0)
package/dist/errors.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { LineCounter } from './parse/line-counter';
2
- export 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' | 'BAD_COLLECTION_TYPE';
2
+ export 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' | 'NON_STRING_KEY' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN' | 'BAD_COLLECTION_TYPE';
3
3
  export type LinePos = {
4
4
  line: number;
5
5
  col: number;
package/dist/log.js CHANGED
@@ -6,8 +6,6 @@ function debug(logLevel, ...messages) {
6
6
  }
7
7
  function warn(logLevel, warning) {
8
8
  if (logLevel === 'debug' || logLevel === 'warn') {
9
- // https://github.com/typescript-eslint/typescript-eslint/issues/7478
10
- // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
11
9
  if (typeof process !== 'undefined' && process.emitWarning)
12
10
  process.emitWarning(warning);
13
11
  else
@@ -5,7 +5,8 @@ import type { StringifyContext } from '../stringify/stringify.js';
5
5
  import type { Alias } from './Alias.js';
6
6
  import { NODE_TYPE } from './identity.js';
7
7
  import type { Scalar } from './Scalar.js';
8
- import type { YAMLMap } from './YAMLMap.js';
8
+ import { ToJSContext } from './toJS.js';
9
+ import type { MapLike, YAMLMap } from './YAMLMap.js';
9
10
  import type { YAMLSeq } from './YAMLSeq.js';
10
11
  export type Node<T = unknown> = Alias | Scalar<T> | YAMLMap<unknown, T> | YAMLSeq<T>;
11
12
  /** Utility type mapper */
@@ -15,6 +16,7 @@ export type NodeType<T> = T extends string | number | bigint | boolean | null |
15
16
  [key: number]: any;
16
17
  } ? YAMLMap<NodeType<keyof T>, NodeType<T[keyof T]>> : Node;
17
18
  export type ParsedNode = Alias.Parsed | Scalar.Parsed | YAMLMap.Parsed | YAMLSeq.Parsed;
19
+ /** `[start, value-end, node-end]` */
18
20
  export type Range = [number, number, number];
19
21
  export declare abstract class NodeBase {
20
22
  readonly [NODE_TYPE]: symbol;
@@ -35,6 +37,11 @@ export declare abstract class NodeBase {
35
37
  srcToken?: Token;
36
38
  /** A fully qualified tag, if required */
37
39
  tag?: string;
40
+ /**
41
+ * Customize the way that a key-value pair is resolved.
42
+ * Used for YAML 1.1 !!merge << handling.
43
+ */
44
+ addToJSMap?: (ctx: ToJSContext | undefined, map: MapLike, value: unknown) => void;
38
45
  /** A plain JS representation of this node */
39
46
  abstract toJSON(): any;
40
47
  abstract toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
@@ -1,24 +1,17 @@
1
1
  'use strict';
2
2
 
3
3
  var log = require('../log.js');
4
+ var merge = require('../schema/yaml-1.1/merge.js');
4
5
  var stringify = require('../stringify/stringify.js');
5
6
  var identity = require('./identity.js');
6
- var Scalar = require('./Scalar.js');
7
7
  var toJS = require('./toJS.js');
8
8
 
9
- const MERGE_KEY = '<<';
10
9
  function addPairToJSMap(ctx, map, { key, value }) {
11
- if (ctx?.doc.schema.merge && isMergeKey(key)) {
12
- value = identity.isAlias(value) ? value.resolve(ctx.doc) : value;
13
- if (identity.isSeq(value))
14
- for (const it of value.items)
15
- mergeToJSMap(ctx, map, it);
16
- else if (Array.isArray(value))
17
- for (const it of value)
18
- mergeToJSMap(ctx, map, it);
19
- else
20
- mergeToJSMap(ctx, map, value);
21
- }
10
+ if (identity.isNode(key) && key.addToJSMap)
11
+ key.addToJSMap(ctx, map, value);
12
+ // TODO: Should drop this special case for bare << handling
13
+ else if (merge.isMergeKey(ctx, key))
14
+ merge.addMergeToJSMap(ctx, map, value);
22
15
  else {
23
16
  const jsKey = toJS.toJS(key, '', ctx);
24
17
  if (map instanceof Map) {
@@ -43,41 +36,6 @@ function addPairToJSMap(ctx, map, { key, value }) {
43
36
  }
44
37
  return map;
45
38
  }
46
- const isMergeKey = (key) => key === MERGE_KEY ||
47
- (identity.isScalar(key) &&
48
- key.value === MERGE_KEY &&
49
- (!key.type || key.type === Scalar.Scalar.PLAIN));
50
- // If the value associated with a merge key is a single mapping node, each of
51
- // its key/value pairs is inserted into the current mapping, unless the key
52
- // already exists in it. If the value associated with the merge key is a
53
- // sequence, then this sequence is expected to contain mapping nodes and each
54
- // of these nodes is merged in turn according to its order in the sequence.
55
- // Keys in mapping nodes earlier in the sequence override keys specified in
56
- // later mapping nodes. -- http://yaml.org/type/merge.html
57
- function mergeToJSMap(ctx, map, value) {
58
- const source = ctx && identity.isAlias(value) ? value.resolve(ctx.doc) : value;
59
- if (!identity.isMap(source))
60
- throw new Error('Merge sources must be maps or map aliases');
61
- const srcMap = source.toJSON(null, ctx, Map);
62
- for (const [key, value] of srcMap) {
63
- if (map instanceof Map) {
64
- if (!map.has(key))
65
- map.set(key, value);
66
- }
67
- else if (map instanceof Set) {
68
- map.add(key);
69
- }
70
- else if (!Object.prototype.hasOwnProperty.call(map, key)) {
71
- Object.defineProperty(map, key, {
72
- value,
73
- writable: true,
74
- enumerable: true,
75
- configurable: true
76
- });
77
- }
78
- }
79
- return map;
80
- }
81
39
  function stringifyKey(key, jsKey, ctx) {
82
40
  if (jsKey === null)
83
41
  return '';
package/dist/options.d.ts CHANGED
@@ -42,6 +42,12 @@ export type ParseOptions = {
42
42
  * Default: `true`
43
43
  */
44
44
  strict?: boolean;
45
+ /**
46
+ * Parse all mapping keys as strings. Treat all non-scalar keys as errors.
47
+ *
48
+ * Default: `false`
49
+ */
50
+ stringKeys?: boolean;
45
51
  /**
46
52
  * YAML requires map keys to be unique. By default, this is checked by
47
53
  * comparing scalar values with `===`; deep equality is not checked for
@@ -59,7 +59,7 @@ export declare class Lexer {
59
59
  *
60
60
  * @returns A generator of lexical tokens
61
61
  */
62
- lex(source: string, incomplete?: boolean): Generator<string, void, unknown>;
62
+ lex(source: string, incomplete?: boolean): Generator<string, void, any>;
63
63
  private atLineEnd;
64
64
  private charAt;
65
65
  private continueScalar;
@@ -57,14 +57,14 @@ export declare class Parser {
57
57
  *
58
58
  * @returns A generator of tokens representing each directive, document, and other structure.
59
59
  */
60
- parse(source: string, incomplete?: boolean): Generator<Token, void, unknown>;
60
+ parse(source: string, incomplete?: boolean): Generator<Token, void, any>;
61
61
  /**
62
62
  * Advance the parser by the `source` of one lexical token.
63
63
  */
64
- next(source: string): Generator<Token, void, unknown>;
64
+ next(source: string): Generator<Token, void, any>;
65
65
  private lexer;
66
66
  /** Call at end of input to push out any remaining constructions */
67
- end(): Generator<Token, void, unknown>;
67
+ end(): Generator<Token, void, any>;
68
68
  private get sourceToken();
69
69
  private step;
70
70
  private peek;
@@ -612,7 +612,9 @@ class Parser {
612
612
  const sep = it.sep;
613
613
  sep.push(this.sourceToken);
614
614
  // @ts-expect-error type guard is wrong here
615
- delete it.key, delete it.sep;
615
+ delete it.key;
616
+ // @ts-expect-error type guard is wrong here
617
+ delete it.sep;
616
618
  this.stack.push({
617
619
  type: 'block-map',
618
620
  offset: this.offset,
@@ -4,6 +4,7 @@ var composer = require('./compose/composer.js');
4
4
  var Document = require('./doc/Document.js');
5
5
  var errors = require('./errors.js');
6
6
  var log = require('./log.js');
7
+ var identity = require('./nodes/identity.js');
7
8
  var lineCounter = require('./parse/line-counter.js');
8
9
  var parser = require('./parse/parser.js');
9
10
 
@@ -95,6 +96,8 @@ function stringify(value, replacer, options) {
95
96
  if (!keepUndefined)
96
97
  return undefined;
97
98
  }
99
+ if (identity.isDocument(value) && !_replacer)
100
+ return value.toString(options);
98
101
  return new Document.Document(value, _replacer, options).toString(options);
99
102
  }
100
103
 
@@ -5,7 +5,6 @@ import type { CollectionTag, ScalarTag } from './types.js';
5
5
  export declare class Schema {
6
6
  compat: Array<CollectionTag | ScalarTag> | null;
7
7
  knownTags: Record<string, CollectionTag | ScalarTag>;
8
- merge: boolean;
9
8
  name: string;
10
9
  sortMapEntries: ((a: Pair, b: Pair) => number) | null;
11
10
  tags: Array<CollectionTag | ScalarTag>;
@@ -14,10 +14,9 @@ class Schema {
14
14
  : compat
15
15
  ? tags.getTags(null, compat)
16
16
  : null;
17
- this.merge = !!merge;
18
17
  this.name = (typeof schema === 'string' && schema) || 'core';
19
18
  this.knownTags = resolveKnownTags ? tags.coreKnownTags : {};
20
- this.tags = tags.getTags(customTags, this.name);
19
+ this.tags = tags.getTags(customTags, this.name, merge);
21
20
  this.toStringOptions = toStringDefaults ?? null;
22
21
  Object.defineProperty(this, identity.MAP, { value: map.map });
23
22
  Object.defineProperty(this, identity.SCALAR, { value: string.string });
@@ -14,6 +14,10 @@ declare const tagsByName: {
14
14
  intOct: ScalarTag;
15
15
  intTime: ScalarTag;
16
16
  map: CollectionTag;
17
+ merge: ScalarTag & {
18
+ identify(value: unknown): boolean;
19
+ test: RegExp;
20
+ };
17
21
  null: ScalarTag & {
18
22
  test: RegExp;
19
23
  };
@@ -29,6 +33,10 @@ export type TagId = keyof typeof tagsByName;
29
33
  export type Tags = Array<ScalarTag | CollectionTag | TagId>;
30
34
  export declare const coreKnownTags: {
31
35
  'tag:yaml.org,2002:binary': ScalarTag;
36
+ 'tag:yaml.org,2002:merge': ScalarTag & {
37
+ identify(value: unknown): boolean;
38
+ test: RegExp;
39
+ };
32
40
  'tag:yaml.org,2002:omap': CollectionTag;
33
41
  'tag:yaml.org,2002:pairs': CollectionTag;
34
42
  'tag:yaml.org,2002:set': CollectionTag;
@@ -36,5 +44,5 @@ export declare const coreKnownTags: {
36
44
  test: RegExp;
37
45
  };
38
46
  };
39
- export declare function getTags(customTags: SchemaOptions['customTags'] | undefined, schemaName: string): (CollectionTag | ScalarTag)[];
47
+ export declare function getTags(customTags: SchemaOptions['customTags'] | undefined, schemaName: string, addMergeTag?: boolean): (CollectionTag | ScalarTag)[];
40
48
  export {};
@@ -10,6 +10,7 @@ var int = require('./core/int.js');
10
10
  var schema = require('./core/schema.js');
11
11
  var schema$1 = require('./json/schema.js');
12
12
  var binary = require('./yaml-1.1/binary.js');
13
+ var merge = require('./yaml-1.1/merge.js');
13
14
  var omap = require('./yaml-1.1/omap.js');
14
15
  var pairs = require('./yaml-1.1/pairs.js');
15
16
  var schema$2 = require('./yaml-1.1/schema.js');
@@ -35,6 +36,7 @@ const tagsByName = {
35
36
  intOct: int.intOct,
36
37
  intTime: timestamp.intTime,
37
38
  map: map.map,
39
+ merge: merge.merge,
38
40
  null: _null.nullTag,
39
41
  omap: omap.omap,
40
42
  pairs: pairs.pairs,
@@ -44,13 +46,20 @@ const tagsByName = {
44
46
  };
45
47
  const coreKnownTags = {
46
48
  'tag:yaml.org,2002:binary': binary.binary,
49
+ 'tag:yaml.org,2002:merge': merge.merge,
47
50
  'tag:yaml.org,2002:omap': omap.omap,
48
51
  'tag:yaml.org,2002:pairs': pairs.pairs,
49
52
  'tag:yaml.org,2002:set': set.set,
50
53
  'tag:yaml.org,2002:timestamp': timestamp.timestamp
51
54
  };
52
- function getTags(customTags, schemaName) {
53
- let tags = schemas.get(schemaName);
55
+ function getTags(customTags, schemaName, addMergeTag) {
56
+ const schemaTags = schemas.get(schemaName);
57
+ if (schemaTags && !customTags) {
58
+ return addMergeTag && !schemaTags.includes(merge.merge)
59
+ ? schemaTags.concat(merge.merge)
60
+ : schemaTags.slice();
61
+ }
62
+ let tags = schemaTags;
54
63
  if (!tags) {
55
64
  if (Array.isArray(customTags))
56
65
  tags = [];
@@ -69,17 +78,21 @@ function getTags(customTags, schemaName) {
69
78
  else if (typeof customTags === 'function') {
70
79
  tags = customTags(tags.slice());
71
80
  }
72
- return tags.map(tag => {
73
- if (typeof tag !== 'string')
74
- return tag;
75
- const tagObj = tagsByName[tag];
76
- if (tagObj)
77
- return tagObj;
78
- const keys = Object.keys(tagsByName)
79
- .map(key => JSON.stringify(key))
80
- .join(', ');
81
- throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
82
- });
81
+ if (addMergeTag)
82
+ tags = tags.concat(merge.merge);
83
+ return tags.reduce((tags, tag) => {
84
+ const tagObj = typeof tag === 'string' ? tagsByName[tag] : tag;
85
+ if (!tagObj) {
86
+ const tagName = JSON.stringify(tag);
87
+ const keys = Object.keys(tagsByName)
88
+ .map(key => JSON.stringify(key))
89
+ .join(', ');
90
+ throw new Error(`Unknown custom tag ${tagName}; use one of ${keys}`);
91
+ }
92
+ if (!tags.includes(tagObj))
93
+ tags.push(tagObj);
94
+ return tags;
95
+ }, []);
83
96
  }
84
97
 
85
98
  exports.coreKnownTags = coreKnownTags;
@@ -12,11 +12,13 @@ interface TagBase {
12
12
  */
13
13
  createNode?: (schema: Schema, value: unknown, ctx: CreateNodeContext) => Node;
14
14
  /**
15
- * If `true`, together with `test` allows for values to be stringified without
16
- * an explicit tag. For most cases, it's unlikely that you'll actually want to
17
- * use this, even if you first think you do.
15
+ * If `true`, allows for values to be stringified without
16
+ * an explicit tag together with `test`.
17
+ * If `'key'`, this only applies if the value is used as a mapping key.
18
+ * For most cases, it's unlikely that you'll actually want to use this,
19
+ * even if you first think you do.
18
20
  */
19
- default?: boolean;
21
+ default?: boolean | 'key';
20
22
  /**
21
23
  * If a tag has multiple forms that should be parsed and/or stringified
22
24
  * differently, use `format` to identify them.
@@ -0,0 +1,9 @@
1
+ import type { ToJSContext } from '../../nodes/toJS.js';
2
+ import type { MapLike } from '../../nodes/YAMLMap.js';
3
+ import type { ScalarTag } from '../types.js';
4
+ export declare const merge: ScalarTag & {
5
+ identify(value: unknown): boolean;
6
+ test: RegExp;
7
+ };
8
+ export declare const isMergeKey: (ctx: ToJSContext | undefined, key: unknown) => boolean | undefined;
9
+ export declare function addMergeToJSMap(ctx: ToJSContext | undefined, map: MapLike, value: unknown): void;
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+
3
+ var identity = require('../../nodes/identity.js');
4
+ var Scalar = require('../../nodes/Scalar.js');
5
+
6
+ // If the value associated with a merge key is a single mapping node, each of
7
+ // its key/value pairs is inserted into the current mapping, unless the key
8
+ // already exists in it. If the value associated with the merge key is a
9
+ // sequence, then this sequence is expected to contain mapping nodes and each
10
+ // of these nodes is merged in turn according to its order in the sequence.
11
+ // Keys in mapping nodes earlier in the sequence override keys specified in
12
+ // later mapping nodes. -- http://yaml.org/type/merge.html
13
+ const MERGE_KEY = '<<';
14
+ const merge = {
15
+ identify: value => value === MERGE_KEY ||
16
+ (typeof value === 'symbol' && value.description === MERGE_KEY),
17
+ default: 'key',
18
+ tag: 'tag:yaml.org,2002:merge',
19
+ test: /^<<$/,
20
+ resolve: () => Object.assign(new Scalar.Scalar(Symbol(MERGE_KEY)), {
21
+ addToJSMap: addMergeToJSMap
22
+ }),
23
+ stringify: () => MERGE_KEY
24
+ };
25
+ const isMergeKey = (ctx, key) => (merge.identify(key) ||
26
+ (identity.isScalar(key) &&
27
+ (!key.type || key.type === Scalar.Scalar.PLAIN) &&
28
+ merge.identify(key.value))) &&
29
+ ctx?.doc.schema.tags.some(tag => tag.tag === merge.tag && tag.default);
30
+ function addMergeToJSMap(ctx, map, value) {
31
+ value = ctx && identity.isAlias(value) ? value.resolve(ctx.doc) : value;
32
+ if (identity.isSeq(value))
33
+ for (const it of value.items)
34
+ mergeValue(ctx, map, it);
35
+ else if (Array.isArray(value))
36
+ for (const it of value)
37
+ mergeValue(ctx, map, it);
38
+ else
39
+ mergeValue(ctx, map, value);
40
+ }
41
+ function mergeValue(ctx, map, value) {
42
+ const source = ctx && identity.isAlias(value) ? value.resolve(ctx.doc) : value;
43
+ if (!identity.isMap(source))
44
+ throw new Error('Merge sources must be maps or map aliases');
45
+ const srcMap = source.toJSON(null, ctx, Map);
46
+ for (const [key, value] of srcMap) {
47
+ if (map instanceof Map) {
48
+ if (!map.has(key))
49
+ map.set(key, value);
50
+ }
51
+ else if (map instanceof Set) {
52
+ map.add(key);
53
+ }
54
+ else if (!Object.prototype.hasOwnProperty.call(map, key)) {
55
+ Object.defineProperty(map, key, {
56
+ value,
57
+ writable: true,
58
+ enumerable: true,
59
+ configurable: true
60
+ });
61
+ }
62
+ }
63
+ return map;
64
+ }
65
+
66
+ exports.addMergeToJSMap = addMergeToJSMap;
67
+ exports.isMergeKey = isMergeKey;
68
+ exports.merge = merge;
@@ -8,6 +8,7 @@ var binary = require('./binary.js');
8
8
  var bool = require('./bool.js');
9
9
  var float = require('./float.js');
10
10
  var int = require('./int.js');
11
+ var merge = require('./merge.js');
11
12
  var omap = require('./omap.js');
12
13
  var pairs = require('./pairs.js');
13
14
  var set = require('./set.js');
@@ -28,6 +29,7 @@ const schema = [
28
29
  float.floatExp,
29
30
  float.float,
30
31
  binary.binary,
32
+ merge.merge,
31
33
  omap.omap,
32
34
  pairs.pairs,
33
35
  set.set,
@@ -56,7 +56,12 @@ function getTagObject(tags, item) {
56
56
  let obj;
57
57
  if (identity.isScalar(item)) {
58
58
  obj = item.value;
59
- const match = tags.filter(t => t.identify?.(obj));
59
+ let match = tags.filter(t => t.identify?.(obj));
60
+ if (match.length > 1) {
61
+ const testMatch = match.filter(t => t.test);
62
+ if (testMatch.length > 0)
63
+ match = testMatch;
64
+ }
60
65
  tagObj =
61
66
  match.find(t => t.format === item.format) ?? match.find(t => !t.format);
62
67
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "license": "ISC",
5
5
  "author": "Eemeli Aro <eemeli@gmail.com>",
6
6
  "repository": "github:eemeli/yaml",
@@ -42,7 +42,7 @@
42
42
  "build:browser": "rollup -c config/rollup.browser-config.mjs",
43
43
  "build:node": "rollup -c config/rollup.node-config.mjs",
44
44
  "clean": "git clean -fdxe node_modules",
45
- "lint": "eslint src/",
45
+ "lint": "eslint config/ src/",
46
46
  "prettier": "prettier --write .",
47
47
  "prestart": "rollup --sourcemap -c config/rollup.node-config.mjs",
48
48
  "start": "node --enable-source-maps -i -e 'YAML=require(\"./dist/index.js\");const{parse,parseDocument,parseAllDocuments}=YAML'",
@@ -69,20 +69,17 @@
69
69
  },
70
70
  "devDependencies": {
71
71
  "@babel/core": "^7.12.10",
72
- "@babel/plugin-transform-class-properties": "^7.23.3",
73
- "@babel/plugin-transform-nullish-coalescing-operator": "^7.23.4",
74
72
  "@babel/plugin-transform-typescript": "^7.12.17",
75
73
  "@babel/preset-env": "^7.12.11",
74
+ "@eslint/js": "^9.9.1",
76
75
  "@rollup/plugin-babel": "^6.0.3",
77
76
  "@rollup/plugin-replace": "^5.0.2",
78
77
  "@rollup/plugin-typescript": "^11.0.0",
79
78
  "@types/jest": "^29.2.4",
80
79
  "@types/node": "^20.11.20",
81
- "@typescript-eslint/eslint-plugin": "^7.0.2",
82
- "@typescript-eslint/parser": "^7.0.2",
83
80
  "babel-jest": "^29.0.1",
84
81
  "cross-env": "^7.0.3",
85
- "eslint": "^8.2.0",
82
+ "eslint": "^9.9.1",
86
83
  "eslint-config-prettier": "^9.0.0",
87
84
  "fast-check": "^2.12.0",
88
85
  "jest": "^29.0.1",
@@ -90,7 +87,8 @@
90
87
  "prettier": "^3.0.2",
91
88
  "rollup": "^4.12.0",
92
89
  "tslib": "^2.1.0",
93
- "typescript": "^5.0.3"
90
+ "typescript": "^5.0.3",
91
+ "typescript-eslint": "^8.4.0"
94
92
  },
95
93
  "engines": {
96
94
  "node": ">= 14"
@@ -1,21 +0,0 @@
1
- /******************************************************************************
2
- Copyright (c) Microsoft Corporation.
3
-
4
- Permission to use, copy, modify, and/or distribute this software for any
5
- purpose with or without fee is hereby granted.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
- PERFORMANCE OF THIS SOFTWARE.
14
- ***************************************************************************** */
15
- function __classPrivateFieldGet(receiver, state, kind, f) {
16
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
17
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
18
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
19
- }
20
-
21
- export { __classPrivateFieldGet };