yaml 1.6.0 → 1.8.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 (112) hide show
  1. package/browser/dist/Anchors.js +24 -39
  2. package/browser/dist/Document.js +99 -111
  3. package/browser/dist/addComment.js +2 -11
  4. package/browser/dist/constants.js +3 -11
  5. package/browser/dist/cst/Alias.js +19 -36
  6. package/browser/dist/cst/BlankLine.js +25 -38
  7. package/browser/dist/cst/BlockValue.js +29 -51
  8. package/browser/dist/cst/Collection.js +42 -65
  9. package/browser/dist/cst/CollectionItem.js +57 -58
  10. package/browser/dist/cst/Comment.js +19 -35
  11. package/browser/dist/cst/Directive.js +20 -56
  12. package/browser/dist/cst/Document.js +47 -71
  13. package/browser/dist/cst/FlowCollection.js +38 -61
  14. package/browser/dist/cst/Node.js +30 -47
  15. package/browser/dist/cst/ParseContext.js +100 -140
  16. package/browser/dist/cst/PlainValue.js +22 -39
  17. package/browser/dist/cst/QuoteDouble.js +26 -42
  18. package/browser/dist/cst/QuoteSingle.js +25 -41
  19. package/browser/dist/cst/Range.js +9 -19
  20. package/browser/dist/cst/parse.js +5 -16
  21. package/browser/dist/cst/source-utils.js +168 -0
  22. package/browser/dist/errors.js +71 -74
  23. package/browser/dist/foldFlowLines.js +4 -15
  24. package/browser/dist/index.js +26 -45
  25. package/browser/dist/listTagNames.js +10 -24
  26. package/browser/dist/schema/Alias.js +33 -50
  27. package/browser/dist/schema/Collection.js +54 -65
  28. package/browser/dist/schema/Map.js +39 -49
  29. package/browser/dist/schema/Merge.js +33 -53
  30. package/browser/dist/schema/Node.js +3 -12
  31. package/browser/dist/schema/Pair.js +53 -56
  32. package/browser/dist/schema/Scalar.js +18 -33
  33. package/browser/dist/schema/Seq.js +23 -40
  34. package/browser/dist/schema/index.js +67 -81
  35. package/browser/dist/schema/parseMap.js +63 -70
  36. package/browser/dist/schema/parseSeq.js +45 -44
  37. package/browser/dist/schema/parseUtils.js +47 -11
  38. package/browser/dist/stringify.js +43 -48
  39. package/browser/dist/tags/core.js +30 -37
  40. package/browser/dist/tags/failsafe/index.js +4 -17
  41. package/browser/dist/tags/failsafe/map.js +17 -26
  42. package/browser/dist/tags/failsafe/seq.js +7 -18
  43. package/browser/dist/tags/failsafe/string.js +7 -19
  44. package/browser/dist/tags/index.js +35 -49
  45. package/browser/dist/tags/json.js +11 -38
  46. package/browser/dist/tags/options.js +8 -20
  47. package/browser/dist/tags/yaml-1.1/binary.js +16 -29
  48. package/browser/dist/tags/yaml-1.1/index.js +34 -54
  49. package/browser/dist/tags/yaml-1.1/omap.js +39 -58
  50. package/browser/dist/tags/yaml-1.1/pairs.js +15 -34
  51. package/browser/dist/tags/yaml-1.1/set.js +35 -61
  52. package/browser/dist/tags/yaml-1.1/timestamp.js +6 -16
  53. package/browser/dist/test-events.js +7 -19
  54. package/browser/dist/toJSON.js +1 -8
  55. package/browser/dist/warnings.js +28 -0
  56. package/browser/map.js +1 -1
  57. package/browser/pair.js +1 -1
  58. package/browser/scalar.js +1 -1
  59. package/browser/schema.js +1 -1
  60. package/browser/seq.js +1 -1
  61. package/browser/types/binary.js +1 -1
  62. package/browser/types/omap.js +1 -1
  63. package/browser/types/pairs.js +1 -1
  64. package/browser/types/set.js +1 -1
  65. package/browser/types/timestamp.js +1 -1
  66. package/dist/Document.js +21 -9
  67. package/dist/cst/BlankLine.js +4 -0
  68. package/dist/cst/BlockValue.js +1 -1
  69. package/dist/cst/CollectionItem.js +27 -6
  70. package/dist/cst/Directive.js +0 -16
  71. package/dist/cst/Node.js +7 -9
  72. package/dist/cst/ParseContext.js +41 -55
  73. package/dist/cst/PlainValue.js +1 -1
  74. package/dist/cst/source-utils.js +178 -0
  75. package/dist/errors.js +37 -3
  76. package/dist/index.js +5 -4
  77. package/dist/schema/Alias.js +2 -1
  78. package/dist/schema/Collection.js +22 -5
  79. package/dist/schema/Map.js +12 -5
  80. package/dist/schema/Merge.js +4 -5
  81. package/dist/schema/Pair.js +20 -2
  82. package/dist/schema/index.js +14 -8
  83. package/dist/schema/parseMap.js +20 -5
  84. package/dist/schema/parseSeq.js +19 -6
  85. package/dist/schema/parseUtils.js +54 -6
  86. package/dist/stringify.js +21 -11
  87. package/dist/tags/core.js +27 -10
  88. package/dist/tags/failsafe/map.js +5 -1
  89. package/dist/tags/failsafe/seq.js +1 -1
  90. package/dist/tags/index.js +13 -1
  91. package/dist/tags/json.js +2 -9
  92. package/dist/tags/yaml-1.1/index.js +6 -6
  93. package/dist/tags/yaml-1.1/omap.js +3 -5
  94. package/dist/tags/yaml-1.1/pairs.js +1 -1
  95. package/dist/tags/yaml-1.1/set.js +3 -1
  96. package/dist/{deprecation.js → warnings.js} +12 -7
  97. package/map.js +1 -1
  98. package/package.json +35 -17
  99. package/pair.js +1 -1
  100. package/scalar.js +1 -1
  101. package/schema.js +1 -1
  102. package/seq.js +1 -1
  103. package/types/binary.js +1 -1
  104. package/types/omap.js +1 -1
  105. package/types/pairs.js +1 -1
  106. package/types/set.js +1 -1
  107. package/types/timestamp.js +1 -1
  108. package/types.mjs +20 -0
  109. package/util.mjs +24 -0
  110. package/browser/dist/cst/getLinePos.js +0 -79
  111. package/browser/dist/deprecation.js +0 -34
  112. package/dist/cst/getLinePos.js +0 -79
@@ -7,6 +7,8 @@ exports.default = void 0;
7
7
 
8
8
  var _addComment = _interopRequireDefault(require("../addComment"));
9
9
 
10
+ var _constants = require("../constants");
11
+
10
12
  var _toJSON = _interopRequireDefault(require("../toJSON"));
11
13
 
12
14
  var _Collection = _interopRequireDefault(require("./Collection"));
@@ -71,12 +73,27 @@ class Pair extends _Node.default {
71
73
 
72
74
  toString(ctx, onComment, onChompKeep) {
73
75
  if (!ctx || !ctx.doc) return JSON.stringify(this);
76
+ const {
77
+ simpleKeys
78
+ } = ctx.doc.options;
74
79
  let {
75
80
  key,
76
81
  value
77
82
  } = this;
78
83
  let keyComment = key instanceof _Node.default && key.comment;
79
- const explicitKey = !key || keyComment || key instanceof _Collection.default;
84
+
85
+ if (simpleKeys) {
86
+ if (keyComment) {
87
+ throw new Error('With simple keys, key nodes cannot have comments');
88
+ }
89
+
90
+ if (key instanceof _Collection.default) {
91
+ const msg = 'With simple keys, collection cannot be used as a key value';
92
+ throw new Error(msg);
93
+ }
94
+ }
95
+
96
+ const explicitKey = !simpleKeys && (!key || keyComment || key instanceof _Collection.default || key.type === _constants.Type.BLOCK_FOLDED || key.type === _constants.Type.BLOCK_LITERAL);
80
97
  const {
81
98
  doc,
82
99
  indent
@@ -89,7 +106,7 @@ class Pair extends _Node.default {
89
106
  let str = doc.schema.stringify(key, ctx, () => keyComment = null, () => chompKeep = true);
90
107
  str = (0, _addComment.default)(str, ctx.indent, keyComment);
91
108
 
92
- if (ctx.allNullValues) {
109
+ if (ctx.allNullValues && !simpleKeys) {
93
110
  if (this.comment) {
94
111
  str = (0, _addComment.default)(str, ctx.indent, this.comment);
95
112
  if (onComment) onComment();
@@ -123,6 +140,7 @@ class Pair extends _Node.default {
123
140
  }
124
141
 
125
142
  ctx.implicitKey = false;
143
+ if (!explicitKey && !this.comment && value instanceof _Scalar.default) ctx.indentAtStart = str.length + 1;
126
144
  chompKeep = false;
127
145
  const valueStr = doc.schema.stringify(value, ctx, () => valueComment = null, () => chompKeep = true);
128
146
  let ws = ' ';
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _deprecation = require("../deprecation");
8
+ var _warnings = require("../warnings");
9
9
 
10
10
  var _constants = require("../constants");
11
11
 
@@ -44,10 +44,12 @@ class Schema {
44
44
  customTags,
45
45
  merge,
46
46
  schema,
47
+ sortMapEntries,
47
48
  tags: deprecatedCustomTags
48
49
  }) {
49
50
  this.merge = !!merge;
50
51
  this.name = schema;
52
+ this.sortMapEntries = sortMapEntries === true ? (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0 : sortMapEntries || null;
51
53
  this.tags = _tags.schemas[schema.replace(/\W/g, '')]; // 'yaml-1.1' -> 'yaml11'
52
54
 
53
55
  if (!this.tags) {
@@ -57,7 +59,7 @@ class Schema {
57
59
 
58
60
  if (!customTags && deprecatedCustomTags) {
59
61
  customTags = deprecatedCustomTags;
60
- (0, _deprecation.warnOptionDeprecation)('tags', 'customTags');
62
+ (0, _warnings.warnOptionDeprecation)('tags', 'customTags');
61
63
  }
62
64
 
63
65
  if (Array.isArray(customTags)) {
@@ -83,6 +85,7 @@ class Schema {
83
85
  }
84
86
 
85
87
  createNode(value, wrapScalars, tag, ctx) {
88
+ if (value instanceof _Node.default) return value;
86
89
  let tagObj;
87
90
 
88
91
  if (tag) {
@@ -113,7 +116,7 @@ class Schema {
113
116
  const obj = {};
114
117
 
115
118
  if (value && typeof value === 'object' && ctx.prevObjects) {
116
- const prev = ctx.prevObjects.find(o => o.value === value);
119
+ const prev = ctx.prevObjects.get(value);
117
120
 
118
121
  if (prev) {
119
122
  const alias = new _Alias.default(prev); // leaves source dirty; must be cleaned by caller
@@ -123,10 +126,11 @@ class Schema {
123
126
  }
124
127
 
125
128
  obj.value = value;
126
- ctx.prevObjects.push(obj);
129
+ ctx.prevObjects.set(value, obj);
127
130
  }
128
131
 
129
132
  obj.node = tagObj.createNode ? tagObj.createNode(this, value, ctx) : wrapScalars ? new _Scalar.default(value) : value;
133
+ if (tag && obj.node instanceof _Node.default) obj.node.tag = tag;
130
134
  return obj.node;
131
135
  }
132
136
 
@@ -186,6 +190,7 @@ class Schema {
186
190
  }
187
191
  }
188
192
  } catch (error) {
193
+ /* istanbul ignore if */
189
194
  if (!error.source) error.source = node;
190
195
  doc.errors.push(error);
191
196
  node.resolved = null;
@@ -198,8 +203,9 @@ class Schema {
198
203
 
199
204
  resolveNodeWithFallback(doc, node, tagName) {
200
205
  const res = this.resolveNode(doc, node, tagName);
201
- if (node.hasOwnProperty('resolved')) return res;
206
+ if (Object.prototype.hasOwnProperty.call(node, 'resolved')) return res;
202
207
  const fallback = isMap(node) ? Schema.defaultTags.MAP : isSeq(node) ? Schema.defaultTags.SEQ : Schema.defaultTags.STR;
208
+ /* istanbul ignore else */
203
209
 
204
210
  if (fallback) {
205
211
  doc.warnings.push(new _errors.YAMLWarning(node, `The tag ${tagName} is unavailable, falling back to ${fallback}`));
@@ -208,9 +214,8 @@ class Schema {
208
214
  return res;
209
215
  } else {
210
216
  doc.errors.push(new _errors.YAMLReferenceError(node, `The tag ${tagName} is unavailable`));
217
+ return null;
211
218
  }
212
-
213
- return null;
214
219
  }
215
220
 
216
221
  getTagObject(item) {
@@ -270,7 +275,7 @@ class Schema {
270
275
  const createCtx = {
271
276
  aliasNodes: [],
272
277
  onTagObj: o => tagObj = o,
273
- prevObjects: []
278
+ prevObjects: new Map()
274
279
  };
275
280
  item = this.createNode(item, true, null, createCtx);
276
281
  const {
@@ -292,6 +297,7 @@ class Schema {
292
297
  if (item instanceof _Pair.default) return item.toString(ctx, onComment, onChompKeep);
293
298
  if (!tagObj) tagObj = this.getTagObject(item);
294
299
  const props = this.stringifyProps(item, tagObj, ctx);
300
+ if (props.length > 0) ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1;
295
301
  const str = typeof tagObj.stringify === 'function' ? tagObj.stringify(item, ctx, onComment, onChompKeep) : item instanceof _Collection.default ? item.toString(ctx, onComment, onChompKeep) : (0, _stringify.stringifyString)(item, ctx, onComment, onChompKeep);
296
302
  return props ? item instanceof _Collection.default && str[0] !== '{' && str[0] !== '[' ? `${props}\n${ctx.indent}${str}` : `${props} ${str}` : str;
297
303
  }
@@ -21,7 +21,11 @@ var _parseUtils = require("./parseUtils");
21
21
 
22
22
  var _Alias = _interopRequireDefault(require("./Alias"));
23
23
 
24
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
24
+ var _Collection = _interopRequireDefault(require("./Collection"));
25
+
26
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
27
+
28
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
25
29
 
26
30
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
31
 
@@ -39,11 +43,13 @@ function parseMap(doc, cst) {
39
43
  const map = new _Map.default();
40
44
  map.items = items;
41
45
  (0, _parseUtils.resolveComments)(map, comments);
46
+ let hasCollectionKey = false;
42
47
 
43
48
  for (let i = 0; i < items.length; ++i) {
44
49
  const {
45
50
  key: iKey
46
51
  } = items[i];
52
+ if (iKey instanceof _Collection.default) hasCollectionKey = true;
47
53
 
48
54
  if (doc.schema.merge && iKey && iKey.value === _Merge.MERGE_KEY) {
49
55
  items[i] = new _Merge.default(items[i]);
@@ -69,7 +75,7 @@ function parseMap(doc, cst) {
69
75
  key: jKey
70
76
  } = items[j];
71
77
 
72
- if (iKey === jKey || iKey && jKey && iKey.hasOwnProperty('value') && iKey.value === jKey.value) {
78
+ if (iKey === jKey || iKey && jKey && Object.prototype.hasOwnProperty.call(iKey, 'value') && iKey.value === jKey.value) {
73
79
  const msg = `Map keys must be unique; "${iKey}" is repeated`;
74
80
  doc.errors.push(new _errors.YAMLSemanticError(cst, msg));
75
81
  break;
@@ -78,6 +84,11 @@ function parseMap(doc, cst) {
78
84
  }
79
85
  }
80
86
 
87
+ if (hasCollectionKey && !doc.options.mapAsMap) {
88
+ const warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
89
+ doc.warnings.push(new _errors.YAMLWarning(cst, warn));
90
+ }
91
+
81
92
  cst.resolved = map;
82
93
  return map;
83
94
  }
@@ -254,7 +265,8 @@ function resolveFlowMapItems(doc, cst) {
254
265
 
255
266
  if (typeof item.char === 'string') {
256
267
  const {
257
- char
268
+ char,
269
+ offset
258
270
  } = item;
259
271
 
260
272
  if (char === '?' && key === undefined && !explicitKey) {
@@ -295,7 +307,10 @@ function resolveFlowMapItems(doc, cst) {
295
307
  continue;
296
308
  }
297
309
 
298
- doc.errors.push(new _errors.YAMLSyntaxError(cst, `Flow map contains an unexpected ${char}`));
310
+ const msg = `Flow map contains an unexpected ${char}`;
311
+ const err = new _errors.YAMLSyntaxError(cst, msg);
312
+ err.offset = offset;
313
+ doc.errors.push(err);
299
314
  } else if (item.type === _constants.Type.BLANK_LINE) {
300
315
  comments.push({
301
316
  afterKey: !!key,
@@ -319,7 +334,7 @@ function resolveFlowMapItems(doc, cst) {
319
334
  }
320
335
  }
321
336
 
322
- if (cst.items[cst.items.length - 1].char !== '}') doc.errors.push(new _errors.YAMLSemanticError(cst, 'Expected flow map to end with }'));
337
+ (0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst);
323
338
  if (key !== undefined) items.push(new _Pair.default(key));
324
339
  return {
325
340
  comments,
@@ -15,6 +15,8 @@ var _parseUtils = require("./parseUtils");
15
15
 
16
16
  var _Seq = _interopRequireDefault(require("./Seq"));
17
17
 
18
+ var _Collection = _interopRequireDefault(require("./Collection"));
19
+
18
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
21
 
20
22
  function parseSeq(doc, cst) {
@@ -31,6 +33,12 @@ function parseSeq(doc, cst) {
31
33
  const seq = new _Seq.default();
32
34
  seq.items = items;
33
35
  (0, _parseUtils.resolveComments)(seq, comments);
36
+
37
+ if (!doc.options.mapAsMap && items.some(it => it instanceof _Pair.default && it.key instanceof _Collection.default)) {
38
+ const warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
39
+ doc.warnings.push(new _errors.YAMLWarning(cst, warn));
40
+ }
41
+
34
42
  cst.resolved = seq;
35
43
  return seq;
36
44
  }
@@ -92,7 +100,8 @@ function resolveFlowSeqItems(doc, cst) {
92
100
 
93
101
  if (typeof item.char === 'string') {
94
102
  const {
95
- char
103
+ char,
104
+ offset
96
105
  } = item;
97
106
 
98
107
  if (char !== ':' && (explicitKey || key !== undefined)) {
@@ -112,8 +121,10 @@ function resolveFlowSeqItems(doc, cst) {
112
121
  key = items.pop();
113
122
 
114
123
  if (key instanceof _Pair.default) {
115
- const msg = 'Chaining flow sequence pairs is invalid (e.g. [ a : b : c ])';
116
- doc.errors.push(new _errors.YAMLSemanticError(char, msg));
124
+ const msg = 'Chaining flow sequence pairs is invalid';
125
+ const err = new _errors.YAMLSemanticError(cst, msg);
126
+ err.offset = offset;
127
+ doc.errors.push(err);
117
128
  }
118
129
 
119
130
  if (!explicitKey) (0, _parseUtils.checkKeyLength)(doc.errors, cst, i, key, keyStart);
@@ -127,7 +138,9 @@ function resolveFlowSeqItems(doc, cst) {
127
138
  next = null;
128
139
  } else if (next === '[' || char !== ']' || i < cst.items.length - 1) {
129
140
  const msg = `Flow sequence contains an unexpected ${char}`;
130
- doc.errors.push(new _errors.YAMLSyntaxError(cst, msg));
141
+ const err = new _errors.YAMLSyntaxError(cst, msg);
142
+ err.offset = offset;
143
+ doc.errors.push(err);
131
144
  }
132
145
  } else if (item.type === _constants.Type.BLANK_LINE) {
133
146
  comments.push({
@@ -140,7 +153,7 @@ function resolveFlowSeqItems(doc, cst) {
140
153
  });
141
154
  } else {
142
155
  if (next) {
143
- const msg = `Expected a ${next} here in flow sequence`;
156
+ const msg = `Expected a ${next} in flow sequence`;
144
157
  doc.errors.push(new _errors.YAMLSemanticError(item, msg));
145
158
  }
146
159
 
@@ -158,7 +171,7 @@ function resolveFlowSeqItems(doc, cst) {
158
171
  }
159
172
  }
160
173
 
161
- if (cst.items[cst.items.length - 1].char !== ']') doc.errors.push(new _errors.YAMLSemanticError(cst, 'Expected flow sequence to end with ]'));
174
+ (0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst);
162
175
  if (key !== undefined) items.push(new _Pair.default(key));
163
176
  return {
164
177
  comments,
@@ -3,11 +3,60 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.checkFlowCollectionEnd = checkFlowCollectionEnd;
6
7
  exports.checkKeyLength = checkKeyLength;
7
8
  exports.resolveComments = resolveComments;
8
9
 
9
10
  var _errors = require("../errors");
10
11
 
12
+ var _constants = require("../constants");
13
+
14
+ function checkFlowCollectionEnd(errors, cst) {
15
+ let char, name;
16
+
17
+ switch (cst.type) {
18
+ case _constants.Type.FLOW_MAP:
19
+ char = '}';
20
+ name = 'flow map';
21
+ break;
22
+
23
+ case _constants.Type.FLOW_SEQ:
24
+ char = ']';
25
+ name = 'flow sequence';
26
+ break;
27
+
28
+ default:
29
+ errors.push(new _errors.YAMLSemanticError(cst, 'Not a flow collection!?'));
30
+ return;
31
+ }
32
+
33
+ let lastItem;
34
+
35
+ for (let i = cst.items.length - 1; i >= 0; --i) {
36
+ const item = cst.items[i];
37
+
38
+ if (!item || item.type !== _constants.Type.COMMENT) {
39
+ lastItem = item;
40
+ break;
41
+ }
42
+ }
43
+
44
+ if (lastItem && lastItem.char !== char) {
45
+ const msg = `Expected ${name} to end with ${char}`;
46
+ let err;
47
+
48
+ if (typeof lastItem.offset === 'number') {
49
+ err = new _errors.YAMLSemanticError(cst, msg);
50
+ err.offset = lastItem.offset + 1;
51
+ } else {
52
+ err = new _errors.YAMLSemanticError(lastItem, msg);
53
+ if (lastItem.range && lastItem.range.end) err.offset = lastItem.range.end - lastItem.range.start;
54
+ }
55
+
56
+ errors.push(err);
57
+ }
58
+ }
59
+
11
60
  function checkKeyLength(errors, node, itemIdx, key, keyStart) {
12
61
  if (!key || typeof keyStart !== 'number') return;
13
62
  const item = node.items[itemIdx];
@@ -31,12 +80,11 @@ function checkKeyLength(errors, node, itemIdx, key, keyStart) {
31
80
  }
32
81
 
33
82
  function resolveComments(collection, comments) {
34
- for (const _ref of comments) {
35
- const {
36
- afterKey,
37
- before,
38
- comment
39
- } = _ref;
83
+ for (const {
84
+ afterKey,
85
+ before,
86
+ comment
87
+ } of comments) {
40
88
  let item = collection.items[before];
41
89
 
42
90
  if (!item) {
package/dist/stringify.js CHANGED
@@ -14,7 +14,15 @@ var _foldFlowLines = _interopRequireWildcard(require("./foldFlowLines"));
14
14
 
15
15
  var _options = require("./tags/options");
16
16
 
17
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
17
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
18
+
19
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
20
+
21
+ const getFoldOptions = ({
22
+ indentAtStart
23
+ }) => indentAtStart ? Object.assign({
24
+ indentAtStart
25
+ }, _options.strOptions.fold) : _options.strOptions.fold;
18
26
 
19
27
  function stringifyNumber({
20
28
  format,
@@ -56,10 +64,11 @@ function lineLengthOverLimit(str, limit) {
56
64
  return true;
57
65
  }
58
66
 
59
- function doubleQuotedString(value, {
60
- implicitKey,
61
- indent
62
- }) {
67
+ function doubleQuotedString(value, ctx) {
68
+ const {
69
+ implicitKey,
70
+ indent
71
+ } = ctx;
63
72
  const {
64
73
  jsonEncoding,
65
74
  minMultiLineLength
@@ -153,7 +162,7 @@ function doubleQuotedString(value, {
153
162
  }
154
163
 
155
164
  str = start ? str + json.slice(start) : json;
156
- return implicitKey ? str : (0, _foldFlowLines.default)(str, indent, _foldFlowLines.FOLD_QUOTED, _options.strOptions.fold);
165
+ return implicitKey ? str : (0, _foldFlowLines.default)(str, indent, _foldFlowLines.FOLD_QUOTED, getFoldOptions(ctx));
157
166
  }
158
167
 
159
168
  function singleQuotedString(value, ctx) {
@@ -170,7 +179,7 @@ function singleQuotedString(value, ctx) {
170
179
  }
171
180
 
172
181
  const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
173
- return implicitKey ? res : (0, _foldFlowLines.default)(res, indent, _foldFlowLines.FOLD_FLOW, _options.strOptions.fold);
182
+ return implicitKey ? res : (0, _foldFlowLines.default)(res, indent, _foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
174
183
  }
175
184
 
176
185
  function blockString({
@@ -272,14 +281,15 @@ function plainString(item, ctx, onComment, onChompKeep) {
272
281
  return blockString(item, ctx, onComment, onChompKeep);
273
282
  }
274
283
 
275
- const str = value.replace(/\n+/g, `$&\n${indent}`); // May need to verify that output will be parsed as a string, as plain numbers
276
- // and booleans get parsed with those types, e.g. '42', 'true' & '0.9e-3'.
284
+ const str = value.replace(/\n+/g, `$&\n${indent}`); // Verify that output will be parsed as a string, as e.g. plain numbers and
285
+ // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
286
+ // and others in v1.1.
277
287
 
278
- if (actualString && /^[\w.+-]+$/.test(str) && typeof tags.resolveScalar(str).value !== 'string') {
288
+ if (actualString && typeof tags.resolveScalar(str).value !== 'string') {
279
289
  return doubleQuotedString(value, ctx);
280
290
  }
281
291
 
282
- const body = implicitKey ? str : (0, _foldFlowLines.default)(str, indent, _foldFlowLines.FOLD_FLOW, _options.strOptions.fold);
292
+ const body = implicitKey ? str : (0, _foldFlowLines.default)(str, indent, _foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
283
293
 
284
294
  if (comment && !inFlow && (body.indexOf('\n') !== -1 || comment.indexOf('\n') !== -1)) {
285
295
  if (onComment) onComment();
package/dist/tags/core.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = exports.floatObj = exports.expObj = exports.nanObj = exports.hexObj = exports.intObj = exports.octObj = exports.boolObj = exports.nullObj = void 0;
7
7
 
8
8
  var _Scalar = _interopRequireDefault(require("../schema/Scalar"));
9
9
 
@@ -15,7 +15,7 @@ var _options = require("./options");
15
15
 
16
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
17
 
18
- var _default = _failsafe.default.concat([{
18
+ const nullObj = {
19
19
  identify: value => value == null,
20
20
  createNode: (schema, value, ctx) => ctx.wrapScalars ? new _Scalar.default(null) : null,
21
21
  default: true,
@@ -24,7 +24,9 @@ var _default = _failsafe.default.concat([{
24
24
  resolve: () => null,
25
25
  options: _options.nullOptions,
26
26
  stringify: () => _options.nullOptions.nullStr
27
- }, {
27
+ };
28
+ exports.nullObj = nullObj;
29
+ const boolObj = {
28
30
  identify: value => typeof value === 'boolean',
29
31
  default: true,
30
32
  tag: 'tag:yaml.org,2002:bool',
@@ -34,7 +36,9 @@ var _default = _failsafe.default.concat([{
34
36
  stringify: ({
35
37
  value
36
38
  }) => value ? _options.boolOptions.trueStr : _options.boolOptions.falseStr
37
- }, {
39
+ };
40
+ exports.boolObj = boolObj;
41
+ const octObj = {
38
42
  identify: value => typeof value === 'number',
39
43
  default: true,
40
44
  tag: 'tag:yaml.org,2002:int',
@@ -44,14 +48,18 @@ var _default = _failsafe.default.concat([{
44
48
  stringify: ({
45
49
  value
46
50
  }) => '0o' + value.toString(8)
47
- }, {
51
+ };
52
+ exports.octObj = octObj;
53
+ const intObj = {
48
54
  identify: value => typeof value === 'number',
49
55
  default: true,
50
56
  tag: 'tag:yaml.org,2002:int',
51
57
  test: /^[-+]?[0-9]+$/,
52
58
  resolve: str => parseInt(str, 10),
53
59
  stringify: _stringify.stringifyNumber
54
- }, {
60
+ };
61
+ exports.intObj = intObj;
62
+ const hexObj = {
55
63
  identify: value => typeof value === 'number',
56
64
  default: true,
57
65
  tag: 'tag:yaml.org,2002:int',
@@ -61,14 +69,18 @@ var _default = _failsafe.default.concat([{
61
69
  stringify: ({
62
70
  value
63
71
  }) => '0x' + value.toString(16)
64
- }, {
72
+ };
73
+ exports.hexObj = hexObj;
74
+ const nanObj = {
65
75
  identify: value => typeof value === 'number',
66
76
  default: true,
67
77
  tag: 'tag:yaml.org,2002:float',
68
78
  test: /^(?:[-+]?\.inf|(\.nan))$/i,
69
79
  resolve: (str, nan) => nan ? NaN : str[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
70
80
  stringify: _stringify.stringifyNumber
71
- }, {
81
+ };
82
+ exports.nanObj = nanObj;
83
+ const expObj = {
72
84
  identify: value => typeof value === 'number',
73
85
  default: true,
74
86
  tag: 'tag:yaml.org,2002:float',
@@ -78,7 +90,9 @@ var _default = _failsafe.default.concat([{
78
90
  stringify: ({
79
91
  value
80
92
  }) => Number(value).toExponential()
81
- }, {
93
+ };
94
+ exports.expObj = expObj;
95
+ const floatObj = {
82
96
  identify: value => typeof value === 'number',
83
97
  default: true,
84
98
  tag: 'tag:yaml.org,2002:float',
@@ -91,6 +105,9 @@ var _default = _failsafe.default.concat([{
91
105
  },
92
106
 
93
107
  stringify: _stringify.stringifyNumber
94
- }]);
108
+ };
109
+ exports.floatObj = floatObj;
110
+
111
+ var _default = _failsafe.default.concat([nullObj, boolObj, octObj, intObj, hexObj, nanObj, expObj, floatObj]);
95
112
 
96
113
  exports.default = _default;
@@ -12,7 +12,7 @@ var _parseMap = _interopRequireDefault(require("../../schema/parseMap"));
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
13
 
14
14
  function createMap(schema, obj, ctx) {
15
- const map = new _Map.default();
15
+ const map = new _Map.default(schema);
16
16
 
17
17
  if (obj instanceof Map) {
18
18
  for (const [key, value] of obj) map.items.push(schema.createPair(key, value, ctx));
@@ -20,6 +20,10 @@ function createMap(schema, obj, ctx) {
20
20
  for (const key of Object.keys(obj)) map.items.push(schema.createPair(key, obj[key], ctx));
21
21
  }
22
22
 
23
+ if (typeof schema.sortMapEntries === 'function') {
24
+ map.items.sort(schema.sortMapEntries);
25
+ }
26
+
23
27
  return map;
24
28
  }
25
29
 
@@ -12,7 +12,7 @@ var _Seq = _interopRequireDefault(require("../../schema/Seq"));
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
13
 
14
14
  function createSeq(schema, obj, ctx) {
15
- const seq = new _Seq.default();
15
+ const seq = new _Seq.default(schema);
16
16
 
17
17
  if (obj && obj[Symbol.iterator]) {
18
18
  for (const it of obj) {
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.tags = exports.schemas = void 0;
7
7
 
8
- var _core = _interopRequireDefault(require("./core"));
8
+ var _core = _interopRequireWildcard(require("./core"));
9
9
 
10
10
  var _failsafe = _interopRequireDefault(require("./failsafe"));
11
11
 
@@ -29,6 +29,10 @@ var _timestamp = require("./yaml-1.1/timestamp");
29
29
 
30
30
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
31
 
32
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
33
+
34
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
35
+
32
36
  const schemas = {
33
37
  core: _core.default,
34
38
  failsafe: _failsafe.default,
@@ -38,9 +42,17 @@ const schemas = {
38
42
  exports.schemas = schemas;
39
43
  const tags = {
40
44
  binary: _binary.default,
45
+ bool: _core.boolObj,
46
+ float: _core.floatObj,
47
+ floatExp: _core.expObj,
48
+ floatNaN: _core.nanObj,
41
49
  floatTime: _timestamp.floatTime,
50
+ int: _core.intObj,
51
+ intHex: _core.hexObj,
52
+ intOct: _core.octObj,
42
53
  intTime: _timestamp.intTime,
43
54
  map: _map.default,
55
+ null: _core.nullObj,
44
56
  omap: _omap.default,
45
57
  pairs: _pairs.default,
46
58
  seq: _seq.default,
package/dist/tags/json.js CHANGED
@@ -33,15 +33,8 @@ const schema = [_map.default, _seq.default, {
33
33
  identify: value => typeof value === 'boolean',
34
34
  default: true,
35
35
  tag: 'tag:yaml.org,2002:bool',
36
- test: /^true$/,
37
- resolve: () => true,
38
- stringify: value => JSON.stringify(value)
39
- }, {
40
- identify: value => typeof value === 'boolean',
41
- default: true,
42
- tag: 'tag:yaml.org,2002:bool',
43
- test: /^false$/,
44
- resolve: () => false,
36
+ test: /^true|false$/,
37
+ resolve: str => str === 'true',
45
38
  stringify: value => JSON.stringify(value)
46
39
  }, {
47
40
  identify: value => typeof value === 'number',