Skip to content

fix: avoid crash when contributors tag is at the start of a file - #521

Open
davidpavlovschi wants to merge 1 commit into
all-contributors:mainfrom
davidpavlovschi:fix/contributors-tag-at-start-of-file
Open

fix: avoid crash when contributors tag is at the start of a file#521
davidpavlovschi wants to merge 1 commit into
all-contributors:mainfrom
davidpavlovschi:fix/contributors-tag-at-start-of-file

Conversation

@davidpavlovschi

Copy link
Copy Markdown

What:

Fixes the crash in all-contributors add/generate when the file being updated starts immediately with the list tag (no title or any content before it — the p5.js CONTRIBUTORS.md case from #376). As reported in the issue:

RangeError: Invalid count value: -1
    at String.repeat (<anonymous>)
    at .../dist/generate/index.js:19:186
    at module.exports (.../dist/generate/index.js:75:78)

Closes #376

Why:

In injectListBetweenTags (src/generate/index.js), nbSpaces is derived from lastIndexOf('\n', startOfOpeningTagIndex). When the tag is at the very start of the file, lastIndexOf returns -1, Math.max(0, …) clamps it to 0, so nbSpaces is 0 and ' '.repeat(nbSpaces - 1) throws RangeError: Invalid count value: -1. For a tag at column 0 on any later line, nbSpaces is 1 and the same expression is repeat(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.js that runs generate() over content whose first byte is <!-- ALL-CONTRIBUTORS-LIST:START --> and asserts a table is injected. It fails on main (reproducing the same RangeError at the String.repeat frame, seen from the test runner rather than the built dist/ 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/generate and npx prettier --check clean.

Checklist:

  • Documentation N/A
  • Tests
  • Ready to be merged
  • Added myself to contributors table N/A

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash when adding contributors and there is no content before the all contributors comment tag

1 participant