refactor(yaml): move options to options object (#5740)

This commit is contained in:
Tim Reichen 2024-08-21 09:00:08 +02:00 committed by GitHub
parent 06d83831fb
commit c05c91416e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -513,7 +513,10 @@ export class DumperState {
// • No ending newline => unaffected; already using strip "-" chomping.
// • Ending newline => removed then restored.
// Importantly, this keeps the "+" chomp indicator from gaining an extra line.
stringifyScalar(string: string, level: number, isKey: boolean): string {
stringifyScalar(
string: string,
{ level, isKey }: { level: number; isKey: boolean },
): string {
if (string.length === 0) {
return "''";
}
@ -570,11 +573,15 @@ export class DumperState {
}
}
stringifyFlowSequence(object: unknown[], level: number): string {
stringifyFlowSequence(
array: unknown[],
{ level }: { level: number },
): string {
let result = "";
for (let index = 0; index < object.length; index += 1) {
for (let index = 0; index < array.length; index += 1) {
// Write only valid elements.
const string = this.stringifyNode(level, object[index], {
const string = this.stringifyNode(array[index], {
level,
block: false,
compact: false,
isKey: false,
@ -588,15 +595,15 @@ export class DumperState {
}
stringifyBlockSequence(
object: unknown[],
level: number,
compact: boolean,
array: unknown[],
{ level, compact }: { level: number; compact: boolean },
): string {
let result = "";
for (let index = 0; index < object.length; index += 1) {
for (let index = 0; index < array.length; index += 1) {
// Write only valid elements.
const string = this.stringifyNode(level + 1, object[index], {
const string = this.stringifyNode(array[index], {
level: level + 1,
block: true,
compact: true,
isKey: false,
@ -619,7 +626,10 @@ export class DumperState {
return result || "[]"; // Empty sequence if no valid values.
}
stringifyFlowMapping(object: Record<string, unknown>, level: number): string {
stringifyFlowMapping(
object: Record<string, unknown>,
{ level }: { level: number },
): string {
let result = "";
const objectKeyList = Object.keys(object);
@ -630,7 +640,8 @@ export class DumperState {
const objectValue = object[objectKey];
const keyString = this.stringifyNode(level, objectKey, {
const keyString = this.stringifyNode(objectKey, {
level,
block: false,
compact: false,
isKey: false,
@ -647,7 +658,8 @@ export class DumperState {
this.condenseFlow ? "" : " "
}`;
const valueString = this.stringifyNode(level, objectValue, {
const valueString = this.stringifyNode(objectValue, {
level,
block: false,
compact: false,
isKey: false,
@ -668,9 +680,11 @@ export class DumperState {
stringifyBlockMapping(
object: Record<string, unknown>,
tag: string | null,
level: number,
compact: boolean,
{ tag, level, compact }: {
tag: string | null;
level: number;
compact: boolean;
},
): string {
const objectKeyList = Object.keys(object);
let result = "";
@ -696,7 +710,8 @@ export class DumperState {
const objectValue = object[objectKey];
const keyString = this.stringifyNode(level + 1, objectKey, {
const keyString = this.stringifyNode(objectKey, {
level: level + 1,
block: true,
compact: true,
isKey: true,
@ -722,7 +737,8 @@ export class DumperState {
pairBuffer += generateNextLine(this.indent, level);
}
const valueString = this.stringifyNode(level + 1, objectValue, {
const valueString = this.stringifyNode(objectValue, {
level: level + 1,
block: true,
compact: explicitPair,
isKey: false,
@ -749,33 +765,33 @@ export class DumperState {
}
detectType(
object: unknown,
value: unknown,
explicit: boolean,
): { tag: string; object: unknown } | null {
): { tag: string; value: unknown } | null {
const typeList = explicit ? this.explicitTypes : this.implicitTypes;
let tag = null;
for (const type of typeList) {
if (type.predicate?.(object)) {
if (type.predicate?.(value)) {
tag = explicit ? type.tag : "?";
if (type.represent) {
const style = this.styleMap[type.tag]! || type.defaultStyle;
if (typeof type.represent === "function") {
object = type.represent(object, style);
return { tag, object };
value = type.represent(value, style);
return { tag, value };
}
if (Object.hasOwn(type.represent, style)) {
object = type.represent[style]!(object, style);
return { tag, object };
value = type.represent[style]!(value, style);
return { tag, value };
}
throw new TypeError(
`!<${type.tag}> tag resolver accepts not "${style}" style`,
);
}
return { tag, object };
return { tag, value };
}
}
return null;
@ -783,27 +799,28 @@ export class DumperState {
// Serializes `object` and writes it to global `result`.
// Returns true on success, or false on invalid object.
stringifyNode(level: number, object: unknown, { block, compact, isKey }: {
stringifyNode(value: unknown, { level, block, compact, isKey }: {
level: number;
block: boolean;
compact: boolean;
isKey: boolean;
}): string | null {
const result = this.detectType(object, false) ??
this.detectType(object, true) ?? { tag: null, object };
const result = this.detectType(value, false) ??
this.detectType(value, true) ?? { tag: null, value };
const tag = result.tag;
object = result.object;
value = result.value;
if (block) {
block = this.flowLevel < 0 || this.flowLevel > level;
}
const objectOrArray = isObject(object) ||
Array.isArray(object);
const objectOrArray = isObject(value) ||
Array.isArray(value);
let duplicateIndex = -1;
let duplicate = false;
if (objectOrArray) {
duplicateIndex = this.duplicates.indexOf(object);
duplicateIndex = this.duplicates.indexOf(value);
duplicate = duplicateIndex !== -1;
}
@ -815,72 +832,76 @@ export class DumperState {
compact = false;
}
if (duplicate && this.usedDuplicates.has(object)) {
if (duplicate && this.usedDuplicates.has(value)) {
return `*ref_${duplicateIndex}`;
} else {
if (objectOrArray && duplicate) {
this.usedDuplicates.add(object);
this.usedDuplicates.add(value);
}
if (isObject(object) && !Array.isArray(object)) {
if (block && Object.keys(object).length !== 0) {
object = this.stringifyBlockMapping(object, tag, level, compact);
if (isObject(value) && !Array.isArray(value)) {
if (block && Object.keys(value).length !== 0) {
value = this.stringifyBlockMapping(value, { tag, level, compact });
if (duplicate) {
object = `&ref_${duplicateIndex}${object}`;
value = `&ref_${duplicateIndex}${value}`;
}
} else {
object = this.stringifyFlowMapping(object, level);
value = this.stringifyFlowMapping(value, { level });
if (duplicate) {
object = `&ref_${duplicateIndex} ${object}`;
value = `&ref_${duplicateIndex} ${value}`;
}
}
} else if (Array.isArray(object)) {
} else if (Array.isArray(value)) {
const arrayLevel = !this.arrayIndent && level > 0 ? level - 1 : level;
if (block && object.length !== 0) {
object = this.stringifyBlockSequence(object, arrayLevel, compact);
if (block && value.length !== 0) {
value = this.stringifyBlockSequence(value, {
level: arrayLevel,
compact,
});
if (duplicate) {
object = `&ref_${duplicateIndex}${object}`;
value = `&ref_${duplicateIndex}${value}`;
}
} else {
object = this.stringifyFlowSequence(object, arrayLevel);
value = this.stringifyFlowSequence(value, { level: arrayLevel });
if (duplicate) {
object = `&ref_${duplicateIndex} ${object}`;
value = `&ref_${duplicateIndex} ${value}`;
}
}
} else if (typeof object === "string") {
} else if (typeof value === "string") {
if (tag !== "?") {
object = this.stringifyScalar(object, level, isKey);
value = this.stringifyScalar(value, { level, isKey });
}
} else {
if (this.skipInvalid) return null;
throw new TypeError(
`unacceptable kind of an object to dump ${
getObjectTypeString(object)
getObjectTypeString(value)
}`,
);
}
if (tag !== null && tag !== "?") {
object = `!<${tag}> ${object}`;
value = `!<${tag}> ${value}`;
}
}
return object as string;
return value as string;
}
getDuplicateReferences(object: unknown) {
const objects: unknown[] = [];
getDuplicateReferences(value: unknown) {
const values: unknown[] = [];
const duplicateObjects: Set<unknown> = new Set();
inspectNode(object, objects, duplicateObjects);
inspectNode(value, values, duplicateObjects);
for (const object of duplicateObjects) this.duplicates.push(object);
for (const value of duplicateObjects) this.duplicates.push(value);
this.usedDuplicates = new Set();
}
stringify(data: unknown): string {
if (this.useAnchors) this.getDuplicateReferences(data);
stringify(value: unknown): string {
if (this.useAnchors) this.getDuplicateReferences(value);
const string = this.stringifyNode(0, data, {
const string = this.stringifyNode(value, {
level: 0,
block: true,
compact: true,
isKey: false,