yaml 2.0.0-7 → 2.0.0

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 (123) hide show
  1. package/README.md +1 -1
  2. package/browser/dist/compose/compose-doc.js +3 -2
  3. package/browser/dist/compose/compose-node.js +14 -3
  4. package/browser/dist/compose/compose-scalar.js +17 -10
  5. package/browser/dist/compose/composer.js +6 -7
  6. package/browser/dist/compose/resolve-block-map.js +19 -5
  7. package/browser/dist/compose/resolve-block-scalar.js +12 -3
  8. package/browser/dist/compose/resolve-block-seq.js +5 -0
  9. package/browser/dist/compose/resolve-flow-collection.js +18 -6
  10. package/browser/dist/compose/resolve-flow-scalar.js +9 -2
  11. package/browser/dist/compose/resolve-props.js +4 -0
  12. package/browser/dist/compose/util-flow-indent-check.js +15 -0
  13. package/browser/dist/doc/Document.js +73 -16
  14. package/browser/dist/doc/anchors.js +2 -2
  15. package/browser/dist/doc/createNode.js +12 -7
  16. package/browser/dist/doc/directives.js +11 -3
  17. package/browser/dist/index.js +1 -2
  18. package/browser/dist/node_modules/tslib/tslib.es6.js +164 -0
  19. package/browser/dist/nodes/Collection.js +28 -15
  20. package/browser/dist/nodes/Node.js +7 -0
  21. package/browser/dist/nodes/Pair.js +11 -3
  22. package/browser/dist/nodes/Scalar.js +1 -1
  23. package/browser/dist/nodes/YAMLMap.js +6 -5
  24. package/browser/dist/nodes/YAMLSeq.js +2 -2
  25. package/browser/dist/nodes/addPairToJSMap.js +3 -2
  26. package/browser/dist/nodes/toJS.js +3 -1
  27. package/browser/dist/parse/cst-scalar.js +2 -6
  28. package/browser/dist/parse/cst-visit.js +2 -2
  29. package/browser/dist/parse/lexer.js +50 -22
  30. package/browser/dist/parse/parser.js +121 -45
  31. package/browser/dist/public-api.js +4 -5
  32. package/browser/dist/schema/Schema.js +19 -4
  33. package/browser/dist/schema/common/null.js +3 -1
  34. package/browser/dist/schema/core/float.js +4 -1
  35. package/browser/dist/schema/tags.js +17 -13
  36. package/browser/dist/schema/yaml-1.1/float.js +4 -1
  37. package/browser/dist/schema/yaml-1.1/omap.js +1 -1
  38. package/browser/dist/schema/yaml-1.1/pairs.js +2 -1
  39. package/browser/dist/stringify/stringify.js +53 -23
  40. package/browser/dist/stringify/stringifyCollection.js +115 -85
  41. package/browser/dist/stringify/stringifyComment.js +18 -12
  42. package/browser/dist/stringify/stringifyDocument.js +37 -15
  43. package/browser/dist/stringify/stringifyPair.js +35 -21
  44. package/browser/dist/stringify/stringifyString.js +41 -43
  45. package/browser/dist/util.js +3 -0
  46. package/browser/dist/visit.js +150 -62
  47. package/dist/compose/compose-doc.js +3 -2
  48. package/dist/compose/compose-node.d.ts +1 -0
  49. package/dist/compose/compose-node.js +14 -3
  50. package/dist/compose/compose-scalar.js +16 -9
  51. package/dist/compose/composer.d.ts +1 -1
  52. package/dist/compose/composer.js +8 -9
  53. package/dist/compose/resolve-block-map.js +19 -5
  54. package/dist/compose/resolve-block-scalar.js +12 -3
  55. package/dist/compose/resolve-block-seq.js +5 -0
  56. package/dist/compose/resolve-flow-collection.js +18 -6
  57. package/dist/compose/resolve-flow-scalar.js +9 -2
  58. package/dist/compose/resolve-props.d.ts +1 -1
  59. package/dist/compose/resolve-props.js +4 -0
  60. package/dist/compose/util-flow-indent-check.d.ts +3 -0
  61. package/dist/compose/util-flow-indent-check.js +17 -0
  62. package/dist/doc/Document.d.ts +13 -4
  63. package/dist/doc/Document.js +79 -22
  64. package/dist/doc/anchors.d.ts +2 -2
  65. package/dist/doc/anchors.js +2 -2
  66. package/dist/doc/createNode.d.ts +3 -2
  67. package/dist/doc/createNode.js +11 -6
  68. package/dist/doc/directives.d.ts +5 -2
  69. package/dist/doc/directives.js +11 -3
  70. package/dist/index.d.ts +4 -4
  71. package/dist/index.js +1 -2
  72. package/dist/node_modules/tslib/tslib.es6.js +76 -0
  73. package/dist/nodes/Alias.d.ts +7 -3
  74. package/dist/nodes/Collection.d.ts +10 -4
  75. package/dist/nodes/Collection.js +27 -14
  76. package/dist/nodes/Node.d.ts +6 -1
  77. package/dist/nodes/Node.js +7 -0
  78. package/dist/nodes/Pair.d.ts +7 -2
  79. package/dist/nodes/Pair.js +10 -2
  80. package/dist/nodes/Scalar.d.ts +2 -0
  81. package/dist/nodes/Scalar.js +1 -1
  82. package/dist/nodes/YAMLMap.d.ts +5 -1
  83. package/dist/nodes/YAMLMap.js +6 -5
  84. package/dist/nodes/YAMLSeq.d.ts +2 -0
  85. package/dist/nodes/YAMLSeq.js +2 -2
  86. package/dist/nodes/addPairToJSMap.js +2 -1
  87. package/dist/nodes/toJS.js +3 -1
  88. package/dist/options.d.ts +67 -15
  89. package/dist/parse/cst-scalar.d.ts +6 -0
  90. package/dist/parse/cst-scalar.js +2 -6
  91. package/dist/parse/cst-visit.js +2 -2
  92. package/dist/parse/lexer.d.ts +2 -0
  93. package/dist/parse/lexer.js +50 -22
  94. package/dist/parse/parser.js +121 -45
  95. package/dist/public-api.js +4 -5
  96. package/dist/schema/Schema.d.ts +6 -4
  97. package/dist/schema/Schema.js +18 -3
  98. package/dist/schema/common/null.js +3 -1
  99. package/dist/schema/core/float.js +4 -1
  100. package/dist/schema/tags.d.ts +1 -2
  101. package/dist/schema/tags.js +17 -13
  102. package/dist/schema/yaml-1.1/float.js +4 -1
  103. package/dist/schema/yaml-1.1/omap.js +1 -1
  104. package/dist/schema/yaml-1.1/pairs.js +2 -1
  105. package/dist/stringify/stringify.d.ts +5 -3
  106. package/dist/stringify/stringify.js +53 -23
  107. package/dist/stringify/stringifyCollection.d.ts +2 -6
  108. package/dist/stringify/stringifyCollection.js +114 -84
  109. package/dist/stringify/stringifyComment.d.ts +10 -2
  110. package/dist/stringify/stringifyComment.js +19 -12
  111. package/dist/stringify/stringifyDocument.js +36 -14
  112. package/dist/stringify/stringifyPair.js +34 -20
  113. package/dist/stringify/stringifyString.js +41 -43
  114. package/dist/test-events.d.ts +1 -1
  115. package/dist/test-events.js +13 -12
  116. package/dist/util.d.ts +3 -0
  117. package/dist/util.js +6 -0
  118. package/dist/visit.d.ts +49 -10
  119. package/dist/visit.js +149 -60
  120. package/package.json +12 -10
  121. package/util.d.ts +3 -0
  122. package/browser/dist/options.js +0 -17
  123. package/dist/options.js +0 -19
package/dist/visit.js CHANGED
@@ -36,29 +36,14 @@ const REMOVE = Symbol('remove node');
36
36
  * specific defined one will be used for each node.
37
37
  */
38
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
- }
39
+ const visitor_ = initVisitor(visitor);
55
40
  if (Node.isDocument(node)) {
56
- const cd = _visit(null, node.contents, visitor, Object.freeze([node]));
41
+ const cd = visit_(null, node.contents, visitor_, Object.freeze([node]));
57
42
  if (cd === REMOVE)
58
43
  node.contents = null;
59
44
  }
60
45
  else
61
- _visit(null, node, visitor, Object.freeze([]));
46
+ visit_(null, node, visitor_, Object.freeze([]));
62
47
  }
63
48
  // Without the `as symbol` casts, TS declares these in the `visit`
64
49
  // namespace using `var`, but then complains about that because
@@ -69,55 +54,104 @@ visit.BREAK = BREAK;
69
54
  visit.SKIP = SKIP;
70
55
  /** Remove the current node */
71
56
  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);
57
+ function visit_(key, node, visitor, path) {
58
+ const ctrl = callVisitor(key, node, visitor, path);
59
+ if (Node.isNode(ctrl) || Node.isPair(ctrl)) {
60
+ replaceNode(key, path, ctrl);
61
+ return visit_(key, ctrl, visitor, path);
87
62
  }
88
- else if (Node.isScalar(node)) {
89
- if (visitor.Scalar)
90
- ctrl = visitor.Scalar(key, node, path);
63
+ if (typeof ctrl !== 'symbol') {
64
+ if (Node.isCollection(node)) {
65
+ path = Object.freeze(path.concat(node));
66
+ for (let i = 0; i < node.items.length; ++i) {
67
+ const ci = visit_(i, node.items[i], visitor, path);
68
+ if (typeof ci === 'number')
69
+ i = ci - 1;
70
+ else if (ci === BREAK)
71
+ return BREAK;
72
+ else if (ci === REMOVE) {
73
+ node.items.splice(i, 1);
74
+ i -= 1;
75
+ }
76
+ }
77
+ }
78
+ else if (Node.isPair(node)) {
79
+ path = Object.freeze(path.concat(node));
80
+ const ck = visit_('key', node.key, visitor, path);
81
+ if (ck === BREAK)
82
+ return BREAK;
83
+ else if (ck === REMOVE)
84
+ node.key = null;
85
+ const cv = visit_('value', node.value, visitor, path);
86
+ if (cv === BREAK)
87
+ return BREAK;
88
+ else if (cv === REMOVE)
89
+ node.value = null;
90
+ }
91
91
  }
92
- else if (Node.isAlias(node)) {
93
- if (visitor.Alias)
94
- ctrl = visitor.Alias(key, node, path);
92
+ return ctrl;
93
+ }
94
+ /**
95
+ * Apply an async visitor to an AST node or document.
96
+ *
97
+ * Walks through the tree (depth-first) starting from `node`, calling a
98
+ * `visitor` function with three arguments:
99
+ * - `key`: For sequence values and map `Pair`, the node's index in the
100
+ * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
101
+ * `null` for the root node.
102
+ * - `node`: The current node.
103
+ * - `path`: The ancestry of the current node.
104
+ *
105
+ * The return value of the visitor may be used to control the traversal:
106
+ * - `Promise`: Must resolve to one of the following values
107
+ * - `undefined` (default): Do nothing and continue
108
+ * - `visit.SKIP`: Do not visit the children of this node, continue with next
109
+ * sibling
110
+ * - `visit.BREAK`: Terminate traversal completely
111
+ * - `visit.REMOVE`: Remove the current node, then continue with the next one
112
+ * - `Node`: Replace the current node, then continue by visiting it
113
+ * - `number`: While iterating the items of a sequence or map, set the index
114
+ * of the next step. This is useful especially if the index of the current
115
+ * node has changed.
116
+ *
117
+ * If `visitor` is a single function, it will be called with all values
118
+ * encountered in the tree, including e.g. `null` values. Alternatively,
119
+ * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
120
+ * `Alias` and `Scalar` node. To define the same visitor function for more than
121
+ * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
122
+ * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
123
+ * specific defined one will be used for each node.
124
+ */
125
+ async function visitAsync(node, visitor) {
126
+ const visitor_ = initVisitor(visitor);
127
+ if (Node.isDocument(node)) {
128
+ const cd = await visitAsync_(null, node.contents, visitor_, Object.freeze([node]));
129
+ if (cd === REMOVE)
130
+ node.contents = null;
95
131
  }
132
+ else
133
+ await visitAsync_(null, node, visitor_, Object.freeze([]));
134
+ }
135
+ // Without the `as symbol` casts, TS declares these in the `visit`
136
+ // namespace using `var`, but then complains about that because
137
+ // `unique symbol` must be `const`.
138
+ /** Terminate visit traversal completely */
139
+ visitAsync.BREAK = BREAK;
140
+ /** Do not visit the children of the current node */
141
+ visitAsync.SKIP = SKIP;
142
+ /** Remove the current node */
143
+ visitAsync.REMOVE = REMOVE;
144
+ async function visitAsync_(key, node, visitor, path) {
145
+ const ctrl = await callVisitor(key, node, visitor, path);
96
146
  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);
147
+ replaceNode(key, path, ctrl);
148
+ return visitAsync_(key, ctrl, visitor, path);
115
149
  }
116
150
  if (typeof ctrl !== 'symbol') {
117
151
  if (Node.isCollection(node)) {
118
152
  path = Object.freeze(path.concat(node));
119
153
  for (let i = 0; i < node.items.length; ++i) {
120
- const ci = _visit(i, node.items[i], visitor, path);
154
+ const ci = await visitAsync_(i, node.items[i], visitor, path);
121
155
  if (typeof ci === 'number')
122
156
  i = ci - 1;
123
157
  else if (ci === BREAK)
@@ -130,12 +164,12 @@ function _visit(key, node, visitor, path) {
130
164
  }
131
165
  else if (Node.isPair(node)) {
132
166
  path = Object.freeze(path.concat(node));
133
- const ck = _visit('key', node.key, visitor, path);
167
+ const ck = await visitAsync_('key', node.key, visitor, path);
134
168
  if (ck === BREAK)
135
169
  return BREAK;
136
170
  else if (ck === REMOVE)
137
171
  node.key = null;
138
- const cv = _visit('value', node.value, visitor, path);
172
+ const cv = await visitAsync_('value', node.value, visitor, path);
139
173
  if (cv === BREAK)
140
174
  return BREAK;
141
175
  else if (cv === REMOVE)
@@ -144,5 +178,60 @@ function _visit(key, node, visitor, path) {
144
178
  }
145
179
  return ctrl;
146
180
  }
181
+ function initVisitor(visitor) {
182
+ if (typeof visitor === 'object' &&
183
+ (visitor.Collection || visitor.Node || visitor.Value)) {
184
+ return Object.assign({
185
+ Alias: visitor.Node,
186
+ Map: visitor.Node,
187
+ Scalar: visitor.Node,
188
+ Seq: visitor.Node
189
+ }, visitor.Value && {
190
+ Map: visitor.Value,
191
+ Scalar: visitor.Value,
192
+ Seq: visitor.Value
193
+ }, visitor.Collection && {
194
+ Map: visitor.Collection,
195
+ Seq: visitor.Collection
196
+ }, visitor);
197
+ }
198
+ return visitor;
199
+ }
200
+ function callVisitor(key, node, visitor, path) {
201
+ var _a, _b, _c, _d, _e;
202
+ if (typeof visitor === 'function')
203
+ return visitor(key, node, path);
204
+ if (Node.isMap(node))
205
+ return (_a = visitor.Map) === null || _a === void 0 ? void 0 : _a.call(visitor, key, node, path);
206
+ if (Node.isSeq(node))
207
+ return (_b = visitor.Seq) === null || _b === void 0 ? void 0 : _b.call(visitor, key, node, path);
208
+ if (Node.isPair(node))
209
+ return (_c = visitor.Pair) === null || _c === void 0 ? void 0 : _c.call(visitor, key, node, path);
210
+ if (Node.isScalar(node))
211
+ return (_d = visitor.Scalar) === null || _d === void 0 ? void 0 : _d.call(visitor, key, node, path);
212
+ if (Node.isAlias(node))
213
+ return (_e = visitor.Alias) === null || _e === void 0 ? void 0 : _e.call(visitor, key, node, path);
214
+ return undefined;
215
+ }
216
+ function replaceNode(key, path, node) {
217
+ const parent = path[path.length - 1];
218
+ if (Node.isCollection(parent)) {
219
+ parent.items[key] = node;
220
+ }
221
+ else if (Node.isPair(parent)) {
222
+ if (key === 'key')
223
+ parent.key = node;
224
+ else
225
+ parent.value = node;
226
+ }
227
+ else if (Node.isDocument(parent)) {
228
+ parent.contents = node;
229
+ }
230
+ else {
231
+ const pt = Node.isAlias(parent) ? 'alias' : 'scalar';
232
+ throw new Error(`Cannot replace node with ${pt} parent`);
233
+ }
234
+ }
147
235
 
148
236
  exports.visit = visit;
237
+ exports.visitAsync = visitAsync;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml",
3
- "version": "2.0.0-7",
3
+ "version": "2.0.0",
4
4
  "license": "ISC",
5
5
  "author": "Eemeli Aro <eemeli@gmail.com>",
6
6
  "repository": "github:eemeli/yaml",
@@ -14,6 +14,7 @@
14
14
  "files": [
15
15
  "browser/",
16
16
  "dist/",
17
+ "util.d.ts",
17
18
  "util.js"
18
19
  ],
19
20
  "type": "commonjs",
@@ -44,9 +45,10 @@
44
45
  "prestart": "npm run build:node",
45
46
  "start": "node -i -e 'YAML=require(\"./dist/index.js\")'",
46
47
  "test": "jest --config config/jest.config.js",
48
+ "test:all": "npm test && npm run test:types && npm run test:dist && npm run test:dist:types",
47
49
  "test:browsers": "cd playground && npm test",
48
50
  "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",
51
+ "test:dist:types": "tsc --allowJs --moduleResolution node --noEmit --target es5 dist/index.js",
50
52
  "test:types": "tsc --noEmit",
51
53
  "docs:install": "cd docs-slate && bundle install",
52
54
  "docs:deploy": "cd docs-slate && ./deploy.sh",
@@ -68,15 +70,15 @@
68
70
  "@babel/plugin-transform-typescript": "^7.12.17",
69
71
  "@babel/preset-env": "^7.12.11",
70
72
  "@rollup/plugin-babel": "^5.2.3",
71
- "@rollup/plugin-replace": "^2.3.4",
73
+ "@rollup/plugin-replace": "^4.0.0",
72
74
  "@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",
75
+ "@types/jest": "^27.0.1",
76
+ "@types/node": "^12.20.47",
77
+ "@typescript-eslint/eslint-plugin": "^5.3.1",
78
+ "@typescript-eslint/parser": "^5.3.1",
77
79
  "babel-jest": "^27.0.1",
78
80
  "cross-env": "^7.0.3",
79
- "eslint": "^7.20.0",
81
+ "eslint": "^8.2.0",
80
82
  "eslint-config-prettier": "^8.1.0",
81
83
  "fast-check": "^2.12.0",
82
84
  "jest": "^27.0.1",
@@ -84,9 +86,9 @@
84
86
  "prettier": "^2.2.1",
85
87
  "rollup": "^2.38.2",
86
88
  "tslib": "^2.1.0",
87
- "typescript": "^4.1.3"
89
+ "typescript": "^4.3.5"
88
90
  },
89
91
  "engines": {
90
- "node": ">= 12"
92
+ "node": ">= 14"
91
93
  }
92
94
  }
package/util.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ // Workaround for incomplete exports support in TypeScript
2
+ // https://github.com/microsoft/TypeScript/issues/33079
3
+ export * from './dist/util.js'
@@ -1,17 +0,0 @@
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
- */
8
- const defaultOptions = {
9
- intAsBigInt: false,
10
- logLevel: 'warn',
11
- prettyErrors: true,
12
- strict: true,
13
- uniqueKeys: true,
14
- version: '1.2'
15
- };
16
-
17
- export { defaultOptions };
package/dist/options.js DELETED
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * `yaml` defines document-specific options in three places: as an argument of
5
- * parse, create and stringify calls, in the values of `YAML.defaultOptions`,
6
- * and in the version-dependent `YAML.Document.defaults` object. Values set in
7
- * `YAML.defaultOptions` override version-dependent defaults, and argument
8
- * options override both.
9
- */
10
- const defaultOptions = {
11
- intAsBigInt: false,
12
- logLevel: 'warn',
13
- prettyErrors: true,
14
- strict: true,
15
- uniqueKeys: true,
16
- version: '1.2'
17
- };
18
-
19
- exports.defaultOptions = defaultOptions;