Skip to content
Draft
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
22 changes: 22 additions & 0 deletions src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ describe("fetchAllResults", () => {
expect(results[0].textMatches[0].matches[0].col).toBe(1);
});

it("derives segment text from fragment when GitHub omits match text", async () => {
const fakeItem = {
path: "package.json",
html_url: "https://github.com/org/repo/blob/main/package.json",
repository: { full_name: "org/repo", archived: false },
text_matches: [
{
fragment: '{"dependencies":{"react-dom":"18.3.1"}}',
matches: [{ indices: [18, 27] }],
},
],
};
globalThis.fetch = (async () =>
new Response(JSON.stringify({ items: [fakeItem], total_count: 1 }), {
status: 200,
headers: { "content-type": "application/json" },
})) as typeof fetch;

const results = await fetchAllResults("react-dom filename:package.json", "org", "tok");
expect(results[0].textMatches[0].matches[0].text).toBe("react-dom");
});

it("marks archived repos correctly", async () => {
const fakeItem = {
path: "lib/old.ts",
Expand Down
14 changes: 13 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ interface RawCodeItem {
text_matches?: RawTextMatch[];
}

function deriveSegmentText(
fragment: string,
indices: [number, number],
providedText?: string,
): string {
if (providedText !== undefined) return providedText;
const [rawStart, rawEnd] = indices;
const start = Math.max(0, Math.min(rawStart, fragment.length));
const end = Math.max(start, Math.min(rawEnd, fragment.length));
return fragment.slice(start, end);
}

interface SearchCodeResponse {
items: RawCodeItem[];
total_count: number;
Expand Down Expand Up @@ -287,7 +299,7 @@ export async function fetchAllResults(
const indices = seg.indices;
const { line: fragLine, col } = segmentLineCol(fragment, indices[0]);
const line = fragmentStartLine + fragLine - 1;
return { text: seg.text ?? "", indices, line, col };
return { text: deriveSegmentText(fragment, indices, seg.text), indices, line, col };
}),
};
}),
Expand Down
2 changes: 1 addition & 1 deletion src/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ describe("buildJsonOutput — line/col fields", () => {
});

it("omits matchedText when seg.text is an empty string", () => {
// api.ts normalizes missing segment text to "" — matchedText must not be emitted
// Empty segment text should not emit matchedText in JSON output.
const groups: RepoGroup[] = [
{
repoFullName: "myorg/repoA",
Expand Down
Loading