yaml 2.8.2 → 2.8.3

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.
@@ -28,19 +28,26 @@ function composeNode(ctx, token, props, onError) {
28
28
  case 'block-map':
29
29
  case 'block-seq':
30
30
  case 'flow-collection':
31
- node = composeCollection(CN, ctx, token, props, onError);
32
- if (anchor)
33
- node.anchor = anchor.source.substring(1);
31
+ try {
32
+ node = composeCollection(CN, ctx, token, props, onError);
33
+ if (anchor)
34
+ node.anchor = anchor.source.substring(1);
35
+ }
36
+ catch (error) {
37
+ // Almost certainly here due to a stack overflow
38
+ const message = error instanceof Error ? error.message : String(error);
39
+ onError(token, 'RESOURCE_EXHAUSTION', message);
40
+ }
34
41
  break;
35
42
  default: {
36
43
  const message = token.type === 'error'
37
44
  ? token.message
38
45
  : `Unsupported token (type: ${token.type})`;
39
46
  onError(token, 'UNEXPECTED_TOKEN', message);
40
- node = composeEmptyNode(ctx, token.offset, undefined, null, props, onError);
41
47
  isSrcToken = false;
42
48
  }
43
49
  }
50
+ node ?? (node = composeEmptyNode(ctx, token.offset, undefined, null, props, onError));
44
51
  if (anchor && node.anchor === '')
45
52
  onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
46
53
  if (atKey &&
@@ -20,6 +20,7 @@ function createStringifyContext(doc, options) {
20
20
  nullStr: 'null',
21
21
  simpleKeys: false,
22
22
  singleQuote: null,
23
+ trailingComma: false,
23
24
  trueStr: 'true',
24
25
  verifyAliasOrder: true
25
26
  }, doc.schema.toStringOptions, options);
@@ -102,12 +102,22 @@ function stringifyFlowCollection({ items }, ctx, { flowChars, itemIndent }) {
102
102
  if (comment)
103
103
  reqNewline = true;
104
104
  let str = stringify(item, itemCtx, () => (comment = null));
105
- if (i < items.length - 1)
105
+ reqNewline || (reqNewline = lines.length > linesAtValue || str.includes('\n'));
106
+ if (i < items.length - 1) {
106
107
  str += ',';
108
+ }
109
+ else if (ctx.options.trailingComma) {
110
+ if (ctx.options.lineWidth > 0) {
111
+ reqNewline || (reqNewline = lines.reduce((sum, line) => sum + line.length + 2, 2) +
112
+ (str.length + 2) >
113
+ ctx.options.lineWidth);
114
+ }
115
+ if (reqNewline) {
116
+ str += ',';
117
+ }
118
+ }
107
119
  if (comment)
108
120
  str += lineComment(str, itemIndent, commentString(comment));
109
- if (!reqNewline && (lines.length > linesAtValue || str.includes('\n')))
110
- reqNewline = true;
111
121
  lines.push(str);
112
122
  linesAtValue = lines.length;
113
123
  }
@@ -30,19 +30,26 @@ function composeNode(ctx, token, props, onError) {
30
30
  case 'block-map':
31
31
  case 'block-seq':
32
32
  case 'flow-collection':
33
- node = composeCollection.composeCollection(CN, ctx, token, props, onError);
34
- if (anchor)
35
- node.anchor = anchor.source.substring(1);
33
+ try {
34
+ node = composeCollection.composeCollection(CN, ctx, token, props, onError);
35
+ if (anchor)
36
+ node.anchor = anchor.source.substring(1);
37
+ }
38
+ catch (error) {
39
+ // Almost certainly here due to a stack overflow
40
+ const message = error instanceof Error ? error.message : String(error);
41
+ onError(token, 'RESOURCE_EXHAUSTION', message);
42
+ }
36
43
  break;
37
44
  default: {
38
45
  const message = token.type === 'error'
39
46
  ? token.message
40
47
  : `Unsupported token (type: ${token.type})`;
41
48
  onError(token, 'UNEXPECTED_TOKEN', message);
42
- node = composeEmptyNode(ctx, token.offset, undefined, null, props, onError);
43
49
  isSrcToken = false;
44
50
  }
45
51
  }
52
+ node ?? (node = composeEmptyNode(ctx, token.offset, undefined, null, props, onError));
46
53
  if (anchor && node.anchor === '')
47
54
  onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
48
55
  if (atKey &&
package/dist/errors.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { LineCounter } from './parse/line-counter';
2
- export type ErrorCode = 'ALIAS_PROPS' | 'BAD_ALIAS' | 'BAD_DIRECTIVE' | 'BAD_DQ_ESCAPE' | 'BAD_INDENT' | 'BAD_PROP_ORDER' | 'BAD_SCALAR_START' | 'BLOCK_AS_IMPLICIT_KEY' | 'BLOCK_IN_FLOW' | 'DUPLICATE_KEY' | 'IMPOSSIBLE' | 'KEY_OVER_1024_CHARS' | 'MISSING_CHAR' | 'MULTILINE_IMPLICIT_KEY' | 'MULTIPLE_ANCHORS' | 'MULTIPLE_DOCS' | 'MULTIPLE_TAGS' | 'NON_STRING_KEY' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN' | 'BAD_COLLECTION_TYPE';
2
+ export type ErrorCode = 'ALIAS_PROPS' | 'BAD_ALIAS' | 'BAD_DIRECTIVE' | 'BAD_DQ_ESCAPE' | 'BAD_INDENT' | 'BAD_PROP_ORDER' | 'BAD_SCALAR_START' | 'BLOCK_AS_IMPLICIT_KEY' | 'BLOCK_IN_FLOW' | 'DUPLICATE_KEY' | 'IMPOSSIBLE' | 'KEY_OVER_1024_CHARS' | 'MISSING_CHAR' | 'MULTILINE_IMPLICIT_KEY' | 'MULTIPLE_ANCHORS' | 'MULTIPLE_DOCS' | 'MULTIPLE_TAGS' | 'NON_STRING_KEY' | 'RESOURCE_EXHAUSTION' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN' | 'BAD_COLLECTION_TYPE';
3
3
  export type LinePos = {
4
4
  line: number;
5
5
  col: number;
package/dist/options.d.ts CHANGED
@@ -326,6 +326,12 @@ export type ToStringOptions = {
326
326
  * Default: `null`
327
327
  */
328
328
  singleQuote?: boolean | null;
329
+ /**
330
+ * Add a trailing comma after the last entry in a flow map or flow sequence that's split across multiple lines.
331
+ *
332
+ * Default: `'false'`
333
+ */
334
+ trailingComma?: boolean;
329
335
  /**
330
336
  * String representation for `true`.
331
337
  * With the core schema, use `'true'`, `'True'`, or `'TRUE'`.
@@ -22,6 +22,7 @@ function createStringifyContext(doc, options) {
22
22
  nullStr: 'null',
23
23
  simpleKeys: false,
24
24
  singleQuote: null,
25
+ trailingComma: false,
25
26
  trueStr: 'true',
26
27
  verifyAliasOrder: true
27
28
  }, doc.schema.toStringOptions, options);
@@ -104,12 +104,22 @@ function stringifyFlowCollection({ items }, ctx, { flowChars, itemIndent }) {
104
104
  if (comment)
105
105
  reqNewline = true;
106
106
  let str = stringify.stringify(item, itemCtx, () => (comment = null));
107
- if (i < items.length - 1)
107
+ reqNewline || (reqNewline = lines.length > linesAtValue || str.includes('\n'));
108
+ if (i < items.length - 1) {
108
109
  str += ',';
110
+ }
111
+ else if (ctx.options.trailingComma) {
112
+ if (ctx.options.lineWidth > 0) {
113
+ reqNewline || (reqNewline = lines.reduce((sum, line) => sum + line.length + 2, 2) +
114
+ (str.length + 2) >
115
+ ctx.options.lineWidth);
116
+ }
117
+ if (reqNewline) {
118
+ str += ',';
119
+ }
120
+ }
109
121
  if (comment)
110
122
  str += stringifyComment.lineComment(str, itemIndent, commentString(comment));
111
- if (!reqNewline && (lines.length > linesAtValue || str.includes('\n')))
112
- reqNewline = true;
113
123
  lines.push(str);
114
124
  linesAtValue = lines.length;
115
125
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml",
3
- "version": "2.8.2",
3
+ "version": "2.8.3",
4
4
  "license": "ISC",
5
5
  "author": "Eemeli Aro <eemeli@gmail.com>",
6
6
  "funding": "https://github.com/sponsors/eemeli",