Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions apps/oxlint/src-js/plugins/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions apps/oxlint/test/tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand All @@ -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");
},
Expand Down
Loading