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
@@ -1,158 +1,134 @@
1
1
  'use strict';
2
2
 
3
- var parse = require('./cst/parse.js');
4
- var Document = require('./doc/Document.js');
5
-
6
- function testEvents(src, options) {
7
- const opt = Object.assign({
8
- keepCstNodes: true,
9
- keepNodeTypes: true,
10
- version: '1.2'
11
- }, options);
12
- const docs = parse.parse(src).map(cstDoc => new Document.Document(null, opt).parse(cstDoc));
13
- const errDoc = docs.find(doc => doc.errors.length > 0);
14
- const error = errDoc ? errDoc.errors[0].message : null;
15
- const events = ['+STR'];
16
-
17
- try {
18
- for (let i = 0; i < docs.length; ++i) {
19
- const doc = docs[i];
20
- let root = doc.contents;
21
- if (Array.isArray(root)) root = root[0];
22
- const [rootStart, rootEnd] = doc.range || [0, 0];
23
- let e = doc.errors[0] && doc.errors[0].source;
24
- if (e && e.type === 'SEQ_ITEM') e = e.node;
25
- if (e && (e.type === 'DOCUMENT' || e.range.start < rootStart)) throw new Error();
26
- let docStart = '+DOC';
27
- const pre = src.slice(0, rootStart);
28
- const explicitDoc = /---\s*$/.test(pre);
29
- if (explicitDoc) docStart += ' ---';else if (!doc.contents) continue;
30
- events.push(docStart);
31
- addEvents(events, doc, e, root);
32
- if (doc.contents && doc.contents.length > 1) throw new Error();
33
- let docEnd = '-DOC';
34
-
35
- if (rootEnd) {
36
- const post = src.slice(rootEnd);
37
- if (/^\.\.\./.test(post)) docEnd += ' ...';
38
- }
39
-
40
- events.push(docEnd);
3
+ var Node = require('./nodes/Node.js');
4
+ var publicApi = require('./public-api.js');
5
+ var visit = require('./visit.js');
6
+
7
+ const scalarChar = {
8
+ BLOCK_FOLDED: '>',
9
+ BLOCK_LITERAL: '|',
10
+ PLAIN: ':',
11
+ QUOTE_DOUBLE: '"',
12
+ QUOTE_SINGLE: "'"
13
+ };
14
+ function anchorExists(doc, anchor) {
15
+ let found = false;
16
+ visit.visit(doc, {
17
+ Value(_key, node) {
18
+ if (node.anchor === anchor) {
19
+ found = true;
20
+ return visit.visit.BREAK;
21
+ }
22
+ }
23
+ });
24
+ return found;
25
+ }
26
+ // test harness for yaml-test-suite event tests
27
+ function testEvents(src) {
28
+ var _a;
29
+ const docs = publicApi.parseAllDocuments(src);
30
+ const errDoc = docs.find(doc => doc.errors.length > 0);
31
+ const error = errDoc ? errDoc.errors[0].message : null;
32
+ const events = ['+STR'];
33
+ try {
34
+ for (let i = 0; i < docs.length; ++i) {
35
+ const doc = docs[i];
36
+ let root = doc.contents;
37
+ if (Array.isArray(root))
38
+ root = root[0];
39
+ // eslint-disable-next-line no-sparse-arrays
40
+ const [rootStart, , rootEnd] = doc.range || [0, , 0];
41
+ const error = doc.errors[0];
42
+ if (error && (!error.pos || error.pos[0] < rootStart))
43
+ throw new Error();
44
+ let docStart = '+DOC';
45
+ if (doc.directives.marker)
46
+ docStart += ' ---';
47
+ else if (doc.contents && doc.contents.range[2] === doc.contents.range[0])
48
+ continue;
49
+ events.push(docStart);
50
+ addEvents(events, doc, (_a = error === null || error === void 0 ? void 0 : error.pos[0]) !== null && _a !== void 0 ? _a : -1, root);
51
+ let docEnd = '-DOC';
52
+ if (rootEnd) {
53
+ const post = src.slice(rootStart, rootEnd);
54
+ if (/^\.\.\.($|\s)/m.test(post))
55
+ docEnd += ' ...';
56
+ }
57
+ events.push(docEnd);
58
+ }
41
59
  }
42
- } catch (e) {
43
- return {
44
- events,
45
- error: error || e
46
- };
47
- }
48
-
49
- events.push('-STR');
50
- return {
51
- events,
52
- error
53
- };
60
+ catch (e) {
61
+ return { events, error: error || e };
62
+ }
63
+ events.push('-STR');
64
+ return { events, error };
54
65
  }
55
-
56
- function addEvents(events, doc, e, node) {
57
- if (!node) {
58
- events.push('=VAL :');
59
- return;
60
- }
61
-
62
- if (e && node.cstNode === e) throw new Error();
63
- let props = '';
64
- let anchor = doc.anchors.getName(node);
65
-
66
- if (anchor) {
67
- if (/\d$/.test(anchor)) {
68
- const alt = anchor.replace(/\d$/, '');
69
- if (doc.anchors.getNode(alt)) anchor = alt;
66
+ function addEvents(events, doc, errPos, node) {
67
+ if (!node) {
68
+ events.push('=VAL :');
69
+ return;
70
70
  }
71
-
72
- props = ` &${anchor}`;
73
- }
74
-
75
- if (node.cstNode && node.cstNode.tag) {
76
- const {
77
- handle,
78
- suffix
79
- } = node.cstNode.tag;
80
- props += handle === '!' && !suffix ? ' <!>' : ` <${node.tag}>`;
81
- }
82
-
83
- let scalar = null;
84
-
85
- switch (node.type) {
86
- case 'ALIAS':
87
- {
88
- let alias = doc.anchors.getName(node.source);
89
-
90
- if (/\d$/.test(alias)) {
91
- const alt = alias.replace(/\d$/, '');
92
- if (doc.anchors.getNode(alt)) alias = alt;
71
+ if (errPos !== -1 && Node.isNode(node) && node.range[0] >= errPos)
72
+ throw new Error();
73
+ let props = '';
74
+ let anchor = Node.isScalar(node) || Node.isCollection(node) ? node.anchor : undefined;
75
+ if (anchor) {
76
+ if (/\d$/.test(anchor)) {
77
+ const alt = anchor.replace(/\d$/, '');
78
+ if (anchorExists(doc, alt))
79
+ anchor = alt;
80
+ }
81
+ props = ` &${anchor}`;
82
+ }
83
+ if (Node.isNode(node) && node.tag)
84
+ props += ` <${node.tag}>`;
85
+ if (Node.isMap(node)) {
86
+ events.push(`+MAP${props}`);
87
+ node.items.forEach(({ key, value }) => {
88
+ addEvents(events, doc, errPos, key);
89
+ addEvents(events, doc, errPos, value);
90
+ });
91
+ events.push('-MAP');
92
+ }
93
+ else if (Node.isSeq(node)) {
94
+ events.push(`+SEQ${props}`);
95
+ node.items.forEach(item => {
96
+ addEvents(events, doc, errPos, item);
97
+ });
98
+ events.push('-SEQ');
99
+ }
100
+ else if (Node.isPair(node)) {
101
+ events.push(`+MAP${props}`);
102
+ addEvents(events, doc, errPos, node.key);
103
+ addEvents(events, doc, errPos, node.value);
104
+ events.push('-MAP');
105
+ }
106
+ else if (Node.isAlias(node)) {
107
+ let alias = node.source;
108
+ if (alias && /\d$/.test(alias)) {
109
+ const alt = alias.replace(/\d$/, '');
110
+ if (anchorExists(doc, alt))
111
+ alias = alt;
93
112
  }
94
-
95
113
  events.push(`=ALI${props} *${alias}`);
96
- }
97
- break;
98
-
99
- case 'BLOCK_FOLDED':
100
- scalar = '>';
101
- break;
102
-
103
- case 'BLOCK_LITERAL':
104
- scalar = '|';
105
- break;
106
-
107
- case 'PLAIN':
108
- scalar = ':';
109
- break;
110
-
111
- case 'QUOTE_DOUBLE':
112
- scalar = '"';
113
- break;
114
-
115
- case 'QUOTE_SINGLE':
116
- scalar = "'";
117
- break;
118
-
119
- case 'PAIR':
120
- events.push(`+MAP${props}`);
121
- addEvents(events, doc, e, node.key);
122
- addEvents(events, doc, e, node.value);
123
- events.push('-MAP');
124
- break;
125
-
126
- case 'FLOW_SEQ':
127
- case 'SEQ':
128
- events.push(`+SEQ${props}`);
129
- node.items.forEach(item => {
130
- addEvents(events, doc, e, item);
131
- });
132
- events.push('-SEQ');
133
- break;
134
-
135
- case 'FLOW_MAP':
136
- case 'MAP':
137
- events.push(`+MAP${props}`);
138
- node.items.forEach(({
139
- key,
140
- value
141
- }) => {
142
- addEvents(events, doc, e, key);
143
- addEvents(events, doc, e, value);
144
- });
145
- events.push('-MAP');
146
- break;
147
-
148
- default:
149
- throw new Error(`Unexpected node type ${node.type}`);
150
- }
151
-
152
- if (scalar) {
153
- const value = node.cstNode.strValue.replace(/\\/g, '\\\\').replace(/\0/g, '\\0').replace(/\x07/g, '\\a').replace(/\x08/g, '\\b').replace(/\t/g, '\\t').replace(/\n/g, '\\n').replace(/\v/g, '\\v').replace(/\f/g, '\\f').replace(/\r/g, '\\r').replace(/\x1b/g, '\\e');
154
- events.push(`=VAL${props} ${scalar}${value}`);
155
- }
114
+ }
115
+ else {
116
+ const scalar = scalarChar[String(node.type)];
117
+ if (!scalar)
118
+ throw new Error(`Unexpected node type ${node.type}`);
119
+ const value = node.source
120
+ .replace(/\\/g, '\\\\')
121
+ .replace(/\0/g, '\\0')
122
+ .replace(/\x07/g, '\\a')
123
+ .replace(/\x08/g, '\\b')
124
+ .replace(/\t/g, '\\t')
125
+ .replace(/\n/g, '\\n')
126
+ .replace(/\v/g, '\\v')
127
+ .replace(/\f/g, '\\f')
128
+ .replace(/\r/g, '\\r')
129
+ .replace(/\x1b/g, '\\e');
130
+ events.push(`=VAL${props} ${scalar}${value}`);
131
+ }
156
132
  }
157
133
 
158
134
  exports.testEvents = testEvents;
package/dist/util.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export { debug, LogLevelId, warn } from './log.js';
2
+ export { findPair } from './nodes/YAMLMap.js';
3
+ export { toJS, ToJSContext } from './nodes/toJS.js';
4
+ export { foldFlowLines } from './stringify/foldFlowLines';
5
+ export { stringifyNumber } from './stringify/stringifyNumber.js';
6
+ export { stringifyString } from './stringify/stringifyString.js';
package/dist/util.js CHANGED
@@ -1,21 +1,18 @@
1
1
  'use strict';
2
2
 
3
+ var log = require('./log.js');
4
+ var YAMLMap = require('./nodes/YAMLMap.js');
5
+ var toJS = require('./nodes/toJS.js');
6
+ var foldFlowLines = require('./stringify/foldFlowLines.js');
3
7
  var stringifyNumber = require('./stringify/stringifyNumber.js');
4
8
  var stringifyString = require('./stringify/stringifyString.js');
5
- var constants = require('./constants.js');
6
- var errors = require('./errors.js');
7
- var YAMLMap = require('./ast/YAMLMap.js');
8
- var toJS = require('./ast/toJS.js');
9
9
 
10
10
 
11
11
 
12
- exports.stringifyNumber = stringifyNumber.stringifyNumber;
13
- exports.stringifyString = stringifyString.stringifyString;
14
- exports.Type = constants.Type;
15
- exports.YAMLError = errors.YAMLError;
16
- exports.YAMLReferenceError = errors.YAMLReferenceError;
17
- exports.YAMLSemanticError = errors.YAMLSemanticError;
18
- exports.YAMLSyntaxError = errors.YAMLSyntaxError;
19
- exports.YAMLWarning = errors.YAMLWarning;
12
+ exports.debug = log.debug;
13
+ exports.warn = log.warn;
20
14
  exports.findPair = YAMLMap.findPair;
21
15
  exports.toJS = toJS.toJS;
16
+ exports.foldFlowLines = foldFlowLines.foldFlowLines;
17
+ exports.stringifyNumber = stringifyNumber.stringifyNumber;
18
+ exports.stringifyString = stringifyString.stringifyString;
@@ -0,0 +1,63 @@
1
+ import type { Document } from './doc/Document.js';
2
+ import type { Alias } from './nodes/Alias.js';
3
+ import { Node } from './nodes/Node.js';
4
+ import type { Pair } from './nodes/Pair.js';
5
+ import type { Scalar } from './nodes/Scalar.js';
6
+ import type { YAMLMap } from './nodes/YAMLMap.js';
7
+ import type { YAMLSeq } from './nodes/YAMLSeq.js';
8
+ export declare type visitorFn<T> = (key: number | 'key' | 'value' | null, node: T, path: readonly (Document | Node | Pair)[]) => void | symbol | number | Node | Pair;
9
+ export declare type visitor = visitorFn<unknown> | {
10
+ Alias?: visitorFn<Alias>;
11
+ Collection?: visitorFn<YAMLMap | YAMLSeq>;
12
+ Map?: visitorFn<YAMLMap>;
13
+ Node?: visitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
14
+ Pair?: visitorFn<Pair>;
15
+ Scalar?: visitorFn<Scalar>;
16
+ Seq?: visitorFn<YAMLSeq>;
17
+ Value?: visitorFn<Scalar | YAMLMap | YAMLSeq>;
18
+ };
19
+ /**
20
+ * Apply a visitor to an AST node or document.
21
+ *
22
+ * Walks through the tree (depth-first) starting from `node`, calling a
23
+ * `visitor` function with three arguments:
24
+ * - `key`: For sequence values and map `Pair`, the node's index in the
25
+ * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
26
+ * `null` for the root node.
27
+ * - `node`: The current node.
28
+ * - `path`: The ancestry of the current node.
29
+ *
30
+ * The return value of the visitor may be used to control the traversal:
31
+ * - `undefined` (default): Do nothing and continue
32
+ * - `visit.SKIP`: Do not visit the children of this node, continue with next
33
+ * sibling
34
+ * - `visit.BREAK`: Terminate traversal completely
35
+ * - `visit.REMOVE`: Remove the current node, then continue with the next one
36
+ * - `Node`: Replace the current node, then continue by visiting it
37
+ * - `number`: While iterating the items of a sequence or map, set the index
38
+ * of the next step. This is useful especially if the index of the current
39
+ * node has changed.
40
+ *
41
+ * If `visitor` is a single function, it will be called with all values
42
+ * encountered in the tree, including e.g. `null` values. Alternatively,
43
+ * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
44
+ * `Alias` and `Scalar` node. To define the same visitor function for more than
45
+ * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
46
+ * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
47
+ * specific defined one will be used for each node.
48
+ */
49
+ export declare function visit(node: Node | Document | null, visitor: visitorFn<unknown> | {
50
+ Alias?: visitorFn<Alias>;
51
+ Collection?: visitorFn<YAMLMap | YAMLSeq>;
52
+ Map?: visitorFn<YAMLMap>;
53
+ Node?: visitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
54
+ Pair?: visitorFn<Pair>;
55
+ Scalar?: visitorFn<Scalar>;
56
+ Seq?: visitorFn<YAMLSeq>;
57
+ Value?: visitorFn<Scalar | YAMLMap | YAMLSeq>;
58
+ }): void;
59
+ export declare namespace visit {
60
+ var BREAK: symbol;
61
+ var SKIP: symbol;
62
+ var REMOVE: symbol;
63
+ }
package/dist/visit.js ADDED
@@ -0,0 +1,148 @@
1
+ 'use strict';
2
+
3
+ var Node = require('./nodes/Node.js');
4
+
5
+ const BREAK = Symbol('break visit');
6
+ const SKIP = Symbol('skip children');
7
+ const REMOVE = Symbol('remove node');
8
+ /**
9
+ * Apply a visitor to an AST node or document.
10
+ *
11
+ * Walks through the tree (depth-first) starting from `node`, calling a
12
+ * `visitor` function with three arguments:
13
+ * - `key`: For sequence values and map `Pair`, the node's index in the
14
+ * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
15
+ * `null` for the root node.
16
+ * - `node`: The current node.
17
+ * - `path`: The ancestry of the current node.
18
+ *
19
+ * The return value of the visitor may be used to control the traversal:
20
+ * - `undefined` (default): Do nothing and continue
21
+ * - `visit.SKIP`: Do not visit the children of this node, continue with next
22
+ * sibling
23
+ * - `visit.BREAK`: Terminate traversal completely
24
+ * - `visit.REMOVE`: Remove the current node, then continue with the next one
25
+ * - `Node`: Replace the current node, then continue by visiting it
26
+ * - `number`: While iterating the items of a sequence or map, set the index
27
+ * of the next step. This is useful especially if the index of the current
28
+ * node has changed.
29
+ *
30
+ * If `visitor` is a single function, it will be called with all values
31
+ * encountered in the tree, including e.g. `null` values. Alternatively,
32
+ * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
33
+ * `Alias` and `Scalar` node. To define the same visitor function for more than
34
+ * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
35
+ * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
36
+ * specific defined one will be used for each node.
37
+ */
38
+ function visit(node, visitor) {
39
+ if (typeof visitor === 'object' &&
40
+ (visitor.Collection || visitor.Node || visitor.Value)) {
41
+ visitor = Object.assign({
42
+ Alias: visitor.Node,
43
+ Map: visitor.Node,
44
+ Scalar: visitor.Node,
45
+ Seq: visitor.Node
46
+ }, visitor.Value && {
47
+ Map: visitor.Value,
48
+ Scalar: visitor.Value,
49
+ Seq: visitor.Value
50
+ }, visitor.Collection && {
51
+ Map: visitor.Collection,
52
+ Seq: visitor.Collection
53
+ }, visitor);
54
+ }
55
+ if (Node.isDocument(node)) {
56
+ const cd = _visit(null, node.contents, visitor, Object.freeze([node]));
57
+ if (cd === REMOVE)
58
+ node.contents = null;
59
+ }
60
+ else
61
+ _visit(null, node, visitor, Object.freeze([]));
62
+ }
63
+ // Without the `as symbol` casts, TS declares these in the `visit`
64
+ // namespace using `var`, but then complains about that because
65
+ // `unique symbol` must be `const`.
66
+ /** Terminate visit traversal completely */
67
+ visit.BREAK = BREAK;
68
+ /** Do not visit the children of the current node */
69
+ visit.SKIP = SKIP;
70
+ /** Remove the current node */
71
+ visit.REMOVE = REMOVE;
72
+ function _visit(key, node, visitor, path) {
73
+ let ctrl = undefined;
74
+ if (typeof visitor === 'function')
75
+ ctrl = visitor(key, node, path);
76
+ else if (Node.isMap(node)) {
77
+ if (visitor.Map)
78
+ ctrl = visitor.Map(key, node, path);
79
+ }
80
+ else if (Node.isSeq(node)) {
81
+ if (visitor.Seq)
82
+ ctrl = visitor.Seq(key, node, path);
83
+ }
84
+ else if (Node.isPair(node)) {
85
+ if (visitor.Pair)
86
+ ctrl = visitor.Pair(key, node, path);
87
+ }
88
+ else if (Node.isScalar(node)) {
89
+ if (visitor.Scalar)
90
+ ctrl = visitor.Scalar(key, node, path);
91
+ }
92
+ else if (Node.isAlias(node)) {
93
+ if (visitor.Alias)
94
+ ctrl = visitor.Alias(key, node, path);
95
+ }
96
+ if (Node.isNode(ctrl) || Node.isPair(ctrl)) {
97
+ const parent = path[path.length - 1];
98
+ if (Node.isCollection(parent)) {
99
+ parent.items[key] = ctrl;
100
+ }
101
+ else if (Node.isPair(parent)) {
102
+ if (key === 'key')
103
+ parent.key = ctrl;
104
+ else
105
+ parent.value = ctrl;
106
+ }
107
+ else if (Node.isDocument(parent)) {
108
+ parent.contents = ctrl;
109
+ }
110
+ else {
111
+ const pt = Node.isAlias(parent) ? 'alias' : 'scalar';
112
+ throw new Error(`Cannot replace node with ${pt} parent`);
113
+ }
114
+ return _visit(key, ctrl, visitor, path);
115
+ }
116
+ if (typeof ctrl !== 'symbol') {
117
+ if (Node.isCollection(node)) {
118
+ path = Object.freeze(path.concat(node));
119
+ for (let i = 0; i < node.items.length; ++i) {
120
+ const ci = _visit(i, node.items[i], visitor, path);
121
+ if (typeof ci === 'number')
122
+ i = ci - 1;
123
+ else if (ci === BREAK)
124
+ return BREAK;
125
+ else if (ci === REMOVE) {
126
+ node.items.splice(i, 1);
127
+ i -= 1;
128
+ }
129
+ }
130
+ }
131
+ else if (Node.isPair(node)) {
132
+ path = Object.freeze(path.concat(node));
133
+ const ck = _visit('key', node.key, visitor, path);
134
+ if (ck === BREAK)
135
+ return BREAK;
136
+ else if (ck === REMOVE)
137
+ node.key = null;
138
+ const cv = _visit('value', node.value, visitor, path);
139
+ if (cv === BREAK)
140
+ return BREAK;
141
+ else if (cv === REMOVE)
142
+ node.value = null;
143
+ }
144
+ }
145
+ return ctrl;
146
+ }
147
+
148
+ exports.visit = visit;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml",
3
- "version": "2.0.0-2",
3
+ "version": "2.0.0-6",
4
4
  "license": "ISC",
5
5
  "author": "Eemeli Aro <eemeli@gmail.com>",
6
6
  "repository": "github:eemeli/yaml",
@@ -14,61 +14,47 @@
14
14
  "files": [
15
15
  "browser/",
16
16
  "dist/",
17
- "types/",
18
- "*.d.ts",
19
- "*.js",
20
- "*.mjs",
21
- "!*config.js"
17
+ "util.js"
22
18
  ],
23
19
  "type": "commonjs",
24
- "main": "./index.js",
20
+ "main": "./dist/index.js",
25
21
  "browser": {
26
- "./index.js": "./browser/index.js",
27
- "./types.js": "./browser/dist/types.js",
28
- "./types.mjs": "./browser/dist/types.js",
29
- "./util.js": "./browser/dist/util.js",
30
- "./util.mjs": "./browser/dist/util.js"
22
+ "./dist/index.js": "./browser/index.js",
23
+ "./dist/util.js": "./browser/dist/util.js",
24
+ "./util.js": "./browser/dist/util.js"
31
25
  },
32
26
  "exports": {
33
27
  ".": {
34
- "node": "./index.js",
28
+ "node": "./dist/index.js",
35
29
  "default": "./browser/index.js"
36
30
  },
37
31
  "./package.json": "./package.json",
38
- "./types": {
39
- "node": {
40
- "import": "./types.mjs",
41
- "require": "./types.js"
42
- },
43
- "default": "./browser/dist/types.js"
44
- },
45
32
  "./util": {
46
- "node": {
47
- "import": "./util.mjs",
48
- "require": "./util.js"
49
- },
33
+ "node": "./dist/util.js",
50
34
  "default": "./browser/dist/util.js"
51
35
  }
52
36
  },
53
37
  "scripts": {
54
38
  "build": "npm run build:node && npm run build:browser",
55
- "build:browser": "rollup -c rollup.browser-config.js",
56
- "build:node": "rollup -c rollup.node-config.js",
39
+ "build:browser": "rollup -c config/rollup.browser-config.js",
40
+ "build:node": "rollup -c config/rollup.node-config.js",
57
41
  "clean": "git clean -fdxe node_modules",
58
42
  "lint": "eslint src/",
59
43
  "prettier": "prettier --write .",
60
- "start": "cross-env TRACE_LEVEL=log npm run build:node && node -i -e 'YAML=require(\".\")'",
61
- "test": "jest",
44
+ "prestart": "npm run build:node",
45
+ "start": "node -i -e 'YAML=require(\"./dist/index.js\")'",
46
+ "test": "jest --config config/jest.config.js",
62
47
  "test:browsers": "cd playground && npm test",
63
- "test:dist": "npm run build:node && jest",
64
- "test:types": "tsc --lib ES2017 --noEmit tests/typings.ts",
48
+ "test:dist": "npm run build:node && jest --config config/jest.config.js",
49
+ "test:dist:types": "tsc --allowJs --moduleResolution node --noEmit --target es2017 dist/index.js",
50
+ "test:types": "tsc --noEmit",
65
51
  "docs:install": "cd docs-slate && bundle install",
66
52
  "docs:deploy": "cd docs-slate && ./deploy.sh",
67
53
  "docs": "cd docs-slate && bundle exec middleman server",
68
54
  "preversion": "npm test && npm run build",
69
55
  "prepublishOnly": "npm run clean && npm test && npm run build"
70
56
  },
71
- "browserslist": "> 0.5%, not dead, not ie 11",
57
+ "browserslist": "defaults, not ie 11",
72
58
  "prettier": {
73
59
  "arrowParens": "avoid",
74
60
  "semi": false,
@@ -79,22 +65,28 @@
79
65
  "@babel/core": "^7.12.10",
80
66
  "@babel/plugin-proposal-class-properties": "^7.12.1",
81
67
  "@babel/plugin-proposal-nullish-coalescing-operator": "^7.12.1",
68
+ "@babel/plugin-transform-typescript": "^7.12.17",
82
69
  "@babel/preset-env": "^7.12.11",
83
70
  "@rollup/plugin-babel": "^5.2.3",
84
- "babel-eslint": "^10.1.0",
85
- "babel-jest": "^26.6.3",
86
- "babel-plugin-trace": "^1.1.0",
87
- "common-tags": "^1.8.0",
71
+ "@rollup/plugin-replace": "^2.3.4",
72
+ "@rollup/plugin-typescript": "^8.1.1",
73
+ "@types/jest": "^26.0.20",
74
+ "@types/node": "^15.6.1",
75
+ "@typescript-eslint/eslint-plugin": "^4.15.2",
76
+ "@typescript-eslint/parser": "^4.15.2",
77
+ "babel-jest": "^27.0.1",
88
78
  "cross-env": "^7.0.3",
89
- "eslint": "^7.19.0",
90
- "eslint-config-prettier": "^7.2.0",
79
+ "eslint": "^7.20.0",
80
+ "eslint-config-prettier": "^8.1.0",
91
81
  "fast-check": "^2.12.0",
92
- "jest": "^26.6.3",
82
+ "jest": "^27.0.1",
83
+ "jest-ts-webcompat-resolver": "^1.0.0",
93
84
  "prettier": "^2.2.1",
94
85
  "rollup": "^2.38.2",
86
+ "tslib": "^2.1.0",
95
87
  "typescript": "^4.1.3"
96
88
  },
97
89
  "engines": {
98
- "node": ">= 10"
90
+ "node": ">= 12"
99
91
  }
100
92
  }