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,343 +1,316 @@
1
- import { addCommentBefore } from './addComment.js';
2
- import { Type } from '../constants.js';
3
- import { resolveScalar } from '../resolve/resolveScalar.js';
1
+ import { Scalar } from '../nodes/Scalar.js';
4
2
  import { foldFlowLines, FOLD_QUOTED, FOLD_FLOW, FOLD_BLOCK } from './foldFlowLines.js';
5
- import { strOptions } from '../tags/options.js';
6
3
 
7
- const getFoldOptions = ({
8
- indentAtStart
9
- }) => indentAtStart ? Object.assign({
10
- indentAtStart
11
- }, strOptions.fold) : strOptions.fold; // Also checks for lines starting with %, as parsing the output as YAML 1.1 will
4
+ const getFoldOptions = (ctx) => ({
5
+ indentAtStart: ctx.indentAtStart,
6
+ lineWidth: ctx.options.lineWidth,
7
+ minContentWidth: ctx.options.minContentWidth
8
+ });
9
+ // Also checks for lines starting with %, as parsing the output as YAML 1.1 will
12
10
  // presume that's starting a new document.
13
-
14
-
15
- const containsDocumentMarker = str => /^(%|---|\.\.\.)/m.test(str);
16
-
17
- function lineLengthOverLimit(str, limit) {
18
- const strLen = str.length;
19
- if (strLen <= limit) return false;
20
-
21
- for (let i = 0, start = 0; i < strLen; ++i) {
22
- if (str[i] === '\n') {
23
- if (i - start > limit) return true;
24
- start = i + 1;
25
- if (strLen - start <= limit) return false;
11
+ const containsDocumentMarker = (str) => /^(%|---|\.\.\.)/m.test(str);
12
+ function lineLengthOverLimit(str, lineWidth, indentLength) {
13
+ if (!lineWidth || lineWidth < 0)
14
+ return false;
15
+ const limit = lineWidth - indentLength;
16
+ const strLen = str.length;
17
+ if (strLen <= limit)
18
+ return false;
19
+ for (let i = 0, start = 0; i < strLen; ++i) {
20
+ if (str[i] === '\n') {
21
+ if (i - start > limit)
22
+ return true;
23
+ start = i + 1;
24
+ if (strLen - start <= limit)
25
+ return false;
26
+ }
26
27
  }
27
- }
28
-
29
- return true;
28
+ return true;
30
29
  }
31
-
32
30
  function doubleQuotedString(value, ctx) {
33
- const {
34
- implicitKey
35
- } = ctx;
36
- const {
37
- jsonEncoding,
38
- minMultiLineLength
39
- } = strOptions.doubleQuoted;
40
- const json = JSON.stringify(value);
41
- if (jsonEncoding) return json;
42
- const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
43
- let str = '';
44
- let start = 0;
45
-
46
- for (let i = 0, ch = json[i]; ch; ch = json[++i]) {
47
- if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') {
48
- // space before newline needs to be escaped to not be folded
49
- str += json.slice(start, i) + '\\ ';
50
- i += 1;
51
- start = i;
52
- ch = '\\';
53
- }
54
-
55
- if (ch === '\\') switch (json[i + 1]) {
56
- case 'u':
57
- {
58
- str += json.slice(start, i);
59
- const code = json.substr(i + 2, 4);
60
-
61
- switch (code) {
62
- case '0000':
63
- str += '\\0';
64
- break;
65
-
66
- case '0007':
67
- str += '\\a';
68
- break;
69
-
70
- case '000b':
71
- str += '\\v';
72
- break;
73
-
74
- case '001b':
75
- str += '\\e';
76
- break;
77
-
78
- case '0085':
79
- str += '\\N';
80
- break;
81
-
82
- case '00a0':
83
- str += '\\_';
84
- break;
85
-
86
- case '2028':
87
- str += '\\L';
88
- break;
89
-
90
- case '2029':
91
- str += '\\P';
92
- break;
93
-
94
- default:
95
- if (code.substr(0, 2) === '00') str += '\\x' + code.substr(2);else str += json.substr(i, 6);
96
- }
97
-
98
- i += 5;
99
- start = i + 1;
100
- }
101
- break;
102
-
103
- case 'n':
104
- if (implicitKey || json[i + 2] === '"' || json.length < minMultiLineLength) {
105
- i += 1;
106
- } else {
107
- // folding will eat first newline
108
- str += json.slice(start, i) + '\n\n';
109
-
110
- while (json[i + 2] === '\\' && json[i + 3] === 'n' && json[i + 4] !== '"') {
111
- str += '\n';
112
- i += 2;
113
- }
114
-
115
- str += indent; // space after newline needs to be escaped to not be folded
116
-
117
- if (json[i + 2] === ' ') str += '\\';
118
- i += 1;
119
- start = i + 1;
31
+ const json = JSON.stringify(value);
32
+ if (ctx.options.doubleQuotedAsJSON)
33
+ return json;
34
+ const { implicitKey } = ctx;
35
+ const minMultiLineLength = ctx.options.doubleQuotedMinMultiLineLength;
36
+ const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
37
+ let str = '';
38
+ let start = 0;
39
+ for (let i = 0, ch = json[i]; ch; ch = json[++i]) {
40
+ if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') {
41
+ // space before newline needs to be escaped to not be folded
42
+ str += json.slice(start, i) + '\\ ';
43
+ i += 1;
44
+ start = i;
45
+ ch = '\\';
120
46
  }
121
-
122
- break;
123
-
124
- default:
125
- i += 1;
47
+ if (ch === '\\')
48
+ switch (json[i + 1]) {
49
+ case 'u':
50
+ {
51
+ str += json.slice(start, i);
52
+ const code = json.substr(i + 2, 4);
53
+ switch (code) {
54
+ case '0000':
55
+ str += '\\0';
56
+ break;
57
+ case '0007':
58
+ str += '\\a';
59
+ break;
60
+ case '000b':
61
+ str += '\\v';
62
+ break;
63
+ case '001b':
64
+ str += '\\e';
65
+ break;
66
+ case '0085':
67
+ str += '\\N';
68
+ break;
69
+ case '00a0':
70
+ str += '\\_';
71
+ break;
72
+ case '2028':
73
+ str += '\\L';
74
+ break;
75
+ case '2029':
76
+ str += '\\P';
77
+ break;
78
+ default:
79
+ if (code.substr(0, 2) === '00')
80
+ str += '\\x' + code.substr(2);
81
+ else
82
+ str += json.substr(i, 6);
83
+ }
84
+ i += 5;
85
+ start = i + 1;
86
+ }
87
+ break;
88
+ case 'n':
89
+ if (implicitKey ||
90
+ json[i + 2] === '"' ||
91
+ json.length < minMultiLineLength) {
92
+ i += 1;
93
+ }
94
+ else {
95
+ // folding will eat first newline
96
+ str += json.slice(start, i) + '\n\n';
97
+ while (json[i + 2] === '\\' &&
98
+ json[i + 3] === 'n' &&
99
+ json[i + 4] !== '"') {
100
+ str += '\n';
101
+ i += 2;
102
+ }
103
+ str += indent;
104
+ // space after newline needs to be escaped to not be folded
105
+ if (json[i + 2] === ' ')
106
+ str += '\\';
107
+ i += 1;
108
+ start = i + 1;
109
+ }
110
+ break;
111
+ default:
112
+ i += 1;
113
+ }
126
114
  }
127
- }
128
-
129
- str = start ? str + json.slice(start) : json;
130
- return implicitKey ? str : foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx));
115
+ str = start ? str + json.slice(start) : json;
116
+ return implicitKey
117
+ ? str
118
+ : foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx));
131
119
  }
132
-
133
120
  function singleQuotedString(value, ctx) {
134
- if (ctx.implicitKey) {
135
- if (/\n/.test(value)) return doubleQuotedString(value, ctx);
136
- } else {
137
- // single quoted string can't have leading or trailing whitespace around newline
138
- if (/[ \t]\n|\n[ \t]/.test(value)) return doubleQuotedString(value, ctx);
139
- }
140
-
141
- const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
142
- const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, "$&\n".concat(indent)) + "'";
143
- return ctx.implicitKey ? res : foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx));
121
+ if (ctx.implicitKey) {
122
+ if (/\n/.test(value))
123
+ return doubleQuotedString(value, ctx);
124
+ }
125
+ else {
126
+ // single quoted string can't have leading or trailing whitespace around newline
127
+ if (/[ \t]\n|\n[ \t]/.test(value))
128
+ return doubleQuotedString(value, ctx);
129
+ }
130
+ const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
131
+ const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
132
+ return ctx.implicitKey
133
+ ? res
134
+ : foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx));
144
135
  }
145
-
146
- function blockString({
147
- comment,
148
- type,
149
- value
150
- }, ctx, onComment, onChompKeep) {
151
- // 1. Block can't end in whitespace unless the last line is non-empty.
152
- // 2. Strings consisting of only whitespace are best rendered explicitly.
153
- if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
154
- return doubleQuotedString(value, ctx);
155
- }
156
-
157
- const indent = ctx.indent || (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
158
- const indentSize = indent ? '2' : '1'; // root is at -1
159
-
160
- const literal = type === Type.BLOCK_FOLDED ? false : type === Type.BLOCK_LITERAL ? true : !lineLengthOverLimit(value, strOptions.fold.lineWidth - indent.length);
161
- let header = literal ? '|' : '>';
162
- if (!value) return header + '\n';
163
- let wsStart = '';
164
- let wsEnd = '';
165
- value = value.replace(/[\n\t ]*$/, ws => {
166
- const n = ws.indexOf('\n');
167
-
168
- if (n === -1) {
169
- header += '-'; // strip
170
- } else if (value === ws || n !== ws.length - 1) {
171
- header += '+'; // keep
172
-
173
- if (onChompKeep) onChompKeep();
136
+ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
137
+ // 1. Block can't end in whitespace unless the last line is non-empty.
138
+ // 2. Strings consisting of only whitespace are best rendered explicitly.
139
+ if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
140
+ return doubleQuotedString(value, ctx);
174
141
  }
175
-
176
- wsEnd = ws.replace(/\n$/, '');
177
- return '';
178
- }).replace(/^[\n ]*/, ws => {
179
- if (ws.indexOf(' ') !== -1) header += indentSize;
180
- const m = ws.match(/ +$/);
181
-
182
- if (m) {
183
- wsStart = ws.slice(0, -m[0].length);
184
- return m[0];
185
- } else {
186
- wsStart = ws;
187
- return '';
142
+ const indent = ctx.indent ||
143
+ (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
144
+ const literal = type === Scalar.BLOCK_FOLDED
145
+ ? false
146
+ : type === Scalar.BLOCK_LITERAL
147
+ ? true
148
+ : !lineLengthOverLimit(value, ctx.options.lineWidth, indent.length);
149
+ if (!value)
150
+ return literal ? '|\n' : '>\n';
151
+ // determine chomping from whitespace at value end
152
+ let chomp;
153
+ let endStart;
154
+ for (endStart = value.length; endStart > 0; --endStart) {
155
+ const ch = value[endStart - 1];
156
+ if (ch !== '\n' && ch !== '\t' && ch !== ' ')
157
+ break;
188
158
  }
189
- });
190
- if (wsEnd) wsEnd = wsEnd.replace(/\n+(?!\n|$)/g, "$&".concat(indent));
191
- if (wsStart) wsStart = wsStart.replace(/\n+/g, "$&".concat(indent));
192
-
193
- if (comment) {
194
- header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ');
195
- if (onComment) onComment();
196
- }
197
-
198
- if (!value) return "".concat(header).concat(indentSize, "\n").concat(indent).concat(wsEnd);
199
-
200
- if (literal) {
201
- value = value.replace(/\n+/g, "$&".concat(indent));
202
- return "".concat(header, "\n").concat(indent).concat(wsStart).concat(value).concat(wsEnd);
203
- }
204
-
205
- value = value.replace(/\n+/g, '\n$&').replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
206
- // ^ ind.line ^ empty ^ capture next empty lines only at end of indent
207
- .replace(/\n+/g, "$&".concat(indent));
208
- const body = foldFlowLines("".concat(wsStart).concat(value).concat(wsEnd), indent, FOLD_BLOCK, strOptions.fold);
209
- return "".concat(header, "\n").concat(indent).concat(body);
159
+ let end = value.substring(endStart);
160
+ const endNlPos = end.indexOf('\n');
161
+ if (endNlPos === -1) {
162
+ chomp = '-'; // strip
163
+ }
164
+ else if (value === end || endNlPos !== end.length - 1) {
165
+ chomp = '+'; // keep
166
+ if (onChompKeep)
167
+ onChompKeep();
168
+ }
169
+ else {
170
+ chomp = ''; // clip
171
+ }
172
+ if (end) {
173
+ value = value.slice(0, -end.length);
174
+ if (end[end.length - 1] === '\n')
175
+ end = end.slice(0, -1);
176
+ end = end.replace(/\n+(?!\n|$)/g, `$&${indent}`);
177
+ }
178
+ // determine indent indicator from whitespace at value start
179
+ let startWithSpace = false;
180
+ let startEnd;
181
+ let startNlPos = -1;
182
+ for (startEnd = 0; startEnd < value.length; ++startEnd) {
183
+ const ch = value[startEnd];
184
+ if (ch === ' ')
185
+ startWithSpace = true;
186
+ else if (ch === '\n')
187
+ startNlPos = startEnd;
188
+ else
189
+ break;
190
+ }
191
+ let start = value.substring(0, startNlPos < startEnd ? startNlPos + 1 : startEnd);
192
+ if (start) {
193
+ value = value.substring(start.length);
194
+ start = start.replace(/\n+/g, `$&${indent}`);
195
+ }
196
+ const indentSize = indent ? '2' : '1'; // root is at -1
197
+ let header = (literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp;
198
+ if (comment) {
199
+ header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ');
200
+ if (onComment)
201
+ onComment();
202
+ }
203
+ if (literal) {
204
+ value = value.replace(/\n+/g, `$&${indent}`);
205
+ return `${header}\n${indent}${start}${value}${end}`;
206
+ }
207
+ value = value
208
+ .replace(/\n+/g, '\n$&')
209
+ .replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
210
+ // ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
211
+ .replace(/\n+/g, `$&${indent}`);
212
+ const body = foldFlowLines(`${start}${value}${end}`, indent, FOLD_BLOCK, getFoldOptions(ctx));
213
+ return `${header}\n${indent}${body}`;
210
214
  }
211
-
212
215
  function plainString(item, ctx, onComment, onChompKeep) {
213
- const {
214
- comment,
215
- type,
216
- value
217
- } = item;
218
- const {
219
- actualString,
220
- implicitKey,
221
- indent,
222
- inFlow
223
- } = ctx;
224
-
225
- if (implicitKey && /[\n[\]{},]/.test(value) || inFlow && /[[\]{},]/.test(value)) {
226
- return doubleQuotedString(value, ctx);
227
- }
228
-
229
- if (!value || /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
230
- const hasDouble = value.indexOf('"') !== -1;
231
- const hasSingle = value.indexOf("'") !== -1;
232
- let quotedString;
233
-
234
- if (hasDouble && !hasSingle) {
235
- quotedString = singleQuotedString;
236
- } else if (hasSingle && !hasDouble) {
237
- quotedString = doubleQuotedString;
238
- } else if (strOptions.defaultQuoteSingle) {
239
- quotedString = singleQuotedString;
240
- } else {
241
- quotedString = doubleQuotedString;
242
- } // not allowed:
243
- // - empty string, '-' or '?'
244
- // - start with an indicator character (except [?:-]) or /[?-] /
245
- // - '\n ', ': ' or ' \n' anywhere
246
- // - '#' not preceded by a non-space char
247
- // - end with ' ' or ':'
248
-
249
-
250
- return implicitKey || inFlow || value.indexOf('\n') === -1 ? quotedString(value, ctx) : blockString(item, ctx, onComment, onChompKeep);
251
- }
252
-
253
- if (!implicitKey && !inFlow && type !== Type.PLAIN && value.indexOf('\n') !== -1) {
254
- // Where allowed & type not set explicitly, prefer block style for multiline strings
255
- return blockString(item, ctx, onComment, onChompKeep);
256
- }
257
-
258
- if (indent === '' && containsDocumentMarker(value)) {
259
- ctx.forceBlockIndent = true;
260
- return blockString(item, ctx, onComment, onChompKeep);
261
- }
262
-
263
- const str = value.replace(/\n+/g, "$&\n".concat(indent)); // Verify that output will be parsed as a string, as e.g. plain numbers and
264
- // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
265
- // and others in v1.1.
266
-
267
- if (actualString) {
268
- const {
269
- tags
270
- } = ctx.doc.schema;
271
- const resolved = resolveScalar(str, tags).value;
272
- if (typeof resolved !== 'string') return doubleQuotedString(value, ctx);
273
- }
274
-
275
- const body = implicitKey ? str : foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx));
276
-
277
- if (comment && !inFlow && (body.indexOf('\n') !== -1 || comment.indexOf('\n') !== -1)) {
278
- if (onComment) onComment();
279
- return addCommentBefore(body, indent, comment);
280
- }
281
-
282
- return body;
216
+ var _a;
217
+ const { type, value } = item;
218
+ const { actualString, implicitKey, indent, inFlow } = ctx;
219
+ if ((implicitKey && /[\n[\]{},]/.test(value)) ||
220
+ (inFlow && /[[\]{},]/.test(value))) {
221
+ return doubleQuotedString(value, ctx);
222
+ }
223
+ if (!value ||
224
+ /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
225
+ const hasDouble = value.indexOf('"') !== -1;
226
+ const hasSingle = value.indexOf("'") !== -1;
227
+ let quotedString;
228
+ if (hasDouble && !hasSingle) {
229
+ quotedString = singleQuotedString;
230
+ }
231
+ else if (hasSingle && !hasDouble) {
232
+ quotedString = doubleQuotedString;
233
+ }
234
+ else if (ctx.options.singleQuote) {
235
+ quotedString = singleQuotedString;
236
+ }
237
+ else {
238
+ quotedString = doubleQuotedString;
239
+ }
240
+ // not allowed:
241
+ // - empty string, '-' or '?'
242
+ // - start with an indicator character (except [?:-]) or /[?-] /
243
+ // - '\n ', ': ' or ' \n' anywhere
244
+ // - '#' not preceded by a non-space char
245
+ // - end with ' ' or ':'
246
+ return implicitKey || inFlow || value.indexOf('\n') === -1
247
+ ? quotedString(value, ctx)
248
+ : blockString(item, ctx, onComment, onChompKeep);
249
+ }
250
+ if (!implicitKey &&
251
+ !inFlow &&
252
+ type !== Scalar.PLAIN &&
253
+ value.indexOf('\n') !== -1) {
254
+ // Where allowed & type not set explicitly, prefer block style for multiline strings
255
+ return blockString(item, ctx, onComment, onChompKeep);
256
+ }
257
+ if (indent === '' && containsDocumentMarker(value)) {
258
+ ctx.forceBlockIndent = true;
259
+ return blockString(item, ctx, onComment, onChompKeep);
260
+ }
261
+ const str = value.replace(/\n+/g, `$&\n${indent}`);
262
+ // Verify that output will be parsed as a string, as e.g. plain numbers and
263
+ // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
264
+ // and others in v1.1.
265
+ if (actualString) {
266
+ for (const tag of ctx.doc.schema.tags) {
267
+ if (tag.default &&
268
+ tag.tag !== 'tag:yaml.org,2002:str' &&
269
+ ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str)))
270
+ return doubleQuotedString(value, ctx);
271
+ }
272
+ }
273
+ return implicitKey
274
+ ? str
275
+ : foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx));
283
276
  }
284
-
285
277
  function stringifyString(item, ctx, onComment, onChompKeep) {
286
- const {
287
- defaultKeyType,
288
- defaultType
289
- } = strOptions;
290
- const {
291
- implicitKey,
292
- inFlow
293
- } = ctx;
294
- let {
295
- type,
296
- value
297
- } = item;
298
-
299
- if (typeof value !== 'string') {
300
- value = String(value);
301
- item = Object.assign({}, item, {
302
- value
303
- });
304
- }
305
-
306
- if (type !== Type.QUOTE_DOUBLE) {
307
- // force double quotes on control characters & unpaired surrogates
308
- if (/[\x00-\x08\x0b-\x1f\x7f-\x9f\u{D800}-\u{DFFF}]/u.test(value)) type = Type.QUOTE_DOUBLE;
309
- }
310
-
311
- const _stringify = _type => {
312
- switch (_type) {
313
- case Type.BLOCK_FOLDED:
314
- case Type.BLOCK_LITERAL:
315
- return implicitKey || inFlow ? doubleQuotedString(value, ctx) // blocks are not valid inside flow containers
316
- : blockString(item, ctx, onComment, onChompKeep);
317
-
318
- case Type.QUOTE_DOUBLE:
319
- return doubleQuotedString(value, ctx);
320
-
321
- case Type.QUOTE_SINGLE:
322
- return singleQuotedString(value, ctx);
323
-
324
- case Type.PLAIN:
325
- return plainString(item, ctx, onComment, onChompKeep);
326
-
327
- default:
328
- return null;
278
+ const { implicitKey, inFlow } = ctx;
279
+ const ss = typeof item.value === 'string'
280
+ ? item
281
+ : Object.assign({}, item, { value: String(item.value) });
282
+ let { type } = item;
283
+ if (type !== Scalar.QUOTE_DOUBLE) {
284
+ // force double quotes on control characters & unpaired surrogates
285
+ if (/[\x00-\x08\x0b-\x1f\x7f-\x9f\u{D800}-\u{DFFF}]/u.test(ss.value))
286
+ type = Scalar.QUOTE_DOUBLE;
329
287
  }
330
- };
331
-
332
- let res = _stringify(type);
333
-
334
- if (res === null) {
335
- const t = implicitKey ? defaultKeyType : defaultType;
336
- res = _stringify(t);
337
- if (res === null) throw new Error("Unsupported default string type ".concat(t));
338
- }
339
-
340
- return res;
288
+ const _stringify = (_type) => {
289
+ switch (_type) {
290
+ case Scalar.BLOCK_FOLDED:
291
+ case Scalar.BLOCK_LITERAL:
292
+ return implicitKey || inFlow
293
+ ? doubleQuotedString(ss.value, ctx) // blocks are not valid inside flow containers
294
+ : blockString(ss, ctx, onComment, onChompKeep);
295
+ case Scalar.QUOTE_DOUBLE:
296
+ return doubleQuotedString(ss.value, ctx);
297
+ case Scalar.QUOTE_SINGLE:
298
+ return singleQuotedString(ss.value, ctx);
299
+ case Scalar.PLAIN:
300
+ return plainString(ss, ctx, onComment, onChompKeep);
301
+ default:
302
+ return null;
303
+ }
304
+ };
305
+ let res = _stringify(type);
306
+ if (res === null) {
307
+ const { defaultKeyType, defaultStringType } = ctx.options;
308
+ const t = (implicitKey && defaultKeyType) || defaultStringType;
309
+ res = _stringify(t);
310
+ if (res === null)
311
+ throw new Error(`Unsupported default string type ${t}`);
312
+ }
313
+ return res;
341
314
  }
342
315
 
343
316
  export { stringifyString };
@@ -1,6 +1,6 @@
1
+ export { debug, warn } from './log.js';
2
+ export { findPair } from './nodes/YAMLMap.js';
3
+ export { toJS } from './nodes/toJS.js';
4
+ export { foldFlowLines } from './stringify/foldFlowLines.js';
1
5
  export { stringifyNumber } from './stringify/stringifyNumber.js';
2
6
  export { stringifyString } from './stringify/stringifyString.js';
3
- export { Type } from './constants.js';
4
- export { YAMLError, YAMLReferenceError, YAMLSemanticError, YAMLSyntaxError, YAMLWarning } from './errors.js';
5
- export { findPair } from './ast/YAMLMap.js';
6
- export { toJS } from './ast/toJS.js';