From 5f2a299309a41e59fb3f591308c3ad84e66b74bd Mon Sep 17 00:00:00 2001 From: Arsh <69170106+lilnasy@users.noreply.github.com> Date: Thu, 27 Nov 2025 06:26:18 +0530 Subject: [PATCH] remove iffy code --- apps/oxlint/src-js/plugins/tokens.ts | 12 +++++------- apps/oxlint/test/tokens.test.ts | 6 ++++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/apps/oxlint/src-js/plugins/tokens.ts b/apps/oxlint/src-js/plugins/tokens.ts index aebc2be58718a..9043577317efd 100644 --- a/apps/oxlint/src-js/plugins/tokens.ts +++ b/apps/oxlint/src-js/plugins/tokens.ts @@ -655,19 +655,17 @@ export function getLastToken( // Binary search for the last token within `node`'s range const nodeTokensLength = nodeTokens.length; - let lastTokenIndex = nodeTokensLength; - for (let lo = 0, hi = nodeTokensLength; lo < hi; ) { - const mid = (lo + hi) >> 1; + let lastTokenIndex = 0; + for (let hi = nodeTokensLength; lastTokenIndex < hi; ) { + const mid = (lastTokenIndex + hi) >> 1; if (nodeTokens[mid].range[0] < rangeEnd) { - lastTokenIndex = mid; - lo = mid + 1; + lastTokenIndex = mid + 1; } else { hi = mid; } } - // TODO: this early return feels iffy - if (lastTokenIndex === nodeTokensLength) return null; + lastTokenIndex--; if (typeof filter !== "function") { if (typeof skip !== "number") return nodeTokens[lastTokenIndex] ?? null; diff --git a/apps/oxlint/test/tokens.test.ts b/apps/oxlint/test/tokens.test.ts index 7aab6293f90af..ea143dd6b73de 100644 --- a/apps/oxlint/test/tokens.test.ts +++ b/apps/oxlint/test/tokens.test.ts @@ -892,7 +892,8 @@ describe("when calling getLastToken", () => { resetSourceAndAst(); sourceText = "foo // comment"; - expect(getLastToken(Program)!.value).toBe("foo"); + // TODO: this verbatim range should be replaced with `ast` + expect(getLastToken({ range: [0, 3] } as Node)!.value).toBe("foo"); resetSourceAndAst(); }); @@ -901,7 +902,8 @@ describe("when calling getLastToken", () => { sourceText = "// comment"; expect( - getLastToken({ range: [0, 11] } as Node, { + // TODO: this verbatim range should be replaced with `ast` + getLastToken({ range: [0, 10] } as Node, { filter() { expect.fail("Unexpected call to filter callback"); },