File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -108,25 +108,20 @@ describe("addBodyLinesToUnreleasedSection", async () => {
108108 } ;
109109 }
110110
111- await it ( "throws error if '[UNRELEASED]' section does not exist" , async ( ) => {
112- const emptyChangelog = newChangelogWithSections ( [ ] ) ;
113- assert . throws ( ( ) => addBodyLinesToUnreleasedSection ( emptyChangelog , [ ] ) ) ;
114-
115- const releasedChangelog = newChangelogWithSections ( [
111+ await it ( "throws error if '[UNRELEASED]' section is not first" , async ( ) => {
112+ const invalidChangelog = newChangelogWithSections ( [
116113 {
117114 headerLine : "## Release 1.0.0" ,
118115 bodyLines : [ ] ,
119116 } ,
120117 {
121- headerLine : "## Release 2.0.0" ,
122- bodyLines : [ ] ,
123- } ,
124- {
125- headerLine : "## Release 3.0.0" ,
118+ headerLine : `## ${ UNRELEASED_PLACEHOLDER } ` ,
126119 bodyLines : [ ] ,
127120 } ,
128121 ] ) ;
129- assert . throws ( ( ) => addBodyLinesToUnreleasedSection ( releasedChangelog , [ ] ) ) ;
122+ assert . throws ( ( ) =>
123+ addBodyLinesToUnreleasedSection ( invalidChangelog , [ "foo" ] ) ,
124+ ) ;
130125 } ) ;
131126
132127 await it ( "overwrites 'No user facing changes.'" , async ( ) => {
Original file line number Diff line number Diff line change @@ -148,32 +148,21 @@ export function addBodyLinesToUnreleasedSection(
148148 changelog : Changelog ,
149149 lines : string [ ] ,
150150) {
151+ // Do nothing if there is nothing to insert.
152+ if ( lines . length === 0 ) return ;
153+
151154 const unreleasedSection = changelog . sections [ 0 ] ;
152155 if ( getHeader ( unreleasedSection ) !== UNRELEASED_PLACEHOLDER ) {
153156 throw Error ( "'[UNRELEASED]' is not the first section of 'CHANGELOG.md'" ) ;
154157 }
155158
156- let insertAtIndex = 0 ;
157- let deleteCount = 0 ;
158-
159- // If the section contains an empty line, preserve it -- insert afterward.
160- if (
161- unreleasedSection . bodyLines . length > 0 &&
162- unreleasedSection . bodyLines [ 0 ] === ""
163- ) {
164- insertAtIndex ++ ;
165- }
166-
167- // If the section contains the stock message 'No user facing changes.'
168- if (
169- lines . length > 0 &&
170- unreleasedSection . bodyLines . length > insertAtIndex &&
171- unreleasedSection . bodyLines [ insertAtIndex ] . trim ( ) === NO_CHANGES_STR
172- ) {
173- deleteCount ++ ; // Delete the line by incrementing the delete marker.
159+ if ( unreleasedSection . bodyLines . includes ( NO_CHANGES_STR ) ) {
160+ unreleasedSection . bodyLines = [ "" , ...lines , "" ] ;
161+ return ;
174162 }
175163
176- unreleasedSection . bodyLines . splice ( insertAtIndex , deleteCount , ...lines ) ;
164+ // Insert `lines` after the first blank line.
165+ unreleasedSection . bodyLines . splice ( 1 , 0 , ...lines ) ;
177166}
178167
179168/**
You can’t perform that action at this time.
0 commit comments