Skip to content

Conversation

@lilnasy
Copy link
Contributor

@lilnasy lilnasy commented Nov 27, 2025

  • The root cause of the "iffy" line of code in feat(linter/plugins): implement SourceCode#getLastToken() #16003 was an incorrectly implemented test.
  • The corrected test revealed that the code attempted to index the 0 index of an empty array.
  • This PR fixes both issues.
  • Also took the opportunity to remove a variable declaration in the binary search.

Copilot AI review requested due to automatic review settings November 27, 2025 01:08
@github-actions github-actions bot added A-linter Area - Linter A-cli Area - CLI A-linter-plugins Area - Linter JS plugins C-bug Category - Bug labels Nov 27, 2025
Copilot finished reviewing on behalf of lilnasy November 27, 2025 01:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug in getLastToken() where it incorrectly handled zero-token files (e.g., files containing only comments). The binary search algorithm was refactored to properly handle empty token arrays, and the tests were corrected to use accurate ranges matching the actual source text length.

  • Refactored binary search to initialize lastTokenIndex to 0 and decrement after the loop, eliminating the "iffy" early return
  • Fixed test ranges to match actual source text lengths (e.g., "// comment" is 10 chars, not 11)
  • Removed unnecessary lo variable from binary search by using lastTokenIndex as the lower bound

Reviewed changes

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

File Description
apps/oxlint/src-js/plugins/tokens.ts Refactored binary search to handle zero-token arrays correctly by initializing lastTokenIndex to 0 and decrementing after the loop, removing the conditional early return
apps/oxlint/test/tokens.test.ts Corrected test ranges from incorrect values (using Program range or wrong lengths) to accurate ranges matching the actual source text

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@overlookmotel overlookmotel self-assigned this Nov 27, 2025
@overlookmotel overlookmotel merged commit 123bffe into oxc-project:main Nov 27, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli Area - CLI A-linter Area - Linter A-linter-plugins Area - Linter JS plugins C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants