Skip to content

fix: correctly handle falsy values in HistoryNavigator._currentPosition - #301176

Open
Sangeet (Muszic) wants to merge 8 commits into
microsoft:mainfrom
Muszic:fix-history-falsy-check
Open

Sangeet (Muszic) wants to merge 8 commits into
microsoft:mainfrom
Muszic:fix-history-falsy-check

Conversation

@Muszic

Copy link
Copy Markdown
Contributor

This PR fixes a bug in HistoryNavigator._currentPosition() where a falsy check (!currentElement) was used instead of a strict null check (currentElement === null).

The Issue:
If the history contained a valid falsy value (such as an empty string '' from a cleared search input), !currentElement evaluated to true, causing _currentPosition() to incorrectly return -1. This broke state-dependent methods like isFirst(), isLast(), and previous().

The Fix:
Replaced if (!currentElement) with if (currentElement === null) to ensure valid falsy values like '', 0, or false are properly indexed.

Tests:
Added a regression test to history.test.ts to ensure isFirst() and previous() correctly handle the start of the history array when the first element is a falsy value.

@Muszic

Copy link
Copy Markdown
Contributor Author

Can you review the pr please Federico Brancasi (@federicobrancasi)

2 similar comments
@Muszic

Copy link
Copy Markdown
Contributor Author

Can you review the pr please Federico Brancasi (@federicobrancasi)

@Muszic

Copy link
Copy Markdown
Contributor Author

Can you review the pr please Federico Brancasi (@federicobrancasi)

@Muszic

Copy link
Copy Markdown
Contributor Author

review the pr please Federico Brancasi (@federicobrancasi)

@Muszic

Copy link
Copy Markdown
Contributor Author

Copilot AI review requested due to automatic review settings June 2, 2026 09:21
@Muszic

Copy link
Copy Markdown
Contributor Author

Federico Brancasi (@federicobrancasi) review the PR please

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR fixes HistoryNavigator handling of falsy history entries (e.g., empty string) by avoiding a truthiness check when determining the current position, and adds a regression test to cover the scenario.

Changes:

  • Update _currentPosition() to treat only null as “no current element” (so falsy T values remain valid).
  • Add a test ensuring previous() returns null when positioned at the first element even if that element is falsy ('').

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/vs/base/common/history.ts Fixes falsy-element handling by replacing a truthiness check with a null check.
src/vs/base/test/common/history.test.ts Adds a regression test for previous() behavior when the first element is a falsy value.


testObject.first();

assert.deepStrictEqual(testObject.previous(), null);
@Muszic
Sangeet (Muszic) requested a review from Copilot July 29, 2026 06:57
@Muszic

Copy link
Copy Markdown
Contributor Author

Federico Brancasi (@federicobrancasi) Please review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants