Skip to content

fix: correct code-map line counting#508

Merged
jahooma merged 1 commit intoCodebuffAI:mainfrom
hiSandog:fix/code-map-line-count-20260418
Apr 18, 2026
Merged

fix: correct code-map line counting#508
jahooma merged 1 commit intoCodebuffAI:mainfrom
hiSandog:fix/code-map-line-count-20260418

Conversation

@hiSandog
Copy link
Copy Markdown
Contributor

Summary

  • fix parseTokens() so multiline files count newlines correctly by applying the nullish fallback before adding 1
  • update the code-map regression test to expect the correct line count for a 3-line input

Validation

  • reproduced the root cause locally with Node: the old expression returns 2 for a 3-line string, while the fixed expression returns 3
  • full bun test packages/code-map/tests/parse.test.ts could not be run in this environment because bun is not installed locally

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 18, 2026

Greptile Summary

This PR fixes an operator-precedence bug in parseTokens() inside packages/code-map/src/parse.ts that caused numLines to be under-counted by 1 for any file with at least one newline.

Root cause: JavaScript's ?? operator has lower precedence than +, so the original expression was evaluated as ?.length ?? (0 + 1), meaning + 1 was only applied in the null branch. A 3-line file (2 newlines) reported numLines = 2 instead of 3.

Fix: (sourceCode.match(/ /g)?.length ?? 0) + 1 — parentheses ensure + 1 is always applied.

Test update: The regression test expectation is corrected from 2 to 3 for the 3-line input. Edge cases (empty string, null source) are all handled correctly.

Confidence Score: 5/5

Safe to merge — minimal, correct operator-precedence fix with an updated regression test.

The change is a single-line fix for a well-understood operator-precedence bug. The fix is provably correct, all edge cases (empty string, null source) are already handled, and the test expectation is updated to match the correct behavior. No other logic is touched.

No files require special attention.

Important Files Changed

Filename Overview
packages/code-map/src/parse.ts One-line operator-precedence fix: wraps (... ?? 0) in parentheses before + 1 so the newline count is always incremented correctly.
packages/code-map/tests/parse.test.ts Updates the multiline regression test expectation from the old (wrong) value of 2 to the correct value of 3 for a 3-line input string.

Reviews (1): Last reviewed commit: "fix: correct code-map line counting" | Re-trigger Greptile

@jahooma jahooma merged commit a75349a into CodebuffAI:main Apr 18, 2026
9 of 11 checks passed
@jahooma
Copy link
Copy Markdown
Contributor

jahooma commented Apr 18, 2026

Awesome, thanks!

jahooma pushed a commit that referenced this pull request Apr 18, 2026
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.

2 participants