yaml 2.0.0-4 → 2.0.0-5

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 (210) 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 +20 -17
  5. package/browser/dist/compose/compose-scalar.js +20 -23
  6. package/browser/dist/compose/composer.js +105 -76
  7. package/browser/dist/compose/resolve-block-map.js +38 -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 +6 -7
  11. package/browser/dist/compose/resolve-flow-collection.js +153 -199
  12. package/browser/dist/compose/resolve-flow-scalar.js +49 -25
  13. package/browser/dist/compose/resolve-props.js +47 -35
  14. package/browser/dist/compose/util-contains-newline.js +15 -12
  15. package/browser/dist/doc/Document.js +50 -99
  16. package/browser/dist/doc/anchors.js +72 -0
  17. package/browser/dist/doc/createNode.js +27 -17
  18. package/browser/dist/errors.js +16 -12
  19. package/browser/dist/index.js +3 -3
  20. package/browser/dist/nodes/Alias.js +51 -28
  21. package/browser/dist/nodes/Collection.js +5 -83
  22. package/browser/dist/nodes/Node.js +2 -1
  23. package/browser/dist/nodes/Pair.js +9 -232
  24. package/browser/dist/nodes/YAMLMap.js +8 -4
  25. package/browser/dist/nodes/YAMLSeq.js +7 -4
  26. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  27. package/browser/dist/nodes/toJS.js +12 -10
  28. package/browser/dist/options.js +0 -2
  29. package/browser/dist/parse/cst-scalar.js +219 -0
  30. package/browser/dist/parse/cst-stringify.js +61 -0
  31. package/browser/dist/parse/cst-visit.js +97 -0
  32. package/{dist/parse/tokens.js → browser/dist/parse/cst.js} +12 -7
  33. package/browser/dist/parse/lexer.js +99 -105
  34. package/browser/dist/parse/parser.js +219 -144
  35. package/browser/dist/public-api.js +11 -14
  36. package/browser/dist/schema/Schema.js +23 -0
  37. package/browser/dist/{tags/failsafe → schema/common}/map.js +0 -0
  38. package/browser/dist/schema/common/null.js +13 -0
  39. package/browser/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  40. package/browser/dist/{tags/failsafe → schema/common}/string.js +0 -0
  41. package/browser/dist/schema/core/bool.js +19 -0
  42. package/browser/dist/schema/core/float.js +40 -0
  43. package/browser/dist/schema/core/int.js +38 -0
  44. package/browser/dist/schema/core/schema.js +23 -0
  45. package/browser/dist/{tags/json.js → schema/json/schema.js} +5 -6
  46. package/browser/dist/schema/tags.js +79 -0
  47. package/browser/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  48. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  49. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  50. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  51. package/browser/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  52. package/browser/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  53. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  54. package/browser/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  55. package/browser/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  56. package/browser/dist/stringify/addComment.js +1 -7
  57. package/browser/dist/stringify/stringify.js +8 -6
  58. package/browser/dist/stringify/stringifyCollection.js +108 -0
  59. package/browser/dist/stringify/stringifyDocument.js +61 -0
  60. package/browser/dist/stringify/stringifyPair.js +113 -0
  61. package/browser/dist/stringify/stringifyString.js +53 -52
  62. package/browser/dist/visit.js +20 -1
  63. package/dist/compose/compose-collection.d.ts +4 -4
  64. package/dist/compose/compose-collection.js +12 -9
  65. package/dist/compose/compose-doc.d.ts +4 -3
  66. package/dist/compose/compose-doc.js +23 -6
  67. package/dist/compose/compose-node.d.ts +15 -7
  68. package/dist/compose/compose-node.js +20 -17
  69. package/dist/compose/compose-scalar.d.ts +4 -3
  70. package/dist/compose/compose-scalar.js +19 -22
  71. package/dist/compose/composer.d.ts +28 -23
  72. package/dist/compose/composer.js +106 -77
  73. package/dist/compose/resolve-block-map.d.ts +4 -4
  74. package/dist/compose/resolve-block-map.js +38 -29
  75. package/dist/compose/resolve-block-scalar.d.ts +5 -3
  76. package/dist/compose/resolve-block-scalar.js +14 -17
  77. package/dist/compose/resolve-block-seq.d.ts +4 -4
  78. package/dist/compose/resolve-block-seq.js +21 -17
  79. package/dist/compose/resolve-end.d.ts +3 -2
  80. package/dist/compose/resolve-end.js +6 -7
  81. package/dist/compose/resolve-flow-collection.d.ts +4 -5
  82. package/dist/compose/resolve-flow-collection.js +152 -198
  83. package/dist/compose/resolve-flow-scalar.d.ts +5 -3
  84. package/dist/compose/resolve-flow-scalar.js +49 -25
  85. package/dist/compose/resolve-props.d.ts +17 -10
  86. package/dist/compose/resolve-props.js +47 -35
  87. package/dist/compose/util-contains-newline.d.ts +1 -1
  88. package/dist/compose/util-contains-newline.js +15 -12
  89. package/dist/compose/util-empty-scalar-position.d.ts +1 -1
  90. package/dist/doc/Document.d.ts +27 -15
  91. package/dist/doc/Document.js +48 -97
  92. package/dist/doc/anchors.d.ts +24 -0
  93. package/dist/doc/anchors.js +77 -0
  94. package/dist/doc/createNode.d.ts +8 -10
  95. package/dist/doc/createNode.js +26 -16
  96. package/dist/errors.d.ts +11 -8
  97. package/dist/errors.js +16 -12
  98. package/dist/index.d.ts +5 -3
  99. package/dist/index.js +3 -3
  100. package/dist/nodes/Alias.d.ts +19 -9
  101. package/dist/nodes/Alias.js +51 -28
  102. package/dist/nodes/Collection.d.ts +3 -17
  103. package/dist/nodes/Collection.js +4 -82
  104. package/dist/nodes/Node.d.ts +6 -4
  105. package/dist/nodes/Node.js +2 -0
  106. package/dist/nodes/Pair.d.ts +6 -11
  107. package/dist/nodes/Pair.js +8 -231
  108. package/dist/nodes/Scalar.d.ts +6 -3
  109. package/dist/nodes/YAMLMap.d.ts +3 -3
  110. package/dist/nodes/YAMLMap.js +8 -4
  111. package/dist/nodes/YAMLSeq.d.ts +3 -3
  112. package/dist/nodes/YAMLSeq.js +7 -4
  113. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  114. package/dist/nodes/addPairToJSMap.js +105 -0
  115. package/dist/nodes/toJS.d.ts +4 -5
  116. package/dist/nodes/toJS.js +12 -10
  117. package/dist/options.d.ts +20 -26
  118. package/dist/options.js +0 -2
  119. package/dist/parse/cst-scalar.d.ts +58 -0
  120. package/dist/parse/cst-scalar.js +223 -0
  121. package/dist/parse/cst-stringify.d.ts +8 -0
  122. package/dist/parse/cst-stringify.js +63 -0
  123. package/dist/parse/cst-visit.d.ts +39 -0
  124. package/dist/parse/cst-visit.js +99 -0
  125. package/dist/parse/{tokens.d.ts → cst.d.ts} +23 -7
  126. package/{browser/dist/parse/tokens.js → dist/parse/cst.js} +27 -1
  127. package/dist/parse/lexer.d.ts +4 -13
  128. package/dist/parse/lexer.js +100 -106
  129. package/dist/parse/parser.d.ts +30 -24
  130. package/dist/parse/parser.js +221 -146
  131. package/dist/public-api.d.ts +7 -7
  132. package/dist/public-api.js +11 -14
  133. package/dist/{doc → schema}/Schema.d.ts +5 -3
  134. package/dist/schema/Schema.js +25 -0
  135. package/dist/{tags/failsafe → schema/common}/map.d.ts +0 -0
  136. package/dist/{tags/failsafe → schema/common}/map.js +0 -0
  137. package/dist/schema/common/null.d.ts +4 -0
  138. package/dist/schema/common/null.js +15 -0
  139. package/dist/{tags/failsafe → schema/common}/seq.d.ts +0 -0
  140. package/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  141. package/dist/{tags/failsafe → schema/common}/string.d.ts +0 -0
  142. package/dist/{tags/failsafe → schema/common}/string.js +0 -0
  143. package/dist/schema/core/bool.d.ts +4 -0
  144. package/dist/schema/core/bool.js +21 -0
  145. package/dist/schema/core/float.d.ts +4 -0
  146. package/dist/schema/core/float.js +44 -0
  147. package/dist/schema/core/int.d.ts +4 -0
  148. package/dist/schema/core/int.js +42 -0
  149. package/dist/schema/core/schema.d.ts +1 -0
  150. package/dist/schema/core/schema.js +25 -0
  151. package/dist/schema/json/schema.d.ts +2 -0
  152. package/dist/{tags/json.js → schema/json/schema.js} +5 -6
  153. package/dist/schema/tags.d.ts +41 -0
  154. package/dist/schema/tags.js +82 -0
  155. package/dist/{tags → schema}/types.d.ts +3 -7
  156. package/dist/{tags → schema}/yaml-1.1/binary.d.ts +0 -0
  157. package/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  158. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  159. package/dist/schema/yaml-1.1/bool.js +29 -0
  160. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  161. package/dist/schema/yaml-1.1/float.js +47 -0
  162. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  163. package/dist/schema/yaml-1.1/int.js +76 -0
  164. package/dist/{tags → schema}/yaml-1.1/omap.d.ts +0 -0
  165. package/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  166. package/dist/{tags → schema}/yaml-1.1/pairs.d.ts +3 -2
  167. package/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  168. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  169. package/dist/schema/yaml-1.1/schema.js +39 -0
  170. package/dist/{tags → schema}/yaml-1.1/set.d.ts +1 -1
  171. package/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  172. package/dist/{tags → schema}/yaml-1.1/timestamp.d.ts +0 -0
  173. package/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  174. package/dist/stringify/addComment.js +0 -7
  175. package/dist/stringify/stringify.d.ts +1 -2
  176. package/dist/stringify/stringify.js +8 -6
  177. package/dist/stringify/stringifyCollection.d.ts +21 -0
  178. package/dist/stringify/stringifyCollection.js +110 -0
  179. package/dist/stringify/stringifyDocument.d.ts +3 -0
  180. package/dist/stringify/stringifyDocument.js +63 -0
  181. package/dist/stringify/stringifyPair.d.ts +3 -0
  182. package/dist/stringify/stringifyPair.js +115 -0
  183. package/dist/stringify/stringifyString.js +53 -52
  184. package/dist/test-events.d.ts +1 -2
  185. package/dist/test-events.js +25 -12
  186. package/dist/util.d.ts +1 -1
  187. package/dist/visit.d.ts +10 -1
  188. package/dist/visit.js +20 -1
  189. package/package.json +4 -3
  190. package/browser/dist/doc/Anchors.js +0 -102
  191. package/browser/dist/doc/Schema.js +0 -27
  192. package/browser/dist/doc/getSchemaTags.js +0 -30
  193. package/browser/dist/tags/core.js +0 -109
  194. package/browser/dist/tags/failsafe/index.js +0 -7
  195. package/browser/dist/tags/index.js +0 -34
  196. package/browser/dist/tags/yaml-1.1/index.js +0 -152
  197. package/dist/doc/Anchors.d.ts +0 -36
  198. package/dist/doc/Anchors.js +0 -104
  199. package/dist/doc/Schema.js +0 -29
  200. package/dist/doc/getSchemaTags.d.ts +0 -3
  201. package/dist/doc/getSchemaTags.js +0 -32
  202. package/dist/tags/core.d.ts +0 -14
  203. package/dist/tags/core.js +0 -119
  204. package/dist/tags/failsafe/index.d.ts +0 -1
  205. package/dist/tags/failsafe/index.js +0 -9
  206. package/dist/tags/index.d.ts +0 -31
  207. package/dist/tags/index.js +0 -37
  208. package/dist/tags/json.d.ts +0 -2
  209. package/dist/tags/yaml-1.1/index.d.ts +0 -2
  210. package/dist/tags/yaml-1.1/index.js +0 -154
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
+ var anchors = require('../doc/anchors.js');
4
+ var visit = require('../visit.js');
3
5
  var Node = require('./Node.js');
4
- var toJS = require('./toJS.js');
5
6
 
6
7
  class Alias extends Node.NodeBase {
7
8
  constructor(source) {
@@ -13,58 +14,80 @@ class Alias extends Node.NodeBase {
13
14
  }
14
15
  });
15
16
  }
16
- toJSON(arg, ctx) {
17
+ /**
18
+ * Resolve the value of this alias within `doc`, finding the last
19
+ * instance of the `source` anchor before this node.
20
+ */
21
+ resolve(doc) {
22
+ let found = undefined;
23
+ visit.visit(doc, {
24
+ Node: (_key, node) => {
25
+ if (node === this)
26
+ return visit.visit.BREAK;
27
+ if (node.anchor === this.source)
28
+ found = node;
29
+ }
30
+ });
31
+ return found;
32
+ }
33
+ toJSON(_arg, ctx) {
17
34
  if (!ctx)
18
- return toJS.toJS(this.source, typeof arg === 'string' ? arg : null, ctx);
19
- const { anchors, maxAliasCount } = ctx;
20
- const anchor = anchors && anchors.get(this.source);
35
+ return { source: this.source };
36
+ const { anchors, doc, maxAliasCount } = ctx;
37
+ const source = this.resolve(doc);
38
+ if (!source) {
39
+ const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
40
+ throw new ReferenceError(msg);
41
+ }
42
+ const data = anchors.get(source);
21
43
  /* istanbul ignore if */
22
- if (!anchor || anchor.res === undefined) {
44
+ if (!data || data.res === undefined) {
23
45
  const msg = 'This should not happen: Alias anchor was not resolved?';
24
46
  throw new ReferenceError(msg);
25
47
  }
26
48
  if (maxAliasCount >= 0) {
27
- anchor.count += 1;
28
- if (anchor.aliasCount === 0)
29
- anchor.aliasCount = getAliasCount(this.source, anchors);
30
- if (anchor.count * anchor.aliasCount > maxAliasCount) {
49
+ data.count += 1;
50
+ if (data.aliasCount === 0)
51
+ data.aliasCount = getAliasCount(doc, source, anchors);
52
+ if (data.count * data.aliasCount > maxAliasCount) {
31
53
  const msg = 'Excessive alias count indicates a resource exhaustion attack';
32
54
  throw new ReferenceError(msg);
33
55
  }
34
56
  }
35
- return anchor.res;
57
+ return data.res;
36
58
  }
37
- // Only called when stringifying an alias mapping key while constructing
38
- // Object output.
39
- toString({ anchors, doc, implicitKey, inStringifyKey }, _onComment, _onChompKeep) {
40
- let anchor = Object.keys(anchors).find(a => anchors[a] === this.source);
41
- if (!anchor && inStringifyKey)
42
- anchor = doc.anchors.getName(this.source) || doc.anchors.newName();
43
- if (anchor)
44
- return `*${anchor}${implicitKey ? ' ' : ''}`;
45
- const msg = doc.anchors.getName(this.source)
46
- ? 'Alias node must be after source node'
47
- : 'Source node not found for alias node';
48
- throw new Error(`${msg} [${this.range}]`);
59
+ toString(ctx, _onComment, _onChompKeep) {
60
+ const src = `*${this.source}`;
61
+ if (ctx) {
62
+ anchors.anchorIsValid(this.source);
63
+ if (ctx.options.verifyAliasOrder && !ctx.anchors.has(this.source)) {
64
+ const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
65
+ throw new Error(msg);
66
+ }
67
+ if (ctx.implicitKey)
68
+ return `${src} `;
69
+ }
70
+ return src;
49
71
  }
50
72
  }
51
- function getAliasCount(node, anchors) {
73
+ function getAliasCount(doc, node, anchors) {
52
74
  if (Node.isAlias(node)) {
53
- const anchor = anchors && anchors.get(node.source);
75
+ const source = node.resolve(doc);
76
+ const anchor = anchors && source && anchors.get(source);
54
77
  return anchor ? anchor.count * anchor.aliasCount : 0;
55
78
  }
56
79
  else if (Node.isCollection(node)) {
57
80
  let count = 0;
58
81
  for (const item of node.items) {
59
- const c = getAliasCount(item, anchors);
82
+ const c = getAliasCount(doc, item, anchors);
60
83
  if (c > count)
61
84
  count = c;
62
85
  }
63
86
  return count;
64
87
  }
65
88
  else if (Node.isPair(node)) {
66
- const kc = getAliasCount(node.key, anchors);
67
- const vc = getAliasCount(node.value, anchors);
89
+ const kc = getAliasCount(doc, node.key, anchors);
90
+ const vc = getAliasCount(doc, node.value, anchors);
68
91
  return Math.max(kc, vc);
69
92
  }
70
93
  return 1;
@@ -1,27 +1,14 @@
1
- import type { Schema } from '../doc/Schema.js';
2
- import { StringifyContext } from '../stringify/stringify.js';
1
+ import type { Schema } from '../schema/Schema.js';
3
2
  import { NodeBase, NODE_TYPE } from './Node.js';
4
3
  export declare function collectionFromPath(schema: Schema, path: unknown[], value: unknown): import("./Node.js").Node;
5
4
  export declare const isEmptyPath: (path: Iterable<unknown> | null | undefined) => boolean;
6
- export declare namespace Collection {
7
- interface StringifyContext {
8
- blockItem(node: StringifyNode): string;
9
- flowChars: {
10
- start: '{' | '[';
11
- end: '}' | ']';
12
- };
13
- itemIndent: string;
14
- }
15
- interface StringifyNode {
16
- type: 'comment' | 'item';
17
- str: string;
18
- }
19
- }
20
5
  export declare abstract class Collection extends NodeBase {
21
6
  static maxFlowStringSingleLineLength: number;
22
7
  schema: Schema | undefined;
23
8
  [NODE_TYPE]: symbol;
24
9
  items: unknown[];
10
+ /** An optional anchor on this node. Used by alias nodes. */
11
+ anchor?: string;
25
12
  /**
26
13
  * If true, stringify this and all child nodes using flow rather than
27
14
  * block styles.
@@ -77,5 +64,4 @@ export declare abstract class Collection extends NodeBase {
77
64
  * boolean to add/remove the item from the set.
78
65
  */
79
66
  setIn([key, ...rest]: Iterable<unknown>, value: unknown): void;
80
- _toString(ctx: StringifyContext, { blockItem, flowChars, itemIndent }: Collection.StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
81
67
  }
@@ -1,8 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  var createNode = require('../doc/createNode.js');
4
- var addComment = require('../stringify/addComment.js');
5
- var stringify = require('../stringify/stringify.js');
6
4
  var Node = require('./Node.js');
7
5
 
8
6
  function collectionFromPath(schema, path, value) {
@@ -26,11 +24,11 @@ function collectionFromPath(schema, path, value) {
26
24
  }
27
25
  }
28
26
  return createNode.createNode(v, undefined, {
29
- onAlias() {
27
+ onAnchor() {
30
28
  throw new Error('Repeated objects are not supported here');
31
29
  },
32
- prevObjects: new Map(),
33
- schema
30
+ schema,
31
+ sourceObjects: new Map()
34
32
  });
35
33
  }
36
34
  // null, undefined, or an empty non-string iterable (e.g. [])
@@ -92,7 +90,7 @@ class Collection extends Node.NodeBase {
92
90
  }
93
91
  hasAllNullValues(allowScalar) {
94
92
  return this.items.every(node => {
95
- if (!node || Node.isNode(node))
93
+ if (!Node.isPair(node))
96
94
  return false;
97
95
  const n = node.value;
98
96
  return (n == null ||
@@ -131,82 +129,6 @@ class Collection extends Node.NodeBase {
131
129
  throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
132
130
  }
133
131
  }
134
- _toString(ctx, { blockItem, flowChars, itemIndent }, onComment, onChompKeep) {
135
- const { indent, indentStep } = ctx;
136
- const inFlow = this.flow || ctx.inFlow;
137
- if (inFlow)
138
- itemIndent += indentStep;
139
- ctx = Object.assign({}, ctx, { indent: itemIndent, inFlow, type: null });
140
- let chompKeep = false;
141
- let hasItemWithNewLine = false;
142
- const nodes = this.items.reduce((nodes, item, i) => {
143
- let comment = null;
144
- if (Node.isNode(item) || Node.isPair(item)) {
145
- if (!chompKeep && item.spaceBefore)
146
- nodes.push({ type: 'comment', str: '' });
147
- if (item.commentBefore) {
148
- // This match will always succeed on a non-empty string
149
- for (const line of item.commentBefore.match(/^.*$/gm))
150
- nodes.push({ type: 'comment', str: `#${line}` });
151
- }
152
- if (item.comment)
153
- comment = item.comment;
154
- const pair = item; // Apply guards manually in the following
155
- if (inFlow &&
156
- ((!chompKeep && item.spaceBefore) ||
157
- item.commentBefore ||
158
- item.comment ||
159
- (pair.key && (pair.key.commentBefore || pair.key.comment)) ||
160
- (pair.value && (pair.value.commentBefore || pair.value.comment))))
161
- hasItemWithNewLine = true;
162
- }
163
- chompKeep = false;
164
- let str = stringify.stringify(item, ctx, () => (comment = null), () => (chompKeep = true));
165
- if (inFlow && !hasItemWithNewLine && str.includes('\n'))
166
- hasItemWithNewLine = true;
167
- if (inFlow && i < this.items.length - 1)
168
- str += ',';
169
- str = addComment.addComment(str, itemIndent, comment);
170
- if (chompKeep && (comment || inFlow))
171
- chompKeep = false;
172
- nodes.push({ type: 'item', str });
173
- return nodes;
174
- }, []);
175
- let str;
176
- if (nodes.length === 0) {
177
- str = flowChars.start + flowChars.end;
178
- }
179
- else if (inFlow) {
180
- const { start, end } = flowChars;
181
- const strings = nodes.map(n => n.str);
182
- if (hasItemWithNewLine ||
183
- strings.reduce((sum, str) => sum + str.length + 2, 2) >
184
- Collection.maxFlowStringSingleLineLength) {
185
- str = start;
186
- for (const s of strings) {
187
- str += s ? `\n${indentStep}${indent}${s}` : '\n';
188
- }
189
- str += `\n${indent}${end}`;
190
- }
191
- else {
192
- str = `${start} ${strings.join(' ')} ${end}`;
193
- }
194
- }
195
- else {
196
- const strings = nodes.map(blockItem);
197
- str = strings.shift() || '';
198
- for (const s of strings)
199
- str += s ? `\n${indent}${s}` : '\n';
200
- }
201
- if (this.comment) {
202
- str += '\n' + this.comment.replace(/^/gm, `${indent}#`);
203
- if (onComment)
204
- onComment();
205
- }
206
- else if (chompKeep && onChompKeep)
207
- onChompKeep();
208
- return str;
209
- }
210
132
  }
211
133
  Collection.maxFlowStringSingleLineLength = 60;
212
134
 
@@ -7,6 +7,7 @@ import type { YAMLMap } from './YAMLMap.js';
7
7
  import type { YAMLSeq } from './YAMLSeq.js';
8
8
  export declare type Node = Alias | Scalar | YAMLMap | YAMLSeq;
9
9
  export declare type ParsedNode = Alias.Parsed | Scalar.Parsed | YAMLMap.Parsed | YAMLSeq.Parsed;
10
+ export declare type Range = [number, number, number];
10
11
  export declare const ALIAS: unique symbol;
11
12
  export declare const DOC: unique symbol;
12
13
  export declare const MAP: unique symbol;
@@ -14,7 +15,7 @@ export declare const PAIR: unique symbol;
14
15
  export declare const SCALAR: unique symbol;
15
16
  export declare const SEQ: unique symbol;
16
17
  export declare const NODE_TYPE: unique symbol;
17
- export declare const isAlias: (node: any) => node is Alias<Node>;
18
+ export declare const isAlias: (node: any) => node is Alias;
18
19
  export declare const isDocument: (node: any) => node is Document<unknown>;
19
20
  export declare const isMap: (node: any) => node is YAMLMap<unknown, unknown>;
20
21
  export declare const isPair: (node: any) => node is Pair<unknown, unknown>;
@@ -22,6 +23,7 @@ export declare const isScalar: (node: any) => node is Scalar<unknown>;
22
23
  export declare const isSeq: (node: any) => node is YAMLSeq<unknown>;
23
24
  export declare function isCollection(node: any): node is YAMLMap | YAMLSeq;
24
25
  export declare function isNode(node: any): node is Node;
26
+ export declare const hasAnchor: (node: unknown) => node is YAMLMap<unknown, unknown> | YAMLSeq<unknown> | Scalar<unknown>;
25
27
  export declare abstract class NodeBase {
26
28
  readonly [NODE_TYPE]: symbol;
27
29
  /** A comment on or immediately after this */
@@ -29,10 +31,10 @@ export declare abstract class NodeBase {
29
31
  /** A comment before this */
30
32
  commentBefore?: string | null;
31
33
  /**
32
- * The [start, end] range of characters of the source parsed
33
- * into this node (undefined for pairs or if not parsed)
34
+ * The [start, value-end, node-end] character offsets for the part of the
35
+ * source parsed into this node (undefined if not parsed).
34
36
  */
35
- range?: [number, number] | null;
37
+ range?: Range | null;
36
38
  /** A blank line before this node and its commentBefore */
37
39
  spaceBefore?: boolean;
38
40
  /** A fully qualified tag, if required */
@@ -33,6 +33,7 @@ function isNode(node) {
33
33
  }
34
34
  return false;
35
35
  }
36
+ const hasAnchor = (node) => (isScalar(node) || isCollection(node)) && !!node.anchor;
36
37
  class NodeBase {
37
38
  constructor(type) {
38
39
  Object.defineProperty(this, NODE_TYPE, { value: type });
@@ -47,6 +48,7 @@ exports.NodeBase = NodeBase;
47
48
  exports.PAIR = PAIR;
48
49
  exports.SCALAR = SCALAR;
49
50
  exports.SEQ = SEQ;
51
+ exports.hasAnchor = hasAnchor;
50
52
  exports.isAlias = isAlias;
51
53
  exports.isCollection = isCollection;
52
54
  exports.isDocument = isDocument;
@@ -1,21 +1,16 @@
1
1
  import { CreateNodeContext } from '../doc/createNode.js';
2
2
  import { StringifyContext } from '../stringify/stringify.js';
3
- import { Scalar } from './Scalar.js';
3
+ import { addPairToJSMap } from './addPairToJSMap.js';
4
+ import { NODE_TYPE } from './Node.js';
4
5
  import { ToJSContext } from './toJS.js';
5
- import { Node, NodeBase } from './Node.js';
6
- export declare function createPair(key: unknown, value: unknown, ctx: CreateNodeContext): Pair<Node, import("./Alias.js").Alias<Node> | Scalar<unknown> | import("./YAMLMap.js").YAMLMap<unknown, unknown> | import("./YAMLSeq.js").YAMLSeq<unknown>>;
7
- export declare class Pair<K = unknown, V = unknown> extends NodeBase {
8
- static readonly MERGE_KEY = "<<";
6
+ export declare function createPair(key: unknown, value: unknown, ctx: CreateNodeContext): Pair<import("./Node.js").Node, import("./Alias.js").Alias | import("./Scalar.js").Scalar<unknown> | import("./YAMLMap.js").YAMLMap<unknown, unknown> | import("./YAMLSeq.js").YAMLSeq<unknown>>;
7
+ export declare class Pair<K = unknown, V = unknown> {
8
+ readonly [NODE_TYPE]: symbol;
9
9
  /** Always Node or null when parsed, but can be set to anything. */
10
10
  key: K;
11
11
  /** Always Node or null when parsed, but can be set to anything. */
12
12
  value: V | null;
13
13
  constructor(key: K, value?: V | null);
14
- get commentBefore(): string | null | undefined;
15
- set commentBefore(cb: string | null | undefined);
16
- get spaceBefore(): boolean | undefined;
17
- set spaceBefore(sb: boolean | undefined);
18
- addToJSMap(ctx: ToJSContext | undefined, map: Map<unknown, unknown> | Set<unknown> | Record<string | number | symbol, unknown>): Map<unknown, unknown> | Set<unknown> | Record<string | number | symbol, unknown>;
19
- toJSON(_?: unknown, ctx?: ToJSContext): Map<unknown, unknown> | Set<unknown> | Record<string | number | symbol, unknown>;
14
+ toJSON(_?: unknown, ctx?: ToJSContext): ReturnType<typeof addPairToJSMap>;
20
15
  toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
21
16
  }
@@ -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,21 @@ 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;
119
- }
120
19
  toJSON(_, ctx) {
121
20
  const pair = ctx && ctx.mapAsMap ? new Map() : {};
122
- return this.addToJSMap(ctx, pair);
21
+ return addPairToJSMap.addPairToJSMap(ctx, pair, this);
123
22
  }
124
23
  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;
24
+ return ctx && ctx.doc
25
+ ? stringifyPair.stringifyPair(this, ctx, onComment, onChompKeep)
26
+ : JSON.stringify(this);
249
27
  }
250
- return JSON.stringify(jsKey);
251
28
  }
252
29
 
253
30
  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 {