diff --git a/dist/index.js b/dist/index.js index f85d5bea..e706eae0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -107128,6 +107128,8 @@ function composeNode(ctx, token, props, onError) { else node.commentBefore = comment; } + if (ctx.options.keepSourceTokens) + node.srcToken = token; return node; } function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anchor, tag }, onError) { @@ -107500,7 +107502,8 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) { var _a; const map = new YAMLMap.YAMLMap(ctx.schema); let offset = bm.offset; - for (const { start, key, sep, value } of bm.items) { + for (const collItem of bm.items) { + const { start, key, sep, value } = collItem; // key properties const keyProps = resolveProps.resolveProps(start, { indicator: 'explicit-key-ind', @@ -107562,7 +107565,10 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) { ? composeNode(ctx, value, valueProps, onError) : composeEmptyNode(ctx, offset, sep, null, valueProps, onError); offset = valueNode.range[2]; - map.items.push(new Pair.Pair(keyNode, valueNode)); + const pair = new Pair.Pair(keyNode, valueNode); + if (ctx.options.keepSourceTokens) + pair.srcToken = collItem; + map.items.push(pair); } else { // key with no value @@ -107574,7 +107580,10 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) { else keyNode.comment = valueProps.comment; } - map.items.push(new Pair.Pair(keyNode)); + const pair = new Pair.Pair(keyNode); + if (ctx.options.keepSourceTokens) + pair.srcToken = collItem; + map.items.push(pair); } } map.range = [bm.offset, offset, offset]; @@ -107612,7 +107621,7 @@ function resolveBlockScalar(scalar, strict, onError) { } // shortcut for empty contents if (!scalar.source || chompStart === 0) { - const value = header.chomp === '+' ? lines.map(line => line[0]).join('\n') : ''; + const value = header.chomp === '+' ? '\n'.repeat(Math.max(0, lines.length - 1)) : ''; let end = start + header.length; if (scalar.source) end += scalar.source.length; @@ -107904,7 +107913,8 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr coll.flow = true; let offset = fc.offset; for (let i = 0; i < fc.items.length; ++i) { - const { start, key, sep, value } = fc.items[i]; + const collItem = fc.items[i]; + const { start, key, sep, value } = collItem; const props = resolveProps.resolveProps(start, { flow: fcName, indicator: 'explicit-key-ind', @@ -108031,6 +108041,8 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr keyNode.comment = valueProps.comment; } const pair = new Pair.Pair(keyNode, valueNode); + if (ctx.options.keepSourceTokens) + pair.srcToken = collItem; if (isMap) { const map = coll; if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode)) @@ -109623,7 +109635,8 @@ class Collection extends Node.NodeBase { * Removes a value from the collection. * @returns `true` if the item was found and removed. */ - deleteIn([key, ...rest]) { + deleteIn(path) { + const [key, ...rest] = path; if (rest.length === 0) return this.delete(key); const node = this.get(key, true); @@ -109637,7 +109650,8 @@ class Collection extends Node.NodeBase { * scalar values from their surrounding node; to disable set `keepScalar` to * `true` (collections are always returned intact). */ - getIn([key, ...rest], keepScalar) { + getIn(path, keepScalar) { + const [key, ...rest] = path; const node = this.get(key, true); if (rest.length === 0) return !keepScalar && Node.isScalar(node) ? node.value : node; @@ -109661,7 +109675,8 @@ class Collection extends Node.NodeBase { /** * Checks if the collection includes a value with the key `key`. */ - hasIn([key, ...rest]) { + hasIn(path) { + const [key, ...rest] = path; if (rest.length === 0) return this.has(key); const node = this.get(key, true); @@ -109671,7 +109686,8 @@ class Collection extends Node.NodeBase { * Sets a value in this collection. For `!!set`, `value` needs to be a * boolean to add/remove the item from the set. */ - setIn([key, ...rest], value) { + setIn(path, value) { + const [key, ...rest] = path; if (rest.length === 0) { this.set(key, value); } @@ -110107,6 +110123,7 @@ var toJS = __nccwpck_require__(18842); const MERGE_KEY = '<<'; function addPairToJSMap(ctx, map, { key, value }) { if (ctx && ctx.doc.schema.merge && isMergeKey(key)) { + value = Node.isAlias(value) ? value.resolve(ctx.doc) : value; if (Node.isSeq(value)) for (const it of value.items) mergeToJSMap(ctx, map, it); @@ -110265,6 +110282,7 @@ exports.toJS = toJS; */ const defaultOptions = { intAsBigInt: false, + keepSourceTokens: false, logLevel: 'warn', prettyErrors: true, strict: true, @@ -110288,10 +110306,6 @@ var resolveFlowScalar = __nccwpck_require__(36390); var errors = __nccwpck_require__(73305); var stringifyString = __nccwpck_require__(46084); -/** - * If `token` is a CST flow or block scalar, determine its string value and a few other attributes. - * Otherwise, return `null`. - */ function resolveAsScalar(token, strict = true, onError) { if (token) { const _onError = (pos, code, message) => { @@ -110333,7 +110347,7 @@ function createScalarToken(value, context) { implicitKey, indent: indent > 0 ? ' '.repeat(indent) : '', inFlow, - options: { lineWidth: -1 } + options: { blockQuote: true, lineWidth: -1 } }); const end = (_a = context.end) !== null && _a !== void 0 ? _a : [ { type: 'newline', offset: -1, indent, source: '\n' } @@ -110402,7 +110416,7 @@ function setScalarValue(token, value, context = {}) { implicitKey: implicitKey || indent === null, indent: indent !== null && indent > 0 ? ' '.repeat(indent) : '', inFlow, - options: { lineWidth: -1 } + options: { blockQuote: true, lineWidth: -1 } }); switch (source[0]) { case '|': @@ -111178,7 +111192,7 @@ class Lexer { const line = this.getLine(); if (line === null) return this.setNext('flow'); - if ((indent !== -1 && indent < this.indentNext) || + if ((indent !== -1 && indent < this.indentNext && line[0] !== '#') || (indent === 0 && (line.startsWith('---') || line.startsWith('...')) && isEmpty(line[3]))) { @@ -111196,8 +111210,11 @@ class Lexer { } } let n = 0; - while (line[n] === ',') - n += (yield* this.pushCount(1)) + (yield* this.pushSpaces(true)); + while (line[n] === ',') { + n += yield* this.pushCount(1); + n += yield* this.pushSpaces(true); + this.flowKey = false; + } n += yield* this.pushIndicators(); switch (line[n]) { case undefined: @@ -113027,11 +113044,15 @@ const coreKnownTags = { function getTags(customTags, schemaName) { let tags = schemas[schemaName]; if (!tags) { - const keys = Object.keys(schemas) - .filter(key => key !== 'yaml11') - .map(key => JSON.stringify(key)) - .join(', '); - throw new Error(`Unknown schema "${schemaName}"; use one of ${keys}`); + if (Array.isArray(customTags)) + tags = []; + else { + const keys = Object.keys(schemas) + .filter(key => key !== 'yaml11') + .map(key => JSON.stringify(key)) + .join(', '); + throw new Error(`Unknown schema "${schemaName}"; use one of ${keys} or define customTags array`); + } } if (Array.isArray(customTags)) { for (const tag of customTags) @@ -113906,6 +113927,7 @@ const createStringifyContext = (doc, options) => ({ indent: '', indentStep: typeof options.indent === 'number' ? ' '.repeat(options.indent) : ' ', options: Object.assign({ + blockQuote: true, defaultKeyType: null, defaultStringType: 'PLAIN', directives: null, @@ -113917,7 +113939,7 @@ const createStringifyContext = (doc, options) => ({ minContentWidth: 20, nullStr: 'null', simpleKeys: false, - singleQuote: false, + singleQuote: null, trueStr: 'true', verifyAliasOrder: true }, options) @@ -114353,14 +114375,14 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) { const valueStr = stringify.stringify(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true)); let ws = ' '; if (vcb || keyComment) { - ws = `${vcb}\n${ctx.indent}`; + ws = valueStr === '' && !ctx.inFlow ? vcb : `${vcb}\n${ctx.indent}`; } else if (!explicitKey && Node.isCollection(value)) { const flow = valueStr[0] === '[' || valueStr[0] === '{'; if (!flow || valueStr.includes('\n')) ws = `\n${ctx.indent}`; } - else if (valueStr[0] === '\n') + else if (valueStr === '' || valueStr[0] === '\n') ws = ''; if (ctx.inFlow) { if (valueCommentDone && onComment) @@ -114507,34 +114529,50 @@ function doubleQuotedString(value, ctx) { : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_QUOTED, getFoldOptions(ctx)); } function singleQuotedString(value, ctx) { - if (ctx.implicitKey) { - if (/\n/.test(value)) - return doubleQuotedString(value, ctx); - } - else { - // single quoted string can't have leading or trailing whitespace around newline - if (/[ \t]\n|\n[ \t]/.test(value)) - return doubleQuotedString(value, ctx); - } + if (ctx.options.singleQuote === false || + (ctx.implicitKey && value.includes('\n')) || + /[ \t]\n|\n[ \t]/.test(value) // single quoted string can't have leading or trailing whitespace around newline + ) + return doubleQuotedString(value, ctx); const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : ''); const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'"; return ctx.implicitKey ? res : foldFlowLines.foldFlowLines(res, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx)); } +function quotedString(value, ctx) { + const { singleQuote } = ctx.options; + let qs; + if (singleQuote === false) + qs = doubleQuotedString; + else { + const hasDouble = value.includes('"'); + const hasSingle = value.includes("'"); + if (hasDouble && !hasSingle) + qs = singleQuotedString; + else if (hasSingle && !hasDouble) + qs = doubleQuotedString; + else + qs = singleQuote ? singleQuotedString : doubleQuotedString; + } + return qs(value, ctx); +} function blockString({ comment, type, value }, ctx, onComment, onChompKeep) { + const { lineWidth, blockQuote } = ctx.options; // 1. Block can't end in whitespace unless the last line is non-empty. // 2. Strings consisting of only whitespace are best rendered explicitly. - if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) { - return doubleQuotedString(value, ctx); + if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) { + return quotedString(value, ctx); } const indent = ctx.indent || (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : ''); - const literal = type === Scalar.Scalar.BLOCK_FOLDED - ? false - : type === Scalar.Scalar.BLOCK_LITERAL - ? true - : !lineLengthOverLimit(value, ctx.options.lineWidth, indent.length); + const literal = blockQuote === 'literal' + ? true + : blockQuote === 'folded' || type === Scalar.Scalar.BLOCK_FOLDED + ? false + : type === Scalar.Scalar.BLOCK_LITERAL + ? true + : !lineLengthOverLimit(value, lineWidth, indent.length); if (!value) return literal ? '|\n' : '>\n'; // determine chomping from whitespace at value end @@ -114607,25 +114645,10 @@ function plainString(item, ctx, onComment, onChompKeep) { const { actualString, implicitKey, indent, inFlow } = ctx; if ((implicitKey && /[\n[\]{},]/.test(value)) || (inFlow && /[[\]{},]/.test(value))) { - return doubleQuotedString(value, ctx); + return quotedString(value, ctx); } if (!value || /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) { - const hasDouble = value.indexOf('"') !== -1; - const hasSingle = value.indexOf("'") !== -1; - let quotedString; - if (hasDouble && !hasSingle) { - quotedString = singleQuotedString; - } - else if (hasSingle && !hasDouble) { - quotedString = doubleQuotedString; - } - else if (ctx.options.singleQuote) { - quotedString = singleQuotedString; - } - else { - quotedString = doubleQuotedString; - } // not allowed: // - empty string, '-' or '?' // - start with an indicator character (except [?:-]) or /[?-] / @@ -114656,7 +114679,7 @@ function plainString(item, ctx, onComment, onChompKeep) { if (tag.default && tag.tag !== 'tag:yaml.org,2002:str' && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str))) - return doubleQuotedString(value, ctx); + return quotedString(value, ctx); } } return implicitKey @@ -114679,7 +114702,7 @@ function stringifyString(item, ctx, onComment, onChompKeep) { case Scalar.Scalar.BLOCK_FOLDED: case Scalar.Scalar.BLOCK_LITERAL: return implicitKey || inFlow - ? doubleQuotedString(ss.value, ctx) // blocks are not valid inside flow containers + ? quotedString(ss.value, ctx) // blocks are not valid inside flow containers : blockString(ss, ctx, onComment, onChompKeep); case Scalar.Scalar.QUOTE_DOUBLE: return doubleQuotedString(ss.value, ctx); diff --git a/dist/post/index.js b/dist/post/index.js index 402158f0..f5b06db1 100644 --- a/dist/post/index.js +++ b/dist/post/index.js @@ -105941,6 +105941,8 @@ function composeNode(ctx, token, props, onError) { else node.commentBefore = comment; } + if (ctx.options.keepSourceTokens) + node.srcToken = token; return node; } function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anchor, tag }, onError) { @@ -106313,7 +106315,8 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) { var _a; const map = new YAMLMap.YAMLMap(ctx.schema); let offset = bm.offset; - for (const { start, key, sep, value } of bm.items) { + for (const collItem of bm.items) { + const { start, key, sep, value } = collItem; // key properties const keyProps = resolveProps.resolveProps(start, { indicator: 'explicit-key-ind', @@ -106375,7 +106378,10 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) { ? composeNode(ctx, value, valueProps, onError) : composeEmptyNode(ctx, offset, sep, null, valueProps, onError); offset = valueNode.range[2]; - map.items.push(new Pair.Pair(keyNode, valueNode)); + const pair = new Pair.Pair(keyNode, valueNode); + if (ctx.options.keepSourceTokens) + pair.srcToken = collItem; + map.items.push(pair); } else { // key with no value @@ -106387,7 +106393,10 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) { else keyNode.comment = valueProps.comment; } - map.items.push(new Pair.Pair(keyNode)); + const pair = new Pair.Pair(keyNode); + if (ctx.options.keepSourceTokens) + pair.srcToken = collItem; + map.items.push(pair); } } map.range = [bm.offset, offset, offset]; @@ -106425,7 +106434,7 @@ function resolveBlockScalar(scalar, strict, onError) { } // shortcut for empty contents if (!scalar.source || chompStart === 0) { - const value = header.chomp === '+' ? lines.map(line => line[0]).join('\n') : ''; + const value = header.chomp === '+' ? '\n'.repeat(Math.max(0, lines.length - 1)) : ''; let end = start + header.length; if (scalar.source) end += scalar.source.length; @@ -106717,7 +106726,8 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr coll.flow = true; let offset = fc.offset; for (let i = 0; i < fc.items.length; ++i) { - const { start, key, sep, value } = fc.items[i]; + const collItem = fc.items[i]; + const { start, key, sep, value } = collItem; const props = resolveProps.resolveProps(start, { flow: fcName, indicator: 'explicit-key-ind', @@ -106844,6 +106854,8 @@ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onErr keyNode.comment = valueProps.comment; } const pair = new Pair.Pair(keyNode, valueNode); + if (ctx.options.keepSourceTokens) + pair.srcToken = collItem; if (isMap) { const map = coll; if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode)) @@ -108436,7 +108448,8 @@ class Collection extends Node.NodeBase { * Removes a value from the collection. * @returns `true` if the item was found and removed. */ - deleteIn([key, ...rest]) { + deleteIn(path) { + const [key, ...rest] = path; if (rest.length === 0) return this.delete(key); const node = this.get(key, true); @@ -108450,7 +108463,8 @@ class Collection extends Node.NodeBase { * scalar values from their surrounding node; to disable set `keepScalar` to * `true` (collections are always returned intact). */ - getIn([key, ...rest], keepScalar) { + getIn(path, keepScalar) { + const [key, ...rest] = path; const node = this.get(key, true); if (rest.length === 0) return !keepScalar && Node.isScalar(node) ? node.value : node; @@ -108474,7 +108488,8 @@ class Collection extends Node.NodeBase { /** * Checks if the collection includes a value with the key `key`. */ - hasIn([key, ...rest]) { + hasIn(path) { + const [key, ...rest] = path; if (rest.length === 0) return this.has(key); const node = this.get(key, true); @@ -108484,7 +108499,8 @@ class Collection extends Node.NodeBase { * Sets a value in this collection. For `!!set`, `value` needs to be a * boolean to add/remove the item from the set. */ - setIn([key, ...rest], value) { + setIn(path, value) { + const [key, ...rest] = path; if (rest.length === 0) { this.set(key, value); } @@ -108920,6 +108936,7 @@ var toJS = __nccwpck_require__(18842); const MERGE_KEY = '<<'; function addPairToJSMap(ctx, map, { key, value }) { if (ctx && ctx.doc.schema.merge && isMergeKey(key)) { + value = Node.isAlias(value) ? value.resolve(ctx.doc) : value; if (Node.isSeq(value)) for (const it of value.items) mergeToJSMap(ctx, map, it); @@ -109078,6 +109095,7 @@ exports.toJS = toJS; */ const defaultOptions = { intAsBigInt: false, + keepSourceTokens: false, logLevel: 'warn', prettyErrors: true, strict: true, @@ -109101,10 +109119,6 @@ var resolveFlowScalar = __nccwpck_require__(36390); var errors = __nccwpck_require__(73305); var stringifyString = __nccwpck_require__(46084); -/** - * If `token` is a CST flow or block scalar, determine its string value and a few other attributes. - * Otherwise, return `null`. - */ function resolveAsScalar(token, strict = true, onError) { if (token) { const _onError = (pos, code, message) => { @@ -109146,7 +109160,7 @@ function createScalarToken(value, context) { implicitKey, indent: indent > 0 ? ' '.repeat(indent) : '', inFlow, - options: { lineWidth: -1 } + options: { blockQuote: true, lineWidth: -1 } }); const end = (_a = context.end) !== null && _a !== void 0 ? _a : [ { type: 'newline', offset: -1, indent, source: '\n' } @@ -109215,7 +109229,7 @@ function setScalarValue(token, value, context = {}) { implicitKey: implicitKey || indent === null, indent: indent !== null && indent > 0 ? ' '.repeat(indent) : '', inFlow, - options: { lineWidth: -1 } + options: { blockQuote: true, lineWidth: -1 } }); switch (source[0]) { case '|': @@ -109991,7 +110005,7 @@ class Lexer { const line = this.getLine(); if (line === null) return this.setNext('flow'); - if ((indent !== -1 && indent < this.indentNext) || + if ((indent !== -1 && indent < this.indentNext && line[0] !== '#') || (indent === 0 && (line.startsWith('---') || line.startsWith('...')) && isEmpty(line[3]))) { @@ -110009,8 +110023,11 @@ class Lexer { } } let n = 0; - while (line[n] === ',') - n += (yield* this.pushCount(1)) + (yield* this.pushSpaces(true)); + while (line[n] === ',') { + n += yield* this.pushCount(1); + n += yield* this.pushSpaces(true); + this.flowKey = false; + } n += yield* this.pushIndicators(); switch (line[n]) { case undefined: @@ -111840,11 +111857,15 @@ const coreKnownTags = { function getTags(customTags, schemaName) { let tags = schemas[schemaName]; if (!tags) { - const keys = Object.keys(schemas) - .filter(key => key !== 'yaml11') - .map(key => JSON.stringify(key)) - .join(', '); - throw new Error(`Unknown schema "${schemaName}"; use one of ${keys}`); + if (Array.isArray(customTags)) + tags = []; + else { + const keys = Object.keys(schemas) + .filter(key => key !== 'yaml11') + .map(key => JSON.stringify(key)) + .join(', '); + throw new Error(`Unknown schema "${schemaName}"; use one of ${keys} or define customTags array`); + } } if (Array.isArray(customTags)) { for (const tag of customTags) @@ -112719,6 +112740,7 @@ const createStringifyContext = (doc, options) => ({ indent: '', indentStep: typeof options.indent === 'number' ? ' '.repeat(options.indent) : ' ', options: Object.assign({ + blockQuote: true, defaultKeyType: null, defaultStringType: 'PLAIN', directives: null, @@ -112730,7 +112752,7 @@ const createStringifyContext = (doc, options) => ({ minContentWidth: 20, nullStr: 'null', simpleKeys: false, - singleQuote: false, + singleQuote: null, trueStr: 'true', verifyAliasOrder: true }, options) @@ -113166,14 +113188,14 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) { const valueStr = stringify.stringify(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true)); let ws = ' '; if (vcb || keyComment) { - ws = `${vcb}\n${ctx.indent}`; + ws = valueStr === '' && !ctx.inFlow ? vcb : `${vcb}\n${ctx.indent}`; } else if (!explicitKey && Node.isCollection(value)) { const flow = valueStr[0] === '[' || valueStr[0] === '{'; if (!flow || valueStr.includes('\n')) ws = `\n${ctx.indent}`; } - else if (valueStr[0] === '\n') + else if (valueStr === '' || valueStr[0] === '\n') ws = ''; if (ctx.inFlow) { if (valueCommentDone && onComment) @@ -113320,34 +113342,50 @@ function doubleQuotedString(value, ctx) { : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_QUOTED, getFoldOptions(ctx)); } function singleQuotedString(value, ctx) { - if (ctx.implicitKey) { - if (/\n/.test(value)) - return doubleQuotedString(value, ctx); - } - else { - // single quoted string can't have leading or trailing whitespace around newline - if (/[ \t]\n|\n[ \t]/.test(value)) - return doubleQuotedString(value, ctx); - } + if (ctx.options.singleQuote === false || + (ctx.implicitKey && value.includes('\n')) || + /[ \t]\n|\n[ \t]/.test(value) // single quoted string can't have leading or trailing whitespace around newline + ) + return doubleQuotedString(value, ctx); const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : ''); const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'"; return ctx.implicitKey ? res : foldFlowLines.foldFlowLines(res, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx)); } +function quotedString(value, ctx) { + const { singleQuote } = ctx.options; + let qs; + if (singleQuote === false) + qs = doubleQuotedString; + else { + const hasDouble = value.includes('"'); + const hasSingle = value.includes("'"); + if (hasDouble && !hasSingle) + qs = singleQuotedString; + else if (hasSingle && !hasDouble) + qs = doubleQuotedString; + else + qs = singleQuote ? singleQuotedString : doubleQuotedString; + } + return qs(value, ctx); +} function blockString({ comment, type, value }, ctx, onComment, onChompKeep) { + const { lineWidth, blockQuote } = ctx.options; // 1. Block can't end in whitespace unless the last line is non-empty. // 2. Strings consisting of only whitespace are best rendered explicitly. - if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) { - return doubleQuotedString(value, ctx); + if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) { + return quotedString(value, ctx); } const indent = ctx.indent || (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : ''); - const literal = type === Scalar.Scalar.BLOCK_FOLDED - ? false - : type === Scalar.Scalar.BLOCK_LITERAL - ? true - : !lineLengthOverLimit(value, ctx.options.lineWidth, indent.length); + const literal = blockQuote === 'literal' + ? true + : blockQuote === 'folded' || type === Scalar.Scalar.BLOCK_FOLDED + ? false + : type === Scalar.Scalar.BLOCK_LITERAL + ? true + : !lineLengthOverLimit(value, lineWidth, indent.length); if (!value) return literal ? '|\n' : '>\n'; // determine chomping from whitespace at value end @@ -113420,25 +113458,10 @@ function plainString(item, ctx, onComment, onChompKeep) { const { actualString, implicitKey, indent, inFlow } = ctx; if ((implicitKey && /[\n[\]{},]/.test(value)) || (inFlow && /[[\]{},]/.test(value))) { - return doubleQuotedString(value, ctx); + return quotedString(value, ctx); } if (!value || /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) { - const hasDouble = value.indexOf('"') !== -1; - const hasSingle = value.indexOf("'") !== -1; - let quotedString; - if (hasDouble && !hasSingle) { - quotedString = singleQuotedString; - } - else if (hasSingle && !hasDouble) { - quotedString = doubleQuotedString; - } - else if (ctx.options.singleQuote) { - quotedString = singleQuotedString; - } - else { - quotedString = doubleQuotedString; - } // not allowed: // - empty string, '-' or '?' // - start with an indicator character (except [?:-]) or /[?-] / @@ -113469,7 +113492,7 @@ function plainString(item, ctx, onComment, onChompKeep) { if (tag.default && tag.tag !== 'tag:yaml.org,2002:str' && ((_a = tag.test) === null || _a === void 0 ? void 0 : _a.test(str))) - return doubleQuotedString(value, ctx); + return quotedString(value, ctx); } } return implicitKey @@ -113492,7 +113515,7 @@ function stringifyString(item, ctx, onComment, onChompKeep) { case Scalar.Scalar.BLOCK_FOLDED: case Scalar.Scalar.BLOCK_LITERAL: return implicitKey || inFlow - ? doubleQuotedString(ss.value, ctx) // blocks are not valid inside flow containers + ? quotedString(ss.value, ctx) // blocks are not valid inside flow containers : blockString(ss, ctx, onComment, onChompKeep); case Scalar.Scalar.QUOTE_DOUBLE: return doubleQuotedString(ss.value, ctx); diff --git a/package.json b/package.json index 895e3a37..65f19b70 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "cheerio": "1.0.0-rc.10", "date-fns": "2.25.0", "semver": "7.3.5", - "yaml": "2.0.0-8" + "yaml": "2.0.0-9" }, "devDependencies": { "@types/node": "16.11.7", diff --git a/yarn.lock b/yarn.lock index d8bfb6e7..ff58e459 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2547,7 +2547,7 @@ yallist@^4.0.0: resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== -yaml@2.0.0-8: - version "2.0.0-8" - resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.0.0-8.tgz#226365f0d804ba7fb8cc2b527a00a7a4a3d8ea5f" - integrity sha512-QaYgJZMfWD6fKN/EYMk6w1oLWPCr1xj9QaPSZW5qkDb3y8nGCXhy2Ono+AF4F+CSL/vGcqswcAT0BaS//pgD2A== +yaml@2.0.0-9: + version "2.0.0-9" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.0.0-9.tgz#0099f0645d1ffa686a2c5141b6da340f545d3634" + integrity sha512-Bf2KowHjyVkIIiGMt7+fbhmlvKOaE8DWuD07bnL4+FQ9sPmEl/5IzGpBpoxPqOaHuyasBjJhyXDcISpJWfhCGw==