yaml 2.0.0-3 → 2.0.0-7

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 (345) 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 +79 -0
  6. package/browser/dist/compose/compose-scalar.js +74 -0
  7. package/browser/dist/compose/composer.js +219 -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 +217 -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 +75 -55
  22. package/browser/dist/doc/directives.js +161 -0
  23. package/browser/dist/errors.js +50 -72
  24. package/browser/dist/index.js +16 -72
  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 +674 -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 +134 -71
  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 +82 -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 +223 -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 +219 -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 +75 -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 +39 -70
  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 +676 -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 +134 -71
  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/constants.js +0 -40
  233. package/browser/dist/cst/Alias.js +0 -26
  234. package/browser/dist/cst/BlankLine.js +0 -34
  235. package/browser/dist/cst/BlockValue.js +0 -240
  236. package/browser/dist/cst/Collection.js +0 -259
  237. package/browser/dist/cst/CollectionItem.js +0 -123
  238. package/browser/dist/cst/Comment.js +0 -27
  239. package/browser/dist/cst/Directive.js +0 -53
  240. package/browser/dist/cst/Document.js +0 -262
  241. package/browser/dist/cst/FlowCollection.js +0 -201
  242. package/browser/dist/cst/Node.js +0 -355
  243. package/browser/dist/cst/ParseContext.js +0 -234
  244. package/browser/dist/cst/PlainValue.js +0 -171
  245. package/browser/dist/cst/QuoteDouble.js +0 -224
  246. package/browser/dist/cst/QuoteSingle.js +0 -100
  247. package/browser/dist/cst/Range.js +0 -56
  248. package/browser/dist/cst/parse.js +0 -46
  249. package/browser/dist/cst/source-utils.js +0 -169
  250. package/browser/dist/doc/Anchors.js +0 -115
  251. package/browser/dist/doc/Schema.js +0 -34
  252. package/browser/dist/doc/getSchemaTags.js +0 -33
  253. package/browser/dist/doc/listTagNames.js +0 -27
  254. package/browser/dist/doc/parseContents.js +0 -60
  255. package/browser/dist/doc/parseDirectives.js +0 -107
  256. package/browser/dist/resolve/collection-utils.js +0 -86
  257. package/browser/dist/resolve/resolveMap.js +0 -315
  258. package/browser/dist/resolve/resolveNode.js +0 -173
  259. package/browser/dist/resolve/resolveScalar.js +0 -20
  260. package/browser/dist/resolve/resolveSeq.js +0 -170
  261. package/browser/dist/resolve/resolveTag.js +0 -107
  262. package/browser/dist/resolve/resolveTagName.js +0 -87
  263. package/browser/dist/stringify/addComment.js +0 -10
  264. package/browser/dist/stringify/stringifyTag.js +0 -28
  265. package/browser/dist/tags/core.js +0 -128
  266. package/browser/dist/tags/failsafe/index.js +0 -7
  267. package/browser/dist/tags/failsafe/map.js +0 -37
  268. package/browser/dist/tags/failsafe/seq.js +0 -34
  269. package/browser/dist/tags/failsafe/string.js +0 -20
  270. package/browser/dist/tags/index.js +0 -39
  271. package/browser/dist/tags/json.js +0 -62
  272. package/browser/dist/tags/options.js +0 -31
  273. package/browser/dist/tags/yaml-1.1/binary.js +0 -83
  274. package/browser/dist/tags/yaml-1.1/index.js +0 -174
  275. package/browser/dist/tags/yaml-1.1/omap.js +0 -86
  276. package/browser/dist/tags/yaml-1.1/pairs.js +0 -62
  277. package/browser/dist/tags/yaml-1.1/set.js +0 -78
  278. package/browser/dist/tags/yaml-1.1/timestamp.js +0 -101
  279. package/browser/dist/types.js +0 -10
  280. package/cst.d.ts +0 -185
  281. package/dist/_virtual/_rollupPluginBabelHelpers.js +0 -18
  282. package/dist/ast/Alias.js +0 -97
  283. package/dist/ast/Collection.js +0 -196
  284. package/dist/ast/Merge.js +0 -74
  285. package/dist/ast/Node.js +0 -5
  286. package/dist/ast/Pair.js +0 -203
  287. package/dist/ast/Scalar.js +0 -24
  288. package/dist/ast/YAMLMap.js +0 -94
  289. package/dist/ast/YAMLSeq.js +0 -69
  290. package/dist/ast/toJS.js +0 -31
  291. package/dist/constants.js +0 -46
  292. package/dist/cst/Alias.js +0 -28
  293. package/dist/cst/BlankLine.js +0 -36
  294. package/dist/cst/BlockValue.js +0 -243
  295. package/dist/cst/Collection.js +0 -262
  296. package/dist/cst/CollectionItem.js +0 -125
  297. package/dist/cst/Comment.js +0 -29
  298. package/dist/cst/Directive.js +0 -55
  299. package/dist/cst/Document.js +0 -264
  300. package/dist/cst/FlowCollection.js +0 -203
  301. package/dist/cst/Node.js +0 -357
  302. package/dist/cst/ParseContext.js +0 -236
  303. package/dist/cst/PlainValue.js +0 -173
  304. package/dist/cst/QuoteDouble.js +0 -226
  305. package/dist/cst/QuoteSingle.js +0 -102
  306. package/dist/cst/Range.js +0 -58
  307. package/dist/cst/parse.js +0 -48
  308. package/dist/cst/source-utils.js +0 -173
  309. package/dist/doc/Anchors.js +0 -117
  310. package/dist/doc/Schema.js +0 -36
  311. package/dist/doc/getSchemaTags.js +0 -35
  312. package/dist/doc/listTagNames.js +0 -29
  313. package/dist/doc/parseContents.js +0 -62
  314. package/dist/doc/parseDirectives.js +0 -109
  315. package/dist/resolve/collection-utils.js +0 -91
  316. package/dist/resolve/resolveMap.js +0 -317
  317. package/dist/resolve/resolveNode.js +0 -175
  318. package/dist/resolve/resolveScalar.js +0 -22
  319. package/dist/resolve/resolveSeq.js +0 -172
  320. package/dist/resolve/resolveTag.js +0 -109
  321. package/dist/resolve/resolveTagName.js +0 -89
  322. package/dist/stringify/addComment.js +0 -13
  323. package/dist/stringify/stringifyTag.js +0 -30
  324. package/dist/tags/core.js +0 -138
  325. package/dist/tags/failsafe/index.js +0 -9
  326. package/dist/tags/failsafe/map.js +0 -39
  327. package/dist/tags/failsafe/seq.js +0 -36
  328. package/dist/tags/failsafe/string.js +0 -22
  329. package/dist/tags/index.js +0 -42
  330. package/dist/tags/json.js +0 -64
  331. package/dist/tags/options.js +0 -37
  332. package/dist/tags/yaml-1.1/binary.js +0 -85
  333. package/dist/tags/yaml-1.1/index.js +0 -176
  334. package/dist/tags/yaml-1.1/omap.js +0 -89
  335. package/dist/tags/yaml-1.1/pairs.js +0 -66
  336. package/dist/tags/yaml-1.1/set.js +0 -81
  337. package/dist/tags/yaml-1.1/timestamp.js +0 -105
  338. package/dist/types.js +0 -29
  339. package/index.d.ts +0 -519
  340. package/index.js +0 -1
  341. package/types.d.ts +0 -379
  342. package/types.js +0 -17
  343. package/types.mjs +0 -17
  344. package/util.d.ts +0 -76
  345. package/util.mjs +0 -15
@@ -0,0 +1,35 @@
1
+ import { hasAnchor } from './Node.js';
2
+
3
+ /**
4
+ * Recursively convert any node or its contents to native JavaScript
5
+ *
6
+ * @param value - The input value
7
+ * @param arg - If `value` defines a `toJSON()` method, use this
8
+ * as its first argument
9
+ * @param ctx - Conversion context, originally set in Document#toJS(). If
10
+ * `{ keep: true }` is not set, output should be suitable for JSON
11
+ * stringification.
12
+ */
13
+ function toJS(value, arg, ctx) {
14
+ if (Array.isArray(value))
15
+ return value.map((v, i) => toJS(v, String(i), ctx));
16
+ if (value && typeof value.toJSON === 'function') {
17
+ if (!ctx || !hasAnchor(value))
18
+ return value.toJSON(arg, ctx);
19
+ const data = { aliasCount: 0, count: 1, res: undefined };
20
+ ctx.anchors.set(value, data);
21
+ ctx.onCreate = res => {
22
+ data.res = res;
23
+ delete ctx.onCreate;
24
+ };
25
+ const res = value.toJSON(arg, ctx);
26
+ if (ctx.onCreate)
27
+ ctx.onCreate(res);
28
+ return res;
29
+ }
30
+ if (typeof value === 'bigint' && !(ctx && ctx.keep))
31
+ return Number(value);
32
+ return value;
33
+ }
34
+
35
+ export { toJS };
@@ -1,98 +1,17 @@
1
- import { defaultTagPrefix } from './constants.js';
2
- import { binaryOptions, boolOptions, intOptions, nullOptions, strOptions } from './tags/options.js';
3
-
1
+ /**
2
+ * `yaml` defines document-specific options in three places: as an argument of
3
+ * parse, create and stringify calls, in the values of `YAML.defaultOptions`,
4
+ * and in the version-dependent `YAML.Document.defaults` object. Values set in
5
+ * `YAML.defaultOptions` override version-dependent defaults, and argument
6
+ * options override both.
7
+ */
4
8
  const defaultOptions = {
5
- anchorPrefix: 'a',
6
- customTags: null,
7
- indent: 2,
8
- indentSeq: true,
9
- keepCstNodes: false,
10
- keepNodeTypes: true,
11
- keepUndefined: false,
12
- logLevel: 'warn',
13
- mapAsMap: false,
14
- maxAliasCount: 100,
15
- prettyErrors: true,
16
- simpleKeys: false,
17
- version: '1.2'
18
- };
19
- const scalarOptions = {
20
- get binary() {
21
- return binaryOptions;
22
- },
23
-
24
- set binary(opt) {
25
- Object.assign(binaryOptions, opt);
26
- },
27
-
28
- get bool() {
29
- return boolOptions;
30
- },
31
-
32
- set bool(opt) {
33
- Object.assign(boolOptions, opt);
34
- },
35
-
36
- get int() {
37
- return intOptions;
38
- },
39
-
40
- set int(opt) {
41
- Object.assign(intOptions, opt);
42
- },
43
-
44
- get null() {
45
- return nullOptions;
46
- },
47
-
48
- set null(opt) {
49
- Object.assign(nullOptions, opt);
50
- },
51
-
52
- get str() {
53
- return strOptions;
54
- },
55
-
56
- set str(opt) {
57
- Object.assign(strOptions, opt);
58
- }
59
-
60
- };
61
- const documentOptions = {
62
- '1.0': {
63
- schema: 'yaml-1.1',
64
- merge: true,
65
- tagPrefixes: [{
66
- handle: '!',
67
- prefix: defaultTagPrefix
68
- }, {
69
- handle: '!!',
70
- prefix: 'tag:private.yaml.org,2002:'
71
- }]
72
- },
73
- 1.1: {
74
- schema: 'yaml-1.1',
75
- merge: true,
76
- tagPrefixes: [{
77
- handle: '!',
78
- prefix: '!'
79
- }, {
80
- handle: '!!',
81
- prefix: defaultTagPrefix
82
- }]
83
- },
84
- 1.2: {
85
- schema: 'core',
86
- merge: false,
87
- resolveKnownTags: true,
88
- tagPrefixes: [{
89
- handle: '!',
90
- prefix: '!'
91
- }, {
92
- handle: '!!',
93
- prefix: defaultTagPrefix
94
- }]
95
- }
9
+ intAsBigInt: false,
10
+ logLevel: 'warn',
11
+ prettyErrors: true,
12
+ strict: true,
13
+ uniqueKeys: true,
14
+ version: '1.2'
96
15
  };
97
16
 
98
- export { defaultOptions, documentOptions, scalarOptions };
17
+ export { defaultOptions };
@@ -0,0 +1,219 @@
1
+ import { resolveBlockScalar } from '../compose/resolve-block-scalar.js';
2
+ import { resolveFlowScalar } from '../compose/resolve-flow-scalar.js';
3
+ import { YAMLParseError } from '../errors.js';
4
+ import { stringifyString } from '../stringify/stringifyString.js';
5
+
6
+ /**
7
+ * If `token` is a CST flow or block scalar, determine its string value and a few other attributes.
8
+ * Otherwise, return `null`.
9
+ */
10
+ function resolveAsScalar(token, strict = true, onError) {
11
+ if (token) {
12
+ const _onError = (pos, code, message) => {
13
+ const offset = typeof pos === 'number' ? pos : Array.isArray(pos) ? pos[0] : pos.offset;
14
+ if (onError)
15
+ onError(offset, code, message);
16
+ else
17
+ throw new YAMLParseError([offset, offset + 1], code, message);
18
+ };
19
+ switch (token.type) {
20
+ case 'scalar':
21
+ case 'single-quoted-scalar':
22
+ case 'double-quoted-scalar':
23
+ return resolveFlowScalar(token, strict, _onError);
24
+ case 'block-scalar':
25
+ return resolveBlockScalar(token, strict, _onError);
26
+ }
27
+ }
28
+ return null;
29
+ }
30
+ /**
31
+ * Create a new scalar token with `value`
32
+ *
33
+ * Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
34
+ * as this function does not support any schema operations and won't check for such conflicts.
35
+ *
36
+ * @param value The string representation of the value, which will have its content properly indented.
37
+ * @param context.end Comments and whitespace after the end of the value, or after the block scalar header. If undefined, a newline will be added.
38
+ * @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
39
+ * @param context.indent The indent level of the token.
40
+ * @param context.inFlow Is this scalar within a flow collection? This may affect the resolved type of the token's value.
41
+ * @param context.offset The offset position of the token.
42
+ * @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
43
+ */
44
+ function createScalarToken(value, context) {
45
+ var _a;
46
+ const { implicitKey = false, indent, inFlow = false, offset = -1, type = 'PLAIN' } = context;
47
+ const source = stringifyString({ type, value }, {
48
+ implicitKey,
49
+ indent: indent > 0 ? ' '.repeat(indent) : '',
50
+ inFlow,
51
+ options: { lineWidth: -1 }
52
+ });
53
+ const end = (_a = context.end) !== null && _a !== void 0 ? _a : [
54
+ { type: 'newline', offset: -1, indent, source: '\n' }
55
+ ];
56
+ switch (source[0]) {
57
+ case '|':
58
+ case '>': {
59
+ const he = source.indexOf('\n');
60
+ const head = source.substring(0, he);
61
+ const body = source.substring(he + 1) + '\n';
62
+ const props = [
63
+ { type: 'block-scalar-header', offset, indent, source: head }
64
+ ];
65
+ if (!addEndtoBlockProps(props, end))
66
+ props.push({ type: 'newline', offset: -1, indent, source: '\n' });
67
+ return { type: 'block-scalar', offset, indent, props, source: body };
68
+ }
69
+ case '"':
70
+ return { type: 'double-quoted-scalar', offset, indent, source, end };
71
+ case "'":
72
+ return { type: 'single-quoted-scalar', offset, indent, source, end };
73
+ default:
74
+ return { type: 'scalar', offset, indent, source, end };
75
+ }
76
+ }
77
+ /**
78
+ * Set the value of `token` to the given string `value`, overwriting any previous contents and type that it may have.
79
+ *
80
+ * Best efforts are made to retain any comments previously associated with the `token`,
81
+ * though all contents within a collection's `items` will be overwritten.
82
+ *
83
+ * Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
84
+ * as this function does not support any schema operations and won't check for such conflicts.
85
+ *
86
+ * @param token Any token. If it does not include an `indent` value, the value will be stringified as if it were an implicit key.
87
+ * @param value The string representation of the value, which will have its content properly indented.
88
+ * @param context.afterKey In most cases, values after a key should have an additional level of indentation.
89
+ * @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
90
+ * @param context.inFlow Being within a flow collection may affect the resolved type of the token's value.
91
+ * @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
92
+ */
93
+ function setScalarValue(token, value, context = {}) {
94
+ let { afterKey = false, implicitKey = false, inFlow = false, type } = context;
95
+ let indent = 'indent' in token ? token.indent : null;
96
+ if (afterKey && typeof indent === 'number')
97
+ indent += 2;
98
+ if (!type)
99
+ switch (token.type) {
100
+ case 'single-quoted-scalar':
101
+ type = 'QUOTE_SINGLE';
102
+ break;
103
+ case 'double-quoted-scalar':
104
+ type = 'QUOTE_DOUBLE';
105
+ break;
106
+ case 'block-scalar': {
107
+ const header = token.props[0];
108
+ if (header.type !== 'block-scalar-header')
109
+ throw new Error('Invalid block scalar header');
110
+ type = header.source[0] === '>' ? 'BLOCK_FOLDED' : 'BLOCK_LITERAL';
111
+ break;
112
+ }
113
+ default:
114
+ type = 'PLAIN';
115
+ }
116
+ const source = stringifyString({ type, value }, {
117
+ implicitKey: implicitKey || indent === null,
118
+ indent: indent !== null && indent > 0 ? ' '.repeat(indent) : '',
119
+ inFlow,
120
+ options: { lineWidth: -1 }
121
+ });
122
+ switch (source[0]) {
123
+ case '|':
124
+ case '>':
125
+ setBlockScalarValue(token, source);
126
+ break;
127
+ case '"':
128
+ setFlowScalarValue(token, source, 'double-quoted-scalar');
129
+ break;
130
+ case "'":
131
+ setFlowScalarValue(token, source, 'single-quoted-scalar');
132
+ break;
133
+ default:
134
+ setFlowScalarValue(token, source, 'scalar');
135
+ }
136
+ }
137
+ function setBlockScalarValue(token, source) {
138
+ const he = source.indexOf('\n');
139
+ const head = source.substring(0, he);
140
+ const body = source.substring(he + 1) + '\n';
141
+ if (token.type === 'block-scalar') {
142
+ const header = token.props[0];
143
+ if (header.type !== 'block-scalar-header')
144
+ throw new Error('Invalid block scalar header');
145
+ header.source = head;
146
+ token.source = body;
147
+ }
148
+ else {
149
+ const { offset } = token;
150
+ const indent = 'indent' in token ? token.indent : -1;
151
+ const props = [
152
+ { type: 'block-scalar-header', offset, indent, source: head }
153
+ ];
154
+ if (!addEndtoBlockProps(props, 'end' in token ? token.end : undefined))
155
+ props.push({ type: 'newline', offset: -1, indent, source: '\n' });
156
+ for (const key of Object.keys(token))
157
+ if (key !== 'type' && key !== 'offset')
158
+ delete token[key];
159
+ Object.assign(token, { type: 'block-scalar', indent, props, source: body });
160
+ }
161
+ }
162
+ /** @returns `true` if last token is a newline */
163
+ function addEndtoBlockProps(props, end) {
164
+ if (end)
165
+ for (const st of end)
166
+ switch (st.type) {
167
+ case 'space':
168
+ case 'comment':
169
+ props.push(st);
170
+ break;
171
+ case 'newline':
172
+ props.push(st);
173
+ return true;
174
+ }
175
+ return false;
176
+ }
177
+ function setFlowScalarValue(token, source, type) {
178
+ switch (token.type) {
179
+ case 'scalar':
180
+ case 'double-quoted-scalar':
181
+ case 'single-quoted-scalar':
182
+ token.type = type;
183
+ token.source = source;
184
+ break;
185
+ case 'block-scalar': {
186
+ const end = token.props.slice(1);
187
+ let oa = source.length;
188
+ if (token.props[0].type === 'block-scalar-header')
189
+ oa -= token.props[0].source.length;
190
+ for (const tok of end)
191
+ tok.offset += oa;
192
+ delete token.props;
193
+ Object.assign(token, { type, source, end });
194
+ break;
195
+ }
196
+ case 'block-map':
197
+ case 'block-seq': {
198
+ const offset = token.offset + source.length;
199
+ const nl = { type: 'newline', offset, indent: token.indent, source: '\n' };
200
+ delete token.items;
201
+ Object.assign(token, { type, source, end: [nl] });
202
+ break;
203
+ }
204
+ default: {
205
+ const indent = 'indent' in token ? token.indent : -1;
206
+ const end = 'end' in token && Array.isArray(token.end)
207
+ ? token.end.filter(st => st.type === 'space' ||
208
+ st.type === 'comment' ||
209
+ st.type === 'newline')
210
+ : [];
211
+ for (const key of Object.keys(token))
212
+ if (key !== 'type' && key !== 'offset')
213
+ delete token[key];
214
+ Object.assign(token, { type, indent, source, end });
215
+ }
216
+ }
217
+ }
218
+
219
+ export { createScalarToken, resolveAsScalar, setScalarValue };
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Stringify a CST document, token, or collection item
3
+ *
4
+ * Fair warning: This applies no validation whatsoever, and
5
+ * simply concatenates the sources in their logical order.
6
+ */
7
+ const stringify = (cst) => 'type' in cst ? stringifyToken(cst) : stringifyItem(cst);
8
+ function stringifyToken(token) {
9
+ switch (token.type) {
10
+ case 'block-scalar': {
11
+ let res = '';
12
+ for (const tok of token.props)
13
+ res += stringifyToken(tok);
14
+ return res + token.source;
15
+ }
16
+ case 'block-map':
17
+ case 'block-seq': {
18
+ let res = '';
19
+ for (const item of token.items)
20
+ res += stringifyItem(item);
21
+ return res;
22
+ }
23
+ case 'flow-collection': {
24
+ let res = token.start.source;
25
+ for (const item of token.items)
26
+ res += stringifyItem(item);
27
+ for (const st of token.end)
28
+ res += st.source;
29
+ return res;
30
+ }
31
+ case 'document': {
32
+ let res = stringifyItem(token);
33
+ if (token.end)
34
+ for (const st of token.end)
35
+ res += st.source;
36
+ return res;
37
+ }
38
+ default: {
39
+ let res = token.source;
40
+ if ('end' in token && token.end)
41
+ for (const st of token.end)
42
+ res += st.source;
43
+ return res;
44
+ }
45
+ }
46
+ }
47
+ function stringifyItem({ start, key, sep, value }) {
48
+ let res = '';
49
+ for (const st of start)
50
+ res += st.source;
51
+ if (key)
52
+ res += stringifyToken(key);
53
+ if (sep)
54
+ for (const st of sep)
55
+ res += st.source;
56
+ if (value)
57
+ res += stringifyToken(value);
58
+ return res;
59
+ }
60
+
61
+ export { stringify };
@@ -0,0 +1,97 @@
1
+ const BREAK = Symbol('break visit');
2
+ const SKIP = Symbol('skip children');
3
+ const REMOVE = Symbol('remove item');
4
+ /**
5
+ * Apply a visitor to a CST document or item.
6
+ *
7
+ * Walks through the tree (depth-first) starting from the root, calling a
8
+ * `visitor` function with two arguments when entering each item:
9
+ * - `item`: The current item, which included the following members:
10
+ * - `start: SourceToken[]` – Source tokens before the key or value,
11
+ * possibly including its anchor or tag.
12
+ * - `key?: Token | null` – Set for pair values. May then be `null`, if
13
+ * the key before the `:` separator is empty.
14
+ * - `sep?: SourceToken[]` – Source tokens between the key and the value,
15
+ * which should include the `:` map value indicator if `value` is set.
16
+ * - `value?: Token` – The value of a sequence item, or of a map pair.
17
+ * - `path`: The steps from the root to the current node, as an array of
18
+ * `['key' | 'value', number]` tuples.
19
+ *
20
+ * The return value of the visitor may be used to control the traversal:
21
+ * - `undefined` (default): Do nothing and continue
22
+ * - `visit.SKIP`: Do not visit the children of this token, continue with
23
+ * next sibling
24
+ * - `visit.BREAK`: Terminate traversal completely
25
+ * - `visit.REMOVE`: Remove the current item, then continue with the next one
26
+ * - `number`: Set the index of the next step. This is useful especially if
27
+ * the index of the current token has changed.
28
+ * - `function`: Define the next visitor for this item. After the original
29
+ * visitor is called on item entry, next visitors are called after handling
30
+ * a non-empty `key` and when exiting the item.
31
+ */
32
+ function visit(cst, visitor) {
33
+ if ('type' in cst && cst.type === 'document')
34
+ cst = { start: cst.start, value: cst.value };
35
+ _visit(Object.freeze([]), cst, visitor);
36
+ }
37
+ // Without the `as symbol` casts, TS declares these in the `visit`
38
+ // namespace using `var`, but then complains about that because
39
+ // `unique symbol` must be `const`.
40
+ /** Terminate visit traversal completely */
41
+ visit.BREAK = BREAK;
42
+ /** Do not visit the children of the current item */
43
+ visit.SKIP = SKIP;
44
+ /** Remove the current item */
45
+ visit.REMOVE = REMOVE;
46
+ /** Find the item at `path` from `cst` as the root */
47
+ visit.itemAtPath = (cst, path) => {
48
+ let item = cst;
49
+ for (const [field, index] of path) {
50
+ const tok = item && item[field];
51
+ if (tok && 'items' in tok) {
52
+ item = tok.items[index];
53
+ }
54
+ else
55
+ return undefined;
56
+ }
57
+ return item;
58
+ };
59
+ /**
60
+ * Get the immediate parent collection of the item at `path` from `cst` as the root.
61
+ *
62
+ * Throws an error if the collection is not found, which should never happen if the item itself exists.
63
+ */
64
+ visit.parentCollection = (cst, path) => {
65
+ const parent = visit.itemAtPath(cst, path.slice(0, -1));
66
+ const field = path[path.length - 1][0];
67
+ const coll = parent && parent[field];
68
+ if (coll && 'items' in coll)
69
+ return coll;
70
+ throw new Error('Parent collection not found');
71
+ };
72
+ function _visit(path, item, visitor) {
73
+ let ctrl = visitor(item, path);
74
+ if (typeof ctrl === 'symbol')
75
+ return ctrl;
76
+ for (const field of ['key', 'value']) {
77
+ const token = item[field];
78
+ if (token && 'items' in token) {
79
+ for (let i = 0; i < token.items.length; ++i) {
80
+ const ci = _visit(Object.freeze(path.concat([[field, i]])), token.items[i], visitor);
81
+ if (typeof ci === 'number')
82
+ i = ci - 1;
83
+ else if (ci === BREAK)
84
+ return BREAK;
85
+ else if (ci === REMOVE) {
86
+ token.items.splice(i, 1);
87
+ i -= 1;
88
+ }
89
+ }
90
+ if (typeof ctrl === 'function' && field === 'key')
91
+ ctrl = ctrl(item, path);
92
+ }
93
+ }
94
+ return typeof ctrl === 'function' ? ctrl(item, path) : ctrl;
95
+ }
96
+
97
+ export { visit };
@@ -0,0 +1,98 @@
1
+ export { createScalarToken, resolveAsScalar, setScalarValue } from './cst-scalar.js';
2
+ export { stringify } from './cst-stringify.js';
3
+ export { visit } from './cst-visit.js';
4
+
5
+ /** The byte order mark */
6
+ const BOM = '\u{FEFF}';
7
+ /** Start of doc-mode */
8
+ const DOCUMENT = '\x02'; // C0: Start of Text
9
+ /** Unexpected end of flow-mode */
10
+ const FLOW_END = '\x18'; // C0: Cancel
11
+ /** Next token is a scalar value */
12
+ const SCALAR = '\x1f'; // C0: Unit Separator
13
+ /** @returns `true` if `token` is a flow or block collection */
14
+ const isCollection = (token) => !!token && 'items' in token;
15
+ /** @returns `true` if `token` is a flow or block scalar; not an alias */
16
+ const isScalar = (token) => !!token &&
17
+ (token.type === 'scalar' ||
18
+ token.type === 'single-quoted-scalar' ||
19
+ token.type === 'double-quoted-scalar' ||
20
+ token.type === 'block-scalar');
21
+ /* istanbul ignore next */
22
+ /** Get a printable representation of a lexer token */
23
+ function prettyToken(token) {
24
+ switch (token) {
25
+ case BOM:
26
+ return '<BOM>';
27
+ case DOCUMENT:
28
+ return '<DOC>';
29
+ case FLOW_END:
30
+ return '<FLOW_END>';
31
+ case SCALAR:
32
+ return '<SCALAR>';
33
+ default:
34
+ return JSON.stringify(token);
35
+ }
36
+ }
37
+ /** Identify the type of a lexer token. May return `null` for unknown tokens. */
38
+ function tokenType(source) {
39
+ switch (source) {
40
+ case BOM:
41
+ return 'byte-order-mark';
42
+ case DOCUMENT:
43
+ return 'doc-mode';
44
+ case FLOW_END:
45
+ return 'flow-error-end';
46
+ case SCALAR:
47
+ return 'scalar';
48
+ case '---':
49
+ return 'doc-start';
50
+ case '...':
51
+ return 'doc-end';
52
+ case '':
53
+ case '\n':
54
+ case '\r\n':
55
+ return 'newline';
56
+ case '-':
57
+ return 'seq-item-ind';
58
+ case '?':
59
+ return 'explicit-key-ind';
60
+ case ':':
61
+ return 'map-value-ind';
62
+ case '{':
63
+ return 'flow-map-start';
64
+ case '}':
65
+ return 'flow-map-end';
66
+ case '[':
67
+ return 'flow-seq-start';
68
+ case ']':
69
+ return 'flow-seq-end';
70
+ case ',':
71
+ return 'comma';
72
+ }
73
+ switch (source[0]) {
74
+ case ' ':
75
+ case '\t':
76
+ return 'space';
77
+ case '#':
78
+ return 'comment';
79
+ case '%':
80
+ return 'directive-line';
81
+ case '*':
82
+ return 'alias';
83
+ case '&':
84
+ return 'anchor';
85
+ case '!':
86
+ return 'tag';
87
+ case "'":
88
+ return 'single-quoted-scalar';
89
+ case '"':
90
+ return 'double-quoted-scalar';
91
+ case '|':
92
+ case '>':
93
+ return 'block-scalar-header';
94
+ }
95
+ return null;
96
+ }
97
+
98
+ export { BOM, DOCUMENT, FLOW_END, SCALAR, isCollection, isScalar, prettyToken, tokenType };