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.
Files changed (220) hide show
  1. package/README.md +5 -5
  2. package/browser/dist/compose/compose-collection.js +12 -9
  3. package/browser/dist/compose/compose-doc.js +23 -6
  4. package/browser/dist/compose/compose-node.js +27 -17
  5. package/browser/dist/compose/compose-scalar.js +21 -23
  6. package/browser/dist/compose/composer.js +108 -78
  7. package/browser/dist/compose/resolve-block-map.js +41 -29
  8. package/browser/dist/compose/resolve-block-scalar.js +14 -17
  9. package/browser/dist/compose/resolve-block-seq.js +21 -17
  10. package/browser/dist/compose/resolve-end.js +7 -8
  11. package/browser/dist/compose/resolve-flow-collection.js +158 -199
  12. package/browser/dist/compose/resolve-flow-scalar.js +62 -26
  13. package/browser/dist/compose/resolve-props.js +72 -37
  14. package/browser/dist/compose/util-contains-newline.js +15 -12
  15. package/browser/dist/compose/util-map-includes.js +17 -0
  16. package/browser/dist/doc/Document.js +74 -99
  17. package/browser/dist/doc/anchors.js +72 -0
  18. package/browser/dist/doc/createNode.js +35 -19
  19. package/browser/dist/doc/directives.js +5 -0
  20. package/browser/dist/errors.js +16 -12
  21. package/browser/dist/index.js +3 -3
  22. package/browser/dist/nodes/Alias.js +51 -28
  23. package/browser/dist/nodes/Collection.js +23 -92
  24. package/browser/dist/nodes/Node.js +9 -1
  25. package/browser/dist/nodes/Pair.js +16 -231
  26. package/browser/dist/nodes/YAMLMap.js +8 -4
  27. package/browser/dist/nodes/YAMLSeq.js +7 -4
  28. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  29. package/browser/dist/nodes/toJS.js +12 -10
  30. package/browser/dist/options.js +1 -2
  31. package/browser/dist/parse/cst-scalar.js +219 -0
  32. package/browser/dist/parse/cst-stringify.js +61 -0
  33. package/browser/dist/parse/cst-visit.js +97 -0
  34. package/{dist/parse/tokens.js → browser/dist/parse/cst.js} +12 -7
  35. package/browser/dist/parse/lexer.js +149 -125
  36. package/browser/dist/parse/parser.js +272 -196
  37. package/browser/dist/public-api.js +11 -14
  38. package/browser/dist/schema/Schema.js +28 -0
  39. package/browser/dist/{tags/failsafe → schema/common}/map.js +0 -0
  40. package/browser/dist/schema/common/null.js +13 -0
  41. package/browser/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  42. package/browser/dist/{tags/failsafe → schema/common}/string.js +0 -0
  43. package/browser/dist/schema/core/bool.js +19 -0
  44. package/browser/dist/schema/core/float.js +40 -0
  45. package/browser/dist/schema/core/int.js +38 -0
  46. package/browser/dist/schema/core/schema.js +23 -0
  47. package/browser/dist/{tags/json.js → schema/json/schema.js} +5 -6
  48. package/browser/dist/schema/tags.js +79 -0
  49. package/browser/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  50. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  51. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  52. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  53. package/browser/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  54. package/browser/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  55. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  56. package/browser/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  57. package/browser/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  58. package/browser/dist/stringify/stringify.js +8 -6
  59. package/browser/dist/stringify/stringifyCollection.js +122 -0
  60. package/browser/dist/stringify/stringifyComment.js +14 -0
  61. package/browser/dist/stringify/stringifyDocument.js +64 -0
  62. package/browser/dist/stringify/stringifyPair.js +111 -0
  63. package/browser/dist/stringify/stringifyString.js +53 -52
  64. package/browser/dist/visit.js +20 -1
  65. package/dist/compose/compose-collection.d.ts +4 -4
  66. package/dist/compose/compose-collection.js +12 -9
  67. package/dist/compose/compose-doc.d.ts +4 -3
  68. package/dist/compose/compose-doc.js +23 -6
  69. package/dist/compose/compose-node.d.ts +15 -7
  70. package/dist/compose/compose-node.js +27 -17
  71. package/dist/compose/compose-scalar.d.ts +4 -3
  72. package/dist/compose/compose-scalar.js +20 -22
  73. package/dist/compose/composer.d.ts +28 -23
  74. package/dist/compose/composer.js +109 -79
  75. package/dist/compose/resolve-block-map.d.ts +5 -4
  76. package/dist/compose/resolve-block-map.js +41 -29
  77. package/dist/compose/resolve-block-scalar.d.ts +5 -3
  78. package/dist/compose/resolve-block-scalar.js +14 -17
  79. package/dist/compose/resolve-block-seq.d.ts +4 -4
  80. package/dist/compose/resolve-block-seq.js +21 -17
  81. package/dist/compose/resolve-end.d.ts +3 -2
  82. package/dist/compose/resolve-end.js +7 -8
  83. package/dist/compose/resolve-flow-collection.d.ts +4 -5
  84. package/dist/compose/resolve-flow-collection.js +157 -198
  85. package/dist/compose/resolve-flow-scalar.d.ts +5 -3
  86. package/dist/compose/resolve-flow-scalar.js +62 -26
  87. package/dist/compose/resolve-props.d.ts +16 -10
  88. package/dist/compose/resolve-props.js +72 -37
  89. package/dist/compose/util-contains-newline.d.ts +1 -1
  90. package/dist/compose/util-contains-newline.js +15 -12
  91. package/dist/compose/util-empty-scalar-position.d.ts +1 -1
  92. package/dist/compose/util-map-includes.d.ts +4 -0
  93. package/dist/compose/util-map-includes.js +19 -0
  94. package/dist/doc/Document.d.ts +35 -15
  95. package/dist/doc/Document.js +72 -97
  96. package/dist/doc/anchors.d.ts +24 -0
  97. package/dist/doc/anchors.js +77 -0
  98. package/dist/doc/createNode.d.ts +10 -11
  99. package/dist/doc/createNode.js +34 -18
  100. package/dist/doc/directives.d.ts +1 -0
  101. package/dist/doc/directives.js +5 -0
  102. package/dist/errors.d.ts +11 -8
  103. package/dist/errors.js +16 -12
  104. package/dist/index.d.ts +5 -3
  105. package/dist/index.js +3 -3
  106. package/dist/nodes/Alias.d.ts +19 -9
  107. package/dist/nodes/Alias.js +51 -28
  108. package/dist/nodes/Collection.d.ts +9 -17
  109. package/dist/nodes/Collection.js +22 -91
  110. package/dist/nodes/Node.d.ts +10 -4
  111. package/dist/nodes/Node.js +9 -0
  112. package/dist/nodes/Pair.d.ts +10 -13
  113. package/dist/nodes/Pair.js +15 -230
  114. package/dist/nodes/Scalar.d.ts +6 -3
  115. package/dist/nodes/YAMLMap.d.ts +3 -3
  116. package/dist/nodes/YAMLMap.js +8 -4
  117. package/dist/nodes/YAMLSeq.d.ts +3 -3
  118. package/dist/nodes/YAMLSeq.js +7 -4
  119. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  120. package/dist/nodes/addPairToJSMap.js +105 -0
  121. package/dist/nodes/toJS.d.ts +4 -5
  122. package/dist/nodes/toJS.js +12 -10
  123. package/dist/options.d.ts +41 -25
  124. package/dist/options.js +1 -2
  125. package/dist/parse/cst-scalar.d.ts +58 -0
  126. package/dist/parse/cst-scalar.js +223 -0
  127. package/dist/parse/cst-stringify.d.ts +8 -0
  128. package/dist/parse/cst-stringify.js +63 -0
  129. package/dist/parse/cst-visit.d.ts +39 -0
  130. package/dist/parse/cst-visit.js +99 -0
  131. package/dist/parse/{tokens.d.ts → cst.d.ts} +23 -7
  132. package/{browser/dist/parse/tokens.js → dist/parse/cst.js} +27 -1
  133. package/dist/parse/lexer.d.ts +7 -14
  134. package/dist/parse/lexer.js +150 -126
  135. package/dist/parse/parser.d.ts +31 -24
  136. package/dist/parse/parser.js +274 -198
  137. package/dist/public-api.d.ts +7 -7
  138. package/dist/public-api.js +11 -14
  139. package/dist/{doc → schema}/Schema.d.ts +6 -3
  140. package/dist/schema/Schema.js +30 -0
  141. package/dist/{tags/failsafe → schema/common}/map.d.ts +0 -0
  142. package/dist/{tags/failsafe → schema/common}/map.js +0 -0
  143. package/dist/schema/common/null.d.ts +4 -0
  144. package/dist/schema/common/null.js +15 -0
  145. package/dist/{tags/failsafe → schema/common}/seq.d.ts +0 -0
  146. package/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  147. package/dist/{tags/failsafe → schema/common}/string.d.ts +0 -0
  148. package/dist/{tags/failsafe → schema/common}/string.js +0 -0
  149. package/dist/schema/core/bool.d.ts +4 -0
  150. package/dist/schema/core/bool.js +21 -0
  151. package/dist/schema/core/float.d.ts +4 -0
  152. package/dist/schema/core/float.js +44 -0
  153. package/dist/schema/core/int.d.ts +4 -0
  154. package/dist/schema/core/int.js +42 -0
  155. package/dist/schema/core/schema.d.ts +1 -0
  156. package/dist/schema/core/schema.js +25 -0
  157. package/dist/schema/json/schema.d.ts +2 -0
  158. package/dist/{tags/json.js → schema/json/schema.js} +5 -6
  159. package/dist/schema/tags.d.ts +41 -0
  160. package/dist/schema/tags.js +82 -0
  161. package/dist/{tags → schema}/types.d.ts +3 -7
  162. package/dist/{tags → schema}/yaml-1.1/binary.d.ts +0 -0
  163. package/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  164. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  165. package/dist/schema/yaml-1.1/bool.js +29 -0
  166. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  167. package/dist/schema/yaml-1.1/float.js +47 -0
  168. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  169. package/dist/schema/yaml-1.1/int.js +76 -0
  170. package/dist/{tags → schema}/yaml-1.1/omap.d.ts +0 -0
  171. package/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  172. package/dist/{tags → schema}/yaml-1.1/pairs.d.ts +3 -2
  173. package/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  174. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  175. package/dist/schema/yaml-1.1/schema.js +39 -0
  176. package/dist/{tags → schema}/yaml-1.1/set.d.ts +1 -1
  177. package/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  178. package/dist/{tags → schema}/yaml-1.1/timestamp.d.ts +0 -0
  179. package/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  180. package/dist/stringify/stringify.d.ts +1 -2
  181. package/dist/stringify/stringify.js +8 -6
  182. package/dist/stringify/stringifyCollection.d.ts +21 -0
  183. package/dist/stringify/stringifyCollection.js +124 -0
  184. package/dist/stringify/stringifyComment.d.ts +2 -0
  185. package/dist/stringify/stringifyComment.js +17 -0
  186. package/dist/stringify/stringifyDocument.d.ts +3 -0
  187. package/dist/stringify/stringifyDocument.js +66 -0
  188. package/dist/stringify/stringifyPair.d.ts +3 -0
  189. package/dist/stringify/stringifyPair.js +113 -0
  190. package/dist/stringify/stringifyString.js +53 -52
  191. package/dist/test-events.d.ts +1 -2
  192. package/dist/test-events.js +25 -12
  193. package/dist/util.d.ts +1 -1
  194. package/dist/visit.d.ts +10 -1
  195. package/dist/visit.js +20 -1
  196. package/package.json +9 -8
  197. package/browser/dist/doc/Anchors.js +0 -102
  198. package/browser/dist/doc/Schema.js +0 -27
  199. package/browser/dist/doc/getSchemaTags.js +0 -30
  200. package/browser/dist/stringify/addComment.js +0 -17
  201. package/browser/dist/tags/core.js +0 -109
  202. package/browser/dist/tags/failsafe/index.js +0 -7
  203. package/browser/dist/tags/index.js +0 -34
  204. package/browser/dist/tags/yaml-1.1/index.js +0 -152
  205. package/dist/doc/Anchors.d.ts +0 -36
  206. package/dist/doc/Anchors.js +0 -104
  207. package/dist/doc/Schema.js +0 -29
  208. package/dist/doc/getSchemaTags.d.ts +0 -3
  209. package/dist/doc/getSchemaTags.js +0 -32
  210. package/dist/stringify/addComment.d.ts +0 -2
  211. package/dist/stringify/addComment.js +0 -20
  212. package/dist/tags/core.d.ts +0 -14
  213. package/dist/tags/core.js +0 -119
  214. package/dist/tags/failsafe/index.d.ts +0 -1
  215. package/dist/tags/failsafe/index.js +0 -9
  216. package/dist/tags/index.d.ts +0 -31
  217. package/dist/tags/index.js +0 -37
  218. package/dist/tags/json.d.ts +0 -2
  219. package/dist/tags/yaml-1.1/index.d.ts +0 -2
  220. package/dist/tags/yaml-1.1/index.js +0 -154
@@ -1,11 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var createNode = require('../doc/createNode.js');
4
- var log = require('../log.js');
5
- var addComment = require('../stringify/addComment.js');
6
- var stringify = require('../stringify/stringify.js');
7
- var Scalar = require('./Scalar.js');
8
- var toJS = require('./toJS.js');
4
+ var stringifyPair = require('../stringify/stringifyPair.js');
5
+ var addPairToJSMap = require('./addPairToJSMap.js');
9
6
  var Node = require('./Node.js');
10
7
 
11
8
  function createPair(key, value, ctx) {
@@ -13,241 +10,29 @@ function createPair(key, value, ctx) {
13
10
  const v = createNode.createNode(value, undefined, ctx);
14
11
  return new Pair(k, v);
15
12
  }
16
- const isMergeKey = (key) => key === Pair.MERGE_KEY ||
17
- (Node.isScalar(key) &&
18
- key.value === Pair.MERGE_KEY &&
19
- (!key.type || key.type === Scalar.Scalar.PLAIN));
20
- // If the value associated with a merge key is a single mapping node, each of
21
- // its key/value pairs is inserted into the current mapping, unless the key
22
- // already exists in it. If the value associated with the merge key is a
23
- // sequence, then this sequence is expected to contain mapping nodes and each
24
- // of these nodes is merged in turn according to its order in the sequence.
25
- // Keys in mapping nodes earlier in the sequence override keys specified in
26
- // later mapping nodes. -- http://yaml.org/type/merge.html
27
- function mergeToJSMap(ctx, map, value) {
28
- if (!Node.isAlias(value) || !Node.isMap(value.source))
29
- throw new Error('Merge sources must be map aliases');
30
- const srcMap = value.source.toJSON(null, ctx, Map);
31
- for (const [key, value] of srcMap) {
32
- if (map instanceof Map) {
33
- if (!map.has(key))
34
- map.set(key, value);
35
- }
36
- else if (map instanceof Set) {
37
- map.add(key);
38
- }
39
- else if (!Object.prototype.hasOwnProperty.call(map, key)) {
40
- Object.defineProperty(map, key, {
41
- value,
42
- writable: true,
43
- enumerable: true,
44
- configurable: true
45
- });
46
- }
47
- }
48
- return map;
49
- }
50
- class Pair extends Node.NodeBase {
13
+ class Pair {
51
14
  constructor(key, value = null) {
52
- super(Node.PAIR);
15
+ Object.defineProperty(this, Node.NODE_TYPE, { value: Node.PAIR });
53
16
  this.key = key;
54
17
  this.value = value;
55
18
  }
56
- // @ts-ignore This is fine.
57
- get commentBefore() {
58
- return Node.isNode(this.key) ? this.key.commentBefore : undefined;
59
- }
60
- set commentBefore(cb) {
61
- if (this.key == null)
62
- this.key = new Scalar.Scalar(null); // FIXME
63
- if (Node.isNode(this.key))
64
- this.key.commentBefore = cb;
65
- else {
66
- const msg = 'Pair.commentBefore is an alias for Pair.key.commentBefore. To set it, the key must be a Node.';
67
- throw new Error(msg);
68
- }
69
- }
70
- // @ts-ignore This is fine.
71
- get spaceBefore() {
72
- return Node.isNode(this.key) ? this.key.spaceBefore : undefined;
73
- }
74
- set spaceBefore(sb) {
75
- if (this.key == null)
76
- this.key = new Scalar.Scalar(null); // FIXME
77
- if (Node.isNode(this.key))
78
- this.key.spaceBefore = sb;
79
- else {
80
- const msg = 'Pair.spaceBefore is an alias for Pair.key.spaceBefore. To set it, the key must be a Node.';
81
- throw new Error(msg);
82
- }
83
- }
84
- addToJSMap(ctx, map) {
85
- if (ctx && ctx.doc.schema.merge && isMergeKey(this.key)) {
86
- if (Node.isSeq(this.value))
87
- for (const it of this.value.items)
88
- mergeToJSMap(ctx, map, it);
89
- else if (Array.isArray(this.value))
90
- for (const it of this.value)
91
- mergeToJSMap(ctx, map, it);
92
- else
93
- mergeToJSMap(ctx, map, this.value);
94
- }
95
- else {
96
- const key = toJS.toJS(this.key, '', ctx);
97
- if (map instanceof Map) {
98
- const value = toJS.toJS(this.value, key, ctx);
99
- map.set(key, value);
100
- }
101
- else if (map instanceof Set) {
102
- map.add(key);
103
- }
104
- else {
105
- const stringKey = stringifyKey(this.key, key, ctx);
106
- const value = toJS.toJS(this.value, stringKey, ctx);
107
- if (stringKey in map)
108
- Object.defineProperty(map, stringKey, {
109
- value,
110
- writable: true,
111
- enumerable: true,
112
- configurable: true
113
- });
114
- else
115
- map[stringKey] = value;
116
- }
117
- }
118
- return map;
19
+ clone(schema) {
20
+ let { key, value } = this;
21
+ if (Node.isNode(key))
22
+ key = key.clone(schema);
23
+ if (Node.isNode(value))
24
+ value = value.clone(schema);
25
+ return new Pair(key, value);
119
26
  }
120
27
  toJSON(_, ctx) {
121
28
  const pair = ctx && ctx.mapAsMap ? new Map() : {};
122
- return this.addToJSMap(ctx, pair);
29
+ return addPairToJSMap.addPairToJSMap(ctx, pair, this);
123
30
  }
124
31
  toString(ctx, onComment, onChompKeep) {
125
- if (!ctx || !ctx.doc)
126
- return JSON.stringify(this);
127
- const { allNullValues, doc, indent, indentStep, options: { indentSeq, simpleKeys } } = ctx;
128
- let { key, value } = this;
129
- let keyComment = (Node.isNode(key) && key.comment) || null;
130
- if (simpleKeys) {
131
- if (keyComment) {
132
- throw new Error('With simple keys, key nodes cannot have comments');
133
- }
134
- if (Node.isCollection(key)) {
135
- const msg = 'With simple keys, collection cannot be used as a key value';
136
- throw new Error(msg);
137
- }
138
- }
139
- let explicitKey = !simpleKeys &&
140
- (!key ||
141
- (keyComment && value == null) ||
142
- Node.isCollection(key) ||
143
- (Node.isScalar(key)
144
- ? key.type === Scalar.Scalar.BLOCK_FOLDED || key.type === Scalar.Scalar.BLOCK_LITERAL
145
- : typeof key === 'object'));
146
- ctx = Object.assign({}, ctx, {
147
- allNullValues: false,
148
- implicitKey: !explicitKey && (simpleKeys || !allNullValues),
149
- indent: indent + indentStep
150
- });
151
- let chompKeep = false;
152
- let str = stringify.stringify(key, ctx, () => (keyComment = null), () => (chompKeep = true));
153
- if (!explicitKey && !ctx.inFlow && str.length > 1024) {
154
- if (simpleKeys)
155
- throw new Error('With simple keys, single line scalar must not span more than 1024 characters');
156
- explicitKey = true;
157
- }
158
- if ((allNullValues && (!simpleKeys || ctx.inFlow)) ||
159
- (value == null && (explicitKey || ctx.inFlow))) {
160
- str = addComment.addComment(str, ctx.indent, keyComment);
161
- if (this.comment) {
162
- if (keyComment && !this.comment.includes('\n'))
163
- str += `\n${ctx.indent || ''}#${this.comment}`;
164
- else
165
- str = addComment.addComment(str, ctx.indent, this.comment);
166
- if (onComment)
167
- onComment();
168
- }
169
- else if (chompKeep && !keyComment && onChompKeep)
170
- onChompKeep();
171
- return ctx.inFlow && !explicitKey ? str : `? ${str}`;
172
- }
173
- str = explicitKey
174
- ? `? ${addComment.addComment(str, ctx.indent, keyComment)}\n${indent}:`
175
- : addComment.addComment(`${str}:`, ctx.indent, keyComment);
176
- if (this.comment) {
177
- if (keyComment && !explicitKey && !this.comment.includes('\n'))
178
- str += `\n${ctx.indent || ''}#${this.comment}`;
179
- else
180
- str = addComment.addComment(str, ctx.indent, this.comment);
181
- if (onComment)
182
- onComment();
183
- }
184
- let vcb = '';
185
- let valueComment = null;
186
- if (Node.isNode(value)) {
187
- if (value.spaceBefore)
188
- vcb = '\n';
189
- if (value.commentBefore) {
190
- const cs = value.commentBefore.replace(/^/gm, `${ctx.indent}#`);
191
- vcb += `\n${cs}`;
192
- }
193
- valueComment = value.comment;
194
- }
195
- else if (value && typeof value === 'object') {
196
- value = doc.createNode(value);
197
- }
198
- ctx.implicitKey = false;
199
- if (!explicitKey && !keyComment && !this.comment && Node.isScalar(value))
200
- ctx.indentAtStart = str.length + 1;
201
- chompKeep = false;
202
- if (!indentSeq &&
203
- indentStep.length >= 2 &&
204
- !ctx.inFlow &&
205
- !explicitKey &&
206
- Node.isSeq(value) &&
207
- !value.flow &&
208
- !value.tag &&
209
- !doc.anchors.getName(value)) {
210
- // If indentSeq === false, consider '- ' as part of indentation where possible
211
- ctx.indent = ctx.indent.substr(2);
212
- }
213
- const valueStr = stringify.stringify(value, ctx, () => (valueComment = null), () => (chompKeep = true));
214
- let ws = ' ';
215
- if (vcb || keyComment || this.comment) {
216
- ws = `${vcb}\n${ctx.indent}`;
217
- }
218
- else if (!explicitKey && Node.isCollection(value)) {
219
- const flow = valueStr[0] === '[' || valueStr[0] === '{';
220
- if (!flow || valueStr.includes('\n'))
221
- ws = `\n${ctx.indent}`;
222
- }
223
- else if (valueStr[0] === '\n')
224
- ws = '';
225
- if (chompKeep && !valueComment && onChompKeep)
226
- onChompKeep();
227
- return addComment.addComment(str + ws + valueStr, ctx.indent, valueComment);
228
- }
229
- }
230
- Pair.MERGE_KEY = '<<';
231
- function stringifyKey(key, jsKey, ctx) {
232
- if (jsKey === null)
233
- return '';
234
- if (typeof jsKey !== 'object')
235
- return String(jsKey);
236
- if (Node.isNode(key) && ctx && ctx.doc) {
237
- const strCtx = stringify.createStringifyContext(ctx.doc, {});
238
- strCtx.inFlow = true;
239
- strCtx.inStringifyKey = true;
240
- const strKey = key.toString(strCtx);
241
- if (!ctx.mapKeyWarned) {
242
- let jsonStr = JSON.stringify(strKey);
243
- if (jsonStr.length > 40)
244
- jsonStr = jsonStr.substring(0, 36) + '..."';
245
- log.warn(ctx.doc.options.logLevel, `Keys with collection values will be stringified due to JS Object restrictions: ${jsonStr}. Set mapAsMap: true to use object keys.`);
246
- ctx.mapKeyWarned = true;
247
- }
248
- return strKey;
32
+ return ctx && ctx.doc
33
+ ? stringifyPair.stringifyPair(this, ctx, onComment, onChompKeep)
34
+ : JSON.stringify(this);
249
35
  }
250
- return JSON.stringify(jsKey);
251
36
  }
252
37
 
253
38
  exports.Pair = Pair;
@@ -1,9 +1,9 @@
1
- import { NodeBase } from './Node.js';
1
+ import { NodeBase, Range } from './Node.js';
2
2
  import { ToJSContext } from './toJS.js';
3
3
  export declare const isScalarValue: (value: unknown) => boolean;
4
4
  export declare namespace Scalar {
5
5
  interface Parsed extends Scalar {
6
- range: [number, number];
6
+ range: Range;
7
7
  source: string;
8
8
  }
9
9
  type BLOCK_FOLDED = 'BLOCK_FOLDED';
@@ -20,7 +20,8 @@ export declare class Scalar<T = unknown> extends NodeBase {
20
20
  static readonly QUOTE_DOUBLE = "QUOTE_DOUBLE";
21
21
  static readonly QUOTE_SINGLE = "QUOTE_SINGLE";
22
22
  value: T;
23
- type?: Scalar.Type;
23
+ /** An optional anchor on this node. Used by alias nodes. */
24
+ anchor?: string;
24
25
  /**
25
26
  * By default (undefined), numbers use decimal notation.
26
27
  * The YAML 1.2 core schema only supports 'HEX' and 'OCT'.
@@ -31,6 +32,8 @@ export declare class Scalar<T = unknown> extends NodeBase {
31
32
  minFractionDigits?: number;
32
33
  /** Set during parsing to the source string value */
33
34
  source?: string;
35
+ /** The scalar style used for the node's string representation */
36
+ type?: Scalar.Type;
34
37
  constructor(value: T);
35
38
  toJSON(arg?: any, ctx?: ToJSContext): any;
36
39
  toString(): string;
@@ -1,14 +1,14 @@
1
- import type { Schema } from '../doc/Schema.js';
1
+ import type { Schema } from '../schema/Schema.js';
2
2
  import type { StringifyContext } from '../stringify/stringify.js';
3
3
  import { Collection } from './Collection.js';
4
- import { ParsedNode } from './Node.js';
4
+ import { ParsedNode, Range } from './Node.js';
5
5
  import { Pair } from './Pair.js';
6
6
  import type { ToJSContext } from './toJS.js';
7
7
  export declare function findPair<K = unknown, V = unknown>(items: Iterable<Pair<K, V>>, key: unknown): Pair<K, V> | undefined;
8
8
  export declare namespace YAMLMap {
9
9
  interface Parsed<K extends ParsedNode = ParsedNode, V extends ParsedNode | null = ParsedNode | null> extends YAMLMap<K, V> {
10
10
  items: Pair<K, V>[];
11
- range: [number, number];
11
+ range: Range;
12
12
  }
13
13
  }
14
14
  export declare class YAMLMap<K = unknown, V = unknown> extends Collection {
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var stringifyCollection = require('../stringify/stringifyCollection.js');
4
+ var addPairToJSMap = require('./addPairToJSMap.js');
3
5
  var Collection = require('./Collection.js');
4
6
  var Node = require('./Node.js');
5
7
  var Pair = require('./Pair.js');
@@ -91,7 +93,7 @@ class YAMLMap extends Collection.Collection {
91
93
  if (ctx && ctx.onCreate)
92
94
  ctx.onCreate(map);
93
95
  for (const item of this.items)
94
- item.addToJSMap(ctx, map);
96
+ addPairToJSMap.addPairToJSMap(ctx, map, item);
95
97
  return map;
96
98
  }
97
99
  toString(ctx, onComment, onChompKeep) {
@@ -103,11 +105,13 @@ class YAMLMap extends Collection.Collection {
103
105
  }
104
106
  if (!ctx.allNullValues && this.hasAllNullValues(false))
105
107
  ctx = Object.assign({}, ctx, { allNullValues: true });
106
- return super._toString(ctx, {
108
+ return stringifyCollection.stringifyCollection(this, ctx, {
107
109
  blockItem: n => n.str,
108
110
  flowChars: { start: '{', end: '}' },
109
- itemIndent: ctx.indent || ''
110
- }, onComment, onChompKeep);
111
+ itemIndent: ctx.indent || '',
112
+ onChompKeep,
113
+ onComment
114
+ });
111
115
  }
112
116
  }
113
117
 
@@ -1,13 +1,13 @@
1
- import type { Schema } from '../doc/Schema.js';
1
+ import type { Schema } from '../schema/Schema.js';
2
2
  import type { StringifyContext } from '../stringify/stringify.js';
3
3
  import { Collection } from './Collection.js';
4
- import { ParsedNode } from './Node.js';
4
+ import { ParsedNode, Range } from './Node.js';
5
5
  import type { Pair } from './Pair.js';
6
6
  import { ToJSContext } from './toJS.js';
7
7
  export declare namespace YAMLSeq {
8
8
  interface Parsed<T extends ParsedNode | Pair<ParsedNode, ParsedNode | null> = ParsedNode> extends YAMLSeq<T> {
9
9
  items: T[];
10
- range: [number, number];
10
+ range: Range;
11
11
  }
12
12
  }
13
13
  export declare class YAMLSeq<T = unknown> extends Collection {
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var stringifyCollection = require('../stringify/stringifyCollection.js');
3
4
  var Collection = require('./Collection.js');
4
5
  var Node = require('./Node.js');
5
6
  var Scalar = require('./Scalar.js');
@@ -85,11 +86,13 @@ class YAMLSeq extends Collection.Collection {
85
86
  toString(ctx, onComment, onChompKeep) {
86
87
  if (!ctx)
87
88
  return JSON.stringify(this);
88
- return super._toString(ctx, {
89
- blockItem: n => (n.type === 'comment' ? n.str : `- ${n.str}`),
89
+ return stringifyCollection.stringifyCollection(this, ctx, {
90
+ blockItem: n => (n.comment ? n.str : `- ${n.str}`),
90
91
  flowChars: { start: '[', end: ']' },
91
- itemIndent: (ctx.indent || '') + ' '
92
- }, onComment, onChompKeep);
92
+ itemIndent: (ctx.indent || '') + ' ',
93
+ onChompKeep,
94
+ onComment
95
+ });
93
96
  }
94
97
  }
95
98
  function asItemIndex(key) {
@@ -0,0 +1,3 @@
1
+ import type { Pair } from './Pair.js';
2
+ import { ToJSContext } from './toJS.js';
3
+ export declare function addPairToJSMap(ctx: ToJSContext | undefined, map: Map<unknown, unknown> | Set<unknown> | Record<string | number | symbol, unknown>, { key, value }: Pair): Map<unknown, unknown> | Set<unknown> | Record<string | number | symbol, unknown>;
@@ -0,0 +1,105 @@
1
+ 'use strict';
2
+
3
+ var log = require('../log.js');
4
+ var stringify = require('../stringify/stringify.js');
5
+ var Node = require('./Node.js');
6
+ var Scalar = require('./Scalar.js');
7
+ var toJS = require('./toJS.js');
8
+
9
+ const MERGE_KEY = '<<';
10
+ function addPairToJSMap(ctx, map, { key, value }) {
11
+ if (ctx && ctx.doc.schema.merge && isMergeKey(key)) {
12
+ if (Node.isSeq(value))
13
+ for (const it of value.items)
14
+ mergeToJSMap(ctx, map, it);
15
+ else if (Array.isArray(value))
16
+ for (const it of value)
17
+ mergeToJSMap(ctx, map, it);
18
+ else
19
+ mergeToJSMap(ctx, map, value);
20
+ }
21
+ else {
22
+ const jsKey = toJS.toJS(key, '', ctx);
23
+ if (map instanceof Map) {
24
+ map.set(jsKey, toJS.toJS(value, jsKey, ctx));
25
+ }
26
+ else if (map instanceof Set) {
27
+ map.add(jsKey);
28
+ }
29
+ else {
30
+ const stringKey = stringifyKey(key, jsKey, ctx);
31
+ const jsValue = toJS.toJS(value, stringKey, ctx);
32
+ if (stringKey in map)
33
+ Object.defineProperty(map, stringKey, {
34
+ value: jsValue,
35
+ writable: true,
36
+ enumerable: true,
37
+ configurable: true
38
+ });
39
+ else
40
+ map[stringKey] = jsValue;
41
+ }
42
+ }
43
+ return map;
44
+ }
45
+ const isMergeKey = (key) => key === MERGE_KEY ||
46
+ (Node.isScalar(key) &&
47
+ key.value === MERGE_KEY &&
48
+ (!key.type || key.type === Scalar.Scalar.PLAIN));
49
+ // If the value associated with a merge key is a single mapping node, each of
50
+ // its key/value pairs is inserted into the current mapping, unless the key
51
+ // already exists in it. If the value associated with the merge key is a
52
+ // sequence, then this sequence is expected to contain mapping nodes and each
53
+ // of these nodes is merged in turn according to its order in the sequence.
54
+ // Keys in mapping nodes earlier in the sequence override keys specified in
55
+ // later mapping nodes. -- http://yaml.org/type/merge.html
56
+ function mergeToJSMap(ctx, map, value) {
57
+ const source = ctx && Node.isAlias(value) ? value.resolve(ctx.doc) : value;
58
+ if (!Node.isMap(source))
59
+ throw new Error('Merge sources must be maps or map aliases');
60
+ const srcMap = source.toJSON(null, ctx, Map);
61
+ for (const [key, value] of srcMap) {
62
+ if (map instanceof Map) {
63
+ if (!map.has(key))
64
+ map.set(key, value);
65
+ }
66
+ else if (map instanceof Set) {
67
+ map.add(key);
68
+ }
69
+ else if (!Object.prototype.hasOwnProperty.call(map, key)) {
70
+ Object.defineProperty(map, key, {
71
+ value,
72
+ writable: true,
73
+ enumerable: true,
74
+ configurable: true
75
+ });
76
+ }
77
+ }
78
+ return map;
79
+ }
80
+ function stringifyKey(key, jsKey, ctx) {
81
+ if (jsKey === null)
82
+ return '';
83
+ if (typeof jsKey !== 'object')
84
+ return String(jsKey);
85
+ if (Node.isNode(key) && ctx && ctx.doc) {
86
+ const strCtx = stringify.createStringifyContext(ctx.doc, {});
87
+ strCtx.anchors = new Set();
88
+ for (const node of ctx.anchors.keys())
89
+ strCtx.anchors.add(node.anchor);
90
+ strCtx.inFlow = true;
91
+ strCtx.inStringifyKey = true;
92
+ const strKey = key.toString(strCtx);
93
+ if (!ctx.mapKeyWarned) {
94
+ let jsonStr = JSON.stringify(strKey);
95
+ if (jsonStr.length > 40)
96
+ jsonStr = jsonStr.substring(0, 36) + '..."';
97
+ log.warn(ctx.doc.options.logLevel, `Keys with collection values will be stringified due to JS Object restrictions: ${jsonStr}. Set mapAsMap: true to use object keys.`);
98
+ ctx.mapKeyWarned = true;
99
+ }
100
+ return strKey;
101
+ }
102
+ return JSON.stringify(jsKey);
103
+ }
104
+
105
+ exports.addPairToJSMap = addPairToJSMap;
@@ -1,14 +1,13 @@
1
1
  import type { Document } from '../doc/Document.js';
2
2
  import type { stringify } from '../stringify/stringify.js';
3
- import type { Node } from './Node.js';
4
- export interface ToJSAnchorValue {
5
- alias: string[];
3
+ import { Node } from './Node.js';
4
+ export interface AnchorData {
6
5
  aliasCount: number;
7
6
  count: number;
8
- res?: unknown;
7
+ res: unknown;
9
8
  }
10
9
  export interface ToJSContext {
11
- anchors: Map<Node, ToJSAnchorValue> | null;
10
+ anchors: Map<Node, AnchorData>;
12
11
  doc: Document;
13
12
  keep: boolean;
14
13
  mapAsMap: boolean;
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var Node = require('./Node.js');
4
+
3
5
  /**
4
6
  * Recursively convert any node or its contents to native JavaScript
5
7
  *
@@ -14,20 +16,20 @@ function toJS(value, arg, ctx) {
14
16
  if (Array.isArray(value))
15
17
  return value.map((v, i) => toJS(v, String(i), ctx));
16
18
  if (value && typeof value.toJSON === 'function') {
17
- if (!ctx)
18
- return value.toJSON(arg);
19
- const anchor = ctx.anchors && ctx.anchors.get(value);
20
- if (anchor)
21
- ctx.onCreate = res => {
22
- anchor.res = res;
23
- delete ctx.onCreate;
24
- };
19
+ if (!ctx || !Node.hasAnchor(value))
20
+ return value.toJSON(arg, ctx);
21
+ const data = { aliasCount: 0, count: 1, res: undefined };
22
+ ctx.anchors.set(value, data);
23
+ ctx.onCreate = res => {
24
+ data.res = res;
25
+ delete ctx.onCreate;
26
+ };
25
27
  const res = value.toJSON(arg, ctx);
26
- if (anchor && ctx.onCreate)
28
+ if (ctx.onCreate)
27
29
  ctx.onCreate(res);
28
30
  return res;
29
31
  }
30
- if (!(ctx && ctx.keep) && typeof value === 'bigint')
32
+ if (typeof value === 'bigint' && !(ctx && ctx.keep))
31
33
  return Number(value);
32
34
  return value;
33
35
  }