fix: avoid crash when contributors tag is at the start of a file - #521
Open
davidpavlovschi wants to merge 1 commit into
Open
Conversation
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 all-contributors#376
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What:
Fixes the crash in
all-contributors add/generatewhen the file being updated starts immediately with the list tag (no title or any content before it — the p5.jsCONTRIBUTORS.mdcase from #376). As reported in the issue:Closes #376
Why:
In
injectListBetweenTags(src/generate/index.js),nbSpacesis derived fromlastIndexOf('\n', startOfOpeningTagIndex). When the tag is at the very start of the file,lastIndexOfreturns-1,Math.max(0, …)clamps it to0, sonbSpacesis0and' '.repeat(nbSpaces - 1)throwsRangeError: Invalid count value: -1. For a tag at column 0 on any later line,nbSpacesis1and the same expression isrepeat(0)— so a zero indent is exactly the established behaviour for an unindented tag; only the file-start position crashes.How:
Clamp the count:
' '.repeat(Math.max(0, nbSpaces - 1)). No existing output changes — every current snapshot passes untouched. The sibling badge path (replaceBadge,' '.repeat(nbSpaces)) subtracts nothing and cannot go negative, so it is deliberately left alone.Added a regression test in
src/generate/__tests__/index.jsthat runsgenerate()over content whose first byte is<!-- ALL-CONTRIBUTORS-LIST:START -->and asserts a table is injected. It fails onmain(reproducing the sameRangeErrorat theString.repeatframe, seen from the test runner rather than the builtdist/bundle) and passes with this change.Verified locally with
npm ci:npx vitest run— 15 files, 114 tests, all passing, no snapshot updates;npx eslint src/generateandnpx prettier --checkclean.Checklist: