yaml 1.5.1 → 1.7.2

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 (62) hide show
  1. package/browser/dist/Document.js +25 -5
  2. package/browser/dist/cst/CollectionItem.js +27 -6
  3. package/browser/dist/cst/Document.js +9 -1
  4. package/browser/dist/cst/FlowCollection.js +9 -1
  5. package/browser/dist/cst/Node.js +7 -4
  6. package/browser/dist/cst/PlainValue.js +1 -1
  7. package/browser/dist/cst/source-utils.js +179 -0
  8. package/browser/dist/errors.js +36 -3
  9. package/browser/dist/index.js +5 -4
  10. package/browser/dist/schema/Alias.js +1 -1
  11. package/browser/dist/schema/Merge.js +1 -1
  12. package/browser/dist/schema/Pair.js +17 -2
  13. package/browser/dist/schema/index.js +4 -3
  14. package/browser/dist/schema/parseMap.js +17 -4
  15. package/browser/dist/schema/parseSeq.js +22 -6
  16. package/browser/dist/schema/parseUtils.js +49 -0
  17. package/browser/dist/stringify.js +4 -3
  18. package/browser/dist/tags/failsafe/map.js +2 -2
  19. package/browser/dist/{deprecation.js → warnings.js} +13 -7
  20. package/browser/map.js +1 -1
  21. package/browser/pair.js +1 -1
  22. package/browser/scalar.js +1 -1
  23. package/browser/schema.js +1 -1
  24. package/browser/seq.js +1 -1
  25. package/browser/types/binary.js +1 -1
  26. package/browser/types/omap.js +1 -1
  27. package/browser/types/pairs.js +1 -1
  28. package/browser/types/set.js +1 -1
  29. package/browser/types/timestamp.js +1 -1
  30. package/dist/Document.js +32 -12
  31. package/dist/cst/CollectionItem.js +27 -6
  32. package/dist/cst/Document.js +9 -1
  33. package/dist/cst/FlowCollection.js +9 -1
  34. package/dist/cst/Node.js +7 -4
  35. package/dist/cst/PlainValue.js +1 -1
  36. package/dist/cst/source-utils.js +178 -0
  37. package/dist/errors.js +37 -3
  38. package/dist/index.js +5 -4
  39. package/dist/schema/Alias.js +1 -1
  40. package/dist/schema/Merge.js +4 -5
  41. package/dist/schema/Pair.js +19 -2
  42. package/dist/schema/index.js +4 -3
  43. package/dist/schema/parseMap.js +20 -5
  44. package/dist/schema/parseSeq.js +19 -6
  45. package/dist/schema/parseUtils.js +54 -6
  46. package/dist/stringify.js +7 -4
  47. package/dist/tags/yaml-1.1/omap.js +3 -5
  48. package/dist/tags/yaml-1.1/set.js +3 -1
  49. package/dist/{deprecation.js → warnings.js} +12 -7
  50. package/map.js +1 -1
  51. package/package.json +18 -16
  52. package/pair.js +1 -1
  53. package/scalar.js +1 -1
  54. package/schema.js +1 -1
  55. package/seq.js +1 -1
  56. package/types/binary.js +1 -1
  57. package/types/omap.js +1 -1
  58. package/types/pairs.js +1 -1
  59. package/types/set.js +1 -1
  60. package/types/timestamp.js +1 -1
  61. package/browser/dist/cst/getLinePos.js +0 -79
  62. package/dist/cst/getLinePos.js +0 -79
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getLinePos = getLinePos;
7
+ exports.getLine = getLine;
8
+ exports.getPrettyContext = getPrettyContext;
9
+
10
+ function findLineStarts(src) {
11
+ const ls = [0];
12
+ let offset = src.indexOf('\n');
13
+
14
+ while (offset !== -1) {
15
+ offset += 1;
16
+ ls.push(offset);
17
+ offset = src.indexOf('\n', offset);
18
+ }
19
+
20
+ return ls;
21
+ }
22
+
23
+ function getSrcInfo(cst) {
24
+ let lineStarts, src;
25
+
26
+ if (typeof cst === 'string') {
27
+ lineStarts = findLineStarts(cst);
28
+ src = cst;
29
+ } else {
30
+ if (Array.isArray(cst)) cst = cst[0];
31
+
32
+ if (cst && cst.context) {
33
+ if (!cst.lineStarts) cst.lineStarts = findLineStarts(cst.context.src);
34
+ lineStarts = cst.lineStarts;
35
+ src = cst.context.src;
36
+ }
37
+ }
38
+
39
+ return {
40
+ lineStarts,
41
+ src
42
+ };
43
+ }
44
+ /**
45
+ * @typedef {Object} LinePos - One-indexed position in the source
46
+ * @property {number} line
47
+ * @property {number} col
48
+ */
49
+
50
+ /**
51
+ * Determine the line/col position matching a character offset.
52
+ *
53
+ * Accepts a source string or a CST document as the second parameter. With
54
+ * the latter, starting indices for lines are cached in the document as
55
+ * `lineStarts: number[]`.
56
+ *
57
+ * Returns a one-indexed `{ line, col }` location if found, or
58
+ * `undefined` otherwise.
59
+ *
60
+ * @param {number} offset
61
+ * @param {string|Document|Document[]} cst
62
+ * @returns {?LinePos}
63
+ */
64
+
65
+
66
+ function getLinePos(offset, cst) {
67
+ if (typeof offset !== 'number' || offset < 0) return null;
68
+ const {
69
+ lineStarts,
70
+ src
71
+ } = getSrcInfo(cst);
72
+ if (!lineStarts || !src || offset > src.length) return null;
73
+
74
+ for (let i = 0; i < lineStarts.length; ++i) {
75
+ const start = lineStarts[i];
76
+
77
+ if (offset < start) {
78
+ return {
79
+ line: i,
80
+ col: offset - lineStarts[i - 1] + 1
81
+ };
82
+ }
83
+
84
+ if (offset === start) return {
85
+ line: i + 1,
86
+ col: 1
87
+ };
88
+ }
89
+
90
+ const line = lineStarts.length;
91
+ return {
92
+ line,
93
+ col: offset - lineStarts[line - 1] + 1
94
+ };
95
+ }
96
+ /**
97
+ * Get a specified line from the source.
98
+ *
99
+ * Accepts a source string or a CST document as the second parameter. With
100
+ * the latter, starting indices for lines are cached in the document as
101
+ * `lineStarts: number[]`.
102
+ *
103
+ * Returns the line as a string if found, or `null` otherwise.
104
+ *
105
+ * @param {number} line One-indexed line number
106
+ * @param {string|Document|Document[]} cst
107
+ * @returns {?string}
108
+ */
109
+
110
+
111
+ function getLine(line, cst) {
112
+ const {
113
+ lineStarts,
114
+ src
115
+ } = getSrcInfo(cst);
116
+ if (!lineStarts || !(line >= 1) || line > lineStarts.length) return null;
117
+ const start = lineStarts[line - 1];
118
+ let end = lineStarts[line]; // undefined for last line; that's ok for slice()
119
+
120
+ while (end && end > start && src[end - 1] === '\n') --end;
121
+
122
+ return src.slice(start, end);
123
+ }
124
+ /**
125
+ * Pretty-print the starting line from the source indicated by the range `pos`
126
+ *
127
+ * Trims output to `maxWidth` chars while keeping the starting column visible,
128
+ * using `…` at either end to indicate dropped characters.
129
+ *
130
+ * Returns a two-line string (or `null`) with `\n` as separator; the second line
131
+ * will hold appropriately indented `^` marks indicating the column range.
132
+ *
133
+ * @param {Object} pos
134
+ * @param {LinePos} pos.start
135
+ * @param {LinePos} [pos.end]
136
+ * @param {string|Document|Document[]*} cst
137
+ * @param {number} [maxWidth=80]
138
+ * @returns {?string}
139
+ */
140
+
141
+
142
+ function getPrettyContext({
143
+ start,
144
+ end
145
+ }, cst, maxWidth = 80) {
146
+ let src = getLine(start.line, cst);
147
+ if (!src) return null;
148
+ let {
149
+ col
150
+ } = start;
151
+
152
+ if (src.length > maxWidth) {
153
+ if (col <= maxWidth - 10) {
154
+ src = src.substr(0, maxWidth - 1) + '…';
155
+ } else {
156
+ const halfWidth = Math.round(maxWidth / 2);
157
+ if (src.length > col + halfWidth) src = src.substr(0, col + halfWidth - 1) + '…';
158
+ col -= src.length - maxWidth;
159
+ src = '…' + src.substr(1 - maxWidth);
160
+ }
161
+ }
162
+
163
+ let errLen = 1;
164
+ let errEnd = '';
165
+
166
+ if (end) {
167
+ if (end.line === start.line && col + (end.col - start.col) <= maxWidth + 1) {
168
+ errLen = end.col - start.col;
169
+ } else {
170
+ errLen = Math.min(src.length + 1, maxWidth) - col;
171
+ errEnd = '…';
172
+ }
173
+ }
174
+
175
+ const offset = col > 1 ? ' '.repeat(col - 1) : '';
176
+ const err = '^'.repeat(errLen);
177
+ return `${src}\n${offset}${err}${errEnd}`;
178
+ }
package/dist/errors.js CHANGED
@@ -7,6 +7,10 @@ exports.YAMLWarning = exports.YAMLSyntaxError = exports.YAMLSemanticError = expo
7
7
 
8
8
  var _Node = _interopRequireDefault(require("./cst/Node"));
9
9
 
10
+ var _sourceUtils = require("./cst/source-utils");
11
+
12
+ var _Range = _interopRequireDefault(require("./cst/Range"));
13
+
10
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
15
 
12
16
  class YAMLError extends Error {
@@ -19,12 +23,42 @@ class YAMLError extends Error {
19
23
  }
20
24
 
21
25
  makePretty() {
22
- if (this.source) {
23
- this.nodeType = this.source.type;
26
+ if (!this.source) return;
27
+ this.nodeType = this.source.type;
28
+ const cst = this.source.context && this.source.context.root;
29
+
30
+ if (typeof this.offset === 'number') {
31
+ this.range = new _Range.default(this.offset, this.offset + 1);
32
+ const start = cst && (0, _sourceUtils.getLinePos)(this.offset, cst);
33
+
34
+ if (start) {
35
+ const end = {
36
+ line: start.line,
37
+ col: start.col + 1
38
+ };
39
+ this.linePos = {
40
+ start,
41
+ end
42
+ };
43
+ }
44
+
45
+ delete this.offset;
46
+ } else {
24
47
  this.range = this.source.range;
25
48
  this.linePos = this.source.rangeAsLinePos;
26
- delete this.source;
27
49
  }
50
+
51
+ if (this.linePos) {
52
+ const {
53
+ line,
54
+ col
55
+ } = this.linePos.start;
56
+ this.message += ` at line ${line}, column ${col}`;
57
+ const ctx = cst && (0, _sourceUtils.getPrettyContext)(this.linePos, cst);
58
+ if (ctx) this.message += `:\n\n${ctx}\n`;
59
+ }
60
+
61
+ delete this.source;
28
62
  }
29
63
 
30
64
  }
package/dist/index.js CHANGED
@@ -13,9 +13,10 @@ var _errors = require("./errors");
13
13
 
14
14
  var _schema = _interopRequireDefault(require("./schema"));
15
15
 
16
+ var _warnings = require("./warnings");
17
+
16
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
19
 
18
- /* global console */
19
20
  const defaultOptions = {
20
21
  anchorPrefix: 'a',
21
22
  customTags: null,
@@ -26,6 +27,7 @@ const defaultOptions = {
26
27
  maxAliasCount: 100,
27
28
  prettyErrors: false,
28
29
  // TODO Set true in v2
30
+ simpleKeys: false,
29
31
  version: '1.2'
30
32
  };
31
33
 
@@ -74,9 +76,8 @@ function parseDocument(src, options) {
74
76
  }
75
77
 
76
78
  function parse(src, options) {
77
- const doc = parseDocument(src, options); // eslint-disable-next-line no-console
78
-
79
- doc.warnings.forEach(warning => console.warn(warning));
79
+ const doc = parseDocument(src, options);
80
+ doc.warnings.forEach(warning => (0, _warnings.warn)(warning));
80
81
  if (doc.errors.length > 0) throw doc.errors[0];
81
82
  return doc.toJSON();
82
83
  }
@@ -78,7 +78,7 @@ class Alias extends _Node.default {
78
78
  } = ctx;
79
79
  const anchor = anchors.find(a => a.node === this.source);
80
80
 
81
- if (!anchor || !anchor.res) {
81
+ if (!anchor || anchor.res === undefined) {
82
82
  const msg = 'This should not happen: Alias anchor was not resolved?';
83
83
  if (this.cstNode) throw new _errors.YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg);
84
84
  }
@@ -46,10 +46,9 @@ class Merge extends _Pair.default {
46
46
 
47
47
 
48
48
  addToJSMap(ctx, map) {
49
- for (const _ref of this.value.items) {
50
- const {
51
- source
52
- } = _ref;
49
+ for (const {
50
+ source
51
+ } of this.value.items) {
53
52
  if (!(source instanceof _Map.default)) throw new Error('Merge sources must be maps');
54
53
  const srcMap = source.toJSON(null, ctx, Map);
55
54
 
@@ -59,7 +58,7 @@ class Merge extends _Pair.default {
59
58
  } else if (map instanceof Set) {
60
59
  map.add(key);
61
60
  } else {
62
- if (!map.hasOwnProperty(key)) map[key] = value;
61
+ if (!Object.prototype.hasOwnProperty.call(map, key)) map[key] = value;
63
62
  }
64
63
  }
65
64
  }
@@ -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();
@@ -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
 
@@ -57,7 +57,7 @@ class Schema {
57
57
 
58
58
  if (!customTags && deprecatedCustomTags) {
59
59
  customTags = deprecatedCustomTags;
60
- (0, _deprecation.warnOptionDeprecation)('tags', 'customTags');
60
+ (0, _warnings.warnOptionDeprecation)('tags', 'customTags');
61
61
  }
62
62
 
63
63
  if (Array.isArray(customTags)) {
@@ -83,6 +83,7 @@ class Schema {
83
83
  }
84
84
 
85
85
  createNode(value, wrapScalars, tag, ctx) {
86
+ if (value instanceof _Node.default) return value;
86
87
  let tagObj;
87
88
 
88
89
  if (tag) {
@@ -198,7 +199,7 @@ class Schema {
198
199
 
199
200
  resolveNodeWithFallback(doc, node, tagName) {
200
201
  const res = this.resolveNode(doc, node, tagName);
201
- if (node.hasOwnProperty('resolved')) return res;
202
+ if (Object.prototype.hasOwnProperty.call(node, 'resolved')) return res;
202
203
  const fallback = isMap(node) ? Schema.defaultTags.MAP : isSeq(node) ? Schema.defaultTags.SEQ : Schema.defaultTags.STR;
203
204
 
204
205
  if (fallback) {
@@ -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; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { 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,9 @@ 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; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { 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; }
18
20
 
19
21
  function stringifyNumber({
20
22
  format,
@@ -272,10 +274,11 @@ function plainString(item, ctx, onComment, onChompKeep) {
272
274
  return blockString(item, ctx, onComment, onChompKeep);
273
275
  }
274
276
 
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'.
277
+ const str = value.replace(/\n+/g, `$&\n${indent}`); // Verify that output will be parsed as a string, as e.g. plain numbers and
278
+ // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
279
+ // and others in v1.1.
277
280
 
278
- if (actualString && /^[\w.+-]+$/.test(str) && typeof tags.resolveScalar(str).value !== 'string') {
281
+ if (actualString && typeof tags.resolveScalar(str).value !== 'string') {
279
282
  return doubleQuotedString(value, ctx);
280
283
  }
281
284
 
@@ -71,11 +71,9 @@ function parseOMap(doc, cst) {
71
71
  const pairs = (0, _pairs.parsePairs)(doc, cst);
72
72
  const seenKeys = [];
73
73
 
74
- for (const _ref of pairs.items) {
75
- const {
76
- key
77
- } = _ref;
78
-
74
+ for (const {
75
+ key
76
+ } of pairs.items) {
79
77
  if (key instanceof _Scalar.default) {
80
78
  if (seenKeys.includes(key.value)) {
81
79
  const msg = 'Ordered maps must not include duplicate keys';
@@ -17,7 +17,9 @@ var _Scalar = _interopRequireDefault(require("../../schema/Scalar"));
17
17
 
18
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
19
 
20
- 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; } }
20
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
21
+
22
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { 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; }
21
23
 
22
24
  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; }
23
25