From 7def00e3778077fa7a6147a9469957624549df34 Mon Sep 17 00:00:00 2001 From: davidpavlovschi Date: Thu, 30 Jul 2026 22:59:31 +0300 Subject: [PATCH] fix: avoid crash when contributors tag is at the start of a file When a file begins with the ALL-CONTRIBUTORS-LIST:START tag, nbSpaces is 0 and ' '.repeat(nbSpaces - 1) throws RangeError: Invalid count value: -1. Clamp the indent to zero, which matches the width already produced for a tag sitting at column 0 on any later line. Closes #376 --- src/generate/__tests__/index.js | 17 +++++++++++++++++ src/generate/index.js | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/generate/__tests__/index.js b/src/generate/__tests__/index.js index 8b247f9a..33a06c87 100644 --- a/src/generate/__tests__/index.js +++ b/src/generate/__tests__/index.js @@ -277,3 +277,20 @@ test('validate if cell width attribute is floored correctly', () => { expect(result).toMatchSnapshot() }) + +test('inject the table when the ALL-CONTRIBUTORS-LIST tag starts the file', () => { + const {kentcdodds, bogas04} = contributors + const {options} = fixtures() + const contributorList = [kentcdodds, bogas04] + const content = [ + 'FOO BAR BAZ', + '', + 'Thanks a lot everyone!', + ].join('\n') + + const result = generate(options, contributorList, content) + + expect(result).toContain('') + expect(result).toContain('') + expect(result).not.toContain('FOO BAR BAZ') +}) diff --git a/src/generate/index.js b/src/generate/index.js index 07ad73f7..61082c59 100644 --- a/src/generate/index.js +++ b/src/generate/index.js @@ -42,7 +42,7 @@ function injectListBetweenTags(newContent) { previousContent.slice(0, endOfOpeningTagIndex + closingTag.length), '\n', '\n', - newContent.replace('\n', `\n${' '.repeat(nbSpaces - 1)}`), + newContent.replace('\n', `\n${' '.repeat(Math.max(0, nbSpaces - 1))}`), '', '\n', '\n\n',