yaml 1.4.0 → 1.5.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 (136) hide show
  1. package/README.md +3 -5
  2. package/browser/dist/Anchors.js +1 -3
  3. package/browser/dist/Document.js +94 -37
  4. package/browser/dist/constants.js +34 -0
  5. package/browser/dist/cst/Alias.js +1 -3
  6. package/browser/dist/cst/BlankLine.js +5 -7
  7. package/browser/dist/cst/BlockValue.js +4 -4
  8. package/browser/dist/cst/Collection.js +7 -7
  9. package/browser/dist/cst/CollectionItem.js +6 -8
  10. package/browser/dist/cst/Comment.js +5 -7
  11. package/browser/dist/cst/Directive.js +5 -7
  12. package/browser/dist/cst/Document.js +8 -10
  13. package/browser/dist/cst/FlowCollection.js +5 -7
  14. package/browser/dist/cst/Node.js +12 -40
  15. package/browser/dist/cst/ParseContext.js +32 -34
  16. package/browser/dist/cst/PlainValue.js +1 -3
  17. package/browser/dist/cst/QuoteDouble.js +1 -3
  18. package/browser/dist/cst/QuoteSingle.js +1 -3
  19. package/browser/dist/cst/Range.js +1 -3
  20. package/browser/dist/cst/getLinePos.js +1 -4
  21. package/browser/dist/cst/parse.js +1 -4
  22. package/browser/dist/deprecation.js +34 -0
  23. package/browser/dist/errors.js +50 -59
  24. package/browser/dist/index.js +5 -4
  25. package/browser/dist/listTagNames.js +1 -3
  26. package/browser/dist/schema/Alias.js +85 -10
  27. package/browser/dist/schema/Map.js +14 -121
  28. package/browser/dist/schema/Merge.js +74 -1
  29. package/browser/dist/schema/Node.js +1 -3
  30. package/browser/dist/schema/Pair.js +36 -22
  31. package/browser/dist/schema/Scalar.js +1 -3
  32. package/browser/dist/schema/Seq.js +1 -3
  33. package/browser/dist/schema/index.js +61 -45
  34. package/browser/dist/schema/parseMap.js +18 -21
  35. package/browser/dist/schema/parseSeq.js +9 -12
  36. package/browser/dist/stringify.js +54 -39
  37. package/browser/dist/{schema → tags}/core.js +32 -20
  38. package/browser/dist/tags/failsafe/index.js +17 -0
  39. package/browser/dist/tags/failsafe/map.js +65 -0
  40. package/browser/dist/tags/failsafe/seq.js +54 -0
  41. package/browser/dist/{schema/_string.js → tags/failsafe/string.js} +11 -11
  42. package/browser/dist/tags/index.js +50 -0
  43. package/browser/dist/{schema → tags}/json.js +8 -8
  44. package/browser/dist/tags/options.js +35 -0
  45. package/browser/dist/{schema/_binary.js → tags/yaml-1.1/binary.js} +16 -22
  46. package/browser/dist/{schema/yaml-1.1.js → tags/yaml-1.1/index.js} +43 -29
  47. package/browser/dist/{schema/_omap.js → tags/yaml-1.1/omap.js} +8 -11
  48. package/browser/dist/{schema/_pairs.js → tags/yaml-1.1/pairs.js} +8 -12
  49. package/browser/dist/tags/yaml-1.1/set.js +140 -0
  50. package/browser/dist/{schema/_timestamp.js → tags/yaml-1.1/timestamp.js} +4 -7
  51. package/browser/dist/test-events.js +1 -4
  52. package/browser/dist/toJSON.js +1 -4
  53. package/browser/index.js +1 -0
  54. package/browser/map.js +2 -1
  55. package/browser/pair.js +2 -1
  56. package/browser/parse-cst.js +1 -1
  57. package/browser/scalar.js +2 -1
  58. package/browser/schema.js +7 -4
  59. package/browser/seq.js +2 -1
  60. package/browser/types/binary.js +7 -1
  61. package/browser/types/omap.js +2 -1
  62. package/browser/types/pairs.js +2 -1
  63. package/browser/types/set.js +2 -1
  64. package/browser/types/timestamp.js +10 -1
  65. package/browser/types.js +11 -0
  66. package/browser/util.js +15 -0
  67. package/dist/Anchors.js +1 -3
  68. package/dist/Document.js +38 -25
  69. package/dist/constants.js +34 -0
  70. package/dist/cst/Alias.js +1 -3
  71. package/dist/cst/BlankLine.js +5 -7
  72. package/dist/cst/BlockValue.js +4 -4
  73. package/dist/cst/Collection.js +7 -7
  74. package/dist/cst/CollectionItem.js +6 -8
  75. package/dist/cst/Comment.js +5 -7
  76. package/dist/cst/Directive.js +5 -7
  77. package/dist/cst/Document.js +8 -10
  78. package/dist/cst/FlowCollection.js +5 -7
  79. package/dist/cst/Node.js +12 -40
  80. package/dist/cst/ParseContext.js +32 -34
  81. package/dist/cst/PlainValue.js +1 -3
  82. package/dist/cst/QuoteDouble.js +1 -3
  83. package/dist/cst/QuoteSingle.js +1 -3
  84. package/dist/cst/Range.js +1 -3
  85. package/dist/cst/getLinePos.js +1 -4
  86. package/dist/cst/parse.js +1 -4
  87. package/dist/deprecation.js +34 -0
  88. package/dist/errors.js +29 -35
  89. package/dist/index.js +5 -4
  90. package/dist/listTagNames.js +1 -3
  91. package/dist/schema/Alias.js +66 -12
  92. package/dist/schema/Map.js +9 -77
  93. package/dist/schema/Merge.js +31 -0
  94. package/dist/schema/Node.js +1 -3
  95. package/dist/schema/Pair.js +29 -15
  96. package/dist/schema/Scalar.js +1 -3
  97. package/dist/schema/Seq.js +1 -3
  98. package/dist/schema/index.js +55 -45
  99. package/dist/schema/parseMap.js +18 -21
  100. package/dist/schema/parseSeq.js +9 -12
  101. package/dist/stringify.js +46 -32
  102. package/dist/{schema → tags}/core.js +32 -19
  103. package/dist/tags/failsafe/index.js +17 -0
  104. package/dist/tags/failsafe/map.js +33 -0
  105. package/dist/tags/failsafe/seq.js +34 -0
  106. package/dist/tags/failsafe/string.js +40 -0
  107. package/dist/tags/index.js +50 -0
  108. package/dist/{schema → tags}/json.js +8 -8
  109. package/dist/tags/options.js +35 -0
  110. package/dist/{schema/_binary.js → tags/yaml-1.1/binary.js} +15 -21
  111. package/dist/{schema/yaml-1.1.js → tags/yaml-1.1/index.js} +43 -30
  112. package/dist/{schema/_omap.js → tags/yaml-1.1/omap.js} +8 -9
  113. package/dist/{schema/_pairs.js → tags/yaml-1.1/pairs.js} +8 -10
  114. package/dist/{schema/_set.js → tags/yaml-1.1/set.js} +9 -42
  115. package/dist/{schema/_timestamp.js → tags/yaml-1.1/timestamp.js} +4 -7
  116. package/dist/test-events.js +1 -4
  117. package/dist/toJSON.js +1 -4
  118. package/index.js +1 -0
  119. package/map.js +2 -1
  120. package/package.json +17 -15
  121. package/pair.js +2 -1
  122. package/parse-cst.js +1 -1
  123. package/scalar.js +2 -1
  124. package/schema.js +7 -4
  125. package/seq.js +2 -1
  126. package/types/binary.js +7 -1
  127. package/types/omap.js +2 -1
  128. package/types/pairs.js +2 -1
  129. package/types/set.js +2 -1
  130. package/types/timestamp.js +10 -1
  131. package/types.js +11 -0
  132. package/util.js +15 -0
  133. package/browser/dist/schema/_set.js +0 -215
  134. package/browser/dist/schema/failsafe.js +0 -125
  135. package/dist/schema/_string.js +0 -40
  136. package/dist/schema/failsafe.js +0 -74
package/dist/errors.js CHANGED
@@ -3,70 +3,64 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.YAMLWarning = exports.YAMLSyntaxError = exports.YAMLSemanticError = exports.YAMLReferenceError = void 0;
6
+ exports.YAMLWarning = exports.YAMLSyntaxError = exports.YAMLSemanticError = exports.YAMLReferenceError = exports.YAMLError = void 0;
7
7
 
8
8
  var _Node = _interopRequireDefault(require("./cst/Node"));
9
9
 
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
11
 
12
- class YAMLReferenceError extends ReferenceError {
13
- constructor(source, message) {
14
- if (!message || !(source instanceof _Node.default)) {
15
- throw new Error('Invalid arguments for new YAMLReferenceError');
16
- }
17
-
12
+ class YAMLError extends Error {
13
+ constructor(name, source, message) {
14
+ if (!message || !(source instanceof _Node.default)) throw new Error(`Invalid arguments for new ${name}`);
18
15
  super();
19
- this.name = 'YAMLReferenceError';
16
+ this.name = name;
20
17
  this.message = message;
21
18
  this.source = source;
22
19
  }
23
20
 
21
+ makePretty() {
22
+ if (this.source) {
23
+ this.nodeType = this.source.type;
24
+ this.range = this.source.range;
25
+ this.linePos = this.source.rangeAsLinePos;
26
+ delete this.source;
27
+ }
28
+ }
29
+
24
30
  }
25
31
 
26
- exports.YAMLReferenceError = YAMLReferenceError;
32
+ exports.YAMLError = YAMLError;
27
33
 
28
- class YAMLSemanticError extends SyntaxError {
34
+ class YAMLReferenceError extends YAMLError {
29
35
  constructor(source, message) {
30
- if (!message || !(source instanceof _Node.default)) {
31
- throw new Error('Invalid arguments for new YAMLSemanticError');
32
- }
36
+ super('YAMLReferenceError', source, message);
37
+ }
33
38
 
34
- super();
35
- this.name = 'YAMLSemanticError';
36
- this.message = message;
37
- this.source = source;
39
+ }
40
+
41
+ exports.YAMLReferenceError = YAMLReferenceError;
42
+
43
+ class YAMLSemanticError extends YAMLError {
44
+ constructor(source, message) {
45
+ super('YAMLSemanticError', source, message);
38
46
  }
39
47
 
40
48
  }
41
49
 
42
50
  exports.YAMLSemanticError = YAMLSemanticError;
43
51
 
44
- class YAMLSyntaxError extends SyntaxError {
52
+ class YAMLSyntaxError extends YAMLError {
45
53
  constructor(source, message) {
46
- if (!message || !(source instanceof _Node.default)) {
47
- throw new Error('Invalid arguments for new YAMLSyntaxError');
48
- }
49
-
50
- super();
51
- this.name = 'YAMLSyntaxError';
52
- this.message = message;
53
- this.source = source;
54
+ super('YAMLSyntaxError', source, message);
54
55
  }
55
56
 
56
57
  }
57
58
 
58
59
  exports.YAMLSyntaxError = YAMLSyntaxError;
59
60
 
60
- class YAMLWarning extends Error {
61
+ class YAMLWarning extends YAMLError {
61
62
  constructor(source, message) {
62
- if (!message || !(source instanceof _Node.default)) {
63
- throw new Error('Invalid arguments for new YAMLWarning');
64
- }
65
-
66
- super();
67
- this.name = 'YAMLWarning';
68
- this.message = message;
69
- this.source = source;
63
+ super('YAMLWarning', source, message);
70
64
  }
71
65
 
72
66
  }
package/dist/index.js CHANGED
@@ -18,11 +18,14 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
18
18
  /* global console */
19
19
  const defaultOptions = {
20
20
  anchorPrefix: 'a',
21
+ customTags: null,
21
22
  keepCstNodes: false,
22
23
  keepNodeTypes: true,
23
24
  keepBlobsInJSON: true,
24
25
  mapAsMap: false,
25
- tags: null,
26
+ maxAliasCount: 100,
27
+ prettyErrors: false,
28
+ // TODO Set true in v2
26
29
  version: '1.2'
27
30
  };
28
31
 
@@ -84,6 +87,4 @@ var _default = {
84
87
  parseDocument,
85
88
  stringify
86
89
  };
87
- exports.default = _default;
88
- module.exports = exports.default;
89
- module.exports.default = exports.default;
90
+ exports.default = _default;
@@ -35,6 +35,4 @@ const visit = (node, tags) => {
35
35
 
36
36
  var _default = node => Object.keys(visit(node, {}));
37
37
 
38
- exports.default = _default;
39
- module.exports = exports.default;
40
- module.exports.default = exports.default;
38
+ exports.default = _default;
@@ -5,26 +5,56 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _Node = require("../cst/Node");
8
+ var _constants = require("../constants");
9
+
10
+ var _errors = require("../errors");
9
11
 
10
12
  var _toJSON = _interopRequireDefault(require("../toJSON"));
11
13
 
12
- var _Node2 = _interopRequireDefault(require("./Node"));
14
+ var _Collection = _interopRequireDefault(require("./Collection"));
15
+
16
+ var _Node = _interopRequireDefault(require("./Node"));
17
+
18
+ var _Pair = _interopRequireDefault(require("./Pair"));
13
19
 
14
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
21
 
16
22
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
23
 
18
- class Alias extends _Node2.default {
24
+ const getAliasCount = (node, anchors) => {
25
+ if (node instanceof Alias) {
26
+ const anchor = anchors.find(a => a.node === node.source);
27
+ return anchor.count * anchor.aliasCount;
28
+ } else if (node instanceof _Collection.default) {
29
+ let count = 0;
30
+
31
+ for (const item of node.items) {
32
+ const c = getAliasCount(item, anchors);
33
+ if (c > count) count = c;
34
+ }
35
+
36
+ return count;
37
+ } else if (node instanceof _Pair.default) {
38
+ const kc = getAliasCount(node.key, anchors);
39
+ const vc = getAliasCount(node.value, anchors);
40
+ return Math.max(kc, vc);
41
+ }
42
+
43
+ return 1;
44
+ };
45
+
46
+ class Alias extends _Node.default {
19
47
  static stringify({
20
48
  range,
21
49
  source
22
50
  }, {
23
51
  anchors,
24
52
  doc,
25
- implicitKey
53
+ implicitKey,
54
+ inStringifyKey
26
55
  }) {
27
- const anchor = Object.keys(anchors).find(a => anchors[a] === source);
56
+ let anchor = Object.keys(anchors).find(a => anchors[a] === source);
57
+ if (!anchor && inStringifyKey) anchor = doc.anchors.getName(source) || doc.anchors.newName();
28
58
  if (anchor) return `*${anchor}${implicitKey ? ' ' : ''}`;
29
59
  const msg = doc.anchors.getName(source) ? 'Alias node must be after source node' : 'Source node not found for alias node';
30
60
  throw new Error(`${msg} [${range}]`);
@@ -33,7 +63,7 @@ class Alias extends _Node2.default {
33
63
  constructor(source) {
34
64
  super();
35
65
  this.source = source;
36
- this.type = _Node.Type.ALIAS;
66
+ this.type = _constants.Type.ALIAS;
37
67
  }
38
68
 
39
69
  set tag(t) {
@@ -41,15 +71,39 @@ class Alias extends _Node2.default {
41
71
  }
42
72
 
43
73
  toJSON(arg, ctx) {
44
- const anchor = ctx && ctx.anchors && ctx.anchors.find(a => a.node === this.source);
45
- return anchor && anchor.res || (0, _toJSON.default)(this.source, arg, ctx);
74
+ if (!ctx) return (0, _toJSON.default)(this.source, arg, ctx);
75
+ const {
76
+ anchors,
77
+ maxAliasCount
78
+ } = ctx;
79
+ const anchor = anchors.find(a => a.node === this.source);
80
+
81
+ if (!anchor || !anchor.res) {
82
+ const msg = 'This should not happen: Alias anchor was not resolved?';
83
+ if (this.cstNode) throw new _errors.YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg);
84
+ }
85
+
86
+ if (maxAliasCount >= 0) {
87
+ anchor.count += 1;
88
+ if (anchor.aliasCount === 0) anchor.aliasCount = getAliasCount(this.source, anchors);
89
+
90
+ if (anchor.count * anchor.aliasCount > maxAliasCount) {
91
+ const msg = 'Excessive alias count indicates a resource exhaustion attack';
92
+ if (this.cstNode) throw new _errors.YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg);
93
+ }
94
+ }
95
+
96
+ return anchor.res;
97
+ } // Only called when stringifying an alias mapping key while constructing
98
+ // Object output.
99
+
100
+
101
+ toString(ctx) {
102
+ return Alias.stringify(this, ctx);
46
103
  }
47
104
 
48
105
  }
49
106
 
50
107
  exports.default = Alias;
51
108
 
52
- _defineProperty(Alias, "default", true);
53
-
54
- module.exports = exports.default;
55
- module.exports.default = exports.default;
109
+ _defineProperty(Alias, "default", true);
@@ -6,19 +6,14 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.findPair = findPair;
7
7
  exports.default = void 0;
8
8
 
9
- var _toJSON = _interopRequireDefault(require("../toJSON"));
10
-
11
9
  var _Collection = _interopRequireDefault(require("./Collection"));
12
10
 
13
- var _Merge = _interopRequireDefault(require("./Merge"));
14
-
15
11
  var _Pair = _interopRequireDefault(require("./Pair"));
16
12
 
17
13
  var _Scalar = _interopRequireDefault(require("./Scalar"));
18
14
 
19
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
16
 
21
- // Published as 'yaml/map'
22
17
  function findPair(items, key) {
23
18
  const k = key instanceof _Scalar.default ? key.value : key;
24
19
 
@@ -61,82 +56,19 @@ class YAMLMap extends _Collection.default {
61
56
  const prev = findPair(this.items, key);
62
57
  if (prev) prev.value = value;else this.items.push(new _Pair.default(key, value));
63
58
  }
59
+ /**
60
+ * @param {*} arg ignored
61
+ * @param {*} ctx Conversion context, originally set in Document#toJSON()
62
+ * @param {Class} Type If set, forces the returned collection type
63
+ * @returns {*} Instance of Type, Map, or Object
64
+ */
64
65
 
65
- toJSON(_, ctx) {
66
- if (ctx && ctx.mapAsMap) return this.toJSMap(ctx);
67
- const map = {};
68
- if (ctx && ctx.onCreate) ctx.onCreate(map);
69
-
70
- for (const item of this.items) {
71
- if (item instanceof _Merge.default) {
72
- // If the value associated with a merge key is a single mapping node,
73
- // each of its key/value pairs is inserted into the current mapping,
74
- // unless the key already exists in it. If the value associated with the
75
- // merge key is a sequence, then this sequence is expected to contain
76
- // mapping nodes and each of these nodes is merged in turn according to
77
- // its order in the sequence. Keys in mapping nodes earlier in the
78
- // sequence override keys specified in later mapping nodes.
79
- // -- http://yaml.org/type/merge.html
80
- const keys = Object.keys(map);
81
- const {
82
- items
83
- } = item.value;
84
-
85
- for (let i = items.length - 1; i >= 0; --i) {
86
- const {
87
- source
88
- } = items[i];
89
-
90
- if (source instanceof YAMLMap) {
91
- const obj = source.toJSON('', ctx);
92
- Object.keys(obj).forEach(key => {
93
- if (!keys.includes(key)) map[key] = obj[key];
94
- });
95
- } else {
96
- throw new Error('Merge sources must be maps');
97
- }
98
- }
99
- } else {
100
- const {
101
- stringKey,
102
- value
103
- } = item;
104
- map[stringKey] = (0, _toJSON.default)(value, stringKey, ctx);
105
- }
106
- }
107
66
 
108
- return map;
109
- }
110
-
111
- toJSMap(ctx) {
112
- const map = new Map();
67
+ toJSON(_, ctx, Type) {
68
+ const map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {};
113
69
  if (ctx && ctx.onCreate) ctx.onCreate(map);
114
70
 
115
- for (const item of this.items) {
116
- if (item instanceof _Merge.default) {
117
- const {
118
- items
119
- } = item.value;
120
-
121
- for (let i = items.length - 1; i >= 0; --i) {
122
- const {
123
- source
124
- } = items[i];
125
-
126
- if (source instanceof YAMLMap) {
127
- for (const [key, value] of source.toJSMap(ctx)) {
128
- if (!map.has(key)) map.set(key, value);
129
- }
130
- } else {
131
- throw new Error('Merge sources must be maps');
132
- }
133
- }
134
- } else {
135
- const key = (0, _toJSON.default)(item.key, '', ctx);
136
- const value = (0, _toJSON.default)(item.value, key, ctx);
137
- map.set(key, value);
138
- }
139
- }
71
+ for (const item of this.items) item.addToJSMap(ctx, map);
140
72
 
141
73
  return map;
142
74
  }
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = exports.MERGE_KEY = void 0;
7
7
 
8
+ var _Map = _interopRequireDefault(require("./Map"));
9
+
8
10
  var _Pair = _interopRequireDefault(require("./Pair"));
9
11
 
10
12
  var _Scalar = _interopRequireDefault(require("./Scalar"));
@@ -34,6 +36,35 @@ class Merge extends _Pair.default {
34
36
  }
35
37
 
36
38
  this.type = 'MERGE_PAIR';
39
+ } // If the value associated with a merge key is a single mapping node, each of
40
+ // its key/value pairs is inserted into the current mapping, unless the key
41
+ // already exists in it. If the value associated with the merge key is a
42
+ // sequence, then this sequence is expected to contain mapping nodes and each
43
+ // of these nodes is merged in turn according to its order in the sequence.
44
+ // Keys in mapping nodes earlier in the sequence override keys specified in
45
+ // later mapping nodes. -- http://yaml.org/type/merge.html
46
+
47
+
48
+ addToJSMap(ctx, map) {
49
+ for (const _ref of this.value.items) {
50
+ const {
51
+ source
52
+ } = _ref;
53
+ if (!(source instanceof _Map.default)) throw new Error('Merge sources must be maps');
54
+ const srcMap = source.toJSON(null, ctx, Map);
55
+
56
+ for (const [key, value] of srcMap) {
57
+ if (map instanceof Map) {
58
+ if (!map.has(key)) map.set(key, value);
59
+ } else if (map instanceof Set) {
60
+ map.add(key);
61
+ } else {
62
+ if (!map.hasOwnProperty(key)) map[key] = value;
63
+ }
64
+ }
65
+ }
66
+
67
+ return map;
37
68
  }
38
69
 
39
70
  toString(ctx, onComment) {
@@ -7,6 +7,4 @@ exports.default = void 0;
7
7
 
8
8
  class Node {}
9
9
 
10
- exports.default = Node;
11
- module.exports = exports.default;
12
- module.exports.default = exports.default;
10
+ exports.default = Node;
@@ -18,6 +18,19 @@ var _Scalar = _interopRequireDefault(require("./Scalar"));
18
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
19
 
20
20
  // Published as 'yaml/pair'
21
+ const stringifyKey = (key, jsKey, ctx) => {
22
+ if (jsKey === null) return '';
23
+ if (typeof jsKey !== 'object') return String(jsKey);
24
+ if (key instanceof _Node.default && ctx && ctx.doc) return key.toString({
25
+ anchors: {},
26
+ doc: ctx.doc,
27
+ indent: '',
28
+ inFlow: true,
29
+ inStringifyKey: true
30
+ });
31
+ return JSON.stringify(jsKey);
32
+ };
33
+
21
34
  class Pair extends _Node.default {
22
35
  constructor(key, value = null) {
23
36
  super();
@@ -35,22 +48,25 @@ class Pair extends _Node.default {
35
48
  this.key.commentBefore = cb;
36
49
  }
37
50
 
38
- get stringKey() {
39
- const key = (0, _toJSON.default)(this.key);
40
- if (key === null) return '';
41
- if (typeof key === 'object') try {
42
- return JSON.stringify(key);
43
- } catch (e) {
44
- /* should not happen, but let's ignore in any case */
51
+ addToJSMap(ctx, map) {
52
+ const key = (0, _toJSON.default)(this.key, '', ctx);
53
+
54
+ if (map instanceof Map) {
55
+ const value = (0, _toJSON.default)(this.value, key, ctx);
56
+ map.set(key, value);
57
+ } else if (map instanceof Set) {
58
+ map.add(key);
59
+ } else {
60
+ const stringKey = stringifyKey(this.key, key, ctx);
61
+ map[stringKey] = (0, _toJSON.default)(this.value, stringKey, ctx);
45
62
  }
46
- return String(key);
63
+
64
+ return map;
47
65
  }
48
66
 
49
67
  toJSON(_, ctx) {
50
- const pair = {};
51
- const sk = this.stringKey;
52
- pair[sk] = (0, _toJSON.default)(this.value, sk, ctx);
53
- return pair;
68
+ const pair = ctx && ctx.mapAsMap ? new Map() : {};
69
+ return this.addToJSMap(ctx, pair);
54
70
  }
55
71
 
56
72
  toString(ctx, onComment, onChompKeep) {
@@ -124,6 +140,4 @@ class Pair extends _Node.default {
124
140
 
125
141
  }
126
142
 
127
- exports.default = Pair;
128
- module.exports = exports.default;
129
- module.exports.default = exports.default;
143
+ exports.default = Pair;
@@ -28,6 +28,4 @@ class Scalar extends _Node.default {
28
28
 
29
29
  }
30
30
 
31
- exports.default = Scalar;
32
- module.exports = exports.default;
33
- module.exports.default = exports.default;
31
+ exports.default = Scalar;
@@ -75,6 +75,4 @@ class YAMLSeq extends _Collection.default {
75
75
 
76
76
  }
77
77
 
78
- exports.default = YAMLSeq;
79
- module.exports = exports.default;
80
- module.exports.default = exports.default;
78
+ exports.default = YAMLSeq;