diff --git a/dist/index.js b/dist/index.js index bfc130109..7fdc6537c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -13569,13 +13569,13 @@ function findPair(items, key) { return undefined; } class YAMLMap extends Collection.Collection { + static get tagName() { + return 'tag:yaml.org,2002:map'; + } constructor(schema) { super(Node.MAP, schema); this.items = []; } - static get tagName() { - return 'tag:yaml.org,2002:map'; - } /** * Adds a value to the collection. * @@ -13683,13 +13683,13 @@ var Scalar = __nccwpck_require__(9338); var toJS = __nccwpck_require__(2463); class YAMLSeq extends Collection.Collection { + static get tagName() { + return 'tag:yaml.org,2002:seq'; + } constructor(schema) { super(Node.SEQ, schema); this.items = []; } - static get tagName() { - return 'tag:yaml.org,2002:seq'; - } add(value) { this.items.push(value); } @@ -17681,6 +17681,7 @@ function createStringifyContext(doc, options) { doubleQuotedAsJSON: false, doubleQuotedMinMultiLineLength: 40, falseStr: 'false', + flowCollectionPadding: true, indentSeq: true, lineWidth: 80, minContentWidth: 20, @@ -17704,6 +17705,7 @@ function createStringifyContext(doc, options) { return { anchors: new Set(), doc, + flowCollectionPadding: opt.flowCollectionPadding ? ' ' : '', indent: '', indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ', inFlow, @@ -17861,7 +17863,7 @@ function stringifyBlockCollection({ comment, items }, ctx, { blockItemPrefix, fl return str; } function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemIndent, onComment }) { - const { indent, indentStep, options: { commentString } } = ctx; + const { indent, indentStep, flowCollectionPadding: fcPadding, options: { commentString } } = ctx; itemIndent += indentStep; const itemCtx = Object.assign({}, ctx, { indent: itemIndent, @@ -17930,7 +17932,7 @@ function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemInden str += `\n${indent}${end}`; } else { - str = `${start} ${lines.join(' ')} ${end}`; + str = `${start}${fcPadding}${lines.join(' ')}${fcPadding}${end}`; } } if (comment) { @@ -18186,19 +18188,18 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) { if (keyComment) str += stringifyComment.lineComment(str, ctx.indent, commentString(keyComment)); } - let vcb = ''; - let valueComment = null; + let vsb, vcb, valueComment; if (Node.isNode(value)) { - if (value.spaceBefore) - vcb = '\n'; - if (value.commentBefore) { - const cs = commentString(value.commentBefore); - vcb += `\n${stringifyComment.indentComment(cs, ctx.indent)}`; - } + vsb = !!value.spaceBefore; + vcb = value.commentBefore; valueComment = value.comment; } - else if (value && typeof value === 'object') { - value = doc.createNode(value); + else { + vsb = false; + vcb = null; + valueComment = null; + if (value && typeof value === 'object') + value = doc.createNode(value); } ctx.implicitKey = false; if (!explicitKey && !keyComment && Node.isScalar(value)) @@ -18213,24 +18214,50 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) { !value.tag && !value.anchor) { // If indentSeq === false, consider '- ' as part of indentation where possible - ctx.indent = ctx.indent.substr(2); + ctx.indent = ctx.indent.substring(2); } let valueCommentDone = false; const valueStr = stringify.stringify(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true)); let ws = ' '; - if (vcb || keyComment) { - if (valueStr === '' && !ctx.inFlow) - ws = vcb === '\n' ? '\n\n' : vcb; - else - ws = `${vcb}\n${ctx.indent}`; + if (keyComment || vsb || vcb) { + ws = vsb ? '\n' : ''; + if (vcb) { + const cs = commentString(vcb); + ws += `\n${stringifyComment.indentComment(cs, ctx.indent)}`; + } + if (valueStr === '' && !ctx.inFlow) { + if (ws === '\n') + ws = '\n\n'; + } + else { + ws += `\n${ctx.indent}`; + } } else if (!explicitKey && Node.isCollection(value)) { - const flow = valueStr[0] === '[' || valueStr[0] === '{'; - if (!flow || valueStr.includes('\n')) - ws = `\n${ctx.indent}`; + const vs0 = valueStr[0]; + const nl0 = valueStr.indexOf('\n'); + const hasNewline = nl0 !== -1; + const flow = ctx.inFlow ?? value.flow ?? value.items.length === 0; + if (hasNewline || !flow) { + let hasPropsLine = false; + if (hasNewline && (vs0 === '&' || vs0 === '!')) { + let sp0 = valueStr.indexOf(' '); + if (vs0 === '&' && + sp0 !== -1 && + sp0 < nl0 && + valueStr[sp0 + 1] === '!') { + sp0 = valueStr.indexOf(' ', sp0 + 1); + } + if (sp0 === -1 || nl0 < sp0) + hasPropsLine = true; + } + if (!hasPropsLine) + ws = `\n${ctx.indent}`; + } } - else if (valueStr === '' || valueStr[0] === '\n') + else if (valueStr === '' || valueStr[0] === '\n') { ws = ''; + } str += ws + valueStr; if (ctx.inFlow) { if (valueCommentDone && onComment) @@ -18488,7 +18515,7 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) { } function plainString(item, ctx, onComment, onChompKeep) { const { type, value } = item; - const { actualString, implicitKey, indent, inFlow } = ctx; + const { actualString, implicitKey, indent, indentStep, inFlow } = ctx; if ((implicitKey && /[\n[\]{},]/.test(value)) || (inFlow && /[[\]{},]/.test(value))) { return quotedString(value, ctx); @@ -18512,9 +18539,14 @@ function plainString(item, ctx, onComment, onChompKeep) { // Where allowed & type not set explicitly, prefer block style for multiline strings return blockString(item, ctx, onComment, onChompKeep); } - if (indent === '' && containsDocumentMarker(value)) { - ctx.forceBlockIndent = true; - return blockString(item, ctx, onComment, onChompKeep); + if (containsDocumentMarker(value)) { + if (indent === '') { + ctx.forceBlockIndent = true; + return blockString(item, ctx, onComment, onChompKeep); + } + else if (implicitKey && indent === indentStep) { + return quotedString(value, ctx); + } } const str = value.replace(/\n+/g, `$&\n${indent}`); // Verify that output will be parsed as a string, as e.g. plain numbers and diff --git a/package-lock.json b/package-lock.json index 0d087e48f..0e45b3afd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,7 +31,7 @@ "ts-jest": "^26.5.6", "ts-node": "^10.9.1", "typescript": "^4.9.4", - "yaml": "^2.1.3", + "yaml": "^2.2.1", "yargs": "^17.6.2" }, "engines": { @@ -8766,9 +8766,9 @@ "dev": true }, "node_modules/yaml": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.1.3.tgz", - "integrity": "sha512-AacA8nRULjKMX2DvWvOAdBZMOfQlypSFkjcOcu9FalllIDJ1kvlREzcdIZmidQUqqeMv7jorHjq2HlLv/+c2lg==", + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.2.1.tgz", + "integrity": "sha512-e0WHiYql7+9wr4cWMx3TVQrNwejKaEe7/rHNmQmqRjazfOP5W8PB6Jpebb5o6fIapbz9o9+2ipcaTM2ZwDI6lw==", "dev": true, "engines": { "node": ">= 14" @@ -15573,9 +15573,9 @@ "dev": true }, "yaml": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.1.3.tgz", - "integrity": "sha512-AacA8nRULjKMX2DvWvOAdBZMOfQlypSFkjcOcu9FalllIDJ1kvlREzcdIZmidQUqqeMv7jorHjq2HlLv/+c2lg==", + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.2.1.tgz", + "integrity": "sha512-e0WHiYql7+9wr4cWMx3TVQrNwejKaEe7/rHNmQmqRjazfOP5W8PB6Jpebb5o6fIapbz9o9+2ipcaTM2ZwDI6lw==", "dev": true }, "yargs": { diff --git a/package.json b/package.json index 4a30c604a..e74281323 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "ts-jest": "^26.5.6", "ts-node": "^10.9.1", "typescript": "^4.9.4", - "yaml": "^2.1.3", + "yaml": "^2.2.1", "yargs": "^17.6.2" } }