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,77 @@
1
+ 'use strict';
2
+
3
+ var Node = require('../nodes/Node.js');
4
+ var visit = require('../visit.js');
5
+
6
+ /**
7
+ * Verify that the input string is a valid anchor.
8
+ *
9
+ * Will throw on errors.
10
+ */
11
+ function anchorIsValid(anchor) {
12
+ if (/[\x00-\x19\s,[\]{}]/.test(anchor)) {
13
+ const sa = JSON.stringify(anchor);
14
+ const msg = `Anchor must not contain whitespace or control characters: ${sa}`;
15
+ throw new Error(msg);
16
+ }
17
+ return true;
18
+ }
19
+ function anchorNames(root) {
20
+ const anchors = new Set();
21
+ visit.visit(root, {
22
+ Value(_key, node) {
23
+ if (node.anchor)
24
+ anchors.add(node.anchor);
25
+ }
26
+ });
27
+ return anchors;
28
+ }
29
+ /** Find a new anchor name with the given `prefix` and a one-indexed suffix. */
30
+ function findNewAnchor(prefix, exclude) {
31
+ for (let i = 1; true; ++i) {
32
+ const name = `${prefix}${i}`;
33
+ if (!exclude.has(name))
34
+ return name;
35
+ }
36
+ }
37
+ function createNodeAnchors(doc, prefix) {
38
+ const aliasObjects = [];
39
+ const sourceObjects = new Map();
40
+ let prevAnchors = null;
41
+ return {
42
+ onAnchor(source) {
43
+ aliasObjects.push(source);
44
+ if (!prevAnchors)
45
+ prevAnchors = anchorNames(doc);
46
+ const anchor = findNewAnchor(prefix, prevAnchors);
47
+ prevAnchors.add(anchor);
48
+ return anchor;
49
+ },
50
+ /**
51
+ * With circular references, the source node is only resolved after all
52
+ * of its child nodes are. This is why anchors are set only after all of
53
+ * the nodes have been created.
54
+ */
55
+ setAnchors() {
56
+ for (const source of aliasObjects) {
57
+ const ref = sourceObjects.get(source);
58
+ if (typeof ref === 'object' &&
59
+ ref.anchor &&
60
+ (Node.isScalar(ref.node) || Node.isCollection(ref.node))) {
61
+ ref.node.anchor = ref.anchor;
62
+ }
63
+ else {
64
+ const error = new Error('Failed to resolve repeated object (this should not happen)');
65
+ error.source = source;
66
+ throw error;
67
+ }
68
+ }
69
+ },
70
+ sourceObjects
71
+ };
72
+ }
73
+
74
+ exports.anchorIsValid = anchorIsValid;
75
+ exports.anchorNames = anchorNames;
76
+ exports.createNodeAnchors = createNodeAnchors;
77
+ exports.findNewAnchor = findNewAnchor;
@@ -0,0 +1,9 @@
1
+ export declare type Reviver = (key: unknown, value: unknown) => unknown;
2
+ /**
3
+ * Applies the JSON.parse reviver algorithm as defined in the ECMA-262 spec,
4
+ * in section 24.5.1.1 "Runtime Semantics: InternalizeJSONProperty" of the
5
+ * 2021 edition: https://tc39.es/ecma262/#sec-json.parse
6
+ *
7
+ * Includes extensions for handling Map and Set objects.
8
+ */
9
+ export declare function applyReviver(reviver: Reviver, obj: unknown, key: unknown, val: any): unknown;
@@ -8,36 +8,49 @@
8
8
  * Includes extensions for handling Map and Set objects.
9
9
  */
10
10
  function applyReviver(reviver, obj, key, val) {
11
- if (val && typeof val === 'object') {
12
- if (Array.isArray(val)) {
13
- for (let i = 0, len = val.length; i < len; ++i) {
14
- const v0 = val[i];
15
- const v1 = applyReviver(reviver, val, String(i), v0);
16
- if (v1 === undefined) delete val[i];else if (v1 !== v0) val[i] = v1;
17
- }
18
- } else if (val instanceof Map) {
19
- for (const k of Array.from(val.keys())) {
20
- const v0 = val.get(k);
21
- const v1 = applyReviver(reviver, val, k, v0);
22
- if (v1 === undefined) val.delete(k);else if (v1 !== v0) val.set(k, v1);
23
- }
24
- } else if (val instanceof Set) {
25
- for (const v0 of Array.from(val)) {
26
- const v1 = applyReviver(reviver, val, v0, v0);
27
- if (v1 === undefined) val.delete(v0);else if (v1 !== v0) {
28
- val.delete(v0);
29
- val.add(v1);
11
+ if (val && typeof val === 'object') {
12
+ if (Array.isArray(val)) {
13
+ for (let i = 0, len = val.length; i < len; ++i) {
14
+ const v0 = val[i];
15
+ const v1 = applyReviver(reviver, val, String(i), v0);
16
+ if (v1 === undefined)
17
+ delete val[i];
18
+ else if (v1 !== v0)
19
+ val[i] = v1;
20
+ }
21
+ }
22
+ else if (val instanceof Map) {
23
+ for (const k of Array.from(val.keys())) {
24
+ const v0 = val.get(k);
25
+ const v1 = applyReviver(reviver, val, k, v0);
26
+ if (v1 === undefined)
27
+ val.delete(k);
28
+ else if (v1 !== v0)
29
+ val.set(k, v1);
30
+ }
31
+ }
32
+ else if (val instanceof Set) {
33
+ for (const v0 of Array.from(val)) {
34
+ const v1 = applyReviver(reviver, val, v0, v0);
35
+ if (v1 === undefined)
36
+ val.delete(v0);
37
+ else if (v1 !== v0) {
38
+ val.delete(v0);
39
+ val.add(v1);
40
+ }
41
+ }
42
+ }
43
+ else {
44
+ for (const [k, v0] of Object.entries(val)) {
45
+ const v1 = applyReviver(reviver, val, k, v0);
46
+ if (v1 === undefined)
47
+ delete val[k];
48
+ else if (v1 !== v0)
49
+ val[k] = v1;
50
+ }
30
51
  }
31
- }
32
- } else {
33
- for (const [k, v0] of Object.entries(val)) {
34
- const v1 = applyReviver(reviver, val, k, v0);
35
- if (v1 === undefined) delete val[k];else if (v1 !== v0) val[k] = v1;
36
- }
37
52
  }
38
- }
39
-
40
- return reviver.call(obj, key, val);
53
+ return reviver.call(obj, key, val);
41
54
  }
42
55
 
43
56
  exports.applyReviver = applyReviver;
@@ -0,0 +1,16 @@
1
+ import { Node } from '../nodes/Node.js';
2
+ import type { Schema } from '../schema/Schema.js';
3
+ import type { CollectionTag, ScalarTag } from '../schema/types.js';
4
+ import type { Replacer } from './Document.js';
5
+ export interface CreateNodeContext {
6
+ keepUndefined?: boolean;
7
+ onAnchor(source: unknown): string;
8
+ onTagObj?: (tagObj: ScalarTag | CollectionTag) => void;
9
+ sourceObjects: Map<unknown, {
10
+ anchor: string | null;
11
+ node: Node | null;
12
+ }>;
13
+ replacer?: Replacer;
14
+ schema: Schema;
15
+ }
16
+ export declare function createNode(value: unknown, tagName: string | undefined, ctx: CreateNodeContext): Node;
@@ -1,64 +1,80 @@
1
1
  'use strict';
2
2
 
3
- var Node = require('../ast/Node.js');
4
- var Scalar = require('../ast/Scalar.js');
5
- var constants = require('../constants.js');
3
+ var Alias = require('../nodes/Alias.js');
4
+ var Node = require('../nodes/Node.js');
5
+ var Scalar = require('../nodes/Scalar.js');
6
6
 
7
+ const defaultTagPrefix = 'tag:yaml.org,2002:';
7
8
  function findTagObject(value, tagName, tags) {
8
- if (tagName) {
9
- const match = tags.filter(t => t.tag === tagName);
10
- const tagObj = match.find(t => !t.format) || match[0];
11
- if (!tagObj) throw new Error(`Tag ${tagName} not found`);
12
- return tagObj;
13
- }
14
-
15
- return tags.find(t => t.identify && t.identify(value) && !t.format);
9
+ if (tagName) {
10
+ const match = tags.filter(t => t.tag === tagName);
11
+ const tagObj = match.find(t => !t.format) || match[0];
12
+ if (!tagObj)
13
+ throw new Error(`Tag ${tagName} not found`);
14
+ return tagObj;
15
+ }
16
+ return tags.find(t => t.identify && t.identify(value) && !t.format);
16
17
  }
17
-
18
18
  function createNode(value, tagName, ctx) {
19
- if (value instanceof Node.Node) return value;
20
- const {
21
- onAlias,
22
- onTagObj,
23
- prevObjects,
24
- wrapScalars
25
- } = ctx;
26
- const {
27
- map,
28
- seq,
29
- tags
30
- } = ctx.schema;
31
- if (tagName && tagName.startsWith('!!')) tagName = constants.defaultTagPrefix + tagName.slice(2);
32
- let tagObj = findTagObject(value, tagName, tags);
33
-
34
- if (!tagObj) {
35
- if (typeof value.toJSON === 'function') value = value.toJSON();
36
- if (!value || typeof value !== 'object') return wrapScalars ? new Scalar.Scalar(value) : value;
37
- tagObj = value instanceof Map ? map : value[Symbol.iterator] ? seq : map;
38
- }
39
-
40
- if (onTagObj) {
41
- onTagObj(tagObj);
42
- delete ctx.onTagObj;
43
- } // Detect duplicate references to the same object & use Alias nodes for all
44
- // after first. The `obj` wrapper allows for circular references to resolve.
45
-
46
-
47
- const obj = {
48
- value: undefined,
49
- node: undefined
50
- };
51
-
52
- if (value && typeof value === 'object') {
53
- const prev = prevObjects.get(value);
54
- if (prev) return onAlias(prev);
55
- obj.value = value;
56
- prevObjects.set(value, obj);
57
- }
58
-
59
- obj.node = tagObj.createNode ? tagObj.createNode(ctx.schema, value, ctx) : wrapScalars ? new Scalar.Scalar(value) : value;
60
- if (tagName && obj.node instanceof Node.Node) obj.node.tag = tagName;
61
- return obj.node;
19
+ var _a, _b;
20
+ if (Node.isNode(value))
21
+ return value;
22
+ if (Node.isPair(value)) {
23
+ const map = (_b = (_a = ctx.schema[Node.MAP]).createNode) === null || _b === void 0 ? void 0 : _b.call(_a, ctx.schema, null, ctx);
24
+ map.items.push(value);
25
+ return map;
26
+ }
27
+ if (value instanceof String ||
28
+ value instanceof Number ||
29
+ value instanceof Boolean ||
30
+ (typeof BigInt === 'function' && value instanceof BigInt) // not supported everywhere
31
+ ) {
32
+ // https://tc39.es/ecma262/#sec-serializejsonproperty
33
+ value = value.valueOf();
34
+ }
35
+ const { onAnchor, onTagObj, schema, sourceObjects } = ctx;
36
+ // Detect duplicate references to the same object & use Alias nodes for all
37
+ // after first. The `ref` wrapper allows for circular references to resolve.
38
+ let ref = undefined;
39
+ if (value && typeof value === 'object') {
40
+ ref = sourceObjects.get(value);
41
+ if (ref) {
42
+ if (!ref.anchor)
43
+ ref.anchor = onAnchor(value);
44
+ return new Alias.Alias(ref.anchor);
45
+ }
46
+ else {
47
+ ref = { anchor: null, node: null };
48
+ sourceObjects.set(value, ref);
49
+ }
50
+ }
51
+ if (tagName && tagName.startsWith('!!'))
52
+ tagName = defaultTagPrefix + tagName.slice(2);
53
+ let tagObj = findTagObject(value, tagName, schema.tags);
54
+ if (!tagObj) {
55
+ if (value && typeof value.toJSON === 'function')
56
+ value = value.toJSON();
57
+ if (!value || typeof value !== 'object')
58
+ return new Scalar.Scalar(value);
59
+ tagObj =
60
+ value instanceof Map
61
+ ? schema[Node.MAP]
62
+ : Symbol.iterator in Object(value)
63
+ ? schema[Node.SEQ]
64
+ : schema[Node.MAP];
65
+ }
66
+ if (onTagObj) {
67
+ onTagObj(tagObj);
68
+ delete ctx.onTagObj;
69
+ }
70
+ const node = (tagObj === null || tagObj === void 0 ? void 0 : tagObj.createNode)
71
+ ? tagObj.createNode(ctx.schema, value, ctx)
72
+ : new Scalar.Scalar(value);
73
+ if (tagName)
74
+ node.tag = tagName;
75
+ if (ref)
76
+ ref.node = node;
77
+ return node;
62
78
  }
63
79
 
64
80
  exports.createNode = createNode;
@@ -0,0 +1,46 @@
1
+ import type { Document } from './Document.js';
2
+ export declare class Directives {
3
+ static defaultYaml: Directives['yaml'];
4
+ static defaultTags: Directives['tags'];
5
+ yaml: {
6
+ version: '1.1' | '1.2';
7
+ explicit?: boolean;
8
+ };
9
+ tags: Record<string, string>;
10
+ /**
11
+ * The directives-end/doc-start marker `---`. If `null`, a marker may still be
12
+ * included in the document's stringified representation.
13
+ */
14
+ marker: true | null;
15
+ /**
16
+ * Used when parsing YAML 1.1, where:
17
+ * > If the document specifies no directives, it is parsed using the same
18
+ * > settings as the previous document. If the document does specify any
19
+ * > directives, all directives of previous documents, if any, are ignored.
20
+ */
21
+ private atNextDocument?;
22
+ constructor(yaml?: Directives['yaml'], tags?: Directives['tags']);
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(): Directives;
28
+ /**
29
+ * @param onError - May be called even if the action was successful
30
+ * @returns `true` on success
31
+ */
32
+ add(line: string, onError: (offset: number, message: string, warning?: boolean) => void): boolean;
33
+ /**
34
+ * Resolves a tag, matching handles to those defined in %TAG directives.
35
+ *
36
+ * @returns Resolved tag, which may also be the non-specific tag `'!'` or a
37
+ * `'!local'` tag, or `null` if unresolvable.
38
+ */
39
+ tagName(source: string, onError: (message: string) => void): string | null;
40
+ /**
41
+ * Given a fully resolved tag, returns its printable string form,
42
+ * taking into account current tag prefixes and defaults.
43
+ */
44
+ tagString(tag: string): string;
45
+ toString(doc?: Document): string;
46
+ }
@@ -0,0 +1,163 @@
1
+ 'use strict';
2
+
3
+ var Node = require('../nodes/Node.js');
4
+ var visit = require('../visit.js');
5
+
6
+ const escapeChars = {
7
+ '!': '%21',
8
+ ',': '%2C',
9
+ '[': '%5B',
10
+ ']': '%5D',
11
+ '{': '%7B',
12
+ '}': '%7D'
13
+ };
14
+ const escapeTagName = (tn) => tn.replace(/[!,[\]{}]/g, ch => escapeChars[ch]);
15
+ class Directives {
16
+ constructor(yaml, tags) {
17
+ /**
18
+ * The directives-end/doc-start marker `---`. If `null`, a marker may still be
19
+ * included in the document's stringified representation.
20
+ */
21
+ this.marker = null;
22
+ this.yaml = Object.assign({}, Directives.defaultYaml, yaml);
23
+ this.tags = Object.assign({}, Directives.defaultTags, tags);
24
+ }
25
+ /**
26
+ * During parsing, get a Directives instance for the current document and
27
+ * update the stream state according to the current version's spec.
28
+ */
29
+ atDocument() {
30
+ const res = new Directives(this.yaml, this.tags);
31
+ switch (this.yaml.version) {
32
+ case '1.1':
33
+ this.atNextDocument = true;
34
+ break;
35
+ case '1.2':
36
+ this.atNextDocument = false;
37
+ this.yaml = {
38
+ explicit: Directives.defaultYaml.explicit,
39
+ version: '1.2'
40
+ };
41
+ this.tags = Object.assign({}, Directives.defaultTags);
42
+ break;
43
+ }
44
+ return res;
45
+ }
46
+ /**
47
+ * @param onError - May be called even if the action was successful
48
+ * @returns `true` on success
49
+ */
50
+ add(line, onError) {
51
+ if (this.atNextDocument) {
52
+ this.yaml = { explicit: Directives.defaultYaml.explicit, version: '1.1' };
53
+ this.tags = Object.assign({}, Directives.defaultTags);
54
+ this.atNextDocument = false;
55
+ }
56
+ const parts = line.trim().split(/[ \t]+/);
57
+ const name = parts.shift();
58
+ switch (name) {
59
+ case '%TAG': {
60
+ if (parts.length !== 2) {
61
+ onError(0, '%TAG directive should contain exactly two parts');
62
+ if (parts.length < 2)
63
+ return false;
64
+ }
65
+ const [handle, prefix] = parts;
66
+ this.tags[handle] = prefix;
67
+ return true;
68
+ }
69
+ case '%YAML': {
70
+ this.yaml.explicit = true;
71
+ if (parts.length < 1) {
72
+ onError(0, '%YAML directive should contain exactly one part');
73
+ return false;
74
+ }
75
+ const [version] = parts;
76
+ if (version === '1.1' || version === '1.2') {
77
+ this.yaml.version = version;
78
+ return true;
79
+ }
80
+ else {
81
+ onError(6, `Unsupported YAML version ${version}`, true);
82
+ return false;
83
+ }
84
+ }
85
+ default:
86
+ onError(0, `Unknown directive ${name}`, true);
87
+ return false;
88
+ }
89
+ }
90
+ /**
91
+ * Resolves a tag, matching handles to those defined in %TAG directives.
92
+ *
93
+ * @returns Resolved tag, which may also be the non-specific tag `'!'` or a
94
+ * `'!local'` tag, or `null` if unresolvable.
95
+ */
96
+ tagName(source, onError) {
97
+ if (source === '!')
98
+ return '!'; // non-specific tag
99
+ if (source[0] !== '!') {
100
+ onError(`Not a valid tag: ${source}`);
101
+ return null;
102
+ }
103
+ if (source[1] === '<') {
104
+ const verbatim = source.slice(2, -1);
105
+ if (verbatim === '!' || verbatim === '!!') {
106
+ onError(`Verbatim tags aren't resolved, so ${source} is invalid.`);
107
+ return null;
108
+ }
109
+ if (source[source.length - 1] !== '>')
110
+ onError('Verbatim tags must end with a >');
111
+ return verbatim;
112
+ }
113
+ const [, handle, suffix] = source.match(/^(.*!)([^!]*)$/);
114
+ if (!suffix)
115
+ onError(`The ${source} tag has no suffix`);
116
+ const prefix = this.tags[handle];
117
+ if (prefix)
118
+ return prefix + decodeURIComponent(suffix);
119
+ if (handle === '!')
120
+ return source; // local tag
121
+ onError(`Could not resolve tag: ${source}`);
122
+ return null;
123
+ }
124
+ /**
125
+ * Given a fully resolved tag, returns its printable string form,
126
+ * taking into account current tag prefixes and defaults.
127
+ */
128
+ tagString(tag) {
129
+ for (const [handle, prefix] of Object.entries(this.tags)) {
130
+ if (tag.startsWith(prefix))
131
+ return handle + escapeTagName(tag.substring(prefix.length));
132
+ }
133
+ return tag[0] === '!' ? tag : `!<${tag}>`;
134
+ }
135
+ toString(doc) {
136
+ const lines = this.yaml.explicit
137
+ ? [`%YAML ${this.yaml.version || '1.2'}`]
138
+ : [];
139
+ const tagEntries = Object.entries(this.tags);
140
+ let tagNames;
141
+ if (doc && tagEntries.length > 0 && Node.isNode(doc.contents)) {
142
+ const tags = {};
143
+ visit.visit(doc.contents, (_key, node) => {
144
+ if (Node.isNode(node) && node.tag)
145
+ tags[node.tag] = true;
146
+ });
147
+ tagNames = Object.keys(tags);
148
+ }
149
+ else
150
+ tagNames = [];
151
+ for (const [handle, prefix] of tagEntries) {
152
+ if (handle === '!!' && prefix === 'tag:yaml.org,2002:')
153
+ continue;
154
+ if (!doc || tagNames.some(tn => tn.startsWith(prefix)))
155
+ lines.push(`%TAG ${handle} ${prefix}`);
156
+ }
157
+ return lines.join('\n');
158
+ }
159
+ }
160
+ Directives.defaultYaml = { explicit: false, version: '1.2' };
161
+ Directives.defaultTags = { '!!': 'tag:yaml.org,2002:' };
162
+
163
+ exports.Directives = Directives;
@@ -0,0 +1,21 @@
1
+ import type { LineCounter } from './parse/line-counter';
2
+ export declare type ErrorCode = 'ALIAS_PROPS' | '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_ANCHOR' | 'MISSING_CHAR' | 'MULTILINE_IMPLICIT_KEY' | 'MULTIPLE_ANCHORS' | 'MULTIPLE_DOCS' | 'MULTIPLE_TAGS' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN';
3
+ export declare type LinePos = {
4
+ line: number;
5
+ col: number;
6
+ };
7
+ export declare class YAMLError extends Error {
8
+ name: 'YAMLParseError' | 'YAMLWarning';
9
+ code: ErrorCode;
10
+ message: string;
11
+ pos: [number, number];
12
+ linePos?: [LinePos] | [LinePos, LinePos];
13
+ constructor(name: YAMLError['name'], pos: [number, number], code: ErrorCode, message: string);
14
+ }
15
+ export declare class YAMLParseError extends YAMLError {
16
+ constructor(pos: [number, number], code: ErrorCode, message: string);
17
+ }
18
+ export declare class YAMLWarning extends YAMLError {
19
+ constructor(pos: [number, number], code: ErrorCode, message: string);
20
+ }
21
+ export declare const prettifyError: (src: string, lc: LineCounter) => (error: YAMLError) => void;