'use strict'; var _rollupPluginBabelHelpers = require('./_rollupPluginBabelHelpers-eed30217.js'); function addCommentBefore(str, indent, comment) { if (!comment) return str; const cc = comment.replace(/[\s\S]^/gm, `$&${indent}#`); return `#${cc}\n${indent}${str}`; } function addComment(str, indent, comment) { return !comment ? str : comment.indexOf('\n') === -1 ? `${str} #${comment}` : `${str}\n` + comment.replace(/^/gm, `${indent || ''}#`); } class Node {} /** * Recursively convert any node or its contents to native JavaScript * * @param value - The input value * @param {string|null} arg - If `value` defines a `toJSON()` method, use this * as its first argument * @param ctx - Conversion context, originally set in Document#toJS(). If * `{ keep: true }` is not set, output should be suitable for JSON * stringification. */ function toJS(value, arg, ctx) { if (Array.isArray(value)) return value.map((v, i) => toJS(v, String(i), ctx)); if (value && typeof value.toJSON === 'function') { const anchor = ctx && ctx.anchors && ctx.anchors.get(value); if (anchor) ctx.onCreate = res => { anchor.res = res; delete ctx.onCreate; }; const res = value.toJSON(arg, ctx); if (anchor && ctx.onCreate) ctx.onCreate(res); return res; } if (!(ctx && ctx.keep) && typeof value === 'bigint') return Number(value); return value; } const isScalarValue = value => !value || typeof value !== 'function' && typeof value !== 'object'; class Scalar extends Node { constructor(value) { super(); this.value = value; } toJSON(arg, ctx) { return ctx && ctx.keep ? this.value : toJS(this.value, arg, ctx); } toString() { return String(this.value); } } function findTagObject(value, tagName, tags) { if (tagName) { const match = tags.filter(t => t.tag === tagName); const tagObj = match.find(t => !t.format) || match[0]; if (!tagObj) throw new Error(`Tag ${tagName} not found`); return tagObj; } return tags.find(t => t.identify && t.identify(value) && !t.format); } function createNode(value, tagName, ctx) { if (value instanceof Node) return value; const { onAlias, onTagObj, prevObjects, wrapScalars } = ctx; const { map, seq, tags } = ctx.schema; if (tagName && tagName.startsWith('!!')) tagName = _rollupPluginBabelHelpers.defaultTagPrefix + tagName.slice(2); let tagObj = findTagObject(value, tagName, tags); if (!tagObj) { if (typeof value.toJSON === 'function') value = value.toJSON(); if (!value || typeof value !== 'object') return wrapScalars ? new Scalar(value) : value; tagObj = value instanceof Map ? map : value[Symbol.iterator] ? seq : map; } if (onTagObj) { onTagObj(tagObj); delete ctx.onTagObj; } // Detect duplicate references to the same object & use Alias nodes for all // after first. The `obj` wrapper allows for circular references to resolve. const obj = { value: undefined, node: undefined }; if (value && typeof value === 'object') { const prev = prevObjects.get(value); if (prev) return onAlias(prev); obj.value = value; prevObjects.set(value, obj); } obj.node = tagObj.createNode ? tagObj.createNode(ctx.schema, value, ctx) : wrapScalars ? new Scalar(value) : value; if (tagName && obj.node instanceof Node) obj.node.tag = tagName; return obj.node; } function collectionFromPath(schema, path, value) { let v = value; for (let i = path.length - 1; i >= 0; --i) { const k = path[i]; if (Number.isInteger(k) && k >= 0) { const a = []; a[k] = v; v = a; } else { const o = {}; Object.defineProperty(o, k, { value: v, writable: true, enumerable: true, configurable: true }); v = o; } } return createNode(v, null, { onAlias() { throw new Error('Repeated objects are not supported here'); }, prevObjects: new Map(), schema, wrapScalars: false }); } // null, undefined, or an empty non-string iterable (e.g. []) const isEmptyPath = path => path == null || typeof path === 'object' && path[Symbol.iterator]().next().done; class Collection extends Node { constructor(schema) { super(); _rollupPluginBabelHelpers._defineProperty(this, "items", []); this.schema = schema; } addIn(path, value) { if (isEmptyPath(path)) this.add(value);else { const [key, ...rest] = path; const node = this.get(key, true); if (node instanceof Collection) node.addIn(rest, value);else if (node === undefined && this.schema) this.set(key, collectionFromPath(this.schema, rest, value));else throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`); } } deleteIn([key, ...rest]) { if (rest.length === 0) return this.delete(key); const node = this.get(key, true); if (node instanceof Collection) return node.deleteIn(rest);else throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`); } getIn([key, ...rest], keepScalar) { const node = this.get(key, true); if (rest.length === 0) return !keepScalar && node instanceof Scalar ? node.value : node;else return node instanceof Collection ? node.getIn(rest, keepScalar) : undefined; } hasAllNullValues() { return this.items.every(node => { if (!node || node.type !== 'PAIR') return false; const n = node.value; return n == null || n instanceof Scalar && n.value == null && !n.commentBefore && !n.comment && !n.tag; }); } hasIn([key, ...rest]) { if (rest.length === 0) return this.has(key); const node = this.get(key, true); return node instanceof Collection ? node.hasIn(rest) : false; } setIn([key, ...rest], value) { if (rest.length === 0) { this.set(key, value); } else { const node = this.get(key, true); if (node instanceof Collection) node.setIn(rest, value);else if (node === undefined && this.schema) this.set(key, collectionFromPath(this.schema, rest, value));else throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`); } } /* istanbul ignore next: overridden in implementations */ toJSON() { return null; } toString(ctx, { blockItem, flowChars, isMap, itemIndent }, onComment, onChompKeep) { const { indent, indentStep, stringify } = ctx; const inFlow = this.type === _rollupPluginBabelHelpers.Type.FLOW_MAP || this.type === _rollupPluginBabelHelpers.Type.FLOW_SEQ || ctx.inFlow; if (inFlow) itemIndent += indentStep; const allNullValues = isMap && this.hasAllNullValues(); ctx = Object.assign({}, ctx, { allNullValues, indent: itemIndent, inFlow, type: null }); let chompKeep = false; let hasItemWithNewLine = false; const nodes = this.items.reduce((nodes, item, i) => { let comment; if (item) { if (!chompKeep && item.spaceBefore) nodes.push({ type: 'comment', str: '' }); if (item.commentBefore) item.commentBefore.match(/^.*$/gm).forEach(line => { nodes.push({ type: 'comment', str: `#${line}` }); }); if (item.comment) comment = item.comment; if (inFlow && (!chompKeep && item.spaceBefore || item.commentBefore || item.comment || item.key && (item.key.commentBefore || item.key.comment) || item.value && (item.value.commentBefore || item.value.comment))) hasItemWithNewLine = true; } chompKeep = false; let str = stringify(item, ctx, () => comment = null, () => chompKeep = true); if (inFlow && !hasItemWithNewLine && str.includes('\n')) hasItemWithNewLine = true; if (inFlow && i < this.items.length - 1) str += ','; str = addComment(str, itemIndent, comment); if (chompKeep && (comment || inFlow)) chompKeep = false; nodes.push({ type: 'item', str }); return nodes; }, []); let str; if (nodes.length === 0) { str = flowChars.start + flowChars.end; } else if (inFlow) { const { start, end } = flowChars; const strings = nodes.map(n => n.str); if (hasItemWithNewLine || strings.reduce((sum, str) => sum + str.length + 2, 2) > Collection.maxFlowStringSingleLineLength) { str = start; for (const s of strings) { str += s ? `\n${indentStep}${indent}${s}` : '\n'; } str += `\n${indent}${end}`; } else { str = `${start} ${strings.join(' ')} ${end}`; } } else { const strings = nodes.map(blockItem); str = strings.shift(); for (const s of strings) str += s ? `\n${indent}${s}` : '\n'; } if (this.comment) { str += '\n' + this.comment.replace(/^/gm, `${indent}#`); if (onComment) onComment(); } else if (chompKeep && onChompKeep) onChompKeep(); return str; } } _rollupPluginBabelHelpers._defineProperty(Collection, "maxFlowStringSingleLineLength", 60); function asItemIndex(key) { let idx = key instanceof Scalar ? key.value : key; if (idx && typeof idx === 'string') idx = Number(idx); return Number.isInteger(idx) && idx >= 0 ? idx : null; } class YAMLSeq extends Collection { add(value) { this.items.push(value); } delete(key) { const idx = asItemIndex(key); if (typeof idx !== 'number') return false; const del = this.items.splice(idx, 1); return del.length > 0; } get(key, keepScalar) { const idx = asItemIndex(key); if (typeof idx !== 'number') return undefined; const it = this.items[idx]; return !keepScalar && it instanceof Scalar ? it.value : it; } has(key) { const idx = asItemIndex(key); return typeof idx === 'number' && idx < this.items.length; } set(key, value) { const idx = asItemIndex(key); if (typeof idx !== 'number') throw new Error(`Expected a valid index, not ${key}.`); const prev = this.items[idx]; if (prev instanceof Scalar && isScalarValue(value)) prev.value = value;else this.items[idx] = value; } toJSON(_, ctx) { const seq = []; if (ctx && ctx.onCreate) ctx.onCreate(seq); let i = 0; for (const item of this.items) seq.push(toJS(item, String(i++), ctx)); return seq; } toString(ctx, onComment, onChompKeep) { if (!ctx) return JSON.stringify(this); return super.toString(ctx, { blockItem: n => n.type === 'comment' ? n.str : `- ${n.str}`, flowChars: { start: '[', end: ']' }, isMap: false, itemIndent: (ctx.indent || '') + ' ' }, onComment, onChompKeep); } } const stringifyKey = (key, jsKey, ctx) => { if (jsKey === null) return ''; if (typeof jsKey !== 'object') return String(jsKey); if (key instanceof Node && ctx && ctx.doc) return key.toString({ anchors: Object.create(null), doc: ctx.doc, indent: '', indentStep: ctx.indentStep, inFlow: true, inStringifyKey: true, stringify: ctx.stringify }); return JSON.stringify(jsKey); }; function createPair(key, value, ctx) { const k = createNode(key, null, ctx); const v = createNode(value, null, ctx); return new Pair(k, v); } class Pair extends Node { constructor(key, value = null) { super(); this.key = key; this.value = value; this.type = Pair.Type.PAIR; } get commentBefore() { return this.key instanceof Node ? this.key.commentBefore : undefined; } set commentBefore(cb) { if (this.key == null) this.key = new Scalar(null); if (this.key instanceof Node) this.key.commentBefore = cb;else { const msg = 'Pair.commentBefore is an alias for Pair.key.commentBefore. To set it, the key must be a Node.'; throw new Error(msg); } } addToJSMap(ctx, map) { const key = toJS(this.key, '', ctx); if (map instanceof Map) { const value = toJS(this.value, key, ctx); map.set(key, value); } else if (map instanceof Set) { map.add(key); } else { const stringKey = stringifyKey(this.key, key, ctx); const value = toJS(this.value, stringKey, ctx); if (stringKey in map) Object.defineProperty(map, stringKey, { value, writable: true, enumerable: true, configurable: true });else map[stringKey] = value; } return map; } toJSON(_, ctx) { const pair = ctx && ctx.mapAsMap ? new Map() : {}; return this.addToJSMap(ctx, pair); } toString(ctx, onComment, onChompKeep) { if (!ctx || !ctx.doc) return JSON.stringify(this); const { indent: indentSize, indentSeq, simpleKeys } = ctx.doc.options; let { key, value } = this; let keyComment = key instanceof Node && key.comment; if (simpleKeys) { if (keyComment) { throw new Error('With simple keys, key nodes cannot have comments'); } if (key instanceof Collection) { const msg = 'With simple keys, collection cannot be used as a key value'; throw new Error(msg); } } let explicitKey = !simpleKeys && (!key || keyComment || key instanceof Collection || key.type === _rollupPluginBabelHelpers.Type.BLOCK_FOLDED || key.type === _rollupPluginBabelHelpers.Type.BLOCK_LITERAL); const { allNullValues, doc, indent, indentStep, stringify } = ctx; ctx = Object.assign({}, ctx, { implicitKey: !explicitKey && (simpleKeys || !allNullValues), indent: indent + indentStep }); let chompKeep = false; let str = stringify(key, ctx, () => keyComment = null, () => chompKeep = true); str = addComment(str, ctx.indent, keyComment); if (allNullValues && !simpleKeys) { if (this.comment) { str = addComment(str, ctx.indent, this.comment); if (onComment) onComment(); } else if (chompKeep && !keyComment && onChompKeep) onChompKeep(); return ctx.inFlow ? str : `? ${str}`; } if (!explicitKey && str.length > 1024) { if (!simpleKeys) { explicitKey = true; } else { const msg = 'With simple keys, single line scalar must not span more than 1024 characters'; throw new Error(msg); } } str = explicitKey ? `? ${str}\n${indent}:` : `${str}:`; if (this.comment) { // expected (but not strictly required) to be a single-line comment str = addComment(str, ctx.indent, this.comment); if (onComment) onComment(); } let vcb = ''; let valueComment = null; if (value instanceof Node) { if (value.spaceBefore) vcb = '\n'; if (value.commentBefore) { const cs = value.commentBefore.replace(/^/gm, `${ctx.indent}#`); vcb += `\n${cs}`; } valueComment = value.comment; } else if (value && typeof value === 'object') { value = doc.createNode(value); } ctx.implicitKey = false; if (!explicitKey && !this.comment && value instanceof Scalar) ctx.indentAtStart = str.length + 1; chompKeep = false; if (!indentSeq && indentSize >= 2 && !ctx.inFlow && !explicitKey && value instanceof YAMLSeq && value.type !== _rollupPluginBabelHelpers.Type.FLOW_SEQ && !value.tag && !doc.anchors.getName(value)) { // If indentSeq === false, consider '- ' as part of indentation where possible ctx.indent = ctx.indent.substr(2); } const valueStr = stringify(value, ctx, () => valueComment = null, () => chompKeep = true); let ws = ' '; if (vcb || this.comment) { ws = `${vcb}\n${ctx.indent}`; } else if (!explicitKey && value instanceof Collection) { const flow = valueStr[0] === '[' || valueStr[0] === '{'; if (!flow || valueStr.includes('\n')) ws = `\n${ctx.indent}`; } else if (valueStr[0] === '\n') ws = ''; if (chompKeep && !valueComment && onChompKeep) onChompKeep(); return addComment(str + ws + valueStr, ctx.indent, valueComment); } } _rollupPluginBabelHelpers._defineProperty(Pair, "Type", { PAIR: 'PAIR', MERGE_PAIR: 'MERGE_PAIR' }); const getAliasCount = (node, anchors) => { if (node instanceof Alias) { const anchor = anchors.get(node.source); return anchor.count * anchor.aliasCount; } else if (node instanceof Collection) { let count = 0; for (const item of node.items) { const c = getAliasCount(item, anchors); if (c > count) count = c; } return count; } else if (node instanceof Pair) { const kc = getAliasCount(node.key, anchors); const vc = getAliasCount(node.value, anchors); return Math.max(kc, vc); } return 1; }; class Alias extends Node { static stringify({ range, source }, { anchors, doc, implicitKey, inStringifyKey }) { let anchor = Object.keys(anchors).find(a => anchors[a] === source); if (!anchor && inStringifyKey) anchor = doc.anchors.getName(source) || doc.anchors.newName(); if (anchor) return `*${anchor}${implicitKey ? ' ' : ''}`; const msg = doc.anchors.getName(source) ? 'Alias node must be after source node' : 'Source node not found for alias node'; throw new Error(`${msg} [${range}]`); } constructor(source) { super(); this.source = source; this.type = _rollupPluginBabelHelpers.Type.ALIAS; } set tag(t) { throw new Error('Alias nodes cannot have tags'); } toJSON(arg, ctx) { if (!ctx) return toJS(this.source, arg, ctx); const { anchors, maxAliasCount } = ctx; const anchor = anchors.get(this.source); /* istanbul ignore if */ if (!anchor || anchor.res === undefined) { const msg = 'This should not happen: Alias anchor was not resolved?'; if (this.cstNode) throw new _rollupPluginBabelHelpers.YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg); } if (maxAliasCount >= 0) { anchor.count += 1; if (anchor.aliasCount === 0) anchor.aliasCount = getAliasCount(this.source, anchors); if (anchor.count * anchor.aliasCount > maxAliasCount) { const msg = 'Excessive alias count indicates a resource exhaustion attack'; if (this.cstNode) throw new _rollupPluginBabelHelpers.YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg); } } return anchor.res; } // Only called when stringifying an alias mapping key while constructing // Object output. toString(ctx) { return Alias.stringify(this, ctx); } } _rollupPluginBabelHelpers._defineProperty(Alias, "default", true); function findPair(items, key) { const k = key instanceof Scalar ? key.value : key; for (const it of items) { if (it instanceof Pair) { if (it.key === key || it.key === k) return it; if (it.key && it.key.value === k) return it; } } return undefined; } class YAMLMap extends Collection { add(pair, overwrite) { if (!pair) pair = new Pair(pair);else if (!(pair instanceof Pair)) pair = new Pair(pair.key || pair, pair.value); const prev = findPair(this.items, pair.key); const sortEntries = this.schema && this.schema.sortMapEntries; if (prev) { if (!overwrite) throw new Error(`Key ${pair.key} already set`); // For scalars, keep the old node & its comments and anchors if (prev.value instanceof Scalar && isScalarValue(pair.value)) prev.value.value = pair.value;else prev.value = pair.value; } else if (sortEntries) { const i = this.items.findIndex(item => sortEntries(pair, item) < 0); if (i === -1) this.items.push(pair);else this.items.splice(i, 0, pair); } else { this.items.push(pair); } } delete(key) { const it = findPair(this.items, key); if (!it) return false; const del = this.items.splice(this.items.indexOf(it), 1); return del.length > 0; } get(key, keepScalar) { const it = findPair(this.items, key); const node = it && it.value; return !keepScalar && node instanceof Scalar ? node.value : node; } has(key) { return !!findPair(this.items, key); } set(key, value) { this.add(new Pair(key, value), true); } /** * @param ctx - Conversion context, originally set in Document#toJS() * @param {Class} Type - If set, forces the returned collection type * @returns Instance of Type, Map, or Object */ toJSON(_, ctx, Type) { const map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {}; if (ctx && ctx.onCreate) ctx.onCreate(map); for (const item of this.items) item.addToJSMap(ctx, map); return map; } toString(ctx, onComment, onChompKeep) { if (!ctx) return JSON.stringify(this); for (const item of this.items) { if (!(item instanceof Pair)) throw new Error(`Map items must all be pairs; found ${JSON.stringify(item)} instead`); } return super.toString(ctx, { blockItem: n => n.str, flowChars: { start: '{', end: '}' }, isMap: true, itemIndent: ctx.indent || '' }, onComment, onChompKeep); } } const binaryOptions = { defaultType: _rollupPluginBabelHelpers.Type.BLOCK_LITERAL, lineWidth: 76 }; const boolOptions = { trueStr: 'true', falseStr: 'false' }; const intOptions = { asBigInt: false }; const nullOptions = { nullStr: 'null' }; const strOptions = { defaultType: _rollupPluginBabelHelpers.Type.PLAIN, defaultKeyType: _rollupPluginBabelHelpers.Type.PLAIN, defaultQuoteSingle: false, doubleQuoted: { jsonEncoding: false, minMultiLineLength: 40 }, fold: { lineWidth: 80, minContentWidth: 20 } }; function resolveScalar(str, tags) { for (const { format, test, resolve } of tags) { if (test && test.test(str)) { let res = resolve(str); if (!(res instanceof Scalar)) res = new Scalar(res); if (format) res.format = format; return res; } } return new Scalar(str); // fallback to string } const FOLD_FLOW = 'flow'; const FOLD_BLOCK = 'block'; const FOLD_QUOTED = 'quoted'; // presumes i+1 is at the start of a line // returns index of last newline in more-indented block const consumeMoreIndentedLines = (text, i) => { let ch = text[i + 1]; while (ch === ' ' || ch === '\t') { do { ch = text[i += 1]; } while (ch && ch !== '\n'); ch = text[i + 1]; } return i; }; /** * Tries to keep input at up to `lineWidth` characters, splitting only on spaces * not followed by newlines or spaces unless `mode` is `'quoted'`. Lines are * terminated with `\n` and started with `indent`. * * @param {string} text * @param {string} indent * @param {string} [mode='flow'] `'block'` prevents more-indented lines * from being folded; `'quoted'` allows for `\` escapes, including escaped * newlines * @param {Object} options * @param {number} [options.indentAtStart] Accounts for leading contents on * the first line, defaulting to `indent.length` * @param {number} [options.lineWidth=80] * @param {number} [options.minContentWidth=20] Allow highly indented lines to * stretch the line width or indent content from the start * @param {function} options.onFold Called once if the text is folded * @param {function} options.onFold Called once if any line of text exceeds * lineWidth characters */ function foldFlowLines(text, indent, mode, { indentAtStart, lineWidth = 80, minContentWidth = 20, onFold, onOverflow }) { if (!lineWidth || lineWidth < 0) return text; const endStep = Math.max(1 + minContentWidth, 1 + lineWidth - indent.length); if (text.length <= endStep) return text; const folds = []; const escapedFolds = {}; let end = lineWidth - indent.length; if (typeof indentAtStart === 'number') { if (indentAtStart > lineWidth - Math.max(2, minContentWidth)) folds.push(0);else end = lineWidth - indentAtStart; } let split = undefined; let prev = undefined; let overflow = false; let i = -1; if (mode === FOLD_BLOCK) { i = consumeMoreIndentedLines(text, i); if (i !== -1) end = i + endStep; } for (let ch; ch = text[i += 1];) { if (mode === FOLD_QUOTED && ch === '\\') { switch (text[i + 1]) { case 'x': i += 3; break; case 'u': i += 5; break; case 'U': i += 9; break; default: i += 1; } } if (ch === '\n') { if (mode === FOLD_BLOCK) i = consumeMoreIndentedLines(text, i); end = i + endStep; split = undefined; } else { if (ch === ' ' && prev && prev !== ' ' && prev !== '\n' && prev !== '\t') { // space surrounded by non-space can be replaced with newline + indent const next = text[i + 1]; if (next && next !== ' ' && next !== '\n' && next !== '\t') split = i; } if (i >= end) { if (split) { folds.push(split); end = split + endStep; split = undefined; } else if (mode === FOLD_QUOTED) { // white-space collected at end may stretch past lineWidth while (prev === ' ' || prev === '\t') { prev = ch; ch = text[i += 1]; overflow = true; } // i - 2 accounts for not-dropped last char + newline-escaping \ folds.push(i - 2); escapedFolds[i - 2] = true; end = i - 2 + endStep; split = undefined; } else { overflow = true; } } } prev = ch; } if (overflow && onOverflow) onOverflow(); if (folds.length === 0) return text; if (onFold) onFold(); let res = text.slice(0, folds[0]); for (let i = 0; i < folds.length; ++i) { const fold = folds[i]; const end = folds[i + 1] || text.length; if (fold === 0) res = `\n${indent}${text.slice(0, end)}`;else { if (mode === FOLD_QUOTED && escapedFolds[fold]) res += `${text[fold]}\\`; res += `\n${indent}${text.slice(fold + 1, end)}`; } } return res; } const getFoldOptions = ({ indentAtStart }) => indentAtStart ? Object.assign({ indentAtStart }, strOptions.fold) : strOptions.fold; // Also checks for lines starting with %, as parsing the output as YAML 1.1 will // presume that's starting a new document. const containsDocumentMarker = str => /^(%|---|\.\.\.)/m.test(str); function lineLengthOverLimit(str, limit) { const strLen = str.length; if (strLen <= limit) return false; for (let i = 0, start = 0; i < strLen; ++i) { if (str[i] === '\n') { if (i - start > limit) return true; start = i + 1; if (strLen - start <= limit) return false; } } return true; } function doubleQuotedString(value, ctx) { const { implicitKey } = ctx; const { jsonEncoding, minMultiLineLength } = strOptions.doubleQuoted; const json = JSON.stringify(value); if (jsonEncoding) return json; const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : ''); let str = ''; let start = 0; for (let i = 0, ch = json[i]; ch; ch = json[++i]) { if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') { // space before newline needs to be escaped to not be folded str += json.slice(start, i) + '\\ '; i += 1; start = i; ch = '\\'; } if (ch === '\\') switch (json[i + 1]) { case 'u': { str += json.slice(start, i); const code = json.substr(i + 2, 4); switch (code) { case '0000': str += '\\0'; break; case '0007': str += '\\a'; break; case '000b': str += '\\v'; break; case '001b': str += '\\e'; break; case '0085': str += '\\N'; break; case '00a0': str += '\\_'; break; case '2028': str += '\\L'; break; case '2029': str += '\\P'; break; default: if (code.substr(0, 2) === '00') str += '\\x' + code.substr(2);else str += json.substr(i, 6); } i += 5; start = i + 1; } break; case 'n': if (implicitKey || json[i + 2] === '"' || json.length < minMultiLineLength) { i += 1; } else { // folding will eat first newline str += json.slice(start, i) + '\n\n'; while (json[i + 2] === '\\' && json[i + 3] === 'n' && json[i + 4] !== '"') { str += '\n'; i += 2; } str += indent; // space after newline needs to be escaped to not be folded if (json[i + 2] === ' ') str += '\\'; i += 1; start = i + 1; } break; default: i += 1; } } str = start ? str + json.slice(start) : json; return implicitKey ? str : foldFlowLines(str, indent, 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); } const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : ''); const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'"; return ctx.implicitKey ? res : foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx)); } function blockString({ comment, type, value }, ctx, onComment, onChompKeep) { // 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); } const indent = ctx.indent || (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : ''); const indentSize = indent ? '2' : '1'; // root is at -1 const literal = type === _rollupPluginBabelHelpers.Type.BLOCK_FOLDED ? false : type === _rollupPluginBabelHelpers.Type.BLOCK_LITERAL ? true : !lineLengthOverLimit(value, strOptions.fold.lineWidth - indent.length); let header = literal ? '|' : '>'; if (!value) return header + '\n'; let wsStart = ''; let wsEnd = ''; value = value.replace(/[\n\t ]*$/, ws => { const n = ws.indexOf('\n'); if (n === -1) { header += '-'; // strip } else if (value === ws || n !== ws.length - 1) { header += '+'; // keep if (onChompKeep) onChompKeep(); } wsEnd = ws.replace(/\n$/, ''); return ''; }).replace(/^[\n ]*/, ws => { if (ws.indexOf(' ') !== -1) header += indentSize; const m = ws.match(/ +$/); if (m) { wsStart = ws.slice(0, -m[0].length); return m[0]; } else { wsStart = ws; return ''; } }); if (wsEnd) wsEnd = wsEnd.replace(/\n+(?!\n|$)/g, `$&${indent}`); if (wsStart) wsStart = wsStart.replace(/\n+/g, `$&${indent}`); if (comment) { header += ' #' + comment.replace(/ ?[\r\n]+/g, ' '); if (onComment) onComment(); } if (!value) return `${header}${indentSize}\n${indent}${wsEnd}`; if (literal) { value = value.replace(/\n+/g, `$&${indent}`); return `${header}\n${indent}${wsStart}${value}${wsEnd}`; } value = value.replace(/\n+/g, '\n$&').replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded // ^ ind.line ^ empty ^ capture next empty lines only at end of indent .replace(/\n+/g, `$&${indent}`); const body = foldFlowLines(`${wsStart}${value}${wsEnd}`, indent, FOLD_BLOCK, strOptions.fold); return `${header}\n${indent}${body}`; } function plainString(item, ctx, onComment, onChompKeep) { const { comment, type, value } = item; const { actualString, implicitKey, indent, inFlow } = ctx; if (implicitKey && /[\n[\]{},]/.test(value) || inFlow && /[[\]{},]/.test(value)) { return doubleQuotedString(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 (strOptions.defaultQuoteSingle) { quotedString = singleQuotedString; } else { quotedString = doubleQuotedString; } // not allowed: // - empty string, '-' or '?' // - start with an indicator character (except [?:-]) or /[?-] / // - '\n ', ': ' or ' \n' anywhere // - '#' not preceded by a non-space char // - end with ' ' or ':' return implicitKey || inFlow || value.indexOf('\n') === -1 ? quotedString(value, ctx) : blockString(item, ctx, onComment, onChompKeep); } if (!implicitKey && !inFlow && type !== _rollupPluginBabelHelpers.Type.PLAIN && value.indexOf('\n') !== -1) { // 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); } const str = value.replace(/\n+/g, `$&\n${indent}`); // Verify that output will be parsed as a string, as e.g. plain numbers and // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'), // and others in v1.1. if (actualString) { const { tags } = ctx.doc.schema; const resolved = resolveScalar(str, tags).value; if (typeof resolved !== 'string') return doubleQuotedString(value, ctx); } const body = implicitKey ? str : foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx)); if (comment && !inFlow && (body.indexOf('\n') !== -1 || comment.indexOf('\n') !== -1)) { if (onComment) onComment(); return addCommentBefore(body, indent, comment); } return body; } function stringifyString(item, ctx, onComment, onChompKeep) { const { defaultKeyType, defaultType } = strOptions; const { implicitKey, inFlow } = ctx; let { type, value } = item; if (typeof value !== 'string') { value = String(value); item = Object.assign({}, item, { value }); } if (type !== _rollupPluginBabelHelpers.Type.QUOTE_DOUBLE) { // force double quotes on control characters & unpaired surrogates if (/[\x00-\x08\x0b-\x1f\x7f-\x9f\u{D800}-\u{DFFF}]/u.test(value)) type = _rollupPluginBabelHelpers.Type.QUOTE_DOUBLE; } const _stringify = _type => { switch (_type) { case _rollupPluginBabelHelpers.Type.BLOCK_FOLDED: case _rollupPluginBabelHelpers.Type.BLOCK_LITERAL: return implicitKey || inFlow ? doubleQuotedString(value, ctx) // blocks are not valid inside flow containers : blockString(item, ctx, onComment, onChompKeep); case _rollupPluginBabelHelpers.Type.QUOTE_DOUBLE: return doubleQuotedString(value, ctx); case _rollupPluginBabelHelpers.Type.QUOTE_SINGLE: return singleQuotedString(value, ctx); case _rollupPluginBabelHelpers.Type.PLAIN: return plainString(item, ctx, onComment, onChompKeep); default: return null; } }; let res = _stringify(type); if (res === null) { const t = implicitKey ? defaultKeyType : defaultType; res = _stringify(t); if (res === null) throw new Error(`Unsupported default string type ${t}`); } return res; } function stringifyNumber({ format, minFractionDigits, tag, value }) { if (typeof value === 'bigint') return String(value); if (!isFinite(value)) return isNaN(value) ? '.nan' : value < 0 ? '-.inf' : '.inf'; let n = JSON.stringify(value); if (!format && minFractionDigits && (!tag || tag === 'tag:yaml.org,2002:float') && /^\d/.test(n)) { let i = n.indexOf('.'); if (i < 0) { i = n.length; n += '.'; } let d = minFractionDigits - (n.length - i - 1); while (d-- > 0) n += '0'; } return n; } exports.Alias = Alias; exports.Collection = Collection; exports.Node = Node; exports.Pair = Pair; exports.Scalar = Scalar; exports.YAMLMap = YAMLMap; exports.YAMLSeq = YAMLSeq; exports.addComment = addComment; exports.binaryOptions = binaryOptions; exports.boolOptions = boolOptions; exports.collectionFromPath = collectionFromPath; exports.createNode = createNode; exports.createPair = createPair; exports.findPair = findPair; exports.intOptions = intOptions; exports.isEmptyPath = isEmptyPath; exports.nullOptions = nullOptions; exports.resolveScalar = resolveScalar; exports.strOptions = strOptions; exports.stringifyNumber = stringifyNumber; exports.stringifyString = stringifyString; exports.toJS = toJS;