Aucune description

Document.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. 'use strict';
  2. var Alias = require('../nodes/Alias.js');
  3. var Collection = require('../nodes/Collection.js');
  4. var Node = require('../nodes/Node.js');
  5. var Pair = require('../nodes/Pair.js');
  6. var toJS = require('../nodes/toJS.js');
  7. var Schema = require('../schema/Schema.js');
  8. var stringify = require('../stringify/stringify.js');
  9. var stringifyDocument = require('../stringify/stringifyDocument.js');
  10. var anchors = require('./anchors.js');
  11. var applyReviver = require('./applyReviver.js');
  12. var createNode = require('./createNode.js');
  13. var directives = require('./directives.js');
  14. class Document {
  15. constructor(value, replacer, options) {
  16. /** A comment before this Document */
  17. this.commentBefore = null;
  18. /** A comment immediately after this Document */
  19. this.comment = null;
  20. /** Errors encountered during parsing. */
  21. this.errors = [];
  22. /** Warnings encountered during parsing. */
  23. this.warnings = [];
  24. Object.defineProperty(this, Node.NODE_TYPE, { value: Node.DOC });
  25. let _replacer = null;
  26. if (typeof replacer === 'function' || Array.isArray(replacer)) {
  27. _replacer = replacer;
  28. }
  29. else if (options === undefined && replacer) {
  30. options = replacer;
  31. replacer = undefined;
  32. }
  33. const opt = Object.assign({
  34. intAsBigInt: false,
  35. keepSourceTokens: false,
  36. logLevel: 'warn',
  37. prettyErrors: true,
  38. strict: true,
  39. uniqueKeys: true,
  40. version: '1.2'
  41. }, options);
  42. this.options = opt;
  43. let { version } = opt;
  44. if (options?._directives) {
  45. this.directives = options._directives.atDocument();
  46. if (this.directives.yaml.explicit)
  47. version = this.directives.yaml.version;
  48. }
  49. else
  50. this.directives = new directives.Directives({ version });
  51. this.setSchema(version, options);
  52. if (value === undefined)
  53. this.contents = null;
  54. else {
  55. this.contents = this.createNode(value, _replacer, options);
  56. }
  57. }
  58. /**
  59. * Create a deep copy of this Document and its contents.
  60. *
  61. * Custom Node values that inherit from `Object` still refer to their original instances.
  62. */
  63. clone() {
  64. const copy = Object.create(Document.prototype, {
  65. [Node.NODE_TYPE]: { value: Node.DOC }
  66. });
  67. copy.commentBefore = this.commentBefore;
  68. copy.comment = this.comment;
  69. copy.errors = this.errors.slice();
  70. copy.warnings = this.warnings.slice();
  71. copy.options = Object.assign({}, this.options);
  72. if (this.directives)
  73. copy.directives = this.directives.clone();
  74. copy.schema = this.schema.clone();
  75. copy.contents = Node.isNode(this.contents)
  76. ? this.contents.clone(copy.schema)
  77. : this.contents;
  78. if (this.range)
  79. copy.range = this.range.slice();
  80. return copy;
  81. }
  82. /** Adds a value to the document. */
  83. add(value) {
  84. if (assertCollection(this.contents))
  85. this.contents.add(value);
  86. }
  87. /** Adds a value to the document. */
  88. addIn(path, value) {
  89. if (assertCollection(this.contents))
  90. this.contents.addIn(path, value);
  91. }
  92. /**
  93. * Create a new `Alias` node, ensuring that the target `node` has the required anchor.
  94. *
  95. * If `node` already has an anchor, `name` is ignored.
  96. * Otherwise, the `node.anchor` value will be set to `name`,
  97. * or if an anchor with that name is already present in the document,
  98. * `name` will be used as a prefix for a new unique anchor.
  99. * If `name` is undefined, the generated anchor will use 'a' as a prefix.
  100. */
  101. createAlias(node, name) {
  102. if (!node.anchor) {
  103. const prev = anchors.anchorNames(this);
  104. node.anchor =
  105. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  106. !name || prev.has(name) ? anchors.findNewAnchor(name || 'a', prev) : name;
  107. }
  108. return new Alias.Alias(node.anchor);
  109. }
  110. createNode(value, replacer, options) {
  111. let _replacer = undefined;
  112. if (typeof replacer === 'function') {
  113. value = replacer.call({ '': value }, '', value);
  114. _replacer = replacer;
  115. }
  116. else if (Array.isArray(replacer)) {
  117. const keyToStr = (v) => typeof v === 'number' || v instanceof String || v instanceof Number;
  118. const asStr = replacer.filter(keyToStr).map(String);
  119. if (asStr.length > 0)
  120. replacer = replacer.concat(asStr);
  121. _replacer = replacer;
  122. }
  123. else if (options === undefined && replacer) {
  124. options = replacer;
  125. replacer = undefined;
  126. }
  127. const { aliasDuplicateObjects, anchorPrefix, flow, keepUndefined, onTagObj, tag } = options ?? {};
  128. const { onAnchor, setAnchors, sourceObjects } = anchors.createNodeAnchors(this,
  129. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  130. anchorPrefix || 'a');
  131. const ctx = {
  132. aliasDuplicateObjects: aliasDuplicateObjects ?? true,
  133. keepUndefined: keepUndefined ?? false,
  134. onAnchor,
  135. onTagObj,
  136. replacer: _replacer,
  137. schema: this.schema,
  138. sourceObjects
  139. };
  140. const node = createNode.createNode(value, tag, ctx);
  141. if (flow && Node.isCollection(node))
  142. node.flow = true;
  143. setAnchors();
  144. return node;
  145. }
  146. /**
  147. * Convert a key and a value into a `Pair` using the current schema,
  148. * recursively wrapping all values as `Scalar` or `Collection` nodes.
  149. */
  150. createPair(key, value, options = {}) {
  151. const k = this.createNode(key, null, options);
  152. const v = this.createNode(value, null, options);
  153. return new Pair.Pair(k, v);
  154. }
  155. /**
  156. * Removes a value from the document.
  157. * @returns `true` if the item was found and removed.
  158. */
  159. delete(key) {
  160. return assertCollection(this.contents) ? this.contents.delete(key) : false;
  161. }
  162. /**
  163. * Removes a value from the document.
  164. * @returns `true` if the item was found and removed.
  165. */
  166. deleteIn(path) {
  167. if (Collection.isEmptyPath(path)) {
  168. if (this.contents == null)
  169. return false;
  170. this.contents = null;
  171. return true;
  172. }
  173. return assertCollection(this.contents)
  174. ? this.contents.deleteIn(path)
  175. : false;
  176. }
  177. /**
  178. * Returns item at `key`, or `undefined` if not found. By default unwraps
  179. * scalar values from their surrounding node; to disable set `keepScalar` to
  180. * `true` (collections are always returned intact).
  181. */
  182. get(key, keepScalar) {
  183. return Node.isCollection(this.contents)
  184. ? this.contents.get(key, keepScalar)
  185. : undefined;
  186. }
  187. /**
  188. * Returns item at `path`, or `undefined` if not found. By default unwraps
  189. * scalar values from their surrounding node; to disable set `keepScalar` to
  190. * `true` (collections are always returned intact).
  191. */
  192. getIn(path, keepScalar) {
  193. if (Collection.isEmptyPath(path))
  194. return !keepScalar && Node.isScalar(this.contents)
  195. ? this.contents.value
  196. : this.contents;
  197. return Node.isCollection(this.contents)
  198. ? this.contents.getIn(path, keepScalar)
  199. : undefined;
  200. }
  201. /**
  202. * Checks if the document includes a value with the key `key`.
  203. */
  204. has(key) {
  205. return Node.isCollection(this.contents) ? this.contents.has(key) : false;
  206. }
  207. /**
  208. * Checks if the document includes a value at `path`.
  209. */
  210. hasIn(path) {
  211. if (Collection.isEmptyPath(path))
  212. return this.contents !== undefined;
  213. return Node.isCollection(this.contents) ? this.contents.hasIn(path) : false;
  214. }
  215. /**
  216. * Sets a value in this document. For `!!set`, `value` needs to be a
  217. * boolean to add/remove the item from the set.
  218. */
  219. set(key, value) {
  220. if (this.contents == null) {
  221. this.contents = Collection.collectionFromPath(this.schema, [key], value);
  222. }
  223. else if (assertCollection(this.contents)) {
  224. this.contents.set(key, value);
  225. }
  226. }
  227. /**
  228. * Sets a value in this document. For `!!set`, `value` needs to be a
  229. * boolean to add/remove the item from the set.
  230. */
  231. setIn(path, value) {
  232. if (Collection.isEmptyPath(path))
  233. this.contents = value;
  234. else if (this.contents == null) {
  235. this.contents = Collection.collectionFromPath(this.schema, Array.from(path), value);
  236. }
  237. else if (assertCollection(this.contents)) {
  238. this.contents.setIn(path, value);
  239. }
  240. }
  241. /**
  242. * Change the YAML version and schema used by the document.
  243. * A `null` version disables support for directives, explicit tags, anchors, and aliases.
  244. * It also requires the `schema` option to be given as a `Schema` instance value.
  245. *
  246. * Overrides all previously set schema options.
  247. */
  248. setSchema(version, options = {}) {
  249. if (typeof version === 'number')
  250. version = String(version);
  251. let opt;
  252. switch (version) {
  253. case '1.1':
  254. if (this.directives)
  255. this.directives.yaml.version = '1.1';
  256. else
  257. this.directives = new directives.Directives({ version: '1.1' });
  258. opt = { merge: true, resolveKnownTags: false, schema: 'yaml-1.1' };
  259. break;
  260. case '1.2':
  261. case 'next':
  262. if (this.directives)
  263. this.directives.yaml.version = version;
  264. else
  265. this.directives = new directives.Directives({ version });
  266. opt = { merge: false, resolveKnownTags: true, schema: 'core' };
  267. break;
  268. case null:
  269. if (this.directives)
  270. delete this.directives;
  271. opt = null;
  272. break;
  273. default: {
  274. const sv = JSON.stringify(version);
  275. throw new Error(`Expected '1.1', '1.2' or null as first argument, but found: ${sv}`);
  276. }
  277. }
  278. // Not using `instanceof Schema` to allow for duck typing
  279. if (options.schema instanceof Object)
  280. this.schema = options.schema;
  281. else if (opt)
  282. this.schema = new Schema.Schema(Object.assign(opt, options));
  283. else
  284. throw new Error(`With a null YAML version, the { schema: Schema } option is required`);
  285. }
  286. // json & jsonArg are only used from toJSON()
  287. toJS({ json, jsonArg, mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
  288. const ctx = {
  289. anchors: new Map(),
  290. doc: this,
  291. keep: !json,
  292. mapAsMap: mapAsMap === true,
  293. mapKeyWarned: false,
  294. maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100,
  295. stringify: stringify.stringify
  296. };
  297. const res = toJS.toJS(this.contents, jsonArg ?? '', ctx);
  298. if (typeof onAnchor === 'function')
  299. for (const { count, res } of ctx.anchors.values())
  300. onAnchor(res, count);
  301. return typeof reviver === 'function'
  302. ? applyReviver.applyReviver(reviver, { '': res }, '', res)
  303. : res;
  304. }
  305. /**
  306. * A JSON representation of the document `contents`.
  307. *
  308. * @param jsonArg Used by `JSON.stringify` to indicate the array index or
  309. * property name.
  310. */
  311. toJSON(jsonArg, onAnchor) {
  312. return this.toJS({ json: true, jsonArg, mapAsMap: false, onAnchor });
  313. }
  314. /** A YAML representation of the document. */
  315. toString(options = {}) {
  316. if (this.errors.length > 0)
  317. throw new Error('Document with errors cannot be stringified');
  318. if ('indent' in options &&
  319. (!Number.isInteger(options.indent) || Number(options.indent) <= 0)) {
  320. const s = JSON.stringify(options.indent);
  321. throw new Error(`"indent" option must be a positive integer, not ${s}`);
  322. }
  323. return stringifyDocument.stringifyDocument(this, options);
  324. }
  325. }
  326. function assertCollection(contents) {
  327. if (Node.isCollection(contents))
  328. return true;
  329. throw new Error('Expected a YAML collection as document contents');
  330. }
  331. exports.Document = Document;