Simplify addBodyLinesToUnreleasedSection

This commit is contained in:
Mario Campos
2026-09-18 12:35:41 -05:00
parent 128614ad8b
commit bc0efd6d91
2 changed files with 14 additions and 30 deletions

View File

@@ -108,25 +108,20 @@ describe("addBodyLinesToUnreleasedSection", async () => {
};
}
await it("throws error if '[UNRELEASED]' section does not exist", async () => {
const emptyChangelog = newChangelogWithSections([]);
assert.throws(() => addBodyLinesToUnreleasedSection(emptyChangelog, []));
const releasedChangelog = newChangelogWithSections([
await it("throws error if '[UNRELEASED]' section is not first", async () => {
const invalidChangelog = newChangelogWithSections([
{
headerLine: "## Release 1.0.0",
bodyLines: [],
},
{
headerLine: "## Release 2.0.0",
bodyLines: [],
},
{
headerLine: "## Release 3.0.0",
headerLine: `## ${UNRELEASED_PLACEHOLDER}`,
bodyLines: [],
},
]);
assert.throws(() => addBodyLinesToUnreleasedSection(releasedChangelog, []));
assert.throws(() =>
addBodyLinesToUnreleasedSection(invalidChangelog, ["foo"]),
);
});
await it("overwrites 'No user facing changes.'", async () => {

View File

@@ -148,32 +148,21 @@ export function addBodyLinesToUnreleasedSection(
changelog: Changelog,
lines: string[],
) {
// Do nothing if there is nothing to insert.
if (lines.length === 0) return;
const unreleasedSection = changelog.sections[0];
if (getHeader(unreleasedSection) !== UNRELEASED_PLACEHOLDER) {
throw Error("'[UNRELEASED]' is not the first section of 'CHANGELOG.md'");
}
let insertAtIndex = 0;
let deleteCount = 0;
// If the section contains an empty line, preserve it -- insert afterward.
if (
unreleasedSection.bodyLines.length > 0 &&
unreleasedSection.bodyLines[0] === ""
) {
insertAtIndex++;
if (unreleasedSection.bodyLines.includes(NO_CHANGES_STR)) {
unreleasedSection.bodyLines = ["", ...lines, ""];
return;
}
// If the section contains the stock message 'No user facing changes.'
if (
lines.length > 0 &&
unreleasedSection.bodyLines.length > insertAtIndex &&
unreleasedSection.bodyLines[insertAtIndex].trim() === NO_CHANGES_STR
) {
deleteCount++; // Delete the line by incrementing the delete marker.
}
unreleasedSection.bodyLines.splice(insertAtIndex, deleteCount, ...lines);
// Insert `lines` after the first blank line.
unreleasedSection.bodyLines.splice(1, 0, ...lines);
}
/**