diff --git a/yaml/_dumper_state.ts b/yaml/_dumper_state.ts index 6928e99b80a8..aecac83d4a63 100644 --- a/yaml/_dumper_state.ts +++ b/yaml/_dumper_state.ts @@ -480,7 +480,6 @@ export class DumperState { condenseFlow: boolean; implicitTypes: Type<"scalar">[]; explicitTypes: Type[]; - tag: string | null = null; duplicates: unknown[] = []; usedDuplicates: Set = new Set(); styleMap: ArrayObject; @@ -590,7 +589,6 @@ export class DumperState { writeFlowSequence(level: number) { let _result = ""; const object = this.dump; - const _tag = this.tag; for (let index = 0; index < object.length; index += 1) { // Write only valid elements. if ( @@ -605,14 +603,12 @@ export class DumperState { } } - this.tag = _tag; this.dump = `[${_result}]`; } writeBlockSequence(level: number, compact: boolean) { let _result = ""; const object = this.dump; - const _tag = this.tag; for (let index = 0; index < object.length; index += 1) { // Write only valid elements. @@ -637,13 +633,11 @@ export class DumperState { } } - this.tag = _tag; this.dump = _result || "[]"; // Empty sequence if no valid values. } writeFlowMapping(level: number) { let _result = ""; - const _tag = this.tag; const object = this.dump; const objectKeyList = Object.keys(object); @@ -686,13 +680,11 @@ export class DumperState { _result += pairBuffer; } - this.tag = _tag; this.dump = `{${_result}}`; } - writeBlockMapping(level: number, compact: boolean) { + writeBlockMapping(tag: string | null, level: number, compact: boolean) { const object = this.dump; - const _tag = this.tag; const objectKeyList = Object.keys(object); let _result = ""; @@ -727,7 +719,7 @@ export class DumperState { continue; // Skip this pair because of invalid key. } - const explicitPair = (this.tag !== null && this.tag !== "?") || + const explicitPair = (tag !== null && tag !== "?") || (this.dump && this.dump.length > 1024); if (explicitPair) { @@ -766,39 +758,38 @@ export class DumperState { _result += pairBuffer; } - this.tag = _tag; this.dump = _result || "{}"; // Empty mapping if no valid pairs. } - detectType(explicit: boolean): boolean { + detectType(explicit: boolean): string | null { const object = this.dump; const typeList = explicit ? this.explicitTypes : this.implicitTypes; + let tag = null; for (const type of typeList) { if (type.predicate?.(object)) { - this.tag = explicit ? type.tag : "?"; + tag = explicit ? type.tag : "?"; if (type.represent) { const style = this.styleMap[type.tag]! || type.defaultStyle; if (typeof type.represent === "function") { this.dump = type.represent(object, style); - return true; + return tag; } if (Object.hasOwn(type.represent, style)) { this.dump = type.represent[style]!(object, style); - return true; + return tag; } throw new TypeError( `!<${type.tag}> tag resolver accepts not "${style}" style`, ); } - return true; + return tag; } } - - return false; + return tag; } // Serializes `object` and writes it to global `result`. @@ -812,12 +803,9 @@ export class DumperState { isKey: boolean; }, ): boolean { - this.tag = null; this.dump = object; - if (!this.detectType(false)) { - this.detectType(true); - } + const tag = this.detectType(false) ?? this.detectType(true); if (block) { block = this.flowLevel < 0 || this.flowLevel > level; @@ -834,7 +822,7 @@ export class DumperState { } if ( - (this.tag !== null && this.tag !== "?") || + (tag !== null && tag !== "?") || duplicate || (this.indent !== 2 && level > 0) ) { @@ -849,7 +837,7 @@ export class DumperState { } if (isObject(this.dump) && !Array.isArray(this.dump)) { if (block && Object.keys(this.dump).length !== 0) { - this.writeBlockMapping(level, compact); + this.writeBlockMapping(tag, level, compact); if (duplicate) { this.dump = `&ref_${duplicateIndex}${this.dump}`; } @@ -873,7 +861,7 @@ export class DumperState { } } } else if (typeof this.dump === "string") { - if (this.tag !== "?") { + if (tag !== "?") { this.writeScalar(this.dump, level, isKey); } } else { @@ -885,8 +873,8 @@ export class DumperState { ); } - if (this.tag !== null && this.tag !== "?") { - this.dump = `!<${this.tag}> ${this.dump}`; + if (tag !== null && tag !== "?") { + this.dump = `!<${tag}> ${this.dump}`; } }