Document-9b4560a1.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. 'use strict';
  2. var PlainValue = require('./PlainValue-ec8e588e.js');
  3. var resolveSeq = require('./resolveSeq-d03cb037.js');
  4. var Schema = require('./Schema-88e323a7.js');
  5. const defaultOptions = {
  6. anchorPrefix: 'a',
  7. customTags: null,
  8. indent: 2,
  9. indentSeq: true,
  10. keepCstNodes: false,
  11. keepNodeTypes: true,
  12. keepBlobsInJSON: true,
  13. mapAsMap: false,
  14. maxAliasCount: 100,
  15. prettyErrors: false,
  16. // TODO Set true in v2
  17. simpleKeys: false,
  18. version: '1.2'
  19. };
  20. const scalarOptions = {
  21. get binary() {
  22. return resolveSeq.binaryOptions;
  23. },
  24. set binary(opt) {
  25. Object.assign(resolveSeq.binaryOptions, opt);
  26. },
  27. get bool() {
  28. return resolveSeq.boolOptions;
  29. },
  30. set bool(opt) {
  31. Object.assign(resolveSeq.boolOptions, opt);
  32. },
  33. get int() {
  34. return resolveSeq.intOptions;
  35. },
  36. set int(opt) {
  37. Object.assign(resolveSeq.intOptions, opt);
  38. },
  39. get null() {
  40. return resolveSeq.nullOptions;
  41. },
  42. set null(opt) {
  43. Object.assign(resolveSeq.nullOptions, opt);
  44. },
  45. get str() {
  46. return resolveSeq.strOptions;
  47. },
  48. set str(opt) {
  49. Object.assign(resolveSeq.strOptions, opt);
  50. }
  51. };
  52. const documentOptions = {
  53. '1.0': {
  54. schema: 'yaml-1.1',
  55. merge: true,
  56. tagPrefixes: [{
  57. handle: '!',
  58. prefix: PlainValue.defaultTagPrefix
  59. }, {
  60. handle: '!!',
  61. prefix: 'tag:private.yaml.org,2002:'
  62. }]
  63. },
  64. 1.1: {
  65. schema: 'yaml-1.1',
  66. merge: true,
  67. tagPrefixes: [{
  68. handle: '!',
  69. prefix: '!'
  70. }, {
  71. handle: '!!',
  72. prefix: PlainValue.defaultTagPrefix
  73. }]
  74. },
  75. 1.2: {
  76. schema: 'core',
  77. merge: false,
  78. tagPrefixes: [{
  79. handle: '!',
  80. prefix: '!'
  81. }, {
  82. handle: '!!',
  83. prefix: PlainValue.defaultTagPrefix
  84. }]
  85. }
  86. };
  87. function stringifyTag(doc, tag) {
  88. if ((doc.version || doc.options.version) === '1.0') {
  89. const priv = tag.match(/^tag:private\.yaml\.org,2002:([^:/]+)$/);
  90. if (priv) return '!' + priv[1];
  91. const vocab = tag.match(/^tag:([a-zA-Z0-9-]+)\.yaml\.org,2002:(.*)/);
  92. return vocab ? `!${vocab[1]}/${vocab[2]}` : `!${tag.replace(/^tag:/, '')}`;
  93. }
  94. let p = doc.tagPrefixes.find(p => tag.indexOf(p.prefix) === 0);
  95. if (!p) {
  96. const dtp = doc.getDefaults().tagPrefixes;
  97. p = dtp && dtp.find(p => tag.indexOf(p.prefix) === 0);
  98. }
  99. if (!p) return tag[0] === '!' ? tag : `!<${tag}>`;
  100. const suffix = tag.substr(p.prefix.length).replace(/[!,[\]{}]/g, ch => ({
  101. '!': '%21',
  102. ',': '%2C',
  103. '[': '%5B',
  104. ']': '%5D',
  105. '{': '%7B',
  106. '}': '%7D'
  107. })[ch]);
  108. return p.handle + suffix;
  109. }
  110. function getTagObject(tags, item) {
  111. if (item instanceof resolveSeq.Alias) return resolveSeq.Alias;
  112. if (item.tag) {
  113. const match = tags.filter(t => t.tag === item.tag);
  114. if (match.length > 0) return match.find(t => t.format === item.format) || match[0];
  115. }
  116. let tagObj, obj;
  117. if (item instanceof resolveSeq.Scalar) {
  118. obj = item.value; // TODO: deprecate/remove class check
  119. const match = tags.filter(t => t.identify && t.identify(obj) || t.class && obj instanceof t.class);
  120. tagObj = match.find(t => t.format === item.format) || match.find(t => !t.format);
  121. } else {
  122. obj = item;
  123. tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
  124. }
  125. if (!tagObj) {
  126. const name = obj && obj.constructor ? obj.constructor.name : typeof obj;
  127. throw new Error(`Tag not resolved for ${name} value`);
  128. }
  129. return tagObj;
  130. } // needs to be called before value stringifier to allow for circular anchor refs
  131. function stringifyProps(node, tagObj, {
  132. anchors,
  133. doc
  134. }) {
  135. const props = [];
  136. const anchor = doc.anchors.getName(node);
  137. if (anchor) {
  138. anchors[anchor] = node;
  139. props.push(`&${anchor}`);
  140. }
  141. if (node.tag) {
  142. props.push(stringifyTag(doc, node.tag));
  143. } else if (!tagObj.default) {
  144. props.push(stringifyTag(doc, tagObj.tag));
  145. }
  146. return props.join(' ');
  147. }
  148. function stringify(item, ctx, onComment, onChompKeep) {
  149. const {
  150. anchors,
  151. schema
  152. } = ctx.doc;
  153. let tagObj;
  154. if (!(item instanceof resolveSeq.Node)) {
  155. const createCtx = {
  156. aliasNodes: [],
  157. onTagObj: o => tagObj = o,
  158. prevObjects: new Map()
  159. };
  160. item = schema.createNode(item, true, null, createCtx);
  161. for (const alias of createCtx.aliasNodes) {
  162. alias.source = alias.source.node;
  163. let name = anchors.getName(alias.source);
  164. if (!name) {
  165. name = anchors.newName();
  166. anchors.map[name] = alias.source;
  167. }
  168. }
  169. }
  170. if (item instanceof resolveSeq.Pair) return item.toString(ctx, onComment, onChompKeep);
  171. if (!tagObj) tagObj = getTagObject(schema.tags, item);
  172. const props = stringifyProps(item, tagObj, ctx);
  173. if (props.length > 0) ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1;
  174. const str = typeof tagObj.stringify === 'function' ? tagObj.stringify(item, ctx, onComment, onChompKeep) : item instanceof resolveSeq.Scalar ? resolveSeq.stringifyString(item, ctx, onComment, onChompKeep) : item.toString(ctx, onComment, onChompKeep);
  175. if (!props) return str;
  176. return item instanceof resolveSeq.Scalar || str[0] === '{' || str[0] === '[' ? `${props} ${str}` : `${props}\n${ctx.indent}${str}`;
  177. }
  178. class Anchors {
  179. static validAnchorNode(node) {
  180. return node instanceof resolveSeq.Scalar || node instanceof resolveSeq.YAMLSeq || node instanceof resolveSeq.YAMLMap;
  181. }
  182. constructor(prefix) {
  183. PlainValue._defineProperty(this, "map", Object.create(null));
  184. this.prefix = prefix;
  185. }
  186. createAlias(node, name) {
  187. this.setAnchor(node, name);
  188. return new resolveSeq.Alias(node);
  189. }
  190. createMergePair(...sources) {
  191. const merge = new resolveSeq.Merge();
  192. merge.value.items = sources.map(s => {
  193. if (s instanceof resolveSeq.Alias) {
  194. if (s.source instanceof resolveSeq.YAMLMap) return s;
  195. } else if (s instanceof resolveSeq.YAMLMap) {
  196. return this.createAlias(s);
  197. }
  198. throw new Error('Merge sources must be Map nodes or their Aliases');
  199. });
  200. return merge;
  201. }
  202. getName(node) {
  203. const {
  204. map
  205. } = this;
  206. return Object.keys(map).find(a => map[a] === node);
  207. }
  208. getNames() {
  209. return Object.keys(this.map);
  210. }
  211. getNode(name) {
  212. return this.map[name];
  213. }
  214. newName(prefix) {
  215. if (!prefix) prefix = this.prefix;
  216. const names = Object.keys(this.map);
  217. for (let i = 1; true; ++i) {
  218. const name = `${prefix}${i}`;
  219. if (!names.includes(name)) return name;
  220. }
  221. } // During parsing, map & aliases contain CST nodes
  222. resolveNodes() {
  223. const {
  224. map,
  225. _cstAliases
  226. } = this;
  227. Object.keys(map).forEach(a => {
  228. map[a] = map[a].resolved;
  229. });
  230. _cstAliases.forEach(a => {
  231. a.source = a.source.resolved;
  232. });
  233. delete this._cstAliases;
  234. }
  235. setAnchor(node, name) {
  236. if (node != null && !Anchors.validAnchorNode(node)) {
  237. throw new Error('Anchors may only be set for Scalar, Seq and Map nodes');
  238. }
  239. if (name && /[\x00-\x19\s,[\]{}]/.test(name)) {
  240. throw new Error('Anchor names must not contain whitespace or control characters');
  241. }
  242. const {
  243. map
  244. } = this;
  245. const prev = node && Object.keys(map).find(a => map[a] === node);
  246. if (prev) {
  247. if (!name) {
  248. return prev;
  249. } else if (prev !== name) {
  250. delete map[prev];
  251. map[name] = node;
  252. }
  253. } else {
  254. if (!name) {
  255. if (!node) return null;
  256. name = this.newName();
  257. }
  258. map[name] = node;
  259. }
  260. return name;
  261. }
  262. }
  263. const visit = (node, tags) => {
  264. if (node && typeof node === 'object') {
  265. const {
  266. tag
  267. } = node;
  268. if (node instanceof resolveSeq.Collection) {
  269. if (tag) tags[tag] = true;
  270. node.items.forEach(n => visit(n, tags));
  271. } else if (node instanceof resolveSeq.Pair) {
  272. visit(node.key, tags);
  273. visit(node.value, tags);
  274. } else if (node instanceof resolveSeq.Scalar) {
  275. if (tag) tags[tag] = true;
  276. }
  277. }
  278. return tags;
  279. };
  280. const listTagNames = node => Object.keys(visit(node, {}));
  281. function parseContents(doc, contents) {
  282. const comments = {
  283. before: [],
  284. after: []
  285. };
  286. let body = undefined;
  287. let spaceBefore = false;
  288. for (const node of contents) {
  289. if (node.valueRange) {
  290. if (body !== undefined) {
  291. const msg = 'Document contains trailing content not separated by a ... or --- line';
  292. doc.errors.push(new PlainValue.YAMLSyntaxError(node, msg));
  293. break;
  294. }
  295. const res = resolveSeq.resolveNode(doc, node);
  296. if (spaceBefore) {
  297. res.spaceBefore = true;
  298. spaceBefore = false;
  299. }
  300. body = res;
  301. } else if (node.comment !== null) {
  302. const cc = body === undefined ? comments.before : comments.after;
  303. cc.push(node.comment);
  304. } else if (node.type === PlainValue.Type.BLANK_LINE) {
  305. spaceBefore = true;
  306. if (body === undefined && comments.before.length > 0 && !doc.commentBefore) {
  307. // space-separated comments at start are parsed as document comments
  308. doc.commentBefore = comments.before.join('\n');
  309. comments.before = [];
  310. }
  311. }
  312. }
  313. doc.contents = body || null;
  314. if (!body) {
  315. doc.comment = comments.before.concat(comments.after).join('\n') || null;
  316. } else {
  317. const cb = comments.before.join('\n');
  318. if (cb) {
  319. const cbNode = body instanceof resolveSeq.Collection && body.items[0] ? body.items[0] : body;
  320. cbNode.commentBefore = cbNode.commentBefore ? `${cb}\n${cbNode.commentBefore}` : cb;
  321. }
  322. doc.comment = comments.after.join('\n') || null;
  323. }
  324. }
  325. function resolveTagDirective({
  326. tagPrefixes
  327. }, directive) {
  328. const [handle, prefix] = directive.parameters;
  329. if (!handle || !prefix) {
  330. const msg = 'Insufficient parameters given for %TAG directive';
  331. throw new PlainValue.YAMLSemanticError(directive, msg);
  332. }
  333. if (tagPrefixes.some(p => p.handle === handle)) {
  334. const msg = 'The %TAG directive must only be given at most once per handle in the same document.';
  335. throw new PlainValue.YAMLSemanticError(directive, msg);
  336. }
  337. return {
  338. handle,
  339. prefix
  340. };
  341. }
  342. function resolveYamlDirective(doc, directive) {
  343. let [version] = directive.parameters;
  344. if (directive.name === 'YAML:1.0') version = '1.0';
  345. if (!version) {
  346. const msg = 'Insufficient parameters given for %YAML directive';
  347. throw new PlainValue.YAMLSemanticError(directive, msg);
  348. }
  349. if (!documentOptions[version]) {
  350. const v0 = doc.version || doc.options.version;
  351. const msg = `Document will be parsed as YAML ${v0} rather than YAML ${version}`;
  352. doc.warnings.push(new PlainValue.YAMLWarning(directive, msg));
  353. }
  354. return version;
  355. }
  356. function parseDirectives(doc, directives, prevDoc) {
  357. const directiveComments = [];
  358. let hasDirectives = false;
  359. for (const directive of directives) {
  360. const {
  361. comment,
  362. name
  363. } = directive;
  364. switch (name) {
  365. case 'TAG':
  366. try {
  367. doc.tagPrefixes.push(resolveTagDirective(doc, directive));
  368. } catch (error) {
  369. doc.errors.push(error);
  370. }
  371. hasDirectives = true;
  372. break;
  373. case 'YAML':
  374. case 'YAML:1.0':
  375. if (doc.version) {
  376. const msg = 'The %YAML directive must only be given at most once per document.';
  377. doc.errors.push(new PlainValue.YAMLSemanticError(directive, msg));
  378. }
  379. try {
  380. doc.version = resolveYamlDirective(doc, directive);
  381. } catch (error) {
  382. doc.errors.push(error);
  383. }
  384. hasDirectives = true;
  385. break;
  386. default:
  387. if (name) {
  388. const msg = `YAML only supports %TAG and %YAML directives, and not %${name}`;
  389. doc.warnings.push(new PlainValue.YAMLWarning(directive, msg));
  390. }
  391. }
  392. if (comment) directiveComments.push(comment);
  393. }
  394. if (prevDoc && !hasDirectives && '1.1' === (doc.version || prevDoc.version || doc.options.version)) {
  395. const copyTagPrefix = ({
  396. handle,
  397. prefix
  398. }) => ({
  399. handle,
  400. prefix
  401. });
  402. doc.tagPrefixes = prevDoc.tagPrefixes.map(copyTagPrefix);
  403. doc.version = prevDoc.version;
  404. }
  405. doc.commentBefore = directiveComments.join('\n') || null;
  406. }
  407. function assertCollection(contents) {
  408. if (contents instanceof resolveSeq.Collection) return true;
  409. throw new Error('Expected a YAML collection as document contents');
  410. }
  411. class Document {
  412. constructor(options) {
  413. this.anchors = new Anchors(options.anchorPrefix);
  414. this.commentBefore = null;
  415. this.comment = null;
  416. this.contents = null;
  417. this.directivesEndMarker = null;
  418. this.errors = [];
  419. this.options = options;
  420. this.schema = null;
  421. this.tagPrefixes = [];
  422. this.version = null;
  423. this.warnings = [];
  424. }
  425. add(value) {
  426. assertCollection(this.contents);
  427. return this.contents.add(value);
  428. }
  429. addIn(path, value) {
  430. assertCollection(this.contents);
  431. this.contents.addIn(path, value);
  432. }
  433. delete(key) {
  434. assertCollection(this.contents);
  435. return this.contents.delete(key);
  436. }
  437. deleteIn(path) {
  438. if (resolveSeq.isEmptyPath(path)) {
  439. if (this.contents == null) return false;
  440. this.contents = null;
  441. return true;
  442. }
  443. assertCollection(this.contents);
  444. return this.contents.deleteIn(path);
  445. }
  446. getDefaults() {
  447. return Document.defaults[this.version] || Document.defaults[this.options.version] || {};
  448. }
  449. get(key, keepScalar) {
  450. return this.contents instanceof resolveSeq.Collection ? this.contents.get(key, keepScalar) : undefined;
  451. }
  452. getIn(path, keepScalar) {
  453. if (resolveSeq.isEmptyPath(path)) return !keepScalar && this.contents instanceof resolveSeq.Scalar ? this.contents.value : this.contents;
  454. return this.contents instanceof resolveSeq.Collection ? this.contents.getIn(path, keepScalar) : undefined;
  455. }
  456. has(key) {
  457. return this.contents instanceof resolveSeq.Collection ? this.contents.has(key) : false;
  458. }
  459. hasIn(path) {
  460. if (resolveSeq.isEmptyPath(path)) return this.contents !== undefined;
  461. return this.contents instanceof resolveSeq.Collection ? this.contents.hasIn(path) : false;
  462. }
  463. set(key, value) {
  464. assertCollection(this.contents);
  465. this.contents.set(key, value);
  466. }
  467. setIn(path, value) {
  468. if (resolveSeq.isEmptyPath(path)) this.contents = value;else {
  469. assertCollection(this.contents);
  470. this.contents.setIn(path, value);
  471. }
  472. }
  473. setSchema(id, customTags) {
  474. if (!id && !customTags && this.schema) return;
  475. if (typeof id === 'number') id = id.toFixed(1);
  476. if (id === '1.0' || id === '1.1' || id === '1.2') {
  477. if (this.version) this.version = id;else this.options.version = id;
  478. delete this.options.schema;
  479. } else if (id && typeof id === 'string') {
  480. this.options.schema = id;
  481. }
  482. if (Array.isArray(customTags)) this.options.customTags = customTags;
  483. const opt = Object.assign({}, this.getDefaults(), this.options);
  484. this.schema = new Schema.Schema(opt);
  485. }
  486. parse(node, prevDoc) {
  487. if (this.options.keepCstNodes) this.cstNode = node;
  488. if (this.options.keepNodeTypes) this.type = 'DOCUMENT';
  489. const {
  490. directives = [],
  491. contents = [],
  492. directivesEndMarker,
  493. error,
  494. valueRange
  495. } = node;
  496. if (error) {
  497. if (!error.source) error.source = this;
  498. this.errors.push(error);
  499. }
  500. parseDirectives(this, directives, prevDoc);
  501. if (directivesEndMarker) this.directivesEndMarker = true;
  502. this.range = valueRange ? [valueRange.start, valueRange.end] : null;
  503. this.setSchema();
  504. this.anchors._cstAliases = [];
  505. parseContents(this, contents);
  506. this.anchors.resolveNodes();
  507. if (this.options.prettyErrors) {
  508. for (const error of this.errors) if (error instanceof PlainValue.YAMLError) error.makePretty();
  509. for (const warn of this.warnings) if (warn instanceof PlainValue.YAMLError) warn.makePretty();
  510. }
  511. return this;
  512. }
  513. listNonDefaultTags() {
  514. return listTagNames(this.contents).filter(t => t.indexOf(Schema.Schema.defaultPrefix) !== 0);
  515. }
  516. setTagPrefix(handle, prefix) {
  517. if (handle[0] !== '!' || handle[handle.length - 1] !== '!') throw new Error('Handle must start and end with !');
  518. if (prefix) {
  519. const prev = this.tagPrefixes.find(p => p.handle === handle);
  520. if (prev) prev.prefix = prefix;else this.tagPrefixes.push({
  521. handle,
  522. prefix
  523. });
  524. } else {
  525. this.tagPrefixes = this.tagPrefixes.filter(p => p.handle !== handle);
  526. }
  527. }
  528. toJSON(arg, onAnchor) {
  529. const {
  530. keepBlobsInJSON,
  531. mapAsMap,
  532. maxAliasCount
  533. } = this.options;
  534. const keep = keepBlobsInJSON && (typeof arg !== 'string' || !(this.contents instanceof resolveSeq.Scalar));
  535. const ctx = {
  536. doc: this,
  537. indentStep: ' ',
  538. keep,
  539. mapAsMap: keep && !!mapAsMap,
  540. maxAliasCount,
  541. stringify // Requiring directly in Pair would create circular dependencies
  542. };
  543. const anchorNames = Object.keys(this.anchors.map);
  544. if (anchorNames.length > 0) ctx.anchors = new Map(anchorNames.map(name => [this.anchors.map[name], {
  545. alias: [],
  546. aliasCount: 0,
  547. count: 1
  548. }]));
  549. const res = resolveSeq.toJSON(this.contents, arg, ctx);
  550. if (typeof onAnchor === 'function' && ctx.anchors) for (const {
  551. count,
  552. res
  553. } of ctx.anchors.values()) onAnchor(res, count);
  554. return res;
  555. }
  556. toString() {
  557. if (this.errors.length > 0) throw new Error('Document with errors cannot be stringified');
  558. const indentSize = this.options.indent;
  559. if (!Number.isInteger(indentSize) || indentSize <= 0) {
  560. const s = JSON.stringify(indentSize);
  561. throw new Error(`"indent" option must be a positive integer, not ${s}`);
  562. }
  563. this.setSchema();
  564. const lines = [];
  565. let hasDirectives = false;
  566. if (this.version) {
  567. let vd = '%YAML 1.2';
  568. if (this.schema.name === 'yaml-1.1') {
  569. if (this.version === '1.0') vd = '%YAML:1.0';else if (this.version === '1.1') vd = '%YAML 1.1';
  570. }
  571. lines.push(vd);
  572. hasDirectives = true;
  573. }
  574. const tagNames = this.listNonDefaultTags();
  575. this.tagPrefixes.forEach(({
  576. handle,
  577. prefix
  578. }) => {
  579. if (tagNames.some(t => t.indexOf(prefix) === 0)) {
  580. lines.push(`%TAG ${handle} ${prefix}`);
  581. hasDirectives = true;
  582. }
  583. });
  584. if (hasDirectives || this.directivesEndMarker) lines.push('---');
  585. if (this.commentBefore) {
  586. if (hasDirectives || !this.directivesEndMarker) lines.unshift('');
  587. lines.unshift(this.commentBefore.replace(/^/gm, '#'));
  588. }
  589. const ctx = {
  590. anchors: Object.create(null),
  591. doc: this,
  592. indent: '',
  593. indentStep: ' '.repeat(indentSize),
  594. stringify // Requiring directly in nodes would create circular dependencies
  595. };
  596. let chompKeep = false;
  597. let contentComment = null;
  598. if (this.contents) {
  599. if (this.contents instanceof resolveSeq.Node) {
  600. if (this.contents.spaceBefore && (hasDirectives || this.directivesEndMarker)) lines.push('');
  601. if (this.contents.commentBefore) lines.push(this.contents.commentBefore.replace(/^/gm, '#')); // top-level block scalars need to be indented if followed by a comment
  602. ctx.forceBlockIndent = !!this.comment;
  603. contentComment = this.contents.comment;
  604. }
  605. const onChompKeep = contentComment ? null : () => chompKeep = true;
  606. const body = stringify(this.contents, ctx, () => contentComment = null, onChompKeep);
  607. lines.push(resolveSeq.addComment(body, '', contentComment));
  608. } else if (this.contents !== undefined) {
  609. lines.push(stringify(this.contents, ctx));
  610. }
  611. if (this.comment) {
  612. if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '') lines.push('');
  613. lines.push(this.comment.replace(/^/gm, '#'));
  614. }
  615. return lines.join('\n') + '\n';
  616. }
  617. }
  618. PlainValue._defineProperty(Document, "defaults", documentOptions);
  619. exports.Document = Document;
  620. exports.defaultOptions = defaultOptions;
  621. exports.scalarOptions = scalarOptions;