-
Notifications
You must be signed in to change notification settings - Fork 629
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
Changes from 5 commits
4bca2a9
7855ee3
11f762b
d37a0a3
7e4c59d
24f610b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
if ( | ||
(tag !== null && tag !== "?") || | ||
duplicate || | ||
(this.indent !== 2 && level > 0) | ||
) { | ||
compact = false; | ||
} | ||
Comment on lines
+792
to
+798
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this moved? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because compact only needs to be evaluated after |
||
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; | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?