yaml 2.0.0-2 → 2.0.0-6

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 (347) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +46 -26
  3. package/browser/dist/compose/compose-collection.js +59 -0
  4. package/browser/dist/compose/compose-doc.js +39 -0
  5. package/browser/dist/compose/compose-node.js +72 -0
  6. package/browser/dist/compose/compose-scalar.js +74 -0
  7. package/browser/dist/compose/composer.js +218 -0
  8. package/browser/dist/compose/resolve-block-map.js +93 -0
  9. package/browser/dist/compose/resolve-block-scalar.js +185 -0
  10. package/browser/dist/compose/resolve-block-seq.js +40 -0
  11. package/browser/dist/compose/resolve-end.js +37 -0
  12. package/browser/dist/compose/resolve-flow-collection.js +189 -0
  13. package/browser/dist/compose/resolve-flow-scalar.js +211 -0
  14. package/browser/dist/compose/resolve-props.js +126 -0
  15. package/browser/dist/compose/util-contains-newline.js +34 -0
  16. package/browser/dist/compose/util-empty-scalar-position.js +27 -0
  17. package/browser/dist/compose/util-map-includes.js +17 -0
  18. package/browser/dist/doc/Document.js +251 -348
  19. package/browser/dist/doc/anchors.js +72 -0
  20. package/browser/dist/doc/applyReviver.js +41 -28
  21. package/browser/dist/doc/createNode.js +71 -55
  22. package/browser/dist/doc/directives.js +161 -0
  23. package/browser/dist/errors.js +50 -72
  24. package/browser/dist/index.js +17 -73
  25. package/browser/dist/log.js +11 -7
  26. package/browser/dist/nodes/Alias.js +94 -0
  27. package/browser/dist/nodes/Collection.js +133 -0
  28. package/browser/dist/nodes/Node.js +41 -0
  29. package/browser/dist/nodes/Pair.js +28 -0
  30. package/browser/dist/nodes/Scalar.js +23 -0
  31. package/browser/dist/nodes/YAMLMap.js +116 -0
  32. package/browser/dist/nodes/YAMLSeq.js +105 -0
  33. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  34. package/browser/dist/nodes/toJS.js +35 -0
  35. package/browser/dist/options.js +14 -95
  36. package/browser/dist/parse/cst-scalar.js +219 -0
  37. package/browser/dist/parse/cst-stringify.js +61 -0
  38. package/browser/dist/parse/cst-visit.js +97 -0
  39. package/browser/dist/parse/cst.js +98 -0
  40. package/browser/dist/parse/lexer.js +656 -0
  41. package/browser/dist/parse/line-counter.js +39 -0
  42. package/browser/dist/parse/parser.js +876 -0
  43. package/browser/dist/public-api.js +101 -0
  44. package/browser/dist/schema/Schema.js +23 -0
  45. package/browser/dist/schema/common/map.js +42 -0
  46. package/browser/dist/schema/common/null.js +13 -0
  47. package/browser/dist/schema/common/seq.js +33 -0
  48. package/browser/dist/schema/common/string.js +14 -0
  49. package/browser/dist/schema/core/bool.js +19 -0
  50. package/browser/dist/schema/core/float.js +40 -0
  51. package/browser/dist/schema/core/int.js +38 -0
  52. package/browser/dist/schema/core/schema.js +23 -0
  53. package/browser/dist/schema/json/schema.js +62 -0
  54. package/browser/dist/schema/tags.js +79 -0
  55. package/browser/dist/schema/yaml-1.1/binary.js +66 -0
  56. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  57. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  58. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  59. package/browser/dist/schema/yaml-1.1/omap.js +73 -0
  60. package/browser/dist/schema/yaml-1.1/pairs.js +77 -0
  61. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  62. package/browser/dist/schema/yaml-1.1/set.js +87 -0
  63. package/browser/dist/schema/yaml-1.1/timestamp.js +101 -0
  64. package/browser/dist/stringify/foldFlowLines.js +121 -139
  65. package/browser/dist/stringify/stringify.js +87 -72
  66. package/browser/dist/stringify/stringifyCollection.js +122 -0
  67. package/browser/dist/stringify/stringifyComment.js +14 -0
  68. package/browser/dist/stringify/stringifyDocument.js +64 -0
  69. package/browser/dist/stringify/stringifyNumber.js +20 -23
  70. package/browser/dist/stringify/stringifyPair.js +111 -0
  71. package/browser/dist/stringify/stringifyString.js +295 -322
  72. package/browser/dist/util.js +4 -4
  73. package/browser/dist/visit.js +146 -0
  74. package/browser/index.js +4 -1
  75. package/dist/compose/compose-collection.d.ts +5 -0
  76. package/dist/compose/compose-collection.js +61 -0
  77. package/dist/compose/compose-doc.d.ts +6 -0
  78. package/dist/compose/compose-doc.js +41 -0
  79. package/dist/compose/compose-node.d.ts +25 -0
  80. package/dist/compose/compose-node.js +75 -0
  81. package/dist/compose/compose-scalar.d.ts +5 -0
  82. package/dist/compose/compose-scalar.js +76 -0
  83. package/dist/compose/composer.d.ts +62 -0
  84. package/dist/compose/composer.js +222 -0
  85. package/dist/compose/resolve-block-map.d.ts +6 -0
  86. package/dist/compose/resolve-block-map.js +95 -0
  87. package/dist/compose/resolve-block-scalar.d.ts +10 -0
  88. package/dist/compose/resolve-block-scalar.js +187 -0
  89. package/dist/compose/resolve-block-seq.d.ts +5 -0
  90. package/dist/compose/resolve-block-seq.js +42 -0
  91. package/dist/compose/resolve-end.d.ts +6 -0
  92. package/dist/compose/resolve-end.js +39 -0
  93. package/dist/compose/resolve-flow-collection.d.ts +6 -0
  94. package/dist/compose/resolve-flow-collection.js +191 -0
  95. package/dist/compose/resolve-flow-scalar.d.ts +10 -0
  96. package/dist/compose/resolve-flow-scalar.js +213 -0
  97. package/dist/compose/resolve-props.d.ts +21 -0
  98. package/dist/compose/resolve-props.js +128 -0
  99. package/dist/compose/util-contains-newline.d.ts +2 -0
  100. package/dist/compose/util-contains-newline.js +36 -0
  101. package/dist/compose/util-empty-scalar-position.d.ts +2 -0
  102. package/dist/compose/util-empty-scalar-position.js +29 -0
  103. package/dist/compose/util-map-includes.d.ts +4 -0
  104. package/dist/compose/util-map-includes.js +19 -0
  105. package/dist/doc/Document.d.ts +130 -0
  106. package/dist/doc/Document.js +250 -347
  107. package/dist/doc/anchors.d.ts +24 -0
  108. package/dist/doc/anchors.js +77 -0
  109. package/dist/doc/applyReviver.d.ts +9 -0
  110. package/dist/doc/applyReviver.js +41 -28
  111. package/dist/doc/createNode.d.ts +16 -0
  112. package/dist/doc/createNode.js +71 -55
  113. package/dist/doc/directives.d.ts +46 -0
  114. package/dist/doc/directives.js +163 -0
  115. package/dist/errors.d.ts +21 -0
  116. package/dist/errors.js +51 -74
  117. package/dist/index.d.ts +19 -0
  118. package/dist/index.js +40 -71
  119. package/dist/log.d.ts +3 -0
  120. package/dist/log.js +11 -6
  121. package/dist/nodes/Alias.d.ts +24 -0
  122. package/dist/nodes/Alias.js +96 -0
  123. package/dist/nodes/Collection.d.ts +67 -0
  124. package/dist/nodes/Collection.js +137 -0
  125. package/dist/nodes/Node.d.ts +48 -0
  126. package/dist/nodes/Node.js +59 -0
  127. package/dist/nodes/Pair.d.ts +16 -0
  128. package/dist/nodes/Pair.js +31 -0
  129. package/dist/nodes/Scalar.d.ts +40 -0
  130. package/dist/nodes/Scalar.js +26 -0
  131. package/dist/nodes/YAMLMap.d.ts +39 -0
  132. package/dist/nodes/YAMLMap.js +119 -0
  133. package/dist/nodes/YAMLSeq.d.ts +53 -0
  134. package/dist/nodes/YAMLSeq.js +107 -0
  135. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  136. package/dist/nodes/addPairToJSMap.js +105 -0
  137. package/dist/nodes/toJS.d.ts +30 -0
  138. package/dist/nodes/toJS.js +37 -0
  139. package/dist/options.d.ts +278 -0
  140. package/dist/options.js +13 -96
  141. package/dist/parse/cst-scalar.d.ts +58 -0
  142. package/dist/parse/cst-scalar.js +223 -0
  143. package/dist/parse/cst-stringify.d.ts +8 -0
  144. package/dist/parse/cst-stringify.js +63 -0
  145. package/dist/parse/cst-visit.d.ts +39 -0
  146. package/dist/parse/cst-visit.js +99 -0
  147. package/dist/parse/cst.d.ts +106 -0
  148. package/dist/parse/cst.js +112 -0
  149. package/dist/parse/lexer.d.ts +85 -0
  150. package/dist/parse/lexer.js +658 -0
  151. package/dist/parse/line-counter.d.ts +22 -0
  152. package/dist/parse/line-counter.js +41 -0
  153. package/dist/parse/parser.d.ts +84 -0
  154. package/dist/parse/parser.js +880 -0
  155. package/dist/public-api.d.ts +43 -0
  156. package/dist/public-api.js +106 -0
  157. package/dist/schema/Schema.d.ts +16 -0
  158. package/dist/schema/Schema.js +25 -0
  159. package/dist/schema/common/map.d.ts +2 -0
  160. package/dist/schema/common/map.js +44 -0
  161. package/dist/schema/common/null.d.ts +4 -0
  162. package/dist/schema/common/null.js +15 -0
  163. package/dist/schema/common/seq.d.ts +2 -0
  164. package/dist/schema/common/seq.js +35 -0
  165. package/dist/schema/common/string.d.ts +2 -0
  166. package/dist/schema/common/string.js +16 -0
  167. package/dist/schema/core/bool.d.ts +4 -0
  168. package/dist/schema/core/bool.js +21 -0
  169. package/dist/schema/core/float.d.ts +4 -0
  170. package/dist/schema/core/float.js +44 -0
  171. package/dist/schema/core/int.d.ts +4 -0
  172. package/dist/schema/core/int.js +42 -0
  173. package/dist/schema/core/schema.d.ts +1 -0
  174. package/dist/schema/core/schema.js +25 -0
  175. package/dist/schema/json/schema.d.ts +2 -0
  176. package/dist/schema/json/schema.js +64 -0
  177. package/dist/schema/tags.d.ts +41 -0
  178. package/dist/schema/tags.js +82 -0
  179. package/dist/schema/types.d.ts +82 -0
  180. package/dist/schema/yaml-1.1/binary.d.ts +2 -0
  181. package/dist/schema/yaml-1.1/binary.js +68 -0
  182. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  183. package/dist/schema/yaml-1.1/bool.js +29 -0
  184. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  185. package/dist/schema/yaml-1.1/float.js +47 -0
  186. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  187. package/dist/schema/yaml-1.1/int.js +76 -0
  188. package/dist/schema/yaml-1.1/omap.d.ts +21 -0
  189. package/dist/schema/yaml-1.1/omap.js +76 -0
  190. package/dist/schema/yaml-1.1/pairs.d.ts +10 -0
  191. package/dist/schema/yaml-1.1/pairs.js +81 -0
  192. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  193. package/dist/schema/yaml-1.1/schema.js +39 -0
  194. package/dist/schema/yaml-1.1/set.d.ts +22 -0
  195. package/dist/schema/yaml-1.1/set.js +90 -0
  196. package/dist/schema/yaml-1.1/timestamp.d.ts +6 -0
  197. package/dist/schema/yaml-1.1/timestamp.js +105 -0
  198. package/dist/stringify/foldFlowLines.d.ts +34 -0
  199. package/dist/stringify/foldFlowLines.js +121 -139
  200. package/dist/stringify/stringify.d.ts +18 -0
  201. package/dist/stringify/stringify.js +87 -71
  202. package/dist/stringify/stringifyCollection.d.ts +21 -0
  203. package/dist/stringify/stringifyCollection.js +124 -0
  204. package/dist/stringify/stringifyComment.d.ts +2 -0
  205. package/dist/stringify/stringifyComment.js +17 -0
  206. package/dist/stringify/stringifyDocument.d.ts +3 -0
  207. package/dist/stringify/stringifyDocument.js +66 -0
  208. package/dist/stringify/stringifyNumber.d.ts +2 -0
  209. package/dist/stringify/stringifyNumber.js +20 -23
  210. package/dist/stringify/stringifyPair.d.ts +3 -0
  211. package/dist/stringify/stringifyPair.js +113 -0
  212. package/dist/stringify/stringifyString.d.ts +3 -0
  213. package/dist/stringify/stringifyString.js +295 -322
  214. package/dist/test-events.d.ts +4 -0
  215. package/dist/test-events.js +124 -148
  216. package/dist/util.d.ts +6 -0
  217. package/dist/util.js +9 -12
  218. package/dist/visit.d.ts +63 -0
  219. package/dist/visit.js +148 -0
  220. package/package.json +31 -39
  221. package/util.js +2 -14
  222. package/browser/dist/_virtual/_rollupPluginBabelHelpers.js +0 -16
  223. package/browser/dist/ast/Alias.js +0 -95
  224. package/browser/dist/ast/Collection.js +0 -192
  225. package/browser/dist/ast/Merge.js +0 -71
  226. package/browser/dist/ast/Node.js +0 -3
  227. package/browser/dist/ast/Pair.js +0 -200
  228. package/browser/dist/ast/Scalar.js +0 -21
  229. package/browser/dist/ast/YAMLMap.js +0 -91
  230. package/browser/dist/ast/YAMLSeq.js +0 -67
  231. package/browser/dist/ast/toJS.js +0 -29
  232. package/browser/dist/ast/visit.js +0 -83
  233. package/browser/dist/constants.js +0 -40
  234. package/browser/dist/cst/Alias.js +0 -26
  235. package/browser/dist/cst/BlankLine.js +0 -34
  236. package/browser/dist/cst/BlockValue.js +0 -240
  237. package/browser/dist/cst/Collection.js +0 -259
  238. package/browser/dist/cst/CollectionItem.js +0 -123
  239. package/browser/dist/cst/Comment.js +0 -27
  240. package/browser/dist/cst/Directive.js +0 -53
  241. package/browser/dist/cst/Document.js +0 -262
  242. package/browser/dist/cst/FlowCollection.js +0 -201
  243. package/browser/dist/cst/Node.js +0 -355
  244. package/browser/dist/cst/ParseContext.js +0 -234
  245. package/browser/dist/cst/PlainValue.js +0 -171
  246. package/browser/dist/cst/QuoteDouble.js +0 -224
  247. package/browser/dist/cst/QuoteSingle.js +0 -100
  248. package/browser/dist/cst/Range.js +0 -56
  249. package/browser/dist/cst/parse.js +0 -46
  250. package/browser/dist/cst/source-utils.js +0 -169
  251. package/browser/dist/doc/Anchors.js +0 -115
  252. package/browser/dist/doc/Schema.js +0 -34
  253. package/browser/dist/doc/getSchemaTags.js +0 -33
  254. package/browser/dist/doc/listTagNames.js +0 -27
  255. package/browser/dist/doc/parseContents.js +0 -60
  256. package/browser/dist/doc/parseDirectives.js +0 -107
  257. package/browser/dist/resolve/collection-utils.js +0 -86
  258. package/browser/dist/resolve/resolveMap.js +0 -315
  259. package/browser/dist/resolve/resolveNode.js +0 -173
  260. package/browser/dist/resolve/resolveScalar.js +0 -20
  261. package/browser/dist/resolve/resolveSeq.js +0 -170
  262. package/browser/dist/resolve/resolveTag.js +0 -107
  263. package/browser/dist/resolve/resolveTagName.js +0 -87
  264. package/browser/dist/stringify/addComment.js +0 -10
  265. package/browser/dist/stringify/stringifyTag.js +0 -28
  266. package/browser/dist/tags/core.js +0 -128
  267. package/browser/dist/tags/failsafe/index.js +0 -7
  268. package/browser/dist/tags/failsafe/map.js +0 -37
  269. package/browser/dist/tags/failsafe/seq.js +0 -34
  270. package/browser/dist/tags/failsafe/string.js +0 -20
  271. package/browser/dist/tags/index.js +0 -39
  272. package/browser/dist/tags/json.js +0 -62
  273. package/browser/dist/tags/options.js +0 -31
  274. package/browser/dist/tags/yaml-1.1/binary.js +0 -83
  275. package/browser/dist/tags/yaml-1.1/index.js +0 -174
  276. package/browser/dist/tags/yaml-1.1/omap.js +0 -86
  277. package/browser/dist/tags/yaml-1.1/pairs.js +0 -62
  278. package/browser/dist/tags/yaml-1.1/set.js +0 -78
  279. package/browser/dist/tags/yaml-1.1/timestamp.js +0 -101
  280. package/browser/dist/types.js +0 -10
  281. package/cst.d.ts +0 -185
  282. package/dist/_virtual/_rollupPluginBabelHelpers.js +0 -18
  283. package/dist/ast/Alias.js +0 -97
  284. package/dist/ast/Collection.js +0 -196
  285. package/dist/ast/Merge.js +0 -74
  286. package/dist/ast/Node.js +0 -5
  287. package/dist/ast/Pair.js +0 -203
  288. package/dist/ast/Scalar.js +0 -24
  289. package/dist/ast/YAMLMap.js +0 -94
  290. package/dist/ast/YAMLSeq.js +0 -69
  291. package/dist/ast/toJS.js +0 -31
  292. package/dist/ast/visit.js +0 -85
  293. package/dist/constants.js +0 -46
  294. package/dist/cst/Alias.js +0 -28
  295. package/dist/cst/BlankLine.js +0 -36
  296. package/dist/cst/BlockValue.js +0 -243
  297. package/dist/cst/Collection.js +0 -262
  298. package/dist/cst/CollectionItem.js +0 -125
  299. package/dist/cst/Comment.js +0 -29
  300. package/dist/cst/Directive.js +0 -55
  301. package/dist/cst/Document.js +0 -264
  302. package/dist/cst/FlowCollection.js +0 -203
  303. package/dist/cst/Node.js +0 -357
  304. package/dist/cst/ParseContext.js +0 -236
  305. package/dist/cst/PlainValue.js +0 -173
  306. package/dist/cst/QuoteDouble.js +0 -226
  307. package/dist/cst/QuoteSingle.js +0 -102
  308. package/dist/cst/Range.js +0 -58
  309. package/dist/cst/parse.js +0 -48
  310. package/dist/cst/source-utils.js +0 -173
  311. package/dist/doc/Anchors.js +0 -117
  312. package/dist/doc/Schema.js +0 -36
  313. package/dist/doc/getSchemaTags.js +0 -35
  314. package/dist/doc/listTagNames.js +0 -29
  315. package/dist/doc/parseContents.js +0 -62
  316. package/dist/doc/parseDirectives.js +0 -109
  317. package/dist/resolve/collection-utils.js +0 -91
  318. package/dist/resolve/resolveMap.js +0 -317
  319. package/dist/resolve/resolveNode.js +0 -175
  320. package/dist/resolve/resolveScalar.js +0 -22
  321. package/dist/resolve/resolveSeq.js +0 -172
  322. package/dist/resolve/resolveTag.js +0 -109
  323. package/dist/resolve/resolveTagName.js +0 -89
  324. package/dist/stringify/addComment.js +0 -13
  325. package/dist/stringify/stringifyTag.js +0 -30
  326. package/dist/tags/core.js +0 -138
  327. package/dist/tags/failsafe/index.js +0 -9
  328. package/dist/tags/failsafe/map.js +0 -39
  329. package/dist/tags/failsafe/seq.js +0 -36
  330. package/dist/tags/failsafe/string.js +0 -22
  331. package/dist/tags/index.js +0 -42
  332. package/dist/tags/json.js +0 -64
  333. package/dist/tags/options.js +0 -37
  334. package/dist/tags/yaml-1.1/binary.js +0 -85
  335. package/dist/tags/yaml-1.1/index.js +0 -176
  336. package/dist/tags/yaml-1.1/omap.js +0 -89
  337. package/dist/tags/yaml-1.1/pairs.js +0 -66
  338. package/dist/tags/yaml-1.1/set.js +0 -81
  339. package/dist/tags/yaml-1.1/timestamp.js +0 -105
  340. package/dist/types.js +0 -29
  341. package/index.d.ts +0 -519
  342. package/index.js +0 -1
  343. package/types.d.ts +0 -379
  344. package/types.js +0 -17
  345. package/types.mjs +0 -17
  346. package/util.d.ts +0 -76
  347. package/util.mjs +0 -15
@@ -0,0 +1,72 @@
1
+ import { isScalar, isCollection } from '../nodes/Node.js';
2
+ import { visit } from '../visit.js';
3
+
4
+ /**
5
+ * Verify that the input string is a valid anchor.
6
+ *
7
+ * Will throw on errors.
8
+ */
9
+ function anchorIsValid(anchor) {
10
+ if (/[\x00-\x19\s,[\]{}]/.test(anchor)) {
11
+ const sa = JSON.stringify(anchor);
12
+ const msg = `Anchor must not contain whitespace or control characters: ${sa}`;
13
+ throw new Error(msg);
14
+ }
15
+ return true;
16
+ }
17
+ function anchorNames(root) {
18
+ const anchors = new Set();
19
+ visit(root, {
20
+ Value(_key, node) {
21
+ if (node.anchor)
22
+ anchors.add(node.anchor);
23
+ }
24
+ });
25
+ return anchors;
26
+ }
27
+ /** Find a new anchor name with the given `prefix` and a one-indexed suffix. */
28
+ function findNewAnchor(prefix, exclude) {
29
+ for (let i = 1; true; ++i) {
30
+ const name = `${prefix}${i}`;
31
+ if (!exclude.has(name))
32
+ return name;
33
+ }
34
+ }
35
+ function createNodeAnchors(doc, prefix) {
36
+ const aliasObjects = [];
37
+ const sourceObjects = new Map();
38
+ let prevAnchors = null;
39
+ return {
40
+ onAnchor(source) {
41
+ aliasObjects.push(source);
42
+ if (!prevAnchors)
43
+ prevAnchors = anchorNames(doc);
44
+ const anchor = findNewAnchor(prefix, prevAnchors);
45
+ prevAnchors.add(anchor);
46
+ return anchor;
47
+ },
48
+ /**
49
+ * With circular references, the source node is only resolved after all
50
+ * of its child nodes are. This is why anchors are set only after all of
51
+ * the nodes have been created.
52
+ */
53
+ setAnchors() {
54
+ for (const source of aliasObjects) {
55
+ const ref = sourceObjects.get(source);
56
+ if (typeof ref === 'object' &&
57
+ ref.anchor &&
58
+ (isScalar(ref.node) || isCollection(ref.node))) {
59
+ ref.node.anchor = ref.anchor;
60
+ }
61
+ else {
62
+ const error = new Error('Failed to resolve repeated object (this should not happen)');
63
+ error.source = source;
64
+ throw error;
65
+ }
66
+ }
67
+ },
68
+ sourceObjects
69
+ };
70
+ }
71
+
72
+ export { anchorIsValid, anchorNames, createNodeAnchors, findNewAnchor };
@@ -6,36 +6,49 @@
6
6
  * Includes extensions for handling Map and Set objects.
7
7
  */
8
8
  function applyReviver(reviver, obj, key, val) {
9
- if (val && typeof val === 'object') {
10
- if (Array.isArray(val)) {
11
- for (let i = 0, len = val.length; i < len; ++i) {
12
- const v0 = val[i];
13
- const v1 = applyReviver(reviver, val, String(i), v0);
14
- if (v1 === undefined) delete val[i];else if (v1 !== v0) val[i] = v1;
15
- }
16
- } else if (val instanceof Map) {
17
- for (const k of Array.from(val.keys())) {
18
- const v0 = val.get(k);
19
- const v1 = applyReviver(reviver, val, k, v0);
20
- if (v1 === undefined) val.delete(k);else if (v1 !== v0) val.set(k, v1);
21
- }
22
- } else if (val instanceof Set) {
23
- for (const v0 of Array.from(val)) {
24
- const v1 = applyReviver(reviver, val, v0, v0);
25
- if (v1 === undefined) val.delete(v0);else if (v1 !== v0) {
26
- val.delete(v0);
27
- val.add(v1);
9
+ if (val && typeof val === 'object') {
10
+ if (Array.isArray(val)) {
11
+ for (let i = 0, len = val.length; i < len; ++i) {
12
+ const v0 = val[i];
13
+ const v1 = applyReviver(reviver, val, String(i), v0);
14
+ if (v1 === undefined)
15
+ delete val[i];
16
+ else if (v1 !== v0)
17
+ val[i] = v1;
18
+ }
19
+ }
20
+ else if (val instanceof Map) {
21
+ for (const k of Array.from(val.keys())) {
22
+ const v0 = val.get(k);
23
+ const v1 = applyReviver(reviver, val, k, v0);
24
+ if (v1 === undefined)
25
+ val.delete(k);
26
+ else if (v1 !== v0)
27
+ val.set(k, v1);
28
+ }
29
+ }
30
+ else if (val instanceof Set) {
31
+ for (const v0 of Array.from(val)) {
32
+ const v1 = applyReviver(reviver, val, v0, v0);
33
+ if (v1 === undefined)
34
+ val.delete(v0);
35
+ else if (v1 !== v0) {
36
+ val.delete(v0);
37
+ val.add(v1);
38
+ }
39
+ }
40
+ }
41
+ else {
42
+ for (const [k, v0] of Object.entries(val)) {
43
+ const v1 = applyReviver(reviver, val, k, v0);
44
+ if (v1 === undefined)
45
+ delete val[k];
46
+ else if (v1 !== v0)
47
+ val[k] = v1;
48
+ }
28
49
  }
29
- }
30
- } else {
31
- for (const [k, v0] of Object.entries(val)) {
32
- const v1 = applyReviver(reviver, val, k, v0);
33
- if (v1 === undefined) delete val[k];else if (v1 !== v0) val[k] = v1;
34
- }
35
50
  }
36
- }
37
-
38
- return reviver.call(obj, key, val);
51
+ return reviver.call(obj, key, val);
39
52
  }
40
53
 
41
54
  export { applyReviver };
@@ -1,62 +1,78 @@
1
- import { Node } from '../ast/Node.js';
2
- import { Scalar } from '../ast/Scalar.js';
3
- import { defaultTagPrefix } from '../constants.js';
1
+ import { Alias } from '../nodes/Alias.js';
2
+ import { isNode, isPair, MAP, SEQ } from '../nodes/Node.js';
3
+ import { Scalar } from '../nodes/Scalar.js';
4
4
 
5
+ const defaultTagPrefix = 'tag:yaml.org,2002:';
5
6
  function findTagObject(value, tagName, tags) {
6
- if (tagName) {
7
- const match = tags.filter(t => t.tag === tagName);
8
- const tagObj = match.find(t => !t.format) || match[0];
9
- if (!tagObj) throw new Error("Tag ".concat(tagName, " not found"));
10
- return tagObj;
11
- }
12
-
13
- return tags.find(t => t.identify && t.identify(value) && !t.format);
7
+ if (tagName) {
8
+ const match = tags.filter(t => t.tag === tagName);
9
+ const tagObj = match.find(t => !t.format) || match[0];
10
+ if (!tagObj)
11
+ throw new Error(`Tag ${tagName} not found`);
12
+ return tagObj;
13
+ }
14
+ return tags.find(t => t.identify && t.identify(value) && !t.format);
14
15
  }
15
-
16
16
  function createNode(value, tagName, ctx) {
17
- if (value instanceof Node) return value;
18
- const {
19
- onAlias,
20
- onTagObj,
21
- prevObjects,
22
- wrapScalars
23
- } = ctx;
24
- const {
25
- map,
26
- seq,
27
- tags
28
- } = ctx.schema;
29
- if (tagName && tagName.startsWith('!!')) tagName = defaultTagPrefix + tagName.slice(2);
30
- let tagObj = findTagObject(value, tagName, tags);
31
-
32
- if (!tagObj) {
33
- if (typeof value.toJSON === 'function') value = value.toJSON();
34
- if (!value || typeof value !== 'object') return wrapScalars ? new Scalar(value) : value;
35
- tagObj = value instanceof Map ? map : value[Symbol.iterator] ? seq : map;
36
- }
37
-
38
- if (onTagObj) {
39
- onTagObj(tagObj);
40
- delete ctx.onTagObj;
41
- } // Detect duplicate references to the same object & use Alias nodes for all
42
- // after first. The `obj` wrapper allows for circular references to resolve.
43
-
44
-
45
- const obj = {
46
- value: undefined,
47
- node: undefined
48
- };
49
-
50
- if (value && typeof value === 'object') {
51
- const prev = prevObjects.get(value);
52
- if (prev) return onAlias(prev);
53
- obj.value = value;
54
- prevObjects.set(value, obj);
55
- }
56
-
57
- obj.node = tagObj.createNode ? tagObj.createNode(ctx.schema, value, ctx) : wrapScalars ? new Scalar(value) : value;
58
- if (tagName && obj.node instanceof Node) obj.node.tag = tagName;
59
- return obj.node;
17
+ var _a, _b;
18
+ if (isNode(value))
19
+ return value;
20
+ if (isPair(value)) {
21
+ const map = (_b = (_a = ctx.schema[MAP]).createNode) === null || _b === void 0 ? void 0 : _b.call(_a, ctx.schema, null, ctx);
22
+ map.items.push(value);
23
+ return map;
24
+ }
25
+ if (value instanceof String ||
26
+ value instanceof Number ||
27
+ value instanceof Boolean ||
28
+ (typeof BigInt === 'function' && value instanceof BigInt) // not supported everywhere
29
+ ) {
30
+ // https://tc39.es/ecma262/#sec-serializejsonproperty
31
+ value = value.valueOf();
32
+ }
33
+ const { onAnchor, onTagObj, schema, sourceObjects } = ctx;
34
+ // Detect duplicate references to the same object & use Alias nodes for all
35
+ // after first. The `ref` wrapper allows for circular references to resolve.
36
+ let ref = undefined;
37
+ if (value && typeof value === 'object') {
38
+ ref = sourceObjects.get(value);
39
+ if (ref) {
40
+ if (!ref.anchor)
41
+ ref.anchor = onAnchor(value);
42
+ return new Alias(ref.anchor);
43
+ }
44
+ else {
45
+ ref = { anchor: null, node: null };
46
+ sourceObjects.set(value, ref);
47
+ }
48
+ }
49
+ if (tagName && tagName.startsWith('!!'))
50
+ tagName = defaultTagPrefix + tagName.slice(2);
51
+ let tagObj = findTagObject(value, tagName, schema.tags);
52
+ if (!tagObj) {
53
+ if (value && typeof value.toJSON === 'function')
54
+ value = value.toJSON();
55
+ if (!value || typeof value !== 'object')
56
+ return new Scalar(value);
57
+ tagObj =
58
+ value instanceof Map
59
+ ? schema[MAP]
60
+ : Symbol.iterator in Object(value)
61
+ ? schema[SEQ]
62
+ : schema[MAP];
63
+ }
64
+ if (onTagObj) {
65
+ onTagObj(tagObj);
66
+ delete ctx.onTagObj;
67
+ }
68
+ const node = (tagObj === null || tagObj === void 0 ? void 0 : tagObj.createNode)
69
+ ? tagObj.createNode(ctx.schema, value, ctx)
70
+ : new Scalar(value);
71
+ if (tagName)
72
+ node.tag = tagName;
73
+ if (ref)
74
+ ref.node = node;
75
+ return node;
60
76
  }
61
77
 
62
78
  export { createNode };
@@ -0,0 +1,161 @@
1
+ import { isNode } from '../nodes/Node.js';
2
+ import { visit } from '../visit.js';
3
+
4
+ const escapeChars = {
5
+ '!': '%21',
6
+ ',': '%2C',
7
+ '[': '%5B',
8
+ ']': '%5D',
9
+ '{': '%7B',
10
+ '}': '%7D'
11
+ };
12
+ const escapeTagName = (tn) => tn.replace(/[!,[\]{}]/g, ch => escapeChars[ch]);
13
+ class Directives {
14
+ constructor(yaml, tags) {
15
+ /**
16
+ * The directives-end/doc-start marker `---`. If `null`, a marker may still be
17
+ * included in the document's stringified representation.
18
+ */
19
+ this.marker = null;
20
+ this.yaml = Object.assign({}, Directives.defaultYaml, yaml);
21
+ this.tags = Object.assign({}, Directives.defaultTags, tags);
22
+ }
23
+ /**
24
+ * During parsing, get a Directives instance for the current document and
25
+ * update the stream state according to the current version's spec.
26
+ */
27
+ atDocument() {
28
+ const res = new Directives(this.yaml, this.tags);
29
+ switch (this.yaml.version) {
30
+ case '1.1':
31
+ this.atNextDocument = true;
32
+ break;
33
+ case '1.2':
34
+ this.atNextDocument = false;
35
+ this.yaml = {
36
+ explicit: Directives.defaultYaml.explicit,
37
+ version: '1.2'
38
+ };
39
+ this.tags = Object.assign({}, Directives.defaultTags);
40
+ break;
41
+ }
42
+ return res;
43
+ }
44
+ /**
45
+ * @param onError - May be called even if the action was successful
46
+ * @returns `true` on success
47
+ */
48
+ add(line, onError) {
49
+ if (this.atNextDocument) {
50
+ this.yaml = { explicit: Directives.defaultYaml.explicit, version: '1.1' };
51
+ this.tags = Object.assign({}, Directives.defaultTags);
52
+ this.atNextDocument = false;
53
+ }
54
+ const parts = line.trim().split(/[ \t]+/);
55
+ const name = parts.shift();
56
+ switch (name) {
57
+ case '%TAG': {
58
+ if (parts.length !== 2) {
59
+ onError(0, '%TAG directive should contain exactly two parts');
60
+ if (parts.length < 2)
61
+ return false;
62
+ }
63
+ const [handle, prefix] = parts;
64
+ this.tags[handle] = prefix;
65
+ return true;
66
+ }
67
+ case '%YAML': {
68
+ this.yaml.explicit = true;
69
+ if (parts.length < 1) {
70
+ onError(0, '%YAML directive should contain exactly one part');
71
+ return false;
72
+ }
73
+ const [version] = parts;
74
+ if (version === '1.1' || version === '1.2') {
75
+ this.yaml.version = version;
76
+ return true;
77
+ }
78
+ else {
79
+ onError(6, `Unsupported YAML version ${version}`, true);
80
+ return false;
81
+ }
82
+ }
83
+ default:
84
+ onError(0, `Unknown directive ${name}`, true);
85
+ return false;
86
+ }
87
+ }
88
+ /**
89
+ * Resolves a tag, matching handles to those defined in %TAG directives.
90
+ *
91
+ * @returns Resolved tag, which may also be the non-specific tag `'!'` or a
92
+ * `'!local'` tag, or `null` if unresolvable.
93
+ */
94
+ tagName(source, onError) {
95
+ if (source === '!')
96
+ return '!'; // non-specific tag
97
+ if (source[0] !== '!') {
98
+ onError(`Not a valid tag: ${source}`);
99
+ return null;
100
+ }
101
+ if (source[1] === '<') {
102
+ const verbatim = source.slice(2, -1);
103
+ if (verbatim === '!' || verbatim === '!!') {
104
+ onError(`Verbatim tags aren't resolved, so ${source} is invalid.`);
105
+ return null;
106
+ }
107
+ if (source[source.length - 1] !== '>')
108
+ onError('Verbatim tags must end with a >');
109
+ return verbatim;
110
+ }
111
+ const [, handle, suffix] = source.match(/^(.*!)([^!]*)$/);
112
+ if (!suffix)
113
+ onError(`The ${source} tag has no suffix`);
114
+ const prefix = this.tags[handle];
115
+ if (prefix)
116
+ return prefix + decodeURIComponent(suffix);
117
+ if (handle === '!')
118
+ return source; // local tag
119
+ onError(`Could not resolve tag: ${source}`);
120
+ return null;
121
+ }
122
+ /**
123
+ * Given a fully resolved tag, returns its printable string form,
124
+ * taking into account current tag prefixes and defaults.
125
+ */
126
+ tagString(tag) {
127
+ for (const [handle, prefix] of Object.entries(this.tags)) {
128
+ if (tag.startsWith(prefix))
129
+ return handle + escapeTagName(tag.substring(prefix.length));
130
+ }
131
+ return tag[0] === '!' ? tag : `!<${tag}>`;
132
+ }
133
+ toString(doc) {
134
+ const lines = this.yaml.explicit
135
+ ? [`%YAML ${this.yaml.version || '1.2'}`]
136
+ : [];
137
+ const tagEntries = Object.entries(this.tags);
138
+ let tagNames;
139
+ if (doc && tagEntries.length > 0 && isNode(doc.contents)) {
140
+ const tags = {};
141
+ visit(doc.contents, (_key, node) => {
142
+ if (isNode(node) && node.tag)
143
+ tags[node.tag] = true;
144
+ });
145
+ tagNames = Object.keys(tags);
146
+ }
147
+ else
148
+ tagNames = [];
149
+ for (const [handle, prefix] of tagEntries) {
150
+ if (handle === '!!' && prefix === 'tag:yaml.org,2002:')
151
+ continue;
152
+ if (!doc || tagNames.some(tn => tn.startsWith(prefix)))
153
+ lines.push(`%TAG ${handle} ${prefix}`);
154
+ }
155
+ return lines.join('\n');
156
+ }
157
+ }
158
+ Directives.defaultYaml = { explicit: false, version: '1.2' };
159
+ Directives.defaultTags = { '!!': 'tag:yaml.org,2002:' };
160
+
161
+ export { Directives };
@@ -1,79 +1,57 @@
1
- import { Node } from './cst/Node.js';
2
- import { getLinePos, getPrettyContext } from './cst/source-utils.js';
3
- import { Range } from './cst/Range.js';
4
-
5
1
  class YAMLError extends Error {
6
- constructor(name, source, message) {
7
- if (!message || !(source instanceof Node)) throw new Error("Invalid arguments for new ".concat(name));
8
- super();
9
- this.name = name;
10
- this.message = message;
11
- this.source = source;
12
- }
13
-
14
- makePretty() {
15
- if (!this.source) return;
16
- this.nodeType = this.source.type;
17
- const cst = this.source.context && this.source.context.root;
18
-
19
- if (typeof this.offset === 'number') {
20
- this.range = new Range(this.offset, this.offset + 1);
21
- const start = cst && getLinePos(this.offset, cst);
22
-
23
- if (start) {
24
- const end = {
25
- line: start.line,
26
- col: start.col + 1
27
- };
28
- this.linePos = {
29
- start,
30
- end
31
- };
32
- }
33
-
34
- delete this.offset;
35
- } else {
36
- this.range = this.source.range;
37
- this.linePos = this.source.rangeAsLinePos;
2
+ constructor(name, pos, code, message) {
3
+ super();
4
+ this.name = name;
5
+ this.code = code;
6
+ this.message = message;
7
+ this.pos = pos;
38
8
  }
39
-
40
- if (this.linePos) {
41
- const {
42
- line,
43
- col
44
- } = this.linePos.start;
45
- this.message += " at line ".concat(line, ", column ").concat(col);
46
- const ctx = cst && getPrettyContext(this.linePos, cst);
47
- if (ctx) this.message += ":\n\n".concat(ctx, "\n");
48
- }
49
-
50
- delete this.source;
51
- }
52
-
53
- }
54
- class YAMLReferenceError extends YAMLError {
55
- constructor(source, message) {
56
- super('YAMLReferenceError', source, message);
57
- }
58
-
59
- }
60
- class YAMLSemanticError extends YAMLError {
61
- constructor(source, message) {
62
- super('YAMLSemanticError', source, message);
63
- }
64
-
65
9
  }
66
- class YAMLSyntaxError extends YAMLError {
67
- constructor(source, message) {
68
- super('YAMLSyntaxError', source, message);
69
- }
70
-
10
+ class YAMLParseError extends YAMLError {
11
+ constructor(pos, code, message) {
12
+ super('YAMLParseError', pos, code, message);
13
+ }
71
14
  }
72
15
  class YAMLWarning extends YAMLError {
73
- constructor(source, message) {
74
- super('YAMLWarning', source, message);
75
- }
76
-
16
+ constructor(pos, code, message) {
17
+ super('YAMLWarning', pos, code, message);
18
+ }
77
19
  }
20
+ const prettifyError = (src, lc) => (error) => {
21
+ if (error.pos[0] === -1)
22
+ return;
23
+ error.linePos = error.pos.map(pos => lc.linePos(pos));
24
+ const { line, col } = error.linePos[0];
25
+ error.message += ` at line ${line}, column ${col}`;
26
+ let ci = col - 1;
27
+ let lineStr = src
28
+ .substring(lc.lineStarts[line - 1], lc.lineStarts[line])
29
+ .replace(/[\n\r]+$/, '');
30
+ // Trim to max 80 chars, keeping col position near the middle
31
+ if (ci >= 60 && lineStr.length > 80) {
32
+ const trimStart = Math.min(ci - 39, lineStr.length - 79);
33
+ lineStr = '…' + lineStr.substring(trimStart);
34
+ ci -= trimStart - 1;
35
+ }
36
+ if (lineStr.length > 80)
37
+ lineStr = lineStr.substring(0, 79) + '…';
38
+ // Include previous line in context if pointing at line start
39
+ if (line > 1 && /^ *$/.test(lineStr.substring(0, ci))) {
40
+ // Regexp won't match if start is trimmed
41
+ let prev = src.substring(lc.lineStarts[line - 2], lc.lineStarts[line - 1]);
42
+ if (prev.length > 80)
43
+ prev = prev.substring(0, 79) + '…\n';
44
+ lineStr = prev + lineStr;
45
+ }
46
+ if (/[^ ]/.test(lineStr)) {
47
+ let count = 1;
48
+ const end = error.linePos[1];
49
+ if (end && end.line === line && end.col > col) {
50
+ count = Math.min(end.col - col, 80 - ci);
51
+ }
52
+ const pointer = ' '.repeat(ci) + '^'.repeat(count);
53
+ error.message += `:\n\n${lineStr}\n${pointer}\n`;
54
+ }
55
+ };
78
56
 
79
- export { YAMLError, YAMLReferenceError, YAMLSemanticError, YAMLSyntaxError, YAMLWarning };
57
+ export { YAMLError, YAMLParseError, YAMLWarning, prettifyError };
@@ -1,74 +1,18 @@
1
- import { LogLevel } from './constants.js';
2
- import { parse as parse$1 } from './cst/parse.js';
3
- export { parse as parseCST } from './cst/parse.js';
4
- import { Document } from './doc/Document.js';
1
+ export { Composer } from './compose/composer.js';
5
2
  export { Document } from './doc/Document.js';
6
- import { YAMLSemanticError } from './errors.js';
7
- import { warn } from './log.js';
8
- export { defaultOptions, scalarOptions } from './options.js';
9
- export { visit } from './ast/visit.js';
10
-
11
- function parseAllDocuments(src, options) {
12
- const stream = [];
13
- let prev;
14
-
15
- for (const cstDoc of parse$1(src)) {
16
- const doc = new Document(undefined, null, options);
17
- doc.parse(cstDoc, prev);
18
- stream.push(doc);
19
- prev = doc;
20
- }
21
-
22
- return stream;
23
- }
24
- function parseDocument(src, options) {
25
- const cst = parse$1(src);
26
- const doc = new Document(cst[0], null, options);
27
-
28
- if (cst.length > 1 && LogLevel.indexOf(doc.options.logLevel) >= LogLevel.ERROR) {
29
- const errMsg = 'Source contains multiple documents; please use YAML.parseAllDocuments()';
30
- doc.errors.unshift(new YAMLSemanticError(cst[1], errMsg));
31
- }
32
-
33
- return doc;
34
- }
35
- function parse(src, reviver, options) {
36
- if (options === undefined && reviver && typeof reviver === 'object') {
37
- options = reviver;
38
- reviver = undefined;
39
- }
40
-
41
- const doc = parseDocument(src, options);
42
- doc.warnings.forEach(warning => warn(doc.options.logLevel, warning));
43
-
44
- if (doc.errors.length > 0) {
45
- if (LogLevel.indexOf(doc.options.logLevel) >= LogLevel.ERROR) throw doc.errors[0];else doc.errors = [];
46
- }
47
-
48
- return doc.toJS({
49
- reviver
50
- });
51
- }
52
- function stringify(value, replacer, options) {
53
- if (typeof options === 'string') options = options.length;
54
-
55
- if (typeof options === 'number') {
56
- const indent = Math.round(options);
57
- options = indent < 1 ? undefined : indent > 8 ? {
58
- indent: 8
59
- } : {
60
- indent
61
- };
62
- }
63
-
64
- if (value === undefined) {
65
- const {
66
- keepUndefined
67
- } = options || replacer || {};
68
- if (!keepUndefined) return undefined;
69
- }
70
-
71
- return new Document(value, replacer, options).toString();
72
- }
73
-
74
- export { parse, parseAllDocuments, parseDocument, stringify };
3
+ export { Schema } from './schema/Schema.js';
4
+ export { YAMLError, YAMLParseError, YAMLWarning } from './errors.js';
5
+ export { Alias } from './nodes/Alias.js';
6
+ export { isAlias, isCollection, isDocument, isMap, isNode, isPair, isScalar, isSeq } from './nodes/Node.js';
7
+ export { Pair } from './nodes/Pair.js';
8
+ export { Scalar } from './nodes/Scalar.js';
9
+ export { YAMLMap } from './nodes/YAMLMap.js';
10
+ export { YAMLSeq } from './nodes/YAMLSeq.js';
11
+ export { defaultOptions } from './options.js';
12
+ import * as cst from './parse/cst.js';
13
+ export { cst as CST };
14
+ export { Lexer } from './parse/lexer.js';
15
+ export { LineCounter } from './parse/line-counter.js';
16
+ export { Parser } from './parse/parser.js';
17
+ export { parse, parseAllDocuments, parseDocument, stringify } from './public-api.js';
18
+ export { visit } from './visit.js';