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): move duplicate and duplicateIndex #5836

Merged
merged 6 commits into from
Aug 29, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 46 additions & 54 deletions yaml/_dumper_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,69 +781,61 @@
block = this.flowLevel < 0 || this.flowLevel > level;
}

let duplicateIndex = -1;
let duplicate = false;
if (isObject(value)) {
duplicateIndex = this.duplicates.indexOf(value);
duplicate = duplicateIndex !== -1;
}

if (
(tag !== null && tag !== "?") ||
duplicate ||
(this.indent !== 2 && level > 0)
) {
compact = false;
}
const duplicateIndex = this.duplicates.indexOf(value);
const duplicate = duplicateIndex !== -1;

if (duplicate && this.usedDuplicates.has(value)) {
return `*ref_${duplicateIndex}`;
} else {
if (isObject(value)) {
if (duplicate) {
this.usedDuplicates.add(value);
}
if (!Array.isArray(value)) {
if (block && Object.keys(value).length !== 0) {
value = this.stringifyBlockMapping(value, { tag, level, compact });
if (duplicate) {
value = `&ref_${duplicateIndex}${value}`;
}
} else {
value = this.stringifyFlowMapping(value, { level });
if (duplicate) {
value = `&ref_${duplicateIndex} ${value}`;
}
if (duplicate) {
if (this.usedDuplicates.has(value)) return `*ref_${duplicateIndex}`;
this.usedDuplicates.add(value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to pertain to the main objective of this PR.

Copy link
Contributor Author

@timreichen timreichen Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (duplicate) {...} would be checked twice otherwise, no?

}
if (

Check warning on line 792 in yaml/_dumper_state.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper_state.ts#L792

Added line #L792 was not covered by tests
(tag !== null && tag !== "?") ||
duplicate ||
(this.indent !== 2 && level > 0)
) {
compact = false;
}
Comment on lines +792 to +798
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this moved?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because compact only needs to be evaluated after if (this.usedDuplicates.has(value)) return ``*ref_${duplicateIndex}``; doesn't return early.

if (!Array.isArray(value)) {
if (block && Object.keys(value).length !== 0) {
value = this.stringifyBlockMapping(value, { tag, level, compact });
if (duplicate) {
value = `&ref_${duplicateIndex}${value}`;
}
} else {
const arrayLevel = !this.arrayIndent && level > 0 ? level - 1 : level;
if (block && value.length !== 0) {
value = this.stringifyBlockSequence(value, {
level: arrayLevel,
compact,
});
if (duplicate) {
value = `&ref_${duplicateIndex}${value}`;
}
} else {
value = this.stringifyFlowSequence(value, { level: arrayLevel });
if (duplicate) {
value = `&ref_${duplicateIndex} ${value}`;
}
value = this.stringifyFlowMapping(value, { level });
if (duplicate) {
value = `&ref_${duplicateIndex} ${value}`;
}
}
} else if (typeof value === "string") {
if (tag !== "?") {
value = this.stringifyScalar(value, { level, isKey });
}
} else {
if (this.skipInvalid) return null;
throw new TypeError(`Cannot stringify ${typeof value}`);
const arrayLevel = !this.arrayIndent && level > 0 ? level - 1 : level;
if (block && value.length !== 0) {
value = this.stringifyBlockSequence(value, {
level: arrayLevel,
compact,
});
if (duplicate) {
value = `&ref_${duplicateIndex}${value}`;
}
} else {
value = this.stringifyFlowSequence(value, { level: arrayLevel });
if (duplicate) {
value = `&ref_${duplicateIndex} ${value}`;
}
}
}

if (tag !== null && tag !== "?") {
value = `!<${tag}> ${value}`;
} else if (typeof value === "string") {
if (tag !== "?") {
value = this.stringifyScalar(value, { level, isKey });
}
} else {
if (this.skipInvalid) return null;
throw new TypeError(`Cannot stringify ${typeof value}`);
}

if (tag !== null && tag !== "?") {
value = `!<${tag}> ${value}`;
}

return value as string;
Expand Down