Skip to content

Commit bd3e2e7

Browse files
committed
remove iffy code
1 parent 91a1833 commit bd3e2e7

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

apps/oxlint/src-js/plugins/tokens.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -655,19 +655,17 @@ export function getLastToken(
655655

656656
// Binary search for the last token within `node`'s range
657657
const nodeTokensLength = nodeTokens.length;
658-
let lastTokenIndex = nodeTokensLength;
659-
for (let lo = 0, hi = nodeTokensLength; lo < hi; ) {
660-
const mid = (lo + hi) >> 1;
658+
let lastTokenIndex = 0;
659+
for (let hi = nodeTokensLength; lastTokenIndex < hi; ) {
660+
const mid = (lastTokenIndex + hi) >> 1;
661661
if (nodeTokens[mid].range[0] < rangeEnd) {
662-
lastTokenIndex = mid;
663-
lo = mid + 1;
662+
lastTokenIndex = mid + 1;
664663
} else {
665664
hi = mid;
666665
}
667666
}
668667

669-
// TODO: this early return feels iffy
670-
if (lastTokenIndex === nodeTokensLength) return null;
668+
lastTokenIndex--;
671669

672670
if (typeof filter !== "function") {
673671
if (typeof skip !== "number") return nodeTokens[lastTokenIndex] ?? null;

apps/oxlint/test/tokens.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,8 @@ describe("when calling getLastToken", () => {
892892
resetSourceAndAst();
893893
sourceText = "foo // comment";
894894

895-
expect(getLastToken(Program)!.value).toBe("foo");
895+
// TODO: this verbatim range should be replaced with `ast`
896+
expect(getLastToken({ range: [0, 3] } as Node)!.value).toBe("foo");
896897
resetSourceAndAst();
897898
});
898899

@@ -901,7 +902,8 @@ describe("when calling getLastToken", () => {
901902
sourceText = "// comment";
902903

903904
expect(
904-
getLastToken({ range: [0, 11] } as Node, {
905+
// TODO: this verbatim range should be replaced with `ast`
906+
getLastToken({ range: [0, 10] } as Node, {
905907
filter() {
906908
expect.fail("Unexpected call to filter callback");
907909
},

0 commit comments

Comments
 (0)