Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(yaml): remove tag property #5689

Merged
merged 14 commits into from
Aug 20, 2024
Merged
42 changes: 15 additions & 27 deletions yaml/_dumper_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ export class DumperState {
condenseFlow: boolean;
implicitTypes: Type<"scalar">[];
explicitTypes: Type<KindType>[];
tag: string | null = null;
duplicates: unknown[] = [];
usedDuplicates: Set<unknown> = new Set();
styleMap: ArrayObject<StyleVariant>;
Expand Down Expand Up @@ -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 (
Expand All @@ -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.
Expand All @@ -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);

Expand Down Expand Up @@ -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 = "";

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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`.
Expand All @@ -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) ?? null;
timreichen marked this conversation as resolved.
Show resolved Hide resolved

if (block) {
block = this.flowLevel < 0 || this.flowLevel > level;
Expand All @@ -834,7 +822,7 @@ export class DumperState {
}

if (
(this.tag !== null && this.tag !== "?") ||
(tag !== null && tag !== "?") ||
duplicate ||
(this.indent !== 2 && level > 0)
) {
Expand All @@ -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}`;
}
Expand All @@ -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 {
Expand All @@ -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}`;
}
}

Expand Down