1
0
Fork 0
mirror of https://github.com/dawidd6/action-ansible-playbook.git synced 2024-11-27 02:27:11 +00:00
action-ansible-playbook/node_modules/yaml/dist/stringify/stringify.js

128 lines
4.2 KiB
JavaScript
Raw Normal View History

2022-10-21 14:44:44 +00:00
'use strict';
var anchors = require('../doc/anchors.js');
2024-01-17 09:08:10 +00:00
var identity = require('../nodes/identity.js');
2022-10-21 14:44:44 +00:00
var stringifyComment = require('./stringifyComment.js');
var stringifyString = require('./stringifyString.js');
function createStringifyContext(doc, options) {
const opt = Object.assign({
blockQuote: true,
commentString: stringifyComment.stringifyComment,
defaultKeyType: null,
defaultStringType: 'PLAIN',
directives: null,
doubleQuotedAsJSON: false,
doubleQuotedMinMultiLineLength: 40,
falseStr: 'false',
2024-01-17 09:08:10 +00:00
flowCollectionPadding: true,
2022-10-21 14:44:44 +00:00
indentSeq: true,
lineWidth: 80,
minContentWidth: 20,
nullStr: 'null',
simpleKeys: false,
singleQuote: null,
trueStr: 'true',
verifyAliasOrder: true
}, doc.schema.toStringOptions, options);
let inFlow;
switch (opt.collectionStyle) {
case 'block':
inFlow = false;
break;
case 'flow':
inFlow = true;
break;
default:
inFlow = null;
}
return {
anchors: new Set(),
doc,
2024-01-17 09:08:10 +00:00
flowCollectionPadding: opt.flowCollectionPadding ? ' ' : '',
2022-10-21 14:44:44 +00:00
indent: '',
indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ',
inFlow,
options: opt
};
}
function getTagObject(tags, item) {
if (item.tag) {
const match = tags.filter(t => t.tag === item.tag);
if (match.length > 0)
return match.find(t => t.format === item.format) ?? match[0];
}
let tagObj = undefined;
let obj;
2024-01-17 09:08:10 +00:00
if (identity.isScalar(item)) {
2022-10-21 14:44:44 +00:00
obj = item.value;
const match = tags.filter(t => t.identify?.(obj));
tagObj =
match.find(t => t.format === item.format) ?? match.find(t => !t.format);
}
else {
obj = item;
tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
}
if (!tagObj) {
const name = obj?.constructor?.name ?? typeof obj;
throw new Error(`Tag not resolved for ${name} value`);
}
return tagObj;
}
// needs to be called before value stringifier to allow for circular anchor refs
function stringifyProps(node, tagObj, { anchors: anchors$1, doc }) {
if (!doc.directives)
return '';
const props = [];
2024-01-17 09:08:10 +00:00
const anchor = (identity.isScalar(node) || identity.isCollection(node)) && node.anchor;
2022-10-21 14:44:44 +00:00
if (anchor && anchors.anchorIsValid(anchor)) {
anchors$1.add(anchor);
props.push(`&${anchor}`);
}
const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
if (tag)
props.push(doc.directives.tagString(tag));
return props.join(' ');
}
function stringify(item, ctx, onComment, onChompKeep) {
2024-01-17 09:08:10 +00:00
if (identity.isPair(item))
2022-10-21 14:44:44 +00:00
return item.toString(ctx, onComment, onChompKeep);
2024-01-17 09:08:10 +00:00
if (identity.isAlias(item)) {
2022-10-21 14:44:44 +00:00
if (ctx.doc.directives)
return item.toString(ctx);
if (ctx.resolvedAliases?.has(item)) {
throw new TypeError(`Cannot stringify circular structure without alias nodes`);
}
else {
if (ctx.resolvedAliases)
ctx.resolvedAliases.add(item);
else
ctx.resolvedAliases = new Set([item]);
item = item.resolve(ctx.doc);
}
}
let tagObj = undefined;
2024-01-17 09:08:10 +00:00
const node = identity.isNode(item)
2022-10-21 14:44:44 +00:00
? item
: ctx.doc.createNode(item, { onTagObj: o => (tagObj = o) });
if (!tagObj)
tagObj = getTagObject(ctx.doc.schema.tags, node);
const props = stringifyProps(node, tagObj, ctx);
if (props.length > 0)
ctx.indentAtStart = (ctx.indentAtStart ?? 0) + props.length + 1;
const str = typeof tagObj.stringify === 'function'
? tagObj.stringify(node, ctx, onComment, onChompKeep)
2024-01-17 09:08:10 +00:00
: identity.isScalar(node)
2022-10-21 14:44:44 +00:00
? stringifyString.stringifyString(node, ctx, onComment, onChompKeep)
: node.toString(ctx, onComment, onChompKeep);
if (!props)
return str;
2024-01-17 09:08:10 +00:00
return identity.isScalar(node) || str[0] === '{' || str[0] === '['
2022-10-21 14:44:44 +00:00
? `${props} ${str}`
: `${props}\n${ctx.indent}${str}`;
}
exports.createStringifyContext = createStringifyContext;
exports.stringify = stringify;