Skip to content

Commit 0cde999

Browse files
committed
Fix apiQuery reconstruction to preserve quoted phrases byte-for-byte
The previous split(/\s+/) + join approach would break GitHub quoted phrases (e.g. "feature flag" /from.*axios/) by splitting 'feature flag' into two separate tokens and losing the quote grouping. Fix: use q.replace(raw, term).trim() to replace only the regex token in-place, leaving every other character in the original query unchanged. This preserves quoted phrases, extra whitespace handling, and all qualifiers byte-for-byte. Regression test added: '"feature flag" /from.*axios/' → '"feature flag" axios'
1 parent 0a3a77e commit 0cde999

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/regex.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ describe("buildApiQuery — qualifier preservation", () => {
144144
const r = buildApiQuery("path:src/ /useState/");
145145
expect(r.apiQuery).toBe("path:src/ useState");
146146
});
147+
148+
it("preserves quoted phrase alongside regex token", () => {
149+
// Regression: split(/\s+/) would break quoted phrases like \"feature flag\";
150+
// the reconstruction must replace only the regex token, byte-for-byte.
151+
const r = buildApiQuery('"feature flag" /from.*axios/');
152+
expect(r.apiQuery).toBe('"feature flag" axios');
153+
expect(r.regexFilter).not.toBeNull();
154+
expect(r.warn).toBeUndefined();
155+
});
147156
});
148157

149158
describe("buildApiQuery — flags", () => {

src/regex.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,10 @@ export function buildApiQuery(q: string): {
6060
// Derive the API search term from the regex pattern.
6161
const { term, warn } = extractApiTerm(pattern);
6262

63-
// Rebuild the API query by replacing the regex token with the derived term,
64-
// preserving all other tokens (qualifiers and free-text terms alike).
65-
const apiQuery = q
66-
.trim()
67-
.split(/\s+/)
68-
.map((t) => (t === raw ? term : t))
69-
.filter((t) => t.length > 0)
70-
.join(" ")
71-
.trim();
63+
// Rebuild the API query by replacing the regex token in-place, preserving
64+
// all other characters byte-for-byte — including quoted phrases (e.g.
65+
// '"exact match" /pattern/') whose internal whitespace must not be split.
66+
const apiQuery = q.replace(raw, term).trim();
7267

7368
return { apiQuery, regexFilter, warn };
7469
}

0 commit comments

Comments
 (0)